1
1
// Copyright (C) 2020 Matthew "strager" Glazar
2
2
// See end of file for extended copyright information.
3
3
4
+ import fs from "node:fs" ;
5
+ import path from "node:path" ;
6
+ import url from "node:url" ;
4
7
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 ) ;
5
12
6
13
export let routes = {
7
14
"/blog/feed.xml" : {
@@ -23,6 +30,36 @@ function qljsDate(attributes, { currentURI }) {
23
30
return `<time>${ timestamp . date } </time>` ;
24
31
}
25
32
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
+
26
63
// quick-lint-js finds bugs in JavaScript programs.
27
64
// Copyright (C) 2020 Matthew "strager" Glazar
28
65
//
0 commit comments