Skip to content

Commit b8acedc

Browse files
committed
nav.js: use CodeMirror for reference syntax blocks, same theme as editor
1 parent 1e3e788 commit b8acedc

1 file changed

Lines changed: 43 additions & 35 deletions

File tree

assets/nav.js

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -87,38 +87,46 @@
8787
document.head.appendChild(s);
8888
}
8989

90-
// Syntax highlighting on reference/example pages
91-
if (!document.getElementById('hljs-css') &&
92-
document.querySelector('.syntax-block, .impl-block, .cm-static-wrap')) {
93-
const lnk = document.createElement('link');
94-
lnk.id = 'hljs-css';
95-
lnk.rel = 'stylesheet';
96-
lnk.href = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-light.min.css';
97-
document.head.appendChild(lnk);
98-
99-
const lnkDark = document.createElement('link');
100-
lnkDark.id = 'hljs-css-dark';
101-
lnkDark.rel = 'stylesheet';
102-
lnkDark.href = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css';
103-
lnkDark.disabled = true;
104-
document.head.appendChild(lnkDark);
105-
106-
const scr = document.createElement('script');
107-
scr.src = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js';
108-
scr.onload = function() {
109-
document.querySelectorAll('.syntax-block, .impl-block, .cm-static-wrap code').forEach(el => {
110-
el.innerHTML = el.innerHTML
111-
.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&');
112-
el.classList.add('language-cpp');
113-
hljs.highlightElement(el);
90+
// CodeMirror syntax highlighting on reference/example pages
91+
if (document.querySelector('.syntax-block, .impl-block') &&
92+
!document.getElementById('ref-cm-loaded')) {
93+
const CDNJS = 'https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16';
94+
function loadScript(src, cb) {
95+
const s = document.createElement('script'); s.src = src;
96+
s.onload = cb; document.head.appendChild(s);
97+
}
98+
function loadLink(href) {
99+
const l = document.createElement('link');
100+
l.rel = 'stylesheet'; l.href = href;
101+
document.head.appendChild(l);
102+
}
103+
loadLink(CDNJS + '/codemirror.min.css');
104+
loadLink(SITE + '/assets/cppmode-theme.css');
105+
loadScript(CDNJS + '/codemirror.min.js', function() {
106+
loadScript(CDNJS + '/mode/clike/clike.min.js', function() {
107+
loadScript(SITE + '/assets/cppmode-keywords.js', function() {
108+
loadScript(SITE + '/assets/cppmode.js', function() {
109+
const marker = document.createElement('span');
110+
marker.id = 'ref-cm-loaded';
111+
document.head.appendChild(marker);
112+
const dark = document.body.classList.contains('dark-mode');
113+
document.querySelectorAll('.syntax-block, .impl-block').forEach(el => {
114+
const code = el.innerHTML
115+
.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').trim();
116+
const wrap = document.createElement('div');
117+
el.parentNode.replaceChild(wrap, el);
118+
CodeMirror(wrap, {
119+
value: code,
120+
mode: 'cppmode',
121+
theme: dark ? 'cppmode-dark' : 'cppmode',
122+
readOnly: true,
123+
lineNumbers: false,
124+
});
125+
});
126+
});
127+
});
114128
});
115-
// Apply dark if already in dark mode
116-
if (document.body.classList.contains('dark-mode')) {
117-
document.getElementById('hljs-css').disabled = true;
118-
document.getElementById('hljs-css-dark').disabled = false;
119-
}
120-
};
121-
document.head.appendChild(scr);
129+
});
122130
}
123131
// Dark mode via CSS class
124132
(function() {
@@ -215,10 +223,10 @@
215223
}
216224
if (window.editor && window.editor.setOption)
217225
window.editor.setOption('theme', on ? 'cppmode-dark' : 'cppmode');
218-
const hljsLight = document.getElementById('hljs-css');
219-
const hljsDark = document.getElementById('hljs-css-dark');
220-
if (hljsLight) hljsLight.disabled = on;
221-
if (hljsDark) hljsDark.disabled = !on;
226+
// Switch static CodeMirror blocks theme
227+
document.querySelectorAll('.CodeMirror').forEach(el => {
228+
if (el.CodeMirror) el.CodeMirror.setOption('theme', on ? 'cppmode-dark' : 'cppmode');
229+
});
222230
try { localStorage.setItem('darkMode', on ? '1' : '0'); } catch(e) {}
223231
}
224232
window.__toggleDark = function() {

0 commit comments

Comments
 (0)