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