HUGO

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
gohugoio Star
  • About Hugo
    • Overview
    • What is Hugo
    • Hugo features
    • Static site generators
    • Hugo's security model
    • Hugo and the GDPR
    • License
  • Installation
    • Overview
    • macOS
    • Linux
    • Windows
    • BSD
  • Getting started
    • Overview
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Quick reference
    • Overview
    • Emojis
    • Functions
    • Methods
    • Page collections
  • Content management
    • Overview
    • Organization
    • Page bundles
    • Content formats
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Static files
    • Table of contents
    • Comments
    • Multilingual
    • Markdown attributes
    • Syntax highlighting
    • Diagrams
    • Mathematics
  • Templates
    • Overview
    • Templating
    • Template lookup order
    • Base templates and blocks
    • Single page templates
    • List templates
    • Homepage template
    • Section templates
    • Taxonomy templates
    • Pagination
    • Content view templates
    • Partial templates
    • Shortcode templates
    • Menu templates
    • Data templates
    • RSS templates
    • Sitemap templates
    • Internal templates
    • Custom output formats
    • 404 page
    • Robots.txt
  • Functions
    • Overview
    • cast
    • collections
    • compare
    • crypto
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • Methods
    • Overview
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Render hooks
    • Overview
    • Introduction
    • Code blocks
    • Headings
    • Images
    • Links
  • Hugo Modules
    • Overview
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • Overview
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • Troubleshooting
    • Overview
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • Developer tools
    • Overview
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • Hosting and deployment
    • Overview
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • Contribute
    • Overview
    • Development
    • Documentation
    • Themes
  • Maintenance
METHODS PAGE METHODS

Fragments

Returns a data structure of the fragments in the given page.

Syntax

PAGE.Fragments

Returns

tableofcontents.Fragments

In a URL, whether absolute or relative, the fragment links to an id attribute of an HTML element on the page.

/articles/article-1#section-2
------------------- ---------
       path         fragment

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 as needed. This creates the relationship between an entry in the table of contents (TOC) and a heading on the page.

Use the Fragments method on a Page object to create a table of contents with the Fragments.ToHTML method, or by walking the Fragments.Map data structure.

Methods

Headings
(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:
<pre>{{ .Fragments.Headings | jsonify (dict "indent" "  ") }}</pre>
HeadingsMap
(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:
<pre>{{ .Fragments.HeadingsMap | jsonify (dict "indent" "  ") }}</pre>
Identifiers
(slice) A slice containing the id of each heading on the page. To inspect the data structure:
<pre>{{ .Fragments.Identifiers | jsonify (dict "indent" "  ") }}</pre>
Identifiers.Contains ID
(bool) Reports whether one or more headings on the page has the given id attribute, useful for validating fragments within a link render hook.
{{ .Fragments.Identifiers.Contains "section-2" }} → true
Identifiers.Count ID
(int) The number of headings on a page with the given id attribute, useful for detecting duplicates.
{{ .Fragments.Identifiers.Count "section-2" }} → 1
ToHTML
(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).

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 site configuration.

{{ $startLevel := 2 }}
{{ $endLevel := 3 }}
{{ $ordered := true }}
{{ .Fragments.ToHTML $startLevel $endLevel $ordered }}

Hugo renders this to:

<nav id="TableOfContents">
  <ol>
    <li><a href="#section-1">Section 1</a>
      <ol>
        <li><a href="#section-11">Section 1.1</a></li>
        <li><a href="#section-12">Section 1.2</a></li>
      </ol>
    </li>
    <li><a href="#section-2">Section 2</a></li>
  </ol>
</nav>

It is safe to use the Fragments methods within a render hook, even for the current page.

When using the Fragments methods within a shortcode, call the shortcode using the {{< >}} notation. If you use the {{% %}} notation, the rendered shortcode is included in the creation of the fragments map, resulting in a circular loop.

See also

  • TableOfContents

On this page

  • Methods

In this section

  • Aliases
  • AllTranslations
  • AlternativeOutputFormats
  • Ancestors
  • BundleType
  • CodeOwners
  • Content
  • CurrentSection
  • Data
  • Date
  • Description
  • Draft
  • Eq
  • ExpiryDate
  • File
  • FirstSection
  • Fragments
  • FuzzyWordCount
  • GetPage
  • GetTerms
  • GitInfo
  • HasMenuCurrent
  • HasShortcode
  • HeadingsFiltered
  • InSection
  • IsAncestor
  • IsDescendant
  • IsHome
  • IsMenuCurrent
  • IsNode
  • IsPage
  • IsSection
  • IsTranslated
  • Keywords
  • Kind
  • Language
  • Lastmod
  • Layout
  • Len
  • LinkTitle
  • Next
  • NextInSection
  • OutputFormats
  • Page
  • Pages
  • Paginate
  • Paginator
  • Param
  • Params
  • Parent
  • Path
  • Permalink
  • Plain
  • PlainWords
  • Prev
  • PrevInSection
  • PublishDate
  • RawContent
  • ReadingTime
  • Ref
  • RegularPages
  • RegularPagesRecursive
  • RelPermalink
  • RelRef
  • Render
  • RenderShortcodes
  • RenderString
  • Resources
  • Scratch
  • Section
  • Sections
  • Site
  • Sitemap
  • Sites
  • Slug
  • Store
  • Summary
  • TableOfContents
  • Title
  • TranslationKey
  • Translations
  • Truncated
  • Type
  • Weight
  • WordCount
Last updated: February 11, 2024: Capitalize the word Markdown throughout the documentation (c36d20d3)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo Sponsors

 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
  • About Hugo
  • Installation
  • Getting started
  • Quick reference
  • Content management
  • Templates
  • Functions
  • Methods
  • Render hooks
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Developer tools
  • Hosting and deployment
  • Contribute
  • Maintenance