@docusaurus/mdx-loader 2.4.1 → 3.0.0-beta.0
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/README.md +13 -0
- package/lib/format.d.ts +14 -0
- package/lib/format.d.ts.map +1 -0
- package/lib/format.js +42 -0
- package/lib/format.js.map +1 -0
- package/lib/frontMatter.d.ts +13 -0
- package/lib/frontMatter.d.ts.map +1 -0
- package/lib/frontMatter.js +23 -0
- package/lib/frontMatter.js.map +1 -0
- package/lib/index.d.ts +7 -7
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/loader.d.ts +5 -14
- package/lib/loader.d.ts.map +1 -1
- package/lib/loader.js +45 -81
- package/lib/loader.js.map +1 -1
- package/lib/preprocessor.d.ts +18 -0
- package/lib/preprocessor.d.ts.map +1 -0
- package/lib/preprocessor.js +34 -0
- package/lib/preprocessor.js.map +1 -0
- package/lib/processor.d.ts +36 -0
- package/lib/processor.d.ts.map +1 -0
- package/lib/processor.js +160 -0
- package/lib/processor.js.map +1 -0
- package/lib/remark/admonitions/index.d.ts +4 -9
- package/lib/remark/admonitions/index.d.ts.map +1 -1
- package/lib/remark/admonitions/index.js +70 -128
- package/lib/remark/admonitions/index.js.map +1 -1
- package/lib/remark/details/index.d.ts +9 -0
- package/lib/remark/details/index.d.ts.map +1 -0
- package/lib/remark/details/index.js +23 -0
- package/lib/remark/details/index.js.map +1 -0
- package/lib/remark/head/index.d.ts +9 -0
- package/lib/remark/head/index.d.ts.map +1 -0
- package/lib/remark/head/index.js +23 -0
- package/lib/remark/head/index.js.map +1 -0
- package/lib/remark/headings/index.d.ts.map +1 -1
- package/lib/remark/headings/index.js +3 -3
- package/lib/remark/headings/index.js.map +1 -1
- package/lib/remark/{unwrapMdxCodeBlocks/index.d.ts → mdx1Compat/codeCompatPlugin.d.ts} +2 -2
- package/lib/remark/mdx1Compat/codeCompatPlugin.d.ts.map +1 -0
- package/lib/remark/mdx1Compat/codeCompatPlugin.js +34 -0
- package/lib/remark/mdx1Compat/codeCompatPlugin.js.map +1 -0
- package/lib/remark/mermaid/index.d.ts.map +1 -1
- package/lib/remark/mermaid/index.js +1 -0
- package/lib/remark/mermaid/index.js.map +1 -1
- package/lib/remark/toc/index.d.ts +8 -2
- package/lib/remark/toc/index.d.ts.map +1 -1
- package/lib/remark/toc/index.js +53 -23
- package/lib/remark/toc/index.js.map +1 -1
- package/lib/remark/transformImage/index.d.ts +2 -1
- package/lib/remark/transformImage/index.d.ts.map +1 -1
- package/lib/remark/transformImage/index.js +42 -15
- package/lib/remark/transformImage/index.js.map +1 -1
- package/lib/remark/transformLinks/index.d.ts +2 -1
- package/lib/remark/transformLinks/index.d.ts.map +1 -1
- package/lib/remark/transformLinks/index.js +30 -11
- package/lib/remark/transformLinks/index.js.map +1 -1
- package/lib/remark/utils/index.d.ts +4 -2
- package/lib/remark/utils/index.d.ts.map +1 -1
- package/lib/remark/utils/index.js +82 -11
- package/lib/remark/utils/index.js.map +1 -1
- package/package.json +28 -20
- package/src/format.ts +50 -0
- package/src/frontMatter.ts +31 -0
- package/src/index.ts +10 -6
- package/src/loader.ts +72 -107
- package/src/preprocessor.ts +47 -0
- package/src/processor.ts +243 -0
- package/src/remark/admonitions/README.md +1 -1
- package/src/remark/admonitions/index.ts +92 -171
- package/src/remark/details/index.ts +25 -0
- package/src/remark/head/index.ts +25 -0
- package/src/remark/headings/index.ts +5 -3
- package/src/remark/mdx1Compat/codeCompatPlugin.ts +36 -0
- package/src/remark/mermaid/index.ts +2 -0
- package/src/remark/toc/index.ts +80 -26
- package/src/remark/transformImage/index.ts +54 -19
- package/src/remark/transformLinks/index.ts +44 -17
- package/src/remark/utils/index.ts +108 -9
- package/lib/remark/unwrapMdxCodeBlocks/index.d.ts.map +0 -1
- package/lib/remark/unwrapMdxCodeBlocks/index.js +0 -29
- package/lib/remark/unwrapMdxCodeBlocks/index.js.map +0 -1
- package/src/deps.d.ts +0 -31
- package/src/remark/unwrapMdxCodeBlocks/index.ts +0 -33
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/mdx-loader",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0-beta.0",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -18,44 +18,52 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@babel/parser": "^7.
|
|
22
|
-
"@babel/traverse": "^7.
|
|
23
|
-
"@docusaurus/logger": "
|
|
24
|
-
"@docusaurus/utils": "
|
|
25
|
-
"@
|
|
21
|
+
"@babel/parser": "^7.22.7",
|
|
22
|
+
"@babel/traverse": "^7.22.8",
|
|
23
|
+
"@docusaurus/logger": "3.0.0-beta.0",
|
|
24
|
+
"@docusaurus/utils": "3.0.0-beta.0",
|
|
25
|
+
"@docusaurus/utils-validation": "3.0.0-beta.0",
|
|
26
|
+
"@mdx-js/mdx": "^2.1.5",
|
|
27
|
+
"@slorber/remark-comment": "^1.0.0",
|
|
26
28
|
"escape-html": "^1.0.3",
|
|
29
|
+
"estree-util-value-to-estree": "^2.1.0",
|
|
27
30
|
"file-loader": "^6.2.0",
|
|
28
|
-
"fs-extra": "^
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
+
"fs-extra": "^11.1.1",
|
|
32
|
+
"hastscript": "^7.1.0",
|
|
33
|
+
"image-size": "^1.0.2",
|
|
34
|
+
"mdast-util-mdx": "^2.0.0",
|
|
35
|
+
"mdast-util-to-string": "^3.2.0",
|
|
36
|
+
"rehype-raw": "^6.1.1",
|
|
37
|
+
"remark-directive": "^2.0.1",
|
|
31
38
|
"remark-emoji": "^2.2.0",
|
|
39
|
+
"remark-gfm": "^3.0.1",
|
|
32
40
|
"stringify-object": "^3.3.0",
|
|
33
|
-
"tslib": "^2.
|
|
34
|
-
"unified": "^
|
|
41
|
+
"tslib": "^2.6.0",
|
|
42
|
+
"unified": "^10.1.2",
|
|
35
43
|
"unist-util-visit": "^2.0.3",
|
|
36
44
|
"url-loader": "^4.1.1",
|
|
37
|
-
"webpack": "^5.
|
|
45
|
+
"webpack": "^5.88.1"
|
|
38
46
|
},
|
|
39
47
|
"devDependencies": {
|
|
40
|
-
"@docusaurus/types": "
|
|
48
|
+
"@docusaurus/types": "3.0.0-beta.0",
|
|
41
49
|
"@types/escape-html": "^1.0.2",
|
|
42
|
-
"@types/mdast": "^3.0.
|
|
50
|
+
"@types/mdast": "^3.0.12",
|
|
43
51
|
"@types/stringify-object": "^3.3.1",
|
|
44
|
-
"@types/unist": "^2.0.
|
|
52
|
+
"@types/unist": "^2.0.7",
|
|
45
53
|
"rehype-stringify": "^8.0.0",
|
|
46
|
-
"remark": "^
|
|
47
|
-
"remark-mdx": "^1.
|
|
54
|
+
"remark": "^14.0.3",
|
|
55
|
+
"remark-mdx": "^2.1.5",
|
|
48
56
|
"remark-rehype": "^8.1.0",
|
|
49
57
|
"to-vfile": "^6.1.0",
|
|
50
58
|
"unist-builder": "^2.0.3",
|
|
51
59
|
"unist-util-remove-position": "^3.0.0"
|
|
52
60
|
},
|
|
53
61
|
"peerDependencies": {
|
|
54
|
-
"react": "^
|
|
55
|
-
"react-dom": "^
|
|
62
|
+
"react": "^18.0.0",
|
|
63
|
+
"react-dom": "^18.0.0"
|
|
56
64
|
},
|
|
57
65
|
"engines": {
|
|
58
66
|
"node": ">=16.14"
|
|
59
67
|
},
|
|
60
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "27a1e90d9fff88af90ecad35bea16d4d7230482a"
|
|
61
69
|
}
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
import type {Format, FormatInput} from './index';
|
|
11
|
+
|
|
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
|
+
|
|
25
|
+
function getExtensionFormat(filepath: string): Format {
|
|
26
|
+
const isMDFormat = mdFormatExtensions.includes(path.extname(filepath));
|
|
27
|
+
// Bias toward mdx if unknown extension
|
|
28
|
+
return isMDFormat ? 'md' : 'mdx';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getFormat({
|
|
32
|
+
filePath,
|
|
33
|
+
frontMatterFormat,
|
|
34
|
+
markdownConfigFormat,
|
|
35
|
+
}: {
|
|
36
|
+
filePath: string;
|
|
37
|
+
frontMatterFormat: MDXFrontMatter['format'];
|
|
38
|
+
markdownConfigFormat: FormatInput;
|
|
39
|
+
}): Format {
|
|
40
|
+
if (frontMatterFormat) {
|
|
41
|
+
if (frontMatterFormat !== 'detect') {
|
|
42
|
+
return frontMatterFormat;
|
|
43
|
+
}
|
|
44
|
+
return getExtensionFormat(filePath);
|
|
45
|
+
}
|
|
46
|
+
if (markdownConfigFormat !== 'detect') {
|
|
47
|
+
return markdownConfigFormat;
|
|
48
|
+
}
|
|
49
|
+
return getExtensionFormat(filePath);
|
|
50
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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 {
|
|
9
|
+
JoiFrontMatter as Joi,
|
|
10
|
+
validateFrontMatter,
|
|
11
|
+
} from '@docusaurus/utils-validation';
|
|
12
|
+
|
|
13
|
+
import type {FormatInput} from './index';
|
|
14
|
+
|
|
15
|
+
export type MDXFrontMatter = {
|
|
16
|
+
format?: FormatInput;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const DefaultMDXFrontMatter: MDXFrontMatter = {
|
|
20
|
+
format: undefined,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const MDXFrontMatterSchema = Joi.object<MDXFrontMatter>({
|
|
24
|
+
format: Joi.string().equal('md', 'mdx', 'detect').optional(),
|
|
25
|
+
}).default(DefaultMDXFrontMatter);
|
|
26
|
+
|
|
27
|
+
export function validateMDXFrontMatter(frontMatter: unknown): MDXFrontMatter {
|
|
28
|
+
return validateFrontMatter(frontMatter, MDXFrontMatterSchema, {
|
|
29
|
+
allowUnknown: false,
|
|
30
|
+
});
|
|
31
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -7,13 +7,15 @@
|
|
|
7
7
|
|
|
8
8
|
import {mdxLoader} from './loader';
|
|
9
9
|
|
|
10
|
+
import type {TOCItem as TOCItemImported} from './remark/toc';
|
|
11
|
+
|
|
10
12
|
export default mdxLoader;
|
|
11
13
|
|
|
12
|
-
export type TOCItem =
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
export type TOCItem = TOCItemImported;
|
|
15
|
+
|
|
16
|
+
export type Format = 'md' | 'mdx';
|
|
17
|
+
|
|
18
|
+
export type FormatInput = Format | 'detect';
|
|
17
19
|
|
|
18
20
|
export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
|
19
21
|
/** As verbatim declared in the MDX document. */
|
|
@@ -32,4 +34,6 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
|
|
32
34
|
readonly assets: Assets;
|
|
33
35
|
(): JSX.Element;
|
|
34
36
|
};
|
|
35
|
-
|
|
37
|
+
|
|
38
|
+
export type {Options, MDXPlugin} from './loader';
|
|
39
|
+
export type {MDXOptions} from './processor';
|
package/src/loader.ts
CHANGED
|
@@ -13,54 +13,25 @@ import {
|
|
|
13
13
|
escapePath,
|
|
14
14
|
getFileLoaderUtils,
|
|
15
15
|
} from '@docusaurus/utils';
|
|
16
|
-
import {createCompiler} from '@mdx-js/mdx';
|
|
17
|
-
import emoji from 'remark-emoji';
|
|
18
16
|
import stringifyObject from 'stringify-object';
|
|
17
|
+
import preprocessor from './preprocessor';
|
|
18
|
+
import {validateMDXFrontMatter} from './frontMatter';
|
|
19
|
+
import {createProcessorCached} from './processor';
|
|
20
|
+
import type {MDXOptions} from './processor';
|
|
19
21
|
|
|
20
|
-
import headings from './remark/headings';
|
|
21
|
-
import toc from './remark/toc';
|
|
22
|
-
import unwrapMdxCodeBlocks from './remark/unwrapMdxCodeBlocks';
|
|
23
|
-
import transformImage from './remark/transformImage';
|
|
24
|
-
import transformLinks from './remark/transformLinks';
|
|
25
|
-
import mermaid from './remark/mermaid';
|
|
26
|
-
|
|
27
|
-
import transformAdmonitions from './remark/admonitions';
|
|
28
22
|
import type {MarkdownConfig} from '@docusaurus/types';
|
|
29
23
|
import type {LoaderContext} from 'webpack';
|
|
30
|
-
|
|
31
|
-
import type
|
|
24
|
+
|
|
25
|
+
// TODO as of April 2023, no way to import/re-export this ESM type easily :/
|
|
26
|
+
// This might change soon, likely after TS 5.2
|
|
27
|
+
// See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
|
|
28
|
+
type Pluggable = any; // TODO fix this asap
|
|
32
29
|
|
|
33
30
|
const {
|
|
34
31
|
loaders: {inlineMarkdownImageFileLoader},
|
|
35
32
|
} = getFileLoaderUtils();
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
/* @jsxRuntime classic */
|
|
39
|
-
/* @jsx mdx */
|
|
40
|
-
/* @jsxFrag React.Fragment */
|
|
41
|
-
`;
|
|
42
|
-
|
|
43
|
-
const DEFAULT_OPTIONS: MDXOptions = {
|
|
44
|
-
admonitions: true,
|
|
45
|
-
rehypePlugins: [],
|
|
46
|
-
remarkPlugins: [unwrapMdxCodeBlocks, emoji, headings, toc],
|
|
47
|
-
beforeDefaultRemarkPlugins: [],
|
|
48
|
-
beforeDefaultRehypePlugins: [],
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const compilerCache = new Map<string | Options, [Processor, Options]>();
|
|
52
|
-
|
|
53
|
-
export type MDXPlugin =
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
-
[Plugin<any[]>, any] | Plugin<any[]>;
|
|
56
|
-
|
|
57
|
-
export type MDXOptions = {
|
|
58
|
-
admonitions: boolean | AdmonitionOptions;
|
|
59
|
-
remarkPlugins: MDXPlugin[];
|
|
60
|
-
rehypePlugins: MDXPlugin[];
|
|
61
|
-
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
62
|
-
beforeDefaultRehypePlugins: MDXPlugin[];
|
|
63
|
-
};
|
|
34
|
+
export type MDXPlugin = Pluggable;
|
|
64
35
|
|
|
65
36
|
export type Options = Partial<MDXOptions> & {
|
|
66
37
|
markdownConfig: MarkdownConfig;
|
|
@@ -139,17 +110,15 @@ function createAssetsExportCode(assets: unknown) {
|
|
|
139
110
|
return `{\n${codeLines.join('\n')}\n}`;
|
|
140
111
|
}
|
|
141
112
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return [plugin];
|
|
113
|
+
// TODO temporary, remove this after v3.1?
|
|
114
|
+
// Some plugin authors use our mdx-loader, despite it not being public API
|
|
115
|
+
// see https://github.com/facebook/docusaurus/issues/8298
|
|
116
|
+
function ensureMarkdownConfig(reqOptions: Options) {
|
|
117
|
+
if (!reqOptions.markdownConfig) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
'Docusaurus v3+ requires MDX loader options.markdownConfig - plugin authors using the MDX loader should make sure to provide that option',
|
|
120
|
+
);
|
|
151
121
|
}
|
|
152
|
-
return [];
|
|
153
122
|
}
|
|
154
123
|
|
|
155
124
|
export async function mdxLoader(
|
|
@@ -158,77 +127,72 @@ export async function mdxLoader(
|
|
|
158
127
|
): Promise<void> {
|
|
159
128
|
const callback = this.async();
|
|
160
129
|
const filePath = this.resourcePath;
|
|
161
|
-
const reqOptions = this.getOptions();
|
|
130
|
+
const reqOptions: Options = this.getOptions();
|
|
131
|
+
const {query} = this;
|
|
132
|
+
ensureMarkdownConfig(reqOptions);
|
|
162
133
|
|
|
163
134
|
const {frontMatter, content: contentWithTitle} = parseFrontMatter(fileString);
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
135
|
+
const mdxFrontMatter = validateMDXFrontMatter(frontMatter.mdx);
|
|
136
|
+
|
|
137
|
+
const {content: contentUnprocessed, contentTitle} = parseMarkdownContentTitle(
|
|
138
|
+
contentWithTitle,
|
|
139
|
+
{
|
|
140
|
+
removeContentTitle: reqOptions.removeContentTitle,
|
|
141
|
+
},
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
const content = preprocessor({
|
|
145
|
+
fileContent: contentUnprocessed,
|
|
146
|
+
filePath,
|
|
147
|
+
admonitions: reqOptions.admonitions,
|
|
148
|
+
markdownConfig: reqOptions.markdownConfig,
|
|
167
149
|
});
|
|
168
150
|
|
|
169
151
|
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
170
152
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
[
|
|
178
|
-
transformImage,
|
|
179
|
-
{
|
|
180
|
-
staticDirs: reqOptions.staticDirs,
|
|
181
|
-
siteDir: reqOptions.siteDir,
|
|
182
|
-
},
|
|
183
|
-
],
|
|
184
|
-
[
|
|
185
|
-
transformLinks,
|
|
186
|
-
{
|
|
187
|
-
staticDirs: reqOptions.staticDirs,
|
|
188
|
-
siteDir: reqOptions.siteDir,
|
|
189
|
-
},
|
|
190
|
-
],
|
|
191
|
-
...(reqOptions.remarkPlugins ?? []),
|
|
192
|
-
];
|
|
193
|
-
|
|
194
|
-
const rehypePlugins: MDXPlugin[] = [
|
|
195
|
-
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
196
|
-
...DEFAULT_OPTIONS.rehypePlugins,
|
|
197
|
-
...(reqOptions.rehypePlugins ?? []),
|
|
198
|
-
];
|
|
199
|
-
|
|
200
|
-
const options: Options = {
|
|
201
|
-
...reqOptions,
|
|
202
|
-
remarkPlugins,
|
|
203
|
-
rehypePlugins,
|
|
204
|
-
};
|
|
205
|
-
compilerCache.set(this.query, [createCompiler(options), options]);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
const [compiler, options] = compilerCache.get(this.query)!;
|
|
153
|
+
const processor = await createProcessorCached({
|
|
154
|
+
filePath,
|
|
155
|
+
reqOptions,
|
|
156
|
+
query,
|
|
157
|
+
mdxFrontMatter,
|
|
158
|
+
});
|
|
209
159
|
|
|
210
160
|
let result: string;
|
|
211
161
|
try {
|
|
212
|
-
result = await
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
162
|
+
result = await processor.process({content, filePath});
|
|
163
|
+
} catch (errorUnknown) {
|
|
164
|
+
const error = errorUnknown as Error;
|
|
165
|
+
|
|
166
|
+
// MDX can emit errors that have useful extra attributes
|
|
167
|
+
const errorJSON = JSON.stringify(error, null, 2);
|
|
168
|
+
const errorDetails =
|
|
169
|
+
errorJSON === '{}'
|
|
170
|
+
? // regular JS error case: print stacktrace
|
|
171
|
+
error.stack ?? 'N/A'
|
|
172
|
+
: // MDX error: print extra attributes + stacktrace
|
|
173
|
+
`${errorJSON}\n${error.stack}`;
|
|
174
|
+
|
|
175
|
+
return callback(
|
|
176
|
+
new Error(
|
|
177
|
+
`MDX compilation failed for file ${logger.path(filePath)}\nCause: ${
|
|
178
|
+
error.message
|
|
179
|
+
}\nDetails:\n${errorDetails}`,
|
|
180
|
+
// TODO error cause doesn't seem to be used by Webpack stats.errors :s
|
|
181
|
+
{cause: error},
|
|
182
|
+
),
|
|
183
|
+
);
|
|
220
184
|
}
|
|
221
185
|
|
|
222
186
|
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
223
187
|
// Partial are not expected to have associated metadata files or front matter
|
|
224
|
-
const isMDXPartial =
|
|
188
|
+
const isMDXPartial = reqOptions.isMDXPartial?.(filePath);
|
|
225
189
|
if (isMDXPartial && hasFrontMatter) {
|
|
226
190
|
const errorMessage = `Docusaurus MDX partial files should not contain front matter.
|
|
227
191
|
Those partial files use the _ prefix as a convention by default, but this is configurable.
|
|
228
192
|
File at ${filePath} contains front matter that will be ignored:
|
|
229
193
|
${JSON.stringify(frontMatter, null, 2)}`;
|
|
230
194
|
|
|
231
|
-
if (!
|
|
195
|
+
if (!reqOptions.isMDXPartialFrontMatterWarningDisabled) {
|
|
232
196
|
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
|
233
197
|
if (shouldError) {
|
|
234
198
|
return callback(new Error(errorMessage));
|
|
@@ -240,8 +204,11 @@ ${JSON.stringify(frontMatter, null, 2)}`;
|
|
|
240
204
|
function getMetadataPath(): string | undefined {
|
|
241
205
|
if (!isMDXPartial) {
|
|
242
206
|
// Read metadata for this MDX and export it.
|
|
243
|
-
if (
|
|
244
|
-
|
|
207
|
+
if (
|
|
208
|
+
reqOptions.metadataPath &&
|
|
209
|
+
typeof reqOptions.metadataPath === 'function'
|
|
210
|
+
) {
|
|
211
|
+
return reqOptions.metadataPath(filePath);
|
|
245
212
|
}
|
|
246
213
|
}
|
|
247
214
|
return undefined;
|
|
@@ -265,6 +232,8 @@ ${JSON.stringify(frontMatter, null, 2)}`;
|
|
|
265
232
|
? reqOptions.createAssets({frontMatter, metadata})
|
|
266
233
|
: undefined;
|
|
267
234
|
|
|
235
|
+
// TODO use remark plugins to insert extra exports instead of string concat?
|
|
236
|
+
// cf how the toc is exported
|
|
268
237
|
const exportsCode = `
|
|
269
238
|
export const frontMatter = ${stringifyObject(frontMatter)};
|
|
270
239
|
export const contentTitle = ${stringifyObject(contentTitle)};
|
|
@@ -273,10 +242,6 @@ ${assets ? `export const assets = ${createAssetsExportCode(assets)};` : ''}
|
|
|
273
242
|
`;
|
|
274
243
|
|
|
275
244
|
const code = `
|
|
276
|
-
${pragma}
|
|
277
|
-
import React from 'react';
|
|
278
|
-
import { mdx } from '@mdx-js/react';
|
|
279
|
-
|
|
280
245
|
${exportsCode}
|
|
281
246
|
${result}
|
|
282
247
|
`;
|
|
@@ -0,0 +1,47 @@
|
|
|
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 {
|
|
9
|
+
escapeMarkdownHeadingIds,
|
|
10
|
+
unwrapMdxCodeBlocks,
|
|
11
|
+
admonitionTitleToDirectiveLabel,
|
|
12
|
+
} from '@docusaurus/utils';
|
|
13
|
+
import {normalizeAdmonitionOptions} from './remark/admonitions';
|
|
14
|
+
import type {Options} from './loader';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Preprocess the string before passing it to MDX
|
|
18
|
+
* This is not particularly recommended but makes it easier to upgrade to MDX 2
|
|
19
|
+
*/
|
|
20
|
+
export default function preprocessContent({
|
|
21
|
+
fileContent: initialFileContent,
|
|
22
|
+
filePath,
|
|
23
|
+
markdownConfig,
|
|
24
|
+
admonitions,
|
|
25
|
+
}: {
|
|
26
|
+
fileContent: string;
|
|
27
|
+
filePath: string;
|
|
28
|
+
markdownConfig: Options['markdownConfig'];
|
|
29
|
+
admonitions: Options['admonitions'] | undefined;
|
|
30
|
+
}): string {
|
|
31
|
+
let fileContent = initialFileContent;
|
|
32
|
+
if (markdownConfig.preprocessor) {
|
|
33
|
+
fileContent = markdownConfig.preprocessor({
|
|
34
|
+
fileContent,
|
|
35
|
+
filePath,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
fileContent = unwrapMdxCodeBlocks(fileContent);
|
|
39
|
+
if (markdownConfig.mdx1Compat.headingIds) {
|
|
40
|
+
fileContent = escapeMarkdownHeadingIds(fileContent);
|
|
41
|
+
}
|
|
42
|
+
if (markdownConfig.mdx1Compat.admonitions && admonitions) {
|
|
43
|
+
const {keywords} = normalizeAdmonitionOptions(admonitions);
|
|
44
|
+
fileContent = admonitionTitleToDirectiveLabel(fileContent, keywords);
|
|
45
|
+
}
|
|
46
|
+
return fileContent;
|
|
47
|
+
}
|