Skip to content

Commit b68ff5a

Browse files
gh-94: Remove redundant JS.
1 parent 187db28 commit b68ff5a

File tree

6 files changed

+140
-166
lines changed

6 files changed

+140
-166
lines changed

docs/404.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818

1919
</script>
2020

21-
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
22-
<script>
23-
document.addEventListener('DOMContentLoaded', function(){
24-
const md = document.getElementById('md').textContent;
25-
const rendered = marked.parse(md);
26-
document.getElementById('content').innerHTML = rendered;
27-
});
28-
</script>
21+
<script src="./render-md.js"></script>
2922
</body>
3023
</html>

docs/CHANGELOG.html

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -251,31 +251,6 @@
251251

252252
</script>
253253

254-
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
255-
<script>
256-
document.addEventListener('DOMContentLoaded', function(){
257-
const md = document.getElementById('md').textContent;
258-
const rendered = marked.parse(md);
259-
const container = document.getElementById('content');
260-
container.innerHTML = rendered;
261-
262-
// Ensure predictable heading IDs so TOC fragment links work.
263-
// Slug function: lowercase, remove punctuation except digits/letters/spaces/-,
264-
// collapse spaces to hyphens.
265-
function slugify(text) {
266-
return text.trim().toLowerCase()
267-
.replace(/<[^>]+>/g, '')
268-
.replace(/[^a-z0-9\s-]/g, '')
269-
.replace(/\s+/g, '-');
270-
}
271-
272-
const headings = container.querySelectorAll('h1,h2,h3,h4,h5,h6');
273-
headings.forEach(h => {
274-
if (!h.id || h.id.trim() === '') {
275-
h.id = slugify(h.textContent || h.innerText || '');
276-
}
277-
});
278-
});
279-
</script>
254+
<script src="./render-md.js"></script>
280255
</body>
281256
</html>

docs/SPECIFICATION.html

Lines changed: 1 addition & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,121 +1075,6 @@
10751075

10761076
</script>
10771077

