thp-lang.org/src/layouts/thpHighlighter.ts

32 lines
958 B
TypeScript
Raw Normal View History

2024-05-28 21:12:00 +00:00
import { thp_highlighter, CodeJar } from "../lexer/highlighter";
/**
* Highlights all THP code snippets mounted in the DOM with class .language-thp
*
* It assumes that the code is inside: <pre class="language-thp"><code>...
*/
export function highlightOnDom() {
const code_elements = document.querySelectorAll("code.language-thp");
for (const e of [...code_elements]) {
const el = e as HTMLElement;
// Some elements with .language-thp don't want to be processed
if (e.getAttribute("data-disabled") !== null) {
continue;
}
const pre_parent = el.parentElement!;
const new_div = document.createElement("div");
const code = el.innerText;
el.parentElement!.className = "language-thp";
pre_parent.removeChild(el);
pre_parent.appendChild(new_div);
CodeJar(new_div, thp_highlighter, {
tab: " ",
}).updateCode(code);
}
}