From fad28141fa23419ed980c249facf11912d0c95b5 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 8 Feb 2025 13:34:09 +0000 Subject: [PATCH] fix: render link in heading correctly in TOC (#6853) - When you use a link in a heading such as `# [Text](link)` (instead of the conventional `# Text`) the TOC should only show `Text` and not `[Text](link)`. - Use the `mdutil.Text` to only get the text from actual text nodes and not the text that was provided in the markdown input. - Regression of e2fddcf681e9206b3da826680205537f87690b7f - Resolves forgejo/forgejo#6847 - Added integration test. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6853 Reviewed-by: Earl Warren Co-authored-by: Gusted Co-committed-by: Gusted --- modules/markup/markdown/transform_heading.go | 3 ++- tests/integration/repo_wiki_test.go | 24 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/modules/markup/markdown/transform_heading.go b/modules/markup/markdown/transform_heading.go index d869ee5de9..79fc9a3067 100644 --- a/modules/markup/markdown/transform_heading.go +++ b/modules/markup/markdown/transform_heading.go @@ -7,6 +7,7 @@ import ( "fmt" "code.gitea.io/gitea/modules/markup" + mdutil "code.gitea.io/gitea/modules/markup/markdown/util" "code.gitea.io/gitea/modules/util" "github.com/yuin/goldmark/ast" @@ -19,7 +20,7 @@ func (g *ASTTransformer) transformHeading(_ *markup.RenderContext, v *ast.Headin v.SetAttribute(attr.Name, []byte(fmt.Sprintf("%v", attr.Value))) } } - txt := v.Lines().Value(reader.Source()) + txt := mdutil.Text(v, reader.Source()) header := markup.Header{ Text: util.UnsafeBytesToString(txt), Level: v.Level, diff --git a/tests/integration/repo_wiki_test.go b/tests/integration/repo_wiki_test.go index 6115010ba4..eeb49cc7cf 100644 --- a/tests/integration/repo_wiki_test.go +++ b/tests/integration/repo_wiki_test.go @@ -89,3 +89,27 @@ func TestWikiBranchNormalize(t *testing.T) { assert.Equal(t, setting.Repository.DefaultBranch, repo.GetWikiBranchName()) assertNormalizeButton(false) } + +func TestWikiTOC(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + username := "user2" + session := loginUser(t, username) + + t.Run("Link in heading", func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + req := NewRequestWithValues(t, "POST", "/user2/repo1/wiki/Home?action=_edit", map[string]string{ + "_csrf": GetCSRF(t, session, "/user2/repo1/wiki/Home"), + "title": "Home", + "content": "# [Helpdesk](Helpdesk)", + }) + session.MakeRequest(t, req, http.StatusSeeOther) + + req = NewRequest(t, "GET", "/user2/repo1/wiki/Home") + resp := MakeRequest(t, req, http.StatusOK) + htmlDoc := NewHTMLParser(t, resp.Body) + + assert.EqualValues(t, "Helpdesk", htmlDoc.Find(".wiki-content-toc a").Text()) + }) +}