Skip to content

Commit 8f5a10e

Browse files
Copilotalexr00
andauthored
Preserve <img> alt attribute when rewriting comment images
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 9bf0f48 commit 8f5a10e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/github/prComment.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ export class TemporaryComment extends CommentBase {
215215
}
216216

217217
const SUGGESTION_EXPRESSION = /```suggestion(\u0020*(\r\n|\n))((?<suggestion>[\s\S]*?)(\r\n|\n))?```/;
218-
const IMG_EXPRESSION = /<img .*src=['"](?<src>.+?)['"].*?>/g;
218+
const IMG_EXPRESSION = /<img .*?src=['"](?<src>.+?)['"].*?>/g;
219+
const IMG_ALT_EXPRESSION = /alt=['"](?<alt>.*?)['"]/;
219220
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}/;
220221
export const COMMIT_SHA_EXPRESSION = /(?<![`\/\w])([0-9a-f]{7})([0-9a-f]{33})?(?![`\/\w])/g;
221222

@@ -352,8 +353,10 @@ export class GHPRComment extends CommentBase {
352353
}
353354

354355
private replaceImg(body: string) {
355-
return body.replace(IMG_EXPRESSION, (_substring, _1, _2, _3, { src }) => {
356-
return `![image](${src})`;
356+
return body.replace(IMG_EXPRESSION, (substring, _1, _2, _3, { src }) => {
357+
const altMatch = substring.match(IMG_ALT_EXPRESSION);
358+
const alt = altMatch?.groups?.alt || 'image';
359+
return `![${alt}](${src})`;
357360
});
358361
}
359362

0 commit comments

Comments
 (0)