diff --git a/assets/js/shortcuts.js b/assets/js/shortcuts.js index 20894ff7..f5a3c38c 100644 --- a/assets/js/shortcuts.js +++ b/assets/js/shortcuts.js @@ -38,7 +38,7 @@ function scrollHeaders() { // Exit early if shortcuts container doesn't exist if (!shortcuts) return; - const allShortcuts = Array.from(shortcuts.querySelectorAll("div")); + const allShortcuts = Array.from(shortcuts.querySelectorAll("a")); headers.map((currentSection) => { // get the position of the section @@ -116,18 +116,26 @@ function setupShortcuts(shortcutDepth = 2) { return; } - const title = el.innerHTML; const elId = el.id; // Gets the element type (e.g. h2, h3) const elType = el.tagName; - // Adds snake-case title as an id attribute to target element - shortcutsTarget.insertAdjacentHTML( - "beforeend", - `
${title}
`, - ); - const shortcut = document.getElementById(`${elId}-shortcut`); - shortcut.addEventListener("click", () => { + // Copy the heading's content without its links: links cannot nest, and + // the permalink ("#") does not belong in the list. + const title = el.cloneNode(true); + title.querySelectorAll(".headerlink").forEach((a) => a.remove()); + title + .querySelectorAll("a") + .forEach((a) => a.replaceWith(...a.childNodes)); + + const shortcut = document.createElement("a"); + shortcut.id = `${elId}-shortcut`; + shortcut.className = `shortcuts-${elType}`; + shortcut.href = `#${elId}`; + shortcut.append(...title.childNodes); + shortcutsTarget.append(shortcut); + + shortcut.addEventListener("click", (event) => { event.preventDefault(); // We don't want the shortcuts to flash through highlights while @@ -137,10 +145,10 @@ function setupShortcuts(shortcutDepth = 2) { // Replace what's in the location bar, without changing browser history // and without triggering a page scroll history.replaceState(null, null, `#${elId}`); - const shortcutDivs = Array.from( - document.querySelectorAll("#shortcuts > div"), + const shortcutLinks = Array.from( + document.querySelectorAll("#shortcuts > a"), ); - shortcutDivs.forEach((e) => e.classList.remove("active")); + shortcutLinks.forEach((e) => e.classList.remove("active")); shortcut.classList.add("active"); let headerOffset = el.offsetTop - 60; @@ -155,9 +163,7 @@ function setupShortcuts(shortcutDepth = 2) { // Removes the shortcuts container if no shortcuts exist. // Also removes the 'Get Help' link. - const shortcuts = Array.from( - document.querySelectorAll("#shortcuts div:not(#shortcuts-header)"), - ); + const shortcuts = Array.from(document.querySelectorAll("#shortcuts a")); const shortcutsContainer = document.getElementById("shortcuts-container"); if (shortcutsContainer) { diff --git a/assets/theme-css/shortcuts.css b/assets/theme-css/shortcuts.css index b3fd540f..6beaf5c5 100644 --- a/assets/theme-css/shortcuts.css +++ b/assets/theme-css/shortcuts.css @@ -19,13 +19,19 @@ padding-bottom: 0.25rem; } -#shortcuts > div:not(.active) { +#shortcuts > a { + display: block; + color: inherit; + text-decoration: none; +} + +#shortcuts > a:not(.active) { margin-left: 0.125rem; border-left: 1px solid var(--pst-color-text-base); opacity: 0.75; } -#shortcuts > div.active { +#shortcuts > a.active { font-weight: bold; color: var(--pst-color-text-base); border-left: 5px solid var(--pst-color-text-base);