summaryrefslogtreecommitdiff
path: root/layouts/_partials/posts/toc.html
blob: 794328c9723678fd52ea874e692caac88de48089 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{{ if .Params.toc }}
<div id="toc-container"><div id="toc">{{ .TableOfContents }}</div></div>
<style>
/* toc */
#toc-container {
    position: absolute;
    bottom: 0;
    right: calc(50% + var(--page-width) / 2);
    left: auto;
    height: 100%;
    top: 12rem;
    @media screen and (max-width: 1200px) {
        display: none
    }
}

#toc {
    /* position */
    position: sticky;
    top: 3rem;
    /* styling */
    font-size: 1rem;
    padding: 0 2rem;
    @media screen and (max-width: 1500px) {
        font-size: 0.825rem;
    }

    ul {
        margin-block-start: 0;
        margin-block-end: 0;
        padding-inline-start: 1rem;

        overflow: hidden;
        position: relative;
        li {
            list-style: none;

            & > a {
                color: var(--secondary-text-color);
                text-decoration: none;
                transition: color .3s;
                display: inline-flex;
                align-items: center;
                gap: .5rem;
                &::before {
                    content: '';
                    display: inline-block;
                    width: 1rem;
                    height: .3rem;
                    background-color: var(--block-background-color);
                    border-radius: .2rem;
                    transition: background .3s;
                }
            }

            /* nested list */
            ul {
                max-height: 25rem;
                overflow-y: auto
            }

            /* active styles */
            &:not(.active) ul {
                max-height: 0
            }
            a:hover,
            &.active > a {
                color: var(--text-color);
            }

            &.active > a::before {
                background-color: var(--secondary-text-color);
            }
        }
    }
}
</style>
<script>
document.addEventListener('DOMContentLoaded', () => {
  const toc = document.getElementById('toc');
  if (!toc) return;

  const headings = document.querySelectorAll('h2, h3');
  const observer = new IntersectionObserver(entries => {
    entries.forEach(entry => {
      if (!entry.isIntersecting) return;

      const id = entry.target.id;
      const tocLink = toc.querySelector(`a[href="#${id}"]`);
      if (!tocLink) return;

      const listItem = tocLink.parentElement;
      const isH2 = entry.target.tagName === 'H2';

      // Clear all active states when a new H2 is encountered
      if (isH2) {
        toc.querySelectorAll('li.active').forEach(el => el.classList.remove('active'));
      }

      // Set current item as active
      listItem.classList.add('active');

      // If this is an H3, find and activate its parent H2
      if (!isH2) {
        const parentH2Item = listItem.closest('ul').previousElementSibling?.closest('li');
        if (parentH2Item) parentH2Item.classList.add('active');
      }
    });
  }, {
    rootMargin: '0px 0px -50% 0px',
    threshold: 0.5
  });

  headings.forEach(heading => observer.observe(heading));
});
</script>
{{ end }}