fix: comment starting with a mermaid block displays error in activity feed (#8896)

In the event a comment starts with a fenced code block, don't display a blank block (or a mermaid error) in the activity feed.  Instead, do not display an abbreviated comment text at all, and just display the issue/PR title.

![image](/attachments/6d45bcef-4cf8-40c6-9ed4-ee52332fa299)

Handles one more case identified in #8781, posted by @skedastically in https://codeberg.org/forgejo/forgejo/issues/8781#issuecomment-6274957.

## 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.
- [x] 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/8896
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
This commit is contained in:
Mathieu Fenniak 2025-08-15 11:31:23 +02:00 committed by Earl Warren
commit b0c453902b
2 changed files with 16 additions and 0 deletions

View file

@ -488,6 +488,12 @@ func encodeContent(params ...string) string {
func abbreviatedComment(comment string) string {
firstLine := strings.Split(comment, "\n")[0]
if strings.HasPrefix(firstLine, "```") {
// First line is is a fenced code block... with no special abbreviate we would display a blank block, or in the
// worst-case a ```mermaid would display an error. Better to omit the comment.
return ""
}
truncatedContent, truncatedRight := util.SplitStringAtByteN(firstLine, 200)
if truncatedRight != "" {
// in case the content is in a Latin family language, we remove the last broken word.

View file

@ -185,6 +185,16 @@ func TestAbbreviatedComment(t *testing.T) {
input: "Interesting point, here's a digram with my thoughts:\n```mermaid\ngraph LR\n a -->|some text| b\n```",
expected: "Interesting point, here's a digram with my thoughts:",
},
{
name: "block start",
input: "```\n# This file describes the expected reviewers for a PR based on the changed\n# files.\n```\n\nI think this comment is wrong...",
expected: "",
},
{
name: "labeled block start",
input: "```mermaid\ngraph LR\n a -->|some text| b\n```",
expected: "",
},
}
for _, tt := range tests {