-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblog.mjs
56 lines (53 loc) · 1.77 KB
/
blog.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
import { globSync } from "glob";
import { readFileSync } from "node:fs";
import { extname, basename } from "node:path";
import { getFrontmatter } from "myst-transforms";
const blogPostsDirective = {
name: "blog-posts",
doc: "Display preview cards for documents.",
options: {
limit: { type: Number, doc: "Number of posts." },
},
run(data, vfile, ctx) {
const size = data.options.limit ?? 3;
const paths = globSync("posts/*.md").sort().reverse(); // For now, string sort
const nodes = paths.map((path) => {
const ext = extname(path);
const name = basename(path, ext)
const content = readFileSync(path, { encoding: "utf-8" });
const ast = ctx.parseMyst(content);
const frontmatter = getFrontmatter(vfile, ast).frontmatter;
const descriptionItems = frontmatter.description
? ctx.parseMyst(frontmatter.description).children
: [];
const subtitleItems = frontmatter.subtitle
? ctx.parseMyst(frontmatter.subtitle).children
: [];
const footerItems = frontmatter.date
? [
{
type: "footer",
// Pull out the first child of `root` node.
children: [ctx.parseMyst(`**Date**: ${frontmatter.date}`)["children"][0]],
},
]
: [];
return {
type: "card",
children: [
{
type: "cardTitle",
children: ctx.parseMyst(frontmatter.title).children,
},
...subtitleItems,
...descriptionItems,
...footerItems,
],
url: `/${path.toString().slice(0, -ext.length)}`,
};
});
return Array.from(nodes).slice(0, size);
},
};
const plugin = { name: "Blog posts", directives: [blogPostsDirective] };
export default plugin;