Skip to content

Commit 1c37a82

Browse files
committed
Refactor header rendering and add clickable headers with highlight effect
1 parent 8d0d6bc commit 1c37a82

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

index.js

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,9 @@ class DOMService {
197197
}
198198

199199
createFolderHeaderWithFile(iconClass, doc) {
200-
const iconStyle = doc.color ? `color: ${doc.color};` : '';
201200
return `
202201
<div class="folder-icons">
203-
<i class="${iconClass} folder-icon" style="${iconStyle}"></i>
202+
<i class="${iconClass} folder-icon"></i>
204203
</div>
205204
<span>${doc.title}</span>
206205
<a href="?${doc.slug}" class="folder-link" title="View folder page">
@@ -209,10 +208,9 @@ class DOMService {
209208
}
210209

211210
createFolderHeaderBasic(iconClass, doc) {
212-
const iconStyle = doc.color ? `color: ${doc.color};` : '';
213211
return `
214212
<div class="folder-icons">
215-
<i class="${iconClass} folder-icon" style="${iconStyle}"></i>
213+
<i class="${iconClass} folder-icon"></i>
216214
</div>
217215
<span>${doc.title}</span>`;
218216
}
@@ -346,6 +344,20 @@ class DocumentService {
346344
}
347345
return link.replace(/^<a /, `<a${attrs} `);
348346
};
347+
348+
// Add custom heading renderer to make headers clickable - without link icon
349+
const originalHeading = renderer.heading.bind(renderer);
350+
renderer.heading = (text, level) => {
351+
const escapedText = text.toLowerCase()
352+
.replace(/[^\w\s-]/g, '')
353+
.replace(/\s+/g, '-');
354+
355+
const id = escapedText;
356+
357+
return `<h${level} id="${id}" class="clickable-header">
358+
${text}
359+
</h${level}>`;
360+
};
349361
}
350362

351363
renderCode(code, language) {
@@ -540,17 +552,6 @@ class Documentation {
540552
const data = await response.json();
541553
this.indexData = data;
542554
window._indexData = data;
543-
544-
// Load custom CSS files if specified
545-
if (data.customCSS) {
546-
const cssFiles = Array.isArray(data.customCSS) ? data.customCSS : [data.customCSS];
547-
cssFiles.forEach(cssFile => {
548-
const link = document.createElement('link');
549-
link.rel = 'stylesheet';
550-
link.href = cssFile;
551-
document.head.appendChild(link);
552-
});
553-
}
554555

555556
this.populateAuthorInfo(data.author);
556557
window.originalDocTitle = data.metadata.site_name || 'Documentation';
@@ -636,6 +637,23 @@ class Documentation {
636637
const headings = document.querySelectorAll('h2, h3, h4, h5, h6');
637638
const headingLinks = this.domService.createOutline(headings);
638639

640+
// Add click handlers to all headers
641+
headings.forEach(heading => {
642+
heading.addEventListener('click', (e) => {
643+
// Don't trigger if clicking on fold toggle
644+
if (e.target.closest('svg') || e.target.closest('.header-anchor')) return;
645+
646+
const id = heading.id;
647+
if (id) {
648+
history.pushState(null, '', `${window.location.pathname}${window.location.search}#${id}`);
649+
this.domService.scrollToElement(heading);
650+
heading.classList.remove('highlight');
651+
void heading.offsetWidth;
652+
heading.classList.add('highlight');
653+
}
654+
});
655+
});
656+
639657
this.setupScrollObserver(headings, headingLinks);
640658

641659
window._currentPath = path;

styles.css

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,9 @@ body {
261261
color: var(--ifm-color-content-secondary);
262262
font-size: 1.2rem;
263263
transition: color 0.2s;
264-
padding: 0.5rem;
265264
}
266265

267-
.social-links a:hover {
266+
social-links a:hover {
268267
color: var(--ifm-color-primary);
269268
}
270269

@@ -568,3 +567,18 @@ body {
568567
.github-link a:hover {
569568
color: var(--ifm-color-primary);
570569
}
570+
571+
.clickable-header {
572+
cursor: pointer;
573+
position: relative;
574+
}
575+
.clickable-header:hover {
576+
color: #3498db;
577+
}
578+
.clickable-header.highlight {
579+
animation: highlight-fade 1.5s;
580+
}
581+
@keyframes highlight-fade {
582+
0% { background-color: rgba(52, 152, 219, 0.2); }
583+
100% { background-color: transparent; }
584+
}

0 commit comments

Comments
 (0)