-
Notifications
You must be signed in to change notification settings - Fork 384
Expand file tree
/
Copy pathfrontmatter_test.ts
More file actions
101 lines (86 loc) · 3.28 KB
/
Copy pathfrontmatter_test.ts
File metadata and controls
101 lines (86 loc) · 3.28 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { walk } from "@std/fs";
import { extract } from "@std/front-matter/yaml";
import { assert, assertEquals } from "@std/assert";
const DIRS_TO_CHECK = ["./runtime", "./deploy", "./examples"];
// Content pages that must carry a `last_modified` frontmatter field so the
// freshness check (scripts/check_freshness.ts) has something to enforce.
const LAST_MODIFIED_DIRS = [
"./runtime",
"./deploy",
"./examples",
"./sandbox",
"./subhosting",
"./ai",
];
// Generated or synced subtrees that aren't hand-edited content pages.
const LAST_MODIFIED_EXCLUDES = ["runtime/reference/std/"];
const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
Deno.test("Frontmatter titles must not contain backticks", async (t) => {
for (const dir of DIRS_TO_CHECK) {
for await (const entry of walk(dir, { exts: [".md", ".mdx"] })) {
const content = await Deno.readTextFile(entry.path);
if (!content.startsWith("---")) continue;
const { attrs } = extract<{ title?: string }>(content);
if (typeof attrs.title !== "string") continue;
await t.step(entry.path, () => {
assert(
!attrs.title!.includes("`"),
`Title contains backticks: "${attrs.title}". Use plain text instead.`,
);
});
}
}
});
Deno.test("CLI command page titles must be just the command name", async (t) => {
for await (
const entry of walk("./runtime/reference/cli", { exts: [".md"] })
) {
const content = await Deno.readTextFile(entry.path);
if (!content.startsWith("---")) continue;
const { attrs } = extract<{ title?: string; command?: string }>(content);
if (typeof attrs.command !== "string") continue;
const expected = `deno ${attrs.command}`;
await t.step(entry.path, () => {
assertEquals(
attrs.title,
expected,
`Title should be "${expected}", got "${attrs.title}". Put descriptions in the "description" field instead.`,
);
});
}
});
Deno.test("Content pages must have a valid last_modified field", async (t) => {
const today = new Date().toISOString().slice(0, 10);
for (const dir of LAST_MODIFIED_DIRS) {
for await (const entry of walk(dir, { exts: [".md", ".mdx"] })) {
const rel = entry.path.replace(/^\.\//, "");
if (LAST_MODIFIED_EXCLUDES.some((p) => rel.startsWith(p))) continue;
const content = await Deno.readTextFile(entry.path);
if (content.trim() === "") continue; // empty placeholder, not a page
await t.step(entry.path, () => {
assert(
content.startsWith("---"),
`Missing frontmatter; add a "last_modified: ${today}" field.`,
);
const { attrs } = extract<{ last_modified?: unknown }>(content);
const value = attrs.last_modified;
assert(
value != null,
`Missing "last_modified" field; add "last_modified: ${today}".`,
);
// YAML parses an unquoted date into a Date; accept either form.
const date = value instanceof Date
? value.toISOString().slice(0, 10)
: String(value).trim();
assert(
DATE_RE.test(date),
`"last_modified" must be a YYYY-MM-DD date, got "${date}".`,
);
assert(
date <= today,
`"last_modified" is in the future ("${date}"); today is ${today}.`,
);
});
}
}
});