2.8 KiB
title, linktitle, description, date, publishdate, lastmod, categories, tags, weight, draft, aliases, toc
| title | linktitle | description | date | publishdate | lastmod | categories | tags | weight | draft | aliases | toc | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Table of Contents | Hugo can automatically parse Markdown content and create a Table of Contents you can leverage in your templates to guide readers to sections of longer pages. | 2017-02-01 | 2017-02-01 | 2017-02-01 |
|
|
130 | false |
|
false |
Hugo can automatically parse Markdown content and create a Table of Contents you can leverage in your templates to guide readers to sections of longer pages.
{{% note "TOC Heading Levels are Fixed" %}} Currently, the {{.TableOfContents}} page variable is fixed in its behavior; i.e., you do not have the option to set the heading level at which the TOC renders. This is a known issue (#1778). {{% /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.
His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me? " he thought. It wasn't a dream. His room, a proper human room although a little too small, lay peacefully between its four familiar walls.
### 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.
Template Example
The following is an example of very basic single page template:
{{% code file="layout/partials/toc.html" download="toc.html" %}}
{{ define "main" }}
<aside id="toc">
{{ .TableOfContents }}
</aside>
<h1>{{ .Title }}</h1>
{{ .Content }}
{{ end }}
{{% /code %}}