on master help sign in
urbit.org/lib/buildGlossary.js
59 lines 1.7 kB history raw
1const fs = require("fs");
2const path = require("path");
3const matter = require("gray-matter");
4const toml = require("@iarna/toml");
6const options = {
7 engines: {
8 toml: toml.parse.bind(toml),
9 },
10 language: "toml",
11 delimiters: "+++",
12};
14const glossary = [];
16function buildGlossary(dir) {
17 const children = fs.readdirSync(dir, { withFileTypes: true });
18 const pages = children.filter((f) => f.isFile() && f.name !== "_index.md");
19 const subdirs = children.filter(
20 (f) => f.isDirectory()
21 );
22 pages.map((page) => {
23 const { data } = matter(fs.readFileSync(path.join(dir, page.name)), options);
24 const glossaryEntry = data.glossaryEntry
25 ? Object.values(data.glossaryEntry)
26 .map((e) => ({
27 ...e, ...{
28 url: "https://urbit.org" + path.join(
29 dir.substr(dir.indexOf("content") + 7),
30 "/",
31 page.name.replace(/.md$/, "")
32 )
33 }
34 }))
35 : [];
36 glossaryEntry.forEach((entry) => glossary.push(entry));
37 });
39 Object.fromEntries(
40 subdirs.map((subdir) => [
41 subdir.name,
42 buildGlossary(path.join(dir, subdir.name)),
43 ])
44 );
47buildGlossary(path.join(process.cwd(), "content"));
48const fileContents = `export const glossary = ${JSON.stringify(glossary)}`;
50try {
51 fs.readdirSync("cache");
52} catch (err) {
53 fs.mkdirSync("cache");
56fs.writeFile("cache/glossary.js", fileContents, function (err) {
57 if (err) return console.error(err);
58 console.log("Site glossary created.");
59});