From a57249500cd0da87ce56b7eacd97e7bc9ba8810f Mon Sep 17 00:00:00 2001 From: Golmote Date: Mon, 7 Sep 2015 19:15:18 +0200 Subject: [PATCH] Autolinker: Don't process all grammars on load, process each one in before-highlight. Should fix #760 --- plugins/autolinker/prism-autolinker.js | 25 +++++++++++----------- plugins/autolinker/prism-autolinker.min.js | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/plugins/autolinker/prism-autolinker.js b/plugins/autolinker/prism-autolinker.js index ae407e06d9..e56303f8ed 100644 --- a/plugins/autolinker/prism-autolinker.js +++ b/plugins/autolinker/prism-autolinker.js @@ -14,19 +14,21 @@ var url = /\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~/.:#=?&]+/, // Tokens that may contain URLs and emails candidates = ['comment', 'url', 'attr-value', 'string']; -for (var language in Prism.languages) { - var tokens = Prism.languages[language]; - - Prism.languages.DFS(tokens, function (key, def, type) { +Prism.hooks.add('before-highlight', function(env) { + // Abort if grammar has already been processed + if (!env.grammar || env.grammar['url-link']) { + return; + } + Prism.languages.DFS(env.grammar, function (key, def, type) { if (candidates.indexOf(type) > -1 && Prism.util.type(def) !== 'Array') { if (!def.pattern) { def = this[key] = { pattern: def }; } - + def.inside = def.inside || {}; - + if (type == 'comment') { def.inside['md-link'] = linkMd; } @@ -36,14 +38,13 @@ for (var language in Prism.languages) { else { def.inside['url-link'] = url; } - + def.inside['email-link'] = email; } }); - - tokens['url-link'] = url; - tokens['email-link'] = email; -} + env.grammar['url-link'] = url; + env.grammar['email-link'] = email; +}); Prism.hooks.add('wrap', function(env) { if (/-link$/.test(env.type)) { @@ -66,4 +67,4 @@ Prism.hooks.add('wrap', function(env) { } }); -})(); +})(); \ No newline at end of file diff --git a/plugins/autolinker/prism-autolinker.min.js b/plugins/autolinker/prism-autolinker.min.js index 7869057c2b..60e7fd4444 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\-+%~/.:#=?&]+/,n=/\b\S+@[\w.]+[a-z]{2}/,e=/\[([^\]]+)]\(([^)]+)\)/,t=["comment","url","attr-value","string"];for(var a in Prism.languages){var l=Prism.languages[a];Prism.languages.DFS(l,function(a,l,r){t.indexOf(r)>-1&&"Array"!==Prism.util.type(l)&&(l.pattern||(l=this[a]={pattern:l}),l.inside=l.inside||{},"comment"==r&&(l.inside["md-link"]=e),"attr-value"==r?Prism.languages.insertBefore("inside","punctuation",{"url-link":i},l):l.inside["url-link"]=i,l.inside["email-link"]=n)}),l["url-link"]=i,l["email-link"]=n}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}})}}(); \ 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\-+%~/.:#=?&]+/,n=/\b\S+@[\w.]+[a-z]{2}/,e=/\[([^\]]+)]\(([^)]+)\)/,t=["comment","url","attr-value","string"];Prism.hooks.add("before-highlight",function(a){a.grammar&&!a.grammar["url-link"]&&(Prism.languages.DFS(a.grammar,function(a,r,l){t.indexOf(l)>-1&&"Array"!==Prism.util.type(r)&&(r.pattern||(r=this[a]={pattern:r}),r.inside=r.inside||{},"comment"==l&&(r.inside["md-link"]=e),"attr-value"==l?Prism.languages.insertBefore("inside","punctuation",{"url-link":i},r):r.inside["url-link"]=i,r.inside["email-link"]=n)}),a.grammar["url-link"]=i,a.grammar["email-link"]=n)}),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}})}}(); \ No newline at end of file