Categories with '&' chars being converted to folders and links with double dashes. How to fix it?

My articles’s front matter may contain categories with names such as “A & B”:

categories: ["A & B", "C"]

Which causes Hugo to create folders with double dashes:

public/categories/a--b

This behavior is not present if the categories contain spaces or dashes:

categories: ["A B"]
public/categories/a-b
categories: ["A-B"]
public/categories/a-b

How can I fix it such that when categories do contain ‘&’, it still maintains the single dash in the directory output and link references? I tried the different Sanitization approaches as described in urls.Anchorize to no effect.

Thanks in advance.

$ hugo env
hugo v0.145.0+extended+withdeploy darwin/arm64 BuildDate=2025-02-26T15:41:25Z VendorInfo=brew
GOOS="darwin"
GOARCH="arm64"
GOVERSION="go1.24.0"
github.com/sass/libsass="3.6.6"
github.com/webmproject/libwebp="v1.3.2"

You cannot change the logical path. If you could change it the way you want to, it would collide with your other examples (“A B”, “A-B”).

Interesting. Is there any way to convert it to use “and” then? Such that “A & B” would become “a-and-b”?

guess that means No

Does that mean there is no way to use an ampersand in a category? Or how would you create links to these categories when urlize creates a-b but the actual file is at a--b?

I hadn’t seen this topic and asked something similar with code examples here: Ampersand in taxonomy term - #3 by dominik.mayer

No.

In what context? Can you provide an example?

Yes, the whole example is here: Ampersand in taxonomy term.

Yeah, you don’t want to do it that way. It’s fragile on a number of fronts.

https://gohugo.io/methods/page/getterms

1 Like

This works great. Thank you!

1 Like