1078-
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
1079-
<script>
1080-
document.addEventListener('DOMContentLoaded', function(){
1081-
const md = document.getElementById('md').textContent;
1082-
const rendered = marked.parse(md);
1083-
const container = document.getElementById('content');
1084-
container.innerHTML = rendered;
1085-
1086-
// Ensure predictable heading IDs so TOC fragment links work.
1087-
// Slug function: lowercase, remove punctuation except digits/letters/spaces/-,
1088-
// collapse spaces to hyphens.
1089-
function slugify(text) {
1090-
return text.trim().toLowerCase()
1091-
.replace(/<[^>]+>/g, '')
1092-
.replace(/[^a-z0-9\s-]/g, '')
1093-
.replace(/\s+/g, '-');
1094-
}
1095-
1096-
const headings = container.querySelectorAll('h1,h2,h3,h4,h5,h6');
1097-
headings.forEach(h => {
1098-
if (!h.id || h.id.trim() === '') {
1099-
h.id = slugify(h.textContent || h.innerText || '');
1100-
}
1101-
});
1102-
1103-
function ensureTocStyle() {
1104-
if (document.getElementById('generated-toc-style')) return;
1105-
const style = document.createElement('style');
1106-
style.id = 'generated-toc-style';
1107-
style.textContent = '\n' +
1108-
'.generated-toc { padding-left: 1.2rem; }\n' +
1109-
'.generated-toc > li { margin: 0.6rem 0; }\n' +
1110-
'.generated-toc li ul { margin-top: 0.25rem; margin-bottom: 0.25rem; }\n';
1111-
document.head.appendChild(style);
1112-
}
1113-
1114-
function findTocHeading(root) {
1115-
return Array.from(root.querySelectorAll('h2')).find(h =>
1116-
(h.textContent || '').trim().toLowerCase() === 'table of contents'
1117-
);
1118-
}
1119-
1120-
function buildTocList(root, tocHeading) {
1121-
const tocLevels = new Set([2, 3, 4]);
1122-
const items = Array.from(root.querySelectorAll('h2,h3,h4')).filter(h => h !== tocHeading);
1123-
if (items.length === 0) return null;
1124-
1125-
const tocRoot = document.createElement('ul');
1126-
tocRoot.className = 'generated-toc';
1127-
const listStack = [tocRoot];
1128-
let currentLevel = 2;
1129-
1130-
items.forEach(h => {
1131-
const level = Number(h.tagName.slice(1));
1132-
if (!tocLevels.has(level)) return;
1133-
1134-
while (currentLevel < level) {
1135-
const parentList = listStack[listStack.length - 1];
1136-
let parentLi = parentList.lastElementChild;
1137-
if (!parentLi || parentLi.tagName.toLowerCase() !== 'li') {
1138-
parentLi = document.createElement('li');
1139-
parentLi.textContent = '';
1140-
parentList.appendChild(parentLi);
1141-
}
1142-
const nested = document.createElement('ul');
1143-
parentLi.appendChild(nested);
1144-
listStack.push(nested);
1145-
currentLevel += 1;
1146-
}
1147-
1148-
while (currentLevel > level) {
1149-
listStack.pop();
1150-
currentLevel -= 1;
1151-
}
1152-
1153-
const li = document.createElement('li');
1154-
const a = document.createElement('a');
1155-
a.href = '#' + h.id;
1156-
a.textContent = (h.textContent || '').trim();
1157-
li.appendChild(a);
1158-
listStack[listStack.length - 1].appendChild(li);
1159-
});
1160-
1161-
return tocRoot;
1162-
}
1163-
1164-
function replaceTocBlock(root) {
1165-
const tocHeading = findTocHeading(root);
1166-
if (!tocHeading) return;
1167-
1168-
let stop = tocHeading.nextElementSibling;
1169-
while (stop && stop.tagName.toLowerCase() !== 'hr') {
1170-
stop = stop.nextElementSibling;
1171-
}
1172-
1173-
let node = tocHeading.nextSibling;
1174-
while (node && node !== stop) {
1175-
const next = node.nextSibling;
1176-
node.remove();
1177-
node = next;
1178-
}
1179-
1180-
ensureTocStyle();
1181-
const tocList = buildTocList(root, tocHeading);
1182-
if (!tocList) return;
1183-
1184-
if (stop) {
1185-
root.insertBefore(tocList, stop);
1186-
} else {
1187-
root.appendChild(tocList);
1188-
}
1189-
}
1190-
1191-
replaceTocBlock(container);
1192-
});
1193-
</script>
1078+
<script src="./render-md.js"></script>
11941079
</body>
11951080
</html>

docs/UNLICENSE.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,6 @@
4343

4444
</script>
4545

46-
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
47-
<script>
48-
document.addEventListener('DOMContentLoaded', function(){
49-
const md = document.getElementById('md').textContent;
50-
const rendered = marked.parse(md);
51-
document.getElementById('content').innerHTML = rendered;
52-
});
53-
</script>
46+
<script src="./render-md.js"></script>
5447
</body>
5548
</html>

docs/index.html

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424

2525
</script>
2626

27-
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
28-
<script>
29-
document.addEventListener('DOMContentLoaded', function(){
30-
const md = document.getElementById('md').textContent;
31-
const rendered = marked.parse(md);
32-
document.getElementById('content').innerHTML = rendered;
33-
});
34-
</script>
27+
<script src="./render-md.js"></script>
3528
</body>
3629
</html>

