Skip to content

Commit 707b1ee

Browse files
committed
refactor(website): generate blog navigation from post front matter
Instead of writing out the blog navigation by hand, generate the nav by reading the front matter of each blog post. This will make writing future blog posts easier and less bug-prone.
1 parent 69bcd86 commit 707b1ee

File tree

8 files changed

+24
-32
lines changed

8 files changed

+24
-32
lines changed

website/public/blog/bug-journey/index.ejs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"title": "Sometimes it *is* a compiler bug",
33
"linkLabelHTML": "Sometimes it <em>is</em> a compiler bug",
44
"description": "My journey in finding and fixing a bug in a C++ toolchain",
5+
"navTitle": "Compiler bug",
56
"blogDate": "2022-05-25T21:04:02-07:00"
67
}--->
78

website/public/blog/cpp-vs-rust-build-times/index.ejs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!---{
22
"title": "Is coding in Rust as bad as in C++?",
33
"description": "A practical comparison of build and test speed between C++ and Rust.",
4+
"navTitle": "C++ vs Rust",
45
"image": "/blog/cpp-vs-rust-build-times/cpp-vs-rust.jpg",
56
"blogDate": "2023-01-05T21:32:37-08:00"
67
}--->

website/public/blog/index.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export async function loadBlogPostsAsync() {
4242
if (typeof meta.blogDate === "undefined") {
4343
throw new Error(`Missing blogDate in front matter of ${indexPath}`);
4444
}
45+
if (typeof meta.navTitle === "undefined") {
46+
throw new Error(`Missing navTitle in front matter of ${indexPath}`);
47+
}
4548
return {
4649
dir: dir.name,
4750
meta: meta,

website/public/blog/show-js-errors-neovim-macos/index.ejs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!---{
22
"title": "Show JavaScript errors in Neovim on macOS",
33
"description": "How to install Homebrew, Neovim, and quick-lint-js on macOS to show JavaScript syntax errors while editing",
4+
"navTitle": "Neovim for JS",
45
"image": "/blog/show-js-errors-neovim-macos/neovim-demo.png",
56
"blogDate": "2022-02-16T21:48:38-08:00"
67
}--->

website/public/blog/syntax-errors-2021/index.ejs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!---{
22
"title": "JavaScript syntax errors compared (2021)",
33
"description": "Comparison of errors produced by different JavaScript parsers",
4+
"navTitle": "JS errors",
45
"blogDate": "2021-12-11T17:19:10-08:00"
56
}--->
67

website/public/blog/version-1.0/index.ejs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"title": "Faster, easier, friendlier: how quick-lint-js will take over ESLint",
33
"linkLabelHTML": "Release: version 1.0",
44
"description": "Faster, easier, friendlier: how quick-lint-js will take over ESLint",
5+
"navTitle": "Release 1.0",
56
"blogDate": "2021-12-13T19:27:37-08:00"
67
}--->
78

website/public/blog/version-2.0/index.ejs.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"title": "Reacts rocks with quick-lint-js",
33
"linkLabelHTML": "Release: version 2.0",
44
"description": "We are proud to announce version 2.0 of quick-lint-js!",
5+
"navTitle": "Release 2.0",
56
"blogDate": "2022-02-04T21:43:20-08:00"
67
}--->
78

website/public/common-nav.ejs.html

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -235,38 +235,7 @@
235235
{
236236
uri: "/blog/",
237237
title: "Blog",
238-
subpages: [
239-
{
240-
uri: "/blog/syntax-errors-2021/",
241-
title: "JS errors",
242-
hidden: true
243-
},
244-
{
245-
uri: "/blog/version-1.0/",
246-
title: "Release 1.0",
247-
hidden: true
248-
},
249-
{
250-
uri: "/blog/version-2.0/",
251-
title: "Release 2.0",
252-
hidden: true
253-
},
254-
{
255-
uri: "/blog/show-js-errors-neovim-macos/",
256-
title: "Neovim for JS",
257-
hidden: true
258-
},
259-
{
260-
uri: "/blog/bug-journey/",
261-
title: "Compiler bug",
262-
hidden: true
263-
},
264-
{
265-
uri: "/blog/cpp-vs-rust-build-times/",
266-
title: "C++ vs Rust",
267-
hidden: true
268-
},
269-
]
238+
subpages: await loadBlogSubpagesIfNeededAsync(),
270239
},
271240
];
272241

@@ -293,6 +262,20 @@
293262
throw new Error("Malformed page or subpage: " + JSON.stringify(subpage));
294263
}
295264

265+
async function loadBlogSubpagesIfNeededAsync() {
266+
if (currentURI.startsWith("/blog/")) {
267+
let { loadBlogPostsAsync } = await importFileAsync("./blog/index.mjs");
268+
let blogPosts = await loadBlogPostsAsync();
269+
return blogPosts.map(post => ({
270+
uri: `/blog/${post.dir}/`,
271+
title: post.meta.navTitle ?? post.meta.title,
272+
hidden: true,
273+
}));
274+
} else {
275+
return [];
276+
}
277+
}
278+
296279
function isOnPageOrSubpage(page) {
297280
return page.uri === currentURI || page.subpages.some(subpage => isOnSubpage(subpage));
298281
}

0 commit comments

Comments
 (0)