Improve readability of encode/escape functionality

pull/393/head
Jeroen Akkerman 2 years ago
parent 0c2cf4cd51
commit d8eae5d74c

@ -844,13 +844,11 @@ function drawLink(editor) {
var options = editor.options; var options = editor.options;
var url = 'https://'; var url = 'https://';
if (options.promptURLs) { if (options.promptURLs) {
url = prompt(options.promptTexts.link, 'https://'); url = prompt(options.promptTexts.link, url);
if (!url) { if (!url) {
return false; return false;
} }
url = escapePromptURL(url);
url = encodeURI(url);
if (/[()]/.test(url)) url = escapePromptURL(url);
} }
_replaceSelection(cm, stat.link, options.insertTexts.link, url); _replaceSelection(cm, stat.link, options.insertTexts.link, url);
} }
@ -864,25 +862,21 @@ function drawImage(editor) {
var options = editor.options; var options = editor.options;
var url = 'https://'; var url = 'https://';
if (options.promptURLs) { if (options.promptURLs) {
url = prompt(options.promptTexts.image, 'https://'); url = prompt(options.promptTexts.image, url);
if (!url) { if (!url) {
return false; return false;
} }
url = escapePromptURL(url);
url = encodeURI(url);
if (/[()]/.test(url)) url = escapePromptURL(url);
} }
_replaceSelection(cm, stat.image, options.insertTexts.image, url); _replaceSelection(cm, stat.image, options.insertTexts.image, url);
} }
/** /**
* Escape URLs to prevent breaking up rendered Markdown links * Encode and escape URLs to prevent breaking up rendered Markdown links.
* @param url {string} The url of the link or image * @param url {string} The url of the link or image
*/ */
function escapePromptURL(url) { function escapePromptURL(url) {
url = url.replace(/\(/g,'\\(').replace(/\)/g,'\\)'); return encodeURI(url).replace(/([\\()])/g, '\\$1');
return url;
} }
/** /**

Loading…
Cancel
Save