|
| 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