How to urlize anchor IDs (not links)?

Hello. I’d like to set anchor tags on existing H1 headings on some single content pages.

The IDs is set as page param like so…

subtitle: "Hugo Rocks"

The titles are called like this…

<h1 id="{{ .Params.subtitle }}">{{ .Params.subtitle  }}<h1>

… but I need to convert the space to a hyphen and lowercase it because it outputs like this…

<h1 id="Hugo Rocks">Hugo Rocks<h1>

So I need to mimic the urlize function outside of a link: how can you do this in Hugo?

See: https://gohugo.io/functions/urlize/

Takes a string, sanitizes it for usage in URLs, and converts spaces to hyphens.

Are you saying this doesn’t work in your use case?

Because this:

 {{ "Test string" | urlize}}

displays:

test-string

So presumably:

{{ .Params.subtitle | urlize }}

Should work.

1 Like