@docusaurus/mdx-loader 2.0.0-beta.21 → 2.0.0-beta.22
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.
- package/lib/index.d.ts +1 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/loader.d.ts +2 -0
- package/lib/loader.d.ts.map +1 -1
- package/lib/loader.js +38 -24
- package/lib/loader.js.map +1 -1
- package/lib/remark/admonitions/index.d.ts +15 -0
- package/lib/remark/admonitions/index.d.ts.map +1 -0
- package/lib/remark/admonitions/index.js +132 -0
- package/lib/remark/admonitions/index.js.map +1 -0
- package/lib/remark/transformImage/index.d.ts.map +1 -1
- package/lib/remark/transformImage/index.js +14 -9
- package/lib/remark/transformImage/index.js.map +1 -1
- package/lib/remark/transformLinks/index.d.ts.map +1 -1
- package/lib/remark/transformLinks/index.js +15 -11
- package/lib/remark/transformLinks/index.js.map +1 -1
- package/package.json +11 -8
- package/src/index.ts +1 -1
- package/src/loader.ts +47 -24
- package/src/remark/admonitions/LICENSE +45 -0
- package/src/remark/admonitions/README.md +3 -0
- package/src/remark/admonitions/index.ts +186 -0
- package/src/remark/transformImage/index.ts +17 -11
- package/src/remark/transformLinks/index.ts +26 -15
package/lib/index.d.ts
CHANGED
|
@@ -4,7 +4,6 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
/// <reference types="react" />
|
|
8
7
|
import { mdxLoader } from './loader';
|
|
9
8
|
export default mdxLoader;
|
|
10
9
|
export declare type TOCItem = {
|
|
@@ -18,7 +17,7 @@ export declare type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined>
|
|
|
18
17
|
/** As provided by the content plugin. */
|
|
19
18
|
readonly metadata: Metadata;
|
|
20
19
|
/** A list of TOC items (headings). */
|
|
21
|
-
readonly toc
|
|
20
|
+
readonly toc: readonly TOCItem[];
|
|
22
21
|
/** First h1 title before any content. */
|
|
23
22
|
readonly contentTitle: string | undefined;
|
|
24
23
|
/**
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AAEnC,eAAe,SAAS,CAAC;AAEzB,oBAAY,OAAO,GAAG;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,oBAAY,gBAAgB,CAAC,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,IAAI;IACxE,gDAAgD;IAChD,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC,yCAAyC;IACzC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAC5B,sCAAsC;IACtC,QAAQ,CAAC,GAAG,EAAE,SAAS,OAAO,EAAE,CAAC;IACjC,yCAAyC;IACzC,QAAQ,CAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1C;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,IAAI,GAAG,CAAC,OAAO,CAAC;CACjB,CAAC;AACF,YAAY,EAAC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAC,MAAM,UAAU,CAAC"}
|
package/lib/loader.d.ts
CHANGED
|
@@ -6,11 +6,13 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import type { LoaderContext } from 'webpack';
|
|
8
8
|
import type { Plugin } from 'unified';
|
|
9
|
+
import type { AdmonitionOptions } from './remark/admonitions';
|
|
9
10
|
export declare type MDXPlugin = [
|
|
10
11
|
Plugin<any[]>,
|
|
11
12
|
any
|
|
12
13
|
] | Plugin<any[]>;
|
|
13
14
|
export declare type MDXOptions = {
|
|
15
|
+
admonitions: boolean | AdmonitionOptions;
|
|
14
16
|
remarkPlugins: MDXPlugin[];
|
|
15
17
|
rehypePlugins: MDXPlugin[];
|
|
16
18
|
beforeDefaultRemarkPlugins: MDXPlugin[];
|
package/lib/loader.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"loader.d.ts","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAqBH,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,SAAS,CAAC;AAC3C,OAAO,KAAK,EAAY,MAAM,EAAC,MAAM,SAAS,CAAC;AAC/C,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,sBAAsB,CAAC;AAsB5D,oBAAY,SAAS,GAEnB;IAAC,MAAM,CAAC,GAAG,EAAE,CAAC;IAAE,GAAG;CAAC,GAAG,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;AAEvC,oBAAY,UAAU,GAAG;IACvB,WAAW,EAAE,OAAO,GAAG,iBAAiB,CAAC;IACzC,aAAa,EAAE,SAAS,EAAE,CAAC;IAC3B,aAAa,EAAE,SAAS,EAAE,CAAC;IAC3B,0BAA0B,EAAE,SAAS,EAAE,CAAC;IACxC,0BAA0B,EAAE,SAAS,EAAE,CAAC;CACzC,CAAC;AAEF,oBAAY,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG;IAC1C,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC;IAC7C,sCAAsC,CAAC,EAAE,OAAO,CAAC;IACjD,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,YAAY,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;IACvD,YAAY,CAAC,EAAE,CAAC,QAAQ,EAAE;QACxB,WAAW,EAAE;YAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;SAAC,CAAC;QACtC,QAAQ,EAAE;YAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;SAAC,CAAC;KACpC,KAAK;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAC,CAAC;IAC/B,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AA8EF,wBAAsB,SAAS,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CA8Hf"}
|
package/lib/loader.js
CHANGED
|
@@ -19,6 +19,7 @@ const toc_1 = tslib_1.__importDefault(require("./remark/toc"));
|
|
|
19
19
|
const unwrapMdxCodeBlocks_1 = tslib_1.__importDefault(require("./remark/unwrapMdxCodeBlocks"));
|
|
20
20
|
const transformImage_1 = tslib_1.__importDefault(require("./remark/transformImage"));
|
|
21
21
|
const transformLinks_1 = tslib_1.__importDefault(require("./remark/transformLinks"));
|
|
22
|
+
const admonitions_1 = tslib_1.__importDefault(require("./remark/admonitions"));
|
|
22
23
|
const { loaders: { inlineMarkdownImageFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
|
|
23
24
|
const pragma = `
|
|
24
25
|
/* @jsxRuntime classic */
|
|
@@ -26,6 +27,7 @@ const pragma = `
|
|
|
26
27
|
/* @jsxFrag React.Fragment */
|
|
27
28
|
`;
|
|
28
29
|
const DEFAULT_OPTIONS = {
|
|
30
|
+
admonitions: true,
|
|
29
31
|
rehypePlugins: [],
|
|
30
32
|
remarkPlugins: [unwrapMdxCodeBlocks_1.default, remark_emoji_1.default, headings_1.default, toc_1.default],
|
|
31
33
|
beforeDefaultRemarkPlugins: [],
|
|
@@ -86,6 +88,15 @@ function createAssetsExportCode(assets) {
|
|
|
86
88
|
.filter(Boolean);
|
|
87
89
|
return `{\n${codeLines.join('\n')}\n}`;
|
|
88
90
|
}
|
|
91
|
+
function getAdmonitionsPlugins(admonitionsOption) {
|
|
92
|
+
if (admonitionsOption) {
|
|
93
|
+
const plugin = admonitionsOption === true
|
|
94
|
+
? admonitions_1.default
|
|
95
|
+
: [admonitions_1.default, admonitionsOption];
|
|
96
|
+
return [plugin];
|
|
97
|
+
}
|
|
98
|
+
return [];
|
|
99
|
+
}
|
|
89
100
|
async function mdxLoader(fileString) {
|
|
90
101
|
const callback = this.async();
|
|
91
102
|
const filePath = this.resourcePath;
|
|
@@ -96,32 +107,35 @@ async function mdxLoader(fileString) {
|
|
|
96
107
|
});
|
|
97
108
|
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
98
109
|
if (!compilerCache.has(this.query)) {
|
|
99
|
-
const
|
|
100
|
-
...reqOptions,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
},
|
|
110
|
-
],
|
|
111
|
-
[
|
|
112
|
-
transformLinks_1.default,
|
|
113
|
-
{
|
|
114
|
-
staticDirs: reqOptions.staticDirs,
|
|
115
|
-
siteDir: reqOptions.siteDir,
|
|
116
|
-
},
|
|
117
|
-
],
|
|
118
|
-
...(reqOptions.remarkPlugins ?? []),
|
|
110
|
+
const remarkPlugins = [
|
|
111
|
+
...(reqOptions.beforeDefaultRemarkPlugins ?? []),
|
|
112
|
+
...getAdmonitionsPlugins(reqOptions.admonitions ?? false),
|
|
113
|
+
...DEFAULT_OPTIONS.remarkPlugins,
|
|
114
|
+
[
|
|
115
|
+
transformImage_1.default,
|
|
116
|
+
{
|
|
117
|
+
staticDirs: reqOptions.staticDirs,
|
|
118
|
+
siteDir: reqOptions.siteDir,
|
|
119
|
+
},
|
|
119
120
|
],
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
[
|
|
122
|
+
transformLinks_1.default,
|
|
123
|
+
{
|
|
124
|
+
staticDirs: reqOptions.staticDirs,
|
|
125
|
+
siteDir: reqOptions.siteDir,
|
|
126
|
+
},
|
|
124
127
|
],
|
|
128
|
+
...(reqOptions.remarkPlugins ?? []),
|
|
129
|
+
];
|
|
130
|
+
const rehypePlugins = [
|
|
131
|
+
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
132
|
+
...DEFAULT_OPTIONS.rehypePlugins,
|
|
133
|
+
...(reqOptions.rehypePlugins ?? []),
|
|
134
|
+
];
|
|
135
|
+
const options = {
|
|
136
|
+
...reqOptions,
|
|
137
|
+
remarkPlugins,
|
|
138
|
+
rehypePlugins,
|
|
125
139
|
};
|
|
126
140
|
compilerCache.set(this.query, [(0, mdx_1.createCompiler)(options), options]);
|
|
127
141
|
}
|
package/lib/loader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,gEAA0B;AAC1B,wEAAwC;AACxC,6CAK2B;AAC3B,qCAA2C;AAC3C,wEAAiC;AACjC,gFAA+C;AAE/C,yEAAyC;AACzC,+DAA+B;AAC/B,+FAA+D;AAC/D,qFAAqD;AACrD,qFAAqD;
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,gEAA0B;AAC1B,wEAAwC;AACxC,6CAK2B;AAC3B,qCAA2C;AAC3C,wEAAiC;AACjC,gFAA+C;AAE/C,yEAAyC;AACzC,+DAA+B;AAC/B,+FAA+D;AAC/D,qFAAqD;AACrD,qFAAqD;AAErD,+EAAwD;AAKxD,MAAM,EACJ,OA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AAE;QAChB,IAAI,CAAC,aAAa,CAAC,YAAY,CAAC,CAAC;KAClC;IAED,MAAM,kBAAkB,GAAG,YAAY;QACrC,CAAC,CAAC,MAAM,gBAAgB,CAAC,YAAY,CAAC;QACtC,CAAC,CAAC,SAAS,CAAC;IAEd,MAAM,QAAQ,GAAG,kBAAkB;QACjC,CAAC,CAAE,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAA8B;QAC9D,CAAC,CAAC,SAAS,CAAC;IAEd,MAAM,MAAM,GACV,UAAU,CAAC,YAAY,IAAI,QAAQ;QACjC,CAAC,CAAC,UAAU,CAAC,YAAY,CAAC,EAAC,WAAW,EAAE,QAAQ,EAAC,CAAC;QAClD,CAAC,CAAC,SAAS,CAAC;IAEhB,MAAM,WAAW,GAAG;6BACO,IAAA,0BAAe,EAAC,WAAW,CAAC;8BAC3B,IAAA,0BAAe,EAAC,YAAY,CAAC;EACzD,kBAAkB,CAAC,CAAC,CAAC,2BAA2B,kBAAkB,GAAG,CAAC,CAAC,CAAC,EAAE;EAC1E,MAAM,CAAC,CAAC,CAAC,yBAAyB,sBAAsB,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE;CACzE,CAAC;IAEA,MAAM,IAAI,GAAG;EACb,MAAM;;;;EAIN,WAAW;EACX,MAAM;CACP,CAAC;IAEA,OAAO,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAC9B,CAAC;AAjID,8BAiIC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
import type { Plugin } from 'unified';
|
|
8
|
+
export declare type AdmonitionOptions = {
|
|
9
|
+
tag: string;
|
|
10
|
+
keywords: string[];
|
|
11
|
+
};
|
|
12
|
+
export declare const DefaultAdmonitionOptions: AdmonitionOptions;
|
|
13
|
+
declare const plugin: Plugin;
|
|
14
|
+
export default plugin;
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/admonitions/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAyB,MAAM,EAAC,MAAM,SAAS,CAAC;AAK5D,oBAAY,iBAAiB,GAAG;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB,CAAC;AAEF,eAAO,MAAM,wBAAwB,EAAE,iBAatC,CAAC;AAgBF,QAAA,MAAM,MAAM,EAAE,MAwIb,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the MIT license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.DefaultAdmonitionOptions = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
|
|
12
|
+
const NEWLINE = '\n';
|
|
13
|
+
exports.DefaultAdmonitionOptions = {
|
|
14
|
+
tag: ':::',
|
|
15
|
+
keywords: [
|
|
16
|
+
'secondary',
|
|
17
|
+
'info',
|
|
18
|
+
'success',
|
|
19
|
+
'danger',
|
|
20
|
+
'note',
|
|
21
|
+
'tip',
|
|
22
|
+
'warning',
|
|
23
|
+
'important',
|
|
24
|
+
'caution',
|
|
25
|
+
],
|
|
26
|
+
};
|
|
27
|
+
function escapeRegExp(s) {
|
|
28
|
+
return s.replace(/[-[\]{}()*+?.\\^$|/]/g, '\\$&');
|
|
29
|
+
}
|
|
30
|
+
function normalizeOptions(options) {
|
|
31
|
+
return { ...exports.DefaultAdmonitionOptions, ...options };
|
|
32
|
+
}
|
|
33
|
+
// This string value does not matter much
|
|
34
|
+
// It is ignored because nodes are using hName/hProperties coming from HAST
|
|
35
|
+
const admonitionNodeType = 'admonitionHTML';
|
|
36
|
+
const plugin = function plugin(optionsInput = {}) {
|
|
37
|
+
const options = normalizeOptions(optionsInput);
|
|
38
|
+
const keywords = Object.values(options.keywords).map(escapeRegExp).join('|');
|
|
39
|
+
const tag = escapeRegExp(options.tag);
|
|
40
|
+
const regex = new RegExp(`${tag}(${keywords})(?: *(.*))?\n`);
|
|
41
|
+
const escapeTag = new RegExp(escapeRegExp(`\\${options.tag}`), 'g');
|
|
42
|
+
// The tokenizer is called on blocks to determine if there is an admonition
|
|
43
|
+
// present and create tags for it
|
|
44
|
+
function blockTokenizer(eat, value, silent) {
|
|
45
|
+
// Stop if no match or match does not start at beginning of line
|
|
46
|
+
const match = regex.exec(value);
|
|
47
|
+
if (!match || match.index !== 0) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
// If silent return the match
|
|
51
|
+
if (silent) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
const now = eat.now();
|
|
55
|
+
const [opening, keyword, title] = match;
|
|
56
|
+
const food = [];
|
|
57
|
+
const content = [];
|
|
58
|
+
let newValue = value;
|
|
59
|
+
// consume lines until a closing tag
|
|
60
|
+
let idx = newValue.indexOf(NEWLINE);
|
|
61
|
+
while (idx !== -1) {
|
|
62
|
+
// grab this line and eat it
|
|
63
|
+
const next = newValue.indexOf(NEWLINE, idx + 1);
|
|
64
|
+
const line = next !== -1 ? newValue.slice(idx + 1, next) : newValue.slice(idx + 1);
|
|
65
|
+
food.push(line);
|
|
66
|
+
newValue = newValue.slice(idx + 1);
|
|
67
|
+
// the closing tag is NOT part of the content
|
|
68
|
+
if (line.startsWith(options.tag)) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
content.push(line);
|
|
72
|
+
idx = newValue.indexOf(NEWLINE);
|
|
73
|
+
}
|
|
74
|
+
// consume the processed tag and replace escape sequences
|
|
75
|
+
const contentString = content.join(NEWLINE).replace(escapeTag, options.tag);
|
|
76
|
+
const add = eat(opening + food.join(NEWLINE));
|
|
77
|
+
// parse the content in block mode
|
|
78
|
+
const exit = this.enterBlock();
|
|
79
|
+
const contentNodes = this.tokenizeBlock(contentString, now);
|
|
80
|
+
exit();
|
|
81
|
+
const titleNodes = this.tokenizeInline(title, now);
|
|
82
|
+
const isSimpleTextTitle = titleNodes.length === 1 && titleNodes[0].type === 'text';
|
|
83
|
+
const element = {
|
|
84
|
+
type: admonitionNodeType,
|
|
85
|
+
data: {
|
|
86
|
+
// hName/hProperties come from HAST
|
|
87
|
+
// See https://github.com/syntax-tree/mdast-util-to-hast#fields-on-nodes
|
|
88
|
+
hName: 'admonition',
|
|
89
|
+
hProperties: {
|
|
90
|
+
...(title && isSimpleTextTitle && { title }),
|
|
91
|
+
type: keyword,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
children: [
|
|
95
|
+
// For titles containing MDX syntax: create a custom element. The theme
|
|
96
|
+
// component will extract it and render it nicely.
|
|
97
|
+
//
|
|
98
|
+
// Temporary workaround, because it's complex in MDX v1 to emit
|
|
99
|
+
// interpolated JSX prop syntax (title={<>my <code>title</code></>}).
|
|
100
|
+
// For this reason, we use children instead of the title prop.
|
|
101
|
+
title &&
|
|
102
|
+
!isSimpleTextTitle && {
|
|
103
|
+
type: admonitionNodeType,
|
|
104
|
+
data: {
|
|
105
|
+
hName: 'mdxAdmonitionTitle',
|
|
106
|
+
hProperties: {},
|
|
107
|
+
},
|
|
108
|
+
children: titleNodes,
|
|
109
|
+
},
|
|
110
|
+
...contentNodes,
|
|
111
|
+
].filter(Boolean),
|
|
112
|
+
};
|
|
113
|
+
return add(element);
|
|
114
|
+
}
|
|
115
|
+
// add tokenizer to parser after fenced code blocks
|
|
116
|
+
const Parser = this.Parser.prototype;
|
|
117
|
+
Parser.blockTokenizers.admonition = blockTokenizer;
|
|
118
|
+
Parser.blockMethods.splice(Parser.blockMethods.indexOf('fencedCode') + 1, 0, 'admonition');
|
|
119
|
+
Parser.interruptParagraph.splice(Parser.interruptParagraph.indexOf('fencedCode') + 1, 0, ['admonition']);
|
|
120
|
+
Parser.interruptList.splice(Parser.interruptList.indexOf('fencedCode') + 1, 0, ['admonition']);
|
|
121
|
+
Parser.interruptBlockquote.splice(Parser.interruptBlockquote.indexOf('fencedCode') + 1, 0, ['admonition']);
|
|
122
|
+
return (root) => {
|
|
123
|
+
// escape everything except admonitionHTML nodes
|
|
124
|
+
(0, unist_util_visit_1.default)(root, (node) => node?.type !== admonitionNodeType, (node) => {
|
|
125
|
+
if (node.value) {
|
|
126
|
+
node.value = node.value.replace(escapeTag, options.tag);
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
exports.default = plugin;
|
|
132
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/admonitions/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,gFAAqC;AAIrC,MAAM,OAAO,GAAG,IAAI,CAAC;AAOR,QAAA,wBAAwB,GAAsB;IACzD,GAAG,EAAE,KAAK;IACV,QAAQ,EAAE;QACR,WAAW;QACX,MAAM;QACN,SAAS;QACT,QAAQ;QACR,MAAM;QACN,KAAK;QACL,SAAS;QACT,WAAW;QACX,SAAS;KACV;CACF,CAAC;AAEF,SAAS,YAAY,CAAC,CAAS;IAC7B,OAAO,CAAC,CAAC,OAAO,CAAC,uBAAuB,EAAE,MAAM,CAAC,CAAC;AACpD,CAAC;AAED,SAAS,gBAAgB,CACvB,OAAmC;IAEnC,OAAO,EAAC,GAAG,gCAAwB,EAAE,GAAG,OAA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CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,EAC9C,CAAC,EACD,CAAC,YAAY,CAAC,CACf,CAAC;IACF,MAAM,CAAC,mBAAmB,CAAC,MAAM,CAC/B,MAAM,CAAC,mBAAmB,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,EACpD,CAAC,EACD,CAAC,YAAY,CAAC,CACf,CAAC;IAEF,OAAO,CAAC,IAAI,EAAE,EAAE;QACd,gDAAgD;QAChD,IAAA,0BAAK,EACH,IAAI,EACJ,CAAC,IAAa,EAAmB,EAAE,CAChC,IAA4B,EAAE,IAAI,KAAK,kBAAkB,EAC5D,CAAC,IAAa,EAAE,EAAE;YAChB,IAAI,IAAI,CAAC,KAAK,EAAE;gBACd,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,GAAG,CAAC,CAAC;aACzD;QACH,CAAC,CACF,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAiBH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAiBH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAQzC,oBAAY,aAAa,GAAG;IAC1B,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AA8HF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,WAAW,CAalE"}
|
|
@@ -17,8 +17,7 @@ const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
|
|
|
17
17
|
const escape_html_1 = tslib_1.__importDefault(require("escape-html"));
|
|
18
18
|
const image_size_1 = tslib_1.__importDefault(require("image-size"));
|
|
19
19
|
const { loaders: { inlineMarkdownImageFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
|
|
20
|
-
async function toImageRequireNode(node, imagePath, filePath) {
|
|
21
|
-
const jsxNode = node;
|
|
20
|
+
async function toImageRequireNode([node, index, parent], imagePath, filePath) {
|
|
22
21
|
let relativeImagePath = (0, utils_1.posixPath)(path_1.default.relative(path_1.default.dirname(filePath), imagePath));
|
|
23
22
|
relativeImagePath = `./${relativeImagePath}`;
|
|
24
23
|
const parsedUrl = url_1.default.parse(node.url);
|
|
@@ -46,9 +45,11 @@ async function toImageRequireNode(node, imagePath, filePath) {
|
|
|
46
45
|
${err.message}`;
|
|
47
46
|
}
|
|
48
47
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
const jsxNode = {
|
|
49
|
+
type: 'jsx',
|
|
50
|
+
value: `<img ${alt}src={${src}}${title}${width}${height} />`,
|
|
51
|
+
};
|
|
52
|
+
parent.children.splice(index, 1, jsxNode);
|
|
52
53
|
}
|
|
53
54
|
async function ensureImageFileExist(imagePath, sourceFilePath) {
|
|
54
55
|
const imageExists = await fs_extra_1.default.pathExists(imagePath);
|
|
@@ -78,7 +79,8 @@ async function getImageAbsolutePath(imagePath, { siteDir, filePath, staticDirs }
|
|
|
78
79
|
await ensureImageFileExist(imageFilePath, filePath);
|
|
79
80
|
return imageFilePath;
|
|
80
81
|
}
|
|
81
|
-
async function processImageNode(
|
|
82
|
+
async function processImageNode(target, context) {
|
|
83
|
+
const [node] = target;
|
|
82
84
|
if (!node.url) {
|
|
83
85
|
throw new Error(`Markdown image URL is mandatory in "${(0, utils_1.toMessageRelativeFilePath)(context.filePath)}" file`);
|
|
84
86
|
}
|
|
@@ -94,13 +96,16 @@ async function processImageNode(node, context) {
|
|
|
94
96
|
// We try to convert image urls without protocol to images with require calls
|
|
95
97
|
// going through webpack ensures that image assets exist at build time
|
|
96
98
|
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
|
|
97
|
-
await toImageRequireNode(
|
|
99
|
+
await toImageRequireNode(target, imagePath, context.filePath);
|
|
98
100
|
}
|
|
99
101
|
function plugin(options) {
|
|
100
102
|
return async (root, vfile) => {
|
|
101
103
|
const promises = [];
|
|
102
|
-
(0, unist_util_visit_1.default)(root, 'image', (node) => {
|
|
103
|
-
promises.push(processImageNode(node,
|
|
104
|
+
(0, unist_util_visit_1.default)(root, 'image', (node, index, parent) => {
|
|
105
|
+
promises.push(processImageNode([node, index, parent], {
|
|
106
|
+
...options,
|
|
107
|
+
filePath: vfile.path,
|
|
108
|
+
}));
|
|
104
109
|
});
|
|
105
110
|
await Promise.all(promises);
|
|
106
111
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,wDAAwB;AACxB,sDAAsB;AACtB,gEAA0B;AAC1B,+BAA+B;AAC/B,wEAAwC;AACxC,6CAM2B;AAC3B,gFAAqC;AACrC,sEAAqC;AACrC,oEAAgC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,wDAAwB;AACxB,sDAAsB;AACtB,gEAA0B;AAC1B,+BAA+B;AAC/B,wEAAwC;AACxC,6CAM2B;AAC3B,gFAAqC;AACrC,sEAAqC;AACrC,oEAAgC;AAKhC,MAAM,EACJ,OAAO,EAAE,EAAC,6BAA6B,EAAC,GACzC,GAAG,IAAA,0BAAkB,GAAE,CAAC;AAazB,KAAK,UAAU,kBAAkB,CAC/B,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,CAAS,EAC7B,SAAiB,EACjB,QAAgB;IAEhB,IAAI,iBAAi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ChD;QACD,OAAO;KACR;IAED,6EAA6E;IAC7E,sEAAsE;IACtE,MAAM,SAAS,GAAG,MAAM,oBAAoB,CAAC,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC1E,MAAM,kBAAkB,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;AAChE,CAAC;AAED,SAAwB,MAAM,CAAC,OAAsB;IACnD,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;QAC3B,MAAM,QAAQ,GAAoB,EAAE,CAAC;QACrC,IAAA,0BAAK,EAAC,IAAI,EAAE,OAAO,EAAE,CAAC,IAAW,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YAClD,QAAQ,CAAC,IAAI,CACX,gBAAgB,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,MAAO,CAAC,EAAE;gBACvC,GAAG,OAAO;gBACV,QAAQ,EAAE,KAAK,CAAC,IAAK;aACtB,CAAC,CACH,CAAC;QACJ,CAAC,CAAC,CAAC;QACH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC,CAAC;AACJ,CAAC;AAbD,yBAaC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAQzC,oBAAY,aAAa,GAAG;IAC1B,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAuHF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,WAAW,CAalE"}
|
|
@@ -18,11 +18,9 @@ const { loaders: { inlineMarkdownLinkFileLoader }, } = (0, utils_1.getFileLoader
|
|
|
18
18
|
/**
|
|
19
19
|
* Transforms the link node to a JSX `<a>` element with a `require()` call.
|
|
20
20
|
*/
|
|
21
|
-
function toAssetRequireNode(node, assetPath, filePath) {
|
|
22
|
-
const jsxNode = node;
|
|
23
|
-
let relativeAssetPath = (0, utils_1.posixPath)(path_1.default.relative(path_1.default.dirname(filePath), assetPath));
|
|
21
|
+
function toAssetRequireNode([node, index, parent], assetPath, filePath) {
|
|
24
22
|
// require("assets/file.pdf") means requiring from a package called assets
|
|
25
|
-
relativeAssetPath = `./${
|
|
23
|
+
const relativeAssetPath = `./${(0, utils_1.posixPath)(path_1.default.relative(path_1.default.dirname(filePath), assetPath))}`;
|
|
26
24
|
const parsedUrl = url_1.default.parse(node.url);
|
|
27
25
|
const hash = parsedUrl.hash ?? '';
|
|
28
26
|
const search = parsedUrl.search ?? '';
|
|
@@ -33,9 +31,11 @@ function toAssetRequireNode(node, assetPath, filePath) {
|
|
|
33
31
|
: ''}${inlineMarkdownLinkFileLoader}${(0, utils_1.escapePath)(relativeAssetPath) + search}').default${hash ? ` + '${hash}'` : ''}`;
|
|
34
32
|
const children = (0, utils_2.stringifyContent)(node);
|
|
35
33
|
const title = node.title ? ` title="${(0, escape_html_1.default)(node.title)}"` : '';
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
const jsxNode = {
|
|
35
|
+
type: 'jsx',
|
|
36
|
+
value: `<a target="_blank" href={${href}}${title}>${children}</a>`,
|
|
37
|
+
};
|
|
38
|
+
parent.children.splice(index, 1, jsxNode);
|
|
39
39
|
}
|
|
40
40
|
async function ensureAssetFileExist(assetPath, sourceFilePath) {
|
|
41
41
|
const assetExists = await fs_extra_1.default.pathExists(assetPath);
|
|
@@ -65,7 +65,8 @@ async function getAssetAbsolutePath(assetPath, { siteDir, filePath, staticDirs }
|
|
|
65
65
|
}
|
|
66
66
|
return null;
|
|
67
67
|
}
|
|
68
|
-
async function processLinkNode(
|
|
68
|
+
async function processLinkNode(target, context) {
|
|
69
|
+
const [node] = target;
|
|
69
70
|
if (!node.url) {
|
|
70
71
|
// Try to improve error feedback
|
|
71
72
|
// see https://github.com/facebook/docusaurus/issues/3309#issuecomment-690371675
|
|
@@ -86,14 +87,17 @@ async function processLinkNode(node, context) {
|
|
|
86
87
|
}
|
|
87
88
|
const assetPath = await getAssetAbsolutePath(decodeURIComponent(parsedUrl.pathname), context);
|
|
88
89
|
if (assetPath) {
|
|
89
|
-
toAssetRequireNode(
|
|
90
|
+
toAssetRequireNode(target, assetPath, context.filePath);
|
|
90
91
|
}
|
|
91
92
|
}
|
|
92
93
|
function plugin(options) {
|
|
93
94
|
return async (root, vfile) => {
|
|
94
95
|
const promises = [];
|
|
95
|
-
(0, unist_util_visit_1.default)(root, 'link', (node) => {
|
|
96
|
-
promises.push(processLinkNode(node,
|
|
96
|
+
(0, unist_util_visit_1.default)(root, 'link', (node, index, parent) => {
|
|
97
|
+
promises.push(processLinkNode([node, index, parent], {
|
|
98
|
+
...options,
|
|
99
|
+
filePath: vfile.path,
|
|
100
|
+
}));
|
|
97
101
|
});
|
|
98
102
|
await Promise.all(promises);
|
|
99
103
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,wDAAwB;AACxB,sDAAsB;AACtB,gEAA0B;AAC1B,6CAM2B;AAC3B,gFAAqC;AACrC,sEAAqC;AACrC,oCAA0C;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,wDAAwB;AACxB,sDAAsB;AACtB,gEAA0B;AAC1B,6CAM2B;AAC3B,gFAAqC;AACrC,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,IAAI,SAAS,EAAE;QACb,kBAAkB,CAAC,MAAM,EAAE,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;KACzD;AACH,CAAC;AAED,SAAwB,MAAM,CAAC,OAAsB;IACnD,OAAO,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;QAC3B,MAAM,QAAQ,GAAoB,EAAE,CAAC;QACrC,IAAA,0BAAK,EAAC,IAAI,EAAE,MAAM,EAAE,CAAC,IAAU,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YAChD,QAAQ,CAAC,IAAI,CACX,eAAe,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,MAAO,CAAC,EAAE;gBACtC,GAAG,OAAO;gBACV,QAAQ,EAAE,KAAK,CAAC,IAAK;aACtB,CAAC,CACH,CAAC;QACJ,CAAC,CAAC,CAAC;QACH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC,CAAC;AACJ,CAAC;AAbD,yBAaC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/mdx-loader",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.22",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@babel/parser": "^7.18.
|
|
22
|
-
"@babel/traverse": "^7.18.
|
|
23
|
-
"@docusaurus/logger": "2.0.0-beta.
|
|
24
|
-
"@docusaurus/utils": "2.0.0-beta.
|
|
21
|
+
"@babel/parser": "^7.18.6",
|
|
22
|
+
"@babel/traverse": "^7.18.6",
|
|
23
|
+
"@docusaurus/logger": "2.0.0-beta.22",
|
|
24
|
+
"@docusaurus/utils": "2.0.0-beta.22",
|
|
25
25
|
"@mdx-js/mdx": "^1.6.22",
|
|
26
26
|
"escape-html": "^1.0.3",
|
|
27
27
|
"file-loader": "^6.2.0",
|
|
@@ -31,18 +31,21 @@
|
|
|
31
31
|
"remark-emoji": "^2.2.0",
|
|
32
32
|
"stringify-object": "^3.3.0",
|
|
33
33
|
"tslib": "^2.4.0",
|
|
34
|
+
"unified": "^9.2.2",
|
|
34
35
|
"unist-util-visit": "^2.0.3",
|
|
35
36
|
"url-loader": "^4.1.1",
|
|
36
|
-
"webpack": "^5.
|
|
37
|
+
"webpack": "^5.73.0"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
|
-
"@docusaurus/types": "2.0.0-beta.
|
|
40
|
+
"@docusaurus/types": "2.0.0-beta.22",
|
|
40
41
|
"@types/escape-html": "^1.0.2",
|
|
41
42
|
"@types/mdast": "^3.0.10",
|
|
42
43
|
"@types/stringify-object": "^3.3.1",
|
|
43
44
|
"@types/unist": "^2.0.6",
|
|
45
|
+
"rehype-stringify": "^8.0.0",
|
|
44
46
|
"remark": "^12.0.1",
|
|
45
47
|
"remark-mdx": "^1.6.21",
|
|
48
|
+
"remark-rehype": "^8.1.0",
|
|
46
49
|
"to-vfile": "^6.1.0",
|
|
47
50
|
"unist-builder": "^2.0.3",
|
|
48
51
|
"unist-util-remove-position": "^3.0.0"
|
|
@@ -54,5 +57,5 @@
|
|
|
54
57
|
"engines": {
|
|
55
58
|
"node": ">=16.14"
|
|
56
59
|
},
|
|
57
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "daf9e462c4eebb7ac26a940932311f987e768f87"
|
|
58
61
|
}
|
package/src/index.ts
CHANGED
|
@@ -21,7 +21,7 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
|
|
21
21
|
/** As provided by the content plugin. */
|
|
22
22
|
readonly metadata: Metadata;
|
|
23
23
|
/** A list of TOC items (headings). */
|
|
24
|
-
readonly toc
|
|
24
|
+
readonly toc: readonly TOCItem[];
|
|
25
25
|
/** First h1 title before any content. */
|
|
26
26
|
readonly contentTitle: string | undefined;
|
|
27
27
|
/**
|
package/src/loader.ts
CHANGED
|
@@ -23,8 +23,10 @@ import unwrapMdxCodeBlocks from './remark/unwrapMdxCodeBlocks';
|
|
|
23
23
|
import transformImage from './remark/transformImage';
|
|
24
24
|
import transformLinks from './remark/transformLinks';
|
|
25
25
|
|
|
26
|
+
import transformAdmonitions from './remark/admonitions';
|
|
26
27
|
import type {LoaderContext} from 'webpack';
|
|
27
28
|
import type {Processor, Plugin} from 'unified';
|
|
29
|
+
import type {AdmonitionOptions} from './remark/admonitions';
|
|
28
30
|
|
|
29
31
|
const {
|
|
30
32
|
loaders: {inlineMarkdownImageFileLoader},
|
|
@@ -37,6 +39,7 @@ const pragma = `
|
|
|
37
39
|
`;
|
|
38
40
|
|
|
39
41
|
const DEFAULT_OPTIONS: MDXOptions = {
|
|
42
|
+
admonitions: true,
|
|
40
43
|
rehypePlugins: [],
|
|
41
44
|
remarkPlugins: [unwrapMdxCodeBlocks, emoji, headings, toc],
|
|
42
45
|
beforeDefaultRemarkPlugins: [],
|
|
@@ -48,7 +51,9 @@ const compilerCache = new Map<string | Options, [Processor, Options]>();
|
|
|
48
51
|
export type MDXPlugin =
|
|
49
52
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
50
53
|
[Plugin<any[]>, any] | Plugin<any[]>;
|
|
54
|
+
|
|
51
55
|
export type MDXOptions = {
|
|
56
|
+
admonitions: boolean | AdmonitionOptions;
|
|
52
57
|
remarkPlugins: MDXPlugin[];
|
|
53
58
|
rehypePlugins: MDXPlugin[];
|
|
54
59
|
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
@@ -132,6 +137,19 @@ function createAssetsExportCode(assets: unknown) {
|
|
|
132
137
|
return `{\n${codeLines.join('\n')}\n}`;
|
|
133
138
|
}
|
|
134
139
|
|
|
140
|
+
function getAdmonitionsPlugins(
|
|
141
|
+
admonitionsOption: MDXOptions['admonitions'],
|
|
142
|
+
): MDXPlugin[] {
|
|
143
|
+
if (admonitionsOption) {
|
|
144
|
+
const plugin: MDXPlugin =
|
|
145
|
+
admonitionsOption === true
|
|
146
|
+
? transformAdmonitions
|
|
147
|
+
: [transformAdmonitions, admonitionsOption];
|
|
148
|
+
return [plugin];
|
|
149
|
+
}
|
|
150
|
+
return [];
|
|
151
|
+
}
|
|
152
|
+
|
|
135
153
|
export async function mdxLoader(
|
|
136
154
|
this: LoaderContext<Options>,
|
|
137
155
|
fileString: string,
|
|
@@ -149,32 +167,37 @@ export async function mdxLoader(
|
|
|
149
167
|
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
150
168
|
|
|
151
169
|
if (!compilerCache.has(this.query)) {
|
|
152
|
-
const
|
|
153
|
-
...reqOptions,
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
},
|
|
163
|
-
],
|
|
164
|
-
[
|
|
165
|
-
transformLinks,
|
|
166
|
-
{
|
|
167
|
-
staticDirs: reqOptions.staticDirs,
|
|
168
|
-
siteDir: reqOptions.siteDir,
|
|
169
|
-
},
|
|
170
|
-
],
|
|
171
|
-
...(reqOptions.remarkPlugins ?? []),
|
|
170
|
+
const remarkPlugins: MDXPlugin[] = [
|
|
171
|
+
...(reqOptions.beforeDefaultRemarkPlugins ?? []),
|
|
172
|
+
...getAdmonitionsPlugins(reqOptions.admonitions ?? false),
|
|
173
|
+
...DEFAULT_OPTIONS.remarkPlugins,
|
|
174
|
+
[
|
|
175
|
+
transformImage,
|
|
176
|
+
{
|
|
177
|
+
staticDirs: reqOptions.staticDirs,
|
|
178
|
+
siteDir: reqOptions.siteDir,
|
|
179
|
+
},
|
|
172
180
|
],
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
181
|
+
[
|
|
182
|
+
transformLinks,
|
|
183
|
+
{
|
|
184
|
+
staticDirs: reqOptions.staticDirs,
|
|
185
|
+
siteDir: reqOptions.siteDir,
|
|
186
|
+
},
|
|
177
187
|
],
|
|
188
|
+
...(reqOptions.remarkPlugins ?? []),
|
|
189
|
+
];
|
|
190
|
+
|
|
191
|
+
const rehypePlugins: MDXPlugin[] = [
|
|
192
|
+
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
193
|
+
...DEFAULT_OPTIONS.rehypePlugins,
|
|
194
|
+
...(reqOptions.rehypePlugins ?? []),
|
|
195
|
+
];
|
|
196
|
+
|
|
197
|
+
const options: Options = {
|
|
198
|
+
...reqOptions,
|
|
199
|
+
remarkPlugins,
|
|
200
|
+
rehypePlugins,
|
|
178
201
|
};
|
|
179
202
|
compilerCache.set(this.query, [createCompiler(options), options]);
|
|
180
203
|
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Elvis Wolcott
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
MIT License
|
|
26
|
+
|
|
27
|
+
Copyright (c) Facebook, Inc. and its affiliates.
|
|
28
|
+
|
|
29
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
30
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
31
|
+
in the Software without restriction, including without limitation the rights
|
|
32
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
33
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
34
|
+
furnished to do so, subject to the following conditions:
|
|
35
|
+
|
|
36
|
+
The above copyright notice and this permission notice shall be included in all
|
|
37
|
+
copies or substantial portions of the Software.
|
|
38
|
+
|
|
39
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
40
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
41
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
42
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
43
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
44
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
45
|
+
SOFTWARE.
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import visit from 'unist-util-visit';
|
|
9
|
+
import type {Transformer, Processor, Plugin} from 'unified';
|
|
10
|
+
import type {Literal} from 'mdast';
|
|
11
|
+
|
|
12
|
+
const NEWLINE = '\n';
|
|
13
|
+
|
|
14
|
+
export type AdmonitionOptions = {
|
|
15
|
+
tag: string;
|
|
16
|
+
keywords: string[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const DefaultAdmonitionOptions: AdmonitionOptions = {
|
|
20
|
+
tag: ':::',
|
|
21
|
+
keywords: [
|
|
22
|
+
'secondary',
|
|
23
|
+
'info',
|
|
24
|
+
'success',
|
|
25
|
+
'danger',
|
|
26
|
+
'note',
|
|
27
|
+
'tip',
|
|
28
|
+
'warning',
|
|
29
|
+
'important',
|
|
30
|
+
'caution',
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
function escapeRegExp(s: string): string {
|
|
35
|
+
return s.replace(/[-[\]{}()*+?.\\^$|/]/g, '\\$&');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeOptions(
|
|
39
|
+
options: Partial<AdmonitionOptions>,
|
|
40
|
+
): AdmonitionOptions {
|
|
41
|
+
return {...DefaultAdmonitionOptions, ...options};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// This string value does not matter much
|
|
45
|
+
// It is ignored because nodes are using hName/hProperties coming from HAST
|
|
46
|
+
const admonitionNodeType = 'admonitionHTML';
|
|
47
|
+
|
|
48
|
+
const plugin: Plugin = function plugin(
|
|
49
|
+
this: Processor,
|
|
50
|
+
optionsInput: Partial<AdmonitionOptions> = {},
|
|
51
|
+
): Transformer {
|
|
52
|
+
const options = normalizeOptions(optionsInput);
|
|
53
|
+
|
|
54
|
+
const keywords = Object.values(options.keywords).map(escapeRegExp).join('|');
|
|
55
|
+
const tag = escapeRegExp(options.tag);
|
|
56
|
+
const regex = new RegExp(`${tag}(${keywords})(?: *(.*))?\n`);
|
|
57
|
+
const escapeTag = new RegExp(escapeRegExp(`\\${options.tag}`), 'g');
|
|
58
|
+
|
|
59
|
+
// The tokenizer is called on blocks to determine if there is an admonition
|
|
60
|
+
// present and create tags for it
|
|
61
|
+
function blockTokenizer(this: any, eat: any, value: string, silent: boolean) {
|
|
62
|
+
// Stop if no match or match does not start at beginning of line
|
|
63
|
+
const match = regex.exec(value);
|
|
64
|
+
if (!match || match.index !== 0) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
// If silent return the match
|
|
68
|
+
if (silent) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const now = eat.now();
|
|
73
|
+
const [opening, keyword, title] = match as string[] as [
|
|
74
|
+
string,
|
|
75
|
+
string,
|
|
76
|
+
string,
|
|
77
|
+
];
|
|
78
|
+
const food = [];
|
|
79
|
+
const content = [];
|
|
80
|
+
|
|
81
|
+
let newValue = value;
|
|
82
|
+
// consume lines until a closing tag
|
|
83
|
+
let idx = newValue.indexOf(NEWLINE);
|
|
84
|
+
while (idx !== -1) {
|
|
85
|
+
// grab this line and eat it
|
|
86
|
+
const next = newValue.indexOf(NEWLINE, idx + 1);
|
|
87
|
+
const line =
|
|
88
|
+
next !== -1 ? newValue.slice(idx + 1, next) : newValue.slice(idx + 1);
|
|
89
|
+
food.push(line);
|
|
90
|
+
newValue = newValue.slice(idx + 1);
|
|
91
|
+
// the closing tag is NOT part of the content
|
|
92
|
+
if (line.startsWith(options.tag)) {
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
content.push(line);
|
|
96
|
+
idx = newValue.indexOf(NEWLINE);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// consume the processed tag and replace escape sequences
|
|
100
|
+
const contentString = content.join(NEWLINE).replace(escapeTag, options.tag);
|
|
101
|
+
const add = eat(opening + food.join(NEWLINE));
|
|
102
|
+
|
|
103
|
+
// parse the content in block mode
|
|
104
|
+
const exit = this.enterBlock();
|
|
105
|
+
const contentNodes = this.tokenizeBlock(contentString, now);
|
|
106
|
+
exit();
|
|
107
|
+
|
|
108
|
+
const titleNodes = this.tokenizeInline(title, now);
|
|
109
|
+
|
|
110
|
+
const isSimpleTextTitle =
|
|
111
|
+
titleNodes.length === 1 && titleNodes[0].type === 'text';
|
|
112
|
+
|
|
113
|
+
const element = {
|
|
114
|
+
type: admonitionNodeType,
|
|
115
|
+
data: {
|
|
116
|
+
// hName/hProperties come from HAST
|
|
117
|
+
// See https://github.com/syntax-tree/mdast-util-to-hast#fields-on-nodes
|
|
118
|
+
hName: 'admonition',
|
|
119
|
+
hProperties: {
|
|
120
|
+
...(title && isSimpleTextTitle && {title}),
|
|
121
|
+
type: keyword,
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
children: [
|
|
125
|
+
// For titles containing MDX syntax: create a custom element. The theme
|
|
126
|
+
// component will extract it and render it nicely.
|
|
127
|
+
//
|
|
128
|
+
// Temporary workaround, because it's complex in MDX v1 to emit
|
|
129
|
+
// interpolated JSX prop syntax (title={<>my <code>title</code></>}).
|
|
130
|
+
// For this reason, we use children instead of the title prop.
|
|
131
|
+
title &&
|
|
132
|
+
!isSimpleTextTitle && {
|
|
133
|
+
type: admonitionNodeType,
|
|
134
|
+
data: {
|
|
135
|
+
hName: 'mdxAdmonitionTitle',
|
|
136
|
+
hProperties: {},
|
|
137
|
+
},
|
|
138
|
+
children: titleNodes,
|
|
139
|
+
},
|
|
140
|
+
...contentNodes,
|
|
141
|
+
].filter(Boolean),
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
return add(element);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// add tokenizer to parser after fenced code blocks
|
|
148
|
+
const Parser = this.Parser.prototype;
|
|
149
|
+
Parser.blockTokenizers.admonition = blockTokenizer;
|
|
150
|
+
Parser.blockMethods.splice(
|
|
151
|
+
Parser.blockMethods.indexOf('fencedCode') + 1,
|
|
152
|
+
0,
|
|
153
|
+
'admonition',
|
|
154
|
+
);
|
|
155
|
+
Parser.interruptParagraph.splice(
|
|
156
|
+
Parser.interruptParagraph.indexOf('fencedCode') + 1,
|
|
157
|
+
0,
|
|
158
|
+
['admonition'],
|
|
159
|
+
);
|
|
160
|
+
Parser.interruptList.splice(
|
|
161
|
+
Parser.interruptList.indexOf('fencedCode') + 1,
|
|
162
|
+
0,
|
|
163
|
+
['admonition'],
|
|
164
|
+
);
|
|
165
|
+
Parser.interruptBlockquote.splice(
|
|
166
|
+
Parser.interruptBlockquote.indexOf('fencedCode') + 1,
|
|
167
|
+
0,
|
|
168
|
+
['admonition'],
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
return (root) => {
|
|
172
|
+
// escape everything except admonitionHTML nodes
|
|
173
|
+
visit(
|
|
174
|
+
root,
|
|
175
|
+
(node: unknown): node is Literal =>
|
|
176
|
+
(node as Literal | undefined)?.type !== admonitionNodeType,
|
|
177
|
+
(node: Literal) => {
|
|
178
|
+
if (node.value) {
|
|
179
|
+
node.value = node.value.replace(escapeTag, options.tag);
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
export default plugin;
|
|
@@ -21,6 +21,7 @@ import visit from 'unist-util-visit';
|
|
|
21
21
|
import escapeHtml from 'escape-html';
|
|
22
22
|
import sizeOf from 'image-size';
|
|
23
23
|
import type {Transformer} from 'unified';
|
|
24
|
+
import type {Parent} from 'unist';
|
|
24
25
|
import type {Image, Literal} from 'mdast';
|
|
25
26
|
|
|
26
27
|
const {
|
|
@@ -36,12 +37,13 @@ type Context = PluginOptions & {
|
|
|
36
37
|
filePath: string;
|
|
37
38
|
};
|
|
38
39
|
|
|
40
|
+
type Target = [node: Image, index: number, parent: Parent];
|
|
41
|
+
|
|
39
42
|
async function toImageRequireNode(
|
|
40
|
-
node:
|
|
43
|
+
[node, index, parent]: Target,
|
|
41
44
|
imagePath: string,
|
|
42
45
|
filePath: string,
|
|
43
46
|
) {
|
|
44
|
-
const jsxNode = node as Literal & Partial<Image>;
|
|
45
47
|
let relativeImagePath = posixPath(
|
|
46
48
|
path.relative(path.dirname(filePath), imagePath),
|
|
47
49
|
);
|
|
@@ -75,12 +77,12 @@ ${(err as Error).message}`;
|
|
|
75
77
|
}
|
|
76
78
|
}
|
|
77
79
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
const jsxNode: Literal = {
|
|
81
|
+
type: 'jsx',
|
|
82
|
+
value: `<img ${alt}src={${src}}${title}${width}${height} />`,
|
|
83
|
+
};
|
|
81
84
|
|
|
82
|
-
(
|
|
83
|
-
jsxNode.value = `<img ${alt}src={${src}}${title}${width}${height} />`;
|
|
85
|
+
parent.children.splice(index, 1, jsxNode);
|
|
84
86
|
}
|
|
85
87
|
|
|
86
88
|
async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
|
|
@@ -129,7 +131,8 @@ async function getImageAbsolutePath(
|
|
|
129
131
|
return imageFilePath;
|
|
130
132
|
}
|
|
131
133
|
|
|
132
|
-
async function processImageNode(
|
|
134
|
+
async function processImageNode(target: Target, context: Context) {
|
|
135
|
+
const [node] = target;
|
|
133
136
|
if (!node.url) {
|
|
134
137
|
throw new Error(
|
|
135
138
|
`Markdown image URL is mandatory in "${toMessageRelativeFilePath(
|
|
@@ -151,15 +154,18 @@ async function processImageNode(node: Image, context: Context) {
|
|
|
151
154
|
// We try to convert image urls without protocol to images with require calls
|
|
152
155
|
// going through webpack ensures that image assets exist at build time
|
|
153
156
|
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
|
|
154
|
-
await toImageRequireNode(
|
|
157
|
+
await toImageRequireNode(target, imagePath, context.filePath);
|
|
155
158
|
}
|
|
156
159
|
|
|
157
160
|
export default function plugin(options: PluginOptions): Transformer {
|
|
158
161
|
return async (root, vfile) => {
|
|
159
162
|
const promises: Promise<void>[] = [];
|
|
160
|
-
visit(root, 'image', (node: Image) => {
|
|
163
|
+
visit(root, 'image', (node: Image, index, parent) => {
|
|
161
164
|
promises.push(
|
|
162
|
-
processImageNode(node,
|
|
165
|
+
processImageNode([node, index, parent!], {
|
|
166
|
+
...options,
|
|
167
|
+
filePath: vfile.path!,
|
|
168
|
+
}),
|
|
163
169
|
);
|
|
164
170
|
});
|
|
165
171
|
await Promise.all(promises);
|
|
@@ -19,6 +19,7 @@ import visit from 'unist-util-visit';
|
|
|
19
19
|
import escapeHtml from 'escape-html';
|
|
20
20
|
import {stringifyContent} from '../utils';
|
|
21
21
|
import type {Transformer} from 'unified';
|
|
22
|
+
import type {Parent} from 'unist';
|
|
22
23
|
import type {Link, Literal} from 'mdast';
|
|
23
24
|
|
|
24
25
|
const {
|
|
@@ -34,16 +35,20 @@ type Context = PluginOptions & {
|
|
|
34
35
|
filePath: string;
|
|
35
36
|
};
|
|
36
37
|
|
|
38
|
+
type Target = [node: Link, index: number, parent: Parent];
|
|
39
|
+
|
|
37
40
|
/**
|
|
38
41
|
* Transforms the link node to a JSX `<a>` element with a `require()` call.
|
|
39
42
|
*/
|
|
40
|
-
function toAssetRequireNode(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
function toAssetRequireNode(
|
|
44
|
+
[node, index, parent]: Target,
|
|
45
|
+
assetPath: string,
|
|
46
|
+
filePath: string,
|
|
47
|
+
) {
|
|
45
48
|
// require("assets/file.pdf") means requiring from a package called assets
|
|
46
|
-
relativeAssetPath = `./${
|
|
49
|
+
const relativeAssetPath = `./${posixPath(
|
|
50
|
+
path.relative(path.dirname(filePath), assetPath),
|
|
51
|
+
)}`;
|
|
47
52
|
|
|
48
53
|
const parsedUrl = url.parse(node.url);
|
|
49
54
|
const hash = parsedUrl.hash ?? '';
|
|
@@ -60,12 +65,12 @@ function toAssetRequireNode(node: Link, assetPath: string, filePath: string) {
|
|
|
60
65
|
const children = stringifyContent(node);
|
|
61
66
|
const title = node.title ? ` title="${escapeHtml(node.title)}"` : '';
|
|
62
67
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
68
|
+
const jsxNode: Literal = {
|
|
69
|
+
type: 'jsx',
|
|
70
|
+
value: `<a target="_blank" href={${href}}${title}>${children}</a>`,
|
|
71
|
+
};
|
|
66
72
|
|
|
67
|
-
(
|
|
68
|
-
jsxNode.value = `<a target="_blank" href={${href}}${title}>${children}</a>`;
|
|
73
|
+
parent.children.splice(index, 1, jsxNode);
|
|
69
74
|
}
|
|
70
75
|
|
|
71
76
|
async function ensureAssetFileExist(assetPath: string, sourceFilePath: string) {
|
|
@@ -106,7 +111,8 @@ async function getAssetAbsolutePath(
|
|
|
106
111
|
return null;
|
|
107
112
|
}
|
|
108
113
|
|
|
109
|
-
async function processLinkNode(
|
|
114
|
+
async function processLinkNode(target: Target, context: Context) {
|
|
115
|
+
const [node] = target;
|
|
110
116
|
if (!node.url) {
|
|
111
117
|
// Try to improve error feedback
|
|
112
118
|
// see https://github.com/facebook/docusaurus/issues/3309#issuecomment-690371675
|
|
@@ -138,15 +144,20 @@ async function processLinkNode(node: Link, context: Context) {
|
|
|
138
144
|
context,
|
|
139
145
|
);
|
|
140
146
|
if (assetPath) {
|
|
141
|
-
toAssetRequireNode(
|
|
147
|
+
toAssetRequireNode(target, assetPath, context.filePath);
|
|
142
148
|
}
|
|
143
149
|
}
|
|
144
150
|
|
|
145
151
|
export default function plugin(options: PluginOptions): Transformer {
|
|
146
152
|
return async (root, vfile) => {
|
|
147
153
|
const promises: Promise<void>[] = [];
|
|
148
|
-
visit(root, 'link', (node: Link) => {
|
|
149
|
-
promises.push(
|
|
154
|
+
visit(root, 'link', (node: Link, index, parent) => {
|
|
155
|
+
promises.push(
|
|
156
|
+
processLinkNode([node, index, parent!], {
|
|
157
|
+
...options,
|
|
158
|
+
filePath: vfile.path!,
|
|
159
|
+
}),
|
|
160
|
+
);
|
|
150
161
|
});
|
|
151
162
|
await Promise.all(promises);
|
|
152
163
|
};
|