Skip to content

Commit ed2ad6e

Browse files
TMHSDigitalclaude
andcommitted
site: inline markdown renderer for entry pages
Code spans, backslash escapes, links through a resolver (code and bold allowed in link text), bare URLs with trailing punctuation kept outside, bold and *italic*. Everything else is escaped. Underscores are literal because entry bodies contain identifiers like Rig_Medium. 14 new assertions (22 total). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Wc67NX9MM964uaVGn4bpZJ
1 parent fee59c1 commit ed2ad6e

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎site/lib/lib.test.mjs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ eq("verifiedAge missing", verifiedAge(null, Date.now()).bucket, "unknown");
4646
eq("entryPageUrl trims the site slash", entryPageUrl({ siteUrl: "https://x.test/site/" }, "a-b"), "https://x.test/site/entry/a-b/");
4747
has("STATUS_NOTES covers needs-review", STATUS_NOTES["needs-review"], "open");
4848

49+
/* markdown: inline --------------------------------------------------------- */
50+
import { renderInline } from "./markdown.mjs";
51+
const passLink = (href) => ({ href, external: /^https?:/.test(href) });
52+
eq("plain text is escaped", renderInline(`a < b & "c"`, passLink), "a &lt; b &amp; &quot;c&quot;");
53+
eq("bold", renderInline("a **b** c", passLink), "a <strong>b</strong> c");
54+
eq("italic", renderInline("a *b* c", passLink), "a <em>b</em> c");
55+
eq("underscores stay literal", renderInline("Rig_Medium and qCC_db", passLink), "Rig_Medium and qCC_db");
56+
eq("code is literal", renderInline("use `**x** <y>`", passLink), "use <code>**x** &lt;y&gt;</code>");
57+
eq("external link", renderInline("[Site](https://a.test/x)", passLink), '<a href="https://a.test/x" rel="noopener noreferrer">Site</a>');
58+
eq("relative link uses the resolver", renderInline("[k](k.md)", () => ({ href: "../k/", external: false })), '<a href="../k/">k</a>');
59+
eq("code inside link text", renderInline("[`docs/provenance.md`](../../docs/provenance.md)", () => ({ href: "https://gh.test/p", external: true })), '<a href="https://gh.test/p" rel="noopener noreferrer"><code>docs/provenance.md</code></a>');
60+
eq("bold inside link text", renderInline("[**x**](https://a.test)", passLink), '<a href="https://a.test" rel="noopener noreferrer"><strong>x</strong></a>');
61+
eq("bare url keeps trailing period outside", renderInline("see https://a.test/x.", passLink), 'see <a href="https://a.test/x" rel="noopener noreferrer">https://a.test/x</a>.');
62+
eq("bare url in parentheses", renderInline("(https://a.test/x)", passLink), '(<a href="https://a.test/x" rel="noopener noreferrer">https://a.test/x</a>)');
63+
eq("backslash escape", renderInline("\\*not italic\\*", passLink), "*not italic*");
64+
eq("html in text is escaped", renderInline("<script>alert(1)</script>", passLink), "&lt;script&gt;alert(1)&lt;/script&gt;");
65+
eq("non-ASCII survives", renderInline("Johannes Sjölund", passLink), "Johannes Sjölund");
66+
4967
/* report (keep last) ------------------------------------------------------ */
5068
if (failures.length) {
5169
console.error(`lib.test failed (${failures.length}):`);

‎site/lib/markdown.mjs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Renders catalog entry bodies to HTML. Deliberately small: it supports the
3+
* markdown the catalog uses (measured 2026-09-24) and throws on anything
4+
* else, so a new construct fails the build instead of rendering wrong.
5+
* Underscores are literal: bodies contain identifiers like Rig_Medium.
6+
*/
7+
import { esc } from "./shared.mjs";
8+
9+
export class MarkdownError extends Error {}
10+
11+
const slot = (i) => `\u0000${i}\u0000`;
12+
const SLOT_RE = /\u0000(\d+)\u0000/g;
13+
14+
/** Bold and italic on already-escaped text. */
15+
function marks(escaped) {
16+
return escaped
17+
.replace(/\*\*(?=\S)(.+?)(?<=\S)\*\*/g, "<strong>$1</strong>")
18+
.replace(/(^|[^*\w])\*(?=\S)([^*]+?)(?<=\S)\*(?![*\w])/g, "$1<em>$2</em>");
19+
}
20+
21+
export function renderInline(text, resolveLink) {
22+
const slots = [];
23+
const keep = (html) => {
24+
slots.push(html);
25+
return slot(slots.length - 1);
26+
};
27+
let s = String(text);
28+
// Code spans first: their contents are literal.
29+
s = s.replace(/`([^`]+)`/g, (_, code) => keep(`<code>${esc(code)}</code>`));
30+
// Backslash escapes.
31+
s = s.replace(/\\([\\`*_[\]()#+\-.!|<>])/g, (_, ch) => keep(esc(ch)));
32+
// Links. The label keeps bold, italic and code; the href goes through the resolver.
33+
s = s.replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, (_, label, href) => {
34+
const { href: out, external } = resolveLink(href);
35+
const rel = external ? ' rel="noopener noreferrer"' : "";
36+
return keep(`<a href="${esc(out)}"${rel}>${marks(esc(label))}</a>`);
37+
});
38+
// Bare URLs, leaving trailing sentence punctuation outside the link.
39+
s = s.replace(/https?:\/\/[^\s<>"]+/g, (url) => {
40+
const [, core, tail] = url.match(/^(.*?)([.,;:!?)\]]*)$/);
41+
return keep(`<a href="${esc(core)}" rel="noopener noreferrer">${esc(core)}</a>`) + tail;
42+
});
43+
s = marks(esc(s));
44+
// Link labels can hold code slots, so restore until stable.
45+
let before;
46+
do {
47+
before = s;
48+
s = s.replace(SLOT_RE, (_, i) => slots[Number(i)]);
49+
} while (s !== before);
50+
return s;
51+
}

0 commit comments

Comments
 (0)