From 24cc25552f607eccf7f7fefff3d6a7bd8175c828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 16 Feb 2025 21:52:46 +0100 Subject: [PATCH] Fix auto generated header ids so they don't contain e.g. hyperlink destinations (note) This makes the header ids match the newly added dt ids. Also make sure newlines are preserved in hooks' `.PlainText`. Fixes #13405 Fixes #13410 --- hugolib/content_render_hooks_test.go | 28 +++++++++++++++++++ markup/goldmark/goldmark_integration_test.go | 2 +- .../extensions/attributes/attributes.go | 21 +++++++++----- .../attributes/attributes_integration_test.go | 9 ++++-- markup/goldmark/internal/render/context.go | 9 +++++- markup/goldmark/toc_integration_test.go | 4 +-- 6 files changed, 59 insertions(+), 14 deletions(-) diff --git a/hugolib/content_render_hooks_test.go b/hugolib/content_render_hooks_test.go index de8eb720f..9df8d2e2e 100644 --- a/hugolib/content_render_hooks_test.go +++ b/hugolib/content_render_hooks_test.go @@ -350,3 +350,31 @@ Image: ![alt-"<>&](/destination-"<> 'title-"<>&') }) } } + +// Issue 13410. +func TestRenderHooksMultilineTitlePlainText(t *testing.T) { + t.Parallel() + + files := ` +-- hugo.toml -- +-- content/p1.md -- +--- +title: "p1" +--- + +First line. +Second line. +---------------- +-- layouts/_default/_markup/render-heading.html -- +Plain text: {{ .PlainText }}|Text: {{ .Text }}| +-- layouts/_default/single.html -- +Content: {{ .Content}}| +} +` + b := Test(t, files) + + b.AssertFileContent("public/p1/index.html", + "Content: Plain text: First line.\nSecond line.|", + "|Text: First line.\nSecond line.||\n", + ) +} diff --git a/markup/goldmark/goldmark_integration_test.go b/markup/goldmark/goldmark_integration_test.go index 184d0d7fc..a65600951 100644 --- a/markup/goldmark/goldmark_integration_test.go +++ b/markup/goldmark/goldmark_integration_test.go @@ -157,7 +157,7 @@ title: "p1" b := hugolib.Test(t, files) b.AssertFileContent("public/p1/index.html", - "

\n Hello Test\n\n #\n

", + "

\n Hello Test\n\n #\n

", ) } diff --git a/markup/goldmark/internal/extensions/attributes/attributes.go b/markup/goldmark/internal/extensions/attributes/attributes.go index 526635f45..f5f6f97b4 100644 --- a/markup/goldmark/internal/extensions/attributes/attributes.go +++ b/markup/goldmark/internal/extensions/attributes/attributes.go @@ -1,6 +1,8 @@ package attributes import ( + "strings" + "github.com/gohugoio/hugo/markup/goldmark/goldmark_config" "github.com/gohugoio/hugo/markup/goldmark/internal/render" "github.com/yuin/goldmark" @@ -181,12 +183,17 @@ func (a *transformer) generateAutoID(n ast.Node, reader text.Reader, pc parser.C } // Markdown settext headers can have multiple lines, use the last line for the ID. -func textHeadingID(node *ast.Heading, reader text.Reader) []byte { - var line []byte - lastIndex := node.Lines().Len() - 1 - if lastIndex > -1 { - lastLine := node.Lines().At(lastIndex) - line = lastLine.Value(reader.Source()) +func textHeadingID(n *ast.Heading, reader text.Reader) []byte { + text := render.TextPlain(n, reader.Source()) + if n.Lines().Len() > 1 { + + // For multiline headings, Goldmark's extension for headings returns the last line. + // We have a slightly different approach, but in most cases the end result should be the same. + // Instead of looking at the text segments in Lines (see #13405 for issues with that), + // we split the text above and use the last line. + parts := strings.Split(text, "\n") + text = parts[len(parts)-1] } - return line + + return []byte(text) } diff --git a/markup/goldmark/internal/extensions/attributes/attributes_integration_test.go b/markup/goldmark/internal/extensions/attributes/attributes_integration_test.go index fcce68ac2..90cf3d084 100644 --- a/markup/goldmark/internal/extensions/attributes/attributes_integration_test.go +++ b/markup/goldmark/internal/extensions/attributes/attributes_integration_test.go @@ -47,10 +47,12 @@ foo [something](/a/b/) bar Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď : Testing accents. -Mutiline set text header +Multiline set text header Second line --------------- +## Example [hyperlink](https://example.com/) in a header + -- layouts/_default/single.html -- {{ .Content }}|Identifiers: {{ .Fragments.Identifiers }}| ` @@ -68,7 +70,8 @@ Second line `
My Title
`, `
良善天父
`, `
Ā ā Ă ă Ą ą Ć ć Ĉ ĉ Ċ ċ Č č Ď
`, - `

Mutiline set text header`, - "|Identifiers: [a-a-a-a-a-a-c-c-c-c-c-c-c-c-d base-name base-name-1 foo-something-bar foobar my-title my-title-1 second-line term title-with-id title-with-id]|", + `

`, + `