Site›Table of Contents

core

Table of Contents

Auto-generated table of contents from page headings

Source

import Markdoc from '@markdoc/markdoc';
const { Tag } = Markdoc;
import { createContentModelSchema, createComponentRenderable } from '../lib/index.js';
import type { HeadingInfo } from '../util.js';

export const toc = createContentModelSchema({
	attributes: {
		depth: {
			type: Number,
			required: false,
			default: 3,
			description: 'Maximum heading depth to include',
		},
		ordered: {
			type: Boolean,
			required: false,
			default: false,
			description: 'Use numbered list instead of bullets',
		},
	},
	contentModel: {
		type: 'sequence',
		fields: [],
	},
	transform(_resolved, attrs, config) {
		const headings: HeadingInfo[] = config.variables?.headings || [];

		const maxLevel = (attrs.depth as number) + 1;
		const filtered = headings.filter((h) => h.level >= 2 && h.level <= maxLevel);

		const items = filtered.map(
			(h) =>
				new Tag('li', { 'data-level': h.level }, [new Tag('a', { href: `#${h.id}` }, [h.text])]),
		);

		const list = new Tag('ul', {}, items);
		const depthMeta = new Tag('meta', { content: attrs.depth });
		const orderedMeta = new Tag('meta', { content: attrs.ordered });

		return createComponentRenderable({
			rune: 'table-of-contents',
			tag: 'nav',
			properties: {
				depth: depthMeta,
				ordered: orderedMeta,
			},
			refs: {
				list,
			},
			children: [list],
		});
	},
});

View packages/runes/src/tags/toc.ts on GitHub

Basic usage

Place the self-closing tag where you want the table of contents to appear.

The table of contents below is generated from this page's own headings:

{% toc /%}
<nav data-rune="table-of-contents" data-rune-fields="{&quot;depth&quot;:3,&quot;ordered&quot;:false}">
  <ul data-name="list">
    <li data-level="2">
      <a href="#basic-usage">Basic usage</a>
    </li>
    <li data-level="2">
      <a href="#attributes">Attributes</a>
    </li>
    <li data-level="3">
      <a href="#universal-attributes">Universal attributes</a>
    </li>
    <li data-level="2">
      <a href="#bg">bg</a>
    </li>
    <li data-level="2">
      <a href="#elevation">elevation</a>
    </li>
    <li data-level="2">
      <a href="#inset">inset</a>
    </li>
    <li data-level="2">
      <a href="#motion">motion</a>
    </li>
    <li data-level="2">
      <a href="#spacing">spacing</a>
    </li>
    <li data-level="2">
      <a href="#substrate">substrate</a>
    </li>
    <li data-level="2">
      <a href="#tint">tint</a>
    </li>
    <li data-level="2">
      <a href="#width">width</a>
    </li>
  </ul>
</nav>
<nav class="rf-toc" data-elevation="flush" data-rune="table-of-contents" data-density="full">
  <ul data-name="list" class="rf-toc__list">
    <li data-level="2">
      <a href="#basic-usage">Basic usage</a>
    </li>
    <li data-level="2">
      <a href="#attributes">Attributes</a>
    </li>
    <li data-level="3">
      <a href="#universal-attributes">Universal attributes</a>
    </li>
    <li data-level="2">
      <a href="#bg">bg</a>
    </li>
    <li data-level="2">
      <a href="#elevation">elevation</a>
    </li>
    <li data-level="2">
      <a href="#inset">inset</a>
    </li>
    <li data-level="2">
      <a href="#motion">motion</a>
    </li>
    <li data-level="2">
      <a href="#spacing">spacing</a>
    </li>
    <li data-level="2">
      <a href="#substrate">substrate</a>
    </li>
    <li data-level="2">
      <a href="#tint">tint</a>
    </li>
    <li data-level="2">
      <a href="#width">width</a>
    </li>
  </ul>
</nav>

Control how deep the heading hierarchy goes with depth:

{% toc depth=2 /%}

Attributes

AttributeTypeRequiredDescription
depthnumber—Maximum heading depth to include
orderedboolean—Use numbered list instead of bullets

Universal attributes

bg

The background layer: image, video, gradient, flat overlay wash and legibility scrim, in a single injected layer behind the rune's content.

AttributeTypeRequiredDescription
bgstring—Background preset applied to this block
bg-fromstring—Gradient start colour — a semantic token name (→ var(--rf-color-*))
bg-gradient"to-t" | "to-b" | "to-l" | "to-r" | "to-tr" | "to-br" | "to-bl" | "to-tl"—Gradient direction (bounded named set)
bg-gradient-type"linear" | "radial" | "conic"—Gradient type
bg-tostring—Gradient end colour — a semantic token name
bg-viastring—Optional middle gradient stop — a semantic token name
scrim"top" | "bottom" | "left" | "right" | "none"—Scrim direction (heaviest edge); presence turns the scrim on, "none" opts out of the default cover scrim
scrim-blur"none" | "sm" | "md" | "lg"—Frost scrim blur amount
scrim-strength"sm" | "md" | "lg"—Gradient scrim strength
scrim-tone"dark" | "light"—Whether the scrim darkens (for light text) or lightens (for dark text)
scrim-type"gradient" | "frost"—Scrim treatment: gradient (default) or frost (backdrop blur)
elevation

How far the rune sits off the page, from sunken to overlay. The theme decides what each step looks like.

AttributeTypeRequiredDescription
elevation"sunken" | "flush" | "flat" | "raised" | "floating" | "overlay" | "none" | "sm" | "md" | "lg"—Surface depth, from sunken to overlay. Deprecated: none, sm, md and lg (read as flat, raised, raised and floating)
inset

Internal padding override.

AttributeTypeRequiredDescription
inset"flush" | "tight" | "default" | "loose" | "breathe"—Inner padding of this block
motion

Scroll-reveal entrance. You choose its character; the theme decides how it moves.

AttributeTypeRequiredDescription
reveal"none" | "fade" | "slide" | "scale" | "blur"—Scroll-reveal entrance character (none|fade|slide|scale|blur); the theme owns the choreography
staggerboolean—Cascade this block's items in as it reveals (no-op on single-child runes)
spacing

Block-level rhythm override.

AttributeTypeRequiredDescription
spacing"flush" | "tight" | "default" | "loose" | "breathe"—Vertical spacing above and below this block
substrate

Generated pattern fills behind the content, drawn by the theme.

AttributeTypeRequiredDescription
substrate"dots" | "grid" | "lines" | "cross" | "checker" | "none"—Generated surface pattern
substrate-fill"inherit" | "inset"—Surface fill the pattern sits on (full colour stays with tint)
substrate-opacity"sm" | "md" | "lg"—Pattern ink strength
substrate-size"sm" | "md" | "lg"—Pattern cell size
substrate-target"self" | "media"—Which surface the pattern fills (overrides the rune/theme default)
tint

Per-rune colour override: a named tint from the theme registry, with inline per-token overrides layered on top.

AttributeTypeRequiredDescription
tintstring—Color tint preset applied to this block
tint-mode"auto" | "dark" | "light"—Whether the tint adapts to auto, dark, or light mode
width

The track a block rune occupies.

AttributeTypeRequiredDescription
width"compact" | "narrow" | "content" | "wide" | "full"—Maximum width constraint for this block
Not availableWhy
dropcap, readingthis rune declares no prose body
framethis rune declares neither a `frameTarget` nor a media section
prominencethis rune has no page-section header or featured quotation