Skip to content

Commit

Permalink
fix(Link): ascape parentheses in link url (#244)
Browse files Browse the repository at this point in the history
Markdown-it incorrectly parses links with unbalanced parentheses in URLs.
This is fixed by escaping the parentheses.
  • Loading branch information
d3m1d0v authored and makhnatkin committed May 28, 2024
1 parent 8b1aa5c commit 6c8db94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/extensions/markdown/Link/Link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ describe('Link extension', () => {
'https://example.com/+_file/#~anchor',
);
});

it('should escape parentheses in url', () =>
same(
'[parentheses](https://example.com/example=?qwe\\(asd)',
doc(p(a({[LinkAttr.Href]: 'https://example.com/example=?qwe(asd'}, 'parentheses'))),
));

it('should escape parentheses in url', () =>
same(
'[parentheses2](https://example.com/example=?qwe\\(asd\\)\\))',
doc(p(a({[LinkAttr.Href]: 'https://example.com/example=?qwe(asd))'}, 'parentheses2'))),
));
});
6 changes: 5 additions & 1 deletion src/extensions/markdown/Link/LinkSpecs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const LinkSpecs: ExtensionAuto = (builder) => {
state.isAutolink = undefined;
return (
'](' +
mark.attrs[LinkAttr.Href] +
escapeParenthesesInUrl(mark.attrs[LinkAttr.Href]) +
(mark.attrs[LinkAttr.Title]
? ' ' + state.quote(mark.attrs[LinkAttr.Title])
: '') +
Expand Down Expand Up @@ -97,3 +97,7 @@ function isPlainURL(link: Mark, parent: Fragment, index: number, side: number) {

return !link.isInSet(next.marks);
}

function escapeParenthesesInUrl(url: string): string {
return url.replaceAll(/\(|\)/g, (p) => '\\' + p);
}

0 comments on commit 6c8db94

Please sign in to comment.