forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.js
64 lines (56 loc) · 1.54 KB
/
link.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { getAndRemoveConfig } from '../utils';
import { isAbsolutePath } from '../../router/util';
export const linkCompiler = ({
renderer,
router,
linkTarget,
linkRel,
compilerClass,
}) =>
(renderer.link = (href, title = '', text) => {
let attrs = [];
const { str, config } = getAndRemoveConfig(title);
linkTarget = config.target || linkTarget;
linkRel =
linkTarget === '_blank'
? compilerClass.config.externalLinkRel || 'noopener'
: '';
title = str;
if (
!isAbsolutePath(href) &&
!compilerClass._matchNotCompileLink(href) &&
!config.ignore
) {
if (href === compilerClass.config.homepage) {
href = 'README';
}
href = router.toURL(href, null, router.getCurrentPath());
} else {
if (!isAbsolutePath(href) && href.slice(0, 2) === './') {
href =
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
}
attrs.push(href.indexOf('mailto:') === 0 ? '' : `target="${linkTarget}"`);
attrs.push(
href.indexOf('mailto:') === 0
? ''
: linkRel !== ''
? ` rel="${linkRel}"`
: ''
);
}
if (config.disabled) {
attrs.push('disabled');
href = 'javascript:void(0)';
}
if (config.class) {
attrs.push(`class="${config.class}"`);
}
if (config.id) {
attrs.push(`id="${config.id}"`);
}
if (title) {
attrs.push(`title="${title}"`);
}
return `<a href="${href}" ${attrs.join(' ')}>${text}</a>`;
});