From 8c8d6460994a9897c9df3cbfacc61c623c55e714 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 19 Jul 2025 15:03:10 +0200 Subject: [PATCH] fix: correct image source for quoted reply (#8565) - Since v10 replies are generated on the fly to handle quoted reply (forgejo/forgejo#5677), this means that we have to do some work to construct markdown that is equivalent to the HTML of the comment. - Images are slightly strange in the context of issues and pull requests, as Forgejo will render them in the context of the repository and as such links such as `/attachments` become `/user/repo/attachments`, the quoted reply did not take into account and would use `/user/repo/attachments` as link which means it gets transformed to `/user/repo//user/repo/attachments`. - Instead of fixing this on the backend (and maybe break some existing links), teach the quoted reply about this context and remove it from the image source before generating the markdown. Reported-by: mrwusel (via Matrix) Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8565 Reviewed-by: 0ko <0ko@noreply.codeberg.org> Reviewed-by: Beowulf Co-authored-by: Gusted Co-committed-by: Gusted --- .../repo/issue/view_content/context_menu.tmpl | 2 +- tests/e2e/fixtures/comment.yml | 4 +- tests/e2e/issue-comment.test.e2e.ts | 6 ++- web_src/js/features/repo-legacy.js | 53 +++++++++++-------- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/templates/repo/issue/view_content/context_menu.tmpl b/templates/repo/issue/view_content/context_menu.tmpl index 6f43f768b3..8cd4a056f0 100644 --- a/templates/repo/issue/view_content/context_menu.tmpl +++ b/templates/repo/issue/view_content/context_menu.tmpl @@ -11,7 +11,7 @@ {{end}}
{{ctx.Locale.Tr "repo.issues.context.copy_link"}}
{{if and .ctxData.IsSigned (not .ctxData.Repository.IsArchived)}} -
{{ctx.Locale.Tr "repo.issues.context.quote_reply"}}
+
{{ctx.Locale.Tr "repo.issues.context.quote_reply"}}
{{if not .ctxData.UnitIssuesGlobalDisabled}}
{{ctx.Locale.Tr "repo.issues.context.reference_issue"}}
{{end}} diff --git a/tests/e2e/fixtures/comment.yml b/tests/e2e/fixtures/comment.yml index f3f8a2d8e8..ae7c850036 100644 --- a/tests/e2e/fixtures/comment.yml +++ b/tests/e2e/fixtures/comment.yml @@ -3,7 +3,7 @@ type: 0 # comment poster_id: 2 issue_id: 1 # in repo_id 1 - content: "## Lorem Ipsum\nI would like to say that **I am not appealed** that it took _so long_ for this `feature` to be [created](https://example.com) $e^{\\pi i} + 1 = 0$\n$$e^{\\pi i} + 1 = 0$$\n#1\n```js\nconsole.log('evil')\nalert('evil')\n```\n:+1: :100:" + content: "## Lorem Ipsum\nI would like to say that **I am not appealed** that it took _so long_ for this `feature` to be [created](https://example.com) $e^{\\pi i} + 1 = 0$\n$$e^{\\pi i} + 1 = 0$$\n#1\n```js\nconsole.log('evil')\nalert('evil')\n```\n:+1: :100:\n![hi there](/attachments/3f4f4016-877b-46b3-b79f-ad24519a9cf2)\n\nsomething something" created_unix: 946684811 updated_unix: 946684811 content_version: 1 @@ -13,7 +13,7 @@ type: 21 # code comment poster_id: 2 issue_id: 19 - content: "## Lorem Ipsum\nI would like to say that **I am not appealed** that it took _so long_ for this `feature` to be [created](https://example.com) $e^{\\pi i} + 1 = 0$\n$$e^{\\pi i} + 1 = 0$$\n#1\n```js\nconsole.log('evil')\nalert('evil')\n```\n:+1: :100:" + content: "## Lorem Ipsum\nI would like to say that **I am not appealed** that it took _so long_ for this `feature` to be [created](https://example.com) $e^{\\pi i} + 1 = 0$\n$$e^{\\pi i} + 1 = 0$$\n#1\n```js\nconsole.log('evil')\nalert('evil')\n```\n:+1: :100:\n![hi there](/attachments/3f4f4016-877b-46b3-b79f-ad24519a9cf2)\n\nsomething something" review_id: 1001 line: 1 tree_path: "test1.txt" diff --git a/tests/e2e/issue-comment.test.e2e.ts b/tests/e2e/issue-comment.test.e2e.ts index d2dbafafd0..2017e4563e 100644 --- a/tests/e2e/issue-comment.test.e2e.ts +++ b/tests/e2e/issue-comment.test.e2e.ts @@ -123,7 +123,8 @@ test('Quote reply', async ({page}, workerInfo) => { "> alert('evil')\n" + '> ```\n' + '> \n' + - '> :+1: :100:\n\n'); + '> :+1: :100: [![hi there](/attachments/3f4f4016-877b-46b3-b79f-ad24519a9cf2)](/user2/repo1/attachments/3f4f4016-877b-46b3-b79f-ad24519a9cf2)\n' + + '> something something\n\n'); await editorTextarea.fill(''); @@ -197,7 +198,8 @@ test('Pull quote reply', async ({page}, workerInfo) => { "> alert('evil')\n" + '> ```\n' + '> \n' + - '> :+1: :100:\n\n'); + '> :+1: :100: [![hi there](/attachments/3f4f4016-877b-46b3-b79f-ad24519a9cf2)](/user2/commitsonpr/attachments/3f4f4016-877b-46b3-b79f-ad24519a9cf2)\n' + + '> something something\n\n'); await editorTextarea.fill(''); }); diff --git a/web_src/js/features/repo-legacy.js b/web_src/js/features/repo-legacy.js index e6af4cbf04..66ee945d02 100644 --- a/web_src/js/features/repo-legacy.js +++ b/web_src/js/features/repo-legacy.js @@ -531,6 +531,13 @@ const filters = { } return el; }, + IMG(el, context) { + const src = el.getAttribute('src'); + if (src?.startsWith(context)) { + el.src = src.slice(context.length); + } + return el; + }, }; function hasContent(node) { @@ -538,32 +545,34 @@ function hasContent(node) { } // This code matches that of what is done by @github/quote-selection -function preprocessFragment(fragment) { - const nodeIterator = document.createNodeIterator(fragment, NodeFilter.SHOW_ELEMENT, { - acceptNode(node) { - if (node.nodeName in filters && hasContent(node)) { - return NodeFilter.FILTER_ACCEPT; +function preprocessFragment(context) { + return function(fragment) { + const nodeIterator = document.createNodeIterator(fragment, NodeFilter.SHOW_ELEMENT, { + acceptNode(node) { + if (node.nodeName in filters && hasContent(node)) { + return NodeFilter.FILTER_ACCEPT; + } + + return NodeFilter.FILTER_SKIP; + }, + }); + const results = []; + let node = nodeIterator.nextNode(); + + while (node) { + if (node instanceof HTMLElement) { + results.push(node); } - - return NodeFilter.FILTER_SKIP; - }, - }); - const results = []; - let node = nodeIterator.nextNode(); - - while (node) { - if (node instanceof HTMLElement) { - results.push(node); + node = nodeIterator.nextNode(); } - node = nodeIterator.nextNode(); - } // process deepest matches first - results.reverse(); + results.reverse(); - for (const el of results) { - el.replaceWith(filters[el.nodeName](el)); - } + for (const el of results) { + el.replaceWith(filters[el.nodeName](el, context)); + } + }; } function initRepoIssueCommentEdit() { @@ -573,7 +582,7 @@ function initRepoIssueCommentEdit() { // Quote reply $(document).on('click', '.quote-reply', async (event) => { event.preventDefault(); - const quote = new MarkdownQuote('', preprocessFragment); + const quote = new MarkdownQuote('', preprocessFragment(event.target.getAttribute('data-context'))); let editorTextArea; if (event.target.classList.contains('quote-reply-diff')) {