-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_searchdata.mjs
39 lines (36 loc) · 1.12 KB
/
generate_searchdata.mjs
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
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!');
});
});
});