@docusaurus/mdx-loader 2.4.1 → 3.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/lib/format.d.ts +14 -0
- package/lib/format.d.ts.map +1 -0
- package/lib/format.js +42 -0
- package/lib/format.js.map +1 -0
- package/lib/frontMatter.d.ts +13 -0
- package/lib/frontMatter.d.ts.map +1 -0
- package/lib/frontMatter.js +23 -0
- package/lib/frontMatter.js.map +1 -0
- package/lib/index.d.ts +7 -7
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js.map +1 -1
- package/lib/loader.d.ts +5 -14
- package/lib/loader.d.ts.map +1 -1
- package/lib/loader.js +45 -81
- package/lib/loader.js.map +1 -1
- package/lib/preprocessor.d.ts +18 -0
- package/lib/preprocessor.d.ts.map +1 -0
- package/lib/preprocessor.js +34 -0
- package/lib/preprocessor.js.map +1 -0
- package/lib/processor.d.ts +36 -0
- package/lib/processor.d.ts.map +1 -0
- package/lib/processor.js +160 -0
- package/lib/processor.js.map +1 -0
- package/lib/remark/admonitions/index.d.ts +4 -9
- package/lib/remark/admonitions/index.d.ts.map +1 -1
- package/lib/remark/admonitions/index.js +70 -128
- package/lib/remark/admonitions/index.js.map +1 -1
- package/lib/remark/details/index.d.ts +9 -0
- package/lib/remark/details/index.d.ts.map +1 -0
- package/lib/remark/details/index.js +23 -0
- package/lib/remark/details/index.js.map +1 -0
- package/lib/remark/head/index.d.ts +9 -0
- package/lib/remark/head/index.d.ts.map +1 -0
- package/lib/remark/head/index.js +23 -0
- package/lib/remark/head/index.js.map +1 -0
- package/lib/remark/headings/index.d.ts.map +1 -1
- package/lib/remark/headings/index.js +3 -3
- package/lib/remark/headings/index.js.map +1 -1
- package/lib/remark/{unwrapMdxCodeBlocks/index.d.ts → mdx1Compat/codeCompatPlugin.d.ts} +2 -2
- package/lib/remark/mdx1Compat/codeCompatPlugin.d.ts.map +1 -0
- package/lib/remark/mdx1Compat/codeCompatPlugin.js +34 -0
- package/lib/remark/mdx1Compat/codeCompatPlugin.js.map +1 -0
- package/lib/remark/mermaid/index.d.ts.map +1 -1
- package/lib/remark/mermaid/index.js +1 -0
- package/lib/remark/mermaid/index.js.map +1 -1
- package/lib/remark/toc/index.d.ts +8 -2
- package/lib/remark/toc/index.d.ts.map +1 -1
- package/lib/remark/toc/index.js +53 -23
- package/lib/remark/toc/index.js.map +1 -1
- package/lib/remark/transformImage/index.d.ts +2 -1
- package/lib/remark/transformImage/index.d.ts.map +1 -1
- package/lib/remark/transformImage/index.js +42 -15
- package/lib/remark/transformImage/index.js.map +1 -1
- package/lib/remark/transformLinks/index.d.ts +2 -1
- package/lib/remark/transformLinks/index.d.ts.map +1 -1
- package/lib/remark/transformLinks/index.js +30 -11
- package/lib/remark/transformLinks/index.js.map +1 -1
- package/lib/remark/utils/index.d.ts +4 -2
- package/lib/remark/utils/index.d.ts.map +1 -1
- package/lib/remark/utils/index.js +82 -11
- package/lib/remark/utils/index.js.map +1 -1
- package/package.json +28 -20
- package/src/format.ts +50 -0
- package/src/frontMatter.ts +31 -0
- package/src/index.ts +10 -6
- package/src/loader.ts +72 -107
- package/src/preprocessor.ts +47 -0
- package/src/processor.ts +243 -0
- package/src/remark/admonitions/README.md +1 -1
- package/src/remark/admonitions/index.ts +92 -171
- package/src/remark/details/index.ts +25 -0
- package/src/remark/head/index.ts +25 -0
- package/src/remark/headings/index.ts +5 -3
- package/src/remark/mdx1Compat/codeCompatPlugin.ts +36 -0
- package/src/remark/mermaid/index.ts +2 -0
- package/src/remark/toc/index.ts +80 -26
- package/src/remark/transformImage/index.ts +54 -19
- package/src/remark/transformLinks/index.ts +44 -17
- package/src/remark/utils/index.ts +108 -9
- package/lib/remark/unwrapMdxCodeBlocks/index.d.ts.map +0 -1
- package/lib/remark/unwrapMdxCodeBlocks/index.js +0 -29
- package/lib/remark/unwrapMdxCodeBlocks/index.js.map +0 -1
- package/src/deps.d.ts +0 -31
- package/src/remark/unwrapMdxCodeBlocks/index.ts +0 -33
package/src/remark/toc/index.ts
CHANGED
|
@@ -8,28 +8,42 @@
|
|
|
8
8
|
import {parse, type ParserOptions} from '@babel/parser';
|
|
9
9
|
import traverse from '@babel/traverse';
|
|
10
10
|
import stringifyObject from 'stringify-object';
|
|
11
|
-
import toString from 'mdast-util-to-string';
|
|
12
11
|
import visit from 'unist-util-visit';
|
|
13
12
|
import {toValue} from '../utils';
|
|
14
|
-
|
|
15
13
|
import type {Identifier} from '@babel/types';
|
|
16
|
-
import type {TOCItem} from '../..';
|
|
17
14
|
import type {Node, Parent} from 'unist';
|
|
18
15
|
import type {Heading, Literal} from 'mdast';
|
|
16
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
19
17
|
import type {Transformer} from 'unified';
|
|
20
18
|
|
|
19
|
+
// TODO as of April 2023, no way to import/re-export this ESM type easily :/
|
|
20
|
+
// This might change soon, likely after TS 5.2
|
|
21
|
+
// See https://github.com/microsoft/TypeScript/issues/49721#issuecomment-1517839391
|
|
22
|
+
// import type {Plugin} from 'unified';
|
|
23
|
+
type Plugin = any; // TODO fix this asap
|
|
24
|
+
|
|
25
|
+
export type TOCItem = {
|
|
26
|
+
readonly value: string;
|
|
27
|
+
readonly id: string;
|
|
28
|
+
readonly level: number;
|
|
29
|
+
};
|
|
30
|
+
|
|
21
31
|
const parseOptions: ParserOptions = {
|
|
22
32
|
plugins: ['jsx'],
|
|
23
33
|
sourceType: 'module',
|
|
24
34
|
};
|
|
25
35
|
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
const isImport = (child: Node): child is Literal => child.type === 'import';
|
|
36
|
+
const isImport = (child: any): child is Literal =>
|
|
37
|
+
child.type === 'mdxjsEsm' && child.value.startsWith('import');
|
|
29
38
|
const hasImports = (index: number) => index > -1;
|
|
30
|
-
const isExport = (child:
|
|
39
|
+
const isExport = (child: any): child is Literal =>
|
|
40
|
+
child.type === 'mdxjsEsm' && child.value.startsWith('export');
|
|
41
|
+
|
|
42
|
+
interface PluginOptions {
|
|
43
|
+
name?: string;
|
|
44
|
+
}
|
|
31
45
|
|
|
32
|
-
const isTarget = (child: Literal) => {
|
|
46
|
+
const isTarget = (child: Literal, name: string) => {
|
|
33
47
|
let found = false;
|
|
34
48
|
const ast = parse(child.value, parseOptions);
|
|
35
49
|
traverse(ast, {
|
|
@@ -42,24 +56,23 @@ const isTarget = (child: Literal) => {
|
|
|
42
56
|
return found;
|
|
43
57
|
};
|
|
44
58
|
|
|
45
|
-
const getOrCreateExistingTargetIndex = (
|
|
59
|
+
const getOrCreateExistingTargetIndex = async (
|
|
60
|
+
children: Node[],
|
|
61
|
+
name: string,
|
|
62
|
+
) => {
|
|
46
63
|
let importsIndex = -1;
|
|
47
64
|
let targetIndex = -1;
|
|
48
65
|
|
|
49
66
|
children.forEach((child, index) => {
|
|
50
67
|
if (isImport(child)) {
|
|
51
68
|
importsIndex = index;
|
|
52
|
-
} else if (isExport(child) && isTarget(child)) {
|
|
69
|
+
} else if (isExport(child) && isTarget(child, name)) {
|
|
53
70
|
targetIndex = index;
|
|
54
71
|
}
|
|
55
72
|
});
|
|
56
73
|
|
|
57
74
|
if (targetIndex === -1) {
|
|
58
|
-
const target =
|
|
59
|
-
default: false,
|
|
60
|
-
type: 'export',
|
|
61
|
-
value: `export const ${name} = [];`,
|
|
62
|
-
};
|
|
75
|
+
const target = await createExportNode(name, []);
|
|
63
76
|
|
|
64
77
|
targetIndex = hasImports(importsIndex) ? importsIndex + 1 : 0;
|
|
65
78
|
children.splice(targetIndex, 0, target);
|
|
@@ -68,31 +81,72 @@ const getOrCreateExistingTargetIndex = (children: Node[]) => {
|
|
|
68
81
|
return targetIndex;
|
|
69
82
|
};
|
|
70
83
|
|
|
71
|
-
|
|
72
|
-
|
|
84
|
+
const plugin: Plugin = function plugin(
|
|
85
|
+
options: PluginOptions = {},
|
|
86
|
+
): Transformer {
|
|
87
|
+
const name = options.name || 'toc';
|
|
88
|
+
|
|
89
|
+
return async (root) => {
|
|
90
|
+
const {toString} = await import('mdast-util-to-string');
|
|
73
91
|
const headings: TOCItem[] = [];
|
|
74
92
|
|
|
75
|
-
visit(root, 'heading', (child: Heading
|
|
93
|
+
visit(root, 'heading', (child: Heading) => {
|
|
76
94
|
const value = toString(child);
|
|
77
95
|
|
|
78
|
-
// depth:
|
|
79
|
-
if (
|
|
96
|
+
// depth:1 headings are titles and not included in the TOC
|
|
97
|
+
if (!value || child.depth < 2) {
|
|
80
98
|
return;
|
|
81
99
|
}
|
|
82
100
|
|
|
83
101
|
headings.push({
|
|
84
|
-
value: toValue(child),
|
|
102
|
+
value: toValue(child, toString),
|
|
85
103
|
id: child.data!.id as string,
|
|
86
104
|
level: child.depth,
|
|
87
105
|
});
|
|
88
106
|
});
|
|
89
107
|
const {children} = root as Parent<Literal>;
|
|
90
|
-
const targetIndex = getOrCreateExistingTargetIndex(children);
|
|
108
|
+
const targetIndex = await getOrCreateExistingTargetIndex(children, name);
|
|
91
109
|
|
|
92
|
-
if (headings
|
|
93
|
-
children[targetIndex]
|
|
94
|
-
headings,
|
|
95
|
-
)};`;
|
|
110
|
+
if (headings?.length) {
|
|
111
|
+
children[targetIndex] = await createExportNode(name, headings);
|
|
96
112
|
}
|
|
97
113
|
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export default plugin;
|
|
117
|
+
|
|
118
|
+
async function createExportNode(name: string, object: any) {
|
|
119
|
+
const {valueToEstree} = await import('estree-util-value-to-estree');
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
type: 'mdxjsEsm',
|
|
123
|
+
value: `export const ${name} = ${stringifyObject(object)}`,
|
|
124
|
+
data: {
|
|
125
|
+
estree: {
|
|
126
|
+
type: 'Program',
|
|
127
|
+
body: [
|
|
128
|
+
{
|
|
129
|
+
type: 'ExportNamedDeclaration',
|
|
130
|
+
declaration: {
|
|
131
|
+
type: 'VariableDeclaration',
|
|
132
|
+
declarations: [
|
|
133
|
+
{
|
|
134
|
+
type: 'VariableDeclarator',
|
|
135
|
+
id: {
|
|
136
|
+
type: 'Identifier',
|
|
137
|
+
name,
|
|
138
|
+
},
|
|
139
|
+
init: valueToEstree(object),
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
kind: 'const',
|
|
143
|
+
},
|
|
144
|
+
specifiers: [],
|
|
145
|
+
source: null,
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
sourceType: 'module',
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
};
|
|
98
152
|
}
|
|
@@ -9,7 +9,6 @@ import path from 'path';
|
|
|
9
9
|
import url from 'url';
|
|
10
10
|
import fs from 'fs-extra';
|
|
11
11
|
import {promisify} from 'util';
|
|
12
|
-
import logger from '@docusaurus/logger';
|
|
13
12
|
import {
|
|
14
13
|
toMessageRelativeFilePath,
|
|
15
14
|
posixPath,
|
|
@@ -20,15 +19,20 @@ import {
|
|
|
20
19
|
import visit from 'unist-util-visit';
|
|
21
20
|
import escapeHtml from 'escape-html';
|
|
22
21
|
import sizeOf from 'image-size';
|
|
22
|
+
import logger from '@docusaurus/logger';
|
|
23
|
+
import {assetRequireAttributeValue} from '../utils';
|
|
24
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
23
25
|
import type {Transformer} from 'unified';
|
|
26
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
27
|
+
import type {MdxJsxTextElement} from 'mdast-util-mdx';
|
|
28
|
+
import type {Image} from 'mdast';
|
|
24
29
|
import type {Parent} from 'unist';
|
|
25
|
-
import type {Image, Literal} from 'mdast';
|
|
26
30
|
|
|
27
31
|
const {
|
|
28
32
|
loaders: {inlineMarkdownImageFileLoader},
|
|
29
33
|
} = getFileLoaderUtils();
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
type PluginOptions = {
|
|
32
36
|
staticDirs: string[];
|
|
33
37
|
siteDir: string;
|
|
34
38
|
};
|
|
@@ -40,10 +44,14 @@ type Context = PluginOptions & {
|
|
|
40
44
|
type Target = [node: Image, index: number, parent: Parent];
|
|
41
45
|
|
|
42
46
|
async function toImageRequireNode(
|
|
43
|
-
[node
|
|
47
|
+
[node]: Target,
|
|
44
48
|
imagePath: string,
|
|
45
49
|
filePath: string,
|
|
46
50
|
) {
|
|
51
|
+
// MdxJsxTextElement => see https://github.com/facebook/docusaurus/pull/8288#discussion_r1125871405
|
|
52
|
+
const jsxNode = node as unknown as MdxJsxTextElement;
|
|
53
|
+
const attributes: MdxJsxTextElement['attributes'] = [];
|
|
54
|
+
|
|
47
55
|
let relativeImagePath = posixPath(
|
|
48
56
|
path.relative(path.dirname(filePath), imagePath),
|
|
49
57
|
);
|
|
@@ -52,21 +60,46 @@ async function toImageRequireNode(
|
|
|
52
60
|
const parsedUrl = url.parse(node.url);
|
|
53
61
|
const hash = parsedUrl.hash ?? '';
|
|
54
62
|
const search = parsedUrl.search ?? '';
|
|
55
|
-
|
|
56
|
-
const alt = node.alt ? `alt={"${escapeHtml(node.alt)}"} ` : '';
|
|
57
|
-
const src = `require("${inlineMarkdownImageFileLoader}${
|
|
63
|
+
const requireString = `${inlineMarkdownImageFileLoader}${
|
|
58
64
|
escapePath(relativeImagePath) + search
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
65
|
+
}`;
|
|
66
|
+
if (node.alt) {
|
|
67
|
+
attributes.push({
|
|
68
|
+
type: 'mdxJsxAttribute',
|
|
69
|
+
name: 'alt',
|
|
70
|
+
value: escapeHtml(node.alt),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
attributes.push({
|
|
75
|
+
type: 'mdxJsxAttribute',
|
|
76
|
+
name: 'src',
|
|
77
|
+
value: assetRequireAttributeValue(requireString, hash),
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (node.title) {
|
|
81
|
+
attributes.push({
|
|
82
|
+
type: 'mdxJsxAttribute',
|
|
83
|
+
name: 'title',
|
|
84
|
+
value: escapeHtml(node.title),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
63
88
|
try {
|
|
64
89
|
const size = (await promisify(sizeOf)(imagePath))!;
|
|
65
90
|
if (size.width) {
|
|
66
|
-
|
|
91
|
+
attributes.push({
|
|
92
|
+
type: 'mdxJsxAttribute',
|
|
93
|
+
name: 'width',
|
|
94
|
+
value: String(size.width),
|
|
95
|
+
});
|
|
67
96
|
}
|
|
68
97
|
if (size.height) {
|
|
69
|
-
|
|
98
|
+
attributes.push({
|
|
99
|
+
type: 'mdxJsxAttribute',
|
|
100
|
+
name: 'height',
|
|
101
|
+
value: String(size.height),
|
|
102
|
+
});
|
|
70
103
|
}
|
|
71
104
|
} catch (err) {
|
|
72
105
|
// Workaround for https://github.com/yarnpkg/berry/pull/3889#issuecomment-1034469784
|
|
@@ -77,12 +110,14 @@ ${(err as Error).message}`;
|
|
|
77
110
|
}
|
|
78
111
|
}
|
|
79
112
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
};
|
|
113
|
+
Object.keys(jsxNode).forEach(
|
|
114
|
+
(key) => delete jsxNode[key as keyof typeof jsxNode],
|
|
115
|
+
);
|
|
84
116
|
|
|
85
|
-
|
|
117
|
+
jsxNode.type = 'mdxJsxTextElement';
|
|
118
|
+
jsxNode.name = 'img';
|
|
119
|
+
jsxNode.attributes = attributes;
|
|
120
|
+
jsxNode.children = [];
|
|
86
121
|
}
|
|
87
122
|
|
|
88
123
|
async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
|
|
@@ -122,7 +157,7 @@ async function getImageAbsolutePath(
|
|
|
122
157
|
}
|
|
123
158
|
return imageFilePath;
|
|
124
159
|
}
|
|
125
|
-
//
|
|
160
|
+
// relative paths are resolved against the source file's folder
|
|
126
161
|
const imageFilePath = path.join(
|
|
127
162
|
path.dirname(filePath),
|
|
128
163
|
decodeURIComponent(imagePath),
|
|
@@ -17,8 +17,11 @@ import {
|
|
|
17
17
|
} from '@docusaurus/utils';
|
|
18
18
|
import visit from 'unist-util-visit';
|
|
19
19
|
import escapeHtml from 'escape-html';
|
|
20
|
-
import {
|
|
20
|
+
import {assetRequireAttributeValue} from '../utils';
|
|
21
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
21
22
|
import type {Transformer} from 'unified';
|
|
23
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
24
|
+
import type {MdxJsxTextElement} from 'mdast-util-mdx';
|
|
22
25
|
import type {Parent} from 'unist';
|
|
23
26
|
import type {Link, Literal} from 'mdast';
|
|
24
27
|
|
|
@@ -26,7 +29,7 @@ const {
|
|
|
26
29
|
loaders: {inlineMarkdownLinkFileLoader},
|
|
27
30
|
} = getFileLoaderUtils();
|
|
28
31
|
|
|
29
|
-
|
|
32
|
+
type PluginOptions = {
|
|
30
33
|
staticDirs: string[];
|
|
31
34
|
siteDir: string;
|
|
32
35
|
};
|
|
@@ -40,11 +43,15 @@ type Target = [node: Link, index: number, parent: Parent];
|
|
|
40
43
|
/**
|
|
41
44
|
* Transforms the link node to a JSX `<a>` element with a `require()` call.
|
|
42
45
|
*/
|
|
43
|
-
function toAssetRequireNode(
|
|
44
|
-
[node
|
|
46
|
+
async function toAssetRequireNode(
|
|
47
|
+
[node]: Target,
|
|
45
48
|
assetPath: string,
|
|
46
49
|
filePath: string,
|
|
47
50
|
) {
|
|
51
|
+
// MdxJsxTextElement => see https://github.com/facebook/docusaurus/pull/8288#discussion_r1125871405
|
|
52
|
+
const jsxNode = node as unknown as MdxJsxTextElement;
|
|
53
|
+
const attributes: MdxJsxTextElement['attributes'] = [];
|
|
54
|
+
|
|
48
55
|
// require("assets/file.pdf") means requiring from a package called assets
|
|
49
56
|
const relativeAssetPath = `./${posixPath(
|
|
50
57
|
path.relative(path.dirname(filePath), assetPath),
|
|
@@ -54,23 +61,43 @@ function toAssetRequireNode(
|
|
|
54
61
|
const hash = parsedUrl.hash ?? '';
|
|
55
62
|
const search = parsedUrl.search ?? '';
|
|
56
63
|
|
|
57
|
-
const
|
|
64
|
+
const requireString = `${
|
|
58
65
|
// A hack to stop Webpack from using its built-in loader to parse JSON
|
|
59
66
|
path.extname(relativeAssetPath) === '.json'
|
|
60
67
|
? `${relativeAssetPath.replace('.json', '.raw')}!=`
|
|
61
68
|
: ''
|
|
62
|
-
}${inlineMarkdownLinkFileLoader}${
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
}${inlineMarkdownLinkFileLoader}${escapePath(relativeAssetPath) + search}`;
|
|
70
|
+
|
|
71
|
+
attributes.push({
|
|
72
|
+
type: 'mdxJsxAttribute',
|
|
73
|
+
name: 'target',
|
|
74
|
+
value: '_blank',
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
attributes.push({
|
|
78
|
+
type: 'mdxJsxAttribute',
|
|
79
|
+
name: 'href',
|
|
80
|
+
value: assetRequireAttributeValue(requireString, hash),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
if (node.title) {
|
|
84
|
+
attributes.push({
|
|
85
|
+
type: 'mdxJsxAttribute',
|
|
86
|
+
name: 'title',
|
|
87
|
+
value: escapeHtml(node.title),
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const {children} = node;
|
|
92
|
+
|
|
93
|
+
Object.keys(jsxNode).forEach(
|
|
94
|
+
(key) => delete jsxNode[key as keyof typeof jsxNode],
|
|
95
|
+
);
|
|
72
96
|
|
|
73
|
-
|
|
97
|
+
jsxNode.type = 'mdxJsxTextElement';
|
|
98
|
+
jsxNode.name = 'a';
|
|
99
|
+
jsxNode.attributes = attributes;
|
|
100
|
+
jsxNode.children = children;
|
|
74
101
|
}
|
|
75
102
|
|
|
76
103
|
async function ensureAssetFileExist(assetPath: string, sourceFilePath: string) {
|
|
@@ -144,7 +171,7 @@ async function processLinkNode(target: Target, context: Context) {
|
|
|
144
171
|
context,
|
|
145
172
|
);
|
|
146
173
|
if (assetPath) {
|
|
147
|
-
toAssetRequireNode(target, assetPath, context.filePath);
|
|
174
|
+
await toAssetRequireNode(target, assetPath, context.filePath);
|
|
148
175
|
}
|
|
149
176
|
}
|
|
150
177
|
|
|
@@ -6,31 +6,130 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import escapeHtml from 'escape-html';
|
|
9
|
-
import toString from 'mdast-util-to-string';
|
|
10
9
|
import type {Parent} from 'unist';
|
|
11
10
|
import type {PhrasingContent, Heading} from 'mdast';
|
|
11
|
+
import type {
|
|
12
|
+
MdxJsxAttribute,
|
|
13
|
+
MdxJsxAttributeValueExpression,
|
|
14
|
+
MdxJsxTextElement,
|
|
15
|
+
// @ts-expect-error: TODO see https://github.com/microsoft/TypeScript/issues/49721
|
|
16
|
+
} from 'mdast-util-mdx';
|
|
12
17
|
|
|
13
|
-
export function stringifyContent(
|
|
14
|
-
|
|
18
|
+
export function stringifyContent(
|
|
19
|
+
node: Parent,
|
|
20
|
+
toString: (param: unknown) => string, // TODO weird but works
|
|
21
|
+
): string {
|
|
22
|
+
return (node.children as PhrasingContent[])
|
|
23
|
+
.map((item) => toValue(item, toString))
|
|
24
|
+
.join('');
|
|
15
25
|
}
|
|
16
26
|
|
|
17
|
-
|
|
27
|
+
// TODO This is really a workaround, and not super reliable
|
|
28
|
+
// For now we only support serializing tagName, className and content
|
|
29
|
+
// Can we implement the TOC with real JSX nodes instead of html strings later?
|
|
30
|
+
function mdxJsxTextElementToHtml(
|
|
31
|
+
element: MdxJsxTextElement,
|
|
32
|
+
toString: (param: unknown) => string, // TODO weird but works
|
|
33
|
+
): string {
|
|
34
|
+
const tag = element.name;
|
|
35
|
+
|
|
36
|
+
const attributes = element.attributes.filter(
|
|
37
|
+
(child): child is MdxJsxAttribute => child.type === 'mdxJsxAttribute',
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const classAttribute =
|
|
41
|
+
attributes.find((attr) => attr.name === 'className') ??
|
|
42
|
+
attributes.find((attr) => attr.name === 'class');
|
|
43
|
+
|
|
44
|
+
const classAttributeString = classAttribute
|
|
45
|
+
? `class="${escapeHtml(String(classAttribute.value))}"`
|
|
46
|
+
: ``;
|
|
47
|
+
|
|
48
|
+
const allAttributes = classAttributeString ? ` ${classAttributeString}` : '';
|
|
49
|
+
|
|
50
|
+
const content = stringifyContent(element, toString);
|
|
51
|
+
|
|
52
|
+
return `<${tag}${allAttributes}>${content}</${tag}>`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function toValue(
|
|
56
|
+
node: PhrasingContent | Heading,
|
|
57
|
+
toString: (param: unknown) => string, // TODO weird but works
|
|
58
|
+
): string {
|
|
18
59
|
switch (node.type) {
|
|
60
|
+
case 'mdxJsxTextElement': {
|
|
61
|
+
return mdxJsxTextElementToHtml(node as MdxJsxTextElement, toString);
|
|
62
|
+
}
|
|
19
63
|
case 'text':
|
|
20
64
|
return escapeHtml(node.value);
|
|
21
65
|
case 'heading':
|
|
22
|
-
return stringifyContent(node);
|
|
66
|
+
return stringifyContent(node, toString);
|
|
23
67
|
case 'inlineCode':
|
|
24
68
|
return `<code>${escapeHtml(node.value)}</code>`;
|
|
25
69
|
case 'emphasis':
|
|
26
|
-
return `<em>${stringifyContent(node)}</em>`;
|
|
70
|
+
return `<em>${stringifyContent(node, toString)}</em>`;
|
|
27
71
|
case 'strong':
|
|
28
|
-
return `<strong>${stringifyContent(node)}</strong>`;
|
|
72
|
+
return `<strong>${stringifyContent(node, toString)}</strong>`;
|
|
29
73
|
case 'delete':
|
|
30
|
-
return `<del>${stringifyContent(node)}</del>`;
|
|
74
|
+
return `<del>${stringifyContent(node, toString)}</del>`;
|
|
31
75
|
case 'link':
|
|
32
|
-
return stringifyContent(node);
|
|
76
|
+
return stringifyContent(node, toString);
|
|
33
77
|
default:
|
|
34
78
|
return toString(node);
|
|
35
79
|
}
|
|
36
80
|
}
|
|
81
|
+
|
|
82
|
+
export function assetRequireAttributeValue(
|
|
83
|
+
requireString: string,
|
|
84
|
+
hash: string,
|
|
85
|
+
): MdxJsxAttributeValueExpression {
|
|
86
|
+
return {
|
|
87
|
+
type: 'mdxJsxAttributeValueExpression',
|
|
88
|
+
value: `require("${requireString}").default${hash && ` + '${hash}'`}`,
|
|
89
|
+
data: {
|
|
90
|
+
estree: {
|
|
91
|
+
type: 'Program',
|
|
92
|
+
body: [
|
|
93
|
+
{
|
|
94
|
+
type: 'ExpressionStatement',
|
|
95
|
+
expression: {
|
|
96
|
+
type: 'BinaryExpression',
|
|
97
|
+
left: {
|
|
98
|
+
type: 'MemberExpression',
|
|
99
|
+
object: {
|
|
100
|
+
type: 'CallExpression',
|
|
101
|
+
callee: {
|
|
102
|
+
type: 'Identifier',
|
|
103
|
+
name: 'require',
|
|
104
|
+
},
|
|
105
|
+
arguments: [
|
|
106
|
+
{
|
|
107
|
+
type: 'Literal',
|
|
108
|
+
value: requireString,
|
|
109
|
+
raw: `"${requireString}"`,
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
optional: false,
|
|
113
|
+
},
|
|
114
|
+
property: {
|
|
115
|
+
type: 'Identifier',
|
|
116
|
+
name: 'default',
|
|
117
|
+
},
|
|
118
|
+
computed: false,
|
|
119
|
+
optional: false,
|
|
120
|
+
},
|
|
121
|
+
operator: '+',
|
|
122
|
+
right: {
|
|
123
|
+
type: 'Literal',
|
|
124
|
+
value: hash,
|
|
125
|
+
raw: `"${hash}"`,
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
sourceType: 'module',
|
|
131
|
+
comments: [],
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/unwrapMdxCodeBlocks/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAC,WAAW,EAAE,SAAS,EAAC,MAAM,SAAS,CAAC;AASpD,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,WAAW,CAe3D"}
|
|
@@ -1,29 +0,0 @@
|
|
|
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
|
-
const tslib_1 = require("tslib");
|
|
10
|
-
const unist_util_visit_1 = tslib_1.__importDefault(require("unist-util-visit"));
|
|
11
|
-
// This plugin is mostly to help integrating Docusaurus with translation systems
|
|
12
|
-
// that do not support well MDX embedded JSX syntax (like Crowdin).
|
|
13
|
-
// We wrap the JSX syntax in code blocks so that translation tools don't mess up
|
|
14
|
-
// with the markup, but the JSX inside such code blocks should still be
|
|
15
|
-
// evaluated as JSX
|
|
16
|
-
// See https://github.com/facebook/docusaurus/pull/4278
|
|
17
|
-
function plugin() {
|
|
18
|
-
return (root) => {
|
|
19
|
-
(0, unist_util_visit_1.default)(root, 'code', (node, index, parent) => {
|
|
20
|
-
if (node.lang === 'mdx-code-block') {
|
|
21
|
-
const newChildren = this.parse(node.value).children;
|
|
22
|
-
// Replace the mdx code block by its content, parsed
|
|
23
|
-
parent.children.splice(parent.children.indexOf(node), 1, ...newChildren);
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
exports.default = plugin;
|
|
29
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/unwrapMdxCodeBlocks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,gFAAqC;AAIrC,gFAAgF;AAChF,mEAAmE;AACnE,gFAAgF;AAChF,uEAAuE;AACvE,mBAAmB;AACnB,uDAAuD;AACvD,SAAwB,MAAM;IAC5B,OAAO,CAAC,IAAI,EAAE,EAAE;QACd,IAAA,0BAAK,EAAC,IAAI,EAAE,MAAM,EAAE,CAAC,IAAU,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YAChD,IAAI,IAAI,CAAC,IAAI,KAAK,gBAAgB,EAAE;gBAClC,MAAM,WAAW,GAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAY,CAAC,QAAQ,CAAC;gBAEhE,oDAAoD;gBACpD,MAAO,CAAC,QAAQ,CAAC,MAAM,CACrB,MAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,EAC9B,CAAC,EACD,GAAG,WAAW,CACf,CAAC;aACH;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAfD,yBAeC"}
|
package/src/deps.d.ts
DELETED
|
@@ -1,31 +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
|
-
// TODO Types provided by MDX 2.0 https://github.com/mdx-js/mdx/blob/main/packages/mdx/types/index.d.ts
|
|
9
|
-
declare module '@mdx-js/mdx' {
|
|
10
|
-
import type {Processor, Plugin} from 'unified';
|
|
11
|
-
|
|
12
|
-
type MDXPlugin =
|
|
13
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
-
[Plugin<any[]>, any] | Plugin<any[]>;
|
|
15
|
-
|
|
16
|
-
type Options = {
|
|
17
|
-
filepath?: string;
|
|
18
|
-
skipExport?: boolean;
|
|
19
|
-
wrapExport?: string;
|
|
20
|
-
remarkPlugins?: MDXPlugin[];
|
|
21
|
-
rehypePlugins?: MDXPlugin[];
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
export function sync(content: string, options?: Options): string;
|
|
25
|
-
export function createMdxAstCompiler(options?: Options): Processor;
|
|
26
|
-
export function createCompiler(options?: Options): Processor;
|
|
27
|
-
export default function mdx(
|
|
28
|
-
content: string,
|
|
29
|
-
options?: Options,
|
|
30
|
-
): Promise<string>;
|
|
31
|
-
}
|
|
@@ -1,33 +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 visit from 'unist-util-visit';
|
|
9
|
-
import type {Transformer, Processor} from 'unified';
|
|
10
|
-
import type {Code, Parent} from 'mdast';
|
|
11
|
-
|
|
12
|
-
// This plugin is mostly to help integrating Docusaurus with translation systems
|
|
13
|
-
// that do not support well MDX embedded JSX syntax (like Crowdin).
|
|
14
|
-
// We wrap the JSX syntax in code blocks so that translation tools don't mess up
|
|
15
|
-
// with the markup, but the JSX inside such code blocks should still be
|
|
16
|
-
// evaluated as JSX
|
|
17
|
-
// See https://github.com/facebook/docusaurus/pull/4278
|
|
18
|
-
export default function plugin(this: Processor): Transformer {
|
|
19
|
-
return (root) => {
|
|
20
|
-
visit(root, 'code', (node: Code, index, parent) => {
|
|
21
|
-
if (node.lang === 'mdx-code-block') {
|
|
22
|
-
const newChildren = (this.parse(node.value) as Parent).children;
|
|
23
|
-
|
|
24
|
-
// Replace the mdx code block by its content, parsed
|
|
25
|
-
parent!.children.splice(
|
|
26
|
-
parent!.children.indexOf(node),
|
|
27
|
-
1,
|
|
28
|
-
...newChildren,
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
}
|