@docusaurus/mdx-loader 0.0.0-6056 → 0.0.0-6060
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/createMDXLoader.d.ts +14 -0
- package/lib/createMDXLoader.d.ts.map +1 -0
- package/lib/createMDXLoader.js +37 -0
- package/lib/createMDXLoader.js.map +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +4 -0
- package/lib/index.js.map +1 -1
- package/lib/loader.d.ts +2 -1
- package/lib/loader.d.ts.map +1 -1
- package/lib/loader.js +9 -101
- package/lib/loader.js.map +1 -1
- package/lib/processor.d.ts +10 -3
- package/lib/processor.d.ts.map +1 -1
- package/lib/processor.js +16 -12
- package/lib/processor.js.map +1 -1
- package/lib/utils.d.ts +42 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +113 -0
- package/lib/utils.js.map +1 -0
- package/package.json +6 -6
- package/src/createMDXLoader.ts +48 -0
- package/src/index.ts +2 -0
- package/src/loader.ts +20 -136
- package/src/processor.ts +27 -19
- package/src/utils.ts +152 -0
|
@@ -0,0 +1,14 @@
|
|
|
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 { Options } from './loader';
|
|
8
|
+
import type { RuleSetRule, RuleSetUseItem } from 'webpack';
|
|
9
|
+
export declare function createMDXLoaderItem(options: Options): Promise<RuleSetUseItem>;
|
|
10
|
+
export declare function createMDXLoaderRule({ include, options, }: {
|
|
11
|
+
include: RuleSetRule['include'];
|
|
12
|
+
options: Options;
|
|
13
|
+
}): Promise<RuleSetRule>;
|
|
14
|
+
//# sourceMappingURL=createMDXLoader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createMDXLoader.d.ts","sourceRoot":"","sources":["../src/createMDXLoader.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,UAAU,CAAC;AACtC,OAAO,KAAK,EAAC,WAAW,EAAE,cAAc,EAAC,MAAM,SAAS,CAAC;AAiBzD,wBAAsB,mBAAmB,CACvC,OAAO,EAAE,OAAO,GACf,OAAO,CAAC,cAAc,CAAC,CAKzB;AAED,wBAAsB,mBAAmB,CAAC,EACxC,OAAO,EACP,OAAO,GACR,EAAE;IACD,OAAO,EAAE,WAAW,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,EAAE,OAAO,CAAC;CAClB,GAAG,OAAO,CAAC,WAAW,CAAC,CAMvB"}
|
|
@@ -0,0 +1,37 @@
|
|
|
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.createMDXLoaderItem = createMDXLoaderItem;
|
|
10
|
+
exports.createMDXLoaderRule = createMDXLoaderRule;
|
|
11
|
+
const processor_1 = require("./processor");
|
|
12
|
+
async function enhancedOptions(options) {
|
|
13
|
+
// Because Jest doesn't like ESM / createProcessors()
|
|
14
|
+
if (process.env.N0DE_ENV === 'test' || process.env.JEST_WORKER_ID) {
|
|
15
|
+
return options;
|
|
16
|
+
}
|
|
17
|
+
// We create the processor earlier here, to avoid the lazy processor creating
|
|
18
|
+
// Lazy creation messes-up with Rsdoctor ability to measure mdx-loader perf
|
|
19
|
+
const newOptions = options.processors
|
|
20
|
+
? options
|
|
21
|
+
: { ...options, processors: await (0, processor_1.createProcessors)({ options }) };
|
|
22
|
+
return newOptions;
|
|
23
|
+
}
|
|
24
|
+
async function createMDXLoaderItem(options) {
|
|
25
|
+
return {
|
|
26
|
+
loader: require.resolve('@docusaurus/mdx-loader'),
|
|
27
|
+
options: await enhancedOptions(options),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async function createMDXLoaderRule({ include, options, }) {
|
|
31
|
+
return {
|
|
32
|
+
test: /\.mdx?$/i,
|
|
33
|
+
include,
|
|
34
|
+
use: [await createMDXLoaderItem(options)],
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=createMDXLoader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createMDXLoader.js","sourceRoot":"","sources":["../src/createMDXLoader.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAqBH,kDAOC;AAED,kDAYC;AAxCD,2CAA6C;AAI7C,KAAK,UAAU,eAAe,CAAC,OAAgB;IAC7C,qDAAqD;IACrD,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,MAAM,IAAI,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;QAClE,OAAO,OAAO,CAAC;IACjB,CAAC;IAED,6EAA6E;IAC7E,2EAA2E;IAC3E,MAAM,UAAU,GAAY,OAAO,CAAC,UAAU;QAC5C,CAAC,CAAC,OAAO;QACT,CAAC,CAAC,EAAC,GAAG,OAAO,EAAE,UAAU,EAAE,MAAM,IAAA,4BAAgB,EAAC,EAAC,OAAO,EAAC,CAAC,EAAC,CAAC;IAEhE,OAAO,UAAU,CAAC;AACpB,CAAC;AAEM,KAAK,UAAU,mBAAmB,CACvC,OAAgB;IAEhB,OAAO;QACL,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,wBAAwB,CAAC;QACjD,OAAO,EAAE,MAAM,eAAe,CAAC,OAAO,CAAC;KACxC,CAAC;AACJ,CAAC;AAEM,KAAK,UAAU,mBAAmB,CAAC,EACxC,OAAO,EACP,OAAO,GAIR;IACC,OAAO;QACL,IAAI,EAAE,UAAU;QAChB,OAAO;QACP,GAAG,EAAE,CAAC,MAAM,mBAAmB,CAAC,OAAO,CAAC,CAAC;KAC1C,CAAC;AACJ,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { mdxLoader } from './loader';
|
|
8
8
|
import type { TOCItem as TOCItemImported } from './remark/toc/types';
|
|
9
|
+
export { createMDXLoaderRule, createMDXLoaderItem } from './createMDXLoader';
|
|
9
10
|
export default mdxLoader;
|
|
10
11
|
export type TOCItem = TOCItemImported;
|
|
11
12
|
export type Format = 'md' | 'mdx';
|
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,oBAAoB,CAAC;AAEnE,eAAe,SAAS,CAAC;AAEzB,MAAM,MAAM,OAAO,GAAG,eAAe,CAAC;AAEtC,MAAM,MAAM,MAAM,GAAG,IAAI,GAAG,KAAK,CAAC;AAElC,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,QAAQ,CAAC;AAE5C,MAAM,MAAM,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;AAEF,YAAY,EAAC,OAAO,EAAE,SAAS,EAAC,MAAM,UAAU,CAAC;AACjD,YAAY,EAAC,UAAU,EAAC,MAAM,aAAa,CAAC"}
|
|
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,oBAAoB,CAAC;AAEnE,OAAO,EAAC,mBAAmB,EAAE,mBAAmB,EAAC,MAAM,mBAAmB,CAAC;AAE3E,eAAe,SAAS,CAAC;AAEzB,MAAM,MAAM,OAAO,GAAG,eAAe,CAAC;AAEtC,MAAM,MAAM,MAAM,GAAG,IAAI,GAAG,KAAK,CAAC;AAElC,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,QAAQ,CAAC;AAE5C,MAAM,MAAM,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;AAEF,YAAY,EAAC,OAAO,EAAE,SAAS,EAAC,MAAM,UAAU,CAAC;AACjD,YAAY,EAAC,UAAU,EAAC,MAAM,aAAa,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -6,6 +6,10 @@
|
|
|
6
6
|
* LICENSE file in the root directory of this source tree.
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.createMDXLoaderItem = exports.createMDXLoaderRule = void 0;
|
|
9
10
|
const loader_1 = require("./loader");
|
|
11
|
+
var createMDXLoader_1 = require("./createMDXLoader");
|
|
12
|
+
Object.defineProperty(exports, "createMDXLoaderRule", { enumerable: true, get: function () { return createMDXLoader_1.createMDXLoaderRule; } });
|
|
13
|
+
Object.defineProperty(exports, "createMDXLoaderItem", { enumerable: true, get: function () { return createMDXLoader_1.createMDXLoaderItem; } });
|
|
10
14
|
exports.default = loader_1.mdxLoader;
|
|
11
15
|
//# 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;AAInC,qDAA2E;AAAnE,sHAAA,mBAAmB,OAAA;AAAE,sHAAA,mBAAmB,OAAA;AAEhD,kBAAe,kBAAS,CAAC"}
|
package/lib/loader.d.ts
CHANGED
|
@@ -4,8 +4,8 @@
|
|
|
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
|
+
import type { SimpleProcessors, MDXOptions } from './processor';
|
|
7
8
|
import type { ResolveMarkdownLink } from './remark/resolveMarkdownLinks';
|
|
8
|
-
import type { MDXOptions } from './processor';
|
|
9
9
|
import type { MarkdownConfig } from '@docusaurus/types';
|
|
10
10
|
import type { LoaderContext } from 'webpack';
|
|
11
11
|
type Pluggable = any;
|
|
@@ -29,6 +29,7 @@ export type Options = Partial<MDXOptions> & {
|
|
|
29
29
|
[key: string]: unknown;
|
|
30
30
|
};
|
|
31
31
|
resolveMarkdownLink?: ResolveMarkdownLink;
|
|
32
|
+
processors?: SimpleProcessors;
|
|
32
33
|
};
|
|
33
34
|
export declare function mdxLoader(this: LoaderContext<Options>, fileContent: string): Promise<void>;
|
|
34
35
|
export {};
|
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;AAeH,OAAO,KAAK,EACV,gBAAgB,EAChB,UAAU,EAEX,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,+BAA+B,CAAC;AAEvE,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,SAAS,CAAC;AAK3C,KAAK,SAAS,GAAG,GAAG,CAAC;AAErB,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;IAC/B,mBAAmB,CAAC,EAAE,mBAAmB,CAAC;IAG1C,UAAU,CAAC,EAAE,gBAAgB,CAAC;CAC/B,CAAC;AAEF,wBAAsB,SAAS,CAC7B,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,WAAW,EAAE,MAAM,GAClB,OAAO,CAAC,IAAI,CAAC,CAoGf"}
|
package/lib/loader.js
CHANGED
|
@@ -8,127 +8,35 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.mdxLoader = mdxLoader;
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
|
-
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
12
11
|
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
13
12
|
const utils_1 = require("@docusaurus/utils");
|
|
14
13
|
const stringify_object_1 = tslib_1.__importDefault(require("stringify-object"));
|
|
15
|
-
const
|
|
16
|
-
const frontMatter_1 = require("./frontMatter");
|
|
17
|
-
const processor_1 = require("./processor");
|
|
18
|
-
/**
|
|
19
|
-
* When this throws, it generally means that there's no metadata file associated
|
|
20
|
-
* with this MDX document. It can happen when using MDX partials (usually
|
|
21
|
-
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
22
|
-
* function in config
|
|
23
|
-
*/
|
|
24
|
-
async function readMetadataPath(metadataPath) {
|
|
25
|
-
try {
|
|
26
|
-
return await fs_extra_1.default.readFile(metadataPath, 'utf8');
|
|
27
|
-
}
|
|
28
|
-
catch (err) {
|
|
29
|
-
logger_1.default.error `MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
|
|
30
|
-
throw err;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Converts assets an object with Webpack require calls code.
|
|
35
|
-
* This is useful for mdx files to reference co-located assets using relative
|
|
36
|
-
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
37
|
-
* For now, we only handle that for images and paths starting with `./`:
|
|
38
|
-
*
|
|
39
|
-
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
40
|
-
*/
|
|
41
|
-
function createAssetsExportCode({ assets, inlineMarkdownAssetImageFileLoader, }) {
|
|
42
|
-
if (typeof assets !== 'object' ||
|
|
43
|
-
!assets ||
|
|
44
|
-
Object.keys(assets).length === 0) {
|
|
45
|
-
return 'undefined';
|
|
46
|
-
}
|
|
47
|
-
// TODO implementation can be completed/enhanced
|
|
48
|
-
function createAssetValueCode(assetValue) {
|
|
49
|
-
if (Array.isArray(assetValue)) {
|
|
50
|
-
const arrayItemCodes = assetValue.map((item) => createAssetValueCode(item) ?? 'undefined');
|
|
51
|
-
return `[${arrayItemCodes.join(', ')}]`;
|
|
52
|
-
}
|
|
53
|
-
// Only process string values starting with ./
|
|
54
|
-
// We could enhance this logic and check if file exists on disc?
|
|
55
|
-
if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
|
|
56
|
-
// TODO do we have other use-cases than image assets?
|
|
57
|
-
// Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
|
|
58
|
-
return `require("${inlineMarkdownAssetImageFileLoader}${(0, utils_1.escapePath)(assetValue)}").default`;
|
|
59
|
-
}
|
|
60
|
-
return undefined;
|
|
61
|
-
}
|
|
62
|
-
const assetEntries = Object.entries(assets);
|
|
63
|
-
const codeLines = assetEntries
|
|
64
|
-
.map(([key, value]) => {
|
|
65
|
-
const assetRequireCode = createAssetValueCode(value);
|
|
66
|
-
return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
|
|
67
|
-
})
|
|
68
|
-
.filter(Boolean);
|
|
69
|
-
return `{\n${codeLines.join('\n')}\n}`;
|
|
70
|
-
}
|
|
71
|
-
// TODO temporary, remove this after v3.1?
|
|
72
|
-
// Some plugin authors use our mdx-loader, despite it not being public API
|
|
73
|
-
// see https://github.com/facebook/docusaurus/issues/8298
|
|
74
|
-
function ensureMarkdownConfig(reqOptions) {
|
|
75
|
-
if (!reqOptions.markdownConfig) {
|
|
76
|
-
throw new Error('Docusaurus v3+ requires MDX loader options.markdownConfig - plugin authors using the MDX loader should make sure to provide that option');
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* data.contentTitle is set by the remark contentTitle plugin
|
|
81
|
-
*/
|
|
82
|
-
function extractContentTitleData(data) {
|
|
83
|
-
return data.contentTitle;
|
|
84
|
-
}
|
|
14
|
+
const utils_2 = require("./utils");
|
|
85
15
|
async function mdxLoader(fileContent) {
|
|
86
16
|
const compilerName = (0, utils_1.getWebpackLoaderCompilerName)(this);
|
|
87
17
|
const callback = this.async();
|
|
88
18
|
const filePath = this.resourcePath;
|
|
89
19
|
const options = this.getOptions();
|
|
90
|
-
ensureMarkdownConfig(options);
|
|
91
20
|
const { frontMatter } = await options.markdownConfig.parseFrontMatter({
|
|
92
21
|
filePath,
|
|
93
22
|
fileContent,
|
|
94
23
|
defaultParseFrontMatter: utils_1.DEFAULT_PARSE_FRONT_MATTER,
|
|
95
24
|
});
|
|
96
|
-
const mdxFrontMatter = (0, frontMatter_1.validateMDXFrontMatter)(frontMatter.mdx);
|
|
97
|
-
const preprocessedContent = (0, preprocessor_1.default)({
|
|
98
|
-
fileContent,
|
|
99
|
-
filePath,
|
|
100
|
-
admonitions: options.admonitions,
|
|
101
|
-
markdownConfig: options.markdownConfig,
|
|
102
|
-
});
|
|
103
25
|
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
104
|
-
const processor = await (0, processor_1.createProcessorCached)({
|
|
105
|
-
filePath,
|
|
106
|
-
options,
|
|
107
|
-
mdxFrontMatter,
|
|
108
|
-
});
|
|
109
26
|
let result;
|
|
110
27
|
try {
|
|
111
|
-
result = await
|
|
112
|
-
|
|
28
|
+
result = await (0, utils_2.compileToJSX)({
|
|
29
|
+
fileContent,
|
|
113
30
|
filePath,
|
|
114
31
|
frontMatter,
|
|
32
|
+
options,
|
|
115
33
|
compilerName,
|
|
116
34
|
});
|
|
117
35
|
}
|
|
118
|
-
catch (
|
|
119
|
-
|
|
120
|
-
// MDX can emit errors that have useful extra attributes
|
|
121
|
-
const errorJSON = JSON.stringify(error, null, 2);
|
|
122
|
-
const errorDetails = errorJSON === '{}'
|
|
123
|
-
? // regular JS error case: print stacktrace
|
|
124
|
-
error.stack ?? 'N/A'
|
|
125
|
-
: // MDX error: print extra attributes + stacktrace
|
|
126
|
-
`${errorJSON}\n${error.stack}`;
|
|
127
|
-
return callback(new Error(`MDX compilation failed for file ${logger_1.default.path(filePath)}\nCause: ${error.message}\nDetails:\n${errorDetails}`,
|
|
128
|
-
// TODO error cause doesn't seem to be used by Webpack stats.errors :s
|
|
129
|
-
{ cause: error }));
|
|
36
|
+
catch (error) {
|
|
37
|
+
return callback(error);
|
|
130
38
|
}
|
|
131
|
-
const contentTitle = extractContentTitleData(result.data);
|
|
39
|
+
const contentTitle = (0, utils_2.extractContentTitleData)(result.data);
|
|
132
40
|
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
133
41
|
// Partial are not expected to have associated metadata files or front matter
|
|
134
42
|
const isMDXPartial = options.isMDXPartial?.(filePath);
|
|
@@ -159,7 +67,7 @@ ${JSON.stringify(frontMatter, null, 2)}`;
|
|
|
159
67
|
this.addDependency(metadataPath);
|
|
160
68
|
}
|
|
161
69
|
const metadataJsonString = metadataPath
|
|
162
|
-
? await readMetadataPath(metadataPath)
|
|
70
|
+
? await (0, utils_2.readMetadataPath)(metadataPath)
|
|
163
71
|
: undefined;
|
|
164
72
|
const metadata = metadataJsonString
|
|
165
73
|
? JSON.parse(metadataJsonString)
|
|
@@ -175,7 +83,7 @@ export const frontMatter = ${(0, stringify_object_1.default)(frontMatter)};
|
|
|
175
83
|
export const contentTitle = ${(0, stringify_object_1.default)(contentTitle)};
|
|
176
84
|
${metadataJsonString ? `export const metadata = ${metadataJsonString};` : ''}
|
|
177
85
|
${assets
|
|
178
|
-
? `export const assets = ${createAssetsExportCode({
|
|
86
|
+
? `export const assets = ${(0, utils_2.createAssetsExportCode)({
|
|
179
87
|
assets,
|
|
180
88
|
inlineMarkdownAssetImageFileLoader: fileLoaderUtils.loaders.inlineMarkdownAssetImageFileLoader,
|
|
181
89
|
})};`
|
package/lib/loader.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;
|
|
1
|
+
{"version":3,"file":"loader.js","sourceRoot":"","sources":["../src/loader.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAkDH,8BAuGC;;AAvJD,wEAAwC;AACxC,6CAI2B;AAC3B,gFAA+C;AAC/C,mCAKiB;AAoCV,KAAK,UAAU,SAAS,CAE7B,WAAmB;IAEnB,MAAM,YAAY,GAAG,IAAA,oCAA4B,EAAC,IAAI,CAAC,CAAC;IACxD,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;IAC9B,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC;IACnC,MAAM,OAAO,GAAY,IAAI,CAAC,UAAU,EAAE,CAAC;IAE3C,MAAM,EAAC,WAAW,EAAC,GAAG,MAAM,OAAO,CAAC,cAAc,CAAC,gBAAgB,CAAC;QAClE,QAAQ;QACR,WAAW;QACX,uBAAuB,EAAE,kCAA0B;KACpD,CAAC,CAAC;IAEH,MAAM,cAAc,GAAG,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IAE3D,IAAI,MAA6B,CAAC;IAClC,IAAI,CAAC;QACH,MAAM,GAAG,MAAM,IAAA,oBAAY,EAAC;YAC1B,WAAW;YACX,QAAQ;YACR,WAAW;YACX,OAAO;YACP,YAAY;SACb,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,KAA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AAC;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,OAAO,CAAC,YAAY,IAAI,QAAQ;QAC9B,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,EAAC,WAAW,EAAE,QAAQ,EAAC,CAAC;QAC/C,CAAC,CAAC,SAAS,CAAC;IAEhB,MAAM,eAAe,GAAG,IAAA,0BAAkB,EAAC,YAAY,KAAK,QAAQ,CAAC,CAAC;IAEtE,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;EAE1E,MAAM;QACJ,CAAC,CAAC,yBAAyB,IAAA,8BAAsB,EAAC;YAC9C,MAAM;YACN,kCAAkC,EAChC,eAAe,CAAC,OAAO,CAAC,kCAAkC;SAC7D,CAAC,GAAG;QACP,CAAC,CAAC,EACN;CACC,CAAC;IAEA,MAAM,IAAI,GAAG;EACb,WAAW;EACX,MAAM,CAAC,OAAO;CACf,CAAC;IAEA,OAAO,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAC9B,CAAC"}
|
package/lib/processor.d.ts
CHANGED
|
@@ -9,13 +9,13 @@ import type { MDXFrontMatter } from './frontMatter';
|
|
|
9
9
|
import type { Options } from './loader';
|
|
10
10
|
import type { AdmonitionOptions } from './remark/admonitions';
|
|
11
11
|
type Pluggable = any;
|
|
12
|
-
type SimpleProcessorResult = {
|
|
12
|
+
export type SimpleProcessorResult = {
|
|
13
13
|
content: string;
|
|
14
14
|
data: {
|
|
15
15
|
[key: string]: unknown;
|
|
16
16
|
};
|
|
17
17
|
};
|
|
18
|
-
type SimpleProcessor = {
|
|
18
|
+
export type SimpleProcessor = {
|
|
19
19
|
process: ({ content, filePath, frontMatter, compilerName, }: {
|
|
20
20
|
content: string;
|
|
21
21
|
filePath: string;
|
|
@@ -38,7 +38,14 @@ export declare function createProcessorUncached(parameters: {
|
|
|
38
38
|
options: Options;
|
|
39
39
|
format: 'md' | 'mdx';
|
|
40
40
|
}): Promise<SimpleProcessor>;
|
|
41
|
-
export
|
|
41
|
+
export type SimpleProcessors = {
|
|
42
|
+
mdProcessor: SimpleProcessor;
|
|
43
|
+
mdxProcessor: SimpleProcessor;
|
|
44
|
+
};
|
|
45
|
+
export declare function createProcessors({ options, }: {
|
|
46
|
+
options: Options;
|
|
47
|
+
}): Promise<SimpleProcessors>;
|
|
48
|
+
export declare function getProcessor({ filePath, mdxFrontMatter, options, }: {
|
|
42
49
|
filePath: string;
|
|
43
50
|
mdxFrontMatter: MDXFrontMatter;
|
|
44
51
|
options: Options;
|
package/lib/processor.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../src/processor.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,mBAAmB,CAAC;AAC3D,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;AAErB,
|
|
1
|
+
{"version":3,"file":"processor.d.ts","sourceRoot":"","sources":["../src/processor.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,mBAAmB,CAAC;AAC3D,OAAO,KAAK,EAAC,cAAc,EAAC,MAAM,eAAe,CAAC;AA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/B,CAAC;AAMF,wBAAsB,gBAAgB,CAAC,EACrC,OAAO,GACR,EAAE;IACD,OAAO,EAAE,OAAO,CAAC;CAClB,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAY5B;AAgBD,wBAAsB,YAAY,CAAC,EACjC,QAAQ,EACR,cAAc,EACd,OAAO,GACR,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,cAAc,EAAE,cAAc,CAAC;IAC/B,OAAO,EAAE,OAAO,CAAC;CAClB,GAAG,OAAO,CAAC,eAAe,CAAC,CAW3B"}
|
package/lib/processor.js
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.createProcessorUncached = createProcessorUncached;
|
|
10
|
-
exports.
|
|
10
|
+
exports.createProcessors = createProcessors;
|
|
11
|
+
exports.getProcessor = getProcessor;
|
|
11
12
|
const tslib_1 = require("tslib");
|
|
12
13
|
const headings_1 = tslib_1.__importDefault(require("./remark/headings"));
|
|
13
14
|
const contentTitle_1 = tslib_1.__importDefault(require("./remark/contentTitle"));
|
|
@@ -158,13 +159,9 @@ async function createProcessorUncached(parameters) {
|
|
|
158
159
|
// Compilers are cached so that Remark/Rehype plugins can run
|
|
159
160
|
// expensive code during initialization
|
|
160
161
|
const ProcessorsCache = new Map();
|
|
161
|
-
async function
|
|
162
|
+
async function createProcessors({ options, }) {
|
|
162
163
|
const { createProcessorSync } = await createProcessorFactory();
|
|
163
|
-
|
|
164
|
-
if (compilers) {
|
|
165
|
-
return compilers;
|
|
166
|
-
}
|
|
167
|
-
const compilerCacheEntry = {
|
|
164
|
+
return {
|
|
168
165
|
mdProcessor: createProcessorSync({
|
|
169
166
|
options,
|
|
170
167
|
format: 'md',
|
|
@@ -174,16 +171,23 @@ async function createProcessorsCacheEntry({ options, }) {
|
|
|
174
171
|
format: 'mdx',
|
|
175
172
|
}),
|
|
176
173
|
};
|
|
177
|
-
ProcessorsCache.set(options, compilerCacheEntry);
|
|
178
|
-
return compilerCacheEntry;
|
|
179
174
|
}
|
|
180
|
-
async function
|
|
181
|
-
const compilers =
|
|
175
|
+
async function createProcessorsCacheEntry({ options, }) {
|
|
176
|
+
const compilers = ProcessorsCache.get(options);
|
|
177
|
+
if (compilers) {
|
|
178
|
+
return compilers;
|
|
179
|
+
}
|
|
180
|
+
const processors = await createProcessors({ options });
|
|
181
|
+
ProcessorsCache.set(options, processors);
|
|
182
|
+
return processors;
|
|
183
|
+
}
|
|
184
|
+
async function getProcessor({ filePath, mdxFrontMatter, options, }) {
|
|
185
|
+
const processors = options.processors ?? (await createProcessorsCacheEntry({ options }));
|
|
182
186
|
const format = (0, format_1.getFormat)({
|
|
183
187
|
filePath,
|
|
184
188
|
frontMatterFormat: mdxFrontMatter.format,
|
|
185
189
|
markdownConfigFormat: options.markdownConfig.format,
|
|
186
190
|
});
|
|
187
|
-
return format === 'md' ?
|
|
191
|
+
return format === 'md' ? processors.mdProcessor : processors.mdxProcessor;
|
|
188
192
|
}
|
|
189
193
|
//# sourceMappingURL=processor.js.map
|
package/lib/processor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processor.js","sourceRoot":"","sources":["../src/processor.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;
|
|
1
|
+
{"version":3,"file":"processor.js","sourceRoot":"","sources":["../src/processor.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;AAkNH,0DAMC;AAYD,4CAgBC;AAgBD,oCAmBC;;AArRD,yEAAyC;AACzC,iFAAiD;AACjD,+DAA+B;AAC/B,qFAAqD;AACrD,qFAAqD;AACrD,iGAAiE;AACjE,uEAAuC;AACvC,iEAAiC;AACjC,uEAAuC;AACvC,+EAAwD;AACxD,yFAAgE;AAChE,oGAAoE;AACpE,qCAAmC;AA6CnC,SAAS,qBAAqB,CAC5B,iBAA4C;IAE5C,IAAI,iBAAiB,EAAE,CAAC;QACtB,MAAM,MAAM,GACV,i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B,EAAE,cAAc,CAAC,MAAM;QACxC,oBAAoB,EAAE,OAAO,CAAC,cAAc,CAAC,MAAM;KACpD,CAAC,CAAC;IAEH,OAAO,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,UAAU,CAAC,WAAW,CAAC,CAAC,CAAC,UAAU,CAAC,YAAY,CAAC;AAC5E,CAAC"}
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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 WebpackCompilerName } from '@docusaurus/utils';
|
|
8
|
+
import { type SimpleProcessorResult } from './processor';
|
|
9
|
+
import type { Options } from './loader';
|
|
10
|
+
/**
|
|
11
|
+
* When this throws, it generally means that there's no metadata file associated
|
|
12
|
+
* with this MDX document. It can happen when using MDX partials (usually
|
|
13
|
+
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
14
|
+
* function in config
|
|
15
|
+
*/
|
|
16
|
+
export declare function readMetadataPath(metadataPath: string): Promise<string>;
|
|
17
|
+
/**
|
|
18
|
+
* Converts assets an object with Webpack require calls code.
|
|
19
|
+
* This is useful for mdx files to reference co-located assets using relative
|
|
20
|
+
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
21
|
+
* For now, we only handle that for images and paths starting with `./`:
|
|
22
|
+
*
|
|
23
|
+
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
24
|
+
*/
|
|
25
|
+
export declare function createAssetsExportCode({ assets, inlineMarkdownAssetImageFileLoader, }: {
|
|
26
|
+
assets: unknown;
|
|
27
|
+
inlineMarkdownAssetImageFileLoader: string;
|
|
28
|
+
}): string;
|
|
29
|
+
/**
|
|
30
|
+
* data.contentTitle is set by the remark contentTitle plugin
|
|
31
|
+
*/
|
|
32
|
+
export declare function extractContentTitleData(data: {
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
}): string | undefined;
|
|
35
|
+
export declare function compileToJSX({ filePath, fileContent, frontMatter, options, compilerName, }: {
|
|
36
|
+
filePath: string;
|
|
37
|
+
fileContent: string;
|
|
38
|
+
frontMatter: Record<string, unknown>;
|
|
39
|
+
options: Options;
|
|
40
|
+
compilerName: WebpackCompilerName;
|
|
41
|
+
}): Promise<SimpleProcessorResult>;
|
|
42
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,EAAa,KAAK,mBAAmB,EAAC,MAAM,mBAAmB,CAAC;AACvE,OAAO,EAAe,KAAK,qBAAqB,EAAC,MAAM,aAAa,CAAC;AAGrE,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,UAAU,CAAC;AAEtC;;;;;GAKG;AACH,wBAAsB,gBAAgB,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAS5E;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,EACrC,MAAM,EACN,kCAAkC,GACnC,EAAE;IACD,MAAM,EAAE,OAAO,CAAC;IAChB,kCAAkC,EAAE,MAAM,CAAC;CAC5C,GAAG,MAAM,CAuCT;AAED;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE;IAC5C,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,GAAG,MAAM,GAAG,SAAS,CAErB;AAED,wBAAsB,YAAY,CAAC,EACjC,QAAQ,EACR,WAAW,EACX,WAAW,EACX,OAAO,EACP,YAAY,GACb,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACrC,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,mBAAmB,CAAC;CACnC,GAAG,OAAO,CAAC,qBAAqB,CAAC,CA2CjC"}
|
package/lib/utils.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
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.readMetadataPath = readMetadataPath;
|
|
10
|
+
exports.createAssetsExportCode = createAssetsExportCode;
|
|
11
|
+
exports.extractContentTitleData = extractContentTitleData;
|
|
12
|
+
exports.compileToJSX = compileToJSX;
|
|
13
|
+
const tslib_1 = require("tslib");
|
|
14
|
+
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
|
|
15
|
+
const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
|
|
16
|
+
const utils_1 = require("@docusaurus/utils");
|
|
17
|
+
const processor_1 = require("./processor");
|
|
18
|
+
const frontMatter_1 = require("./frontMatter");
|
|
19
|
+
const preprocessor_1 = tslib_1.__importDefault(require("./preprocessor"));
|
|
20
|
+
/**
|
|
21
|
+
* When this throws, it generally means that there's no metadata file associated
|
|
22
|
+
* with this MDX document. It can happen when using MDX partials (usually
|
|
23
|
+
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
24
|
+
* function in config
|
|
25
|
+
*/
|
|
26
|
+
async function readMetadataPath(metadataPath) {
|
|
27
|
+
try {
|
|
28
|
+
return await fs_extra_1.default.readFile(metadataPath, 'utf8');
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
throw new Error(logger_1.default.interpolate `MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`, { cause: error });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Converts assets an object with Webpack require calls code.
|
|
36
|
+
* This is useful for mdx files to reference co-located assets using relative
|
|
37
|
+
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
38
|
+
* For now, we only handle that for images and paths starting with `./`:
|
|
39
|
+
*
|
|
40
|
+
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
41
|
+
*/
|
|
42
|
+
function createAssetsExportCode({ assets, inlineMarkdownAssetImageFileLoader, }) {
|
|
43
|
+
if (typeof assets !== 'object' ||
|
|
44
|
+
!assets ||
|
|
45
|
+
Object.keys(assets).length === 0) {
|
|
46
|
+
return 'undefined';
|
|
47
|
+
}
|
|
48
|
+
// TODO implementation can be completed/enhanced
|
|
49
|
+
function createAssetValueCode(assetValue) {
|
|
50
|
+
if (Array.isArray(assetValue)) {
|
|
51
|
+
const arrayItemCodes = assetValue.map((item) => createAssetValueCode(item) ?? 'undefined');
|
|
52
|
+
return `[${arrayItemCodes.join(', ')}]`;
|
|
53
|
+
}
|
|
54
|
+
// Only process string values starting with ./
|
|
55
|
+
// We could enhance this logic and check if file exists on disc?
|
|
56
|
+
if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
|
|
57
|
+
// TODO do we have other use-cases than image assets?
|
|
58
|
+
// Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
|
|
59
|
+
return `require("${inlineMarkdownAssetImageFileLoader}${(0, utils_1.escapePath)(assetValue)}").default`;
|
|
60
|
+
}
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const assetEntries = Object.entries(assets);
|
|
64
|
+
const codeLines = assetEntries
|
|
65
|
+
.map(([key, value]) => {
|
|
66
|
+
const assetRequireCode = createAssetValueCode(value);
|
|
67
|
+
return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
|
|
68
|
+
})
|
|
69
|
+
.filter(Boolean);
|
|
70
|
+
return `{\n${codeLines.join('\n')}\n}`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* data.contentTitle is set by the remark contentTitle plugin
|
|
74
|
+
*/
|
|
75
|
+
function extractContentTitleData(data) {
|
|
76
|
+
return data.contentTitle;
|
|
77
|
+
}
|
|
78
|
+
async function compileToJSX({ filePath, fileContent, frontMatter, options, compilerName, }) {
|
|
79
|
+
const preprocessedFileContent = (0, preprocessor_1.default)({
|
|
80
|
+
fileContent,
|
|
81
|
+
filePath,
|
|
82
|
+
admonitions: options.admonitions,
|
|
83
|
+
markdownConfig: options.markdownConfig,
|
|
84
|
+
});
|
|
85
|
+
const mdxFrontMatter = (0, frontMatter_1.validateMDXFrontMatter)(frontMatter.mdx);
|
|
86
|
+
const processor = await (0, processor_1.getProcessor)({
|
|
87
|
+
filePath,
|
|
88
|
+
options,
|
|
89
|
+
mdxFrontMatter,
|
|
90
|
+
});
|
|
91
|
+
try {
|
|
92
|
+
return await processor.process({
|
|
93
|
+
content: preprocessedFileContent,
|
|
94
|
+
filePath,
|
|
95
|
+
frontMatter,
|
|
96
|
+
compilerName,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
catch (errorUnknown) {
|
|
100
|
+
const error = errorUnknown;
|
|
101
|
+
// MDX can emit errors that have useful extra attributes
|
|
102
|
+
const errorJSON = JSON.stringify(error, null, 2);
|
|
103
|
+
const errorDetails = errorJSON === '{}'
|
|
104
|
+
? // regular JS error case: print stacktrace
|
|
105
|
+
error.stack ?? 'N/A'
|
|
106
|
+
: // MDX error: print extra attributes + stacktrace
|
|
107
|
+
`${errorJSON}\n${error.stack}`;
|
|
108
|
+
throw new Error(`MDX compilation failed for file ${logger_1.default.path(filePath)}\nCause: ${error.message}\nDetails:\n${errorDetails}`,
|
|
109
|
+
// TODO error cause doesn't seem to be used by Webpack stats.errors :s
|
|
110
|
+
{ cause: error });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=utils.js.map
|
package/lib/utils.js.map
ADDED
|
@@ -0,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O,EAAE,uBAAuB;YAChC,QAAQ;YACR,WAAW;YACX,YAAY;SACb,CAAC,CAAC;IACL,CAAC;IAAC,OAAO,YAAY,EAAE,CAAC;QACtB,MAAM,KAAK,GAAG,YAAqB,CAAC;QAEpC,wDAAwD;QACxD,MAAM,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC;QACjD,MAAM,YAAY,GAChB,SAAS,KAAK,IAAI;YAChB,CAAC,CAAC,0CAA0C;gBAC1C,KAAK,CAAC,KAAK,IAAI,KAAK;YACtB,CAAC,CAAC,iDAAiD;gBACjD,GAAG,SAAS,KAAK,KAAK,CAAC,KAAK,EAAE,CAAC;QAErC,MAAM,IAAI,KAAK,CACb,mCAAmC,gBAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,YACtD,KAAK,CAAC,OACR,eAAe,YAAY,EAAE;QAC7B,sEAAsE;QACtE,EAAC,KAAK,EAAE,KAAK,EAAC,CACf,CAAC;IACJ,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/mdx-loader",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-6060",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/logger": "0.0.0-
|
|
22
|
-
"@docusaurus/utils": "0.0.0-
|
|
23
|
-
"@docusaurus/utils-validation": "0.0.0-
|
|
21
|
+
"@docusaurus/logger": "0.0.0-6060",
|
|
22
|
+
"@docusaurus/utils": "0.0.0-6060",
|
|
23
|
+
"@docusaurus/utils-validation": "0.0.0-6060",
|
|
24
24
|
"@mdx-js/mdx": "^3.0.0",
|
|
25
25
|
"@slorber/remark-comment": "^1.0.0",
|
|
26
26
|
"escape-html": "^1.0.3",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"webpack": "^5.88.1"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
|
-
"@docusaurus/types": "0.0.0-
|
|
47
|
+
"@docusaurus/types": "0.0.0-6060",
|
|
48
48
|
"@types/escape-html": "^1.0.2",
|
|
49
49
|
"@types/mdast": "^4.0.2",
|
|
50
50
|
"@types/stringify-object": "^3.3.1",
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"engines": {
|
|
65
65
|
"node": ">=18.0"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "75cf549134cde8901d2598e6c6d2439a334512ff"
|
|
68
68
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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 {createProcessors} from './processor';
|
|
9
|
+
import type {Options} from './loader';
|
|
10
|
+
import type {RuleSetRule, RuleSetUseItem} from 'webpack';
|
|
11
|
+
|
|
12
|
+
async function enhancedOptions(options: Options): Promise<Options> {
|
|
13
|
+
// Because Jest doesn't like ESM / createProcessors()
|
|
14
|
+
if (process.env.N0DE_ENV === 'test' || process.env.JEST_WORKER_ID) {
|
|
15
|
+
return options;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// We create the processor earlier here, to avoid the lazy processor creating
|
|
19
|
+
// Lazy creation messes-up with Rsdoctor ability to measure mdx-loader perf
|
|
20
|
+
const newOptions: Options = options.processors
|
|
21
|
+
? options
|
|
22
|
+
: {...options, processors: await createProcessors({options})};
|
|
23
|
+
|
|
24
|
+
return newOptions;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function createMDXLoaderItem(
|
|
28
|
+
options: Options,
|
|
29
|
+
): Promise<RuleSetUseItem> {
|
|
30
|
+
return {
|
|
31
|
+
loader: require.resolve('@docusaurus/mdx-loader'),
|
|
32
|
+
options: await enhancedOptions(options),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function createMDXLoaderRule({
|
|
37
|
+
include,
|
|
38
|
+
options,
|
|
39
|
+
}: {
|
|
40
|
+
include: RuleSetRule['include'];
|
|
41
|
+
options: Options;
|
|
42
|
+
}): Promise<RuleSetRule> {
|
|
43
|
+
return {
|
|
44
|
+
test: /\.mdx?$/i,
|
|
45
|
+
include,
|
|
46
|
+
use: [await createMDXLoaderItem(options)],
|
|
47
|
+
};
|
|
48
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,8 @@ import {mdxLoader} from './loader';
|
|
|
9
9
|
|
|
10
10
|
import type {TOCItem as TOCItemImported} from './remark/toc/types';
|
|
11
11
|
|
|
12
|
+
export {createMDXLoaderRule, createMDXLoaderItem} from './createMDXLoader';
|
|
13
|
+
|
|
12
14
|
export default mdxLoader;
|
|
13
15
|
|
|
14
16
|
export type TOCItem = TOCItemImported;
|
package/src/loader.ts
CHANGED
|
@@ -5,20 +5,25 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import fs from 'fs-extra';
|
|
9
8
|
import logger from '@docusaurus/logger';
|
|
10
9
|
import {
|
|
11
10
|
DEFAULT_PARSE_FRONT_MATTER,
|
|
12
|
-
escapePath,
|
|
13
11
|
getFileLoaderUtils,
|
|
14
12
|
getWebpackLoaderCompilerName,
|
|
15
13
|
} from '@docusaurus/utils';
|
|
16
14
|
import stringifyObject from 'stringify-object';
|
|
17
|
-
import
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
import {
|
|
16
|
+
compileToJSX,
|
|
17
|
+
createAssetsExportCode,
|
|
18
|
+
extractContentTitleData,
|
|
19
|
+
readMetadataPath,
|
|
20
|
+
} from './utils';
|
|
21
|
+
import type {
|
|
22
|
+
SimpleProcessors,
|
|
23
|
+
MDXOptions,
|
|
24
|
+
SimpleProcessorResult,
|
|
25
|
+
} from './processor';
|
|
20
26
|
import type {ResolveMarkdownLink} from './remark/resolveMarkdownLinks';
|
|
21
|
-
import type {MDXOptions} from './processor';
|
|
22
27
|
|
|
23
28
|
import type {MarkdownConfig} from '@docusaurus/types';
|
|
24
29
|
import type {LoaderContext} from 'webpack';
|
|
@@ -43,97 +48,10 @@ export type Options = Partial<MDXOptions> & {
|
|
|
43
48
|
metadata: {[key: string]: unknown};
|
|
44
49
|
}) => {[key: string]: unknown};
|
|
45
50
|
resolveMarkdownLink?: ResolveMarkdownLink;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* When this throws, it generally means that there's no metadata file associated
|
|
50
|
-
* with this MDX document. It can happen when using MDX partials (usually
|
|
51
|
-
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
52
|
-
* function in config
|
|
53
|
-
*/
|
|
54
|
-
async function readMetadataPath(metadataPath: string) {
|
|
55
|
-
try {
|
|
56
|
-
return await fs.readFile(metadataPath, 'utf8');
|
|
57
|
-
} catch (err) {
|
|
58
|
-
logger.error`MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
|
|
59
|
-
throw err;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Converts assets an object with Webpack require calls code.
|
|
65
|
-
* This is useful for mdx files to reference co-located assets using relative
|
|
66
|
-
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
67
|
-
* For now, we only handle that for images and paths starting with `./`:
|
|
68
|
-
*
|
|
69
|
-
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
70
|
-
*/
|
|
71
|
-
function createAssetsExportCode({
|
|
72
|
-
assets,
|
|
73
|
-
inlineMarkdownAssetImageFileLoader,
|
|
74
|
-
}: {
|
|
75
|
-
assets: unknown;
|
|
76
|
-
inlineMarkdownAssetImageFileLoader: string;
|
|
77
|
-
}) {
|
|
78
|
-
if (
|
|
79
|
-
typeof assets !== 'object' ||
|
|
80
|
-
!assets ||
|
|
81
|
-
Object.keys(assets).length === 0
|
|
82
|
-
) {
|
|
83
|
-
return 'undefined';
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// TODO implementation can be completed/enhanced
|
|
87
|
-
function createAssetValueCode(assetValue: unknown): string | undefined {
|
|
88
|
-
if (Array.isArray(assetValue)) {
|
|
89
|
-
const arrayItemCodes = assetValue.map(
|
|
90
|
-
(item: unknown) => createAssetValueCode(item) ?? 'undefined',
|
|
91
|
-
);
|
|
92
|
-
return `[${arrayItemCodes.join(', ')}]`;
|
|
93
|
-
}
|
|
94
|
-
// Only process string values starting with ./
|
|
95
|
-
// We could enhance this logic and check if file exists on disc?
|
|
96
|
-
if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
|
|
97
|
-
// TODO do we have other use-cases than image assets?
|
|
98
|
-
// Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
|
|
99
|
-
return `require("${inlineMarkdownAssetImageFileLoader}${escapePath(
|
|
100
|
-
assetValue,
|
|
101
|
-
)}").default`;
|
|
102
|
-
}
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
const assetEntries = Object.entries(assets);
|
|
107
|
-
|
|
108
|
-
const codeLines = assetEntries
|
|
109
|
-
.map(([key, value]: [string, unknown]) => {
|
|
110
|
-
const assetRequireCode = createAssetValueCode(value);
|
|
111
|
-
return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
|
|
112
|
-
})
|
|
113
|
-
.filter(Boolean);
|
|
114
51
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
// TODO temporary, remove this after v3.1?
|
|
119
|
-
// Some plugin authors use our mdx-loader, despite it not being public API
|
|
120
|
-
// see https://github.com/facebook/docusaurus/issues/8298
|
|
121
|
-
function ensureMarkdownConfig(reqOptions: Options) {
|
|
122
|
-
if (!reqOptions.markdownConfig) {
|
|
123
|
-
throw new Error(
|
|
124
|
-
'Docusaurus v3+ requires MDX loader options.markdownConfig - plugin authors using the MDX loader should make sure to provide that option',
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* data.contentTitle is set by the remark contentTitle plugin
|
|
131
|
-
*/
|
|
132
|
-
function extractContentTitleData(data: {
|
|
133
|
-
[key: string]: unknown;
|
|
134
|
-
}): string | undefined {
|
|
135
|
-
return data.contentTitle as string | undefined;
|
|
136
|
-
}
|
|
52
|
+
// Will usually be created by "createMDXLoaderItem"
|
|
53
|
+
processors?: SimpleProcessors;
|
|
54
|
+
};
|
|
137
55
|
|
|
138
56
|
export async function mdxLoader(
|
|
139
57
|
this: LoaderContext<Options>,
|
|
@@ -144,59 +62,25 @@ export async function mdxLoader(
|
|
|
144
62
|
const filePath = this.resourcePath;
|
|
145
63
|
const options: Options = this.getOptions();
|
|
146
64
|
|
|
147
|
-
ensureMarkdownConfig(options);
|
|
148
|
-
|
|
149
65
|
const {frontMatter} = await options.markdownConfig.parseFrontMatter({
|
|
150
66
|
filePath,
|
|
151
67
|
fileContent,
|
|
152
68
|
defaultParseFrontMatter: DEFAULT_PARSE_FRONT_MATTER,
|
|
153
69
|
});
|
|
154
|
-
const mdxFrontMatter = validateMDXFrontMatter(frontMatter.mdx);
|
|
155
|
-
|
|
156
|
-
const preprocessedContent = preprocessor({
|
|
157
|
-
fileContent,
|
|
158
|
-
filePath,
|
|
159
|
-
admonitions: options.admonitions,
|
|
160
|
-
markdownConfig: options.markdownConfig,
|
|
161
|
-
});
|
|
162
70
|
|
|
163
71
|
const hasFrontMatter = Object.keys(frontMatter).length > 0;
|
|
164
72
|
|
|
165
|
-
|
|
166
|
-
filePath,
|
|
167
|
-
options,
|
|
168
|
-
mdxFrontMatter,
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
let result: {content: string; data: {[key: string]: unknown}};
|
|
73
|
+
let result: SimpleProcessorResult;
|
|
172
74
|
try {
|
|
173
|
-
result = await
|
|
174
|
-
|
|
75
|
+
result = await compileToJSX({
|
|
76
|
+
fileContent,
|
|
175
77
|
filePath,
|
|
176
78
|
frontMatter,
|
|
79
|
+
options,
|
|
177
80
|
compilerName,
|
|
178
81
|
});
|
|
179
|
-
} catch (
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
// MDX can emit errors that have useful extra attributes
|
|
183
|
-
const errorJSON = JSON.stringify(error, null, 2);
|
|
184
|
-
const errorDetails =
|
|
185
|
-
errorJSON === '{}'
|
|
186
|
-
? // regular JS error case: print stacktrace
|
|
187
|
-
error.stack ?? 'N/A'
|
|
188
|
-
: // MDX error: print extra attributes + stacktrace
|
|
189
|
-
`${errorJSON}\n${error.stack}`;
|
|
190
|
-
|
|
191
|
-
return callback(
|
|
192
|
-
new Error(
|
|
193
|
-
`MDX compilation failed for file ${logger.path(filePath)}\nCause: ${
|
|
194
|
-
error.message
|
|
195
|
-
}\nDetails:\n${errorDetails}`,
|
|
196
|
-
// TODO error cause doesn't seem to be used by Webpack stats.errors :s
|
|
197
|
-
{cause: error},
|
|
198
|
-
),
|
|
199
|
-
);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
return callback(error as Error);
|
|
200
84
|
}
|
|
201
85
|
|
|
202
86
|
const contentTitle = extractContentTitleData(result.data);
|
package/src/processor.ts
CHANGED
|
@@ -31,10 +31,13 @@ import type {ProcessorOptions} from '@mdx-js/mdx';
|
|
|
31
31
|
// See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
|
|
32
32
|
type Pluggable = any; // TODO fix this asap
|
|
33
33
|
|
|
34
|
-
type SimpleProcessorResult = {
|
|
34
|
+
export type SimpleProcessorResult = {
|
|
35
|
+
content: string;
|
|
36
|
+
data: {[key: string]: unknown};
|
|
37
|
+
};
|
|
35
38
|
|
|
36
39
|
// TODO alt interface because impossible to import type Processor (ESM + TS :/)
|
|
37
|
-
type SimpleProcessor = {
|
|
40
|
+
export type SimpleProcessor = {
|
|
38
41
|
process: ({
|
|
39
42
|
content,
|
|
40
43
|
filePath,
|
|
@@ -219,28 +222,22 @@ export async function createProcessorUncached(parameters: {
|
|
|
219
222
|
}
|
|
220
223
|
|
|
221
224
|
// We use different compilers depending on the file type (md vs mdx)
|
|
222
|
-
type
|
|
225
|
+
export type SimpleProcessors = {
|
|
223
226
|
mdProcessor: SimpleProcessor;
|
|
224
227
|
mdxProcessor: SimpleProcessor;
|
|
225
228
|
};
|
|
226
229
|
|
|
227
230
|
// Compilers are cached so that Remark/Rehype plugins can run
|
|
228
231
|
// expensive code during initialization
|
|
229
|
-
const ProcessorsCache = new Map<string | Options,
|
|
232
|
+
const ProcessorsCache = new Map<string | Options, SimpleProcessors>();
|
|
230
233
|
|
|
231
|
-
async function
|
|
234
|
+
export async function createProcessors({
|
|
232
235
|
options,
|
|
233
236
|
}: {
|
|
234
237
|
options: Options;
|
|
235
|
-
}): Promise<
|
|
238
|
+
}): Promise<SimpleProcessors> {
|
|
236
239
|
const {createProcessorSync} = await createProcessorFactory();
|
|
237
|
-
|
|
238
|
-
const compilers = ProcessorsCache.get(options);
|
|
239
|
-
if (compilers) {
|
|
240
|
-
return compilers;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
const compilerCacheEntry: ProcessorsCacheEntry = {
|
|
240
|
+
return {
|
|
244
241
|
mdProcessor: createProcessorSync({
|
|
245
242
|
options,
|
|
246
243
|
format: 'md',
|
|
@@ -250,13 +247,23 @@ async function createProcessorsCacheEntry({
|
|
|
250
247
|
format: 'mdx',
|
|
251
248
|
}),
|
|
252
249
|
};
|
|
250
|
+
}
|
|
253
251
|
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
252
|
+
async function createProcessorsCacheEntry({
|
|
253
|
+
options,
|
|
254
|
+
}: {
|
|
255
|
+
options: Options;
|
|
256
|
+
}): Promise<SimpleProcessors> {
|
|
257
|
+
const compilers = ProcessorsCache.get(options);
|
|
258
|
+
if (compilers) {
|
|
259
|
+
return compilers;
|
|
260
|
+
}
|
|
261
|
+
const processors = await createProcessors({options});
|
|
262
|
+
ProcessorsCache.set(options, processors);
|
|
263
|
+
return processors;
|
|
257
264
|
}
|
|
258
265
|
|
|
259
|
-
export async function
|
|
266
|
+
export async function getProcessor({
|
|
260
267
|
filePath,
|
|
261
268
|
mdxFrontMatter,
|
|
262
269
|
options,
|
|
@@ -265,7 +272,8 @@ export async function createProcessorCached({
|
|
|
265
272
|
mdxFrontMatter: MDXFrontMatter;
|
|
266
273
|
options: Options;
|
|
267
274
|
}): Promise<SimpleProcessor> {
|
|
268
|
-
const
|
|
275
|
+
const processors =
|
|
276
|
+
options.processors ?? (await createProcessorsCacheEntry({options}));
|
|
269
277
|
|
|
270
278
|
const format = getFormat({
|
|
271
279
|
filePath,
|
|
@@ -273,5 +281,5 @@ export async function createProcessorCached({
|
|
|
273
281
|
markdownConfigFormat: options.markdownConfig.format,
|
|
274
282
|
});
|
|
275
283
|
|
|
276
|
-
return format === 'md' ?
|
|
284
|
+
return format === 'md' ? processors.mdProcessor : processors.mdxProcessor;
|
|
277
285
|
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
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 logger from '@docusaurus/logger';
|
|
10
|
+
import {escapePath, type WebpackCompilerName} from '@docusaurus/utils';
|
|
11
|
+
import {getProcessor, type SimpleProcessorResult} from './processor';
|
|
12
|
+
import {validateMDXFrontMatter} from './frontMatter';
|
|
13
|
+
import preprocessor from './preprocessor';
|
|
14
|
+
import type {Options} from './loader';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* When this throws, it generally means that there's no metadata file associated
|
|
18
|
+
* with this MDX document. It can happen when using MDX partials (usually
|
|
19
|
+
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
20
|
+
* function in config
|
|
21
|
+
*/
|
|
22
|
+
export async function readMetadataPath(metadataPath: string): Promise<string> {
|
|
23
|
+
try {
|
|
24
|
+
return await fs.readFile(metadataPath, 'utf8');
|
|
25
|
+
} catch (error) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
logger.interpolate`MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`,
|
|
28
|
+
{cause: error as Error},
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Converts assets an object with Webpack require calls code.
|
|
35
|
+
* This is useful for mdx files to reference co-located assets using relative
|
|
36
|
+
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
37
|
+
* For now, we only handle that for images and paths starting with `./`:
|
|
38
|
+
*
|
|
39
|
+
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
40
|
+
*/
|
|
41
|
+
export function createAssetsExportCode({
|
|
42
|
+
assets,
|
|
43
|
+
inlineMarkdownAssetImageFileLoader,
|
|
44
|
+
}: {
|
|
45
|
+
assets: unknown;
|
|
46
|
+
inlineMarkdownAssetImageFileLoader: string;
|
|
47
|
+
}): string {
|
|
48
|
+
if (
|
|
49
|
+
typeof assets !== 'object' ||
|
|
50
|
+
!assets ||
|
|
51
|
+
Object.keys(assets).length === 0
|
|
52
|
+
) {
|
|
53
|
+
return 'undefined';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// TODO implementation can be completed/enhanced
|
|
57
|
+
function createAssetValueCode(assetValue: unknown): string | undefined {
|
|
58
|
+
if (Array.isArray(assetValue)) {
|
|
59
|
+
const arrayItemCodes = assetValue.map(
|
|
60
|
+
(item: unknown) => createAssetValueCode(item) ?? 'undefined',
|
|
61
|
+
);
|
|
62
|
+
return `[${arrayItemCodes.join(', ')}]`;
|
|
63
|
+
}
|
|
64
|
+
// Only process string values starting with ./
|
|
65
|
+
// We could enhance this logic and check if file exists on disc?
|
|
66
|
+
if (typeof assetValue === 'string' && assetValue.startsWith('./')) {
|
|
67
|
+
// TODO do we have other use-cases than image assets?
|
|
68
|
+
// Probably not worth adding more support, as we want to move to Webpack 5 new asset system (https://github.com/facebook/docusaurus/pull/4708)
|
|
69
|
+
return `require("${inlineMarkdownAssetImageFileLoader}${escapePath(
|
|
70
|
+
assetValue,
|
|
71
|
+
)}").default`;
|
|
72
|
+
}
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const assetEntries = Object.entries(assets);
|
|
77
|
+
|
|
78
|
+
const codeLines = assetEntries
|
|
79
|
+
.map(([key, value]: [string, unknown]) => {
|
|
80
|
+
const assetRequireCode = createAssetValueCode(value);
|
|
81
|
+
return assetRequireCode ? `"${key}": ${assetRequireCode},` : undefined;
|
|
82
|
+
})
|
|
83
|
+
.filter(Boolean);
|
|
84
|
+
|
|
85
|
+
return `{\n${codeLines.join('\n')}\n}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* data.contentTitle is set by the remark contentTitle plugin
|
|
90
|
+
*/
|
|
91
|
+
export function extractContentTitleData(data: {
|
|
92
|
+
[key: string]: unknown;
|
|
93
|
+
}): string | undefined {
|
|
94
|
+
return data.contentTitle as string | undefined;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function compileToJSX({
|
|
98
|
+
filePath,
|
|
99
|
+
fileContent,
|
|
100
|
+
frontMatter,
|
|
101
|
+
options,
|
|
102
|
+
compilerName,
|
|
103
|
+
}: {
|
|
104
|
+
filePath: string;
|
|
105
|
+
fileContent: string;
|
|
106
|
+
frontMatter: Record<string, unknown>;
|
|
107
|
+
options: Options;
|
|
108
|
+
compilerName: WebpackCompilerName;
|
|
109
|
+
}): Promise<SimpleProcessorResult> {
|
|
110
|
+
const preprocessedFileContent = preprocessor({
|
|
111
|
+
fileContent,
|
|
112
|
+
filePath,
|
|
113
|
+
admonitions: options.admonitions,
|
|
114
|
+
markdownConfig: options.markdownConfig,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const mdxFrontMatter = validateMDXFrontMatter(frontMatter.mdx);
|
|
118
|
+
|
|
119
|
+
const processor = await getProcessor({
|
|
120
|
+
filePath,
|
|
121
|
+
options,
|
|
122
|
+
mdxFrontMatter,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
return await processor.process({
|
|
127
|
+
content: preprocessedFileContent,
|
|
128
|
+
filePath,
|
|
129
|
+
frontMatter,
|
|
130
|
+
compilerName,
|
|
131
|
+
});
|
|
132
|
+
} catch (errorUnknown) {
|
|
133
|
+
const error = errorUnknown as Error;
|
|
134
|
+
|
|
135
|
+
// MDX can emit errors that have useful extra attributes
|
|
136
|
+
const errorJSON = JSON.stringify(error, null, 2);
|
|
137
|
+
const errorDetails =
|
|
138
|
+
errorJSON === '{}'
|
|
139
|
+
? // regular JS error case: print stacktrace
|
|
140
|
+
error.stack ?? 'N/A'
|
|
141
|
+
: // MDX error: print extra attributes + stacktrace
|
|
142
|
+
`${errorJSON}\n${error.stack}`;
|
|
143
|
+
|
|
144
|
+
throw new Error(
|
|
145
|
+
`MDX compilation failed for file ${logger.path(filePath)}\nCause: ${
|
|
146
|
+
error.message
|
|
147
|
+
}\nDetails:\n${errorDetails}`,
|
|
148
|
+
// TODO error cause doesn't seem to be used by Webpack stats.errors :s
|
|
149
|
+
{cause: error},
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|