Skip to content

Commit

Permalink
Autolinker: Don't process all grammars on load, process each one in b…
Browse files Browse the repository at this point in the history
…efore-highlight. Should fix #760
  • Loading branch information
Golmote committed Sep 7, 2015
1 parent 889cda5 commit a572495
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 13 additions & 12 deletions plugins/autolinker/prism-autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)) {
Expand All @@ -66,4 +67,4 @@ Prism.hooks.add('wrap', function(env) {
}
});

})();
})();
2 changes: 1 addition & 1 deletion plugins/autolinker/prism-autolinker.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a572495

Please sign in to comment.