-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
131 lines (118 loc) · 3.64 KB
/
utils.js
File metadata and controls
131 lines (118 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import path from "node:path";
import {writeFileSync, readdirSync, readFileSync} from "node:fs";
import {mkdir} from "node:fs/promises";
import nunjucks from "nunjucks";
import {marked} from "marked";
import JSON5 from "json5";
const njk = new nunjucks.Environment(
new nunjucks.FileSystemLoader("src/pages")
);
njk.addFilter("tokenizeHeading", (heading) => {
return encodeURI(heading.toLowerCase().split(" ").join("_"));
});
njk.addFilter("formatDate", (dateString) => {
const date = new Date(dateString);
return date.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
});
marked.use({
renderer: {
heading(token) {
const id = encodeURI(token.text.toLowerCase().split(" ").join("_"));
if (token.depth === 1) return `<h1 id="${id}">${token.text}</h1>\n`;
return `<h${token.depth} class="heading" id="${id}">${token.text}<button type="button" data-anchor="${id}"><svg aria-hidden="true" viewBox="0 0 24 24">
<title aria-hidden="true">Copy Anchor “${token.text}”</title>
<use href="/assets/icons.svg#link"></use>
</svg>
</button>
</h${token.depth}>\n`;
},
},
});
export class SiteBuilder {
constructor() {
this.routes = [];
this.collections = new Map();
this.njk = njk;
}
collection = (dir, template, group, clause) => {
const files = readdirSync(dir).map((file) => path.join(dir, file));
const items = files
.map((file) => {
const ext = path.extname(file);
const slug = file.replace(ext, "").replace("content", "");
const source = readFileSync(file, "utf-8");
const {data, content} = parseFrontmatter(source);
return {slug, ext, content, ...data};
})
.filter(clause);
this.collections.set(group, {items, template, group});
};
page = (path, handler) => {
this.routes.push({path, handler});
};
build = async () => {
for (const collection of this.collections.values()) {
for (const {ext, content, slug, ...data} of collection.items) {
const handler = this.getHandler(ext);
this.page(slug + "/", () =>
handler(
{content, group: collection.group, ...data, slug},
collection.template
)
);
}
}
for (const {path, handler} of this.routes) {
const filePath = this.createFilePath(path);
const content = await handler();
await this.writeFile(filePath, content);
}
};
createFilePath = (path) => {
if (path === "/") return "dist/index.html";
if (path.endsWith("/")) return `dist${path}index.html`;
return `dist${path}.html`;
};
writeFile = async (path, content) => {
const dir = path.substring(0, path.lastIndexOf("/"));
await mkdir(dir, {recursive: true});
writeFileSync(path, content);
console.log(`↝ ${path}`);
};
getHandler = (ext) => {
if (ext === ".md") {
return mdRenderer;
}
return jinjaRenderer;
};
}
export function parseFrontmatter(source) {
const lines = source.split("\n");
if (!lines[0].startsWith("+++")) {
return {data: {}, content: source};
}
const closingIndex = lines.slice(1).findIndex((line) => line === "+++");
if (closingIndex === -1) {
return {data: {}, content: source};
}
const frontmatter = lines.slice(1, closingIndex + 1).join("\n");
const content = lines.slice(closingIndex + 2).join("\n");
if (lines[0].endsWith("json")) {
const data = JSON5.parse(frontmatter);
return {data, content};
}
return {data: {}, content};
}
export function mdRenderer(context, template) {
const {content, ...data} = context;
const md = njk.renderString(content, data);
const parsed = marked.parse(md);
return jinjaRenderer({content: parsed, ...data}, template);
}
export function jinjaRenderer(context, template = "_includes/base.html") {
return njk.render(template, context);
}