HUGO
Menu
GitHub 87548 stars Mastodon

urls.Anchorize

Returns the given string, sanitized for usage in an HTML id attribute.

Syntax

urls.Anchorize INPUT

Returns

string

Alias

anchorize

anchorizeurlize 函数相似:

  • 使用 anchorize 函数生成 HTML id 属性值
  • 使用 urlize 函数清理字符串以用于 URL

例如:

{{ $s := "A B C" }}
{{ $s | anchorize }} → a-b-c
{{ $s | urlize }} → a-b-c

{{ $s := "a b   c" }}
{{ $s | anchorize }} → a-b---c
{{ $s | urlize }} → a-b-c

{{ $s := "< a, b, & c >" }}
{{ $s | anchorize }} → -a-b--c-
{{ $s | urlize }} → a-b-c

{{ $s := "main.go" }}
{{ $s | anchorize }} → maingo
{{ $s | urlize }} → main.go

{{ $s := "Hugö" }}
{{ $s | anchorize }} → hugö
{{ $s | urlize }} → hug%C3%B6
{{__hugo_ctx/}}



## Sanitizing logic

With the default Markdown renderer, Goldmark, the sanitizing logic is controlled by your site configuration:




markup:
  goldmark:
    parser:
      autoHeadingIDType: github
[markup]
  [markup.goldmark]
    [markup.goldmark.parser]
      autoHeadingIDType = 'github'
{
   "markup": {
      "goldmark": {
         "parser": {
            "autoHeadingIDType": "github"
         }
      }
   }
}
This controls the behavior of the `anchorize` function and the generation of heading IDs when rendering Markdown to HTML. Set `autoHeadingIDType` to one of: github : Compatible with GitHub. This is the default. github-ascii : Similar to the `github` setting, but removes non-ASCII characters. blackfriday : Provided for backwards compatibility with Hugo v0.59.1 and earlier. This option will be removed in a future release.

Last updated: January 1, 0001
Improve this page