hugo/content/en/content-management/toc.md
Bjørn Erik Pedersen e556848805 Squashed 'docs/' changes from 4895c29c5..9abd3043a
9abd3043a Add docs for shimming JS libraries
6a1c8dcd7 Update sitemap-template.md (#1245)
37c397332 Update frontends.md
a0f86f6df Update configuration.md
bb00cb2c1 Update page-bundles.md
773212de6 Restructure and simplify
fcba7dddf Some minor clarifications of weight sorting
759b967fc Update configuration-markup.md
56708f0b7 module import path remove slash at end
59f4f4acd Doc: Fix typo in hugo command
faacf2e97 Clarify pagination documentation (#1208)
d8eb60887 netlify: Bump to 0.75.1
8cedf6231 Merge branch 'temp751'
188e2bf56 releaser: Add release notes to /docs for release of 0.75.1
c96d4b7a3 Update index.md
1a9d192f7 Update index.md
32731b916 Update index.md
a5bfa0c9a Restore the ... home page
b6850bf96 Release 0.75.0
d6e5e624f releaser: Add release notes to /docs for release of 0.75.0
8cd6b4f47 typo: already -> already
2cb2b22bb Merge commit '534ae9c57a902aea9ed6e62390dec11fa74b7122'
e3525de23 docs: Regen docs helper
fd746dd83 docs: Regenerate CLI docs
e20127980 Add "hugo mod npm pack"
8e82c7ce1 markup/highlight: Add support to linkable line anchors on Chroma
21e94911b markup/asciidocext: Fix AsciiDoc TOC with code
50b8dace5 modules: Add noVendor to module config
d05b541fe modules: Make ignoreVendor a glob pattern
c946082e7 docs: Update replaceRE func
149054341 docs: Update replace func
d917567df docs: Update merge function
f1e093c92 docs: Regen CLI docs
c7bac967d docs: Regen docs helper
7a38f7a45 Merge commit '7d7771b673e5949f554515a2c236b23192c765c8'
1a5a7263a markup/asciidoc: Add support for .TableOfContents

git-subtree-dir: docs
git-subtree-split: 9abd3043a9214b390e8cc148f4588bf630620851
2020-10-06 16:22:20 +02:00

5.8 KiB

title linktitle description date publishdate lastmod categories keywords menu weight draft aliases toc
Table of Contents Hugo can automatically parse Markdown content and create a Table of Contents you can use in your templates. 2017-02-01 2017-02-01 2017-02-01
content management
table of contents
toc
docs
parent weight
content-management 130
130 false
/extras/toc/
true

{{% note "TOC Heading Levels are Fixed" %}}

Previously, there was no out-of-the-box way to specify which heading levels you want the TOC to render. See the related GitHub discussion (#1778). As such, the resulting <nav id="TableOfContents"><ul></ul></nav> was going to start at <h1> when pulling from {{.Content}}.

Hugo v0.60.0 made a switch to Goldmark as the default library for Markdown which has improved and configurable implementation of TOC. Take a look at how to configure TOC for Goldmark renderer.

{{% /note %}}

Usage

Create your markdown the way you normally would with the appropriate headings. Here is some example content:

<!-- Your front matter up here -->

## Introduction

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.

## My Heading

He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment.

### My Subheading

A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather. Drops

Hugo will take this Markdown and create a table of contents from ## Introduction, ## My Heading, and ### My Subheading and then store it in the page variable.TableOfContents.

The built-in .TableOfContents variables outputs a <nav id="TableOfContents"> element with a child <ul>, whose child <li> elements begin with appropriate HTML headings. See the available settings to configure what heading levels you want to include in TOC.

{{% note "Table of contents not available for MMark" %}} Hugo documents created in the MMark Markdown dialect do not currently display TOCs. TOCs are, however, compatible with all other supported Markdown formats. {{% /note %}}

Template Example: Basic TOC

The following is an example of a very basic single page template:

{{< code file="layout/_default/single.html" download="single.html" >}} {{ define "main" }}

{{ .Title }}

{{ .Content }}
{{ end }} {{< /code >}}

Template Example: TOC Partial

The following is a partial template that adds slightly more logic for page-level control over your table of contents. It assumes you are using a toc field in your content's front matter that, unless specifically set to false, will add a TOC to any page with a .WordCount (see Page Variables) greater than 400. This example also demonstrates how to use conditionals in your templating:

{{< code file="layouts/partials/toc.html" download="toc.html" >}} {{ if and (gt .WordCount 400 ) (.Params.toc) }}

{{ end }} {{< /code >}}

{{% note %}} With the preceding example, even pages with > 400 words and toc not set to false will not render a table of contents if there are no headings in the page for the {{.TableOfContents}} variable to pull from. {{% /note %}}

Usage with AsciiDoc

Hugo supports table of contents with AsciiDoc content format.

In the header of your content file, specify the AsciiDoc TOC directives, by using the macro or auto style:

// <!-- Your front matter up here -->
:toc: macro
// Set toclevels to be at least your hugo [markup.tableOfContents.endLevel] config key
:toclevels: 4
toc::[]

== Introduction

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.

== My Heading

He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment.

=== My Subheading

A collection of textile samples lay spread out on the table - Samsa was a travelling salesman - and above it there hung a picture that he had recently cut out of an illustrated magazine and housed in a nice, gilded frame. It showed a lady fitted out with a fur hat and fur boa who sat upright, raising a heavy fur muff that covered the whole of her lower arm towards the viewer. Gregor then turned to look out the window at the dull weather. Drops

Hugo will take this AsciiDoc and create a table of contents store it in the page variable .TableOfContents, in the same as described for Markdown.