[d24f17c] | 1 | (function () {
|
---|
| 2 |
|
---|
| 3 | if (typeof Prism === 'undefined') {
|
---|
| 4 | return;
|
---|
| 5 | }
|
---|
| 6 |
|
---|
| 7 | var url = /\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~/.:=&!$'()*,;@]+(?:\?[\w\-+%~/.:=?&!$'()*,;@]*)?(?:#[\w\-+%~/.:#=?&!$'()*,;@]*)?/;
|
---|
| 8 | var email = /\b\S+@[\w.]+[a-z]{2}/;
|
---|
| 9 | var linkMd = /\[([^\]]+)\]\(([^)]+)\)/;
|
---|
| 10 |
|
---|
| 11 | // Tokens that may contain URLs and emails
|
---|
| 12 | var candidates = ['comment', 'url', 'attr-value', 'string'];
|
---|
| 13 |
|
---|
| 14 | Prism.plugins.autolinker = {
|
---|
| 15 | processGrammar: function (grammar) {
|
---|
| 16 | // Abort if grammar has already been processed
|
---|
| 17 | if (!grammar || grammar['url-link']) {
|
---|
| 18 | return;
|
---|
| 19 | }
|
---|
| 20 | Prism.languages.DFS(grammar, function (key, def, type) {
|
---|
| 21 | if (candidates.indexOf(type) > -1 && !Array.isArray(def)) {
|
---|
| 22 | if (!def.pattern) {
|
---|
| 23 | def = this[key] = {
|
---|
| 24 | pattern: def
|
---|
| 25 | };
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | def.inside = def.inside || {};
|
---|
| 29 |
|
---|
| 30 | if (type == 'comment') {
|
---|
| 31 | def.inside['md-link'] = linkMd;
|
---|
| 32 | }
|
---|
| 33 | if (type == 'attr-value') {
|
---|
| 34 | Prism.languages.insertBefore('inside', 'punctuation', { 'url-link': url }, def);
|
---|
| 35 | } else {
|
---|
| 36 | def.inside['url-link'] = url;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | def.inside['email-link'] = email;
|
---|
| 40 | }
|
---|
| 41 | });
|
---|
| 42 | grammar['url-link'] = url;
|
---|
| 43 | grammar['email-link'] = email;
|
---|
| 44 | }
|
---|
| 45 | };
|
---|
| 46 |
|
---|
| 47 | Prism.hooks.add('before-highlight', function (env) {
|
---|
| 48 | Prism.plugins.autolinker.processGrammar(env.grammar);
|
---|
| 49 | });
|
---|
| 50 |
|
---|
| 51 | Prism.hooks.add('wrap', function (env) {
|
---|
| 52 | if (/-link$/.test(env.type)) {
|
---|
| 53 | env.tag = 'a';
|
---|
| 54 |
|
---|
| 55 | var href = env.content;
|
---|
| 56 |
|
---|
| 57 | if (env.type == 'email-link' && href.indexOf('mailto:') != 0) {
|
---|
| 58 | href = 'mailto:' + href;
|
---|
| 59 | } else if (env.type == 'md-link') {
|
---|
| 60 | // Markdown
|
---|
| 61 | var match = env.content.match(linkMd);
|
---|
| 62 |
|
---|
| 63 | href = match[2];
|
---|
| 64 | env.content = match[1];
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | env.attributes.href = href;
|
---|
| 68 |
|
---|
| 69 | // Silently catch any error thrown by decodeURIComponent (#1186)
|
---|
| 70 | try {
|
---|
| 71 | env.content = decodeURIComponent(env.content);
|
---|
| 72 | } catch (e) { /* noop */ }
|
---|
| 73 | }
|
---|
| 74 | });
|
---|
| 75 |
|
---|
| 76 | }());
|
---|