From a8490e6637caea50bb7f237db26bc26cea0c9ca1 Mon Sep 17 00:00:00 2001 From: psii Date: Wed, 27 Aug 2025 08:14:52 +0200 Subject: [PATCH] Markdown: generate unique per comment HTML IDs for footnotes and headers (#8880) This PR fixes the duplicate HTML ID issue (#8778) that can occur when rendering an issue page. An issue page contains multiple renderings of Markdown elements (the issue description and issue comments). When there is more than one Markdown rendering that uses footnotes we have an ID collision because the footnote ID count starts anew for each rendering. This PR adds a `Metas["scope"]` entry to the `RenderContext` and sets it to "comment-{index}" when rendering Markdown for a comment. `Metas["scope"]` gets used as suffix during ID generation. Fixes #8778. ## Additional Notes - ID collision also happens if there are multiple header elements with the same ID. It does not seem to impact users because there are no links referencing headers. However, to err on the safe side, this PR also adds scope to the header IDs. - `Metas["scope"]` might be useful for other Markdown renderings beside comments, e.g. multiple Markdown renderings are possible on Wiki pages. - We apply `Metas["scope"]` as suffix instead of prefix because there are several places in the code that expect `user-content-` to be the ID's prefix. ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [X] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [X] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/.md` to be be used for the release notes instead of the title. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8880 Reviewed-by: Otto Co-authored-by: psii Co-committed-by: psii --- modules/markup/markdown/goldmark.go | 13 +++++++ modules/markup/markdown/markdown_test.go | 43 ++++++++++++++++++++++++ routers/web/repo/issue.go | 9 +++-- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/modules/markup/markdown/goldmark.go b/modules/markup/markdown/goldmark.go index 67d81488fd..8a3da3b08f 100644 --- a/modules/markup/markdown/goldmark.go +++ b/modules/markup/markdown/goldmark.go @@ -9,6 +9,7 @@ import ( "strings" "forgejo.org/modules/markup" + "forgejo.org/modules/markup/common" markdownutil "forgejo.org/modules/markup/markdown/util" "forgejo.org/modules/setting" @@ -74,6 +75,18 @@ func (g *ASTTransformer) Transform(node *ast.Document, reader text.Reader, pc pa } case *ast.CodeSpan: g.transformCodeSpan(ctx, v, reader) + case *common.Footnote: + if scope, found := ctx.Metas["scope"]; found { + v.Name = fmt.Appendf(v.Name, "-%s", scope) + } + case *common.FootnoteLink: + if scope, found := ctx.Metas["scope"]; found { + v.Name = fmt.Appendf(v.Name, "-%s", scope) + } + case *common.FootnoteBackLink: + if scope, found := ctx.Metas["scope"]; found { + v.Name = fmt.Appendf(v.Name, "-%s", scope) + } } return ast.WalkContinue, nil }) diff --git a/modules/markup/markdown/markdown_test.go b/modules/markup/markdown/markdown_test.go index c854861031..f9306b99a2 100644 --- a/modules/markup/markdown/markdown_test.go +++ b/modules/markup/markdown/markdown_test.go @@ -802,6 +802,49 @@ Citation needed[^0].`, } } +func TestFootnoteWithScope(t *testing.T) { + testcases := []struct { + testcase string + expected string + }{ + { + `Citation needed[^0]. +[^0]: Source`, + `

Citation needed1.

+
+
+
    +
  1. +

    Source ↩︎

    +
  2. +
+
+`, + }, { + `[^0]: Source + +Citation needed[^0].`, + `

Citation needed1.

+
+
+
    +
  1. +

    Source ↩︎

    +
  2. +
+
+`, + }, + } + + for _, test := range testcases { + metas := map[string]string{"scope": "comment-999"} + res, err := markdown.RenderString(&markup.RenderContext{Ctx: git.DefaultContext, Metas: metas}, test.testcase) + require.NoError(t, err, "Unexpected error in testcase: %q", test.testcase) + assert.Equal(t, test.expected, string(res), "Unexpected result in testcase %q", test.testcase) + } +} + func TestTaskList(t *testing.T) { testcases := []struct { testcase string diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index cab508e6c6..715f5dca8f 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1590,6 +1590,7 @@ func ViewIssue(ctx *context.Context) { ok bool marked = make(map[int64]issues_model.RoleDescriptor) comment *issues_model.Comment + commentIdx int participants = make([]*user_model.User, 1, 10) latestCloseCommentID int64 ) @@ -1645,15 +1646,17 @@ func ViewIssue(ctx *context.Context) { return } - for _, comment = range issue.Comments { + for commentIdx, comment = range issue.Comments { comment.Issue = issue + metas := ctx.Repo.Repository.ComposeMetas(ctx) + metas["scope"] = fmt.Sprintf("comment-%d", commentIdx) if comment.Type == issues_model.CommentTypeComment || comment.Type == issues_model.CommentTypeReview { comment.RenderedContent, err = markdown.RenderString(&markup.RenderContext{ Links: markup.Links{ Base: ctx.Repo.RepoLink, }, - Metas: ctx.Repo.Repository.ComposeMetas(ctx), + Metas: metas, GitRepo: ctx.Repo.GitRepo, Ctx: ctx, }, comment.Content) @@ -1730,7 +1733,7 @@ func ViewIssue(ctx *context.Context) { Links: markup.Links{ Base: ctx.Repo.RepoLink, }, - Metas: ctx.Repo.Repository.ComposeMetas(ctx), + Metas: metas, GitRepo: ctx.Repo.GitRepo, Ctx: ctx, }, comment.Content)