mirror of
https://github.com/urbit/developers.urbit.org.git
synced 2024-08-16 17:20:35 +03:00
build: add buildGlossary.js, run on build
This commit is contained in:
parent
da0f85abdb
commit
2ee13cf066
59
lib/buildGlossary.js
Normal file
59
lib/buildGlossary.js
Normal 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.");
|
||||
});
|
@ -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",
|
||||
|
Loading…
Reference in New Issue
Block a user