// Lazy, bounded projection. No CDN, HTML injection, or document writes. const selector = '.page-html pre code, .periodical-body pre code, #preview pre code, .preview pre code, .preview-content pre code, .public-document pre code'; const seen = new WeakMap(), queue = new Set(); let scheduled = false, bundle; const idle = callback => window.requestIdleCallback ? requestIdleCallback(callback, {timeout:1000}) : setTimeout(callback, 0); function language(code) { return [...code.classList, ...code.parentElement.classList].find(c => /^(language|lang)-/.test(c))?.replace(/^(language|lang)-/, '') || ''; } function schedule() { if (scheduled || !queue.size) return; scheduled = true; idle(async () => { try { const code = queue.values().next().value; queue.delete(code); if (!code?.isConnected) return; const text = code.textContent, mode = language(code); const signature = mode + '\0' + text; if (!mode || text.length > 32768 || seen.get(code) === signature) return; bundle ||= import('/static/vendor/codemirror/editor.bundle.js?v=command-ux-1'); await bundle; if (!code.isConnected || code.textContent !== text || language(code) !== mode) { watch(code); return; } seen.set(code, signature); window.PagesWikiEditor.highlightCodeBlock(code, mode); } catch (_) { /* Keep the readable plain-text fallback. */ } finally { scheduled = false; schedule(); } }); } const visible = 'IntersectionObserver' in window ? new IntersectionObserver(entries => { for (const entry of entries) if (entry.isIntersecting) { visible.unobserve(entry.target); queue.add(entry.target); } schedule(); }, {rootMargin:'100px'}) : null; function watch(code) { if (!language(code) || code.textContent.length > 32768 || seen.get(code) === language(code) + '\0' + code.textContent) return; if (visible) visible.observe(code); else { queue.add(code); schedule(); } } function scan(root) { if (root.matches?.(selector)) watch(root); root.querySelectorAll?.(selector).forEach(watch); } scan(document); new MutationObserver(records => { for (const record of records) { // Preview replacements and fetched projections are processed incrementally. for (const node of record.addedNodes) if (node.nodeType === 1) scan(node); for (const node of record.removedNodes) if (node.nodeType === 1) { const release = code => { visible?.unobserve(code); queue.delete(code); }; if (node.matches('code')) release(node); node.querySelectorAll('code').forEach(release); } if (record.type === 'attributes' && record.target.matches('pre,code')) scan(record.target); const code = record.target.nodeType === 1 ? record.target.closest?.('pre code') : record.target.parentElement?.closest('pre code'); if (code?.matches(selector)) watch(code); } }).observe(document.body, {childList:true, subtree:true, characterData:true, attributes:true, attributeFilter:['class']});