Files
hugo/content/content-management/toc.md
T
Bud Parr 6896947058 Update content to use Hugo's built-in menu
* add generic menu declaration
* update about section
* commands
* update content management section
* update contribute section
* functions
* update getting started section
* update hosting and deployment section
* fix mailing list entry (no menu)
* update news section
* update Templates section
* update tools section
* update themes section
* troubleshooting
* update variables section

Fixes rdwatters/hugo-docs-concept#45
See PR rdwatters/hugo-docs-concept#62
2017-03-31 17:50:49 -05:00

4.1 KiB

title, linktitle, description, date, publishdate, lastmod, categories, tags, menu, weight, draft, aliases, toc
title linktitle description date publishdate lastmod categories tags menu 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
content management
table of contents
toc
main
parent weight
Content Management 130
130 false
/extras/toc/
/content-management/toc/
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 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.

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: 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" }}
<main id="main">
    <header>
        <h1>{{ .Title }}</h1>
    </header>
        {{ .Content }}
    <aside id="toc">
        {{ .TableOfContents }}
    </aside>
</main>
{{ 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][] 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") }}
<aside id="toc">
    <header>
    <h2 class="toc-heading">{{.Title}}</h2>
    </header>
    {{.TableOfContents}}
</aside>
{{ 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 %}}