File size: 1,762 Bytes
2d1d74f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
document.addEventListener('DOMContentLoaded', () => {
    // Highlight TOC and Sidebar based on scroll position
    const mainContent = document.getElementById('main-content');
    const sections = document.querySelectorAll('section[id]');
    
    // Sidebar Link Logic
    const sidebarLinks = document.querySelectorAll('.sidebar-link');
    const currentUrl = window.location.pathname;
    
    sidebarLinks.forEach(link => {
        // Simple check for demo purposes
        if(link.getAttribute('href') === currentUrl || (currentUrl === '/' && link.getAttribute('href') === '/index.html')) {
            link.classList.add('nav-item-active', 'font-medium');
            link.classList.remove('text-muted', 'hover:text-primary');
        }
    });

    // Intersection Observer for TOC
    const observerOptions = {
        root: mainContent,
        rootMargin: '-20% 0px -70% 0px', // Trigger when section is near top
        threshold: 0
    };

    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                const id = entry.target.getAttribute('id');
                
                // Update TOC
                document.querySelectorAll('.toc-link').forEach(link => {
                    link.classList.remove('toc-item-active', 'font-medium');
                    link.classList.add('text-muted');
                    if (link.getAttribute('href') === `#${id}`) {
                        link.classList.add('toc-item-active', 'font-medium');
                        link.classList.remove('text-muted');
                    }
                });
            }
        });
    }, observerOptions);

    sections.forEach(section => observer.observe(section));
});