aboutsummaryrefslogtreecommitdiff
path: root/generate_searchdata.mjs
diff options
context:
space:
mode:
authorikoamu <ikoamu@gmail.com>2024-04-07 19:42:59 +0900
committerikoamu <ikoamu@gmail.com>2024-04-07 19:42:59 +0900
commit283887a8cfda2d509f6bcd6dadca919c3c874e61 (patch)
treef9ee2abcc855f070e0e09f75d62cfd6839364128 /generate_searchdata.mjs
parent5bd6bb734c2781d9809e3ab621127b3bddf56d99 (diff)
feat generate searchdata script
Diffstat (limited to 'generate_searchdata.mjs')
-rw-r--r--generate_searchdata.mjs39
1 files changed, 39 insertions, 0 deletions
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!');
+ });
+ });
+});
+