|
4 | 4 | return; |
5 | 5 | } |
6 | 6 |
|
7 | | - var url = /\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~/.:=&@]+(?:\?[\w\-+%~/.:=?&!$'()*,;@]*)?(?:#[\w\-+%~/.:#=?&!$'()*,;@]*)?/; |
| 7 | + var url = /\b([a-z]{3,7}:\/\/|tel:)[\w\-+%~/.:=&!$'()*,;@]+(?:\?[\w\-+%~/.:=?&!$'()*,;@]*)?(?:#[\w\-+%~/.:#=?&!$'()*,;@]*)?/; |
8 | 8 | var email = /\b\S+@[\w.]+[a-z]{2}/; |
9 | 9 | var linkMd = /\[([^\]]+)\]\(([^)]+)\)/; |
10 | 10 |
|
|
13 | 13 |
|
14 | 14 | Prism.plugins.autolinker = { |
15 | 15 | processGrammar: function (grammar) { |
16 | | - // Abort if grammar has already been processed |
| 16 | + // abort if grammar has already been processed |
17 | 17 | if (!grammar || grammar['url-link']) { |
18 | 18 | return; |
19 | 19 | } |
|
26 | 26 | } |
27 | 27 |
|
28 | 28 | def.inside = def.inside || {}; |
29 | | - |
| 29 | + |
30 | 30 | if (type == 'comment') { |
31 | 31 | def.inside['md-link'] = linkMd; |
32 | 32 | } |
|
45 | 45 | }; |
46 | 46 |
|
47 | 47 | Prism.hooks.add('before-tokenize', function (env) { |
48 | | - if (env.language !== 'markdown') { |
49 | | - Prism.plugins.autolinker.processGrammar(env.grammar); |
50 | | - } |
| 48 | + if (env.language === 'markdown') return; |
| 49 | + Prism.plugins.autolinker.processGrammar(env.grammar); |
51 | 50 | }); |
| 51 | + |
| 52 | + |
| 53 | + const onClickEvent = 'if ((event.ctrlKey || event.metaKey) && event.shiftKey) { event.preventDefault(); window.open(this.href, \'_blank\') }'; |
| 54 | + |
| 55 | + const isMac = navigator.platform.indexOf('Mac') > -1; |
| 56 | + |
| 57 | + const linkTitle = isMac ? '⌘ + ⇧ + click to open link' : 'Ctrl + Shift + click to open link'; |
| 58 | + |
| 59 | + Prism.hooks.add('wrap', function (env) { |
| 60 | + |
| 61 | + if (env.language === 'markdown') return; |
| 62 | + |
| 63 | + if (env.language === 'markdown' && |
| 64 | + env.type === 'url-reference') { |
| 65 | + |
| 66 | + let matches = env.content.match(url); |
| 67 | + |
| 68 | + if (matches && matches[0]) { |
| 69 | + |
| 70 | + matches[0] = matches[0].replaceAll('\'','').replaceAll('"','').replaceAll('`',''); |
| 71 | + |
| 72 | + env.content = env.content.replace(matches[0], '<a class="token url-link" title="' + linkTitle + '" onclick="' + onClickEvent + '" href="'+ matches[0] + '">' + matches[0] + '</a>'); |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + if (/-link$/.test(env.type)) { |
| 79 | + env.tag = 'a'; |
| 80 | + |
| 81 | + var href = env.content; |
| 82 | + |
| 83 | + if (env.type == 'email-link' && href.indexOf('mailto:') != 0) { |
| 84 | + href = 'mailto:' + href; |
| 85 | + } else if (env.type == 'md-link') { |
| 86 | + // markdown |
| 87 | + var match = env.content.match(linkMd); |
| 88 | + |
| 89 | + href = match[2]; |
| 90 | + env.content = match[1]; |
| 91 | + } |
| 92 | + |
| 93 | + env.attributes.href = href.replaceAll('\'','').replaceAll('"','').replaceAll('`',''); |
52 | 94 |
|
53 | | - Prism.hooks.add('wrap', function (env) { |
54 | | - if (env.language !== 'markdown') { |
55 | | - if (/-link$/.test(env.type)) { |
56 | | - env.tag = 'a'; |
57 | | - |
58 | | - var href = env.content; |
59 | | - |
60 | | - if (env.type == 'email-link' && href.indexOf('mailto:') != 0) { |
61 | | - href = 'mailto:' + href; |
62 | | - } else if (env.type == 'md-link') { |
63 | | - // Markdown |
64 | | - var match = env.content.match(linkMd); |
65 | | - |
66 | | - href = match[2]; |
67 | | - env.content = match[1]; |
68 | | - } |
69 | | - |
70 | | - var isMac = navigator.platform.indexOf('Mac') > -1; |
71 | | - |
72 | | - env.attributes.href = href.replaceAll('\'','').replaceAll('"','').replaceAll('`',''); |
73 | | - |
74 | | - env.attributes.onclick = 'if ((event.ctrlKey || event.metaKey) && event.shiftKey) { event.preventDefault(); window.open(this.href, "_blank") }'; |
75 | | - env.attributes.title = isMac ? '⌘ + ⇧ + click to open link' : 'Ctrl + Shift + click to open link'; |
| 95 | + env.attributes.onclick = onClickEvent; |
| 96 | + env.attributes.title = linkTitle; |
76 | 97 |
|
77 | | - // silently catch any error thrown by decodeURIComponent |
78 | | - try { |
79 | | - env.content = decodeURIComponent(env.content); |
80 | | - } catch (e) {} |
81 | | - } |
| 98 | + // silently catch any error thrown by decodeURIComponent |
| 99 | + try { |
| 100 | + env.content = decodeURIComponent(env.content); |
| 101 | + } catch (e) {} |
82 | 102 | } |
83 | 103 | }); |
84 | 104 |
|
|
0 commit comments