build: add buildGlossary.js, run on build

This commit is contained in:
Matilde Park 2022-10-18 20:37:07 -07:00
parent da0f85abdb
commit 2ee13cf066
2 changed files with 61 additions and 1 deletions

59
lib/buildGlossary.js Normal file
View File

@ -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.");
});

View File

@ -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",