Skip to content

Commit

Permalink
fix: prevent from encoding already encoded url links (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-nagornyi authored Apr 14, 2024
1 parent f0ac6cb commit 5b75aeb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/telegramify.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ const createHandlers = (definitions, unsupportedTagsStrategy) => ({
link: (node, _parent, context) => {
const exit = context.enter('link');
const text = phrasing(node, context, {before: '|', after: '>'}) || escapeSymbols(node.title);
const url = encodeURI(node.url);
const isUrlEncoded = decodeURI(node.url) !== node.url;
const url = isUrlEncoded ? node.url : encodeURI(node.url);
exit();

if (!isURL(url)) return escapeSymbols(text) || escapeSymbols(url);
Expand Down
6 changes: 6 additions & 0 deletions tests/convert.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ describe('Test convert method', () => {
expect(convert(markdown)).toBe(tgMarkdown);
});

it('Link with invalid URL', () => {
const markdown = '[test](/atlassian)';
const tgMarkdown = 'test\n';
expect(convert(markdown)).toBe(tgMarkdown);
});

it('Link with parentheses', () => {
const markdown = '[Atlassian](http://atlas()sian.com)';
const tgMarkdown = '[Atlassian](http://atlas\\(\\)sian.com)\n';
Expand Down

0 comments on commit 5b75aeb

Please sign in to comment.