forgejo/modules/markup/markdown
psii a8490e6637 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>
2025-08-27 08:14:52 +02:00
..
callout chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
math fix: enable multi-line math equations in wiki (#8424) 2025-07-06 22:00:09 +02:00
util fix: enable multi-line math equations in wiki (#8424) 2025-07-06 22:00:09 +02:00
ast.go chore: Remove IsXXX 2025-01-31 16:22:29 +01:00
color_util.go feat: support color dots for 4 character hex. 2024-11-02 13:37:14 +01:00
color_util_test.go feat: support color dots for 4 character hex. 2024-11-02 13:37:14 +01:00
convertyaml.go chore: replace gopkg.in/yaml.v3 with go.yaml.in/yaml/v3 (#8956) 2025-08-20 15:31:12 +02:00
goldmark.go Markdown: generate unique per comment HTML IDs for footnotes and headers (#8880) 2025-08-27 08:14:52 +02:00
markdown.go fix: enable multi-line math equations in wiki (#8424) 2025-07-06 22:00:09 +02:00
markdown_test.go Markdown: generate unique per comment HTML IDs for footnotes and headers (#8880) 2025-08-27 08:14:52 +02:00
meta.go chore: replace gopkg.in/yaml.v3 with go.yaml.in/yaml/v3 (#8956) 2025-08-20 15:31:12 +02:00
meta_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
prefixed_id.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
renderconfig.go chore: replace gopkg.in/yaml.v3 with go.yaml.in/yaml/v3 (#8956) 2025-08-20 15:31:12 +02:00
renderconfig_test.go chore: replace gopkg.in/yaml.v3 with go.yaml.in/yaml/v3 (#8956) 2025-08-20 15:31:12 +02:00
toc.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
transform_codespan.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
transform_heading.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
transform_image.go fix(ui): add missing lazy load attribute to images (#8246) 2025-06-25 18:31:03 +02:00
transform_link.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
transform_list.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00