Skip to content

Commit a0d2a53

Browse files
committed
refactor(website): move loadBlogPostsAsync into a reusable .mjs file
1 parent 3db5cac commit a0d2a53

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

website/public/blog/index.ejs.html

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,7 @@
1212
<script>
1313
//<%
1414
let ejs = await import("ejs");
15-
let fs = await import("node:fs");
16-
let path = await import("node:path");
17-
let { readFrontMatterFromFileAsync } = await importFileAsync("../../src/front-matter.mjs");
18-
19-
async function loadBlogPostsAsync() {
20-
let directories = await fs.promises.readdir(absoluteFilePath('.'), {withFileTypes: true});
21-
directories = directories.filter(dir => dir.isDirectory());
22-
let posts = await Promise.all(directories.map(async dir => {
23-
let indexPath = absoluteFilePath(path.join(dir.name, "index.ejs.html"));
24-
let meta = await readFrontMatterFromFileAsync(indexPath);
25-
if (typeof meta.blogDate === 'undefined') {
26-
throw new Error(`Missing blogDate in front matter of ${indexPath}`);
27-
}
28-
return {
29-
dir: dir.name,
30-
meta: meta,
31-
};
32-
}));
33-
sortPostsReverseChronologically(posts);
34-
return posts;
35-
}
36-
37-
function sortPostsReverseChronologically(posts) {
38-
posts.sort((a, b) => {
39-
if (a.meta.blogDate < b.meta.blogDate) return +1;
40-
if (a.meta.blogDate > b.meta.blogDate) return -1;
41-
return 0;
42-
});
43-
}
15+
let { loadBlogPostsAsync } = await importFileAsync("./index.mjs");
4416
//%>
4517
</script>
4618
<link href="../main.css" rel="stylesheet" />

website/public/blog/index.mjs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
// Copyright (C) 2020 Matthew "strager" Glazar
22
// See end of file for extended copyright information.
33

4+
import fs from "node:fs";
5+
import path from "node:path";
6+
import url from "node:url";
47
import { parseTimestamp } from "../../src/timestamp.mjs";
8+
import { readFrontMatterFromFileAsync } from "../../src/front-matter.mjs";
9+
10+
let __filename = url.fileURLToPath(import.meta.url);
11+
let __dirname = path.dirname(__filename);
512

613
export let routes = {
714
"/blog/feed.xml": {
@@ -23,6 +30,36 @@ function qljsDate(attributes, { currentURI }) {
2330
return `<time>${timestamp.date}</time>`;
2431
}
2532

33+
export async function loadBlogPostsAsync() {
34+
let directories = await fs.promises.readdir(__dirname, {
35+
withFileTypes: true,
36+
});
37+
directories = directories.filter((dir) => dir.isDirectory());
38+
let posts = await Promise.all(
39+
directories.map(async (dir) => {
40+
let indexPath = path.join(__dirname, dir.name, "index.ejs.html");
41+
let meta = await readFrontMatterFromFileAsync(indexPath);
42+
if (typeof meta.blogDate === "undefined") {
43+
throw new Error(`Missing blogDate in front matter of ${indexPath}`);
44+
}
45+
return {
46+
dir: dir.name,
47+
meta: meta,
48+
};
49+
})
50+
);
51+
sortPostsReverseChronologically(posts);
52+
return posts;
53+
}
54+
55+
function sortPostsReverseChronologically(posts) {
56+
posts.sort((a, b) => {
57+
if (a.meta.blogDate < b.meta.blogDate) return +1;
58+
if (a.meta.blogDate > b.meta.blogDate) return -1;
59+
return 0;
60+
});
61+
}
62+
2663
// quick-lint-js finds bugs in JavaScript programs.
2764
// Copyright (C) 2020 Matthew "strager" Glazar
2865
//

0 commit comments

Comments
 (0)