Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 413x 8x 8x 406x 88x 52x 52x 52x 36x 22x 22x 14x 8x 406x 413x 406x 406x 406x 406x 22x 22x 22x 3145x 3145x 3145x 3145x 49x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 17x 17x 17x 17x 13x 13x | import { Marked, type Tokens } from "marked";
import { BASE_PATH } from "@/lib/static-export";
/** GitHub's heading anchor: lower case, punctuation dropped, spaces to hyphens,
* so a link written against the file on GitHub lands on the same heading here. */
export function headingId(text: string): string {
return text
.toLowerCase()
.replace(/[^\p{L}\p{N}\s_-]/gu, "")
.trim()
.replace(/\s+/g, "-");
}
/** A heading that already carries its number in the source, "## 7. Incidents"
* or "### 7.2 Clocks": the document numbers itself, so the renderer does not. */
const NUMBERED = /^\d+(\.\d+)*\.?\s/;
/** Section numbers for the document being rendered: h2 counts "1.", h3
* "1.1"; off for a document whose own headings are numbered, and off for
* renderMarkdown on its own. splitMermaid switches it on for one document
* and renders it in order. */
const numbering = { on: false, h2: 0, h3: 0 };
function sectionNumber(depth: number, text: string): string {
if (!numbering.on || NUMBERED.test(text)) return "";
if (depth === 2) {
numbering.h2 += 1;
numbering.h3 = 0;
return `${numbering.h2}.`;
}
if (depth === 3 && numbering.h2 > 0) {
numbering.h3 += 1;
return `${numbering.h2}.${numbering.h3}`;
}
return "";
}
const marked = new Marked({
gfm: true,
renderer: {
heading({ tokens, depth }: Tokens.Heading) {
const html = this.parser.parseInline(tokens);
const text = tokens.map((t) => ("text" in t ? t.text : t.raw)).join("");
// The anchor stays GitHub's, from the text without the number, so a
// link written against the file on GitHub still lands here.
const id = headingId(text);
const n = sectionNumber(depth, text);
const label = n ? `<span class="doc-num">${n}</span> ` : "";
return `<h${depth} id="${id}">${label}${html}</h${depth}>\n`;
},
image({ href, title, text }: Tokens.Image) {
// A diagram is scaled to the column; the link opens it at full size.
const alt = text.replace(/"/g, """);
const t = title ? ` title="${title.replace(/"/g, """)}"` : "";
return `<a href="${href}" target="_blank" rel="noopener noreferrer" class="doc-figure"><img src="${href}" alt="${alt}"${t} loading="lazy" /></a>`;
},
link({ href, title, tokens }: Tokens.Link) {
const text = this.parser.parseInline(tokens);
const external = /^https?:\/\//.test(href);
const attrs = [
`href="${href}"`,
title ? `title="${title}"` : "",
external ? 'target="_blank" rel="noopener noreferrer"' : "",
]
.filter(Boolean)
.join(" ");
return `<a ${attrs}>${text}</a>`;
},
},
});
/** Markdown (GitHub flavour) to HTML, for the reports the hub publishes. The
* input is the repository's own documentation, never a visitor's. */
export function renderMarkdown(markdown: string): string {
return marked.parse(markdown) as string;
}
export type DocSegment =
| { kind: "html"; html: string }
| { kind: "mermaid"; chart: string };
/** Split a document at its ```mermaid fences: the text between them is
* rendered to HTML, each fence becomes a diagram the page draws with
* MermaidDiagram (marked would only show the source as code). */
export function splitMermaid(markdown: string): DocSegment[] {
// A document that numbers its own h2 headings keeps its numbers.
numbering.on = !/^##\s+\d+(\.\d+)*\.?\s/m.test(markdown);
numbering.h2 = 0;
numbering.h3 = 0;
const out: DocSegment[] = [];
const fence = /^```mermaid[ \t]*\n([\s\S]*?)^```[ \t]*$/gm;
let last = 0;
for (const m of markdown.matchAll(fence)) {
const before = markdown.slice(last, m.index);
Eif (before.trim()) out.push({ kind: "html", html: renderMarkdown(before) });
out.push({ kind: "mermaid", chart: m[1].trim() });
last = (m.index ?? 0) + m[0].length;
}
const rest = markdown.slice(last);
Eif (rest.trim()) out.push({ kind: "html", html: renderMarkdown(rest) });
numbering.on = false;
return out;
}
/** A published document's segments, ready to show. Its images are copied to
* public/published-docs by scripts/gen-docs-content.py and referenced
* through the __BASE_PATH__ placeholder, so the static export (basePath)
* and the hub (none) both find them. Used for the English body on the
* server and for a translation fetched on the client (ADR-071). */
export function docSegments(markdown: string): DocSegment[] {
return splitMermaid(markdown).map((seg) =>
seg.kind === "html"
? { ...seg, html: seg.html.replaceAll("__BASE_PATH__/", `${BASE_PATH}/`) }
: seg,
);
}
|