Skip to content

Commit 363d0f9

Browse files
authored
Merge pull request #15 from jupyter-book/agoose77/fix-blog-frontmatter
🛠️ Validate `frontmatter` before using it
2 parents f725ef4 + f893b98 commit 363d0f9

File tree

3 files changed

+97
-74
lines changed

3 files changed

+97
-74
lines changed

package-lock.json

Lines changed: 71 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"license": "ISC",
88
"devDependencies": {
99
"glob": "^11.0.0",
10-
"myst-transforms": "^1.3.27",
10+
"myst-transforms": "^1.3.33",
11+
"myst-frontmatter": "^1.7.10",
1112
"mystmd": "^1.3.17"
1213
}
1314
}

plugins/blog.mjs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { globSync } from "glob";
22
import { readFileSync } from "node:fs";
33
import { extname, basename } from "node:path";
44
import { getFrontmatter } from "myst-transforms";
5+
import { validatePageFrontmatter } from "myst-frontmatter";
6+
import { fileError, fileWarn } from "myst-common";
57

68
const blogPostsDirective = {
79
name: "blog-posts",
@@ -14,10 +16,27 @@ const blogPostsDirective = {
1416
const paths = globSync("posts/*.md").sort().reverse(); // For now, string sort
1517
const nodes = paths.map((path) => {
1618
const ext = extname(path);
17-
const name = basename(path, ext)
19+
const name = basename(path, ext);
1820
const content = readFileSync(path, { encoding: "utf-8" });
1921
const ast = ctx.parseMyst(content);
20-
const frontmatter = getFrontmatter(vfile, ast).frontmatter;
22+
const frontmatter = validatePageFrontmatter(
23+
getFrontmatter(vfile, ast).frontmatter,
24+
{
25+
property: "frontmatter",
26+
file: vfile.path,
27+
messages: {},
28+
errorLogFn: (message) => {
29+
fileError(vfile, message, {
30+
ruleId: RuleId.validPageFrontmatter,
31+
});
32+
},
33+
warningLogFn: (message) => {
34+
fileWarn(vfile, message, {
35+
ruleId: RuleId.validPageFrontmatter,
36+
});
37+
},
38+
},
39+
);
2140
const descriptionItems = frontmatter.description
2241
? ctx.parseMyst(frontmatter.description).children
2342
: [];
@@ -29,7 +48,9 @@ const blogPostsDirective = {
2948
{
3049
type: "footer",
3150
// Pull out the first child of `root` node.
32-
children: [ctx.parseMyst(`**Date**: ${frontmatter.date}`)["children"][0]],
51+
children: [
52+
ctx.parseMyst(`**Date**: ${frontmatter.date}`)["children"][0],
53+
],
3354
},
3455
]
3556
: [];

0 commit comments

Comments
 (0)