Skip to content

Commit 80eb9cf

Browse files
Run syntax highlighting only when needed
1 parent 823c124 commit 80eb9cf

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

Diff for: util/gh-pages/script.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,13 @@ function onEachLazy(lazyArray, func) {
138138
}
139139
}
140140

141+
function highlightIfNeeded(elem) {
142+
onEachLazy(elem.querySelectorAll("pre > code.language-rust:not(.highlighted)"), el => {
143+
hljs.highlightElement(el)
144+
el.classList.add("highlighted");
145+
});
146+
}
147+
141148
function expandLintId(lintId) {
142149
searchState.inputElem.value = lintId;
143150
searchState.filterLints();
@@ -146,7 +153,7 @@ function expandLintId(lintId) {
146153
const lintElem = document.getElementById(lintId);
147154
const isCollapsed = lintElem.classList.remove("collapsed");
148155
lintElem.querySelector(".label-doc-folding").innerText = "-";
149-
onEachLazy(lintElem.querySelectorAll("pre > code.language-rust"), el => hljs.highlightElement(el));
156+
highlightIfNeeded(lintElem);
150157
}
151158

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

166173
function copyToClipboard(event) {
@@ -198,7 +205,10 @@ function handleBlur(event, elementId) {
198205
function toggleExpansion(expand) {
199206
onEachLazy(
200207
document.querySelectorAll("article"),
201-
expand ? el => el.classList.remove("collapsed") : el => el.classList.add("collapsed"),
208+
expand ? el => {
209+
el.classList.remove("collapsed");
210+
highlightIfNeeded(el);
211+
} : el => el.classList.add("collapsed"),
202212
);
203213
}
204214

0 commit comments

Comments
 (0)