|
1 | | -/* code-highlight.js |
2 | | - Converts static code blocks to read-only CodeMirror instances. |
3 | | - Handles: <pre><code>, <pre>, .syntax-block, .impl-block |
4 | | - Loaded at end of <body> so DOM is already available -- no DOMContentLoaded needed. */ |
5 | | - |
6 | 1 | (function () { |
7 | 2 | function highlight() { |
8 | | - // <pre> and <pre><code> blocks |
| 3 | + // Handle <pre> blocks |
9 | 4 | document.querySelectorAll('pre').forEach(function (pre) { |
10 | 5 | if (pre.dataset.cmDone) return; |
11 | 6 | pre.dataset.cmDone = '1'; |
12 | 7 | var code = pre.querySelector('code'); |
13 | | - var text = code ? code.textContent : pre.textContent; |
| 8 | + var text = (code ? code.textContent : pre.textContent).replace(/^\n/, ''); |
14 | 9 | var wrapper = document.createElement('div'); |
15 | 10 | wrapper.className = 'cm-static-wrap'; |
16 | | - wrapper.style.cssText = 'margin-bottom:1.25rem;border-radius:8px;overflow:hidden;'; |
17 | 11 | pre.parentNode.insertBefore(wrapper, pre); |
18 | 12 | pre.style.display = 'none'; |
19 | | - var cm = CodeMirror(wrapper, { |
20 | | - value: text.replace(/^\n/, ''), |
| 13 | + CodeMirror(wrapper, { |
| 14 | + value: text, |
21 | 15 | mode: 'cppmode', |
22 | 16 | theme: 'cppmode', |
23 | 17 | readOnly: true, |
|
26 | 20 | scrollbarStyle: 'null', |
27 | 21 | viewportMargin: Infinity, |
28 | 22 | }); |
29 | | - cm.getWrapperElement().style.fontSize = '13px'; |
30 | 23 | }); |
31 | 24 |
|
32 | | - // .syntax-block divs (reference pages - function signatures) |
| 25 | + // Handle .syntax-block divs (reference pages) |
33 | 26 | document.querySelectorAll('.syntax-block').forEach(function (el) { |
34 | 27 | if (el.dataset.cmDone) return; |
35 | 28 | el.dataset.cmDone = '1'; |
36 | | - var text = el.textContent; |
| 29 | + var text = el.textContent.replace(/^\n/, ''); |
37 | 30 | var wrapper = document.createElement('div'); |
38 | 31 | wrapper.className = 'cm-static-wrap cm-syntax-wrap'; |
39 | | - wrapper.style.cssText = 'margin-bottom:0.5rem;border-radius:6px;overflow:hidden;background:#f8f8f8;'; |
40 | 32 | el.parentNode.insertBefore(wrapper, el); |
41 | 33 | el.style.display = 'none'; |
42 | | - var cm = CodeMirror(wrapper, { |
43 | | - value: text.replace(/^\n/, '').trim(), |
| 34 | + CodeMirror(wrapper, { |
| 35 | + value: text, |
44 | 36 | mode: 'cppmode', |
45 | 37 | theme: 'cppmode', |
46 | 38 | readOnly: true, |
|
49 | 41 | scrollbarStyle: 'null', |
50 | 42 | viewportMargin: Infinity, |
51 | 43 | }); |
52 | | - cm.getWrapperElement().style.fontSize = '13px'; |
53 | 44 | }); |
54 | 45 |
|
55 | | - // .impl-block divs (reference pages - implementation examples) |
| 46 | + // Handle .impl-block divs (reference pages - dark background) |
56 | 47 | document.querySelectorAll('.impl-block').forEach(function (el) { |
57 | 48 | if (el.dataset.cmDone) return; |
58 | 49 | el.dataset.cmDone = '1'; |
59 | | - var text = el.textContent; |
| 50 | + var text = el.textContent.replace(/^\n/, ''); |
60 | 51 | var wrapper = document.createElement('div'); |
61 | 52 | wrapper.className = 'cm-static-wrap cm-impl-wrap'; |
62 | | - wrapper.style.cssText = 'margin-bottom:1.25rem;border-radius:6px;overflow:hidden;max-height:400px;'; |
63 | 53 | el.parentNode.insertBefore(wrapper, el); |
64 | 54 | el.style.display = 'none'; |
65 | | - var cm = CodeMirror(wrapper, { |
66 | | - value: text.replace(/^\n/, ''), |
| 55 | + CodeMirror(wrapper, { |
| 56 | + value: text, |
67 | 57 | mode: 'cppmode', |
68 | 58 | theme: 'cppmode', |
69 | 59 | readOnly: true, |
|
72 | 62 | scrollbarStyle: 'native', |
73 | 63 | viewportMargin: Infinity, |
74 | 64 | }); |
75 | | - cm.getWrapperElement().style.fontSize = '12px'; |
76 | 65 | }); |
77 | 66 | } |
78 | 67 |
|
79 | | - // Scripts load at end of body so DOM is ready -- call immediately |
80 | | - // but also handle the rare case where this loads early |
81 | 68 | if (document.readyState === 'loading') { |
82 | 69 | document.addEventListener('DOMContentLoaded', highlight); |
83 | 70 | } else { |
|
0 commit comments