| 1 |
--- |
| 2 |
title: Fragments |
| 3 |
description: Returns a data structure of the fragments in the given page. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: tableofcontents.Fragments |
| 9 |
signatures: [PAGE.Fragments] |
| 10 |
--- |
| 11 |
|
| 12 |
In a URL, whether absolute or relative, the [fragment](g) links to an `id` attribute of an HTML element on the page. |
| 13 |
|
| 14 |
```text |
| 15 |
/articles/article-1#section-2 |
| 16 |
------------------- --------- |
| 17 |
path fragment |
| 18 |
``` |
| 19 |
|
| 20 |
Hugo assigns an `id` attribute to each Markdown [ATX][] and [setext][] heading within the page content. You can override the `id` with a [Markdown attribute](g) as needed. This creates the relationship between an entry in the [table of contents][] (TOC) and a heading on the page. |
| 21 |
|
| 22 |
Use the `Fragments` method on a `Page` object to create a table of contents with the `Fragments.ToHTML` method, or by [walking](g) the `Fragments.Map` data structure. Use the methods below to inspect, validate, and render page fragments. |
| 23 |
|
| 24 |
## Methods |
| 25 |
|
| 26 |
Use these methods on the `Fragments` object. |
| 27 |
|
| 28 |
`Headings` |
| 29 |
: (`slice`) A slice of maps of all headings on the page, with first-level keys for each heading. Each map contains the following keys: `ID`, `Level`, `Title` and `Headings`. To inspect the data structure: |
| 30 |
|
| 31 |
```go-html-template |
| 32 |
<pre>{{ debug.Dump .Fragments.Headings }}</pre> |
| 33 |
``` |
| 34 |
|
| 35 |
`HeadingsMap` |
| 36 |
: (`map`) A nested map of all headings on the page. Each map contains the following keys: `ID`, `Level`, `Title` and `Headings`. To inspect the data structure: |
| 37 |
|
| 38 |
```go-html-template |
| 39 |
<pre>{{ debug.Dump .Fragments.HeadingsMap }}</pre> |
| 40 |
``` |
| 41 |
|
| 42 |
`Identifiers` |
| 43 |
: (`slice`) A slice containing the `id` attribute of each heading on the page. If so configured, will also contain the `id` attribute of each description term (i.e., `dt` element) on the page. |
| 44 |
|
| 45 |
See [configure Markup][]. |
| 46 |
|
| 47 |
To inspect the data structure: |
| 48 |
|
| 49 |
```go-html-template |
| 50 |
<pre>{{ debug.Dump .Fragments.Identifiers }}</pre> |
| 51 |
``` |
| 52 |
|
| 53 |
`Identifiers.Contains ID` |
| 54 |
: (`bool`) Reports whether one or more headings on the page has the given `id` attribute, useful for validating fragments within a link [render hook](g). |
| 55 |
|
| 56 |
```go-html-template |
| 57 |
{{ .Fragments.Identifiers.Contains "section-2" }} → true |
| 58 |
``` |
| 59 |
|
| 60 |
`Identifiers.Count ID` |
| 61 |
: (`int`) The number of headings on a page with the given `id` attribute, useful for detecting duplicates. |
| 62 |
|
| 63 |
```go-html-template |
| 64 |
{{ .Fragments.Identifiers.Count "section-2" }} → 1 |
| 65 |
``` |
| 66 |
|
| 67 |
`ToHTML` |
| 68 |
: (`template.HTML`) Returns a TOC as a nested list, either ordered or unordered, identical to the HTML returned by the [`TableOfContents`][] method. This method take three arguments: the start level (`int`), the end level (`int`), and a boolean (`true` to return an ordered list, `false` to return an unordered list). |
| 69 |
|
| 70 |
Use this method when you want to control the start level, end level, or list type independently from the table of contents settings in your project configuration. |
| 71 |
|
| 72 |
```go-html-template |
| 73 |
{{ $startLevel := 2 }} |
| 74 |
{{ $endLevel := 3 }} |
| 75 |
{{ $ordered := true }} |
| 76 |
{{ .Fragments.ToHTML $startLevel $endLevel $ordered }} |
| 77 |
``` |
| 78 |
|
| 79 |
Hugo renders this to: |
| 80 |
|
| 81 |
```html |
| 82 |
<nav id="TableOfContents"> |
| 83 |
<ol> |
| 84 |
<li><a href="#section-1">Section 1</a> |
| 85 |
<ol> |
| 86 |
<li><a href="#section-11">Section 1.1</a></li> |
| 87 |
<li><a href="#section-12">Section 1.2</a></li> |
| 88 |
</ol> |
| 89 |
</li> |
| 90 |
<li><a href="#section-2">Section 2</a></li> |
| 91 |
</ol> |
| 92 |
</nav> |
| 93 |
``` |
| 94 |
|
| 95 |
## Notes |
| 96 |
|
| 97 |
> [!NOTE] |
| 98 |
> It is safe to use the `Fragments` methods within a render hook, even for the current page. |
| 99 |
> |
| 100 |
> When using the `Fragments` methods within a shortcode, call the shortcode using [standard notation][]. If you use [Markdown notation][] the rendered shortcode is included in the creation of the fragments map, resulting in a circular loop. |
| 101 |
|
| 102 |
[ATX]: https://spec.commonmark.org/current/#atx-headings |
| 103 |
[Markdown notation]: /content-management/shortcodes/#notation |
| 104 |
[`TableOfContents`]: /methods/page/tableofcontents/ |
| 105 |
[configure Markup]: /configuration/markup/#parserautodefinitiontermid |
| 106 |
[setext]: https://spec.commonmark.org/current/#setext-headings |
| 107 |
[standard notation]: /content-management/shortcodes/#notation |
| 108 |
[table of contents]: /methods/page/tableofcontents/ |