Hugo’s intersect Function⛅
Updated  2022-August-29

Page contents

News

2022-May-14  Published this evolving⁠[1] article.

 

Prerequisites

This article assumes you know about…

 

Syntax

In a nutshell, the syntax of Hugo’s intersect function is this:

intersect SLICE1 SLICE2

Or, in a pipeline, this:

SLICE2 | intersect SLICE1

 

💡

The order of the slice that results from this intersection is the order of SLICE1. This means that…

  • this is a way to force a particular order on a slice that is the result of an intersection

  • and Hugo’s intersect function is not commutative, i.e., intersect SLICE1 SLICE2 is not necessarily equal to intersect SLICE2 SLICE1.

 

To learn more about this function, see…

 

Example

In Infinite Ink’s Hugo project, I tag an article by including something like this in the front matter of the source of the article:

tags:
  - content
  - gohugo
  - mathematics
  - tech
  - yaml
  - secret

Note that the value of this tags[3] key is a slice.

 

To hide Infinite Ink’s secret⁠[4] tags from the public, I do these two steps.

  1. In the Hugo project’s config.yaml,⁠[5] I create the following user-⁠defined listablePortals site-⁠wide parameter.

    params:
     listablePortals:
      - activism-hacktivism
      - asciidoc
      - backup-sync
      - business-economics-money
      - cheatsheet
      - cli
      - elsewhere
      - emoji
      - forked
      - git
      - git-bash
      - gohugo
      - golang
      - intellij
      - iusethis
      - joplin
      - longform
      - markdown
      - mastodon
      - mathematics
      - mathjax
      - messaging
      - meta
      - nix-nux
      - privacy-security
      - productivity
      - qutebrowser
      - tech
      - tumblelog
      - twitter
      - unicode
      - utf-8
      - vim
      - vscode
      - webdev
      - windows
      - words
      - zeitgeist

    Note that the value of this listablePortals key is a slice.

 

  1. In the relevant Hugo layout file, I include this Go Template code:

    {{ $tags := .Params.tags }}
    {{ $portals :=  intersect site.Params.listablePortals $tags }}
    
    {{ range $portals }}
    <a href="/portal/{{- . -}}/">#{{- . -}}</a>&thinsp;
    {{ end }}

 

For this article, this intersection code produces this:

  #gohugo     #mathematics     #tech

 

More examples of results of this intersection code is in each tumblelogged item in the Elsewhere Links News section of Infinite Ink’s News.

 

See also

Endnotes


1. Many Infinite Ink articles, including this one, are evergreen and regularly updated.
2. Go Template code is also known as Go HTML. For details, see gohugo.io/categories/templates/, golang.org/pkg/html/template/, and golang.org/pkg/text/template/.
3. This tags front-⁠matter key is one of Hugo’s default taxonomies. To learn about this, see gohugo.io/content-management/taxonomies/ and www.ii.com/hugo-tutorial/#_taxonomies.
4. An Infinite Ink secret tag, for example yaml, is one that has not been turned into a portal. Most secret tags will eventually be portalized.⁠🤞
5. Alternatively, you could specify listablePortals in Hugo’s data Directory.

Discuss or share 📝 🤔 🐘