Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lib/rdoc/generator/template/aliki/js/aliki.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ function generateToc() {
link.setAttribute('data-target', heading.id);

li.appendChild(link);
setHeadingScrollHandler(heading, link);
tocList.appendChild(li);
});

Expand Down Expand Up @@ -271,21 +272,30 @@ function hookTocActiveHighlighting() {
targetHeadings.forEach((heading) => {
observer.observe(heading);
});
}

// Smooth scroll when clicking TOC links
tocLinks.forEach((link) => {
link.addEventListener('click', (e) => {
e.preventDefault();
const targetId = link.getAttribute('data-target');
const targetHeading = document.getElementById(targetId);
if (targetHeading) {
targetHeading.scrollIntoView({ behavior: 'smooth', block: 'start' });
history.pushState(null, '', `#${targetId}`);
}
});
function setHeadingScrollHandler(heading, link) {
// Smooth scroll to heading when clicking link
if (!heading.id) return;

link.addEventListener('click', (e) => {
e.preventDefault();
heading.scrollIntoView({ behavior: 'smooth', block: 'start' });
history.pushState(null, '', `#${heading.id}`);
});
}

function setHeadingSelfLinkScrollHandlers() {
// Clicking link inside heading scrolls smoothly to heading itself
const headings = document.querySelectorAll('h1, h2, h3, h4, h5, h6');
headings.forEach((heading) => {
if (!heading.id) return;

const link = heading.querySelector(`a[href^="#${heading.id}"]`);
if (link) setHeadingScrollHandler(heading, link);
})
}

/* ===== Mobile Search Modal ===== */

function hookSearchModal() {
Expand Down Expand Up @@ -466,6 +476,7 @@ document.addEventListener('DOMContentLoaded', () => {
hookFocus();
hookSidebar();
generateToc();
setHeadingSelfLinkScrollHandlers();
hookTocActiveHighlighting();
hookSearchModal();
wrapCodeBlocksWithCopyButton();
Expand Down