-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspecification.js
More file actions
70 lines (63 loc) · 1.92 KB
/
Copy pathspecification.js
File metadata and controls
70 lines (63 loc) · 1.92 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
import { renderMarkdown } from "./markdown.js";
import { escapeHtml } from "./typesetting.js";
const statusPanel = document.querySelector("#specification-status");
const content = document.querySelector("#specification-content");
const toc = document.querySelector("#toc");
function slugify(text) {
return text
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
}
function setStatus(message, variant = "default") {
statusPanel.textContent = message;
statusPanel.hidden = false;
statusPanel.classList.remove("is-error", "is-success");
if (variant === "error") {
statusPanel.classList.add("is-error");
}
if (variant === "success") {
statusPanel.classList.add("is-success");
}
}
function assignHeadingIds() {
const seen = new Map();
[...content.querySelectorAll("h1, h2, h3")].forEach((heading) => {
const base = slugify(heading.textContent ?? "section") || "section";
const count = (seen.get(base) ?? 0) + 1;
seen.set(base, count);
heading.id = count === 1 ? base : `${base}-${count}`;
});
}
function buildToc() {
const headings = [...content.querySelectorAll("h2, h3")];
toc.innerHTML = headings
.map(
(heading) =>
`<a class="toc-level-${heading.tagName.toLowerCase()}" href="#${heading.id}">${escapeHtml(heading.textContent ?? "")}</a>`,
)
.join("");
}
async function loadSpecification() {
try {
const response = await fetch(
new URL("./specification.md", import.meta.url),
);
if (!response.ok) {
throw new Error(`Unable to fetch specification.md (${response.status})`);
}
const markdown = await response.text();
content.innerHTML = renderMarkdown(markdown);
assignHeadingIds();
buildToc();
statusPanel.hidden = true;
} catch (error) {
setStatus(
error instanceof Error
? error.message
: "Unable to load the specification.",
"error",
);
}
}
loadSpecification();