Fix Autolinker url-decoding all tokens (#1723) This PR fixes #1721. The problem was that the url-decoding introduced in #1173 was applied to all tokens.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
diff --git a/plugins/autolinker/prism-autolinker.js b/plugins/autolinker/prism-autolinker.js
index 3913c98..a35115b 100644
--- a/plugins/autolinker/prism-autolinker.js
+++ b/plugins/autolinker/prism-autolinker.js
@@ -10,7 +10,7 @@ if (
var url = /\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~/.:=&]+(?:\?[\w\-+%~/.:#=?&!$'()*,;]*)?(?:#[\w\-+%~/.:#=?&!$'()*,;]*)?/,
email = /\b\S+@[\w.]+[a-z]{2}/,
linkMd = /\[([^\]]+)]\(([^)]+)\)/,
-
+
// Tokens that may contain URLs and emails
candidates = ['comment', 'url', 'attr-value', 'string'];
@@ -55,27 +55,27 @@ Prism.hooks.add('before-highlight', function(env) {
Prism.hooks.add('wrap', function(env) {
if (/-link$/.test(env.type)) {
env.tag = 'a';
-
+
var href = env.content;
-
+
if (env.type == 'email-link' && href.indexOf('mailto:') != 0) {
href = 'mailto:' + href;
}
else if (env.type == 'md-link') {
// Markdown
var match = env.content.match(linkMd);
-
+
href = match[2];
env.content = match[1];
}
-
+
env.attributes.href = href;
- }
- // Silently catch any error thrown by decodeURIComponent (#1186)
- try {
- env.content = decodeURIComponent(env.content);
- } catch(e) {}
+ // Silently catch any error thrown by decodeURIComponent (#1186)
+ try {
+ env.content = decodeURIComponent(env.content);
+ } catch(e) {}
+ }
});
})();
diff --git a/plugins/autolinker/prism-autolinker.min.js b/plugins/autolinker/prism-autolinker.min.js
index 9fdced9..d202038 100644
--- a/plugins/autolinker/prism-autolinker.min.js
+++ b/plugins/autolinker/prism-autolinker.min.js
@@ -1 +1 @@
-!function(){if(("undefined"==typeof self||self.Prism)&&("undefined"==typeof global||global.Prism)){var i=/\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~\/.:=&]+(?:\?[\w\-+%~\/.:#=?&!$'()*,;]*)?(?:#[\w\-+%~\/.:#=?&!$'()*,;]*)?/,n=/\b\S+@[\w.]+[a-z]{2}/,e=/\[([^\]]+)]\(([^)]+)\)/,t=["comment","url","attr-value","string"];Prism.plugins.autolinker={processGrammar:function(r){r&&!r["url-link"]&&(Prism.languages.DFS(r,function(r,a,l){t.indexOf(l)>-1&&"Array"!==Prism.util.type(a)&&(a.pattern||(a=this[r]={pattern:a}),a.inside=a.inside||{},"comment"==l&&(a.inside["md-link"]=e),"attr-value"==l?Prism.languages.insertBefore("inside","punctuation",{"url-link":i},a):a.inside["url-link"]=i,a.inside["email-link"]=n)}),r["url-link"]=i,r["email-link"]=n)}},Prism.hooks.add("before-highlight",function(i){Prism.plugins.autolinker.processGrammar(i.grammar)}),Prism.hooks.add("wrap",function(i){if(/-link$/.test(i.type)){i.tag="a";var n=i.content;if("email-link"==i.type&&0!=n.indexOf("mailto:"))n="mailto:"+n;else if("md-link"==i.type){var t=i.content.match(e);n=t[2],i.content=t[1]}i.attributes.href=n}try{i.content=decodeURIComponent(i.content)}catch(r){}})}}();
\ No newline at end of file
+!function(){if(("undefined"==typeof self||self.Prism)&&("undefined"==typeof global||global.Prism)){var i=/\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~\/.:=&]+(?:\?[\w\-+%~\/.:#=?&!$'()*,;]*)?(?:#[\w\-+%~\/.:#=?&!$'()*,;]*)?/,n=/\b\S+@[\w.]+[a-z]{2}/,e=/\[([^\]]+)]\(([^)]+)\)/,t=["comment","url","attr-value","string"];Prism.plugins.autolinker={processGrammar:function(r){r&&!r["url-link"]&&(Prism.languages.DFS(r,function(r,a,l){t.indexOf(l)>-1&&"Array"!==Prism.util.type(a)&&(a.pattern||(a=this[r]={pattern:a}),a.inside=a.inside||{},"comment"==l&&(a.inside["md-link"]=e),"attr-value"==l?Prism.languages.insertBefore("inside","punctuation",{"url-link":i},a):a.inside["url-link"]=i,a.inside["email-link"]=n)}),r["url-link"]=i,r["email-link"]=n)}},Prism.hooks.add("before-highlight",function(i){Prism.plugins.autolinker.processGrammar(i.grammar)}),Prism.hooks.add("wrap",function(i){if(/-link$/.test(i.type)){i.tag="a";var n=i.content;if("email-link"==i.type&&0!=n.indexOf("mailto:"))n="mailto:"+n;else if("md-link"==i.type){var t=i.content.match(e);n=t[2],i.content=t[1]}i.attributes.href=n;try{i.content=decodeURIComponent(i.content)}catch(r){}}})}}();
\ No newline at end of file