From b0c453902b3d585c31128e12a1b85d78697adcd8 Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Fri, 15 Aug 2025 11:31:23 +0200 Subject: [PATCH] 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/.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 Co-authored-by: Mathieu Fenniak Co-committed-by: Mathieu Fenniak --- services/feed/action.go | 6 ++++++ services/feed/action_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/services/feed/action.go b/services/feed/action.go index f178e99001..2d07f39284 100644 --- a/services/feed/action.go +++ b/services/feed/action.go @@ -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. diff --git a/services/feed/action_test.go b/services/feed/action_test.go index 35b170e95c..48de7995bf 100644 --- a/services/feed/action_test.go +++ b/services/feed/action_test.go @@ -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 {