Samui? Samui!David's Neighbour's Notizen über sein Leben als Auswanderer auf der Insel Koh Samui in Thailand. Auf Deutsch, und so...

  Component

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 extends this by providing setup options per page and keeping up-to-date with current SEO practices.

Installation

[module]
  [[module.imports]]
    path = 'github.com/davidsneighbour/hugo-modules/modules/sitemap'
module:
  imports:
  - path: github.com/davidsneighbour/hugo-modules/modules/sitemap
{
   "module": {
      "imports": [
         {
            "path": "github.com/davidsneighbour/hugo-modules/modules/sitemap"
         }
      ]
   }
}

Usage

This module works out of the box and there is no need for any configuration. Once you ran hugo you can find the 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 includes a pointer to the sitemap file as well.

Exclude a page from sitemap

To exclude a specific page from all sitemaps add it to the config variable in the frontmatter of that page:

[config]
  sitemap = false
config:
  sitemap: false
{
   "config": {
      "sitemap": false
   }
}

| sitemap | boolean | true | include this page in the sitemap |

Without any configuration the default is true, meaning to include any page into the sitemap.

Global sitemap configuration

Add/edit global defaults in hugo.toml:

[params]
  [params.dnb]
    [params.dnb.sitemap]
      enabled = true
params:
  dnb:
    sitemap:
      enabled: true
{
   "params": {
      "dnb": {
         "sitemap": {
            "enabled": true
         }
      }
   }
}

You can edit the following additional configuration parameters:

  • full (boolean, default false) - show priority and changefreq 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

This module also provides an HTML sitemap, that you can include via shortcode:

{{< sitemap >}}

Add the sitemap as shortcode {{< sitemap >}} anywhere you want.

A sample implementation can be found on kollitsch.dev . The following configuration was used:

[params]
  [params.dnb]
    [params.dnb.sitemap]
      [params.dnb.sitemap.htmlmap]
        [[params.dnb.sitemap.htmlmap.item]]
          label = 'Blog Posts'
          section = 'blog'
          type = '.Type'
        [[params.dnb.sitemap.htmlmap.item]]
          label = 'GoHugo Components by DNB'
          section = 'components'
          sortdirection = 'ASC'
          sortvalue = '.Title'
          type = '.Type'
        [[params.dnb.sitemap.htmlmap.item]]
          label = 'Tags'
          section = 'tags'
          selection = 'in-pages'
          sortdirection = 'ASC'
          sortvalue = '.Title'
          type = '.Type'
        [[params.dnb.sitemap.htmlmap.item]]
          label = 'Other pages'
          section = ['blog', 'tags', 'components']
          selection = 'not-in'
          sortdirection = 'ASC'
          sortvalue = '.Title'
          type = '.Type'
params:
  dnb:
    sitemap:
      htmlmap:
        item:
        - label: Blog Posts
          section: blog
          type: .Type
        - label: GoHugo Components by DNB
          section: components
          sortdirection: ASC
          sortvalue: .Title
          type: .Type
        - label: Tags
          section: tags
          selection: in-pages
          sortdirection: ASC
          sortvalue: .Title
          type: .Type
        - label: Other pages
          section:
          - blog
          - tags
          - components
          selection: not-in
          sortdirection: ASC
          sortvalue: .Title
          type: .Type
{
   "params": {
      "dnb": {
         "sitemap": {
            "htmlmap": {
               "item": [
                  {
                     "label": "Blog Posts",
                     "section": "blog",
                     "type": ".Type"
                  },
                  {
                     "label": "GoHugo Components by DNB",
                     "section": "components",
                     "sortdirection": "ASC",
                     "sortvalue": ".Title",
                     "type": ".Type"
                  },
                  {
                     "label": "Tags",
                     "section": "tags",
                     "selection": "in-pages",
                     "sortdirection": "ASC",
                     "sortvalue": ".Title",
                     "type": ".Type"
                  },
                  {
                     "label": "Other pages",
                     "section": [
                        "blog",
                        "tags",
                        "components"
                     ],
                     "selection": "not-in",
                     "sortdirection": "ASC",
                     "sortvalue": ".Title",
                     "type": ".Type"
                  }
               ]
            }
         }
      }
   }
}

The parameters are as follows:

  • selection - Type of page selection.
    • in-regular (default, just omit the parameter) - selects the pages from the site.RegularPages collection.
    • in-pages - selects from site.Pages
    • not-in - selects all pages NOT in site.Pages
  • type - field option for the page selection
  • section - value option for the page selection
  • label - Label for the section headline
  • sortvalue - if you wish to sort the selection set this to the field to sort by
  • sortdirection (default ASC) - direction to sort in, ASC or DESC

Page selection:

Sample:

[dnb]
  [dnb.sitemap]
    [dnb.sitemap.htmlmap]
      [[dnb.sitemap.htmlmap.item]]
        section = 'tags'
        selection = 'in-pages'
        type = '.Type'
dnb:
  sitemap:
    htmlmap:
      item:
      - section: tags
        selection: in-pages
        type: .Type
{
   "dnb": {
      "sitemap": {
         "htmlmap": {
            "item": [
               {
                  "section": "tags",
                  "selection": "in-pages",
                  "type": ".Type"
               }
            ]
         }
      }
   }
}

Results in the pages being selected via:

{{- $pages = (where site.Pages .Type "tags") -}}

You can add a sitemap also to any template as a partial:

{{- $config := site.Params.dnb.sitemap.htmlmap -}}
{{- $sitemapdata := (dict "config" $config) -}}
{{- partialCached "sitemap.html" $sitemapdata $sitemapdata -}}

The above is the current shortcode, ehm, code. You can just use the global configuration or build your own configuration dictionary to fill your sitemap. This makes the sitemap also usable to just show a collection of pages anywhere:

{{ $config := dict "config" (dict "item" (dict
    "type" ".Type"
    "section"  "components"
    "label" "GoHugo Components by DNB"
    "sortvalue" ".Title"
    "sortdirection" "ASC"
)) }}
{{- partialCached "sitemap.html" $config $config -}}

This template would show all items in my content/components section, sorted by title.

Development

Formatters

Formatters are dedicated layout files for certain variable types. dnb-hugo offers reusable templates for any structural need (two and three column tables or plain printout) and takes over the markup and styling of the output.

The configuration for a single formatter offers the following parameters:

[dnb]
  [dnb.debug]
    [[dnb.debug.formatters]]
      catch = 'navigation\.MenuEntry$'
      class = 'struct'
      description = ''
      internal = 'map'
      label = 'Menu Entry'
      slug = 'menuentry'
      type = 'navigation.MenuEntry'
      weight = 100
dnb:
  debug:
    formatters:
    - catch: navigation\.MenuEntry$
      class: struct
      description: ""
      internal: map
      label: Menu Entry
      slug: menuentry
      type: navigation.MenuEntry
      weight: 100
{
   "dnb": {
      "debug": {
         "formatters": [
            {
               "catch": "navigation\\.MenuEntry$",
               "class": "struct",
               "description": "",
               "internal": "map",
               "label": "Menu Entry",
               "slug": "menuentry",
               "type": "navigation.MenuEntry",
               "weight": 100
            }
         ]
      }
   }
}
  • internal (required, if no catch or type is used) - Set to map or slice to give a general indicator of the variable type.
  • catch (required, if no type or internal is used) - A regular expression to match on the type. For instance "navigation\\.MenuEntry$"
  • type (required, if no catch or internal is used) - A string expression to match on the type. For instance boolean.
  • class - A type class to define the output format. Not yet implemented.
  • weight - This parameter is used to sort the formatters before they are used to display a variable type. If no weight is given then the order in the configuration is used. First come (based on type or catch) first serve.
  • slug (required) - Filename part for the formatter layout in layouts/partials/debugging-formatters/SLUG.html.
  • label (required) - A title to show for the debugging-table that is used to debug dictionaries and slices.
  • description (required) - A description to show as overlay for the debugging-table that is used to debug dictionaries and slices.

