From 283887a8cfda2d509f6bcd6dadca919c3c874e61 Mon Sep 17 00:00:00 2001 From: ikoamu Date: Sun, 7 Apr 2024 19:42:59 +0900 Subject: feat generate searchdata script --- generate_searchdata.mjs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 generate_searchdata.mjs (limited to 'generate_searchdata.mjs') diff --git a/generate_searchdata.mjs b/generate_searchdata.mjs new file mode 100644 index 0000000..c226e03 --- /dev/null +++ b/generate_searchdata.mjs @@ -0,0 +1,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!'); + }); + }); +}); + -- cgit