HUGO
Menu
GitHub 90498 stars Mastodon

urls.Anchorize

Returns the given string, sanitized for usage in an HTML id attribute.
Syntax
urls.Anchorize INPUT
Returns
string
Alias
anchorize

Usage

The urls.Anchorize function returns the given string, sanitized for usage in an HTML id attribute. The function sanitizes the string with the same strategy that Hugo uses to generate id attributes for headings, as defined by the autoIDType setting in your project configuration. The default strategy is github.

To sanitize a string for usage in a URL, use the urls.URLize function instead.

Examples

{{ urls.Anchorize "Hello, World!" }} → hello-world
{{ urls.Anchorize "a b   c" }} → a-b---c
{{ urls.Anchorize "main.go" }} → maingo
{{ urls.Anchorize "Les Misérables" }} → les-misérables

With autoIDType set to github-ascii:

{{ urls.Anchorize "Hello, World!" }} → hello-world
{{ urls.Anchorize "a b   c" }} → a-b---c
{{ urls.Anchorize "main.go" }} → maingo
{{ urls.Anchorize "Les Misérables" }} → les-miserables

With autoIDType set to blackfriday:

{{ urls.Anchorize "Hello, World!" }} → hello-world
{{ urls.Anchorize "a b   c" }} → a-b-c
{{ urls.Anchorize "main.go" }} → main-go
{{ urls.Anchorize "Les Misérables" }} → les-misérables

Comparing the result with urls.URLize, using the default strategy:

{{ $s := "A B C" }}
{{ urls.Anchorize $s }} → a-b-c
{{ urls.URLize $s }} → a-b-c

{{ $s := "a b   c" }}
{{ urls.Anchorize $s }} → a-b---c
{{ urls.URLize $s }} → a-b-c

{{ $s := "< a, b, & c >" }}
{{ urls.Anchorize $s }} → -a-b--c-
{{ urls.URLize $s }} → a-b-c

{{ $s := "main.go" }}
{{ urls.Anchorize $s }} → maingo
{{ urls.URLize $s }} → main.go

{{ $s := "Hugö" }}
{{ urls.Anchorize $s }} → hugö
{{ urls.URLize $s }} → hug%C3%B6