Sitemap
This is a Hugo theme component with layouts to add a configurable sitemap to your website. Hugo itself has internal templates that add sitemaps, but this component has additional setup options per page.
Add this module |
---|
[[module.imports]] path = "github.com/davidsneighbour/hugo-sitemap" disable = false ignoreConfig = false ignoreImports = false |
Latest Version |
v1.1.11 (2023-06-30) |
Usage
There is no need to configure anything without having any special needs. Add the module to your repository structure and run it. Once you ran hugo
you will find a file sitemap.xml
in your public
directory. This is the file you want to submit to search engines.
If you are using the Robots component, then your resulting robots.txt
will have a pointer to the sitemap file as well.
Exclude a page from sitemap
Add frontmatter to individual pages with the following setup:
sitemap (boolean): include this page in the sitemap
Add/edit global defaults in config.toml > params
or config/_defaults/params.toml
:
Without any configuration the default is true, meaning to include any page into the sitemap.
You can edit the following additional configuration parameters:
- full (boolean, default false) - show
priority
andchangefreq
tags (ignored by Google) - format (string, default “2006-01-02”) - date format for
lastmod
tag
DEPRECATED: Frontmatter robotsdisallow
from earlier hugo-robots
versions did result in the page being omitted from the sitemap. This is deprecated, but currently still supported. The module will echo a note on CLI about this.
HTML Sitemap
If you want to add an HTML sitemap you can do so via shortcode:
1{{< sitemap >}}
Add the sitemap as shortcode {{< sitemap >}}
anywhere you want.
A sample implementation can be found on kollitsch.dev. The following configuration was used:
1[dnb]
2 [dnb.sitemap]
3 [dnb.sitemap.htmlmap]
4[[dnb.sitemap.htmlmap.item]]
5 label = 'Blog Posts'
6 section = 'blog'
7 type = '.Type'
8[[dnb.sitemap.htmlmap.item]]
9 label = 'GoHugo Components by DNB'
10 section = 'components'
11 sortdirection = 'ASC'
12 sortvalue = '.Title'
13 type = '.Type'
14[[dnb.sitemap.htmlmap.item]]
15 label = 'Tags'
16 section = 'tags'
17 selection = 'in-pages'
18 sortdirection = 'ASC'
19 sortvalue = '.Title'
20 type = '.Type'
21[[dnb.sitemap.htmlmap.item]]
22 label = 'Other pages'
23 section = ['blog', 'tags', 'components']
24 selection = 'not-in'
25 sortdirection = 'ASC'
26 sortvalue = '.Title'
27 type = '.Type'
1dnb:
2 sitemap:
3 htmlmap:
4 item:
5 - label: Blog Posts
6 section: blog
7 type: .Type
8 - label: GoHugo Components by DNB
9 section: components
10 sortdirection: ASC
11 sortvalue: .Title
12 type: .Type
13 - label: Tags
14 section: tags
15 selection: in-pages
16 sortdirection: ASC
17 sortvalue: .Title
18 type: .Type
19 - label: Other pages
20 section:
21 - blog
22 - tags
23 - components
24 selection: not-in
25 sortdirection: ASC
26 sortvalue: .Title
27 type: .Type
1{
2 "dnb": {
3 "sitemap": {
4 "htmlmap": {
5 "item": [
6 {
7 "label": "Blog Posts",
8 "section": "blog",
9 "type": ".Type"
10 },
11 {
12 "label": "GoHugo Components by DNB",
13 "section": "components",
14 "sortdirection": "ASC",
15 "sortvalue": ".Title",
16 "type": ".Type"
17 },
18 {
19 "label": "Tags",
20 "section": "tags",
21 "selection": "in-pages",
22 "sortdirection": "ASC",
23 "sortvalue": ".Title",
24 "type": ".Type"
25 },
26 {
27 "label": "Other pages",
28 "section": [
29 "blog",
30 "tags",
31 "components"
32 ],
33 "selection": "not-in",
34 "sortdirection": "ASC",
35 "sortvalue": ".Title",
36 "type": ".Type"
37 }
38 ]
39 }
40 }
41 }
42}
The parameters are as follows:
selection
- Type of page selection.in-regular
(default, just omit the parameter) - selects the pages from thesite.RegularPages
collection.in-pages
- selects fromsite.Pages
not-in
- selects all pages NOT insite.Pages
type
- field option for the page selectionsection
- value option for the page selectionlabel
- Label for the section headlinesortvalue
- if you wish to sort the selection set this to the field to sort bysortdirection
(defaultASC
) - direction to sort in,ASC
orDESC
Page selection:
Sample:
Results in the pages being selected via:
1{{- $pages = (where site.Pages .Type "tags") -}}
You can add a sitemap also to any template as a partial:
The above is the current shortcode, ehm, code. You can just use the global configuration or build your own configuration dict
ionary to fill your sitemap. This makes the sitemap also usable to just show a collection of pages anywhere:
This template would show all items in my content/components
section, sorted by title.