-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-blog-heading-structure.mjs
More file actions
125 lines (105 loc) · 3.17 KB
/
verify-blog-heading-structure.mjs
File metadata and controls
125 lines (105 loc) · 3.17 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import fs from 'node:fs';
import path from 'node:path';
import {
distDir,
extractH1Texts,
getBlogSourceEntries,
listRenderedHtmlFiles,
requireFile,
} from './blog-heading-utils.mjs';
const failures = [];
const results = [];
const notes = [];
function assert(condition, route, message) {
if (!condition) {
failures.push({ route, message });
throw new Error(message);
}
}
function runCheck(name, route, fn) {
try {
fn();
results.push({ name, route, status: 'pass' });
} catch (error) {
results.push({ name, route, status: 'fail', detail: error.message });
}
}
function verifyCollectionRoute(route) {
const html = requireFile(route);
const h1s = extractH1Texts(html);
runCheck('collection_single_h1', route, () => {
assert(h1s.length === 1, route, `Expected exactly one <h1> on ${route}, found ${h1s.length}.`);
assert(h1s[0].length > 0, route, `Expected non-empty <h1> text on ${route}.`);
});
}
function verifyPostRoute(route, expectedTitle) {
const html = requireFile(route);
const h1s = extractH1Texts(html);
runCheck('post_single_h1', route, () => {
assert(h1s.length === 1, route, `Expected exactly one <h1> on ${route}, found ${h1s.length}.`);
});
runCheck('post_title_matches_frontmatter', route, () => {
assert(h1s[0] === expectedTitle, route, `Expected <h1> "${expectedTitle}" on ${route}, found "${h1s[0] ?? ''}".`);
});
}
function verifyCollectionRoutes() {
const collectionRoutes = [
'blog/index.html',
'en/blog/index.html',
...listRenderedHtmlFiles('blog/tags'),
...listRenderedHtmlFiles('en/blog/tags'),
...listRenderedHtmlFiles('blog/authors'),
...listRenderedHtmlFiles('en/blog/authors'),
];
if (!collectionRoutes.some((route) => route.includes('/authors/'))) {
notes.push('No author archive pages were generated in this build, so author-route output checks were skipped.');
}
for (const route of collectionRoutes) {
verifyCollectionRoute(route);
}
}
function verifyPostRoutes() {
for (const entry of getBlogSourceEntries({ locale: 'zh' })) {
verifyPostRoute(path.posix.join('blog', entry.slug, 'index.html'), entry.title);
}
for (const entry of getBlogSourceEntries({ locale: 'en' })) {
verifyPostRoute(path.posix.join('en/blog', entry.slug, 'index.html'), entry.title);
}
}
function main() {
if (!fs.existsSync(distDir)) {
throw new Error('dist directory not found. Run `npm run build` first.');
}
verifyCollectionRoutes();
verifyPostRoutes();
const summary = {
status: failures.length === 0 ? 'pass' : 'fail',
checkedRoutes: Array.from(new Set(results.map((result) => result.route))),
totals: {
checks: results.length,
passed: results.filter((result) => result.status === 'pass').length,
failed: results.filter((result) => result.status === 'fail').length,
},
notes,
failures,
};
console.log(JSON.stringify(summary, null, 2));
if (failures.length > 0) {
process.exit(1);
}
}
try {
main();
} catch (error) {
console.error(
JSON.stringify(
{
status: 'fail',
failures: [{ route: 'build', message: error.message }],
},
null,
2,
),
);
process.exit(1);
}