Skip to content

Commit a572495

Browse files
committed
Autolinker: Don't process all grammars on load, process each one in before-highlight. Should fix #760
1 parent 889cda5 commit a572495

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

plugins/autolinker/prism-autolinker.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,21 @@ var url = /\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~/.:#=?&]+/,
1414
// Tokens that may contain URLs and emails
1515
candidates = ['comment', 'url', 'attr-value', 'string'];
1616

17-
for (var language in Prism.languages) {
18-
var tokens = Prism.languages[language];
19-
20-
Prism.languages.DFS(tokens, function (key, def, type) {
17+
Prism.hooks.add('before-highlight', function(env) {
18+
// Abort if grammar has already been processed
19+
if (!env.grammar || env.grammar['url-link']) {
20+
return;
21+
}
22+
Prism.languages.DFS(env.grammar, function (key, def, type) {
2123
if (candidates.indexOf(type) > -1 && Prism.util.type(def) !== 'Array') {
2224
if (!def.pattern) {
2325
def = this[key] = {
2426
pattern: def
2527
};
2628
}
27-
29+
2830
def.inside = def.inside || {};
29-
31+
3032
if (type == 'comment') {
3133
def.inside['md-link'] = linkMd;
3234
}
@@ -36,14 +38,13 @@ for (var language in Prism.languages) {
3638
else {
3739
def.inside['url-link'] = url;
3840
}
39-
41+
4042
def.inside['email-link'] = email;
4143
}
4244
});
43-
44-
tokens['url-link'] = url;
45-
tokens['email-link'] = email;
46-
}
45+
env.grammar['url-link'] = url;
46+
env.grammar['email-link'] = email;
47+
});
4748

4849
Prism.hooks.add('wrap', function(env) {
4950
if (/-link$/.test(env.type)) {
@@ -66,4 +67,4 @@ Prism.hooks.add('wrap', function(env) {
6667
}
6768
});
6869

69-
})();
70+
})();

plugins/autolinker/prism-autolinker.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)