on aadd412 help sign in
urbit.org/lib/cache.js
75 lines 1.8 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 index = [];
16function buildSearchIndex(dir) {
17 const dirName = dir.split("/").slice(-1).join("");
18 let metadata = {
19 data: {
20 title: dirName.charAt(0).toUpperCase() + dirName.slice(1),
21 },
22 };
24 try {
25 metadata = matter(fs.readFileSync(path.join(dir, "_index.md")), options);
26 } catch (err) {}
28 const children = fs.readdirSync(dir, { withFileTypes: true });
29 const pages = children.filter((f) => f.isFile() && f.name !== "_index.md");
30 const subdirs = children.filter(
31 (f) => f.isDirectory() && f.name !== "ids" && f.name !== "glossary"
32 );
34 index.push(
35 ...pages.map((page) => {
36 const { data: pageData, content: pageContent } = matter(
37 fs.readFileSync(path.join(dir, page.name)),
38 options
39 );
40 return {
41 title: pageData.title,
42 base: page.name,
43 slug: path.join(
44 dir.substr(dir.indexOf("content") + 7),
45 "/",
46 page.name.replace(/.md$/, "")
47 ),
48 content: pageData.description || "",
49 parent: metadata.data.title,
50 };
51 })
52 );
54 Object.fromEntries(
55 subdirs.map((subdir) => [
56 subdir.name,
57 buildSearchIndex(path.join(dir, subdir.name)),
58 ])
59 );
62buildSearchIndex(path.join(process.cwd(), "content"));
64const fileContents = `export const index = ${JSON.stringify(index)}`;
66try {
67 fs.readdirSync("cache");
68} catch (err) {
69 fs.mkdirSync("cache");
72fs.writeFile("cache/data.js", fileContents, function (err) {
73 if (err) return console.error(err);
74 console.log("Site index created.");
75});