73f355ceUpdate theme83ff50c2Use example.com in examples71292134Add alias news > release-notes2e15f642Update theme8eef09d2Add Pygments configuration572b9e75Clean up the code shortcode usea1b2fd3bRemove the code fence language codes1473b1d9Remove redundant textb92c2042Update theme8f439c28Edit contributing section in README8bcf8a19Add contributing section to README4c44ee1cFix broken content file2bdc7710Clarify .Data.Pages sorting in lists.md092271c2Use infinitive mood for main titlesb9b8abefUpdate theme to reflect change to home page contentb897b71bChange copy to use sentence casefd675ee5Enable RSS feed for sections060a5e27Correct movie title in taxonomies.md6a5ca96aUpdate displayed site name for Hub22f4b7a4Add example of starting up the local serverd9612cb3Update themea8c3988aUpdate theme4198189dUpdate theme12d6b016Update theme2b1c4197Update themeb6d90a1eFix News release titlescfe751dbAdd some build info to README git-subtree-dir: docs git-subtree-split:73f355ce0d
3.8 KiB
title, linktitle, description, date, publishdate, lastmod, categories, menu, weight, draft, aliases, toc
| title | linktitle | description | date | publishdate | lastmod | categories | 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 |
|
|
130 | false |
|
true |
{{% note "TOC Heading Levels are Fixed" %}}
Currently, the {{.TableOfContents}} page variable does not allow you 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> is going to start at <h1> when pulling from {{.Content}}.
{{% /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 any <h1>'s (i.e., # in markdown) inside your content.'
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 }}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 ) (ne .Params.toc "false") }}
{{ 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 %}}