forgejo/modules/markup
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
..
asciicast chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
common fix various typos (#7690) 2025-04-28 06:46:29 +00:00
console Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
csv Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
external chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
markdown Markdown: generate unique per comment HTML IDs for footnotes and headers (#8880) 2025-08-27 08:14:52 +02:00
mdstripper Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
orgmode Update module github.com/alecthomas/chroma/v2 to v2.20.0 (forgejo) (#8783) 2025-08-06 01:30:20 +02:00
tests/repo/repo1_filepreview fix inline file preview for files with encoded URL, fix #5069 (#6525) 2025-01-30 08:20:05 +00:00
camo.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
camo_test.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
file_preview.go chore(ui): remove ineffective class small in inline code preview (#7414) 2025-04-01 15:55:56 +00:00
html.go chore: simplify storing and sending custom emoji's 2025-08-10 21:45:55 +02:00
html_internal_test.go fix: make hash pattern more strict (#7775) 2025-05-05 05:29:55 +00:00
html_test.go chore: simplify storing and sending custom emoji's 2025-08-10 21:45:55 +02:00
renderer.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
renderer_test.go Move IsReadmeFile* from modules/markup/ to modules/util (#22877) 2023-02-13 15:01:09 -05:00
sanitizer.go fix(ui): add missing lazy load attribute to images (#8246) 2025-06-25 18:31:03 +02:00
sanitizer_test.go fix(ui): add missing lazy load attribute to images (#8246) 2025-06-25 18:31:03 +02:00