From 5d0294f5f5196070479e64bacdbf2d7ea9abf852 Mon Sep 17 00:00:00 2001 From: Zignature Date: Sat, 15 Jan 2022 13:20:28 +0100 Subject: [PATCH] Fix issue #373 problems in urls with special characters Added function to escape URLs entered via JS prompt. It's a partial fix because people cannot be stopped entering URLs manually and forgetting to escape them. --- src/js/easymde.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/js/easymde.js b/src/js/easymde.js index cece3b0..72f7254 100644 --- a/src/js/easymde.js +++ b/src/js/easymde.js @@ -848,6 +848,8 @@ function drawLink(editor) { if (!url) { return false; } + + if (/[()<>]/.test(url)) url = escapeUrl(url); } _replaceSelection(cm, stat.link, options.insertTexts.link, url); } @@ -865,10 +867,26 @@ function drawImage(editor) { if (!url) { return false; } + + if (/[()<>]/.test(url)) url = escapeUrl(url); } _replaceSelection(cm, stat.image, options.insertTexts.image, url); } +/** + * Escape URLs to prevent breaking up rendered Markdown links + * @param url {string} The url of the link or image + */ +function escapeUrl(url) { + + url = url.replace(/\(/g,'\\(') + .replace(/\)/g,'\\)') + .replace(//g,'\\>'); + + return url; +} + /** * Action for opening the browse-file window to upload an image to a server. * @param editor {EasyMDE} The EasyMDE object