-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhljs-enhance.js
executable file
·36 lines (31 loc) · 1.31 KB
/
hljs-enhance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// hljs-enhance - https://github.com/center-key/hljs-enhance - MIT License
const hljsEnhance = {
setup() {
// <figure class=hljs-enhance>
// <figcaption>Title</figcaption>
// <pre><code class="language-javascript hljs hljs-done>
// ...
const init = (elem) => {
const indent = ' ';
const padding = new RegExp(elem.textContent.match(/\n[ \t]+/), 'g');
elem.textContent = elem.textContent.replace(padding, '\n').replace(/\t/g, indent).trim();
elem.parentElement.classList.add('hljs-enhance');
elem.closest('figure')?.classList.add('hljs-enhance');
globalThis.hljs.highlightElement(elem);
elem.classList.add('hljs-done');
};
globalThis.document.querySelectorAll('pre code:not(.hljs-done)').forEach(init);
globalThis.document.querySelectorAll('figure.hljs-enhance >div')
.forEach(elem => elem.classList.add('hljs')); //trim boxes without code
},
onDomReady(callback) {
const state = globalThis.document?.readyState;
if (state === 'complete' || !state)
callback();
else
globalThis.window.addEventListener('DOMContentLoaded', callback);
return state;
},
};
globalThis.hljsEnhance = hljsEnhance;
hljsEnhance.onDomReady(hljsEnhance.setup);