The accessible, attribute-only table of contents. One attribute turns the headings already on your page into a live outline: it builds the list, gives headings ids, highlights the section being read and glides a marker between links. Zero dependencies, about 5 KB gzip, works in every framework and on plain sites.
<nav data-chapter data-chapter-build data-chapter-marker></nav>
<script src="https://unpkg.com/chapterize-js" defer></script>- Attribute-only. No JavaScript config. Ids are generated from heading text, sticky headers are read from your CSS
scroll-padding-top. - Accessible first.
aria-current="location", an automatic landmark name, focus follows clicks, reduced motion is honoured live (even over CSSscroll-behavior: smooth). - Correct at the edges. Short final sections each get a turn, the last section wins at the bottom, the first click lands below sticky headers, a smooth scroll does not flicker through the sections it passes.
- Framework-proof. It watches the DOM: client-side routes, re-rendered headings and late content just work. Importing it on the server is a no-op.
- Beautiful by default, opt-in.
data-chapter-markerdraws a gliding line, a pill or a minimal marker from a CSP-safe constructed stylesheet in@layer chapter.
chapterize-js on npm, version 0.1.0. The site is chapterizejs.com.
npm install chapterize-jsimport 'chapterize-js/auto'; // spies every [data-chapter] nav, now and laterOr with no build step: <script src="https://unpkg.com/chapterize-js" defer></script> registers a global Chapterize and initialises when the DOM is ready. Add data-manual to the script tag to call Chapterize.init() yourself.
| Entry | File | Use |
|---|---|---|
chapterize-js |
dist/index.js (ESM), dist/index.cjs |
init, chapterjs, refresh, destroy, slug, no side effects |
chapterize-js/auto |
dist/auto.js |
calls init() on import (no-op on the server) |
chapterize-js/global |
dist/global.min.js |
script tag, global Chapterize, auto-init |
| Attribute | On | Meaning |
|---|---|---|
data-chapter |
nav | Spy this element's same-page links. Required. |
data-chapter-build |
nav | Build a nested <ol> from headings. Empty = main h2, main h3; a value = your selector. |
data-chapter-marker |
nav | Built-in marker: line (default), pill, minimal. |
data-chapter-offset |
nav | Pixels hidden by a sticky header. Default: the scroller's scroll-padding-top. |
data-chapter-hash="false" |
nav | Don't write #id on click. Hash routers (#/…, #!…) are left alone automatically. |
data-chapter-label |
heading | Outline text for this heading. |
data-chapter-ignore |
heading or container | Keep out of the built outline. |
Attributes are live: change one and the nav updates.
import { init, chapterjs, refresh, destroy, slug } from 'chapterize-js';
const toc = init(); // every [data-chapter] nav, now and later; idempotent
const one = chapterjs(nav, { build: 'article h2', marker: 'pill', offset: 64, hash: false });
one.index; one.id; one.links; one.targets;
one.go('install'); // scroll to a section by id or index
one.marker('minimal'); // switch presets; false removes it
one.refresh(); // re-measure after a layout change it cannot see
one.destroy(); // undo everything it added
refresh(); destroy(); // every instance at once; destroy() also stops DOM watching
slug('Café crème'); // 'café-crème', the id the outline would generateOptions mirror the attributes (attributes win): build, offset, marker, hash, and observe (default true: watch the DOM). init() also takes root.
document.addEventListener('chapter:change', (e) => {
const { index, id, link, target, previous } = e.detail;
});data-chapter-marker draws a faint rail, a fill up to the active link, a glowing line that eases between links and a soft band behind the active row. pill glides a rounded highlight behind the link instead; minimal is just the line.
Every visual reads a custom property on the nav: --chapter-color, --chapter-color-2, --chapter-width, --chapter-duration (.6s), --chapter-ease, --chapter-indent, --chapter-rail, --chapter-glow, --chapter-band, --chapter-radius. The styles sit in @layer chapter, so any rule you write wins.
Drawing your own? Leave the attribute off. The nav still gets --chapter-y, --chapter-h, --chapter-x, --chapter-w (the active link's box) and --chapter-progress (0 to 1); the active link gets .is-active and aria-current="location", its parent entries .is-trail.
The recipe is the same in every framework. Render a nav with data-chapter, and call init() once in the browser after the page has rendered. Calling init() again is safe. The table of contents updates when the route changes, headings are re-rendered, or a nav is added later. When a nav is removed, its table of contents is removed too.
<nav data-chapter data-chapter-build="article h2, article h3" data-chapter-marker></nav>
<article>…</article>
<script src="https://unpkg.com/chapterize-js" defer></script>Call init() after the component has rendered in the browser, so the list agrees with the HTML the server sent. React Strict Mode running the effect twice is safe.
import { useEffect } from 'react';
import { init } from 'chapterize-js';
export function Toc() {
useEffect(() => { init(); }, []);
return <nav data-chapter data-chapter-build="article h2, article h3" data-chapter-marker />;
}App Router: put the nav in any Server Component and render one small Client Component that calls init(), once, in a layout. Pages stay static, the nav ships no JavaScript of its own, and on client-side navigation (next/link) the table of contents rebuilds from the new page.
'use client';
import { useEffect } from 'react';
import { init } from 'chapterize-js';
export default function ChapterInit() {
useEffect(() => { init(); }, []);
return null;
}import ChapterInit from './chapter-init';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<ChapterInit />
</body>
</html>
);
}export default function Page() {
return (
<>
<nav data-chapter data-chapter-build="article h2, article h3" data-chapter-marker />
<article>{/* headings and content */}</article>
</>
);
}<script setup>
import { onMounted } from 'vue';
import { init } from 'chapterize-js';
onMounted(() => init());
</script>
<template>
<nav data-chapter data-chapter-build="article h2, article h3" data-chapter-marker></nav>
</template>A client plugin that runs after the app has mounted, so the server HTML is in place first.
// plugins/toc.client.js
import { init } from 'chapterize-js';
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('app:mounted', () => init());
});<script>
import { onMount } from 'svelte';
import { init } from 'chapterize-js';
onMount(() => init());
</script>
<nav data-chapter data-chapter-build="article h2, article h3" data-chapter-marker></nav>afterNextRender runs in the browser, after the view is on the page. Links work on a page that uses <base href>.
import { Component, afterNextRender } from '@angular/core';
import { init } from 'chapterize-js';
@Component({
selector: 'app-toc',
template: `<nav data-chapter data-chapter-build="article h2, article h3" data-chapter-marker></nav>`,
})
export class TocComponent {
constructor() { afterNextRender(() => init()); }
}The script runs once per page. With <ClientRouter />, the table of contents follows each page swap.
<nav data-chapter data-chapter-build="article h2, article h3" data-chapter-marker></nav>
<script>
import 'chapterize-js/auto';
</script>import { onMount } from 'solid-js';
import { init } from 'chapterize-js';
export function Toc() {
onMount(() => init());
return <nav data-chapter data-chapter-build="article h2, article h3" data-chapter-marker />;
}Enqueue the global build in the theme, and put the nav in a template part or a Custom HTML block.
add_action('wp_enqueue_scripts', function () {
wp_enqueue_script('chapterize-js', 'https://unpkg.com/chapterize-js', [], null, ['strategy' => 'defer']);
});<nav data-chapter data-chapter-build=".entry-content h2, .wp-block-post-content h2" data-chapter-marker></nav>Hugo's headings already have ids, and those ids are kept. Build the list from your content, or highlight the links in Hugo's own .TableOfContents.
<nav data-chapter data-chapter-build=".content h2, .content h3" data-chapter-marker></nav>
<!-- or: <div data-chapter data-chapter-marker>{{ .TableOfContents }}</div> -->
<script src="https://unpkg.com/chapterize-js" defer></script>In the layout that wraps your Markdown:
<nav data-chapter data-chapter-build="main h2, main h3" data-chapter-marker></nav>
<main>{{ content | safe }}</main>
<script src="https://unpkg.com/chapterize-js" defer></script>Declare a library in the theme, attach it, and add the nav to the node template.
# mytheme.libraries.yml
toc:
js:
https://unpkg.com/chapterize-js: { type: external, minified: true, attributes: { defer: true } }{{ attach_library('mytheme/toc') }}
<nav data-chapter data-chapter-build=".node__content h2, .node__content h3" data-chapter-marker></nav>Current Chrome, Edge, Firefox and Safari (Safari 16.4+). Modern features are used where they help, each with a fallback:
| Feature | Used for | Without it |
|---|---|---|
| IntersectionObserver | section crossings, no scroll polling | required (every supported browser has it) |
scrollend |
ending a click's hold exactly | a 3 s timer, then the hold ends at the next real scroll |
constructed stylesheets + @layer |
CSP-safe marker styles that your CSS overrides | the list still spies, without the marker |
| scroll-driven animations | (site only) the header progress bar | --chapter-progress / chapter:change |
native scroll-target-group |
not used yet: Chromium-only, see docs/ROADMAP.md | n/a |
init() watches the document, not shadow trees. For a nav or headings inside a shadow root, call chapterjs(nav) yourself and refresh() after that tree changes.
- The active link gets
aria-current="location". - A nav without a name is labelled by its own heading, else "On this page".
- Clicking moves focus to the section (with
tabindex="-1"if needed, removed on destroy), so the next Tab continues there. - Reduced motion: clicks jump instantly and the marker does not glide, live.
- A nav that scrolls on its own keeps the active link in view, without moving the page.
| Setup | Builds the TOC | Ids | aria-current | Reduced motion | Size (gzip) | |
|---|---|---|---|---|---|---|
| Chapterize JS | attributes | yes | generated | yes | yes, live | ~5 KB incl. marker CSS |
| Tocbot | JS config | yes | must exist | no | no | 4.3 KB + CSS |
| Gumshoe | JS | no | must exist | no | no | unmaintained since 2019 |
| Bootstrap ScrollSpy | attributes | no | must exist | no | partly | part of Bootstrap |
MIT © Byron Johnson