Skip to content

Commit

Permalink
Run syntax highlighting only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Oct 3, 2024
1 parent 823c124 commit 80eb9cf
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions util/gh-pages/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ function onEachLazy(lazyArray, func) {
}
}

function highlightIfNeeded(elem) {
onEachLazy(elem.querySelectorAll("pre > code.language-rust:not(.highlighted)"), el => {
hljs.highlightElement(el)
el.classList.add("highlighted");
});
}

function expandLintId(lintId) {
searchState.inputElem.value = lintId;
searchState.filterLints();
Expand All @@ -146,7 +153,7 @@ function expandLintId(lintId) {
const lintElem = document.getElementById(lintId);
const isCollapsed = lintElem.classList.remove("collapsed");
lintElem.querySelector(".label-doc-folding").innerText = "-";
onEachLazy(lintElem.querySelectorAll("pre > code.language-rust"), el => hljs.highlightElement(el));
highlightIfNeeded(lintElem);
}

// Show details for one lint
Expand All @@ -160,7 +167,7 @@ function expandLint(lintId) {
const lintElem = document.getElementById(lintId);
const isCollapsed = lintElem.classList.toggle("collapsed");
lintElem.querySelector(".label-doc-folding").innerText = isCollapsed ? "+" : "-";
onEachLazy(lintElem.querySelectorAll("pre > code.language-rust"), el => hljs.highlightElement(el));
highlightIfNeeded(lintElem);
}

function copyToClipboard(event) {
Expand Down Expand Up @@ -198,7 +205,10 @@ function handleBlur(event, elementId) {
function toggleExpansion(expand) {
onEachLazy(
document.querySelectorAll("article"),
expand ? el => el.classList.remove("collapsed") : el => el.classList.add("collapsed"),
expand ? el => {
el.classList.remove("collapsed");
highlightIfNeeded(el);
} : el => el.classList.add("collapsed"),
);
}

Expand Down

0 comments on commit 80eb9cf

Please sign in to comment.