diff --git a/lib/buildGlossary.js b/lib/buildGlossary.js new file mode 100644 index 0000000..ef5fc63 --- /dev/null +++ b/lib/buildGlossary.js @@ -0,0 +1,59 @@ +const fs = require("fs"); +const path = require("path"); +const matter = require("gray-matter"); +const toml = require("@iarna/toml"); + +const options = { + engines: { + toml: toml.parse.bind(toml), + }, + language: "toml", + delimiters: "+++", +}; + +const glossary = []; + +function buildGlossary(dir) { + const children = fs.readdirSync(dir, { withFileTypes: true }); + const pages = children.filter((f) => f.isFile() && f.name !== "_index.md"); + const subdirs = children.filter( + (f) => f.isDirectory() + ); + pages.map((page) => { + const { data } = matter(fs.readFileSync(path.join(dir, page.name)), options); + const glossaryEntry = data.glossaryEntry + ? Object.values(data.glossaryEntry) + .map((e) => ({ + ...e, ...{ + url: "https://developers.urbit.org" + path.join( + dir.substr(dir.indexOf("content") + 7), + "/", + page.name.replace(/.md$/, "") + ) + } + })) + : []; + glossaryEntry.forEach((entry) => glossary.push(entry)); + }); + + Object.fromEntries( + subdirs.map((subdir) => [ + subdir.name, + buildGlossary(path.join(dir, subdir.name)), + ]) + ); +} + +buildGlossary(path.join(process.cwd(), "content")); +const fileContents = `export const index = ${JSON.stringify(glossary)}`; + +try { + fs.readdirSync("cache"); +} catch (err) { + fs.mkdirSync("cache"); +} + +fs.writeFile("cache/glossary.js", fileContents, function (err) { + if (err) return console.error(err); + console.log("Site glossary created."); +}); diff --git a/package.json b/package.json index 444d5e3..83d3f62 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "private": true, "scripts": { "dev": "npm run prebuild && next dev", - "prebuild": "npm run cache-posts && npm run guide && npm run overview && npm run reference", + "prebuild": "npm run cache-posts && npm run glossary && npm run guide && npm run overview && npm run reference", "cache-posts": "node lib/cache.js", + "glossary": "node lib/buildGlossary.js", "guide": "node lib/buildPageTree guides weight", "overview": "node lib/buildPageTree overview weight", "reference": "node lib/buildPageTree reference weight",