@docusaurus/mdx-loader 0.0.0-4940 → 0.0.0-4945
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 +25 -22
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -184
- package/lib/index.js.map +1 -1
- package/lib/loader.d.ts +39 -0
- package/lib/loader.d.ts.map +1 -0
- package/lib/loader.js +194 -0
- package/lib/loader.js.map +1 -0
- package/package.json +6 -6
- package/src/deps.d.ts +6 -3
- package/src/index.ts +23 -232
- package/src/loader.ts +253 -0
- package/src/remark/toc/index.ts +1 -1
- package/src/mdx-loader.d.ts +0 -37
package/lib/index.d.ts
CHANGED
|
@@ -4,27 +4,30 @@
|
|
|
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
|
-
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
removeContentTitle?: boolean;
|
|
15
|
-
metadataPath?: string | ((filePath: string) => string);
|
|
16
|
-
createAssets?: (metadata: {
|
|
17
|
-
frontMatter: {
|
|
18
|
-
[key: string]: unknown;
|
|
19
|
-
};
|
|
20
|
-
metadata: {
|
|
21
|
-
[key: string]: unknown;
|
|
22
|
-
};
|
|
23
|
-
}) => {
|
|
24
|
-
[key: string]: unknown;
|
|
25
|
-
};
|
|
26
|
-
filepath: string;
|
|
7
|
+
/// <reference types="react" />
|
|
8
|
+
import { mdxLoader } from './loader';
|
|
9
|
+
export default mdxLoader;
|
|
10
|
+
export declare type TOCItem = {
|
|
11
|
+
readonly value: string;
|
|
12
|
+
readonly id: string;
|
|
13
|
+
readonly level: number;
|
|
27
14
|
};
|
|
28
|
-
export
|
|
29
|
-
|
|
15
|
+
export declare type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
|
16
|
+
/** As verbatim declared in the MDX document. */
|
|
17
|
+
readonly frontMatter: FrontMatter;
|
|
18
|
+
/** As provided by the content plugin. */
|
|
19
|
+
readonly metadata: Metadata;
|
|
20
|
+
/** A list of TOC items (headings). */
|
|
21
|
+
readonly toc: readonly TOCItem[];
|
|
22
|
+
/** First h1 title before any content. */
|
|
23
|
+
readonly contentTitle: string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* Usually image assets that may be collocated like `./img/thumbnail.png`.
|
|
26
|
+
* The loader would also bundle these assets and the client should use these
|
|
27
|
+
* in priority.
|
|
28
|
+
*/
|
|
29
|
+
readonly assets: Assets;
|
|
30
|
+
(): JSX.Element;
|
|
31
|
+
};
|
|
32
|
+
export type { Options, MDXPlugin, MDXOptions } from './loader';
|
|
30
33
|
//# 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
|
|
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/index.js
CHANGED
|
@@ -6,188 +6,6 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const mdx_1 = require("@mdx-js/mdx");
|
|
12
|
-
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
13
|
-
const remark_emoji_1 = tslib_1.__importDefault(require("remark-emoji"));
|
|
14
|
-
const utils_1 = require("@docusaurus/utils");
|
|
15
|
-
const stringify_object_1 = tslib_1.__importDefault(require("stringify-object"));
|
|
16
|
-
const headings_1 = tslib_1.__importDefault(require("./remark/headings"));
|
|
17
|
-
const toc_1 = tslib_1.__importDefault(require("./remark/toc"));
|
|
18
|
-
const unwrapMdxCodeBlocks_1 = tslib_1.__importDefault(require("./remark/unwrapMdxCodeBlocks"));
|
|
19
|
-
const transformImage_1 = tslib_1.__importDefault(require("./remark/transformImage"));
|
|
20
|
-
const transformLinks_1 = tslib_1.__importDefault(require("./remark/transformLinks"));
|
|
21
|
-
const { loaders: { inlineMarkdownImageFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
|
|
22
|
-
const pragma = `
|
|
23
|
-
/* @jsxRuntime classic */
|
|
24
|
-
/* @jsx mdx */
|
|
25
|
-
/* @jsxFrag mdx.Fragment */
|
|
26
|
-
`;
|
|
27
|
-
const DEFAULT_OPTIONS = {
|
|
28
|
-
rehypePlugins: [],
|
|
29
|
-
remarkPlugins: [unwrapMdxCodeBlocks_1.default, remark_emoji_1.default, headings_1.default, toc_1.default],
|
|
30
|
-
beforeDefaultRemarkPlugins: [],
|
|
31
|
-
beforeDefaultRehypePlugins: [],
|
|
32
|
-
};
|
|
33
|
-
const compilerCache = new Map();
|
|
34
|
-
/**
|
|
35
|
-
* When this throws, it generally means that there's no metadata file associated
|
|
36
|
-
* with this MDX document. It can happen when using MDX partials (usually
|
|
37
|
-
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
38
|
-
* function in config
|
|
39
|
-
*/
|
|
40
|
-
async function readMetadataPath(metadataPath) {
|
|
41
|
-
try {
|
|
42
|
-
return await fs_extra_1.default.readFile(metadataPath, 'utf8');
|
|
43
|
-
}
|
|
44
|
-
catch (err) {
|
|
45
|
-
logger_1.default.error `MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
|
|
46
|
-
throw err;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Converts assets an object with Webpack require calls code.
|
|
51
|
-
* This is useful for mdx files to reference co-located assets using relative
|
|
52
|
-
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
53
|
-
* For now, we only handle that for images and paths starting with `./`:
|
|
54
|
-
*
|
|
55
|
-
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
56
|
-
*/
|
|
57
|
-
function createAssetsExportCode(assets) {
|
|
58
|
-
if (Object.keys(assets).length === 0) {
|
|
59
|
-
return 'undefined';
|
|
60
|
-
}
|
|
61
|
-
// TODO implementation can be completed/enhanced
|
|
62
|
-
function createAssetValueCode(assetValue) {
|
|
63
|
-
if (Array.isArray(assetValue)) {
|
|
64
|
-
const arrayItemCodes = assetValue.map((item) => createAssetValueCode(item) ?? 'undefined');
|
|
65
|
-
return `[${arrayItemCodes.join(', ')}]`;
|
|
66
|
-
}
|
|
67
|
-
// Only process string values starting with ./
|
|
68
|
-
// We could enhance this logic and check if file exists on disc?
|
|
69
|
-
if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
|
|
70
|
-
// TODO do we have other use-cases than image assets?
|
|
71
|
-
// Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
|
|
72
|
-
const inlineLoader = inlineMarkdownImageFileLoader;
|
|
73
|
-
return `require("${inlineLoader}${(0, utils_1.escapePath)(assetValue)}").default`;
|
|
74
|
-
}
|
|
75
|
-
return undefined;
|
|
76
|
-
}
|
|
77
|
-
const assetEntries = Object.entries(assets);
|
|
78
|
-
const codeLines = assetEntries
|
|
79
|
-
.map(([key, value]) => {
|
|
80
|
-
const assetRequireCode = createAssetValueCode(value);
|
|
81
|
-
return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
|
|
82
|
-
})
|
|
83
|
-
.filter(Boolean);
|
|
84
|
-
return `{\n${codeLines.join('\n')}\n}`;
|
|
85
|
-
}
|
|
86
|
-
async function mdxLoader(fileString) {
|
|
87
|
-
const callback = this.async();
|
|
88
|
-
const filePath = this.resourcePath;
|
|
89
|
-
const reqOptions = this.getOptions() ?? {};
|
|
90
|
-
const { frontMatter, content: contentWithTitle } = (0, utils_1.parseFrontMatter)(fileString);
|
|
91
|
-
const { content, contentTitle } = (0, utils_1.parseMarkdownContentTitle)(contentWithTitle, {
|
|
92
|
-
removeContentTitle: reqOptions.removeContentTitle,
|
|
93
|
-
});
|
|
94
|
-
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
95
|
-
if (!compilerCache.has(this.query)) {
|
|
96
|
-
const options = {
|
|
97
|
-
...reqOptions,
|
|
98
|
-
remarkPlugins: [
|
|
99
|
-
...(reqOptions.beforeDefaultRemarkPlugins ?? []),
|
|
100
|
-
...DEFAULT_OPTIONS.remarkPlugins,
|
|
101
|
-
[
|
|
102
|
-
transformImage_1.default,
|
|
103
|
-
{
|
|
104
|
-
staticDirs: reqOptions.staticDirs,
|
|
105
|
-
siteDir: reqOptions.siteDir,
|
|
106
|
-
},
|
|
107
|
-
],
|
|
108
|
-
[
|
|
109
|
-
transformLinks_1.default,
|
|
110
|
-
{
|
|
111
|
-
staticDirs: reqOptions.staticDirs,
|
|
112
|
-
siteDir: reqOptions.siteDir,
|
|
113
|
-
},
|
|
114
|
-
],
|
|
115
|
-
...(reqOptions.remarkPlugins ?? []),
|
|
116
|
-
],
|
|
117
|
-
rehypePlugins: [
|
|
118
|
-
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
119
|
-
...DEFAULT_OPTIONS.rehypePlugins,
|
|
120
|
-
...(reqOptions.rehypePlugins ?? []),
|
|
121
|
-
],
|
|
122
|
-
};
|
|
123
|
-
compilerCache.set(this.query, [(0, mdx_1.createCompiler)(options), options]);
|
|
124
|
-
}
|
|
125
|
-
const [compiler, options] = compilerCache.get(this.query);
|
|
126
|
-
let result;
|
|
127
|
-
try {
|
|
128
|
-
result = await compiler
|
|
129
|
-
.process({
|
|
130
|
-
contents: content,
|
|
131
|
-
path: this.resourcePath,
|
|
132
|
-
})
|
|
133
|
-
.then((res) => res.toString());
|
|
134
|
-
}
|
|
135
|
-
catch (err) {
|
|
136
|
-
return callback(err);
|
|
137
|
-
}
|
|
138
|
-
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
139
|
-
// Partial are not expected to have associated metadata files or front matter
|
|
140
|
-
const isMDXPartial = options.isMDXPartial?.(filePath);
|
|
141
|
-
if (isMDXPartial && hasFrontMatter) {
|
|
142
|
-
const errorMessage = `Docusaurus MDX partial files should not contain front matter.
|
|
143
|
-
Those partial files use the _ prefix as a convention by default, but this is configurable.
|
|
144
|
-
File at ${filePath} contains front matter that will be ignored:
|
|
145
|
-
${JSON.stringify(frontMatter, null, 2)}`;
|
|
146
|
-
if (!options.isMDXPartialFrontMatterWarningDisabled) {
|
|
147
|
-
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
|
148
|
-
if (shouldError) {
|
|
149
|
-
return callback(new Error(errorMessage));
|
|
150
|
-
}
|
|
151
|
-
logger_1.default.warn(errorMessage);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
function getMetadataPath() {
|
|
155
|
-
if (!isMDXPartial) {
|
|
156
|
-
// Read metadata for this MDX and export it.
|
|
157
|
-
if (options.metadataPath && typeof options.metadataPath === 'function') {
|
|
158
|
-
return options.metadataPath(filePath);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return undefined;
|
|
162
|
-
}
|
|
163
|
-
const metadataPath = getMetadataPath();
|
|
164
|
-
if (metadataPath) {
|
|
165
|
-
this.addDependency(metadataPath);
|
|
166
|
-
}
|
|
167
|
-
const metadataJsonString = metadataPath
|
|
168
|
-
? await readMetadataPath(metadataPath)
|
|
169
|
-
: undefined;
|
|
170
|
-
const metadata = metadataJsonString
|
|
171
|
-
? JSON.parse(metadataJsonString)
|
|
172
|
-
: undefined;
|
|
173
|
-
const assets = reqOptions.createAssets && metadata
|
|
174
|
-
? reqOptions.createAssets({ frontMatter, metadata })
|
|
175
|
-
: undefined;
|
|
176
|
-
const exportsCode = `
|
|
177
|
-
export const frontMatter = ${(0, stringify_object_1.default)(frontMatter)};
|
|
178
|
-
export const contentTitle = ${(0, stringify_object_1.default)(contentTitle)};
|
|
179
|
-
${metadataJsonString ? `export const metadata = ${metadataJsonString};` : ''}
|
|
180
|
-
${assets ? `export const assets = ${createAssetsExportCode(assets)};` : ''}
|
|
181
|
-
`;
|
|
182
|
-
const code = `
|
|
183
|
-
${pragma}
|
|
184
|
-
import React from 'react';
|
|
185
|
-
import { mdx } from '@mdx-js/react';
|
|
186
|
-
|
|
187
|
-
${exportsCode}
|
|
188
|
-
${result}
|
|
189
|
-
`;
|
|
190
|
-
return callback(null, code);
|
|
191
|
-
}
|
|
192
|
-
exports.default = mdxLoader;
|
|
9
|
+
const loader_1 = require("./loader");
|
|
10
|
+
exports.default = loader_1.mdxLoader;
|
|
193
11
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAEH,qCAAmC;AAEnC,kBAAe,kBAAS,CAAC"}
|
package/lib/loader.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
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 { LoaderContext } from 'webpack';
|
|
8
|
+
import type { Plugin } from 'unified';
|
|
9
|
+
export declare type MDXPlugin = [
|
|
10
|
+
Plugin<any[]>,
|
|
11
|
+
any
|
|
12
|
+
] | Plugin<any[]>;
|
|
13
|
+
export declare type MDXOptions = {
|
|
14
|
+
remarkPlugins: MDXPlugin[];
|
|
15
|
+
rehypePlugins: MDXPlugin[];
|
|
16
|
+
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
17
|
+
beforeDefaultRehypePlugins: MDXPlugin[];
|
|
18
|
+
};
|
|
19
|
+
export declare type Options = MDXOptions & {
|
|
20
|
+
staticDirs: string[];
|
|
21
|
+
siteDir: string;
|
|
22
|
+
isMDXPartial?: (filePath: string) => boolean;
|
|
23
|
+
isMDXPartialFrontMatterWarningDisabled?: boolean;
|
|
24
|
+
removeContentTitle?: boolean;
|
|
25
|
+
metadataPath?: string | ((filePath: string) => string);
|
|
26
|
+
createAssets?: (metadata: {
|
|
27
|
+
frontMatter: {
|
|
28
|
+
[key: string]: unknown;
|
|
29
|
+
};
|
|
30
|
+
metadata: {
|
|
31
|
+
[key: string]: unknown;
|
|
32
|
+
};
|
|
33
|
+
}) => {
|
|
34
|
+
[key: string]: unknown;
|
|
35
|
+
};
|
|
36
|
+
filepath: string;
|
|
37
|
+
};
|
|
38
|
+
export declare function mdxLoader(this: LoaderContext<Options>, fileString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CAAC;CAClB,CAAC;AA6DF,wBAAsB,SAAS,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAyHf"}
|
package/lib/loader.js
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
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.mdxLoader = void 0;
|
|
10
|
+
const tslib_1 = require("tslib");
|
|
11
|
+
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
12
|
+
const mdx_1 = require("@mdx-js/mdx");
|
|
13
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
14
|
+
const remark_emoji_1 = tslib_1.__importDefault(require("remark-emoji"));
|
|
15
|
+
const utils_1 = require("@docusaurus/utils");
|
|
16
|
+
const stringify_object_1 = tslib_1.__importDefault(require("stringify-object"));
|
|
17
|
+
const headings_1 = tslib_1.__importDefault(require("./remark/headings"));
|
|
18
|
+
const toc_1 = tslib_1.__importDefault(require("./remark/toc"));
|
|
19
|
+
const unwrapMdxCodeBlocks_1 = tslib_1.__importDefault(require("./remark/unwrapMdxCodeBlocks"));
|
|
20
|
+
const transformImage_1 = tslib_1.__importDefault(require("./remark/transformImage"));
|
|
21
|
+
const transformLinks_1 = tslib_1.__importDefault(require("./remark/transformLinks"));
|
|
22
|
+
const { loaders: { inlineMarkdownImageFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
|
|
23
|
+
const pragma = `
|
|
24
|
+
/* @jsxRuntime classic */
|
|
25
|
+
/* @jsx mdx */
|
|
26
|
+
/* @jsxFrag mdx.Fragment */
|
|
27
|
+
`;
|
|
28
|
+
const DEFAULT_OPTIONS = {
|
|
29
|
+
rehypePlugins: [],
|
|
30
|
+
remarkPlugins: [unwrapMdxCodeBlocks_1.default, remark_emoji_1.default, headings_1.default, toc_1.default],
|
|
31
|
+
beforeDefaultRemarkPlugins: [],
|
|
32
|
+
beforeDefaultRehypePlugins: [],
|
|
33
|
+
};
|
|
34
|
+
const compilerCache = new Map();
|
|
35
|
+
/**
|
|
36
|
+
* When this throws, it generally means that there's no metadata file associated
|
|
37
|
+
* with this MDX document. It can happen when using MDX partials (usually
|
|
38
|
+
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
39
|
+
* function in config
|
|
40
|
+
*/
|
|
41
|
+
async function readMetadataPath(metadataPath) {
|
|
42
|
+
try {
|
|
43
|
+
return await fs_extra_1.default.readFile(metadataPath, 'utf8');
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
logger_1.default.error `MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
|
|
47
|
+
throw err;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Converts assets an object with Webpack require calls code.
|
|
52
|
+
* This is useful for mdx files to reference co-located assets using relative
|
|
53
|
+
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
54
|
+
* For now, we only handle that for images and paths starting with `./`:
|
|
55
|
+
*
|
|
56
|
+
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
57
|
+
*/
|
|
58
|
+
function createAssetsExportCode(assets) {
|
|
59
|
+
if (Object.keys(assets).length === 0) {
|
|
60
|
+
return 'undefined';
|
|
61
|
+
}
|
|
62
|
+
// TODO implementation can be completed/enhanced
|
|
63
|
+
function createAssetValueCode(assetValue) {
|
|
64
|
+
if (Array.isArray(assetValue)) {
|
|
65
|
+
const arrayItemCodes = assetValue.map((item) => createAssetValueCode(item) ?? 'undefined');
|
|
66
|
+
return `[${arrayItemCodes.join(', ')}]`;
|
|
67
|
+
}
|
|
68
|
+
// Only process string values starting with ./
|
|
69
|
+
// We could enhance this logic and check if file exists on disc?
|
|
70
|
+
if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
|
|
71
|
+
// TODO do we have other use-cases than image assets?
|
|
72
|
+
// Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
|
|
73
|
+
const inlineLoader = inlineMarkdownImageFileLoader;
|
|
74
|
+
return `require("${inlineLoader}${(0, utils_1.escapePath)(assetValue)}").default`;
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const assetEntries = Object.entries(assets);
|
|
79
|
+
const codeLines = assetEntries
|
|
80
|
+
.map(([key, value]) => {
|
|
81
|
+
const assetRequireCode = createAssetValueCode(value);
|
|
82
|
+
return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
|
|
83
|
+
})
|
|
84
|
+
.filter(Boolean);
|
|
85
|
+
return `{\n${codeLines.join('\n')}\n}`;
|
|
86
|
+
}
|
|
87
|
+
async function mdxLoader(fileString) {
|
|
88
|
+
const callback = this.async();
|
|
89
|
+
const filePath = this.resourcePath;
|
|
90
|
+
const reqOptions = this.getOptions() ?? {};
|
|
91
|
+
const { frontMatter, content: contentWithTitle } = (0, utils_1.parseFrontMatter)(fileString);
|
|
92
|
+
const { content, contentTitle } = (0, utils_1.parseMarkdownContentTitle)(contentWithTitle, {
|
|
93
|
+
removeContentTitle: reqOptions.removeContentTitle,
|
|
94
|
+
});
|
|
95
|
+
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
96
|
+
if (!compilerCache.has(this.query)) {
|
|
97
|
+
const options = {
|
|
98
|
+
...reqOptions,
|
|
99
|
+
remarkPlugins: [
|
|
100
|
+
...(reqOptions.beforeDefaultRemarkPlugins ?? []),
|
|
101
|
+
...DEFAULT_OPTIONS.remarkPlugins,
|
|
102
|
+
[
|
|
103
|
+
transformImage_1.default,
|
|
104
|
+
{
|
|
105
|
+
staticDirs: reqOptions.staticDirs,
|
|
106
|
+
siteDir: reqOptions.siteDir,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
[
|
|
110
|
+
transformLinks_1.default,
|
|
111
|
+
{
|
|
112
|
+
staticDirs: reqOptions.staticDirs,
|
|
113
|
+
siteDir: reqOptions.siteDir,
|
|
114
|
+
},
|
|
115
|
+
],
|
|
116
|
+
...(reqOptions.remarkPlugins ?? []),
|
|
117
|
+
],
|
|
118
|
+
rehypePlugins: [
|
|
119
|
+
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
120
|
+
...DEFAULT_OPTIONS.rehypePlugins,
|
|
121
|
+
...(reqOptions.rehypePlugins ?? []),
|
|
122
|
+
],
|
|
123
|
+
};
|
|
124
|
+
compilerCache.set(this.query, [(0, mdx_1.createCompiler)(options), options]);
|
|
125
|
+
}
|
|
126
|
+
const [compiler, options] = compilerCache.get(this.query);
|
|
127
|
+
let result;
|
|
128
|
+
try {
|
|
129
|
+
result = await compiler
|
|
130
|
+
.process({
|
|
131
|
+
contents: content,
|
|
132
|
+
path: this.resourcePath,
|
|
133
|
+
})
|
|
134
|
+
.then((res) => res.toString());
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
return callback(err);
|
|
138
|
+
}
|
|
139
|
+
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
140
|
+
// Partial are not expected to have associated metadata files or front matter
|
|
141
|
+
const isMDXPartial = options.isMDXPartial?.(filePath);
|
|
142
|
+
if (isMDXPartial && hasFrontMatter) {
|
|
143
|
+
const errorMessage = `Docusaurus MDX partial files should not contain front matter.
|
|
144
|
+
Those partial files use the _ prefix as a convention by default, but this is configurable.
|
|
145
|
+
File at ${filePath} contains front matter that will be ignored:
|
|
146
|
+
${JSON.stringify(frontMatter, null, 2)}`;
|
|
147
|
+
if (!options.isMDXPartialFrontMatterWarningDisabled) {
|
|
148
|
+
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
|
149
|
+
if (shouldError) {
|
|
150
|
+
return callback(new Error(errorMessage));
|
|
151
|
+
}
|
|
152
|
+
logger_1.default.warn(errorMessage);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function getMetadataPath() {
|
|
156
|
+
if (!isMDXPartial) {
|
|
157
|
+
// Read metadata for this MDX and export it.
|
|
158
|
+
if (options.metadataPath && typeof options.metadataPath === 'function') {
|
|
159
|
+
return options.metadataPath(filePath);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const metadataPath = getMetadataPath();
|
|
165
|
+
if (metadataPath) {
|
|
166
|
+
this.addDependency(metadataPath);
|
|
167
|
+
}
|
|
168
|
+
const metadataJsonString = metadataPath
|
|
169
|
+
? await readMetadataPath(metadataPath)
|
|
170
|
+
: undefined;
|
|
171
|
+
const metadata = metadataJsonString
|
|
172
|
+
? JSON.parse(metadataJsonString)
|
|
173
|
+
: undefined;
|
|
174
|
+
const assets = reqOptions.createAssets && metadata
|
|
175
|
+
? reqOptions.createAssets({ frontMatter, metadata })
|
|
176
|
+
: undefined;
|
|
177
|
+
const exportsCode = `
|
|
178
|
+
export const frontMatter = ${(0, stringify_object_1.default)(frontMatter)};
|
|
179
|
+
export const contentTitle = ${(0, stringify_object_1.default)(contentTitle)};
|
|
180
|
+
${metadataJsonString ? `export const metadata = ${metadataJsonString};` : ''}
|
|
181
|
+
${assets ? `export const assets = ${createAssetsExportCode(assets)};` : ''}
|
|
182
|
+
`;
|
|
183
|
+
const code = `
|
|
184
|
+
${pragma}
|
|
185
|
+
import React from 'react';
|
|
186
|
+
import { mdx } from '@mdx-js/react';
|
|
187
|
+
|
|
188
|
+
${exportsCode}
|
|
189
|
+
${result}
|
|
190
|
+
`;
|
|
191
|
+
return callback(null, code);
|
|
192
|
+
}
|
|
193
|
+
exports.mdxLoader = mdxLoader;
|
|
194
|
+
//# sourceMappingURL=loader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
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AS,CAAC;IAEd,MAAM,QAAQ,GAAG,kBAAkB;QACjC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,kBAAkB,CAAC;QAChC,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;AA5HD,8BA4HC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/mdx-loader",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-4945",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
-
"types": "
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
7
|
"publishConfig": {
|
|
8
8
|
"access": "public"
|
|
9
9
|
},
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@babel/parser": "^7.17.10",
|
|
22
22
|
"@babel/traverse": "^7.17.10",
|
|
23
|
-
"@docusaurus/logger": "0.0.0-
|
|
24
|
-
"@docusaurus/utils": "0.0.0-
|
|
23
|
+
"@docusaurus/logger": "0.0.0-4945",
|
|
24
|
+
"@docusaurus/utils": "0.0.0-4945",
|
|
25
25
|
"@mdx-js/mdx": "^1.6.22",
|
|
26
26
|
"escape-html": "^1.0.3",
|
|
27
27
|
"file-loader": "^6.2.0",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"webpack": "^5.72.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@docusaurus/types": "0.0.0-
|
|
39
|
+
"@docusaurus/types": "0.0.0-4945",
|
|
40
40
|
"@types/escape-html": "^1.0.2",
|
|
41
41
|
"@types/mdast": "^3.0.10",
|
|
42
42
|
"@types/stringify-object": "^3.3.1",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"engines": {
|
|
55
55
|
"node": ">=14"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "a3b20e1bfad392b7f91afef66de502e41a3adcb4"
|
|
58
58
|
}
|
package/src/deps.d.ts
CHANGED
|
@@ -7,10 +7,13 @@
|
|
|
7
7
|
|
|
8
8
|
// TODO Types provided by MDX 2.0 https://github.com/mdx-js/mdx/blob/main/packages/mdx/types/index.d.ts
|
|
9
9
|
declare module '@mdx-js/mdx' {
|
|
10
|
-
import type {Processor} from 'unified';
|
|
11
|
-
import type {MDXPlugin} from '@docusaurus/mdx-loader';
|
|
10
|
+
import type {Processor, Plugin} from 'unified';
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
type MDXPlugin =
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
[Plugin<any[]>, any] | Plugin<any[]>;
|
|
15
|
+
|
|
16
|
+
type Options = {
|
|
14
17
|
filepath?: string;
|
|
15
18
|
skipExport?: boolean;
|
|
16
19
|
wrapExport?: string;
|
package/src/index.ts
CHANGED
|
@@ -5,240 +5,31 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
import {createCompiler} from '@mdx-js/mdx';
|
|
10
|
-
import logger from '@docusaurus/logger';
|
|
11
|
-
import emoji from 'remark-emoji';
|
|
12
|
-
import {
|
|
13
|
-
parseFrontMatter,
|
|
14
|
-
parseMarkdownContentTitle,
|
|
15
|
-
escapePath,
|
|
16
|
-
getFileLoaderUtils,
|
|
17
|
-
} from '@docusaurus/utils';
|
|
18
|
-
import stringifyObject from 'stringify-object';
|
|
19
|
-
import headings from './remark/headings';
|
|
20
|
-
import toc from './remark/toc';
|
|
21
|
-
import unwrapMdxCodeBlocks from './remark/unwrapMdxCodeBlocks';
|
|
22
|
-
import transformImage from './remark/transformImage';
|
|
23
|
-
import transformLinks from './remark/transformLinks';
|
|
24
|
-
import type {MDXOptions} from '@docusaurus/mdx-loader';
|
|
25
|
-
import type {LoaderContext} from 'webpack';
|
|
26
|
-
import type {Processor} from 'unified';
|
|
8
|
+
import {mdxLoader} from './loader';
|
|
27
9
|
|
|
28
|
-
|
|
29
|
-
loaders: {inlineMarkdownImageFileLoader},
|
|
30
|
-
} = getFileLoaderUtils();
|
|
10
|
+
export default mdxLoader;
|
|
31
11
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
`;
|
|
37
|
-
|
|
38
|
-
const DEFAULT_OPTIONS: MDXOptions = {
|
|
39
|
-
rehypePlugins: [],
|
|
40
|
-
remarkPlugins: [unwrapMdxCodeBlocks, emoji, headings, toc],
|
|
41
|
-
beforeDefaultRemarkPlugins: [],
|
|
42
|
-
beforeDefaultRehypePlugins: [],
|
|
12
|
+
export type TOCItem = {
|
|
13
|
+
readonly value: string;
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly level: number;
|
|
43
16
|
};
|
|
44
17
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
18
|
+
export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
|
19
|
+
/** As verbatim declared in the MDX document. */
|
|
20
|
+
readonly frontMatter: FrontMatter;
|
|
21
|
+
/** As provided by the content plugin. */
|
|
22
|
+
readonly metadata: Metadata;
|
|
23
|
+
/** A list of TOC items (headings). */
|
|
24
|
+
readonly toc: readonly TOCItem[];
|
|
25
|
+
/** First h1 title before any content. */
|
|
26
|
+
readonly contentTitle: string | undefined;
|
|
27
|
+
/**
|
|
28
|
+
* Usually image assets that may be collocated like `./img/thumbnail.png`.
|
|
29
|
+
* The loader would also bundle these assets and the client should use these
|
|
30
|
+
* in priority.
|
|
31
|
+
*/
|
|
32
|
+
readonly assets: Assets;
|
|
33
|
+
(): JSX.Element;
|
|
59
34
|
};
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* When this throws, it generally means that there's no metadata file associated
|
|
63
|
-
* with this MDX document. It can happen when using MDX partials (usually
|
|
64
|
-
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
65
|
-
* function in config
|
|
66
|
-
*/
|
|
67
|
-
async function readMetadataPath(metadataPath: string) {
|
|
68
|
-
try {
|
|
69
|
-
return await fs.readFile(metadataPath, 'utf8');
|
|
70
|
-
} catch (err) {
|
|
71
|
-
logger.error`MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
|
|
72
|
-
throw err;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Converts assets an object with Webpack require calls code.
|
|
78
|
-
* This is useful for mdx files to reference co-located assets using relative
|
|
79
|
-
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
80
|
-
* For now, we only handle that for images and paths starting with `./`:
|
|
81
|
-
*
|
|
82
|
-
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
83
|
-
*/
|
|
84
|
-
function createAssetsExportCode(assets: {[key: string]: unknown}) {
|
|
85
|
-
if (Object.keys(assets).length === 0) {
|
|
86
|
-
return 'undefined';
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// TODO implementation can be completed/enhanced
|
|
90
|
-
function createAssetValueCode(assetValue: unknown): string | undefined {
|
|
91
|
-
if (Array.isArray(assetValue)) {
|
|
92
|
-
const arrayItemCodes = assetValue.map(
|
|
93
|
-
(item) => createAssetValueCode(item) ?? 'undefined',
|
|
94
|
-
);
|
|
95
|
-
return `[${arrayItemCodes.join(', ')}]`;
|
|
96
|
-
}
|
|
97
|
-
// Only process string values starting with ./
|
|
98
|
-
// We could enhance this logic and check if file exists on disc?
|
|
99
|
-
if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
|
|
100
|
-
// TODO do we have other use-cases than image assets?
|
|
101
|
-
// Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
|
|
102
|
-
const inlineLoader = inlineMarkdownImageFileLoader;
|
|
103
|
-
return `require("${inlineLoader}${escapePath(assetValue)}").default`;
|
|
104
|
-
}
|
|
105
|
-
return undefined;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const assetEntries = Object.entries(assets);
|
|
109
|
-
|
|
110
|
-
const codeLines = assetEntries
|
|
111
|
-
.map(([key, value]) => {
|
|
112
|
-
const assetRequireCode = createAssetValueCode(value);
|
|
113
|
-
return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
|
|
114
|
-
})
|
|
115
|
-
.filter(Boolean);
|
|
116
|
-
|
|
117
|
-
return `{\n${codeLines.join('\n')}\n}`;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export default async function mdxLoader(
|
|
121
|
-
this: LoaderContext<Options>,
|
|
122
|
-
fileString: string,
|
|
123
|
-
): Promise<void> {
|
|
124
|
-
const callback = this.async();
|
|
125
|
-
const filePath = this.resourcePath;
|
|
126
|
-
const reqOptions = this.getOptions() ?? {};
|
|
127
|
-
|
|
128
|
-
const {frontMatter, content: contentWithTitle} = parseFrontMatter(fileString);
|
|
129
|
-
|
|
130
|
-
const {content, contentTitle} = parseMarkdownContentTitle(contentWithTitle, {
|
|
131
|
-
removeContentTitle: reqOptions.removeContentTitle,
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
135
|
-
|
|
136
|
-
if (!compilerCache.has(this.query)) {
|
|
137
|
-
const options: Options = {
|
|
138
|
-
...reqOptions,
|
|
139
|
-
remarkPlugins: [
|
|
140
|
-
...(reqOptions.beforeDefaultRemarkPlugins ?? []),
|
|
141
|
-
...DEFAULT_OPTIONS.remarkPlugins,
|
|
142
|
-
[
|
|
143
|
-
transformImage,
|
|
144
|
-
{
|
|
145
|
-
staticDirs: reqOptions.staticDirs,
|
|
146
|
-
siteDir: reqOptions.siteDir,
|
|
147
|
-
},
|
|
148
|
-
],
|
|
149
|
-
[
|
|
150
|
-
transformLinks,
|
|
151
|
-
{
|
|
152
|
-
staticDirs: reqOptions.staticDirs,
|
|
153
|
-
siteDir: reqOptions.siteDir,
|
|
154
|
-
},
|
|
155
|
-
],
|
|
156
|
-
...(reqOptions.remarkPlugins ?? []),
|
|
157
|
-
],
|
|
158
|
-
rehypePlugins: [
|
|
159
|
-
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
160
|
-
...DEFAULT_OPTIONS.rehypePlugins,
|
|
161
|
-
...(reqOptions.rehypePlugins ?? []),
|
|
162
|
-
],
|
|
163
|
-
};
|
|
164
|
-
compilerCache.set(this.query, [createCompiler(options), options]);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const [compiler, options] = compilerCache.get(this.query)!;
|
|
168
|
-
|
|
169
|
-
let result: string;
|
|
170
|
-
try {
|
|
171
|
-
result = await compiler
|
|
172
|
-
.process({
|
|
173
|
-
contents: content,
|
|
174
|
-
path: this.resourcePath,
|
|
175
|
-
})
|
|
176
|
-
.then((res) => res.toString());
|
|
177
|
-
} catch (err) {
|
|
178
|
-
return callback(err as Error);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
182
|
-
// Partial are not expected to have associated metadata files or front matter
|
|
183
|
-
const isMDXPartial = options.isMDXPartial?.(filePath);
|
|
184
|
-
if (isMDXPartial && hasFrontMatter) {
|
|
185
|
-
const errorMessage = `Docusaurus MDX partial files should not contain front matter.
|
|
186
|
-
Those partial files use the _ prefix as a convention by default, but this is configurable.
|
|
187
|
-
File at ${filePath} contains front matter that will be ignored:
|
|
188
|
-
${JSON.stringify(frontMatter, null, 2)}`;
|
|
189
|
-
|
|
190
|
-
if (!options.isMDXPartialFrontMatterWarningDisabled) {
|
|
191
|
-
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
|
192
|
-
if (shouldError) {
|
|
193
|
-
return callback(new Error(errorMessage));
|
|
194
|
-
}
|
|
195
|
-
logger.warn(errorMessage);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function getMetadataPath(): string | undefined {
|
|
200
|
-
if (!isMDXPartial) {
|
|
201
|
-
// Read metadata for this MDX and export it.
|
|
202
|
-
if (options.metadataPath && typeof options.metadataPath === 'function') {
|
|
203
|
-
return options.metadataPath(filePath);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
return undefined;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const metadataPath = getMetadataPath();
|
|
210
|
-
if (metadataPath) {
|
|
211
|
-
this.addDependency(metadataPath);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const metadataJsonString = metadataPath
|
|
215
|
-
? await readMetadataPath(metadataPath)
|
|
216
|
-
: undefined;
|
|
217
|
-
|
|
218
|
-
const metadata = metadataJsonString
|
|
219
|
-
? JSON.parse(metadataJsonString)
|
|
220
|
-
: undefined;
|
|
221
|
-
|
|
222
|
-
const assets =
|
|
223
|
-
reqOptions.createAssets && metadata
|
|
224
|
-
? reqOptions.createAssets({frontMatter, metadata})
|
|
225
|
-
: undefined;
|
|
226
|
-
|
|
227
|
-
const exportsCode = `
|
|
228
|
-
export const frontMatter = ${stringifyObject(frontMatter)};
|
|
229
|
-
export const contentTitle = ${stringifyObject(contentTitle)};
|
|
230
|
-
${metadataJsonString ? `export const metadata = ${metadataJsonString};` : ''}
|
|
231
|
-
${assets ? `export const assets = ${createAssetsExportCode(assets)};` : ''}
|
|
232
|
-
`;
|
|
233
|
-
|
|
234
|
-
const code = `
|
|
235
|
-
${pragma}
|
|
236
|
-
import React from 'react';
|
|
237
|
-
import { mdx } from '@mdx-js/react';
|
|
238
|
-
|
|
239
|
-
${exportsCode}
|
|
240
|
-
${result}
|
|
241
|
-
`;
|
|
242
|
-
|
|
243
|
-
return callback(null, code);
|
|
244
|
-
}
|
|
35
|
+
export type {Options, MDXPlugin, MDXOptions} from './loader';
|
package/src/loader.ts
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
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 fs from 'fs-extra';
|
|
9
|
+
import {createCompiler} from '@mdx-js/mdx';
|
|
10
|
+
import logger from '@docusaurus/logger';
|
|
11
|
+
import emoji from 'remark-emoji';
|
|
12
|
+
import {
|
|
13
|
+
parseFrontMatter,
|
|
14
|
+
parseMarkdownContentTitle,
|
|
15
|
+
escapePath,
|
|
16
|
+
getFileLoaderUtils,
|
|
17
|
+
} from '@docusaurus/utils';
|
|
18
|
+
import stringifyObject from 'stringify-object';
|
|
19
|
+
import headings from './remark/headings';
|
|
20
|
+
import toc from './remark/toc';
|
|
21
|
+
import unwrapMdxCodeBlocks from './remark/unwrapMdxCodeBlocks';
|
|
22
|
+
import transformImage from './remark/transformImage';
|
|
23
|
+
import transformLinks from './remark/transformLinks';
|
|
24
|
+
import type {LoaderContext} from 'webpack';
|
|
25
|
+
import type {Processor, Plugin} from 'unified';
|
|
26
|
+
|
|
27
|
+
const {
|
|
28
|
+
loaders: {inlineMarkdownImageFileLoader},
|
|
29
|
+
} = getFileLoaderUtils();
|
|
30
|
+
|
|
31
|
+
const pragma = `
|
|
32
|
+
/* @jsxRuntime classic */
|
|
33
|
+
/* @jsx mdx */
|
|
34
|
+
/* @jsxFrag mdx.Fragment */
|
|
35
|
+
`;
|
|
36
|
+
|
|
37
|
+
const DEFAULT_OPTIONS: MDXOptions = {
|
|
38
|
+
rehypePlugins: [],
|
|
39
|
+
remarkPlugins: [unwrapMdxCodeBlocks, emoji, headings, toc],
|
|
40
|
+
beforeDefaultRemarkPlugins: [],
|
|
41
|
+
beforeDefaultRehypePlugins: [],
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const compilerCache = new Map<string | Options, [Processor, Options]>();
|
|
45
|
+
|
|
46
|
+
export type MDXPlugin =
|
|
47
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
+
[Plugin<any[]>, any] | Plugin<any[]>;
|
|
49
|
+
export type MDXOptions = {
|
|
50
|
+
remarkPlugins: MDXPlugin[];
|
|
51
|
+
rehypePlugins: MDXPlugin[];
|
|
52
|
+
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
53
|
+
beforeDefaultRehypePlugins: MDXPlugin[];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type Options = MDXOptions & {
|
|
57
|
+
staticDirs: string[];
|
|
58
|
+
siteDir: string;
|
|
59
|
+
isMDXPartial?: (filePath: string) => boolean;
|
|
60
|
+
isMDXPartialFrontMatterWarningDisabled?: boolean;
|
|
61
|
+
removeContentTitle?: boolean;
|
|
62
|
+
metadataPath?: string | ((filePath: string) => string);
|
|
63
|
+
createAssets?: (metadata: {
|
|
64
|
+
frontMatter: {[key: string]: unknown};
|
|
65
|
+
metadata: {[key: string]: unknown};
|
|
66
|
+
}) => {[key: string]: unknown};
|
|
67
|
+
filepath: string;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* When this throws, it generally means that there's no metadata file associated
|
|
72
|
+
* with this MDX document. It can happen when using MDX partials (usually
|
|
73
|
+
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
74
|
+
* function in config
|
|
75
|
+
*/
|
|
76
|
+
async function readMetadataPath(metadataPath: string) {
|
|
77
|
+
try {
|
|
78
|
+
return await fs.readFile(metadataPath, 'utf8');
|
|
79
|
+
} catch (err) {
|
|
80
|
+
logger.error`MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Converts assets an object with Webpack require calls code.
|
|
87
|
+
* This is useful for mdx files to reference co-located assets using relative
|
|
88
|
+
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
89
|
+
* For now, we only handle that for images and paths starting with `./`:
|
|
90
|
+
*
|
|
91
|
+
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
92
|
+
*/
|
|
93
|
+
function createAssetsExportCode(assets: {[key: string]: unknown}) {
|
|
94
|
+
if (Object.keys(assets).length === 0) {
|
|
95
|
+
return 'undefined';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// TODO implementation can be completed/enhanced
|
|
99
|
+
function createAssetValueCode(assetValue: unknown): string | undefined {
|
|
100
|
+
if (Array.isArray(assetValue)) {
|
|
101
|
+
const arrayItemCodes = assetValue.map(
|
|
102
|
+
(item) => createAssetValueCode(item) ?? 'undefined',
|
|
103
|
+
);
|
|
104
|
+
return `[${arrayItemCodes.join(', ')}]`;
|
|
105
|
+
}
|
|
106
|
+
// Only process string values starting with ./
|
|
107
|
+
// We could enhance this logic and check if file exists on disc?
|
|
108
|
+
if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
|
|
109
|
+
// TODO do we have other use-cases than image assets?
|
|
110
|
+
// Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
|
|
111
|
+
const inlineLoader = inlineMarkdownImageFileLoader;
|
|
112
|
+
return `require("${inlineLoader}${escapePath(assetValue)}").default`;
|
|
113
|
+
}
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const assetEntries = Object.entries(assets);
|
|
118
|
+
|
|
119
|
+
const codeLines = assetEntries
|
|
120
|
+
.map(([key, value]) => {
|
|
121
|
+
const assetRequireCode = createAssetValueCode(value);
|
|
122
|
+
return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
|
|
123
|
+
})
|
|
124
|
+
.filter(Boolean);
|
|
125
|
+
|
|
126
|
+
return `{\n${codeLines.join('\n')}\n}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function mdxLoader(
|
|
130
|
+
this: LoaderContext<Options>,
|
|
131
|
+
fileString: string,
|
|
132
|
+
): Promise<void> {
|
|
133
|
+
const callback = this.async();
|
|
134
|
+
const filePath = this.resourcePath;
|
|
135
|
+
const reqOptions = this.getOptions() ?? {};
|
|
136
|
+
|
|
137
|
+
const {frontMatter, content: contentWithTitle} = parseFrontMatter(fileString);
|
|
138
|
+
|
|
139
|
+
const {content, contentTitle} = parseMarkdownContentTitle(contentWithTitle, {
|
|
140
|
+
removeContentTitle: reqOptions.removeContentTitle,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
144
|
+
|
|
145
|
+
if (!compilerCache.has(this.query)) {
|
|
146
|
+
const options: Options = {
|
|
147
|
+
...reqOptions,
|
|
148
|
+
remarkPlugins: [
|
|
149
|
+
...(reqOptions.beforeDefaultRemarkPlugins ?? []),
|
|
150
|
+
...DEFAULT_OPTIONS.remarkPlugins,
|
|
151
|
+
[
|
|
152
|
+
transformImage,
|
|
153
|
+
{
|
|
154
|
+
staticDirs: reqOptions.staticDirs,
|
|
155
|
+
siteDir: reqOptions.siteDir,
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
[
|
|
159
|
+
transformLinks,
|
|
160
|
+
{
|
|
161
|
+
staticDirs: reqOptions.staticDirs,
|
|
162
|
+
siteDir: reqOptions.siteDir,
|
|
163
|
+
},
|
|
164
|
+
],
|
|
165
|
+
...(reqOptions.remarkPlugins ?? []),
|
|
166
|
+
],
|
|
167
|
+
rehypePlugins: [
|
|
168
|
+
...(reqOptions.beforeDefaultRehypePlugins ?? []),
|
|
169
|
+
...DEFAULT_OPTIONS.rehypePlugins,
|
|
170
|
+
...(reqOptions.rehypePlugins ?? []),
|
|
171
|
+
],
|
|
172
|
+
};
|
|
173
|
+
compilerCache.set(this.query, [createCompiler(options), options]);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const [compiler, options] = compilerCache.get(this.query)!;
|
|
177
|
+
|
|
178
|
+
let result: string;
|
|
179
|
+
try {
|
|
180
|
+
result = await compiler
|
|
181
|
+
.process({
|
|
182
|
+
contents: content,
|
|
183
|
+
path: this.resourcePath,
|
|
184
|
+
})
|
|
185
|
+
.then((res) => res.toString());
|
|
186
|
+
} catch (err) {
|
|
187
|
+
return callback(err as Error);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
191
|
+
// Partial are not expected to have associated metadata files or front matter
|
|
192
|
+
const isMDXPartial = options.isMDXPartial?.(filePath);
|
|
193
|
+
if (isMDXPartial && hasFrontMatter) {
|
|
194
|
+
const errorMessage = `Docusaurus MDX partial files should not contain front matter.
|
|
195
|
+
Those partial files use the _ prefix as a convention by default, but this is configurable.
|
|
196
|
+
File at ${filePath} contains front matter that will be ignored:
|
|
197
|
+
${JSON.stringify(frontMatter, null, 2)}`;
|
|
198
|
+
|
|
199
|
+
if (!options.isMDXPartialFrontMatterWarningDisabled) {
|
|
200
|
+
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
|
201
|
+
if (shouldError) {
|
|
202
|
+
return callback(new Error(errorMessage));
|
|
203
|
+
}
|
|
204
|
+
logger.warn(errorMessage);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function getMetadataPath(): string | undefined {
|
|
209
|
+
if (!isMDXPartial) {
|
|
210
|
+
// Read metadata for this MDX and export it.
|
|
211
|
+
if (options.metadataPath && typeof options.metadataPath === 'function') {
|
|
212
|
+
return options.metadataPath(filePath);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const metadataPath = getMetadataPath();
|
|
219
|
+
if (metadataPath) {
|
|
220
|
+
this.addDependency(metadataPath);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const metadataJsonString = metadataPath
|
|
224
|
+
? await readMetadataPath(metadataPath)
|
|
225
|
+
: undefined;
|
|
226
|
+
|
|
227
|
+
const metadata = metadataJsonString
|
|
228
|
+
? JSON.parse(metadataJsonString)
|
|
229
|
+
: undefined;
|
|
230
|
+
|
|
231
|
+
const assets =
|
|
232
|
+
reqOptions.createAssets && metadata
|
|
233
|
+
? reqOptions.createAssets({frontMatter, metadata})
|
|
234
|
+
: undefined;
|
|
235
|
+
|
|
236
|
+
const exportsCode = `
|
|
237
|
+
export const frontMatter = ${stringifyObject(frontMatter)};
|
|
238
|
+
export const contentTitle = ${stringifyObject(contentTitle)};
|
|
239
|
+
${metadataJsonString ? `export const metadata = ${metadataJsonString};` : ''}
|
|
240
|
+
${assets ? `export const assets = ${createAssetsExportCode(assets)};` : ''}
|
|
241
|
+
`;
|
|
242
|
+
|
|
243
|
+
const code = `
|
|
244
|
+
${pragma}
|
|
245
|
+
import React from 'react';
|
|
246
|
+
import { mdx } from '@mdx-js/react';
|
|
247
|
+
|
|
248
|
+
${exportsCode}
|
|
249
|
+
${result}
|
|
250
|
+
`;
|
|
251
|
+
|
|
252
|
+
return callback(null, code);
|
|
253
|
+
}
|
package/src/remark/toc/index.ts
CHANGED
|
@@ -13,7 +13,7 @@ import toString from 'mdast-util-to-string';
|
|
|
13
13
|
import visit from 'unist-util-visit';
|
|
14
14
|
import {toValue} from '../utils';
|
|
15
15
|
|
|
16
|
-
import type {TOCItem} from '
|
|
16
|
+
import type {TOCItem} from '../..';
|
|
17
17
|
import type {Node, Parent} from 'unist';
|
|
18
18
|
import type {Heading, Literal} from 'mdast';
|
|
19
19
|
import type {Transformer} from 'unified';
|
package/src/mdx-loader.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
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 type {Plugin} from 'unified';
|
|
9
|
-
import type {TOCItem} from '@docusaurus/types';
|
|
10
|
-
|
|
11
|
-
export type MDXPlugin =
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
-
[Plugin<any[]>, any] | Plugin<any[]>;
|
|
14
|
-
export type MDXOptions = {
|
|
15
|
-
remarkPlugins: MDXPlugin[];
|
|
16
|
-
rehypePlugins: MDXPlugin[];
|
|
17
|
-
beforeDefaultRemarkPlugins: MDXPlugin[];
|
|
18
|
-
beforeDefaultRehypePlugins: MDXPlugin[];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
|
22
|
-
/** As verbatim declared in the MDX document. */
|
|
23
|
-
readonly frontMatter: FrontMatter;
|
|
24
|
-
/** As provided by the content plugin. */
|
|
25
|
-
readonly metadata: Metadata;
|
|
26
|
-
/** A list of TOC items (headings). */
|
|
27
|
-
readonly toc: readonly TOCItem[];
|
|
28
|
-
/** First h1 title before any content. */
|
|
29
|
-
readonly contentTitle: string | undefined;
|
|
30
|
-
/**
|
|
31
|
-
* Usually image assets that may be collocated like `./img/thumbnail.png`.
|
|
32
|
-
* The loader would also bundle these assets and the client should use these
|
|
33
|
-
* in priority.
|
|
34
|
-
*/
|
|
35
|
-
readonly assets: Assets;
|
|
36
|
-
(): JSX.Element;
|
|
37
|
-
};
|