From 5b75aebf1d3deaccc581cc2b53f7c9022db1182b Mon Sep 17 00:00:00 2001 From: anton-nagornyi Date: Sun, 14 Apr 2024 12:18:44 +0300 Subject: [PATCH] fix: prevent from encoding already encoded url links (#8) --- lib/telegramify.js | 3 ++- tests/convert.spec.js | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/telegramify.js b/lib/telegramify.js index 321e029..0f42778 100644 --- a/lib/telegramify.js +++ b/lib/telegramify.js @@ -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); diff --git a/tests/convert.spec.js b/tests/convert.spec.js index 6730b69..fc5a5e0 100644 --- a/tests/convert.spec.js +++ b/tests/convert.spec.js @@ -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';