forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlink.js
More file actions
72 lines (62 loc) · 1.84 KB
/
link.js
File metadata and controls
72 lines (62 loc) · 1.84 KB
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
65
66
67
68
69
70
71
72
import { getAndRemoveConfig } from '../utils.js';
import { isAbsolutePath } from '../../router/util.js';
export const linkCompiler = ({
renderer,
router,
linkTarget,
linkRel,
compiler,
}) =>
(renderer.link = function ({ href, title = '', tokens }) {
const attrs = [];
const text = this.parser.parseInline(tokens) || '';
const { str, config } = getAndRemoveConfig(title);
const isAbsolute = isAbsolutePath(href);
const isNotCompilable = compiler._matchNotCompileLink(href);
const isMailto = href.startsWith('mailto:');
linkTarget = config.target || linkTarget;
linkRel =
linkTarget === '_blank'
? compiler.config.externalLinkRel || 'noopener'
: '';
title = str;
if (!isAbsolute && !isNotCompilable && !config.ignore) {
if (href === compiler.config.homepage) {
href = 'README';
}
href = router.toURL(href, null, router.getCurrentPath());
if (config.target && !isMailto) {
attrs.push(`target="${linkTarget}"`);
}
} else {
if (!isAbsolute && href.startsWith('./')) {
href = router
.toURL(href, null, router.getCurrentPath())
.replace(/^#\//, '/');
}
if (!isMailto) {
attrs.push(`target="${linkTarget}"`);
if (linkRel !== '') {
attrs.push(`rel="${linkRel}"`);
}
}
}
if (config.disabled) {
attrs.push('disabled');
href = 'javascript:void(0)';
}
if (config.class) {
let classes = config.class;
if (Array.isArray(config.class)) {
classes = config.class.join(' ');
}
attrs.push(`class="${classes}"`);
}
if (config.id) {
attrs.push(`id="${config.id}"`);
}
if (title) {
attrs.push(`title="${title}"`);
}
return /* html */ `<a href="${href}" ${attrs.join(' ')}>${text}</a>`;
});