Skip to content

Commit 1f90f36

Browse files
TMHSDigitalclaude
andcommitted
site: checks for generated pages
checkPage reports leaked markdown (double asterisks, bracket-paren link syntax, backticks) in visible text outside scripts and code, anything but exactly one h1, a missing canonical link or title, and any internal href or src that does not resolve inside the built site. 10 new assertions (82 total). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wc67NX9MM964uaVGn4bpZJ
1 parent 5777730 commit 1f90f36

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎site/lib/lib.test.mjs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,22 @@ eq("json-ld parses with a licence URL", JSON.parse(ldBlock).mainEntity.license,
133133
eq("json-ld omits licence without spdx", jsonLd({ ...base, license: "custom", license_spdx: undefined }, site).mainEntity.license, undefined);
134134
eq("scriptJson escapes <", scriptJson({ a: "</script>" }), '{"a":"\\u003c/script>"}');
135135

136+
/* page checks -------------------------------------------------------------- */
137+
import { checkPage } from "./page-checks.mjs";
138+
const okPage = `<html><head><title>T</title><link rel="canonical" href="https://x/" /></head><body><h1>A</h1><p><code>**x**</code></p><a href="../e2/">n</a><a href="https://a.test">x</a><script>var a = "**";</script></body></html>`;
139+
const kinds = new Map([["entry/e2", "dir"], ["", "dir"]]);
140+
const opts = { file: "entry/e1/index.html", kindOf: (p) => kinds.get(p) || null };
141+
eq("clean page passes", checkPage(okPage, opts).length, 0);
142+
has("leaked bold", checkPage(okPage.replace("<h1>A</h1>", "<h1>A</h1>**b**"), opts).join(), "double asterisk");
143+
has("leaked link", checkPage(okPage.replace("<h1>A</h1>", "<h1>A</h1>[a](b)"), opts).join(), "markdown link");
144+
has("leaked backtick", checkPage(okPage.replace("<h1>A</h1>", "<h1>A</h1>`x`"), opts).join(), "backtick");
145+
has("two h1", checkPage(okPage.replace("<h1>A</h1>", "<h1>A</h1><h1>B</h1>"), opts).join(), "expected one h1, found 2");
146+
has("no canonical", checkPage(okPage.replace(/<link rel="canonical"[^>]*>/, ""), opts).join(), "missing canonical");
147+
has("empty title", checkPage(okPage.replace("<title>T</title>", "<title></title>"), opts).join(), "missing or empty title");
148+
has("dangling link", checkPage(okPage.replace("../e2/", "../e3/"), opts).join(), "../e3/ does not resolve");
149+
has("link leaving the site", checkPage(okPage.replace("../e2/", "../../../x/"), opts).join(), "leaves the site");
150+
eq("query and hash are ignored", checkPage(okPage.replace("../e2/", "../../?cat=2d#catalog"), opts).length, 0);
151+
136152
/* report (keep last) ------------------------------------------------------ */
137153
if (failures.length) {
138154
console.error(`lib.test failed (${failures.length}):`);

‎site/lib/page-checks.mjs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/** Checks run on every generated page; the build fails on any error. */
2+
import path from "node:path";
3+
4+
const LEAKS = [
5+
[/\*\*/, "a double asterisk"],
6+
[/\]\(/, "a markdown link"],
7+
[/`/, "a backtick"],
8+
];
9+
10+
export function checkPage(html, { file, kindOf }) {
11+
const errors = [];
12+
const text = html
13+
.replace(/<script[\s\S]*?<\/script>/g, " ")
14+
.replace(/<style[\s\S]*?<\/style>/g, " ")
15+
.replace(/<code>[\s\S]*?<\/code>/g, " ")
16+
.replace(/<[^>]+>/g, " ");
17+
for (const [re, what] of LEAKS) if (re.test(text)) errors.push(`${file}: leaked markdown (${what})`);
18+
const h1 = (html.match(/<h1[\s>]/g) || []).length;
19+
if (h1 !== 1) errors.push(`${file}: expected one h1, found ${h1}`);
20+
if (!/<link rel="canonical" href="[^"]+"/.test(html)) errors.push(`${file}: missing canonical link`);
21+
if (!/<title>[^<\s][^<]*<\/title>/.test(html)) errors.push(`${file}: missing or empty title`);
22+
const dir = path.posix.dirname(file);
23+
for (const m of html.matchAll(/\s(?:href|src)="([^"]+)"/g)) {
24+
const href = m[1];
25+
if (/^(https?:|mailto:|#|data:)/i.test(href)) continue;
26+
const clean = href.split(/[?#]/)[0];
27+
let target = path.posix.normalize(path.posix.join(dir, clean || ".")).replace(/\/$/, "");
28+
if (target === ".") target = "";
29+
if (target.startsWith("..")) {
30+
errors.push(`${file}: link ${href} leaves the site`);
31+
continue;
32+
}
33+
if (!kindOf(target)) errors.push(`${file}: link ${href} does not resolve`);
34+
}
35+
return errors;
36+
}

0 commit comments

Comments
 (0)