Table of Contents¶
Automatic table of contents generation from page headings with active section highlighting.

The On this page panel on the right is the table of contents, built from the page's h1–h3 headings (h2–h3 on layouts that scope the TOC to the body, such as article).
Overview¶
- Auto-Generated: Extracts from h1-h3 headings (the range is a parameter, see Heading Levels)
- Scroll Spy: Highlights current section
- Smooth Scroll: Animated navigation
- Responsive: Sidebar on desktop, offcanvas on mobile
Implementation¶
The theme builds the TOC in Liquid while Jekyll renders the page, with no plugin, so it also works on GitHub Pages. Three files take part:
| File | Role |
|---|---|
_layouts/default.html |
Wraps the panel in <div class="bd-toc"> and includes navigation/sidebar-right.html, unless the page hides the panel |
_includes/navigation/sidebar-right.html |
Renders the #tocContents panel and its <nav id="TableOfContents">, then runs the parser on the rendered page |
_includes/content/toc.html |
The parser: a vendored copy of allejo/jekyll-toc 1.1.0, which splits the HTML at its headings and returns a nested list of links |
Every layout whose chain reaches default gets the panel, article, note and collection among them. Layouts built directly on root, such as home, landing and admin, render no TOC.
Include Template¶
sidebar-right.html calls the parser like this:
{% raw %}{% include content/toc.html sanitize=true html=toc_src h_min=toc_h_min h_max=3 class="list-group-flush" item_class="list-group-item" anchor_class="" skip_no_ids=true %}{% endraw %}
toc_src is the rendered page (content), or only the slice between the body sentinels when the layout sets them; toc_h_min is 1, or 2 inside the sentinels. When that HTML has no <h2>, <h3> or <h4>, the call is skipped and the panel reads "No sections on this page." instead.
Include Parameters¶
| Parameter | Default | Set by sidebar-right.html |
Purpose |
|---|---|---|---|
html |
required | the rendered page | The compiled HTML to read headings from; without it the include outputs nothing |
sanitize |
false |
true |
Strip markup (links, code, emphasis) from the heading text in the list |
h_min |
1 |
1, or 2 inside the sentinels |
Lowest heading level listed |
h_max |
6 |
3 |
Highest heading level listed |
class |
none | list-group-flush |
Class on the root list |
id |
none | none | id on the root list |
ordered |
false |
none | Emit <ol> instead of <ul> |
item_class |
none | list-group-item |
Class on each <li>; %level% becomes the heading level |
submenu_class |
none | none | Class on each nested list; %level% becomes the nested heading level minus one |
anchor_class |
none | empty | Class on each link |
base_url |
none | none | Prefix for each link, for a TOC placed on another page than its headings |
skip_no_ids |
false |
true |
Leave out headings that have no id |
Inline TOC in Your Own Layout¶
The include works wherever the rendered HTML is at hand. In a layout, content holds it, so a layout of your own can print an inline TOC above the body:
{% raw %}{% include content/toc.html html=content sanitize=true h_min=2 h_max=3 class="inline_toc" %}
{{ content }}{% endraw %}
Configuration¶
Showing and Hiding the Panel¶
The theme reads no toc key from front matter. The panel is on for every page whose layout reaches default, and it shares its switch with the left sidebar, which _includes/navigation/sidebar-config.html resolves from the sidebar key:
| Front matter | Panel |
|---|---|
No sidebar key |
Shown, except on a post with post_type: featured or post_type: breaking, where it is hidden by default |
sidebar: false |
Hidden, together with the left sidebar and the mobile TOC button |
sidebar: true, or a mapping such as sidebar: { nav: docs } |
Shown, featured and breaking posts included |
To hide it on one page:
To hide it for a whole collection, set the same key in the defaults of _config.yml:
Readers can also collapse the panel themselves, with its hide button or the t key. The choice is kept per browser, in localStorage.
Heading Levels¶
The range is set where the TOC is included, with the h_min / h_max parameters. _includes/navigation/sidebar-right.html renders the right-hand panel with h_min=1 h_max=3, so the page title and its h2/h3 sections appear (layouts that scope the TOC to the body start at h2 instead):
The include itself defaults to h_min=1 and h_max=6; anything outside the range is skipped, as is any heading without an id, or with the no_toc class.
Scoping the TOC to the Body¶
sidebar-right.html receives the whole rendered child layout, so a layout's own headings (an article's <h1> title, the "About the Author" aside) would otherwise land in the TOC. A layout opts out by wrapping its body in two empty sentinel elements:
When the sentinels are present, the TOC parses only the HTML between them and starts at h2 (h_min=2), since the page title already heads the page. default.html then removes them, so they never reach the visitor. They are elements rather than HTML comments because theme templates may not ship HTML comments, which ship to production in every rendered page (#375). The article layout uses them; layouts without the sentinels, docs included, keep the h_min=1 behaviour above. A heading that must keep its id but stay out of the TOC can take class="no_toc", as the author-bio heading does.
Styling¶
Basic Styles¶
.toc {
position: sticky;
top: 80px;
max-height: calc(100vh - 100px);
overflow-y: auto;
}
.toc-title {
font-size: 0.875rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-bottom: 1rem;
}
.toc-list {
list-style: none;
padding: 0;
margin: 0;
}
.toc-link {
display: block;
padding: 0.25rem 0;
color: var(--bs-secondary);
text-decoration: none;
font-size: 0.875rem;
border-left: 2px solid transparent;
padding-left: 0.75rem;
}
.toc-link:hover {
color: var(--bs-primary);
}
.toc-link.active {
color: var(--bs-primary);
border-left-color: var(--bs-primary);
font-weight: 500;
}
Nested Levels¶
Scroll Spy¶
assets/js/modules/navigation/scroll-spy.js bolds the entry for the section you are reading. The rule is positional rather than visibility-based: the active heading is the last one whose top has crossed the reading line — a line scroll-padding-top below the top of the viewport, i.e. just under the fixed header, which is also where clicking a TOC entry parks its heading. Once the page is scrolled to the bottom, the last heading wins, so trailing sections shorter than the viewport are still reachable.
// Simplified: the active heading, recomputed on each scroll frame.
function activeHeading(headings) { // headings sorted by document offset
const pad = parseFloat(getComputedStyle(document.documentElement).scrollPaddingTop);
const atBottom =
window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 4;
if (atBottom) return headings[headings.length - 1];
const line = window.scrollY + pad + 4;
let active = headings[0];
for (const heading of headings) {
if (heading.top > line) break;
active = heading;
}
return active;
}
Heading offsets are measured once and re-measured on resize or content reflow (ResizeObserver), and the recompute is throttled to one animation frame per scroll, so the whole page is re-evaluated on every frame without measuring the DOM each time.
Asking IntersectionObserver for the "most visible" heading looks simpler but does not work: headings are only a few pixels tall, so every heading inside the observer band reports the same intersectionRatio, and headings leaving the band trigger no callback at all. The highlight then lands on whichever heading happened to be in the last callback batch.
Two details matter for feel. A clicked entry stays active while the smooth scroll animates, instead of flashing every heading passed on the way. And keeping the active entry visible inside a long TOC adjusts the TOC container's own scrollTop — scrollIntoView() would bubble up and scroll the page, which feeds straight back into the spy.
Configuration (assets/js/modules/navigation/config.js):
| Key | Default | Purpose |
|---|---|---|
scrollSpy.offset |
null |
Reading-line distance from the viewport top. null derives it from scroll-padding-top; set a number to pin it. |
scrollSpy.tolerance |
4 |
Slack (px) at the reading line and when detecting the bottom of the page. |
Smooth Scrolling¶
CSS Method¶
JavaScript Method¶
document.querySelectorAll('.toc-link').forEach((link) => {
link.addEventListener('click', (e) => {
e.preventDefault();
const targetId = link.getAttribute('href').slice(1);
const target = document.getElementById(targetId);
const headerOffset = 80;
const position = target.offsetTop - headerOffset;
window.scrollTo({
top: position,
behavior: 'smooth'
});
history.pushState(null, '', `#${targetId}`);
});
});
Responsive Behavior¶
One panel serves every width. sidebar-right.html renders it as a Bootstrap responsive offcanvas:
<div class="offcanvas-lg offcanvas-end" tabindex="-1" id="tocContents" aria-labelledby="tocLabel">
<div class="offcanvas-header">…</div>
<div class="offcanvas-body">
<nav id="TableOfContents" role="navigation" aria-label="Table of Contents">…</nav>
</div>
</div>
Desktop¶
From the lg breakpoint up, offcanvas-lg leaves the panel in place, in the right-hand .bd-toc column beside the content.
Mobile¶
Below lg the same panel becomes a drawer that slides in from the right, opened by the floating TOC button from _includes/navigation/toc-fab.html (see Mobile TOC).
Accessibility¶
ARIA Attributes¶
Keyboard Navigation¶
- Tab through TOC links
- Enter to navigate to section
- Focus moves to heading
Troubleshooting¶
TOC Not Generating¶
- Verify headings have IDs — Kramdown adds them to Markdown headings, but a heading written in raw HTML needs its own
id, because the panel skips headings without one (skip_no_ids=true) - Check the page does not set
sidebar: false, and is not afeaturedorbreakingpost without asidebarkey (see Showing and Hiding the Panel) - Check the layout reaches
default— layouts built directly onrootrender no TOC panel - "No sections on this page." means the parsed HTML has no
<h2>,<h3>or<h4>; on a layout with body sentinels, only the headings between them count
Scroll Spy Not Working¶
- Check heading IDs match TOC hrefs — the spy resolves each TOC link's
hrefto a heading byid, so a link with no matching element is skipped - Check the reading line —
scroll-padding-toponhtml(orconfig.scrollSpy.offset) decides when a heading becomes current - Check nothing else claims
#TableOfContents— a second scroll spy (adata-bs-spy="scroll"element targeting it) fights over the.activeclass
Styling Issues¶
- Check sticky positioning
- Verify z-index
- Test overflow behavior