Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hyperlinks in headers break their ToC entries #13401

Open
Expurple opened this issue Feb 15, 2025 · 2 comments
Open

Hyperlinks in headers break their ToC entries #13401

Expurple opened this issue Feb 15, 2025 · 2 comments

Comments

@Expurple
Copy link

I originally reported this to the theme that I'm using. I'm not sure whether the theme or Hugo is responsible for the issue, but the theme maintainer responded that Hugo's built-in ToC generator is at fault.


Describe the bug

By themselves, hyperlinks in headers are functional. But they generate weird ToC entries.

Reproducible example

## Example [hyperlink](https://example.com/) in a header

generates a split-up ToC entry.

The left end takes you to the header (as expected):

Image

The middle part doesn't look like a hyperlink, but it's a separate hyperlink that takes you to an external website:

Image

Finally, the right part doesn't work. It's just nornal text that doesn't take you anywhere:

Image

Expected behavior

Normally, a ToC entry is a one big hyperlink takes you to the header:

Image

This is the most obvious behavior that I can imagine for my reproducer.

Another possible option to keep it "split" but fix the right end (so that it takes you to the header) and underline the middle part (so that it looks like a hyperlink, which it is).

Desktop (please complete the following information):

  • OS: Kubuntu 24.10 (x64)
  • Browser: Firefox 134.0 (64-bit)
  • Hugo Version: 0.129.0 from the distro
  • Kayal Version: 0.3.4

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.129.0+extended linux/amd64 BuildDate=2024-08-13T17:51:38Z VendorInfo=ubuntu:0.129.0-2

Does this issue reproduce with the latest release?

Yes. Before reporting, I downloaded Hugo 0.143.1 from Github Releases and tried it too.

@jmooring
Copy link
Member

jmooring commented Feb 15, 2025

Reference: https://discourse.gohugo.io/t/hyperlinks-in-headers-leak-into-header-anchors/53541

If you place ## Example [hyperlink](https://example.com/) in a header within a README.md file in a GitHub repository...

  1. The link is retained within the h2 element
  2. The id attribute of the h2 element is set to #example-hyperlink-in-a-header (it ignores the link target)
  3. The TOC entry does not contain the hyperlink; it's text is Example hyperlink in a header
  4. The TOC entry's href target is #example-hyperlink-in-a-header

With AsciiDoc content the result (both heading and TOC entry) is worse.

Obviously anything we do here is a breaking change, though what we have is already broken.

@jmooring
Copy link
Member

bluemonday might be handy here:

package main

import (
	"fmt"

	"github.com/microcosm-cc/bluemonday"
)

func main() {
	p := bluemonday.NewPolicy()
	p.AllowElements("code", "del", "em", "ins", "mark", "span", "strong", "sub", "sup")
	html := p.Sanitize(
		`A <a href="#fooo">link</a> in <strong>the</strong> <em>header</em> with <code>code</code>`,
	)
	fmt.Println(html)
}

out: A link in <strong>the</strong> <em>header</em> with <code>code</code>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants