@zachleat/table-of-contents 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -0
- package/package.json +32 -0
- package/table-of-contents.js +149 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Zach Leatherman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# `<table-of-contents>`
|
|
2
|
+
|
|
3
|
+
A zero-dependency web component that client renders a table of contents from the headings on the page and highlights the sections currently in view.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @zachleat/table-of-contents
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```html
|
|
14
|
+
<script type="module" src="table-of-contents.js"></script>
|
|
15
|
+
|
|
16
|
+
<nav aria-labelledby="toc-title">
|
|
17
|
+
<table-of-contents>
|
|
18
|
+
<h2 id="toc-title">Table of Contents</h2>
|
|
19
|
+
</table-of-contents>
|
|
20
|
+
</nav>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Child content is kept and the list is added after it. Wrap it in a labeled `<nav>` so assistive technology can find it. The element is hidden when no headings match.
|
|
24
|
+
|
|
25
|
+
Add `?nodefine` to skip auto-registration and define it yourself:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { TableOfContents } from "./table-of-contents.js?nodefine";
|
|
29
|
+
|
|
30
|
+
TableOfContents.define("my-toc");
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
`?nodefine` reads `import.meta.url`, so it only works with unbundled ES modules.
|
|
34
|
+
|
|
35
|
+
## Attributes
|
|
36
|
+
|
|
37
|
+
- `selector`: headings to include (default `main :is(h2, h3, h4, h5, h6)`). Headings without an `id` are listed without a link.
|
|
38
|
+
|
|
39
|
+
## Styling
|
|
40
|
+
|
|
41
|
+
No styles are included. Links for sections in view get the `toc-in-view` class.
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@zachleat/table-of-contents",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A zero-dependency web component that client renders a table of contents from the headings on the page.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "table-of-contents.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./table-of-contents.js",
|
|
9
|
+
"./table-of-contents.js": "./table-of-contents.js"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"table-of-contents.js"
|
|
13
|
+
],
|
|
14
|
+
"keywords": [
|
|
15
|
+
"web-components",
|
|
16
|
+
"custom-elements",
|
|
17
|
+
"table-of-contents"
|
|
18
|
+
],
|
|
19
|
+
"author": "Zach Leatherman",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"repository": {
|
|
25
|
+
"type": "git",
|
|
26
|
+
"url": "git+https://github.com/zachleat/table-of-contents.git"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/zachleat/table-of-contents#readme",
|
|
29
|
+
"bugs": {
|
|
30
|
+
"url": "https://github.com/zachleat/table-of-contents/issues"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
export class TableOfContents extends HTMLElement {
|
|
2
|
+
static tagName = "table-of-contents";
|
|
3
|
+
|
|
4
|
+
static defaults = {
|
|
5
|
+
selector: "main :is(h2, h3, h4, h5, h6)",
|
|
6
|
+
activeClass: "toc-in-view",
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
static define(tagName = this.tagName, registry = globalThis.customElements) {
|
|
10
|
+
if(registry && !registry.get(tagName)) {
|
|
11
|
+
registry.define(tagName, this);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static getLevel(heading) {
|
|
16
|
+
return parseInt(heading.tagName.slice(1), 10);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Ignores same-page anchor links and hidden decorations inside headings.
|
|
20
|
+
static getText(heading) {
|
|
21
|
+
let clone = heading.cloneNode(true);
|
|
22
|
+
for(let el of clone.querySelectorAll("a[href^='#'], button, script, style, template, [hidden], [aria-hidden='true']")) {
|
|
23
|
+
el.remove();
|
|
24
|
+
}
|
|
25
|
+
let text = clone.textContent.trim() || heading.textContent.trim();
|
|
26
|
+
return text.replace(/\s+/g, " ");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static generate(headings) {
|
|
30
|
+
let root = document.createElement("ol");
|
|
31
|
+
let stack = [{ level: this.getLevel(headings[0]), list: root }];
|
|
32
|
+
for(let heading of headings) {
|
|
33
|
+
let level = this.getLevel(heading);
|
|
34
|
+
let top = stack.at(-1);
|
|
35
|
+
let popped;
|
|
36
|
+
|
|
37
|
+
while(stack.length > 1 && level < top.level) {
|
|
38
|
+
popped = stack.pop();
|
|
39
|
+
top = stack.at(-1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if(stack.length === 1 && level < top.level) {
|
|
43
|
+
top.level = level;
|
|
44
|
+
} else if(level > top.level) {
|
|
45
|
+
if(popped) {
|
|
46
|
+
popped.level = level;
|
|
47
|
+
} else {
|
|
48
|
+
popped = { level, list: document.createElement("ol") };
|
|
49
|
+
top.item.append(popped.list);
|
|
50
|
+
}
|
|
51
|
+
stack.push(popped);
|
|
52
|
+
top = popped;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let text = this.getText(heading);
|
|
56
|
+
top.item = document.createElement("li");
|
|
57
|
+
|
|
58
|
+
if(heading.id) {
|
|
59
|
+
let link = document.createElement("a");
|
|
60
|
+
link.setAttribute("href", `#${heading.id}`);
|
|
61
|
+
link.textContent = text;
|
|
62
|
+
top.item.append(link);
|
|
63
|
+
} else {
|
|
64
|
+
top.item.append(text);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
top.list.append(top.item);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return root;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
connectedCallback() {
|
|
74
|
+
if(document.readyState === "loading") {
|
|
75
|
+
document.addEventListener("DOMContentLoaded", () => this.init(), { once: true });
|
|
76
|
+
} else {
|
|
77
|
+
this.init();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
disconnectedCallback() {
|
|
82
|
+
this.observer?.disconnect();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
init() {
|
|
86
|
+
if(!this.isConnected) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
let selector = this.getAttribute("selector") || TableOfContents.defaults.selector;
|
|
91
|
+
let headings = Array.from(document.querySelectorAll(selector)).filter(h => !this.contains(h));
|
|
92
|
+
|
|
93
|
+
// Existing child content is kept before the list.
|
|
94
|
+
this.list?.remove();
|
|
95
|
+
this.list = undefined;
|
|
96
|
+
this.observer?.disconnect();
|
|
97
|
+
|
|
98
|
+
// Hide when empty, without overriding an author-set `hidden`.
|
|
99
|
+
if(this.hiddenWhenEmpty) {
|
|
100
|
+
this.hidden = false;
|
|
101
|
+
}
|
|
102
|
+
this.hiddenWhenEmpty = headings.length === 0 && !this.hidden;
|
|
103
|
+
if(headings.length === 0) {
|
|
104
|
+
this.hidden = true;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
this.list = TableOfContents.generate(headings);
|
|
109
|
+
this.append(this.list);
|
|
110
|
+
this.observe(headings);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// A heading is active while it or any content before the next heading is visible.
|
|
114
|
+
observe(headings) {
|
|
115
|
+
this.observer?.disconnect();
|
|
116
|
+
|
|
117
|
+
let headingSet = new Set(headings);
|
|
118
|
+
let targets = new Map();
|
|
119
|
+
let visible = new Set();
|
|
120
|
+
|
|
121
|
+
this.observer = new IntersectionObserver(entries => {
|
|
122
|
+
for(let entry of entries) {
|
|
123
|
+
visible[entry.isIntersecting ? "add" : "delete"](entry.target);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
let activeIds = new Set(Array.from(visible, el => targets.get(el)));
|
|
127
|
+
for(let link of this.list.querySelectorAll("a[href^='#']")) {
|
|
128
|
+
let id = link.getAttribute("href").slice(1);
|
|
129
|
+
link.classList.toggle(TableOfContents.defaults.activeClass, activeIds.has(id));
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
for(let heading of headings) {
|
|
134
|
+
if(!heading.id) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let el = heading;
|
|
139
|
+
do {
|
|
140
|
+
targets.set(el, heading.id);
|
|
141
|
+
this.observer.observe(el);
|
|
142
|
+
} while((el = el.nextElementSibling) && !headingSet.has(el));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if(!new URL(import.meta.url).searchParams.has("nodefine")) {
|
|
148
|
+
TableOfContents.define();
|
|
149
|
+
}
|