Skip to content

Commit 302f02c

Browse files
authored
handle multi line images (#918)
fixes #904
1 parent 160d0e0 commit 302f02c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/package.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ export class MarkdownProcessor extends BaseProcessor {
772772
contents = contents.replace(markdownPathRegex, urlReplace);
773773

774774
// Replace <img> links with urls
775-
contents = contents.replace(/<img.+?src=["']([/.\w\s#-]+)['"].*?>/g, (all, link) => {
775+
contents = contents.replace(/<img[^>]+src=["']([/.\w\s#-]+)['"][^>]*>/gm, (all, link) => {
776776
const isLinkRelative = !/^\w+:\/\//.test(link) && link[0] !== '#';
777777

778778
if (!this.baseImagesUrl && isLinkRelative) {
@@ -840,7 +840,13 @@ export class MarkdownProcessor extends BaseProcessor {
840840
}
841841

842842
const src = decodeURI(rawSrc);
843-
const srcUrl = new url.URL(src);
843+
let srcUrl: url.URL
844+
845+
try {
846+
srcUrl = new url.URL(src);
847+
} catch (err) {
848+
throw new Error(`Invalid image source in ${this.name}: ${src}`);
849+
}
844850

845851
if (/^data:$/i.test(srcUrl.protocol) && /^image$/i.test(srcUrl.host) && /\/svg/i.test(srcUrl.pathname)) {
846852
throw new Error(`SVG data URLs are not allowed in ${this.name}: ${src}`);
@@ -1273,7 +1279,13 @@ export function validateManifest(manifest: Manifest): Manifest {
12731279

12741280
(manifest.badges ?? []).forEach(badge => {
12751281
const decodedUrl = decodeURI(badge.url);
1276-
const srcUrl = new url.URL(decodedUrl);
1282+
let srcUrl: url.URL;
1283+
1284+
try {
1285+
srcUrl = new url.URL(decodedUrl);
1286+
} catch (err) {
1287+
throw new Error(`Badge URL is invalid: ${badge.url}`);
1288+
}
12771289

12781290
if (!/^https:$/i.test(srcUrl.protocol)) {
12791291
throw new Error(`Badge URLs must come from an HTTPS source: ${badge.url}`);

src/test/package.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -2896,6 +2896,22 @@ describe('MarkdownProcessor', () => {
28962896
await throws(() => processor.onFile(readme));
28972897
});
28982898

2899+
it('should allow img tags spanning across lines, issue #904', async () => {
2900+
const manifest = {
2901+
name: 'test',
2902+
publisher: 'mocha',
2903+
version: '0.0.1',
2904+
engines: Object.create(null),
2905+
repository: 'https://github.com/username/repository',
2906+
};
2907+
const contents = `<img src="img/screenshots/demo.webp" width="556" height="482"\nalt="recording of exploring view opened from the command 'Snippets Ranger: Show me that dur Range, Partner'. An entry of 'Markdown snippets' from the table of contents is selected and clicked, it takes the user down to the table with the snippets displayed for that extension."/>`;
2908+
const processor = new ReadmeProcessor(manifest, {});
2909+
const readme = { path: 'extension/readme.md', contents };
2910+
2911+
const file = await processor.onFile(readme);
2912+
assert.ok(file);
2913+
});
2914+
28992915
it('should catch an unchanged README.md', async () => {
29002916
const manifest = {
29012917
name: 'test',

0 commit comments

Comments
 (0)