hugo/docs/content/en/functions/anchorize.md
2022-03-26 11:04:57 +02:00

1.6 KiB

title description date categories menu keywords signature hugoversion workson relatedfuncs
anchorize Takes a string and sanitizes it the same way as the [`defaultMarkdownHandler`](https://gohugo.io/getting-started/configuration-markup#configure-markup) does for markdown headers. 2018-10-13
functions
docs
parent
functions
markdown
strings
anchorize INPUT
0.39
humanize

If Goldmark is set as defaultMarkdownHandler, the sanitizing logic adheres to the setting markup.goldmark.parser.autoHeadingIDType. If Blackfriday is set as defaultMarkdownHandler, this template function uses the SanitizedAnchorName logic from Blackfriday (the same applies when markup.goldmark.parser.autoHeadingIDType is set to blackfriday).

Since the defaultMarkdownHandler and this template function use the same sanitizing logic, you can use the latter to determine the ID of a header for linking with anchor tags.

{{ anchorize "This is a header" }} --> "this-is-a-header"
{{ anchorize "This is also    a header" }} --> "this-is-also----a-header"
{{ anchorize "main.go" }} --> "maingo"
{{ anchorize "Article 123" }} --> "article-123"
{{ anchorize "<- Let's try this, shall we?" }} --> "--lets-try-this-shall-we"
{{ anchorize "Hello, 世界" }} --> "hello-世界"