Table Of Contents
Progressively enhanced h2/h3 navigation with collision-safe anchors and keyboard focus.
Install
npx ghostcn@1.4.0 add table-of-contents --skip-existingUsage
Place next to your article content inside {{#post}}. The script scans .ghcn-article-content by default. Pass contentSelector=".your-article-content" for a different selector or heading="In this article" for a custom title. It lists H2/H3 headings, excludes editor-card UI headings, adds unique anchors, moves keyboard focus on navigation, and stays hidden with fewer than two headings. linkClass styles the generated links.
Ghostcn links its deferred script in default.hbs. If you use another layout, link it there manually:
<script defer src="{{asset "js/components/table-of-contents.js"}}"></script>{{#post}}
{{> "components/table-of-contents"}}
{{/post}}Customize this instance
{{#post}}
{{> "components/table-of-contents" class="my-6 rounded-xl" linkClass="underline"}}
{{/post}}Styling parameters
| Parameter | Target |
|---|---|
class | Component root — .ghcn-toc |
titleClass | .ghcn-toc__title |
listClass | .ghcn-toc__list |
linkClass | .ghcn-toc__link |
Styling parameters affect only elements that render. See Style component instances for specificity, dark mode, and preserving customizations.
Installed files
partials/components/table-of-contents.hbsassets/css/components/table-of-contents.cssassets/js/components/table-of-contents.js
Was this helpful?