1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-23 19:49:45 +01:00
home-manager/highlight.load.js

36 lines
1 KiB
JavaScript
Raw Normal View History

/* This file is NOT part of highlight.js */
class PreserveCallouts {
constructor(options) {
self.callouts = [];
/* Using OBJECT REPLACEMENT CHARACTER as a marker of where the callout
should be inserted. We hope that this won't cause conflicts. */
self.marker = '\u{FFFC}';
}
'before:highlightElement'({el, language}) {
const re = /<a id="[^"]+"><\/a><span><img src="images\/callouts\/\d+.svg" alt="\d+" border="0"><\/span>/g;
const array = [...el.innerHTML.matchAll(re)];
if (array.length > 0) {
self.callouts = array;
el.innerHTML = el.innerHTML.replaceAll(re, self.marker);
}
}
'after:highlightElement'({ el, result, text }) {
if (self.callouts.length > 0) {
el.innerHTML = el.innerHTML.replaceAll(
self.marker, (str) => self.callouts.shift());
}
}
}
document.addEventListener('DOMContentLoaded', (event) => {
hljs.addPlugin(new PreserveCallouts());
document.querySelectorAll('pre.programlisting, pre.screen').forEach((el) => {
hljs.highlightElement(el);
});
});