@docusaurus/mdx-loader 0.0.0-5569 → 0.0.0-5574
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/format.d.ts +12 -0
- package/lib/format.d.ts.map +1 -0
- package/lib/format.js +35 -0
- package/lib/format.js.map +1 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/loader.d.ts +1 -8
- package/lib/loader.d.ts.map +1 -1
- package/lib/loader.js +14 -125
- package/lib/loader.js.map +1 -1
- package/lib/processor.d.ts +36 -0
- package/lib/processor.d.ts.map +1 -0
- package/lib/processor.js +158 -0
- package/lib/processor.js.map +1 -0
- package/package.json +7 -6
- package/src/format.ts +40 -0
- package/src/index.ts +3 -1
- package/src/loader.ts +18 -167
- package/src/processor.ts +241 -0
package/lib/format.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
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 { MDXFrontMatter } from './frontMatter';
|
|
8
|
+
export declare function getFormat({ filePath, frontMatterFormat, }: {
|
|
9
|
+
filePath: string;
|
|
10
|
+
frontMatterFormat: MDXFrontMatter['format'];
|
|
11
|
+
}): 'md' | 'mdx';
|
|
12
|
+
//# sourceMappingURL=format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.d.ts","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,eAAe,CAAC;AAmBlD,wBAAgB,SAAS,CAAC,EACxB,QAAQ,EACR,iBAAiB,GAClB,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,iBAAiB,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAC;CAC7C,GAAG,IAAI,GAAG,KAAK,CAMf"}
|
package/lib/format.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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.getFormat = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const path_1 = tslib_1.__importDefault(require("path"));
|
|
12
|
+
// Copied from https://mdxjs.com/packages/mdx/#optionsmdextensions
|
|
13
|
+
// Although we are likely to only use .md / .mdx anyway...
|
|
14
|
+
const mdFormatExtensions = [
|
|
15
|
+
'.md',
|
|
16
|
+
'.markdown',
|
|
17
|
+
'.mdown',
|
|
18
|
+
'.mkdn',
|
|
19
|
+
'.mkd',
|
|
20
|
+
'.mdwn',
|
|
21
|
+
'.mkdown',
|
|
22
|
+
'.ron',
|
|
23
|
+
];
|
|
24
|
+
function isMDFormat(filepath) {
|
|
25
|
+
return mdFormatExtensions.includes(path_1.default.extname(filepath));
|
|
26
|
+
}
|
|
27
|
+
function getFormat({ filePath, frontMatterFormat, }) {
|
|
28
|
+
if (frontMatterFormat !== 'detect') {
|
|
29
|
+
return frontMatterFormat;
|
|
30
|
+
}
|
|
31
|
+
// Bias toward mdx if unknown extension
|
|
32
|
+
return isMDFormat(filePath) ? 'md' : 'mdx';
|
|
33
|
+
}
|
|
34
|
+
exports.getFormat = getFormat;
|
|
35
|
+
//# sourceMappingURL=format.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format.js","sourceRoot":"","sources":["../src/format.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wDAAwB;AAGxB,kEAAkE;AAClE,0DAA0D;AAC1D,MAAM,kBAAkB,GAAG;IACzB,KAAK;IACL,WAAW;IACX,QAAQ;IACR,OAAO;IACP,MAAM;IACN,OAAO;IACP,SAAS;IACT,MAAM;CACP,CAAC;AAEF,SAAS,UAAU,CAAC,QAAgB;IAClC,OAAO,kBAAkB,CAAC,QAAQ,CAAC,cAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;AAC7D,CAAC;AAED,SAAgB,SAAS,CAAC,EACxB,QAAQ,EACR,iBAAiB,GAIlB;IACC,IAAI,iBAAiB,KAAK,QAAQ,EAAE;QAClC,OAAO,iBAAiB,CAAC;KAC1B;IACD,uCAAuC;IACvC,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;AAC7C,CAAC;AAZD,8BAYC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -25,5 +25,6 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
|
|
25
25
|
readonly assets: Assets;
|
|
26
26
|
(): JSX.Element;
|
|
27
27
|
};
|
|
28
|
-
export type { Options, MDXPlugin
|
|
28
|
+
export type { Options, MDXPlugin } from './loader';
|
|
29
|
+
export type { MDXOptions } from './processor';
|
|
29
30
|
//# sourceMappingURL=index.d.ts.map
|
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;AAEH,OAAO,EAAC,SAAS,EAAC,MAAM,UAAU,CAAC;AAEnC,OAAO,KAAK,EAAC,OAAO,IAAI,eAAe,EAAC,MAAM,cAAc,CAAC;AAE7D,eAAe,SAAS,CAAC;AAEzB,MAAM,MAAM,OAAO,GAAG,eAAe,CAAC;AAEtC,MAAM,MAAM,gBAAgB,CAAC,WAAW,EAAE,QAAQ,EAAE,MAAM,GAAG,SAAS,IAAI;IACxE,gDAAgD;IAChD,QAAQ,CAA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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
|
+
import type { MDXOptions } from './processor';
|
|
7
8
|
import type { MarkdownConfig } from '@docusaurus/types';
|
|
8
9
|
import type { LoaderContext } from 'webpack';
|
|
9
|
-
import type { AdmonitionOptions } from './remark/admonitions';
|
|
10
10
|
type Pluggable = any;
|
|
11
11
|
export type MDXPlugin = Pluggable;
|
|
12
|
-
export type MDXOptions = {
|
|
13
|
-
admonitions: boolean | Partial<AdmonitionOptions>;
|
|
14
|
-
remarkPlugins: MDXPlugin[];
|
|
15
|
-
rehypePlugins: MDXPlugin[];
|
|
16
|
-
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
17
|
-
beforeDefaultRehypePlugins: MDXPlugin[];
|
|
18
|
-
};
|
|
19
12
|
export type Options = Partial<MDXOptions> & {
|
|
20
13
|
markdownConfig: MarkdownConfig;
|
|
21
14
|
staticDirs: string[];
|
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;AAcH,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,aAAa,CAAC;AAE5C,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,SAAS,CAAC;AAK3C,KAAK,SAAS,GAAG,GAAG,CAAC;AAMrB,MAAM,MAAM,SAAS,GAAG,SAAS,CAAC;AAElC,MAAM,MAAM,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,GAAG;IAC1C,cAAc,EAAE,cAAc,CAAC;IAC/B,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;CAChC,CAAC;AA4EF,wBAAsB,SAAS,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAiHf"}
|
package/lib/loader.js
CHANGED
|
@@ -9,46 +9,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
exports.mdxLoader = void 0;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
12
|
-
const path_1 = tslib_1.__importDefault(require("path"));
|
|
13
12
|
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
14
13
|
const utils_1 = require("@docusaurus/utils");
|
|
15
|
-
const remark_emoji_1 = tslib_1.__importDefault(require("remark-emoji"));
|
|
16
14
|
const stringify_object_1 = tslib_1.__importDefault(require("stringify-object"));
|
|
17
15
|
const preprocessor_1 = tslib_1.__importDefault(require("./preprocessor"));
|
|
18
|
-
const headings_1 = tslib_1.__importDefault(require("./remark/headings"));
|
|
19
|
-
const toc_1 = tslib_1.__importDefault(require("./remark/toc"));
|
|
20
|
-
const transformImage_1 = tslib_1.__importDefault(require("./remark/transformImage"));
|
|
21
|
-
const transformLinks_1 = tslib_1.__importDefault(require("./remark/transformLinks"));
|
|
22
|
-
const details_1 = tslib_1.__importDefault(require("./remark/details"));
|
|
23
|
-
const head_1 = tslib_1.__importDefault(require("./remark/head"));
|
|
24
|
-
const mermaid_1 = tslib_1.__importDefault(require("./remark/mermaid"));
|
|
25
|
-
const admonitions_1 = tslib_1.__importDefault(require("./remark/admonitions"));
|
|
26
|
-
const codeCompatPlugin_1 = tslib_1.__importDefault(require("./remark/mdx1Compat/codeCompatPlugin"));
|
|
27
16
|
const frontMatter_1 = require("./frontMatter");
|
|
28
|
-
|
|
29
|
-
// Although we are likely to only use .md / .mdx anyway...
|
|
30
|
-
const mdFormatExtensions = [
|
|
31
|
-
'.md',
|
|
32
|
-
'.markdown',
|
|
33
|
-
'.mdown',
|
|
34
|
-
'.mkdn',
|
|
35
|
-
'.mkd',
|
|
36
|
-
'.mdwn',
|
|
37
|
-
'.mkdown',
|
|
38
|
-
'.ron',
|
|
39
|
-
];
|
|
40
|
-
function isMDFormat(filepath) {
|
|
41
|
-
return mdFormatExtensions.includes(path_1.default.extname(filepath));
|
|
42
|
-
}
|
|
17
|
+
const processor_1 = require("./processor");
|
|
43
18
|
const { loaders: { inlineMarkdownImageFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
|
|
44
|
-
const DEFAULT_OPTIONS = {
|
|
45
|
-
admonitions: true,
|
|
46
|
-
rehypePlugins: [],
|
|
47
|
-
remarkPlugins: [remark_emoji_1.default, headings_1.default, toc_1.default],
|
|
48
|
-
beforeDefaultRemarkPlugins: [],
|
|
49
|
-
beforeDefaultRehypePlugins: [],
|
|
50
|
-
};
|
|
51
|
-
const compilerCache = new Map();
|
|
52
19
|
/**
|
|
53
20
|
* When this throws, it generally means that there's no metadata file associated
|
|
54
21
|
* with this MDX document. It can happen when using MDX partials (usually
|
|
@@ -103,15 +70,6 @@ function createAssetsExportCode(assets) {
|
|
|
103
70
|
.filter(Boolean);
|
|
104
71
|
return `{\n${codeLines.join('\n')}\n}`;
|
|
105
72
|
}
|
|
106
|
-
function getAdmonitionsPlugins(admonitionsOption) {
|
|
107
|
-
if (admonitionsOption) {
|
|
108
|
-
const plugin = admonitionsOption === true
|
|
109
|
-
? admonitions_1.default
|
|
110
|
-
: [admonitions_1.default, admonitionsOption];
|
|
111
|
-
return [plugin];
|
|
112
|
-
}
|
|
113
|
-
return [];
|
|
114
|
-
}
|
|
115
73
|
// TODO temporary, remove this after v3.1?
|
|
116
74
|
// Some plugin authors use our mdx-loader, despite it not being public API
|
|
117
75
|
// see https://github.com/facebook/docusaurus/issues/8298
|
|
@@ -124,11 +82,8 @@ async function mdxLoader(fileString) {
|
|
|
124
82
|
const callback = this.async();
|
|
125
83
|
const filePath = this.resourcePath;
|
|
126
84
|
const reqOptions = this.getOptions();
|
|
85
|
+
const { query } = this;
|
|
127
86
|
ensureMarkdownConfig(reqOptions);
|
|
128
|
-
const { createProcessor } = await import('@mdx-js/mdx');
|
|
129
|
-
const { default: gfm } = await import('remark-gfm');
|
|
130
|
-
const { default: comment } = await import('remark-comment');
|
|
131
|
-
const { default: directives } = await import('remark-directive');
|
|
132
87
|
const { frontMatter, content: contentWithTitle } = (0, utils_1.parseFrontMatter)(fileString);
|
|
133
88
|
const mdxFrontMatter = (0, frontMatter_1.validateMDXFrontMatter)(frontMatter.mdx);
|
|
134
89
|
const { content: contentUnprocessed, contentTitle } = (0, utils_1.parseMarkdownContentTitle)(contentWithTitle, {
|
|
@@ -141,82 +96,15 @@ async function mdxLoader(fileString) {
|
|
|
141
96
|
markdownConfig: reqOptions.markdownConfig,
|
|
142
97
|
});
|
|
143
98
|
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
const remarkPlugins = [
|
|
151
|
-
...(reqOptions.beforeDefaultRemarkPlugins ?? []),
|
|
152
|
-
directives,
|
|
153
|
-
...getAdmonitionsPlugins(reqOptions.admonitions ?? false),
|
|
154
|
-
...DEFAULT_OPTIONS.remarkPlugins,
|
|
155
|
-
details_1.default,
|
|
156
|
-
head_1.default,
|
|
157
|
-
...(reqOptions.markdownConfig.mermaid ? [mermaid_1.default] : []),
|
|
158
|
-
[
|
|
159
|
-
transformImage_1.default,
|
|
160
|
-
{
|
|
161
|
-
staticDirs: reqOptions.staticDirs,
|
|
162
|
-
siteDir: reqOptions.siteDir,
|
|
163
|
-
},
|
|
164
|
-
],
|
|
165
|
-
[
|
|
166
|
-
transformLinks_1.default,
|
|
167
|
-
{
|
|
168
|
-
staticDirs: reqOptions.staticDirs,
|
|
169
|
-
siteDir: reqOptions.siteDir,
|
|
170
|
-
},
|
|
171
|
-
],
|
|
172
|
-
gfm,
|
|
173
|
-
reqOptions.markdownConfig.mdx1Compat.comments ? comment : null,
|
|
174
|
-
...(reqOptions.remarkPlugins ?? []),
|
|
175
|
-
].filter((plugin) => Boolean(plugin));
|
|
176
|
-
// codeCompatPlugin needs to be applied last after user-provided plugins
|
|
177
|
-
// (after npm2yarn for example)
|
|
178
|
-
remarkPlugins.push(codeCompatPlugin_1.default);
|
|
179
|
-
const rehypePlugins = [
|
|
180
|
-
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
181
|
-
...DEFAULT_OPTIONS.rehypePlugins,
|
|
182
|
-
...(reqOptions.rehypePlugins ?? []),
|
|
183
|
-
];
|
|
184
|
-
const options = {
|
|
185
|
-
...reqOptions,
|
|
186
|
-
remarkPlugins,
|
|
187
|
-
rehypePlugins,
|
|
188
|
-
providerImportSource: '@mdx-js/react',
|
|
189
|
-
};
|
|
190
|
-
const compilerCacheEntry = {
|
|
191
|
-
mdCompiler: createProcessor({
|
|
192
|
-
...options,
|
|
193
|
-
format: 'md',
|
|
194
|
-
}),
|
|
195
|
-
mdxCompiler: createProcessor({
|
|
196
|
-
...options,
|
|
197
|
-
format: 'mdx',
|
|
198
|
-
}),
|
|
199
|
-
options,
|
|
200
|
-
};
|
|
201
|
-
compilerCache.set(this.query, compilerCacheEntry);
|
|
202
|
-
}
|
|
203
|
-
const { mdCompiler, mdxCompiler, options } = compilerCache.get(this.query);
|
|
204
|
-
function getCompiler() {
|
|
205
|
-
const format = mdxFrontMatter.format === 'detect'
|
|
206
|
-
? isMDFormat(filePath)
|
|
207
|
-
? 'md'
|
|
208
|
-
: 'mdx'
|
|
209
|
-
: mdxFrontMatter.format;
|
|
210
|
-
return format === 'md' ? mdCompiler : mdxCompiler;
|
|
211
|
-
}
|
|
99
|
+
const processor = await (0, processor_1.createProcessorCached)({
|
|
100
|
+
filePath,
|
|
101
|
+
reqOptions,
|
|
102
|
+
query,
|
|
103
|
+
mdxFrontMatter,
|
|
104
|
+
});
|
|
212
105
|
let result;
|
|
213
106
|
try {
|
|
214
|
-
result = await
|
|
215
|
-
.process({
|
|
216
|
-
value: content,
|
|
217
|
-
path: filePath,
|
|
218
|
-
})
|
|
219
|
-
.then((res) => res.toString());
|
|
107
|
+
result = await processor.process({ content, filePath });
|
|
220
108
|
}
|
|
221
109
|
catch (errorUnknown) {
|
|
222
110
|
const error = errorUnknown;
|
|
@@ -226,13 +114,13 @@ async function mdxLoader(fileString) {
|
|
|
226
114
|
}
|
|
227
115
|
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
228
116
|
// Partial are not expected to have associated metadata files or front matter
|
|
229
|
-
const isMDXPartial =
|
|
117
|
+
const isMDXPartial = reqOptions.isMDXPartial?.(filePath);
|
|
230
118
|
if (isMDXPartial && hasFrontMatter) {
|
|
231
119
|
const errorMessage = `Docusaurus MDX partial files should not contain front matter.
|
|
232
120
|
Those partial files use the _ prefix as a convention by default, but this is configurable.
|
|
233
121
|
File at ${filePath} contains front matter that will be ignored:
|
|
234
122
|
${JSON.stringify(frontMatter, null, 2)}`;
|
|
235
|
-
if (!
|
|
123
|
+
if (!reqOptions.isMDXPartialFrontMatterWarningDisabled) {
|
|
236
124
|
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
|
237
125
|
if (shouldError) {
|
|
238
126
|
return callback(new Error(errorMessage));
|
|
@@ -243,8 +131,9 @@ ${JSON.stringify(frontMatter, null, 2)}`;
|
|
|
243
131
|
function getMetadataPath() {
|
|
244
132
|
if (!isMDXPartial) {
|
|
245
133
|
// Read metadata for this MDX and export it.
|
|
246
|
-
if (
|
|
247
|
-
|
|
134
|
+
if (reqOptions.metadataPath &&
|
|
135
|
+
typeof reqOptions.metadataPath === 'function') {
|
|
136
|
+
return reqOptions.metadataPath(filePath);
|
|
248
137
|
}
|
|
249
138
|
}
|
|
250
139
|
return undefined;
|
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,
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,gEAA0B;AAC1B,wEAAwC;AACxC,6CAK2B;AAC3B,gFAA+C;AAC/C,0EAA0C;AAC1C,+CAAqD;AACrD,2CAAkD;AAWlD,MAAM,EACJ,OAAO,EAAE,EAAC,6BAA6B,EAAC,GACzC,GAAG,IAAA,0BAAkB,GAAE,CAAC;AAkBzB;;;;;GAKG;AACH,KAAK,UAAU,gBAAgB,CAAC,YAAoB;IAClD,IAAI;QACF,OAAO,MAAM,kBAAE,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;KAChD;IAAC,OAAO,GAAG,EAAE;QACZ,gBAAM,CAAC,KAAK,CAAA,gDAAgD,YAAY,4DAA4D,CAAC;QACrI,MAAM,GAAG,CAAC;KACX;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,sBAAsB,CAAC,MAAe;IAC7C,IACE,OAAO,MAAM,KAAK,QAAQ;QAC1B,C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AI,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,4EAA4E;IAC5E,6BAA6B;IAC7B,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,WAAW;EACX,MAAM;CACP,CAAC;IAEA,OAAO,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAC9B,CAAC;AApHD,8BAoHC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
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 { MDXFrontMatter } from './frontMatter';
|
|
8
|
+
import type { Options } from './loader';
|
|
9
|
+
import type { AdmonitionOptions } from './remark/admonitions';
|
|
10
|
+
type Pluggable = any;
|
|
11
|
+
type SimpleProcessor = {
|
|
12
|
+
process: ({ content, filePath, }: {
|
|
13
|
+
content: string;
|
|
14
|
+
filePath: string;
|
|
15
|
+
}) => Promise<string>;
|
|
16
|
+
};
|
|
17
|
+
export type MDXPlugin = Pluggable;
|
|
18
|
+
export type MDXOptions = {
|
|
19
|
+
admonitions: boolean | Partial<AdmonitionOptions>;
|
|
20
|
+
remarkPlugins: MDXPlugin[];
|
|
21
|
+
rehypePlugins: MDXPlugin[];
|
|
22
|
+
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
23
|
+
beforeDefaultRehypePlugins: MDXPlugin[];
|
|
24
|
+
};
|
|
25
|
+
export declare function createProcessorUncached(parameters: {
|
|
26
|
+
options: Options;
|
|
27
|
+
format: 'md' | 'mdx';
|
|
28
|
+
}): Promise<SimpleProcessor>;
|
|
29
|
+
export declare function createProcessorCached({ filePath, mdxFrontMatter, query, reqOptions, }: {
|
|
30
|
+
filePath: string;
|
|
31
|
+
mdxFrontMatter: MDXFrontMatter;
|
|
32
|
+
query: string | Options;
|
|
33
|
+
reqOptions: Options;
|
|
34
|
+
}): Promise<SimpleProcessor>;
|
|
35
|
+
export {};
|
|
36
|
+
//# sourceMappingURL=processor.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../src/processor.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,eAAe,CAAC;AAClD,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,UAAU,CAAC;AACtC,OAAO,KAAK,EAAC,iBAAiB,EAAC,MAAM,sBAAsB,CAAC;AAQ5D,KAAK,SAAS,GAAG,GAAG,CAAC;AAGrB,KAAK,eAAe,GAAG;IACrB,OAAO,EAAE,CAAC,EACR,OAAO,EACP,QAAQ,GACT,EAAE;QACD,OAAO,EAAE,MAAM,CAAC;QAChB,QAAQ,EAAE,MAAM,CAAC;KAClB,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;CACvB,CAAC;AAUF,MAAM,MAAM,SAAS,GAAG,SAAS,CAAC;AAElC,MAAM,MAAM,UAAU,GAAG;IACvB,WAAW,EAAE,OAAO,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;IAClD,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;AAoHF,wBAAsB,uBAAuB,CAAC,UAAU,EAAE;IACxD,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,IAAI,GAAG,KAAK,CAAC;CACtB,GAAG,OAAO,CAAC,eAAe,CAAC,CAG3B;AA0CD,wBAAsB,qBAAqB,CAAC,EAC1C,QAAQ,EACR,cAAc,EACd,KAAK,EACL,UAAU,GACX,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,cAAc,CAAC;IAC/B,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;IACxB,UAAU,EAAE,OAAO,CAAC;CACrB,GAAG,OAAO,CAAC,eAAe,CAAC,CAS3B"}
|
package/lib/processor.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
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.createProcessorCached = exports.createProcessorUncached = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const remark_emoji_1 = tslib_1.__importDefault(require("remark-emoji"));
|
|
12
|
+
const headings_1 = tslib_1.__importDefault(require("./remark/headings"));
|
|
13
|
+
const toc_1 = tslib_1.__importDefault(require("./remark/toc"));
|
|
14
|
+
const transformImage_1 = tslib_1.__importDefault(require("./remark/transformImage"));
|
|
15
|
+
const transformLinks_1 = tslib_1.__importDefault(require("./remark/transformLinks"));
|
|
16
|
+
const details_1 = tslib_1.__importDefault(require("./remark/details"));
|
|
17
|
+
const head_1 = tslib_1.__importDefault(require("./remark/head"));
|
|
18
|
+
const mermaid_1 = tslib_1.__importDefault(require("./remark/mermaid"));
|
|
19
|
+
const admonitions_1 = tslib_1.__importDefault(require("./remark/admonitions"));
|
|
20
|
+
const codeCompatPlugin_1 = tslib_1.__importDefault(require("./remark/mdx1Compat/codeCompatPlugin"));
|
|
21
|
+
const format_1 = require("./format");
|
|
22
|
+
const DEFAULT_OPTIONS = {
|
|
23
|
+
admonitions: true,
|
|
24
|
+
rehypePlugins: [],
|
|
25
|
+
remarkPlugins: [remark_emoji_1.default, headings_1.default, toc_1.default],
|
|
26
|
+
beforeDefaultRemarkPlugins: [],
|
|
27
|
+
beforeDefaultRehypePlugins: [],
|
|
28
|
+
};
|
|
29
|
+
function getAdmonitionsPlugins(admonitionsOption) {
|
|
30
|
+
if (admonitionsOption) {
|
|
31
|
+
const plugin = admonitionsOption === true
|
|
32
|
+
? admonitions_1.default
|
|
33
|
+
: [admonitions_1.default, admonitionsOption];
|
|
34
|
+
return [plugin];
|
|
35
|
+
}
|
|
36
|
+
return [];
|
|
37
|
+
}
|
|
38
|
+
// Need to be async due to ESM dynamic imports...
|
|
39
|
+
async function createProcessorFactory() {
|
|
40
|
+
const { createProcessor: createMdxProcessor } = await import('@mdx-js/mdx');
|
|
41
|
+
const { default: rehypeRaw } = await import('rehype-raw');
|
|
42
|
+
const { default: gfm } = await import('remark-gfm');
|
|
43
|
+
const { default: comment } = await import('remark-comment');
|
|
44
|
+
const { default: directive } = await import('remark-directive');
|
|
45
|
+
// /!\ this method is synchronous on purpose
|
|
46
|
+
// Using async code here can create cache entry race conditions!
|
|
47
|
+
function createProcessorSync({ options, format, }) {
|
|
48
|
+
const remarkPlugins = [
|
|
49
|
+
...(options.beforeDefaultRemarkPlugins ?? []),
|
|
50
|
+
directive,
|
|
51
|
+
...getAdmonitionsPlugins(options.admonitions ?? false),
|
|
52
|
+
...DEFAULT_OPTIONS.remarkPlugins,
|
|
53
|
+
details_1.default,
|
|
54
|
+
head_1.default,
|
|
55
|
+
...(options.markdownConfig.mermaid ? [mermaid_1.default] : []),
|
|
56
|
+
[
|
|
57
|
+
transformImage_1.default,
|
|
58
|
+
{
|
|
59
|
+
staticDirs: options.staticDirs,
|
|
60
|
+
siteDir: options.siteDir,
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
transformLinks_1.default,
|
|
65
|
+
{
|
|
66
|
+
staticDirs: options.staticDirs,
|
|
67
|
+
siteDir: options.siteDir,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
gfm,
|
|
71
|
+
options.markdownConfig.mdx1Compat.comments ? comment : null,
|
|
72
|
+
...(options.remarkPlugins ?? []),
|
|
73
|
+
].filter((plugin) => Boolean(plugin));
|
|
74
|
+
// codeCompatPlugin needs to be applied last after user-provided plugins
|
|
75
|
+
// (after npm2yarn for example)
|
|
76
|
+
remarkPlugins.push(codeCompatPlugin_1.default);
|
|
77
|
+
const rehypePlugins = [
|
|
78
|
+
...(options.beforeDefaultRehypePlugins ?? []),
|
|
79
|
+
...DEFAULT_OPTIONS.rehypePlugins,
|
|
80
|
+
...(options.rehypePlugins ?? []),
|
|
81
|
+
];
|
|
82
|
+
if (format === 'md') {
|
|
83
|
+
// This is what permits to embed HTML elements with format 'md'
|
|
84
|
+
// See https://github.com/facebook/docusaurus/pull/8960
|
|
85
|
+
// See https://github.com/mdx-js/mdx/pull/2295#issuecomment-1540085960
|
|
86
|
+
const rehypeRawPlugin = [
|
|
87
|
+
rehypeRaw,
|
|
88
|
+
{
|
|
89
|
+
passThrough: [
|
|
90
|
+
'mdxFlowExpression',
|
|
91
|
+
'mdxJsxFlowElement',
|
|
92
|
+
'mdxJsxTextElement',
|
|
93
|
+
'mdxTextExpression',
|
|
94
|
+
'mdxjsEsm',
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
];
|
|
98
|
+
rehypePlugins.unshift(rehypeRawPlugin);
|
|
99
|
+
}
|
|
100
|
+
const processorOptions = {
|
|
101
|
+
...options,
|
|
102
|
+
remarkPlugins,
|
|
103
|
+
rehypePlugins,
|
|
104
|
+
providerImportSource: '@mdx-js/react',
|
|
105
|
+
};
|
|
106
|
+
const mdxProcessor = createMdxProcessor({
|
|
107
|
+
...processorOptions,
|
|
108
|
+
format,
|
|
109
|
+
});
|
|
110
|
+
return {
|
|
111
|
+
process: async ({ content, filePath }) => mdxProcessor
|
|
112
|
+
.process({
|
|
113
|
+
value: content,
|
|
114
|
+
path: filePath,
|
|
115
|
+
})
|
|
116
|
+
.then((res) => res.toString()),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
return { createProcessorSync };
|
|
120
|
+
}
|
|
121
|
+
// Will be useful for tests
|
|
122
|
+
async function createProcessorUncached(parameters) {
|
|
123
|
+
const { createProcessorSync } = await createProcessorFactory();
|
|
124
|
+
return createProcessorSync(parameters);
|
|
125
|
+
}
|
|
126
|
+
exports.createProcessorUncached = createProcessorUncached;
|
|
127
|
+
// Compilers are cached so that Remark/Rehype plugins can run
|
|
128
|
+
// expensive code during initialization
|
|
129
|
+
const ProcessorsCache = new Map();
|
|
130
|
+
async function createProcessorsCacheEntry({ query, reqOptions, }) {
|
|
131
|
+
const { createProcessorSync } = await createProcessorFactory();
|
|
132
|
+
const compilers = ProcessorsCache.get(query);
|
|
133
|
+
if (compilers) {
|
|
134
|
+
return compilers;
|
|
135
|
+
}
|
|
136
|
+
const compilerCacheEntry = {
|
|
137
|
+
mdProcessor: createProcessorSync({
|
|
138
|
+
options: reqOptions,
|
|
139
|
+
format: 'md',
|
|
140
|
+
}),
|
|
141
|
+
mdxProcessor: createProcessorSync({
|
|
142
|
+
options: reqOptions,
|
|
143
|
+
format: 'mdx',
|
|
144
|
+
}),
|
|
145
|
+
};
|
|
146
|
+
ProcessorsCache.set(query, compilerCacheEntry);
|
|
147
|
+
return compilerCacheEntry;
|
|
148
|
+
}
|
|
149
|
+
async function createProcessorCached({ filePath, mdxFrontMatter, query, reqOptions, }) {
|
|
150
|
+
const compilers = await createProcessorsCacheEntry({ query, reqOptions });
|
|
151
|
+
const format = (0, format_1.getFormat)({
|
|
152
|
+
filePath,
|
|
153
|
+
frontMatterFormat: mdxFrontMatter.format,
|
|
154
|
+
});
|
|
155
|
+
return format === 'md' ? compilers.mdProcessor : compilers.mdxProcessor;
|
|
156
|
+
}
|
|
157
|
+
exports.createProcessorCached = createProcessorCached;
|
|
158
|
+
//# sourceMappingURL=processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"processor.js","sourceRoot":"","sources":["../src/processor.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,wEAAiC;AACjC,yEAAyC;AACzC,+DAA+B;AAC/B,qFAAqD;AACrD,qFAAqD;AACrD,uEAAuC;AACvC,iEAAiC;AACjC,uEAAuC;AACvC,+EAAwD;AACxD,oGAAoE;AACpE,qCAAmC;AAwBnC,MAAM,eAAe,GAAe;IAClC,WAAW,EAAE,IAAI;IACjB,aAAa,EAAE,EAAE;IACjB,aAAa,EAAE,CAAC,sBAAK,EAAE,kBAAQ,EAAE,aAAG,CAAC;IACrC,0BAA0B,EAAE,EAAE;IAC9B,0BAA0B,EAAE,EAAE;CAC/B,CAAC;AAYF,SAAS,qBAAqB,CAC5B,iBAA4C;IAE5C,IAAI,iBAAiB,EAAE;QACrB,MAAM,MAAM,GACV,iBAAiB,KAAK,IAAI;YACxB,CAAC,CAAC,qBAAoB;YACtB,CAAC,CAAC,CAAC,qBAAoB,EAAE,iBAAiB,CAAC,CAAC;QAChD,OAAO,CAAC,MAAM,CAAC,CAAC;KACjB;IAED,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,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json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/mdx-loader",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-5574",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -20,9 +20,9 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@babel/parser": "^7.21.2",
|
|
22
22
|
"@babel/traverse": "^7.21.2",
|
|
23
|
-
"@docusaurus/logger": "0.0.0-
|
|
24
|
-
"@docusaurus/utils": "0.0.0-
|
|
25
|
-
"@docusaurus/utils-validation": "0.0.0-
|
|
23
|
+
"@docusaurus/logger": "0.0.0-5574",
|
|
24
|
+
"@docusaurus/utils": "0.0.0-5574",
|
|
25
|
+
"@docusaurus/utils-validation": "0.0.0-5574",
|
|
26
26
|
"@mdx-js/mdx": "^2.1.5",
|
|
27
27
|
"escape-html": "^1.0.3",
|
|
28
28
|
"estree-util-value-to-estree": "^2.1.0",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"image-size": "^1.0.2",
|
|
33
33
|
"mdast-util-mdx": "^2.0.0",
|
|
34
34
|
"mdast-util-to-string": "^3.0.0",
|
|
35
|
+
"rehype-raw": "^6.1.1",
|
|
35
36
|
"remark-comment": "^1.0.0",
|
|
36
37
|
"remark-directive": "^2.0.1",
|
|
37
38
|
"remark-emoji": "^2.2.0",
|
|
@@ -44,7 +45,7 @@
|
|
|
44
45
|
"webpack": "^5.76.0"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
|
-
"@docusaurus/types": "0.0.0-
|
|
48
|
+
"@docusaurus/types": "0.0.0-5574",
|
|
48
49
|
"@types/escape-html": "^1.0.2",
|
|
49
50
|
"@types/mdast": "^3.0.10",
|
|
50
51
|
"@types/stringify-object": "^3.3.1",
|
|
@@ -64,5 +65,5 @@
|
|
|
64
65
|
"engines": {
|
|
65
66
|
"node": ">=16.14"
|
|
66
67
|
},
|
|
67
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "e1b202828b53b2ea84d6d884df81e5a20e26b9cd"
|
|
68
69
|
}
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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 path from 'path';
|
|
9
|
+
import type {MDXFrontMatter} from './frontMatter';
|
|
10
|
+
|
|
11
|
+
// Copied from https://mdxjs.com/packages/mdx/#optionsmdextensions
|
|
12
|
+
// Although we are likely to only use .md / .mdx anyway...
|
|
13
|
+
const mdFormatExtensions = [
|
|
14
|
+
'.md',
|
|
15
|
+
'.markdown',
|
|
16
|
+
'.mdown',
|
|
17
|
+
'.mkdn',
|
|
18
|
+
'.mkd',
|
|
19
|
+
'.mdwn',
|
|
20
|
+
'.mkdown',
|
|
21
|
+
'.ron',
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
function isMDFormat(filepath: string) {
|
|
25
|
+
return mdFormatExtensions.includes(path.extname(filepath));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function getFormat({
|
|
29
|
+
filePath,
|
|
30
|
+
frontMatterFormat,
|
|
31
|
+
}: {
|
|
32
|
+
filePath: string;
|
|
33
|
+
frontMatterFormat: MDXFrontMatter['format'];
|
|
34
|
+
}): 'md' | 'mdx' {
|
|
35
|
+
if (frontMatterFormat !== 'detect') {
|
|
36
|
+
return frontMatterFormat;
|
|
37
|
+
}
|
|
38
|
+
// Bias toward mdx if unknown extension
|
|
39
|
+
return isMDFormat(filePath) ? 'md' : 'mdx';
|
|
40
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -30,4 +30,6 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
|
|
30
30
|
readonly assets: Assets;
|
|
31
31
|
(): JSX.Element;
|
|
32
32
|
};
|
|
33
|
-
|
|
33
|
+
|
|
34
|
+
export type {Options, MDXPlugin} from './loader';
|
|
35
|
+
export type {MDXOptions} from './processor';
|
package/src/loader.ts
CHANGED
|
@@ -6,7 +6,6 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import fs from 'fs-extra';
|
|
9
|
-
import path from 'path';
|
|
10
9
|
import logger from '@docusaurus/logger';
|
|
11
10
|
import {
|
|
12
11
|
parseFrontMatter,
|
|
@@ -14,82 +13,26 @@ import {
|
|
|
14
13
|
escapePath,
|
|
15
14
|
getFileLoaderUtils,
|
|
16
15
|
} from '@docusaurus/utils';
|
|
17
|
-
import emoji from 'remark-emoji';
|
|
18
16
|
import stringifyObject from 'stringify-object';
|
|
19
17
|
import preprocessor from './preprocessor';
|
|
20
|
-
import headings from './remark/headings';
|
|
21
|
-
import toc from './remark/toc';
|
|
22
|
-
import transformImage from './remark/transformImage';
|
|
23
|
-
import transformLinks from './remark/transformLinks';
|
|
24
|
-
import details from './remark/details';
|
|
25
|
-
import head from './remark/head';
|
|
26
|
-
import mermaid from './remark/mermaid';
|
|
27
|
-
import transformAdmonitions from './remark/admonitions';
|
|
28
|
-
import codeCompatPlugin from './remark/mdx1Compat/codeCompatPlugin';
|
|
29
18
|
import {validateMDXFrontMatter} from './frontMatter';
|
|
19
|
+
import {createProcessorCached} from './processor';
|
|
20
|
+
import type {MDXOptions} from './processor';
|
|
30
21
|
|
|
31
22
|
import type {MarkdownConfig} from '@docusaurus/types';
|
|
32
23
|
import type {LoaderContext} from 'webpack';
|
|
33
24
|
|
|
34
|
-
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
35
|
-
import type {Processor} from 'unified';
|
|
36
|
-
import type {AdmonitionOptions} from './remark/admonitions';
|
|
37
|
-
|
|
38
|
-
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
39
|
-
import type {ProcessorOptions} from '@mdx-js/mdx';
|
|
40
|
-
|
|
41
25
|
// TODO as of April 2023, no way to import/re-export this ESM type easily :/
|
|
42
26
|
// This might change soon, likely after TS 5.2
|
|
43
27
|
// See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
|
|
44
28
|
type Pluggable = any; // TODO fix this asap
|
|
45
29
|
|
|
46
|
-
// Copied from https://mdxjs.com/packages/mdx/#optionsmdextensions
|
|
47
|
-
// Although we are likely to only use .md / .mdx anyway...
|
|
48
|
-
const mdFormatExtensions = [
|
|
49
|
-
'.md',
|
|
50
|
-
'.markdown',
|
|
51
|
-
'.mdown',
|
|
52
|
-
'.mkdn',
|
|
53
|
-
'.mkd',
|
|
54
|
-
'.mdwn',
|
|
55
|
-
'.mkdown',
|
|
56
|
-
'.ron',
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
function isMDFormat(filepath: string) {
|
|
60
|
-
return mdFormatExtensions.includes(path.extname(filepath));
|
|
61
|
-
}
|
|
62
|
-
|
|
63
30
|
const {
|
|
64
31
|
loaders: {inlineMarkdownImageFileLoader},
|
|
65
32
|
} = getFileLoaderUtils();
|
|
66
33
|
|
|
67
|
-
const DEFAULT_OPTIONS: MDXOptions = {
|
|
68
|
-
admonitions: true,
|
|
69
|
-
rehypePlugins: [],
|
|
70
|
-
remarkPlugins: [emoji, headings, toc],
|
|
71
|
-
beforeDefaultRemarkPlugins: [],
|
|
72
|
-
beforeDefaultRehypePlugins: [],
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
type CompilerCacheEntry = {
|
|
76
|
-
mdCompiler: Processor;
|
|
77
|
-
mdxCompiler: Processor;
|
|
78
|
-
options: Options;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
const compilerCache = new Map<string | Options, CompilerCacheEntry>();
|
|
82
|
-
|
|
83
34
|
export type MDXPlugin = Pluggable;
|
|
84
35
|
|
|
85
|
-
export type MDXOptions = {
|
|
86
|
-
admonitions: boolean | Partial<AdmonitionOptions>;
|
|
87
|
-
remarkPlugins: MDXPlugin[];
|
|
88
|
-
rehypePlugins: MDXPlugin[];
|
|
89
|
-
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
90
|
-
beforeDefaultRehypePlugins: MDXPlugin[];
|
|
91
|
-
};
|
|
92
|
-
|
|
93
36
|
export type Options = Partial<MDXOptions> & {
|
|
94
37
|
markdownConfig: MarkdownConfig;
|
|
95
38
|
staticDirs: string[];
|
|
@@ -167,20 +110,6 @@ function createAssetsExportCode(assets: unknown) {
|
|
|
167
110
|
return `{\n${codeLines.join('\n')}\n}`;
|
|
168
111
|
}
|
|
169
112
|
|
|
170
|
-
function getAdmonitionsPlugins(
|
|
171
|
-
admonitionsOption: MDXOptions['admonitions'],
|
|
172
|
-
): MDXPlugin[] {
|
|
173
|
-
if (admonitionsOption) {
|
|
174
|
-
const plugin: MDXPlugin =
|
|
175
|
-
admonitionsOption === true
|
|
176
|
-
? transformAdmonitions
|
|
177
|
-
: [transformAdmonitions, admonitionsOption];
|
|
178
|
-
return [plugin];
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return [];
|
|
182
|
-
}
|
|
183
|
-
|
|
184
113
|
// TODO temporary, remove this after v3.1?
|
|
185
114
|
// Some plugin authors use our mdx-loader, despite it not being public API
|
|
186
115
|
// see https://github.com/facebook/docusaurus/issues/8298
|
|
@@ -198,14 +127,10 @@ export async function mdxLoader(
|
|
|
198
127
|
): Promise<void> {
|
|
199
128
|
const callback = this.async();
|
|
200
129
|
const filePath = this.resourcePath;
|
|
201
|
-
const reqOptions = this.getOptions();
|
|
130
|
+
const reqOptions: Options = this.getOptions();
|
|
131
|
+
const {query} = this;
|
|
202
132
|
ensureMarkdownConfig(reqOptions);
|
|
203
133
|
|
|
204
|
-
const {createProcessor} = await import('@mdx-js/mdx');
|
|
205
|
-
const {default: gfm} = await import('remark-gfm');
|
|
206
|
-
const {default: comment} = await import('remark-comment');
|
|
207
|
-
const {default: directives} = await import('remark-directive');
|
|
208
|
-
|
|
209
134
|
const {frontMatter, content: contentWithTitle} = parseFrontMatter(fileString);
|
|
210
135
|
const mdxFrontMatter = validateMDXFrontMatter(frontMatter.mdx);
|
|
211
136
|
|
|
@@ -225,93 +150,16 @@ export async function mdxLoader(
|
|
|
225
150
|
|
|
226
151
|
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
227
152
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
const remarkPlugins: MDXPlugin[] = [
|
|
236
|
-
...(reqOptions.beforeDefaultRemarkPlugins ?? []),
|
|
237
|
-
directives,
|
|
238
|
-
...getAdmonitionsPlugins(reqOptions.admonitions ?? false),
|
|
239
|
-
...DEFAULT_OPTIONS.remarkPlugins,
|
|
240
|
-
details,
|
|
241
|
-
head,
|
|
242
|
-
...(reqOptions.markdownConfig.mermaid ? [mermaid] : []),
|
|
243
|
-
[
|
|
244
|
-
transformImage,
|
|
245
|
-
{
|
|
246
|
-
staticDirs: reqOptions.staticDirs,
|
|
247
|
-
siteDir: reqOptions.siteDir,
|
|
248
|
-
},
|
|
249
|
-
],
|
|
250
|
-
[
|
|
251
|
-
transformLinks,
|
|
252
|
-
{
|
|
253
|
-
staticDirs: reqOptions.staticDirs,
|
|
254
|
-
siteDir: reqOptions.siteDir,
|
|
255
|
-
},
|
|
256
|
-
],
|
|
257
|
-
gfm,
|
|
258
|
-
reqOptions.markdownConfig.mdx1Compat.comments ? comment : null,
|
|
259
|
-
...(reqOptions.remarkPlugins ?? []),
|
|
260
|
-
].filter((plugin): plugin is MDXPlugin => Boolean(plugin));
|
|
261
|
-
|
|
262
|
-
// codeCompatPlugin needs to be applied last after user-provided plugins
|
|
263
|
-
// (after npm2yarn for example)
|
|
264
|
-
remarkPlugins.push(codeCompatPlugin);
|
|
265
|
-
|
|
266
|
-
const rehypePlugins: MDXPlugin[] = [
|
|
267
|
-
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
268
|
-
...DEFAULT_OPTIONS.rehypePlugins,
|
|
269
|
-
...(reqOptions.rehypePlugins ?? []),
|
|
270
|
-
];
|
|
271
|
-
|
|
272
|
-
const options: ProcessorOptions & Options = {
|
|
273
|
-
...reqOptions,
|
|
274
|
-
remarkPlugins,
|
|
275
|
-
rehypePlugins,
|
|
276
|
-
providerImportSource: '@mdx-js/react',
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
const compilerCacheEntry: CompilerCacheEntry = {
|
|
280
|
-
mdCompiler: createProcessor({
|
|
281
|
-
...options,
|
|
282
|
-
format: 'md',
|
|
283
|
-
}),
|
|
284
|
-
mdxCompiler: createProcessor({
|
|
285
|
-
...options,
|
|
286
|
-
format: 'mdx',
|
|
287
|
-
}),
|
|
288
|
-
options,
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
compilerCache.set(this.query, compilerCacheEntry);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
const {mdCompiler, mdxCompiler, options} = compilerCache.get(this.query)!;
|
|
295
|
-
|
|
296
|
-
function getCompiler() {
|
|
297
|
-
const format =
|
|
298
|
-
mdxFrontMatter.format === 'detect'
|
|
299
|
-
? isMDFormat(filePath)
|
|
300
|
-
? 'md'
|
|
301
|
-
: 'mdx'
|
|
302
|
-
: mdxFrontMatter.format;
|
|
303
|
-
|
|
304
|
-
return format === 'md' ? mdCompiler : mdxCompiler;
|
|
305
|
-
}
|
|
153
|
+
const processor = await createProcessorCached({
|
|
154
|
+
filePath,
|
|
155
|
+
reqOptions,
|
|
156
|
+
query,
|
|
157
|
+
mdxFrontMatter,
|
|
158
|
+
});
|
|
306
159
|
|
|
307
160
|
let result: string;
|
|
308
161
|
try {
|
|
309
|
-
result = await
|
|
310
|
-
.process({
|
|
311
|
-
value: content,
|
|
312
|
-
path: filePath,
|
|
313
|
-
})
|
|
314
|
-
.then((res) => res.toString());
|
|
162
|
+
result = await processor.process({content, filePath});
|
|
315
163
|
} catch (errorUnknown) {
|
|
316
164
|
const error = errorUnknown as Error;
|
|
317
165
|
return callback(
|
|
@@ -327,14 +175,14 @@ export async function mdxLoader(
|
|
|
327
175
|
|
|
328
176
|
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
329
177
|
// Partial are not expected to have associated metadata files or front matter
|
|
330
|
-
const isMDXPartial =
|
|
178
|
+
const isMDXPartial = reqOptions.isMDXPartial?.(filePath);
|
|
331
179
|
if (isMDXPartial && hasFrontMatter) {
|
|
332
180
|
const errorMessage = `Docusaurus MDX partial files should not contain front matter.
|
|
333
181
|
Those partial files use the _ prefix as a convention by default, but this is configurable.
|
|
334
182
|
File at ${filePath} contains front matter that will be ignored:
|
|
335
183
|
${JSON.stringify(frontMatter, null, 2)}`;
|
|
336
184
|
|
|
337
|
-
if (!
|
|
185
|
+
if (!reqOptions.isMDXPartialFrontMatterWarningDisabled) {
|
|
338
186
|
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
|
339
187
|
if (shouldError) {
|
|
340
188
|
return callback(new Error(errorMessage));
|
|
@@ -346,8 +194,11 @@ ${JSON.stringify(frontMatter, null, 2)}`;
|
|
|
346
194
|
function getMetadataPath(): string | undefined {
|
|
347
195
|
if (!isMDXPartial) {
|
|
348
196
|
// Read metadata for this MDX and export it.
|
|
349
|
-
if (
|
|
350
|
-
|
|
197
|
+
if (
|
|
198
|
+
reqOptions.metadataPath &&
|
|
199
|
+
typeof reqOptions.metadataPath === 'function'
|
|
200
|
+
) {
|
|
201
|
+
return reqOptions.metadataPath(filePath);
|
|
351
202
|
}
|
|
352
203
|
}
|
|
353
204
|
return undefined;
|
package/src/processor.ts
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
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 emoji from 'remark-emoji';
|
|
9
|
+
import headings from './remark/headings';
|
|
10
|
+
import toc from './remark/toc';
|
|
11
|
+
import transformImage from './remark/transformImage';
|
|
12
|
+
import transformLinks from './remark/transformLinks';
|
|
13
|
+
import details from './remark/details';
|
|
14
|
+
import head from './remark/head';
|
|
15
|
+
import mermaid from './remark/mermaid';
|
|
16
|
+
import transformAdmonitions from './remark/admonitions';
|
|
17
|
+
import codeCompatPlugin from './remark/mdx1Compat/codeCompatPlugin';
|
|
18
|
+
import {getFormat} from './format';
|
|
19
|
+
import type {MDXFrontMatter} from './frontMatter';
|
|
20
|
+
import type {Options} from './loader';
|
|
21
|
+
import type {AdmonitionOptions} from './remark/admonitions';
|
|
22
|
+
|
|
23
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
24
|
+
import type {ProcessorOptions} from '@mdx-js/mdx';
|
|
25
|
+
|
|
26
|
+
// TODO as of April 2023, no way to import/re-export this ESM type easily :/
|
|
27
|
+
// This might change soon, likely after TS 5.2
|
|
28
|
+
// See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
|
|
29
|
+
type Pluggable = any; // TODO fix this asap
|
|
30
|
+
|
|
31
|
+
// TODO alt interface because impossible to import type Processor (ESM + TS :/)
|
|
32
|
+
type SimpleProcessor = {
|
|
33
|
+
process: ({
|
|
34
|
+
content,
|
|
35
|
+
filePath,
|
|
36
|
+
}: {
|
|
37
|
+
content: string;
|
|
38
|
+
filePath: string;
|
|
39
|
+
}) => Promise<string>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const DEFAULT_OPTIONS: MDXOptions = {
|
|
43
|
+
admonitions: true,
|
|
44
|
+
rehypePlugins: [],
|
|
45
|
+
remarkPlugins: [emoji, headings, toc],
|
|
46
|
+
beforeDefaultRemarkPlugins: [],
|
|
47
|
+
beforeDefaultRehypePlugins: [],
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type MDXPlugin = Pluggable;
|
|
51
|
+
|
|
52
|
+
export type MDXOptions = {
|
|
53
|
+
admonitions: boolean | Partial<AdmonitionOptions>;
|
|
54
|
+
remarkPlugins: MDXPlugin[];
|
|
55
|
+
rehypePlugins: MDXPlugin[];
|
|
56
|
+
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
57
|
+
beforeDefaultRehypePlugins: MDXPlugin[];
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
function getAdmonitionsPlugins(
|
|
61
|
+
admonitionsOption: MDXOptions['admonitions'],
|
|
62
|
+
): MDXPlugin[] {
|
|
63
|
+
if (admonitionsOption) {
|
|
64
|
+
const plugin: MDXPlugin =
|
|
65
|
+
admonitionsOption === true
|
|
66
|
+
? transformAdmonitions
|
|
67
|
+
: [transformAdmonitions, admonitionsOption];
|
|
68
|
+
return [plugin];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return [];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Need to be async due to ESM dynamic imports...
|
|
75
|
+
async function createProcessorFactory() {
|
|
76
|
+
const {createProcessor: createMdxProcessor} = await import('@mdx-js/mdx');
|
|
77
|
+
const {default: rehypeRaw} = await import('rehype-raw');
|
|
78
|
+
const {default: gfm} = await import('remark-gfm');
|
|
79
|
+
const {default: comment} = await import('remark-comment');
|
|
80
|
+
const {default: directive} = await import('remark-directive');
|
|
81
|
+
|
|
82
|
+
// /!\ this method is synchronous on purpose
|
|
83
|
+
// Using async code here can create cache entry race conditions!
|
|
84
|
+
function createProcessorSync({
|
|
85
|
+
options,
|
|
86
|
+
format,
|
|
87
|
+
}: {
|
|
88
|
+
options: Options;
|
|
89
|
+
format: 'md' | 'mdx';
|
|
90
|
+
}): SimpleProcessor {
|
|
91
|
+
const remarkPlugins: MDXPlugin[] = [
|
|
92
|
+
...(options.beforeDefaultRemarkPlugins ?? []),
|
|
93
|
+
directive,
|
|
94
|
+
...getAdmonitionsPlugins(options.admonitions ?? false),
|
|
95
|
+
...DEFAULT_OPTIONS.remarkPlugins,
|
|
96
|
+
details,
|
|
97
|
+
head,
|
|
98
|
+
...(options.markdownConfig.mermaid ? [mermaid] : []),
|
|
99
|
+
[
|
|
100
|
+
transformImage,
|
|
101
|
+
{
|
|
102
|
+
staticDirs: options.staticDirs,
|
|
103
|
+
siteDir: options.siteDir,
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
[
|
|
107
|
+
transformLinks,
|
|
108
|
+
{
|
|
109
|
+
staticDirs: options.staticDirs,
|
|
110
|
+
siteDir: options.siteDir,
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
gfm,
|
|
114
|
+
options.markdownConfig.mdx1Compat.comments ? comment : null,
|
|
115
|
+
...(options.remarkPlugins ?? []),
|
|
116
|
+
].filter((plugin): plugin is MDXPlugin => Boolean(plugin));
|
|
117
|
+
|
|
118
|
+
// codeCompatPlugin needs to be applied last after user-provided plugins
|
|
119
|
+
// (after npm2yarn for example)
|
|
120
|
+
remarkPlugins.push(codeCompatPlugin);
|
|
121
|
+
|
|
122
|
+
const rehypePlugins: MDXPlugin[] = [
|
|
123
|
+
...(options.beforeDefaultRehypePlugins ?? []),
|
|
124
|
+
...DEFAULT_OPTIONS.rehypePlugins,
|
|
125
|
+
...(options.rehypePlugins ?? []),
|
|
126
|
+
];
|
|
127
|
+
|
|
128
|
+
if (format === 'md') {
|
|
129
|
+
// This is what permits to embed HTML elements with format 'md'
|
|
130
|
+
// See https://github.com/facebook/docusaurus/pull/8960
|
|
131
|
+
// See https://github.com/mdx-js/mdx/pull/2295#issuecomment-1540085960
|
|
132
|
+
const rehypeRawPlugin: MDXPlugin = [
|
|
133
|
+
rehypeRaw,
|
|
134
|
+
{
|
|
135
|
+
passThrough: [
|
|
136
|
+
'mdxFlowExpression',
|
|
137
|
+
'mdxJsxFlowElement',
|
|
138
|
+
'mdxJsxTextElement',
|
|
139
|
+
'mdxTextExpression',
|
|
140
|
+
'mdxjsEsm',
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
];
|
|
144
|
+
rehypePlugins.unshift(rehypeRawPlugin);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const processorOptions: ProcessorOptions & Options = {
|
|
148
|
+
...options,
|
|
149
|
+
remarkPlugins,
|
|
150
|
+
rehypePlugins,
|
|
151
|
+
providerImportSource: '@mdx-js/react',
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const mdxProcessor = createMdxProcessor({
|
|
155
|
+
...processorOptions,
|
|
156
|
+
format,
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
process: async ({content, filePath}) =>
|
|
161
|
+
mdxProcessor
|
|
162
|
+
.process({
|
|
163
|
+
value: content,
|
|
164
|
+
path: filePath,
|
|
165
|
+
})
|
|
166
|
+
.then((res) => res.toString()),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {createProcessorSync};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Will be useful for tests
|
|
174
|
+
export async function createProcessorUncached(parameters: {
|
|
175
|
+
options: Options;
|
|
176
|
+
format: 'md' | 'mdx';
|
|
177
|
+
}): Promise<SimpleProcessor> {
|
|
178
|
+
const {createProcessorSync} = await createProcessorFactory();
|
|
179
|
+
return createProcessorSync(parameters);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// We use different compilers depending on the file type (md vs mdx)
|
|
183
|
+
type ProcessorsCacheEntry = {
|
|
184
|
+
mdProcessor: SimpleProcessor;
|
|
185
|
+
mdxProcessor: SimpleProcessor;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Compilers are cached so that Remark/Rehype plugins can run
|
|
189
|
+
// expensive code during initialization
|
|
190
|
+
const ProcessorsCache = new Map<string | Options, ProcessorsCacheEntry>();
|
|
191
|
+
|
|
192
|
+
async function createProcessorsCacheEntry({
|
|
193
|
+
query,
|
|
194
|
+
reqOptions,
|
|
195
|
+
}: {
|
|
196
|
+
query: string | Options;
|
|
197
|
+
reqOptions: Options;
|
|
198
|
+
}): Promise<ProcessorsCacheEntry> {
|
|
199
|
+
const {createProcessorSync} = await createProcessorFactory();
|
|
200
|
+
|
|
201
|
+
const compilers = ProcessorsCache.get(query);
|
|
202
|
+
if (compilers) {
|
|
203
|
+
return compilers;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const compilerCacheEntry: ProcessorsCacheEntry = {
|
|
207
|
+
mdProcessor: createProcessorSync({
|
|
208
|
+
options: reqOptions,
|
|
209
|
+
format: 'md',
|
|
210
|
+
}),
|
|
211
|
+
mdxProcessor: createProcessorSync({
|
|
212
|
+
options: reqOptions,
|
|
213
|
+
format: 'mdx',
|
|
214
|
+
}),
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
ProcessorsCache.set(query, compilerCacheEntry);
|
|
218
|
+
|
|
219
|
+
return compilerCacheEntry;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export async function createProcessorCached({
|
|
223
|
+
filePath,
|
|
224
|
+
mdxFrontMatter,
|
|
225
|
+
query,
|
|
226
|
+
reqOptions,
|
|
227
|
+
}: {
|
|
228
|
+
filePath: string;
|
|
229
|
+
mdxFrontMatter: MDXFrontMatter;
|
|
230
|
+
query: string | Options;
|
|
231
|
+
reqOptions: Options;
|
|
232
|
+
}): Promise<SimpleProcessor> {
|
|
233
|
+
const compilers = await createProcessorsCacheEntry({query, reqOptions});
|
|
234
|
+
|
|
235
|
+
const format = getFormat({
|
|
236
|
+
filePath,
|
|
237
|
+
frontMatterFormat: mdxFrontMatter.format,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
return format === 'md' ? compilers.mdProcessor : compilers.mdxProcessor;
|
|
241
|
+
}
|