1
Fork 0

rustdoc: dynamically show-hide line numbers on code examples

This commit is contained in:
Michael Howell 2022-09-21 15:43:38 -07:00
parent 7a718f3be2
commit f66769fe8d
3 changed files with 54 additions and 11 deletions

View file

@ -697,20 +697,39 @@ function loadCss(cssFileName) {
}
}());
window.rustdoc_add_line_numbers_to_examples = () => {
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
const parent = x.parentNode;
const line_numbers = parent.querySelectorAll(".line-number");
if (line_numbers.length > 0) {
return;
}
const count = x.textContent.split("\n").length;
const elems = [];
for (let i = 0; i < count; ++i) {
elems.push(i + 1);
}
const node = document.createElement("pre");
addClass(node, "line-number");
node.innerHTML = elems.join("\n");
parent.insertBefore(node, x);
});
};
window.rustdoc_remove_line_numbers_from_examples = () => {
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
const parent = x.parentNode;
const line_numbers = parent.querySelectorAll(".line-number");
for (const node of line_numbers) {
parent.removeChild(node);
}
});
};
(function() {
// To avoid checking on "rustdoc-line-numbers" value on every loop...
if (getSettingValue("line-numbers") === "true") {
onEachLazy(document.getElementsByClassName("rust-example-rendered"), x => {
const count = x.textContent.split("\n").length;
const elems = [];
for (let i = 0; i < count; ++i) {
elems.push(i + 1);
}
const node = document.createElement("pre");
addClass(node, "line-number");
node.innerHTML = elems.join("\n");
x.parentNode.insertBefore(node, x);
});
window.rustdoc_add_line_numbers_to_examples();
}
}());