aboutsummaryrefslogtreecommitdiff
path: root/generate_searchdata.mjs
blob: c226e03513edff27493da65d505cfc4962b791e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import fs from 'fs';
import { unified }  from 'unified';
import uniorgParse from 'uniorg-parse';
import { toString } from 'orgast-util-to-string';

fs.readFile('graphdata.json', 'utf8', (_, data) => {
  const graphdata = JSON.parse(data);
  const nodes = graphdata.data.nodes; 
  const processer = unified().use(uniorgParse);
  const searchData = [];

  fs.readdir('notes', (_, files) => {
    files.forEach(file => {
      const org = fs.readFileSync(`notes/${file}`, 'utf8');
      const tree = processer.parse(org);
      const filtered = tree.children.filter(
        node => node.type !== 'property-drawer' && node.type !== 'keyword'
      );
      const content = toString({
        ...tree,
        children: filtered,
      });
      const node = nodes.find(n => n.id === file);
      if (node) {
        searchData.push({
          id: node.id,
          title: node.title,
          tags: node.tags[0] ? node.tags : null,
          content,
        });
      }
    });
    fs.writeFile('searchdata.json', JSON.stringify(searchData), (err) => {
      if (err) throw err;
      console.log('The file has been saved!');
    });
  });
});