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/<pull request number>.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 <otto@codeberg.org>
Co-authored-by: psii <psii@posteo.de>
Co-committed-by: psii <psii@posteo.de>
This commit is contained in:
psii 2025-08-27 08:14:52 +02:00 committed by Earl Warren
commit a8490e6637
3 changed files with 62 additions and 3 deletions

View file

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

View file

@ -802,6 +802,49 @@ Citation needed[^0].`,
}
}
func TestFootnoteWithScope(t *testing.T) {
testcases := []struct {
testcase string
expected string
}{
{
`Citation needed[^0].
[^0]: Source`,
`<p>Citation needed<sup id="fnref:user-content-0-comment-999"><a href="#fn:user-content-0-comment-999" rel="nofollow">1</a></sup>.</p>
<div>
<hr/>
<ol>
<li id="fn:user-content-0-comment-999">
<p>Source <a href="#fnref:user-content-0-comment-999" rel="nofollow"></a></p>
</li>
</ol>
</div>
`,
}, {
`[^0]: Source
Citation needed[^0].`,
`<p>Citation needed<sup id="fnref:user-content-0-comment-999"><a href="#fn:user-content-0-comment-999" rel="nofollow">1</a></sup>.</p>
<div>
<hr/>
<ol>
<li id="fn:user-content-0-comment-999">
<p>Source <a href="#fnref:user-content-0-comment-999" rel="nofollow"></a></p>
</li>
</ol>
</div>
`,
},
}
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

View file

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