@vuepress/markdown 2.0.0-beta.28 → 2.0.0-beta.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -18,12 +18,6 @@ export interface LinksPluginOptions {
|
|
|
18
18
|
* ```
|
|
19
19
|
*/
|
|
20
20
|
externalAttrs?: Record<string, string>;
|
|
21
|
-
/**
|
|
22
|
-
* Whether to render an outbound icon to external links
|
|
23
|
-
*
|
|
24
|
-
* @default true
|
|
25
|
-
*/
|
|
26
|
-
externalIcon?: boolean;
|
|
27
21
|
}
|
|
28
22
|
/**
|
|
29
23
|
* Process links in markdown file
|
|
@@ -10,7 +10,6 @@ const resolvePaths_1 = require("./resolvePaths");
|
|
|
10
10
|
* - external links: add extra attrs and external icon
|
|
11
11
|
*/
|
|
12
12
|
const linksPlugin = (md, options = {}) => {
|
|
13
|
-
var _a;
|
|
14
13
|
// tag of internal links
|
|
15
14
|
const internalTag = options.internalTag || 'RouterLink';
|
|
16
15
|
// attrs that going to be added to external links
|
|
@@ -19,12 +18,8 @@ const linksPlugin = (md, options = {}) => {
|
|
|
19
18
|
rel: 'noopener noreferrer',
|
|
20
19
|
...options.externalAttrs,
|
|
21
20
|
};
|
|
22
|
-
// external icon
|
|
23
|
-
const externalIcon = (_a = options.externalIcon) !== null && _a !== void 0 ? _a : true;
|
|
24
21
|
let hasOpenInternalLink = false;
|
|
25
|
-
let hasOpenExternalLink = false;
|
|
26
22
|
const handleLinkOpen = (tokens, idx, env) => {
|
|
27
|
-
var _a;
|
|
28
23
|
// get current token
|
|
29
24
|
const token = tokens[idx];
|
|
30
25
|
// get `href` attr index
|
|
@@ -38,78 +33,64 @@ const linksPlugin = (md, options = {}) => {
|
|
|
38
33
|
const hrefAttr = token.attrs[hrefIndex];
|
|
39
34
|
const hrefLink = hrefAttr[1];
|
|
40
35
|
// get `base` and `filePathRelative` from `env`
|
|
41
|
-
const { base = '/', filePathRelative = null
|
|
36
|
+
const { base = '/', filePathRelative = null } = env;
|
|
42
37
|
// check if a link is an external link
|
|
43
38
|
if ((0, shared_1.isLinkExternal)(hrefLink, base)) {
|
|
44
39
|
// set `externalAttrs` to current token
|
|
45
40
|
Object.entries(externalAttrs).forEach(([key, val]) => token.attrSet(key, val));
|
|
46
|
-
// check if we should render external icon
|
|
47
|
-
if (
|
|
48
|
-
// frontmatter should override plugin option
|
|
49
|
-
((_a = frontmatter.externalIcon) !== null && _a !== void 0 ? _a : externalIcon) &&
|
|
50
|
-
// only when an external link has `target="_blank"`
|
|
51
|
-
// should we render external icon
|
|
52
|
-
externalAttrs.target === '_blank') {
|
|
53
|
-
hasOpenExternalLink = true;
|
|
54
|
-
}
|
|
55
41
|
return;
|
|
56
42
|
}
|
|
57
43
|
// check if a link is an internal link
|
|
58
44
|
const internalLinkMatch = hrefLink.match(/^((?:.*)(?:\/|\.md|\.html))(#.*)?$/);
|
|
59
|
-
if (internalLinkMatch) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// extract internal links for file / page existence check
|
|
90
|
-
const links = env.links || (env.links = []);
|
|
91
|
-
links.push({
|
|
92
|
-
raw: hrefLink,
|
|
93
|
-
relative: relativePath,
|
|
94
|
-
absolute: absolutePath,
|
|
95
|
-
});
|
|
45
|
+
if (!internalLinkMatch) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// convert
|
|
49
|
+
// <a href="hrefLink">
|
|
50
|
+
// to
|
|
51
|
+
// <RouterLink to="toProp">
|
|
52
|
+
// notice that the path and hash are encoded by markdown-it
|
|
53
|
+
const rawPath = internalLinkMatch[1];
|
|
54
|
+
const rawHash = internalLinkMatch[2] || '';
|
|
55
|
+
// resolve relative and absolute path
|
|
56
|
+
const { relativePath, absolutePath } = (0, resolvePaths_1.resolvePaths)(rawPath, base, filePathRelative);
|
|
57
|
+
// normalize markdown file path to route path
|
|
58
|
+
//
|
|
59
|
+
// we are removing the `base` from absolute path because it should not be
|
|
60
|
+
// passed to `<RouterLink>`
|
|
61
|
+
//
|
|
62
|
+
// '/foo/index.md' => '/foo/'
|
|
63
|
+
// '/foo/bar.md' => '/foo/bar.html'
|
|
64
|
+
const normalizedPath = absolutePath
|
|
65
|
+
.replace(new RegExp(`^${base}`), '/')
|
|
66
|
+
.replace(/(^|\/)(README|index).md$/i, '$1')
|
|
67
|
+
.replace(/\.md$/, '.html');
|
|
68
|
+
if (internalTag === 'RouterLink') {
|
|
69
|
+
// convert starting tag of internal link to `<RouterLink>`
|
|
70
|
+
token.tag = internalTag;
|
|
71
|
+
// replace the original `href` attr with `to` attr
|
|
72
|
+
hrefAttr[0] = 'to';
|
|
73
|
+
// set `hasOpenInternalLink` to modify the ending tag
|
|
74
|
+
hasOpenInternalLink = true;
|
|
96
75
|
}
|
|
76
|
+
hrefAttr[1] = `${normalizedPath}${rawHash}`;
|
|
77
|
+
// extract internal links for file / page existence check
|
|
78
|
+
const links = env.links || (env.links = []);
|
|
79
|
+
links.push({
|
|
80
|
+
raw: hrefLink,
|
|
81
|
+
relative: relativePath,
|
|
82
|
+
absolute: absolutePath,
|
|
83
|
+
});
|
|
97
84
|
};
|
|
98
85
|
md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
|
|
99
86
|
handleLinkOpen(tokens, idx, env);
|
|
100
87
|
return self.renderToken(tokens, idx, options);
|
|
101
88
|
};
|
|
102
89
|
md.renderer.rules.link_close = (tokens, idx, options, env, self) => {
|
|
103
|
-
const token = tokens[idx];
|
|
104
|
-
// add external icon before ending tag of external link
|
|
105
|
-
if (hasOpenExternalLink) {
|
|
106
|
-
hasOpenExternalLink = false;
|
|
107
|
-
return '<OutboundLink/>' + self.renderToken(tokens, idx, options);
|
|
108
|
-
}
|
|
109
90
|
// convert ending tag of internal link
|
|
110
91
|
if (hasOpenInternalLink) {
|
|
111
92
|
hasOpenInternalLink = false;
|
|
112
|
-
|
|
93
|
+
tokens[idx].tag = internalTag;
|
|
113
94
|
}
|
|
114
95
|
return self.renderToken(tokens, idx, options);
|
|
115
96
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vuepress/markdown",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.29",
|
|
4
4
|
"description": "Markdown package of VuePress",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"vuepress",
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "2c6eb4270f96b6351cd1f565798bed1f662a6bb2"
|
|
41
41
|
}
|