-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.mjs
167 lines (151 loc) · 5 KB
/
build.mjs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import fs from 'node:fs/promises'
import MarkdownIt from 'markdown-it'
import MarkdownItHighlightjs from 'markdown-it-highlightjs'
import Yaml from 'yaml'
import pug from 'pug'
const markdown = MarkdownIt({
html: true,
})
markdown.use(MarkdownItHighlightjs, {auto: false, code: false, inline: true})
const DATE_FORMATTER = new Intl.DateTimeFormat('en-US', {dateStyle: 'long'})
const urlize = str => str.replaceAll(/ /g, '-').replaceAll(/[:'<>]/g, '')
const RENDER_TYPE = {
html: text => text,
md: text => markdown.render(text),
}
await fs.rm('./public', {force: true, recursive: true})
await fs.mkdir('./public')
await fs.mkdir('./public/blog')
await fs.mkdir('./public/blog/post')
await fs.mkdir('./public/blog/css')
await fs.link('./theme/style.css', './public/blog/css/style.css')
await fs.link('./redirects.conf', './public/blog/redirects.conf')
async function read_post_entry(filename) {
const filematch = filename.match(/^(?<date>[0-9]{4}-[0-9]{2}-[0-9]{2})-(?<slug>[^.]+)(?:\.(?<ext>html|md))?$/)
if (!filematch) throw new Error('failed to match on post name')
const {date, slug, ext} = filematch.groups
let postfile = `${postdir}/${filename}`
let assets = []
if (ext === undefined) {
let found = false
for (const subfile of await fs.readdir(postfile)) {
if (subfile === `${date}-${slug}.md` || subfile === `${date}-${slug}.html`) {
if (found) throw new Error('Found multiple post files')
postfile += '/'+subfile
found = true
} else {
assets.push({path: `${postdir}/${filename}/${subfile}`, filename: subfile})
}
}
if (!found) throw new Error('Failed to find post file')
}
const filetype = postfile.split('.').at(-1)
if (!(filetype in RENDER_TYPE)) throw new Error('unknown file type')
return {date, slug, filetype, filename: postfile, assets}
}
async function parse_post(post) {
const lines = (await fs.readFile(post.filename, 'utf-8')).split('\n')
let mode = 'start', frontmatter_raw = '', body_truncated = '', body = ''
for (const line of lines) {
if (mode === 'start') {
if (line !== '---') throw new Error('missing front matter start indicator')
mode = 'frontmatter'
} else if (mode === 'frontmatter') {
if (line === '---') {
mode = 'body'
} else {
frontmatter_raw += line+'\n'
}
} else if (mode === 'body') {
if (line === '<!--more-->') {
body += '\n'
mode = 'body_truncated'
} else {
body += line+'\n'
body_truncated += line+'\n'
}
} else if (mode === 'body_truncated') {
body += line+'\n'
} else {
throw new Error("can't happen")
}
}
if (mode !== 'body' && mode !== 'body_truncated') throw new Error('failed to enter body mode')
const frontmatter = Yaml.parse(frontmatter_raw)
if ((!frontmatter.short) !== (mode === 'body_truncated')) throw new Error('short flag does not match truncation')
const render = RENDER_TYPE[post.filetype]
return {
...post,
tags: [], // TODO default in case frontmatter does not define
...frontmatter,
htmlBody: render(body),
htmlSummary: render(body_truncated),
}
}
const postdir = './posts'
const posts = await Promise.all((await fs.readdir(postdir))
.map(async filename => {
try {
return parse_post(await read_post_entry(filename))
} catch (e) {
console.error(`error in ${filename}:`)
throw e
}
}))
posts.sort((a, b) => a.date > b.date ? -1 : 1)
const posts_by_tag = {}
for (const post of posts) {
for (const tag of post.tags) {
(posts_by_tag[tag] ||= []).push(post)
}
}
const templateGlobals = {
ENV: process.env.ENV,
asset: f => `/blog/${f}`,
urlize,
formatDate: d => DATE_FORMATTER.format(new Date(d+'T00:00:00')),
}
const renderPost = pug.compileFile(`./theme/post.pug`)
for (const post of posts) {
const html = renderPost({
post,
...templateGlobals,
})
await fs.mkdir(`public/blog/post/${post.slug}`, {recursive: true}) // NOTE: recursive only to suppress EEXIST
await fs.writeFile(`public/blog/post/${post.slug}/index.html`, html)
for (const asset of post.assets) {
await fs.cp(asset.path, `public/blog/post/${post.slug}/${asset.filename}`, {recursive: true, preserveTimestamps: true})
}
}
const renderTag = pug.compileFile(`./theme/tag.pug`)
for (const [tag, posts] of Object.entries(posts_by_tag)) {
const html = renderTag({
tag,
posts,
...templateGlobals,
})
await fs.mkdir(`public/blog/tags/${urlize(tag)}`, {recursive: true}) // NOTE: recursive only to suppress EEXIST
await fs.writeFile(`public/blog/tags/${urlize(tag)}/index.html`, html)
}
await fs.writeFile(`public/blog/index.html`, pug.renderFile(`./theme/index.pug`, {
posts,
...templateGlobals,
}))
await fs.writeFile(`public/blog/index.xml`, pug.renderFile(`./theme/index.xml.pug`, {
posts,
...templateGlobals,
}))
await fs.writeFile(`public/blog/tags/index.html`, pug.renderFile(`./theme/tags.pug`, {
posts,
posts_by_tag,
...templateGlobals,
}))
await fs.writeFile(`public/blog/sitemap.xml`, pug.renderFile(`./theme/sitemap.xml.pug`, {
posts,
posts_by_tag,
...templateGlobals,
}))
await fs.writeFile(`public/blog/summary.html`, pug.renderFile(`./theme/index-summary.pug`, {
posts,
...templateGlobals,
}))