Evaluation of the type is done in the order or internal, then catch, then type. First come first serve.

Debugging and Logging to CLI

Debug from your layout file into the CLI/server log

Some times we developers want to inform and warn our users, or even throw an error. The debug partial is your connection to the CLI with some more options than GoHugo’s internal error functionality.

{{- partial "debug-cli.html"
      (dict
        "message" "going into PostProcessing"
        "context" .
        "severity" "warn"
        "level" 4
        "slug" dnb-some-error
      )
-}}

Note: Multiline layout functions are supported since Hugo 0.81.0. In older versions remove the new lines in these samples.

The dictionary options are as follows:

  • message: The message to print. It will be prefixed with the datetime and the severity slug.
  • context: The context to debug, typically the dot. There is currently nothing else than the dot expected, we have explicit debugging on the todo list where the context can be something to debug to the CLI.
  • severity: Slug marking the severity level. one of debug, info (default), warn, error or fatal.
  • level: 1 to 10 for the severity level. Can be used to have a more fine grained control over severity levels.
  • slug: (not implemented, keep an eye on #71) an ID to use so users can silence errors (level 7 and up)
  • namespace: (not implemented as partial option, see configuration section) namespace slug to differentiate yourself from others (default dnb)

The resulting error message will look like this:

SEVERITY TIMESTAMP [namespaceslug/severity-level] message

Note: GoHugo will print all messages that occur more than once will printed only once. This applies to identical error messages. To work around this (if you wish to for instance notify the user about multiple image transformations not working) you should add an identifier (the image url? the resource id?) to the debugging message.

Note2: Hugo makes only ERROR and WARN levels available, so all SEVERITY stamps in the beginning of each log line will be either a red ERROR (from errors and fatals — 1 to 4) or a yellow WARN for all others (debug to warn — 5 to 10).

Pictures

This component for GoHugo adds partials and shortcodes to resolve and process images on your website. It offers responsive image formats and optimised loading based on current browser abilities.

The simplest way is to use this module as a drop-in. Just install it and forget you ever did. It will work out of the box with the default settings and replace the internal handling of image markdown (![alt text](image path)) and the {{< figure >}} shortcode. You can adapt it’s features to your needs by using the partials and global configuration options.

Notes

  • Image processing (aka. resizing, filters, cropping etc) is only available in Global and Page Resources. Global Resources are located in the assets folder of your repository, Page Resources are located within the content directory with your content files in so called Page Bundles. The images in your static directory are loaded as they are, not processed (other than evaluation of content type and sizing) and will not result in responsive image tags. All other features or options will work.
  • Lookup order of images:
    • page bundle
    • global resources (assets folder)
    • static folder
    • after that, a warning about the image not being found is issued on CLI and on the page itself it fails silently
  • Using the name attribute implies that page resources are used, and no further lookup will be done if the image is not found in the page bundle.

Markdown

This module implements a markdown render hook for GoHugo that resizes and prepares HTML for responsive images. It is the simplest way to use this module. Just install it and forget you ever did. It will work out of the box with the default settings and replace the internal handling of image markdown (![alt text](image path)).

Shortcodes

Available shortcodes currently are image, figure and gallery. Those shortcodes are served by partials that you can use in your own layout files with more extensive configurability. figure overrides the GoHugo internal figure shortcode. All shortcodes add responsive image processing and markup to your website.

Figure

Possible call scenarios:

With unnamed parameters (static images preferred):

{{< figure "path/to/image" >}}
{{< figure "path/to/image" "alt-title" >}}

With named parameters:

{{< figure src="path/to/image" title="" alt="" >}}
{{< figure src="path/to/image" title="" alt="" >}}caption{{< /figure >}}
{{< figure name="resource name" title="" alt="" >}}caption{{< /figure >}}

Parameters

Option Type Notes
name string Resource name to show (required, if no src parameter is used, resources are defined in frontmatter or it’s the filename of the image in a page bundle)
src string Image to show (required, if no name parameter is used). must be relative to the static folder
link string Links the image to an URL
linktarget string Target of the link (optional, default is to open the link in the same tab. Typically you would want _blank as value for a new window, but anything goes here)
class string Additional classes for the image (optional)
alt string alt attribute for the image (optional, suggested)
title string title attribute for the image (optional)
command string Command for image processing (optional, required with options)
options string Options for image processing (optional, required with command)
width number Width of the image (optional, could be evaluated from the resulting image)
height number Height of the image (optional, could be evaluated from the resulting image)

Notes

Tagvariants:

{{< figure >}}Something{{< /figure >}}
{{< figure title="Something" >}}

The two samples above have the same output. Using the title-attribute removes the requirement to add a closing tag. But you can add as much markdown/formatting to your caption by using an opening and closing tag for the shortcode. While the title tag is “markdownified”, this might be an easier way to add more complex content to your caption.

Commands:

TODO: Currently this allows for only one command/options combination. The future way will be to add an “array” of commands and options that are executed successively.

Image Shortcode:

The {{< image >}} shortcode is a synonym for the {{< figure >}} shortcode and has the same features/options. It is added for compatibility with older implementations and themes.

to be written.

Notes: right now it expects a galleryid parameter for a folder inside of pagebundle/gallery/galleryid and a type for bootstrap4 or bootstrap5. All images in that directory are parsed and shown. No sorting (todo), no gallery selection by frontmatter (todo).

Parameters

Option Type Notes

Partials

Figure

The figure partial executes the markdown creation of single images and can be called with an options dictionary of the following content:

{
 "name": "",
 "src": "",
 "height": 100,
 "width": 100,
 "title": "",
 "alt": "",
 "class": "",
 "link": "link to put the image in, param `link` required on the shortcode",
 "caption": "markdownified content of .Inner, used for the caption of the image",
 "srcset": "",
}

To be written.

Global Configuration

To be written.

[dnb]
  [dnb.pictures]
    aspect_ratio = '2:1'
    default_image = 'images/og_sitewide.png'
    [dnb.pictures.responsive]
      [dnb.pictures.responsive.fullwidth]
        break_points = ['520', '540', '720', '960']
        image_sizes = ['494', '517', '674', '914']
      [dnb.pictures.responsive.halfwidth]
        break_points = ['520', '540']
        image_sizes = ['494', '517']
dnb:
  pictures:
    aspect_ratio: "2:1"
    default_image: images/og_sitewide.png
    responsive:
      fullwidth:
        break_points:
        - "520"
        - "540"
        - "720"
        - "960"
        image_sizes:
        - "494"
        - "517"
        - "674"
        - "914"
      halfwidth:
        break_points:
        - "520"
        - "540"
        image_sizes:
        - "494"
        - "517"
{
   "dnb": {
      "pictures": {
         "aspect_ratio": "2:1",
         "default_image": "images/og_sitewide.png",
         "responsive": {
            "fullwidth": {
               "break_points": [
                  "520",
                  "540",
                  "720",
                  "960"
               ],
               "image_sizes": [
                  "494",
                  "517",
                  "674",
                  "914"
               ]
            },
            "halfwidth": {
               "break_points": [
                  "520",
                  "540"
               ],
               "image_sizes": [
                  "494",
                  "517"
               ]
            }
         }
      }
   }
}

Optimisation

To be written.

Notes about:

  • resources directory
  • caching (with hugo-netlification)
  • preloading (needs implementation)
  • maybe a walkthrough how this module implements current features?

Further Readings

Usage Examples

Markdown Render Hook

![Good Dog 1](dog-1.jpg)

Pictures

![Good Dog 2](dog-2.jpg "with a title text")

Pictures

![Good Dog 3](dog-3.jpg?size=wide "with css-processed instructions")

Pictures

![Assets Cat](images/pictures/assets-cat.jpg "cat picture from the assets directory")

Pictures

![Static Cat](/dnb/pictures/static-cat.jpg "cat picture from the static directory")

Static Cat

{{< gallery gallery="gallery1" >}}

Image Shortcode

{{< image src="dog-1.jpg" >}}
{{< image src="dog-2.jpg" >}}

Sample Photo Sources