Color mode

Table of Contents

Table of contents rendering with scroll-spy: extracts headings from article HTML and highlights the section currently in view.

日本語

Overview

toc() provides a TableOfContents component that renders the article's h2–h4 headings (that carry an id) as a nested list. initTableOfContents is the client entry: it tracks scrolling and marks links as read plus sets aria-current on the active heading's link.

The component renders nothing when fewer than 2 items are extracted.

Usage

ts
import { defineConfig } from "@riebeckite/core";
import { tocPlugin } from "@riebeckite/plugin-toc";
 
export default defineConfig({
  // ...
  plugins: [tocPlugin()],
});

tocPlugin() registers the plugin, bundles style.css, and declares initTableOfContents as a client entry.

Render the component

tsx
import TableOfContents, {
  extractTableOfContents,
} from "@riebeckite/plugin-toc";
 
const items = extractTableOfContents(post.html ?? "");
 
// ...in your route
return (
  <Article
    asideContent={
      <TableOfContents className="table-of-contents--desktop" items={items} />
    }
  />
);

The client entry finds elements by the data-toc-target attribute emitted on each link, so it works when multiple ToCs (desktop/mobile) are rendered.

API

  • extractTableOfContents(html) — extracts h2–h4 headings with an id as TableOfContentsItem[], decoded and stripped of inline HTML

Exports

  • tocPlugin() — plugin factory
  • TableOfContents — list component (default export of components/table-of-contents.tsx)
  • extractTableOfContents(html) — heading extractor
  • initTableOfContents — browser scroll-spy init (also via @riebeckite/plugin-toc/client)
  • Type: TableOfContentsItem ({ id, level, title })

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/toc/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Table of Contents
4 +
5 + Table of contents rendering with scroll-spy: extracts headings from article
6 + HTML and highlights the section currently in view.
7 +
8 + [日本語](./toc.md)
9 +
10 + ## Overview
11 +
12 + `toc()` provides a `TableOfContents` component that renders the article's
13 + `h2`–`h4` headings (that carry an `id`) as a nested list. `initTableOfContents`
14 + is the client entry: it tracks scrolling and marks links as read plus sets
15 + `aria-current` on the active heading's link.
16 +
17 + The component renders nothing when fewer than 2 items are extracted.
18 +
19 + ## Usage
20 +
21 + ```ts
22 + import { defineConfig } from "@riebeckite/core";
23 + import { tocPlugin } from "@riebeckite/plugin-toc";
24 +
25 + export default defineConfig({
26 + // ...
27 + plugins: [tocPlugin()],
28 + });
29 + ```
30 +
31 + `tocPlugin()` registers the plugin, bundles `style.css`, and declares
32 + `initTableOfContents` as a client entry.
33 +
34 + ### Render the component
35 +
36 + ```tsx
37 + import TableOfContents, {
38 + extractTableOfContents,
39 + } from "@riebeckite/plugin-toc";
40 +
41 + const items = extractTableOfContents(post.html ?? "");
42 +
43 + // ...in your route
44 + return (
45 + <Article
46 + asideContent={
47 + <TableOfContents className="table-of-contents--desktop" items={items} />
48 + }
49 + />
50 + );
51 + ```
52 +
53 + The client entry finds elements by the `data-toc-target` attribute emitted on
54 + each link, so it works when multiple ToCs (desktop/mobile) are rendered.
55 +
56 + ## API
57 +
58 + - `extractTableOfContents(html)` — extracts `h2`–`h4` headings with an `id`
59 + as `TableOfContentsItem[]`, decoded and stripped of inline HTML
60 +
61 + ## Exports
62 +
63 + - `tocPlugin()` — plugin factory
64 + - `TableOfContents` — list component (default export of
65 + `components/table-of-contents.tsx`)
66 + - `extractTableOfContents(html)` — heading extractor
67 + - `initTableOfContents` — browser scroll-spy init (also via
68 + `@riebeckite/plugin-toc/client`)
69 + - Type: `TableOfContentsItem` (`{ id, level, title }`)
70 +
71 + ## See also
72 +
73 + - [Plugin guide](../reference/plugin-api.en.md)
74 +