mirror of
https://github.com/Ionaru/easy-markdown-editor
synced 2025-06-27 13:11:01 -06:00
Improve readability of encode/escape functionality
This commit is contained in:
parent
0c2cf4cd51
commit
d8eae5d74c
@ -844,13 +844,11 @@ function drawLink(editor) {
|
||||
var options = editor.options;
|
||||
var url = 'https://';
|
||||
if (options.promptURLs) {
|
||||
url = prompt(options.promptTexts.link, 'https://');
|
||||
url = prompt(options.promptTexts.link, url);
|
||||
if (!url) {
|
||||
return false;
|
||||
}
|
||||
|
||||
url = encodeURI(url);
|
||||
if (/[()]/.test(url)) url = escapePromptURL(url);
|
||||
url = escapePromptURL(url);
|
||||
}
|
||||
_replaceSelection(cm, stat.link, options.insertTexts.link, url);
|
||||
}
|
||||
@ -864,25 +862,21 @@ function drawImage(editor) {
|
||||
var options = editor.options;
|
||||
var url = 'https://';
|
||||
if (options.promptURLs) {
|
||||
url = prompt(options.promptTexts.image, 'https://');
|
||||
url = prompt(options.promptTexts.image, url);
|
||||
if (!url) {
|
||||
return false;
|
||||
}
|
||||
|
||||
url = encodeURI(url);
|
||||
if (/[()]/.test(url)) url = escapePromptURL(url);
|
||||
url = escapePromptURL(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
|
||||
*/
|
||||
function escapePromptURL(url) {
|
||||
url = url.replace(/\(/g,'\\(').replace(/\)/g,'\\)');
|
||||
|
||||
return url;
|
||||
return encodeURI(url).replace(/([\\()])/g, '\\$1');
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user