barrettruth.com/scripts/post.js

37 lines
1.2 KiB
JavaScript

const urlToTopic = () => {
return new URL(window.location.href).pathname.split("/")[2];
};
document.documentElement.style.setProperty(
"--topic-color",
getTopicColor(urlToTopic()),
);
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll("article h2").forEach((h2) => {
const mdHeading = document.createElement("span");
mdHeading.textContent = "# ";
mdHeading.style.color = getTopicColor(urlToTopic());
h2.prepend(mdHeading);
});
document.querySelectorAll(".problem-header h3").forEach((h3, i) => {
const toggle = document.createElement("span");
toggle.textContent = i === 0 ? "v" : ">";
h3.parentElement.nextElementSibling.style.display =
toggle.textContent === ">" ? "none" : "block";
toggle.classList.add("problem-toggle");
toggle.addEventListener("click", () => {
const content = h3.parentElement.nextElementSibling;
toggle.textContent = toggle.textContent === ">" ? "v" : ">";
content.style.display = toggle.textContent === ">" ? "none" : "block";
});
const mdHeading = document.createElement("span");
mdHeading.textContent = "## ";
mdHeading.style.color = getTopicColor(urlToTopic());
h3.prepend(mdHeading);
h3.prepend(toggle);
});
});