用法

按通常方式创建您的markdown文档, 包含适当的headings. 下面是markdown内容举例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<!-- 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会处理这个markdown文档, 从 ## Introduction, ## My Heading### My Subheading 创建目录, 并保存在页面变量.TableOfContents中.

内置的 .TableOfContents 变量输出 <nav id="TableOfContents"> 元素,这个元素具有子元素<ul>, <ul>的子元素<li>以合适的HTML标题开始. 参考 目录配置选项 文档了解如何配置目录标题层级的信息.

模板举例:基础的目录

下面是一个非常简单的single page template 单独页面模板

layout/_default/single.html
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

{{ define "main" }}
<main>
    <article>
    <header>
        <h1>{{ .Title }}</h1>
    </header>
        {{ .Content }}
    </article>
    <aside>
        {{ .TableOfContents }}
    </aside>
</main>
{{ end }}

模板举例: TOC 部分模板

下面的部分模板添加稍微多一点对于TOC的页面级别控制逻辑. 它假设您的内容的front matter使用了toc域, 除非特定声明为false, hugo会在任何具有.WordCount (参考Page Variables) 词数大于400的页面展示目录.这个例子还展示了如何在模板中使用条件逻辑conditionals:

layouts/partials/toc.html
1
2
3
4
5
6
7
8
9

{{ if and (gt .WordCount 400 ) (.Params.toc) }}
<aside>
    <header>
    <h2>{{.Title}}</h2>
    </header>
    {{.TableOfContents}}
</aside>
{{ end }}

使用 AsciiDoc

hugo支持asciiDoc内容格式的目录

在内容文件的头部,声明AsciiDoc的Toc指令以确保目录可以生成。HUGO会使用生成的TOC来赋值给页面变量.TableOfContents, 和Markdown文件的方式一样。例子如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// <!-- Your front matter up here -->
:toc:
// Set toclevels to be at least your hugo [markup.tableOfContents.endLevel] config key
:toclevels: 4

== 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会处理这个AsciiDoc文档,创建内容目录,并保存在.TableOfContents页面变量中,如上面Markdown中描述的一样.