ref: update semtree; update semtree impl.

This commit is contained in:
manunamz
2024-09-02 11:56:15 -04:00
parent 7d7eddfa73
commit 06ebc36941
3 changed files with 66 additions and 46 deletions
+1 -1
View File
@@ -28,7 +28,7 @@
"gray-matter": "^4.0.3",
"remark-caml": "^0.0.6-rm",
"remark-wikirefs": "^0.0.7-rm",
"semtree": "^0.0.4",
"semtree": "^0.0.12",
"unist-util-select": "^5.0.0",
"wikirefs": "^0.0.4"
}
+50 -39
View File
@@ -1,14 +1,17 @@
import type { SemTree } from 'semtree';
import { getCollection } from 'astro:content';
import path from 'path';
import { SemTree } from 'semtree';
import * as semtree from 'semtree';
export async function buildBonsai() {
export async function buildBonsai(): Promise<SemTree | undefined> {
// init vars
const bonsai = new SemTree({
const opts = {
// if set to 'false', make sure to extract urls of index docs below
virtualTrunk: true,
});
// semtree options: https://github.com/wikibonsai/semtree?tab=readme-ov-file#options
};
const bonsaiText: any = {}; // { filename: content } hash
const rootFilename: string = 'i.bonsai';
// build 'bonsaiText' hash
@@ -16,46 +19,54 @@ export async function buildBonsai() {
allIndexDocs.forEach((doc: any) => { // remove preceding/trailing newlines/whitespace
bonsaiText[path.basename(doc.id, '.md')] = doc.body.replace(/^\s+|\s+$/g, '');
});
let res;
let bonsai: SemTree | string = 'uninitialized bonsai';
try {
// build bonsai tree data struct
res = bonsai.parse(bonsaiText, rootFilename);
// append url for template rendering and init fam metadata
const allEntryDocs = await getCollection('entries');
for (const node of bonsai.nodes) {
const doc: any = allEntryDocs.find((doc) => path.basename(doc.id, '.md') == node.text);
if (doc !== undefined) {
node.url = '/entries/' + doc.slug;
bonsai = semtree.create(rootFilename, bonsaiText, opts);
if (typeof bonsai === 'string') {
throw new Error(bonsai);
} else {
// append url for template rendering and init fam metadata
const allEntryDocs = await getCollection('entries');
for (const node of bonsai.nodes) {
const doc: any = allEntryDocs.find((doc) => path.basename(doc.id, '.md') == node.text);
if (doc !== undefined) {
node.url = '/entries/' + doc.slug;
}
}
// uncomment if 'virtualTrunk' is set to 'false'
// for (const node of bonsai.nodes) {
// const doc: any = allIndexDocs.find((doc) => path.basename(doc.id, '.md') == node.text);
// if (doc !== undefined) {
// node.url = '/index/' + doc.slug;
// }
// }
// uncomment in case blog posts are desired on the #tag map
// const allBlogDocs = await getCollection('blog');
// for (const node of bonsai.nodes) {
// const doc: any = allBlogDocs.find((doc) => path.basename(doc.id, '.md') == node.text);
// if (doc !== undefined) {
// node.url = '/blog/' + doc.slug;
// }
// }
console.log('bonsai: \n'
+ '\n---\n'
+ 'root: ' + bonsai.root
+ '\n---\n'
+ 'trunk: ' + bonsai.trunk
+ '\n---\n'
+ 'petioleMap: ' + JSON.stringify(bonsai.petioleMap)
+ '\n---\n'
+ 'orphans: ' + bonsai.orphans
+ '\n---\n'
+ 'nodes: ' + JSON.stringify(bonsai.nodes)
+ '\n---\n'
);
return bonsai;
}
// uncomment if 'virtualTrunk' is set to 'false'
// for (const node of bonsai.nodes) {
// const doc: any = allIndexDocs.find((doc) => path.basename(doc.id, '.md') == node.text);
// if (doc !== undefined) {
// node.url = '/index/' + doc.slug;
// }
// }
// uncomment in case blog posts are desired on the #tag map
// const allBlogDocs = await getCollection('blog');
// for (const node of bonsai.nodes) {
// const doc: any = allBlogDocs.find((doc) => path.basename(doc.id, '.md') == node.text);
// if (doc !== undefined) {
// node.url = '/blog/' + doc.slug;
// }
// }
return bonsai;
} catch (e) {
console.error(e, res);
}
if (bonsai.duplicates.length > 0) {
console.log('bonsai duplicates: ' + bonsai.duplicates);
} else {
console.log('bonsai: \n'
+ 'res: ' + JSON.stringify(res) + '\n'
+ 'root: ' + bonsai.root + '\n'
+ 'duplicates: ' + bonsai.duplicates
);
console.error(e, bonsai);
}
}
export const bonsai = await buildBonsai();
export const bonsai: SemTree | undefined = await buildBonsai();
+15 -6
View File
@@ -2676,7 +2676,7 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
nanoid@^3.3.4, nanoid@^3.3.6:
nanoid@^3.3.6:
version "3.3.6"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.6.tgz#443380c856d6e9f9824267d960b4236ad583ea4c"
integrity sha512-BGcqMMJuToF7i1rt+2PWSNVnWIkGCU78jBG3RxO/bZlnZPK2Cmi2QaffxGO/2RvWi9sL+FAiRiXMgsyxQ1DIDA==
@@ -3211,12 +3211,14 @@ section-matter@^1.0.0:
extend-shallow "^2.0.1"
kind-of "^6.0.0"
semtree@^0.0.4:
version "0.0.4"
resolved "https://registry.yarnpkg.com/semtree/-/semtree-0.0.4.tgz#2c22727524ad4af4e5c8758a7a7908a79b627abf"
integrity sha512-JUGAa9KLpFLMvPzR1+OA++wL6tGJN71LL5Z6KT5YJVYUn9kBUv2FY5SYvo4tR5aLssO+BWDyoXyMy416SeZawA==
semtree@^0.0.12:
version "0.0.12"
resolved "https://registry.yarnpkg.com/semtree/-/semtree-0.0.12.tgz#a942285774b37862383fb51604359cee49d8a477"
integrity sha512-CAsJk4DwmVcr8hsxSXYH3rQNVkr0tAepO5FnJ3T3v8unZ6hhWKI0PWmGiaIPx+S/uti8BExUZDpA3jmQXij9Tg==
dependencies:
nanoid "^3.3.4"
caml-mkdn "^0.0.2"
gray-matter "^4.0.3"
wikirefs "^0.0.5"
semver@^6.3.1:
version "6.3.1"
@@ -3821,6 +3823,13 @@ wikirefs@^0.0.4:
dependencies:
escape-mkdn "^0.0.3"
wikirefs@^0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/wikirefs/-/wikirefs-0.0.5.tgz#78a097a72ac6a0a5215c9be753c9e27643d70a17"
integrity sha512-wxGl7+XC+f4/dfhf9Z2J+dozgHDZoKvX6UpHRohQyTKDHIxfFMdoN3pDx9ySFNG5vC0xQXF9iIfPhLUwKXjXVg==
dependencies:
escape-mkdn "^0.0.3"
wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214"