Files
Graphite/website/static/js/text-justification.js
Keavon Chambers 45f0d21f25 Lint the website with the frontend's ESLint config instead of a separate copy, selecting elements by data attributes (#4655)
* Lint the website with the frontend's ESLint config instead of a diverging copy, selecting elements by data attributes

* Give the crate graph's marker attribute a value, cache npm in the website workflow, and mention installing the website's packages
2026-10-07 16:05:42 -07:00

33 lines
1.1 KiB
JavaScript

window.addEventListener("DOMContentLoaded", () => {
// eslint-disable-next-line graphite/require-data-selectors -- Markdown writes these paragraphs, which leaves no way to mark them
document.querySelectorAll("section p").forEach((paragraph) => {
const /** @type {[Text, NodeList][]} */ mutationQueue = [];
// Recursively traverse the DOM tree and modify the text nodes
const recursivelyAddWbr = (/** @type {ChildNode} **/ node) => {
if (node.nodeType === Node.TEXT_NODE) {
if (!(node instanceof Text)) return;
const newNodes = node.textContent.split("/");
for (let i = 0; i < newNodes.length - 1; i++) {
newNodes[i] += "/";
}
const tempSpan = document.createElement("span");
tempSpan.innerHTML = newNodes.join("<wbr>");
const replacementNodes = tempSpan.childNodes;
mutationQueue.push([node, replacementNodes]);
} else {
node.childNodes.forEach(recursivelyAddWbr);
}
};
// Perform the recursive traversal
recursivelyAddWbr(paragraph);
// Replace the text nodes
mutationQueue.forEach(([node, newNodes]) => node.replaceWith(...newNodes));
});
});