Skip to content

Commit ae20f70

Browse files
authored
Update codeit-autolinker.js
1 parent d053e0f commit ae20f70

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

lib/plugins/codeit-autolinker.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,29 @@
4848
//if (env.language === 'markdown') return;
4949
Prism.plugins.autolinker.processGrammar(env.grammar);
5050
});
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+
5259
Prism.hooks.add('wrap', function (env) {
5360

5461
//if (env.language === 'markdown') return;
5562

5663
if (env.type === 'url-reference') {
5764

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);
6766

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+
}
6974

7075
}
7176

@@ -83,13 +88,11 @@
8388
href = match[2];
8489
env.content = match[1];
8590
}
86-
87-
var isMac = navigator.platform.indexOf('Mac') > -1;
88-
91+
8992
env.attributes.href = href.replaceAll('\'','').replaceAll('"','').replaceAll('`','');
9093

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;
9396

9497
// silently catch any error thrown by decodeURIComponent
9598
try {

0 commit comments

Comments
 (0)