Anchorize behavior has changed in v0.94.2

Since upgrading to v0.94.2, the behavior of anchorize has changed.

According to the docs:

{{anchorize "main.go"}} → "main-go"
{{anchorize "This is also a header"}} → "this-is-also-a-header"
{{anchorize "<- Let's try this, shall we?"}} → "let-s-try-this-shall-we"

I get:

{{anchorize "main.go"}} → "maingo"
{{anchorize "This is also a header"}} → "this-is-also----------a-header"
{{anchorize "<- Let's try this, shall we?"}} → "--lets-try-this-shall-we"

Can somebody confirm if they get the same results?

With Hugo 0.95.0: I can confirm that only “This is a header”, “Article 123”, “Hello, 世界” are rendered like described in the docs. Others like your find.

1 Like

I am afraid this regression in the behavior happened after this fix. The fix was to allow multiple hyphens in the slug if the user specifically wanted that:

This would be present since v0.93.0.

@moorereason Can you confirm?

Update: Corrected.

1 Like

It’s been like this for over two years. The change was introduced in v0.62.2.

[markup.goldmark.parser]
autoHeadingIDType = 'github'  # github (default), github-ascii, or blackfriday
String to anchorize autoHeadingIDType Result
a    b
github a----b
a    b
github-ascii a----b
a    b
blackfriday a-b
a.b
github ab
a.b
github-ascii ab
a.b
blackfriday a-b
<- a’b
github --ab
<- a’b
github-ascii --ab
<- a’b
blackfriday a-b
a, 世
github a-世
a, 世
github-ascii a-
a, 世
blackfriday a-世

And yes, our github setting creates anchors in the same way that GitHub creates anchors.

I’ve updated the documentation.

7 Likes

Thanks for sharing and updating the docs. I reverted to v0.92.1 which is the previous version I was using and the anchorize behavior is the same.

This is a bit puzzling, as something else then seems to have changed since v0.92.1 to v0.94.2 (and v0.95) as my code is behaving differently.

This is the contents of a partial that’s called in products/single:

{{$product := replace .File.BaseFileName (anchorize .Title | urlize) ""}}
{{$product := strings.TrimRight "-" $product}}
{{$product := cond (in .File.BaseFileName (anchorize .Title | urlize)) $product (anchorize .Params.product | urlize)}}
{{$productProfile := .GetPage (printf "/products/%s" $product)}}
{{return $productProfile}}

With a {{.File.BaseFileName}} like Nectar.com and {{.Title}} like Apple & Honey, in v0.92.1 it would give:

nectar-com-
nectar-com
nectar-com
Page(/products/nectar-com.md)

It would produce the same results regardless of if the {{.Title}} was Apple - Honey, Apple/Honey, Apple/ Honey, Apple / Honey or Apple + Honey.

Following the upgrade, I now get:

nectar-com-
nectar-com
nectarcom
nopPage

I get the same outcome with the following {{.Title}} formats Apple - Honey, or Apple / Honey and Apple + Honey. It seems only Apple/ Honey renders the same outcome as before.

As the change seems to be linked to this statement:

{{$product := cond (in .File.BaseFileName (anchorize .Title | urlize)) $product (anchorize .Params.product | urlize)}}

I though anchorize was the culprit, but that does not seem to be the case.

Any thoughts on what else may have changed that is causing my results to be different?

1 Like

Yes, the urlize function changed as of v0.93.0 due to the change that @kaushalmodi referenced above. Sequential hyphens are no longer reduced to a single hyphen

input version urlize output
a----b 0.92.2 a-b
a----b 0.93.0 a----b
1 Like

I see. Thanks for sharing.

Do you have any suggestions on how I can update my code to have the same behavior as before?

Can you explain, concisely, what you are trying to achieve?

1 Like

Sure. Thank you for your willingness to help.

Essentially, what I’m trying to do is given a .Title with special characters, I need to replace these with hyphens.

Examples:

apple + pear --> apple-pear
apple/pear   --> apple-pear
apple/ pear  --> apple-pear
apple / pear --> apple-pear
apple & pear --> apple-pear
apple (pear) --> apple-pear
apple - pear --> apple-pear

The anchorize method will reflect the method used to create anchors from Markdown to make the navigation correct.

I’m not sure I follow.

If I use the anchorize method to the above examples, I don’t get the required outcome.

Here’s what I get:

apple + pear --> apple--pear
apple/pear   --> applepear
apple/ pear  --> apple-pear
apple / pear --> apple--pear
apple & pear --> apple--pear
apple (pear) --> apple-pear
apple - pear --> apple---pear

You’ve explained how you would like to transform a string, but you have not explained what you are trying to do. It seems like you want to obtain a page reference from something, but it is difficult to understand the entire picture.

I understand, I was trying to remove all logic from what I’m trying to do, to focus in on the problem. I’ll need some more time to put together a full picture.

A solution could be to change the configuration but nobody knows if you will have strange side effects then elsewhere. Because I don’t have them I use in config.yml:

markup:
...
  goldmark:
    ...
    parser:
      ...
      autoHeadingIDType: blackfriday
...

Result with anchorize:

apple + pear --> apple-pear
Apple + Pear --> apple-pear
apple/pear --> apple-pear
apple/ pear --> apple-pear
apple / pear --> apple-pear
APPLE & PEAR --> apple-pear
apple (pear) --> apple-pear
apple - pear --> apple-pear
2 Likes

@runhide If you have multiple anchors with apple-pear the browser will probably just go to the first one when you click on a link with #apple-pear.

If that is not what you want, a more complete description of what you are trying to achieve (as requested by Joe) is definitely what you want, since then we (and especially Joe, as a extremely knowledge and helpful Hugo ‘guru’) can help you get the end result you seek.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.