Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,31 @@ export const defaultTranslators: TranslatorConfigObject = {
const src = node.getAttribute('src') || '';
if (!src || (!options.keepDataImages && /^data:/i.test(src))) return { ignore: true };

let encodedSrc = '';
for (const chr of src) {
switch (chr) {
case '(':
encodedSrc += '%28';
break;
case ')':
encodedSrc += '%29';
break;
case '_':
encodedSrc += '%5F';
break;
case '*':
encodedSrc += '%2A';
break;
default:
encodedSrc += chr;
}
}

const alt = node.getAttribute('alt') || '';
const title = node.getAttribute('title') || '';

return {
content: `![${alt}](${src}${title && ` "${title}"`})`,
content: `![${alt}](${encodedSrc}${title && ` "${title}"`})`,
recurse: false
}
},
Expand Down
4 changes: 2 additions & 2 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,12 @@ text`);
instance.options.keepDataImages = true;
const resKeep = translate(`<img alt="normal" src="normal_img.jpg">
<img src="data:image/gif;base64,R0lGODlhEA"/>`);
expect(resKeep).toBe(`![normal](normal_img.jpg) ![](data:image/gif;base64,R0lGODlhEA)`);
expect(resKeep).toBe(`![normal](normal%5Fimg.jpg) ![](data:image/gif;base64,R0lGODlhEA)`);

instance.options.keepDataImages = false;
const resNoKeep = translate(`<img alt="normal" src="normal_img.jpg">
<img src="data:image/gif;base64,R0lGODlhEA"/>`);
expect(resNoKeep).toBe(`![normal](normal_img.jpg)`);
expect(resNoKeep).toBe(`![normal](normal%5Fimg.jpg)`);

instance.options.keepDataImages = originalKeepDataImages;
});
Expand Down
12 changes: 12 additions & 0 deletions test/special-cases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,16 @@ describe(`Special Cases`, () => {
expect(res).toBe('Paragraph 1\n\n \nParagraph 2');
});
});

describe(`handles parenthesis within an URL correctly`, () => {
test(`handles parenthesis within an anchor href`, () => {
const res = translate(`<a href="http://example.com/my-cool-article(2).png">My cool article</a>`);
expect(res).toBe("[My cool article](http://example.com/my-cool-article%282%29.png)");
});

test(`handles parenthesis within an img src`, () => {
const res = translate(`<img src="http://example.com/some-selfie(2).png"/>`);
expect(res).toBe("![](http://example.com/some-selfie%282%29.png)");
});
});
});