Skip to content

Commit

Permalink
handle multi line images (#918)
Browse files Browse the repository at this point in the history
fixes #904
  • Loading branch information
joaomoreno authored Dec 15, 2023
1 parent 160d0e0 commit 302f02c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ export class MarkdownProcessor extends BaseProcessor {
contents = contents.replace(markdownPathRegex, urlReplace);

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

if (!this.baseImagesUrl && isLinkRelative) {
Expand Down Expand Up @@ -840,7 +840,13 @@ export class MarkdownProcessor extends BaseProcessor {
}

const src = decodeURI(rawSrc);
const srcUrl = new url.URL(src);
let srcUrl: url.URL

try {
srcUrl = new url.URL(src);
} catch (err) {
throw new Error(`Invalid image source in ${this.name}: ${src}`);
}

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

(manifest.badges ?? []).forEach(badge => {
const decodedUrl = decodeURI(badge.url);
const srcUrl = new url.URL(decodedUrl);
let srcUrl: url.URL;

try {
srcUrl = new url.URL(decodedUrl);
} catch (err) {
throw new Error(`Badge URL is invalid: ${badge.url}`);
}

if (!/^https:$/i.test(srcUrl.protocol)) {
throw new Error(`Badge URLs must come from an HTTPS source: ${badge.url}`);
Expand Down
16 changes: 16 additions & 0 deletions src/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2896,6 +2896,22 @@ describe('MarkdownProcessor', () => {
await throws(() => processor.onFile(readme));
});

it('should allow img tags spanning across lines, issue #904', async () => {
const manifest = {
name: 'test',
publisher: 'mocha',
version: '0.0.1',
engines: Object.create(null),
repository: 'https://github.com/username/repository',
};
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."/>`;
const processor = new ReadmeProcessor(manifest, {});
const readme = { path: 'extension/readme.md', contents };

const file = await processor.onFile(readme);
assert.ok(file);
});

it('should catch an unchanged README.md', async () => {
const manifest = {
name: 'test',
Expand Down

0 comments on commit 302f02c

Please sign in to comment.