|
48 | 48 | //if (env.language === 'markdown') return; |
49 | 49 | Prism.plugins.autolinker.processGrammar(env.grammar); |
50 | 50 | }); |
51 | | - |
| 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 | + |
52 | 59 | Prism.hooks.add('wrap', function (env) { |
53 | 60 |
|
54 | 61 | //if (env.language === 'markdown') return; |
55 | 62 |
|
56 | 63 | if (env.type === 'url-reference') { |
57 | 64 |
|
58 | | - console.log(env.content.match(url)); |
59 | | - |
60 | | - let splitContent = env.content.split(' '); |
61 | | - |
62 | | - let href = splitContent[splitContent.length - 1]; |
63 | | - |
64 | | - console.log(href); |
65 | | - |
66 | | - splitContent[splitContent.length - 1] = '<a class="token url-link" href="' + href + '">' + href + '</a>'; |
| 65 | + let matches = env.content.match(url); |
67 | 66 |
|
68 | | - env.content = splitContent.join(' '); |
| 67 | + if (matches && matches[0]) { |
| 68 | + |
| 69 | + matches[0] = matches[0].replaceAll('\'','').replaceAll('"','').replaceAll('`',''); |
| 70 | + |
| 71 | + env.content.replace(matches[0], '<a class="token url-link" onclick="' + onClickEvent + '" href="'+ matches[0] + '">' + matches[0] + '</a>'); |
| 72 | + |
| 73 | + } |
69 | 74 |
|
70 | 75 | } |
71 | 76 |
|
|
83 | 88 | href = match[2]; |
84 | 89 | env.content = match[1]; |
85 | 90 | } |
86 | | - |
87 | | - var isMac = navigator.platform.indexOf('Mac') > -1; |
88 | | - |
| 91 | + |
89 | 92 | env.attributes.href = href.replaceAll('\'','').replaceAll('"','').replaceAll('`',''); |
90 | 93 |
|
91 | | - env.attributes.onclick = 'if ((event.ctrlKey || event.metaKey) && event.shiftKey) { event.preventDefault(); window.open(this.href, "_blank") }'; |
92 | | - env.attributes.title = isMac ? '⌘ + ⇧ + click to open link' : 'Ctrl + Shift + click to open link'; |
| 94 | + env.attributes.onclick = onClickEvent; |
| 95 | + env.attributes.title = linkTitle; |
93 | 96 |
|
94 | 97 | // silently catch any error thrown by decodeURIComponent |
95 | 98 | try { |
|
0 commit comments