-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
87 lines (73 loc) · 2.45 KB
/
build.js
File metadata and controls
87 lines (73 loc) · 2.45 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
import { basename, join } from 'node:path';
/**
* Parse frontmatter and content from an HTML file.
* @param {string} raw - The raw HTML file content.
* @returns {{ meta: Object, content: string }} Parsed metadata and content.
*/
export function parse(raw) {
const meta = {};
const frontmatter = /^<!---\n([\s\S]*?)\n--->/.exec(raw);
if (!frontmatter) {
return { meta, content: raw };
}
for (const line of frontmatter[1].split('\n')) {
const [key, ...rest] = line.split(':');
meta[key.trim()] = rest.join(':').trim();
}
return {
meta,
content: raw.slice(frontmatter[0].length).trim()
};
}
/**
* Render a page by replacing layout placeholders with content and metadata.
* @param {string} layout - The layout HTML template.
* @param {Object} meta - Key-value metadata from frontmatter.
* @param {string} content - The page content.
* @returns {string} The rendered HTML.
*/
export function render(layout, meta, content) {
let output = layout.replace('<!--- content --->', content);
for (const [key, value] of Object.entries(meta)) {
output = output.replace(`<!--- ${key} --->`, value);
}
return output;
}
/**
* Build all HTML pages using layout templates.
* @param {function({ frontmatter: Object, content: string }): string} [callback]
* Optional transform function. Receives frontmatter and content for each page.
* Return a string to replace the content, or undefined to keep the original.
*/
export async function build(callback) {
const glob = new Bun.Glob('**/*.html');
const layouts = {};
const pages = [];
for await (const path of glob.scan('.')) {
if (path.startsWith('dist/')) {
continue;
}
if (/^layout\..*\.html$/.test(basename(path))) {
const name = basename(path).match(/^layout\.(.*)\.html$/)[1];
layouts[name] = await Bun.file(path).text();
} else {
pages.push(path);
}
}
for (const path of pages) {
const raw = await Bun.file(path).text();
let { meta, content } = parse(raw);
const layoutName = meta.layout || 'default';
delete meta.layout;
const layout = layouts[layoutName];
if (!layout) {
console.error(`No layout file named "layout.${layoutName}.html" found for ${path}`);
continue;
}
if (typeof callback === 'function') {
content = callback({ frontmatter: meta, content }) ?? content;
}
const outPath = join('dist', path);
await Bun.write(outPath, render(layout, meta, content));
}
}