diff --git a/src/github/prComment.ts b/src/github/prComment.ts index d2ccba181e..eec7df13a0 100644 --- a/src/github/prComment.ts +++ b/src/github/prComment.ts @@ -215,7 +215,8 @@ export class TemporaryComment extends CommentBase { } const SUGGESTION_EXPRESSION = /```suggestion(\u0020*(\r\n|\n))((?[\s\S]*?)(\r\n|\n))?```/; -const IMG_EXPRESSION = /.+?)['"].*?>/g; +const IMG_EXPRESSION = /]*?\ssrc\s*=\s*['"](?[^'"]+?)['"][^>]*?>/gi; +const IMG_ALT_EXPRESSION = /\salt\s*=\s*(['"])(?[\s\S]*?)\1/i; const UUID_EXPRESSION = /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}/; export const COMMIT_SHA_EXPRESSION = /(? { - return `![image](${src})`; + return body.replace(IMG_EXPRESSION, (substring, _1, _2, _3, { src }) => { + const altMatch = substring.match(IMG_ALT_EXPRESSION); + const alt = (altMatch?.groups?.alt ?? '').replace(/[\r\n]+/g, ' ').trim(); + const safeAlt = alt.replace(/([\\\[\]`])/g, '\\$1'); + return `![${safeAlt || 'image'}](${src})`; }); }