DEV Community

spO0q
spO0q

Posted on

Hugo: remove accents from anchors

So I have this very specific need: removing accents from generated anchors with Hugo.

To my knowledge, there is no accent in English, but other languages contain plenty of them.

For example, in French, it gives:

<h2 id="#les-frameworks-de-r%C3%A9f%C3%A9rence">
Enter fullscreen mode Exit fullscreen mode

The problem is that if you use {{ .TableOfContents }} to generate a table of contents for your posts, anchors have to be exactly same as those generated in {{ .Content }}.

Therefore, it's not possible to manually filter headings or override them in layouts.

This global configuration seems effective:

[markup]
  [markup.goldmark]
    [markup.goldmark.parser]
      autoHeadingIDType = 'github-ascii'
Enter fullscreen mode Exit fullscreen mode

This config is similar to the default autoHeadingIDType (github), but it removes non-ASCII characters.

Now, I get:

<h2 id="#les-frameworks-de-reference">
Enter fullscreen mode Exit fullscreen mode

Top comments (0)