on master help sign in
build: add glossary on build script
commit6d54701a96e6db26c94b49db28841ff2b8d5ea79 authorMatilde Park <matilde.park@urbit.org> dateWed 19 Oct 2022, 12:24 (4 years ago) parentec5c661
2 files changed+61−1
lib/buildGlossary.js+59 package.json+2−1
lib/buildGlossary.jsnew file+59
@@ -0,0 +1,59 @@
1+const fs = require("fs");
2+const path = require("path");
3+const matter = require("gray-matter");
4+const toml = require("@iarna/toml");
5+
6+const options = {
7+ engines: {
8+ toml: toml.parse.bind(toml),
9+ },
10+ language: "toml",
11+ delimiters: "+++",
12+};
13+
14+const glossary = [];
15+
16+function 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+ });
38+
39+ Object.fromEntries(
40+ subdirs.map((subdir) => [
41+ subdir.name,
42+ buildGlossary(path.join(dir, subdir.name)),
43+ ])
44+ );
45+}
46+
47+buildGlossary(path.join(process.cwd(), "content"));
48+const fileContents = `export const glossary = ${JSON.stringify(glossary)}`;
49+
50+try {
51+ fs.readdirSync("cache");
52+} catch (err) {
53+ fs.mkdirSync("cache");
54+}
55+
56+fs.writeFile("cache/glossary.js", fileContents, function (err) {
57+ if (err) return console.error(err);
58+ console.log("Site glossary created.");
59+});
package.json+2−1
@@ -7,11 +7,12 @@
77 "build": "next build",
88 "export": "npm run prebuild && next build && next export",
99 "start": "next start",
10− "prebuild": "npm run cache-posts && npm run rss && npm run community-tree && npm run getting-started-tree && npm run overview-tree",
10+ "prebuild": "npm run cache-posts && npm run rss && npm run glossary && npm run community-tree && npm run getting-started-tree && npm run overview-tree",
1111 "community-tree": "node lib/buildPageTree community weight",
1212 "getting-started-tree": "node lib/buildPageTree getting-started weight",
1313 "overview-tree": "node lib/buildPageTree overview weight",
1414 "cache-posts": "node lib/cache.js",
15+ "glossary": "node lib/buildGlossary.js",
1516 "rss": "node lib/rss.js",
1617 "vercel-build": "yum install libuuid-devel libmount-devel && cp /lib64/{libuuid,libmount,libblkid}.so.1 node_modules/canvas/build/Release/ && npm run build"
1718 },