docs/render-md.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/* Centralized markdown renderer for the docs.
2+
Loads marked.js if necessary, then renders the markdown embedded
3+
in the page's `#md` element into `#content` and normalizes heading ids. */
4+
(function(){
5+
function slugify(text) {
6+
return (text || '')
7+
.trim()
8+
.toLowerCase()
9+
.replace(/<[^>]+>/g, '')
10+
.replace(/[^a-z0-9\s-]/g, '')
11+
.replace(/\s+/g, '-');
12+
}
13+
14+
function ensureTocStyle() {
15+
if (document.getElementById('generated-toc-style')) return;
16+
var style = document.createElement('style');
17+
style.id = 'generated-toc-style';
18+
style.textContent = '\n' +
19+
'.generated-toc { padding-left: 1.2rem; }\n' +
20+
'.generated-toc > li { margin: 0.6rem 0; }\n' +
21+
'.generated-toc li ul { margin-top: 0.25rem; margin-bottom: 0.25rem; }\n';
22+
document.head.appendChild(style);
23+
}
24+
25+
function findTocHeading(root) {
26+
return Array.from(root.querySelectorAll('h2')).find(function(h){
27+
return (h.textContent || '').trim().toLowerCase() === 'table of contents';
28+
});
29+
}
30+
31+
function buildTocList(root, tocHeading) {
32+
var tocLevels = new Set([2,3,4]);
33+
var items = Array.from(root.querySelectorAll('h2,h3,h4')).filter(function(h){ return h !== tocHeading; });
34+
if (items.length === 0) return null;
35+
36+
var tocRoot = document.createElement('ul');
37+
tocRoot.className = 'generated-toc';
38+
var listStack = [tocRoot];
39+
var currentLevel = 2;
40+
41+
items.forEach(function(h){
42+
var level = Number(h.tagName.slice(1));
43+
if (!tocLevels.has(level)) return;
44+
45+
while (currentLevel < level) {
46+
var parentList = listStack[listStack.length - 1];
47+
var parentLi = parentList.lastElementChild;
48+
if (!parentLi || parentLi.tagName.toLowerCase() !== 'li') {
49+
parentLi = document.createElement('li');
50+
parentLi.textContent = '';
51+
parentList.appendChild(parentLi);
52+
}
53+
var nested = document.createElement('ul');
54+
parentLi.appendChild(nested);
55+
listStack.push(nested);
56+
currentLevel += 1;
57+
}
58+
59+
while (currentLevel > level) {
60+
listStack.pop();
61+
currentLevel -= 1;
62+
}
63+
64+
var li = document.createElement('li');
65+
var a = document.createElement('a');
66+
a.href = '#' + h.id;
67+
a.textContent = (h.textContent || '').trim();
68+
li.appendChild(a);
69+
listStack[listStack.length - 1].appendChild(li);
70+
});
71+
72+
return tocRoot;
73+
}
74+
75+
function replaceTocBlock(root) {
76+
var tocHeading = findTocHeading(root);
77+
if (!tocHeading) return;
78+
79+
var stop = tocHeading.nextElementSibling;
80+
while (stop && stop.tagName.toLowerCase() !== 'hr') {
81+
stop = stop.nextElementSibling;
82+
}
83+
84+
var node = tocHeading.nextSibling;
85+
while (node && node !== stop) {
86+
var next = node.nextSibling;
87+
node.remove();
88+
node = next;
89+
}
90+
91+
ensureTocStyle();
92+
var tocList = buildTocList(root, tocHeading);
93+
if (!tocList) return;
94+
95+
if (stop) {
96+
root.insertBefore(tocList, stop);
97+
} else {
98+
root.appendChild(tocList);
99+
}
100+
}
101+
102+
function render() {
103+
var mdEl = document.getElementById('md');
104+
if (!mdEl) return;
105+
var md = mdEl.textContent;
106+
var rendered = (window.marked && typeof marked.parse === 'function') ? marked.parse(md) : md;
107+
var container = document.getElementById('content');
108+
if (container) container.innerHTML = rendered;
109+
110+
var headings = container ? container.querySelectorAll('h1,h2,h3,h4,h5,h6') : [];
111+
Array.prototype.forEach.call(headings, function(h){
112+
if (!h.id || h.id.trim() === '') {
113+
h.id = slugify(h.textContent || h.innerText || '');
114+
}
115+
});
116+
117+
replaceTocBlock(container);
118+
}
119+
120+
function ensureMarked(cb) {
121+
if (window.marked && typeof marked.parse === 'function') {
122+
cb();
123+
return;
124+
}
125+
var s = document.createElement('script');
126+
s.src = 'https://cdn.jsdelivr.net/npm/marked/marked.min.js';
127+
s.onload = cb;
128+
s.onerror = cb;
129+
document.head.appendChild(s);
130+
}
131+
132+
document.addEventListener('DOMContentLoaded', function(){
133+
ensureMarked(render);
134+
});
135+
})();

0 commit comments

Comments
 (0)