0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-02-14 10:08:30 -05:00

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 e2fddcf681
- Resolves forgejo/forgejo#6847
- Added integration test.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/6853
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Gusted <postmaster@gusted.xyz>
Co-committed-by: Gusted <postmaster@gusted.xyz>
This commit is contained in:
Gusted 2025-02-08 13:34:09 +00:00 committed by Gusted
parent 97f743a89e
commit fad28141fa
2 changed files with 26 additions and 1 deletions

View file

@ -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,

View file

@ -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())
})
}