@docusaurus/mdx-loader 2.0.0-beta.15 → 2.0.0-beta.16
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.map +1 -1
- package/lib/index.js +21 -16
- package/lib/index.js.map +1 -1
- package/lib/remark/headings/index.d.ts +1 -2
- package/lib/remark/headings/index.d.ts.map +1 -1
- package/lib/remark/headings/index.js +7 -8
- package/lib/remark/headings/index.js.map +1 -1
- package/lib/remark/toc/index.d.ts +3 -3
- package/lib/remark/toc/index.d.ts.map +1 -1
- package/lib/remark/toc/index.js +20 -7
- package/lib/remark/toc/index.js.map +1 -1
- package/lib/remark/transformImage/index.d.ts +3 -3
- package/lib/remark/transformImage/index.d.ts.map +1 -1
- package/lib/remark/transformImage/index.js +14 -19
- package/lib/remark/transformImage/index.js.map +1 -1
- package/lib/remark/transformLinks/index.d.ts +3 -3
- package/lib/remark/transformLinks/index.d.ts.map +1 -1
- package/lib/remark/transformLinks/index.js +3 -4
- package/lib/remark/transformLinks/index.js.map +1 -1
- package/lib/remark/unwrapMdxCodeBlocks/index.d.ts +1 -2
- package/lib/remark/unwrapMdxCodeBlocks/index.d.ts.map +1 -1
- package/lib/remark/unwrapMdxCodeBlocks/index.js +5 -5
- package/lib/remark/unwrapMdxCodeBlocks/index.js.map +1 -1
- package/lib/remark/utils/index.d.ts +2 -2
- package/lib/remark/utils/index.d.ts.map +1 -1
- package/lib/remark/utils/index.js +17 -19
- package/lib/remark/utils/index.js.map +1 -1
- package/package.json +10 -10
- package/src/deps.d.ts +4 -3
- package/src/index.ts +22 -18
- package/src/remark/headings/index.ts +7 -13
- package/src/remark/toc/index.ts +27 -12
- package/src/remark/transformImage/index.ts +15 -22
- package/src/remark/transformLinks/index.ts +4 -7
- package/src/remark/unwrapMdxCodeBlocks/index.ts +7 -10
- package/src/remark/utils/index.ts +19 -21
- package/lib/remark/toc/search.d.ts +0 -14
- package/lib/remark/toc/search.d.ts.map +0 -1
- package/lib/remark/toc/search.js +0 -66
- package/lib/remark/toc/search.js.map +0 -1
- package/src/remark/toc/search.ts +0 -80
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;AAkBH,OAAO,KAAK,EAAC,4BAA4B,EAAC,MAAM,wBAAwB,CAAC;AACzE,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,SAAS,CAAC;AAa3C,aAAK,OAAO,GAAG,4BAA4B,GAAG;IAC5C,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,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACrC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACnC,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAkBH,OAAO,KAAK,EAAC,4BAA4B,EAAC,MAAM,wBAAwB,CAAC;AACzE,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,SAAS,CAAC;AAa3C,aAAK,OAAO,GAAG,4BAA4B,GAAG;IAC5C,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,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;QACrC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KACnC,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AA6DF,wBAA8B,SAAS,CACrC,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CA+Gf"}
|
package/lib/index.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
|
-
const fs_extra_1 = require("fs-extra");
|
|
10
|
+
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
11
11
|
const mdx_1 = (0, tslib_1.__importDefault)(require("@mdx-js/mdx"));
|
|
12
12
|
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
13
13
|
const remark_emoji_1 = (0, tslib_1.__importDefault)(require("remark-emoji"));
|
|
@@ -25,22 +25,29 @@ const DEFAULT_OPTIONS = {
|
|
|
25
25
|
beforeDefaultRemarkPlugins: [],
|
|
26
26
|
beforeDefaultRehypePlugins: [],
|
|
27
27
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
/**
|
|
29
|
+
* When this throws, it generally means that there's no metadata file associated
|
|
30
|
+
* with this MDX document. It can happen when using MDX partials (usually
|
|
31
|
+
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
32
|
+
* function in config
|
|
33
|
+
*/
|
|
31
34
|
async function readMetadataPath(metadataPath) {
|
|
32
35
|
try {
|
|
33
|
-
return await
|
|
36
|
+
return await fs_extra_1.default.readFile(metadataPath, 'utf8');
|
|
34
37
|
}
|
|
35
|
-
catch (
|
|
36
|
-
|
|
38
|
+
catch (err) {
|
|
39
|
+
logger_1.default.error `MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
|
|
40
|
+
throw err;
|
|
37
41
|
}
|
|
38
42
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Converts assets an object with Webpack require calls code.
|
|
45
|
+
* This is useful for mdx files to reference co-located assets using relative
|
|
46
|
+
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
47
|
+
* For now, we only handle that for images and paths starting with `./`:
|
|
48
|
+
*
|
|
49
|
+
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
50
|
+
*/
|
|
44
51
|
function createAssetsExportCode(assets) {
|
|
45
52
|
if (Object.keys(assets).length === 0) {
|
|
46
53
|
return 'undefined';
|
|
@@ -115,7 +122,7 @@ async function mdxLoader(fileString) {
|
|
|
115
122
|
return callback(err);
|
|
116
123
|
}
|
|
117
124
|
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
118
|
-
// Partial are not expected to have
|
|
125
|
+
// Partial are not expected to have associated metadata files or front matter
|
|
119
126
|
const isMDXPartial = options.isMDXPartial && options.isMDXPartial(filePath);
|
|
120
127
|
if (isMDXPartial && hasFrontMatter) {
|
|
121
128
|
const errorMessage = `Docusaurus MDX partial files should not contain FrontMatter.
|
|
@@ -127,9 +134,7 @@ ${JSON.stringify(frontMatter, null, 2)}`;
|
|
|
127
134
|
if (shouldError) {
|
|
128
135
|
return callback(new Error(errorMessage));
|
|
129
136
|
}
|
|
130
|
-
|
|
131
|
-
logger_1.default.warn(errorMessage);
|
|
132
|
-
}
|
|
137
|
+
logger_1.default.warn(errorMessage);
|
|
133
138
|
}
|
|
134
139
|
}
|
|
135
140
|
function getMetadataPath() {
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,qEAA0B;AAC1B,mEAA8B;AAC9B,6EAAwC;AACxC,6EAAiC;AACjC,6CAK2B;AAC3B,qFAA+C;AAC/C,8EAAyC;AACzC,oEAA+B;AAC/B,oGAA+D;AAC/D,0FAAqD;AACrD,0FAAqD;AAIrD,MAAM,EACJ,OAAO,EAAE,EAAC,6BAA6B,EAAC,GACzC,GAAG,IAAA,0BAAkB,GAAE,CAAC;AAEzB,MAAM,eAAe,GAAiC;IACpD,aAAa,EAAE,EAAE;IACjB,aAAa,EAAE,CAAC,6BAAmB,EAAE,sBAAK,EAAE,kBAAQ,EAAE,aAAG,CAAC;IAC1D,0BAA0B,EAAE,EAAE;IAC9B,0BAA0B,EAAE,EAAE;CAC/B,CAAC;AAgBF;;;;;GAKG;AACH,KAAK,UAAU,gBAAgB,CAAC,YAAoB;IAClD,IAAI;QACF,OAAO,MAAM,kBAAE,CAAC,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;KAChD;IAAC,OAAO,GAAG,EAAE;QACZ,gBAAM,CAAC,KAAK,CAAA,gDAAgD,YAAY,4DAA4D,CAAC;QACrI,MAAM,GAAG,CAAC;KACX;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,sBAAsB,CAAC,MAA+B;IAC7D,IAAI,M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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;;;;EAIb,WAAW;EACX,MAAM;CACP,CAAC;IAEA,OAAO,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAC9B,CAAC;AAlHD,4BAkHC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAIzC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAIzC,MAAM,CAAC,OAAO,UAAU,MAAM,IAAI,WAAW,CAwD5C"}
|
|
@@ -11,10 +11,10 @@ const tslib_1 = require("tslib");
|
|
|
11
11
|
const utils_1 = require("@docusaurus/utils");
|
|
12
12
|
const unist_util_visit_1 = (0, tslib_1.__importDefault)(require("unist-util-visit"));
|
|
13
13
|
const mdast_util_to_string_1 = (0, tslib_1.__importDefault)(require("mdast-util-to-string"));
|
|
14
|
-
function
|
|
15
|
-
|
|
14
|
+
function plugin() {
|
|
15
|
+
return (root) => {
|
|
16
16
|
const slugs = (0, utils_1.createSlugger)();
|
|
17
|
-
|
|
17
|
+
(0, unist_util_visit_1.default)(root, 'heading', (headingNode) => {
|
|
18
18
|
const data = headingNode.data || (headingNode.data = {});
|
|
19
19
|
const properties = (data.hProperties || (data.hProperties = {}));
|
|
20
20
|
let { id } = properties;
|
|
@@ -31,7 +31,8 @@ function headings() {
|
|
|
31
31
|
id = parsedHeading.id || slugs.slug(heading);
|
|
32
32
|
if (parsedHeading.id) {
|
|
33
33
|
// When there's an id, it is always in the last child node
|
|
34
|
-
// Sometimes heading is in multiple "parts" (** syntax creates a child
|
|
34
|
+
// Sometimes heading is in multiple "parts" (** syntax creates a child
|
|
35
|
+
// node):
|
|
35
36
|
// ## part1 *part2* part3 {#id}
|
|
36
37
|
const lastNode = headingNode.children[headingNode.children.length - 1];
|
|
37
38
|
if (headingNode.children.length > 1) {
|
|
@@ -52,10 +53,8 @@ function headings() {
|
|
|
52
53
|
}
|
|
53
54
|
data.id = id;
|
|
54
55
|
properties.id = id;
|
|
55
|
-
};
|
|
56
|
-
(0, unist_util_visit_1.default)(ast, 'heading', visitor);
|
|
56
|
+
});
|
|
57
57
|
};
|
|
58
|
-
return transformer;
|
|
59
58
|
}
|
|
60
|
-
exports.default =
|
|
59
|
+
exports.default = plugin;
|
|
61
60
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,gMAAgM;AAEhM,6CAAwE;AACxE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/headings/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,gMAAgM;AAEhM,6CAAwE;AACxE,qFAAqC;AACrC,6FAA4C;AAK5C,SAAwB,MAAM;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AAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CACxB,CAAC;oBAEV,IAAI,WAAW,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;wBACnC,MAAM,YAAY,GAAG,IAAA,8BAAsB,EAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;wBACjE,iDAAiD;wBACjD,IAAI,YAAY,EAAE;4BAChB,QAAQ,CAAC,KAAK,GAAG,YAAY,CAAC;yBAC/B;wBACD,mEAAmE;6BAC9D;4BACH,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC;yBAC5B;qBACF;yBAAM;wBACL,QAAQ,CAAC,KAAK,GAAG,aAAa,CAAC,IAAI,CAAC;qBACrC;iBACF;aACF;YAED,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;YACb,UAAU,CAAC,EAAE,GAAG,EAAE,CAAC;QACrB,CAAC,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAxDD,yBAwDC"}
|
|
@@ -4,10 +4,10 @@
|
|
|
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 {
|
|
7
|
+
import type { Transformer } from 'unified';
|
|
8
8
|
interface PluginOptions {
|
|
9
9
|
name?: string;
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
export
|
|
11
|
+
export default function plugin(options?: PluginOptions): Transformer;
|
|
12
|
+
export {};
|
|
13
13
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAWzC,UAAU,aAAa;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAyCD,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,OAAO,GAAE,aAAkB,GAAG,WAAW,CA6BvE"}
|
package/lib/remark/toc/index.js
CHANGED
|
@@ -10,7 +10,9 @@ const tslib_1 = require("tslib");
|
|
|
10
10
|
const parser_1 = require("@babel/parser");
|
|
11
11
|
const traverse_1 = (0, tslib_1.__importDefault)(require("@babel/traverse"));
|
|
12
12
|
const stringify_object_1 = (0, tslib_1.__importDefault)(require("stringify-object"));
|
|
13
|
-
const
|
|
13
|
+
const mdast_util_to_string_1 = (0, tslib_1.__importDefault)(require("mdast-util-to-string"));
|
|
14
|
+
const unist_util_visit_1 = (0, tslib_1.__importDefault)(require("unist-util-visit"));
|
|
15
|
+
const utils_1 = require("../utils");
|
|
14
16
|
const parseOptions = {
|
|
15
17
|
plugins: ['jsx'],
|
|
16
18
|
sourceType: 'module',
|
|
@@ -52,17 +54,28 @@ const getOrCreateExistingTargetIndex = (children, name) => {
|
|
|
52
54
|
}
|
|
53
55
|
return targetIndex;
|
|
54
56
|
};
|
|
55
|
-
|
|
57
|
+
function plugin(options = {}) {
|
|
56
58
|
const name = options.name || 'toc';
|
|
57
|
-
|
|
58
|
-
const headings =
|
|
59
|
-
|
|
59
|
+
return (root) => {
|
|
60
|
+
const headings = [];
|
|
61
|
+
(0, unist_util_visit_1.default)(root, 'heading', (child, _index, parent) => {
|
|
62
|
+
const value = (0, mdast_util_to_string_1.default)(child);
|
|
63
|
+
// depth:1 headings are titles and not included in the TOC
|
|
64
|
+
if (parent !== root || !value || child.depth < 2) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
headings.push({
|
|
68
|
+
value: (0, utils_1.toValue)(child),
|
|
69
|
+
id: child.data.id,
|
|
70
|
+
level: child.depth,
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
const { children } = root;
|
|
60
74
|
const targetIndex = getOrCreateExistingTargetIndex(children, name);
|
|
61
75
|
if (headings && headings.length) {
|
|
62
76
|
children[targetIndex].value = `export const ${name} = ${(0, stringify_object_1.default)(headings)};`;
|
|
63
77
|
}
|
|
64
78
|
};
|
|
65
|
-
|
|
66
|
-
};
|
|
79
|
+
}
|
|
67
80
|
exports.default = plugin;
|
|
68
81
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,0CAAwD;AAExD,4EAAuC;AACvC,qFAA+C;AAC/C,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,0CAAwD;AAExD,4EAAuC;AACvC,qFAA+C;AAC/C,6FAA4C;AAC5C,qFAAqC;AACrC,oCAAiC;AAOjC,MAAM,YAAY,GAAkB;IAClC,OAAO,EAAE,CAAC,KAAK,CAAC;IAChB,UAAU,EAAE,QAAQ;CACrB,CAAC;AAEF,MAAM,QAAQ,GAAG,CAAC,KAAW,EAAoB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC;AAC5E,MAAM,UAAU,GAAG,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;AACjD,MAAM,QAAQ,GAAG,CAAC,KAAW,EAAoB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,CAAC;AAM5E,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAE,IAAY,EAAE,EAAE;IAChD,IAAI,KAAK,GAAG,KAAK,CAAC;IAClB,MAAM,GAAG,GAAG,IAAA,cAAK,EAAC,KAAK,CAAC,KAAK,EAAE,YAAY,CAAC,CAAC;IAC7C,IAAA,kBAAQ,EAAC,GAAG,EAAE;QACZ,kBAAkB,EAAE,CAAC,IAAI,EAAE,EAAE;YAC3B,IAAK,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c,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;YACxD,MAAM,KAAK,GAAG,IAAA,8BAAQ,EAAC,KAAK,CAAC,CAAC;YAE9B,0DAA0D;YAC1D,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE;gBAChD,OAAO;aACR;YAED,QAAQ,CAAC,IAAI,CAAC;gBACZ,KAAK,EAAE,IAAA,eAAO,EAAC,KAAK,CAAC;gBACrB,EAAE,EAAE,KAAK,CAAC,IAAK,CAAC,EAAY;gBAC5B,KAAK,EAAE,KAAK,CAAC,KAAK;aACnB,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,MAAM,EAAC,QAAQ,EAAC,GAAG,IAAuB,CAAC;QAC3C,MAAM,WAAW,GAAG,8BAA8B,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;QAEnE,IAAI,QAAQ,IAAI,QAAQ,CAAC,MAAM,EAAE;YAC/B,QAAQ,CAAC,WAAW,CAAC,CAAC,KAAK,GAAG,gBAAgB,IAAI,MAAM,IAAA,0BAAe,EACrE,QAAQ,CACT,GAAG,CAAC;SACN;IACH,CAAC,CAAC;AACJ,CAAC;AA7BD,yBA6BC"}
|
|
@@ -4,11 +4,11 @@
|
|
|
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 {
|
|
7
|
+
import type { Transformer } from 'unified';
|
|
8
8
|
declare type PluginOptions = {
|
|
9
9
|
staticDirs: string[];
|
|
10
10
|
siteDir: string;
|
|
11
11
|
};
|
|
12
|
-
|
|
13
|
-
export
|
|
12
|
+
export default function plugin(options: PluginOptions): Transformer;
|
|
13
|
+
export {};
|
|
14
14
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH,OAAO,KAAK,EAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAQzC,aAAK,aAAa,GAAG;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAqHF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,WAAW,CAUlE"}
|
|
@@ -39,13 +39,13 @@ async function toImageRequireNode(node, imagePath, filePath) {
|
|
|
39
39
|
height = ` height="${size.height}"`;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
catch (
|
|
42
|
+
catch (err) {
|
|
43
43
|
logger_1.default.error `The image at path=${imagePath} can't be read correctly. Please ensure it's a valid image.
|
|
44
|
-
${
|
|
44
|
+
${err.message}`;
|
|
45
45
|
}
|
|
46
46
|
Object.keys(jsxNode).forEach((key) => delete jsxNode[key]);
|
|
47
47
|
jsxNode.type = 'jsx';
|
|
48
|
-
jsxNode.value = `<img ${alt}src={${src}}${title}${width}${height} />`;
|
|
48
|
+
jsxNode.value = `<img loading="lazy" ${alt}src={${src}}${title}${width}${height} />`;
|
|
49
49
|
}
|
|
50
50
|
async function ensureImageFileExist(imagePath, sourceFilePath) {
|
|
51
51
|
const imageExists = await fs_extra_1.default.pathExists(imagePath);
|
|
@@ -70,14 +70,10 @@ async function getImageAbsolutePath(imagePath, { siteDir, filePath, staticDirs }
|
|
|
70
70
|
}
|
|
71
71
|
return imageFilePath;
|
|
72
72
|
}
|
|
73
|
-
//
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const imageFilePath = path_1.default.join(path_1.default.dirname(filePath), imagePath);
|
|
78
|
-
await ensureImageFileExist(imageFilePath, filePath);
|
|
79
|
-
return imageFilePath;
|
|
80
|
-
}
|
|
73
|
+
// relative paths are resolved against the source file's folder
|
|
74
|
+
const imageFilePath = path_1.default.join(path_1.default.dirname(filePath), imagePath);
|
|
75
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
76
|
+
return imageFilePath;
|
|
81
77
|
}
|
|
82
78
|
async function processImageNode(node, context) {
|
|
83
79
|
if (!node.url) {
|
|
@@ -85,27 +81,26 @@ async function processImageNode(node, context) {
|
|
|
85
81
|
}
|
|
86
82
|
const parsedUrl = url_1.default.parse(node.url);
|
|
87
83
|
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
88
|
-
// pathname:// is an escape hatch,
|
|
89
|
-
//
|
|
90
|
-
// we don't have to document this for now,
|
|
91
|
-
// it's mostly to make next release less risky (2.0.0-alpha.59)
|
|
84
|
+
// pathname:// is an escape hatch, in case user does not want her images to
|
|
85
|
+
// be converted to require calls going through webpack loader
|
|
92
86
|
if (parsedUrl.protocol === 'pathname:') {
|
|
93
87
|
node.url = node.url.replace('pathname://', '');
|
|
94
88
|
}
|
|
95
89
|
return;
|
|
96
90
|
}
|
|
91
|
+
// We try to convert image urls without protocol to images with require calls
|
|
92
|
+
// going through webpack ensures that image assets exist at build time
|
|
97
93
|
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
|
|
98
94
|
await toImageRequireNode(node, imagePath, context.filePath);
|
|
99
95
|
}
|
|
100
|
-
|
|
101
|
-
|
|
96
|
+
function plugin(options) {
|
|
97
|
+
return async (root, vfile) => {
|
|
102
98
|
const promises = [];
|
|
103
99
|
(0, unist_util_visit_1.default)(root, 'image', (node) => {
|
|
104
100
|
promises.push(processImageNode(node, { ...options, filePath: vfile.path }));
|
|
105
101
|
});
|
|
106
102
|
await Promise.all(promises);
|
|
107
103
|
};
|
|
108
|
-
|
|
109
|
-
};
|
|
104
|
+
}
|
|
110
105
|
exports.default = plugin;
|
|
111
106
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|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E;YACnC,QAAQ,CAAC,IAAI,CACX,gBAAgB,CAAC,IAAI,EAAE,EAAC,GAAG,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,IAAK,EAAC,CAAC,CAC5D,CAAC;QACJ,CAAC,CAAC,CAAC;QACH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC,CAAC;AACJ,CAAC;AAVD,yBAUC"}
|
|
@@ -4,11 +4,11 @@
|
|
|
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 {
|
|
7
|
+
import type { Transformer } from 'unified';
|
|
8
8
|
declare type PluginOptions = {
|
|
9
9
|
staticDirs: string[];
|
|
10
10
|
siteDir: string;
|
|
11
11
|
};
|
|
12
|
-
|
|
13
|
-
export
|
|
12
|
+
export default function plugin(options: PluginOptions): Transformer;
|
|
13
|
+
export {};
|
|
14
14
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,OAAO,KAAK,EAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,SAAS,CAAC;AAOzC,aAAK,aAAa,GAAG;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AA4GF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,WAAW,CAQlE"}
|
|
@@ -89,15 +89,14 @@ async function processLinkNode(node, context) {
|
|
|
89
89
|
toAssetRequireNode(node, assetPath, context.filePath);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
function plugin(options) {
|
|
93
|
+
return async (root, vfile) => {
|
|
94
94
|
const promises = [];
|
|
95
95
|
(0, unist_util_visit_1.default)(root, 'link', (node) => {
|
|
96
96
|
promises.push(processLinkNode(node, { ...options, filePath: vfile.path }));
|
|
97
97
|
});
|
|
98
98
|
await Promise.all(promises);
|
|
99
99
|
};
|
|
100
|
-
|
|
101
|
-
};
|
|
100
|
+
}
|
|
102
101
|
exports.default = plugin;
|
|
103
102
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6CAM2B;AAC3B,qFAAqC;AACrC,6DAAwB;AACxB,2DAAsB;AACtB,qEAA0B;AAC1B,2EAAqC;AACrC,oCAA0C;AAI1C,MAAM,EACJ,OAAO,EAAE,EAAC,4BAA4B,EAAC,GACxC,GAAG,IAAA,0BAAkB,GAAE,CAAC;AAWzB,8DAA8D;AAC9D,SAAS,kBAAkB,CAAC,IAAU,EAAE,SAAiB,EAAE,QAAgB;;IACzE,MAAM,OAAO,GAAG,IAA+B,CAAC;IAChD,IAAI,iBAAiB,GAAG,IAAA,iBAAS,EAC/B,cAAI,CAAC,QAAQ,CAAC,cAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC,CACjD,CAAC;IACF,0EAA0E;IAC1E,iBAAiB,GAAG,KAAK,iBAAiB,EAAE,CAAC;IAE7C,MAAM,SAAS,GAAG,aAAG,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACtC,MAAM,IAAI,GAAG,MAAA,SAAS,CAAC,IAAI,mCAAI,EAAE,CAAC;IAClC,MAAM,MAAM,GAAG,MAAA,SAAS,CAAC,MAAM,mCAAI,EAAE,CAAC;IAEtC,MAAM,IAAI,GAAG,YAAY;IACvB,sEAAsE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AAE;QAC3B,MAAM,QAAQ,GAAoB,EAAE,CAAC;QACrC,IAAA,0BAAK,EAAC,IAAI,EAAE,MAAM,EAAE,CAAC,IAAU,EAAE,EAAE;YACjC,QAAQ,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,EAAE,EAAC,GAAG,OAAO,EAAE,QAAQ,EAAE,KAAK,CAAC,IAAK,EAAC,CAAC,CAAC,CAAC;QAC5E,CAAC,CAAC,CAAC;QACH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC,CAAC;AACJ,CAAC;AARD,yBAQC"}
|
|
@@ -5,6 +5,5 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
import type { Transformer, Processor } from 'unified';
|
|
8
|
-
|
|
9
|
-
export default plugin;
|
|
8
|
+
export default function plugin(this: Processor): Transformer;
|
|
10
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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"}
|
|
@@ -9,12 +9,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
10
|
const unist_util_visit_1 = (0, tslib_1.__importDefault)(require("unist-util-visit"));
|
|
11
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
|
|
14
|
-
//
|
|
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
|
|
15
16
|
// See https://github.com/facebook/docusaurus/pull/4278
|
|
16
17
|
function plugin() {
|
|
17
|
-
|
|
18
|
+
return (root) => {
|
|
18
19
|
(0, unist_util_visit_1.default)(root, 'code', (node, _index, parent) => {
|
|
19
20
|
if (node.lang === 'mdx-code-block') {
|
|
20
21
|
const newChildren = this.parse(node.value).children;
|
|
@@ -23,7 +24,6 @@ function plugin() {
|
|
|
23
24
|
}
|
|
24
25
|
});
|
|
25
26
|
};
|
|
26
|
-
return transformer;
|
|
27
27
|
}
|
|
28
28
|
exports.default = plugin;
|
|
29
29
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/unwrapMdxCodeBlocks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,qFAAqC;AAIrC,gFAAgF;AAChF,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/unwrapMdxCodeBlocks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,qFAAqC;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,MAAM,EAAE,MAAM,EAAE,EAAE;YACjD,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"}
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
import type { Parent } from 'unist';
|
|
8
|
-
import type {
|
|
8
|
+
import type { PhrasingContent, Heading } from 'mdast';
|
|
9
9
|
export declare function stringifyContent(node: Parent): string;
|
|
10
|
-
export declare function toValue(node:
|
|
10
|
+
export declare function toValue(node: PhrasingContent | Heading): string;
|
|
11
11
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,OAAO,CAAC;AAClC,OAAO,KAAK,EAAC,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/utils/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,OAAO,CAAC;AAClC,OAAO,KAAK,EAAC,eAAe,EAAE,OAAO,EAAC,MAAM,OAAO,CAAC;AAEpD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,GAAG,MAAM,CAoB/D"}
|
|
@@ -11,28 +11,26 @@ const tslib_1 = require("tslib");
|
|
|
11
11
|
const escape_html_1 = (0, tslib_1.__importDefault)(require("escape-html"));
|
|
12
12
|
const mdast_util_to_string_1 = (0, tslib_1.__importDefault)(require("mdast-util-to-string"));
|
|
13
13
|
function stringifyContent(node) {
|
|
14
|
-
return
|
|
15
|
-
.map(toValue)
|
|
16
|
-
.join('');
|
|
14
|
+
return node.children.map(toValue).join('');
|
|
17
15
|
}
|
|
18
16
|
exports.stringifyContent = stringifyContent;
|
|
19
17
|
function toValue(node) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
18
|
+
switch (node === null || node === void 0 ? void 0 : node.type) {
|
|
19
|
+
case 'text':
|
|
20
|
+
return (0, escape_html_1.default)(node.value);
|
|
21
|
+
case 'heading':
|
|
22
|
+
return stringifyContent(node);
|
|
23
|
+
case 'inlineCode':
|
|
24
|
+
return `<code>${(0, escape_html_1.default)(node.value)}</code>`;
|
|
25
|
+
case 'emphasis':
|
|
26
|
+
return `<em>${stringifyContent(node)}</em>`;
|
|
27
|
+
case 'strong':
|
|
28
|
+
return `<strong>${stringifyContent(node)}</strong>`;
|
|
29
|
+
case 'delete':
|
|
30
|
+
return `<del>${stringifyContent(node)}</del>`;
|
|
31
|
+
case 'link':
|
|
32
|
+
return stringifyContent(node);
|
|
33
|
+
default:
|
|
36
34
|
}
|
|
37
35
|
return (0, mdast_util_to_string_1.default)(node);
|
|
38
36
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/utils/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,2EAAqC;AACrC,6FAA4C;AAI5C,SAAgB,gBAAgB,CAAC,IAAY;IAC3C,OAAQ,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/utils/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;AAEH,2EAAqC;AACrC,6FAA4C;AAI5C,SAAgB,gBAAgB,CAAC,IAAY;IAC3C,OAAQ,IAAI,CAAC,QAA8B,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACpE,CAAC;AAFD,4CAEC;AAED,SAAgB,OAAO,CAAC,IAA+B;IACrD,QAAQ,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,IAAI,EAAE;QAClB,KAAK,MAAM;YACT,OAAO,IAAA,qBAAU,EAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAChC,KAAK,SAAS;YACZ,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAChC,KAAK,YAAY;YACf,OAAO,SAAS,IAAA,qBAAU,EAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC;QAClD,KAAK,UAAU;YACb,OAAO,OAAO,gBAAgB,CAAC,IAAI,CAAC,OAAO,CAAC;QAC9C,KAAK,QAAQ;YACX,OAAO,WAAW,gBAAgB,CAAC,IAAI,CAAC,WAAW,CAAC;QACtD,KAAK,QAAQ;YACX,OAAO,QAAQ,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC;QAChD,KAAK,MAAM;YACT,OAAO,gBAAgB,CAAC,IAAI,CAAC,CAAC;QAChC,QAAQ;KACT;IAED,OAAO,IAAA,8BAAQ,EAAC,IAAI,CAAC,CAAC;AACxB,CAAC;AApBD,0BAoBC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/mdx-loader",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.16",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "src/mdx-loader.d.ts",
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@babel/parser": "^7.
|
|
22
|
-
"@babel/traverse": "^7.
|
|
23
|
-
"@docusaurus/logger": "2.0.0-beta.
|
|
24
|
-
"@docusaurus/utils": "2.0.0-beta.
|
|
25
|
-
"@mdx-js/mdx": "^1.6.
|
|
21
|
+
"@babel/parser": "^7.17.3",
|
|
22
|
+
"@babel/traverse": "^7.17.3",
|
|
23
|
+
"@docusaurus/logger": "2.0.0-beta.16",
|
|
24
|
+
"@docusaurus/utils": "2.0.0-beta.16",
|
|
25
|
+
"@mdx-js/mdx": "^1.6.22",
|
|
26
26
|
"escape-html": "^1.0.3",
|
|
27
27
|
"file-loader": "^6.2.0",
|
|
28
|
-
"fs-extra": "^10.0.
|
|
28
|
+
"fs-extra": "^10.0.1",
|
|
29
29
|
"image-size": "^1.0.1",
|
|
30
30
|
"mdast-util-to-string": "^2.0.0",
|
|
31
31
|
"remark-emoji": "^2.1.0",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
"tslib": "^2.3.1",
|
|
34
34
|
"unist-util-visit": "^2.0.2",
|
|
35
35
|
"url-loader": "^4.1.1",
|
|
36
|
-
"webpack": "^5.
|
|
36
|
+
"webpack": "^5.69.1"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@docusaurus/types": "2.0.0-beta.
|
|
39
|
+
"@docusaurus/types": "2.0.0-beta.16",
|
|
40
40
|
"@types/escape-html": "^1.0.1",
|
|
41
41
|
"@types/mdast": "^3.0.7",
|
|
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": "eb43c4d4f95a4fb97dc9bb9dc615413e0dc2e1e7"
|
|
58
58
|
}
|
package/src/deps.d.ts
CHANGED
|
@@ -23,7 +23,8 @@ declare module '@mdx-js/mdx' {
|
|
|
23
23
|
function createMdxAstCompiler(options?: Options): Processor;
|
|
24
24
|
function createCompiler(options?: Options): Processor;
|
|
25
25
|
}
|
|
26
|
-
function mdx(
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
export default function mdx(
|
|
27
|
+
content: string,
|
|
28
|
+
options?: mdx.Options,
|
|
29
|
+
): Promise<string>;
|
|
29
30
|
}
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
8
|
+
import fs from 'fs-extra';
|
|
9
9
|
import mdx from '@mdx-js/mdx';
|
|
10
10
|
import logger from '@docusaurus/logger';
|
|
11
11
|
import emoji from 'remark-emoji';
|
|
@@ -49,24 +49,29 @@ type Options = RemarkAndRehypePluginOptions & {
|
|
|
49
49
|
filepath: string;
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
/**
|
|
53
|
+
* When this throws, it generally means that there's no metadata file associated
|
|
54
|
+
* with this MDX document. It can happen when using MDX partials (usually
|
|
55
|
+
* starting with _). That's why it's important to provide the `isMDXPartial`
|
|
56
|
+
* function in config
|
|
57
|
+
*/
|
|
55
58
|
async function readMetadataPath(metadataPath: string) {
|
|
56
59
|
try {
|
|
57
|
-
return await readFile(metadataPath, 'utf8');
|
|
58
|
-
} catch (
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
);
|
|
60
|
+
return await fs.readFile(metadataPath, 'utf8');
|
|
61
|
+
} catch (err) {
|
|
62
|
+
logger.error`MDX loader can't read MDX metadata file path=${metadataPath}. Maybe the isMDXPartial option function was not provided?`;
|
|
63
|
+
throw err;
|
|
62
64
|
}
|
|
63
65
|
}
|
|
64
66
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Converts assets an object with Webpack require calls code.
|
|
69
|
+
* This is useful for mdx files to reference co-located assets using relative
|
|
70
|
+
* paths. Those assets should enter the Webpack assets pipeline and be hashed.
|
|
71
|
+
* For now, we only handle that for images and paths starting with `./`:
|
|
72
|
+
*
|
|
73
|
+
* `{image: "./myImage.png"}` => `{image: require("./myImage.png")}`
|
|
74
|
+
*/
|
|
70
75
|
function createAssetsExportCode(assets: Record<string, unknown>) {
|
|
71
76
|
if (Object.keys(assets).length === 0) {
|
|
72
77
|
return 'undefined';
|
|
@@ -148,7 +153,7 @@ export default async function mdxLoader(
|
|
|
148
153
|
filepath: filePath,
|
|
149
154
|
};
|
|
150
155
|
|
|
151
|
-
let result;
|
|
156
|
+
let result: string;
|
|
152
157
|
try {
|
|
153
158
|
result = await mdx(content, options);
|
|
154
159
|
} catch (err) {
|
|
@@ -156,7 +161,7 @@ export default async function mdxLoader(
|
|
|
156
161
|
}
|
|
157
162
|
|
|
158
163
|
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
159
|
-
// Partial are not expected to have
|
|
164
|
+
// Partial are not expected to have associated metadata files or front matter
|
|
160
165
|
const isMDXPartial = options.isMDXPartial && options.isMDXPartial(filePath);
|
|
161
166
|
if (isMDXPartial && hasFrontMatter) {
|
|
162
167
|
const errorMessage = `Docusaurus MDX partial files should not contain FrontMatter.
|
|
@@ -168,9 +173,8 @@ ${JSON.stringify(frontMatter, null, 2)}`;
|
|
|
168
173
|
const shouldError = process.env.NODE_ENV === 'test' || process.env.CI;
|
|
169
174
|
if (shouldError) {
|
|
170
175
|
return callback(new Error(errorMessage));
|
|
171
|
-
} else {
|
|
172
|
-
logger.warn(errorMessage);
|
|
173
176
|
}
|
|
177
|
+
logger.warn(errorMessage);
|
|
174
178
|
}
|
|
175
179
|
}
|
|
176
180
|
|
|
@@ -8,17 +8,16 @@
|
|
|
8
8
|
/* Based on remark-slug (https://github.com/remarkjs/remark-slug) and gatsby-remark-autolink-headers (https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-remark-autolink-headers) */
|
|
9
9
|
|
|
10
10
|
import {parseMarkdownHeadingId, createSlugger} from '@docusaurus/utils';
|
|
11
|
-
import visit
|
|
11
|
+
import visit from 'unist-util-visit';
|
|
12
12
|
import toString from 'mdast-util-to-string';
|
|
13
13
|
import type {Transformer} from 'unified';
|
|
14
14
|
import type {Parent} from 'unist';
|
|
15
15
|
import type {Heading, Text} from 'mdast';
|
|
16
16
|
|
|
17
|
-
function
|
|
18
|
-
|
|
17
|
+
export default function plugin(): Transformer {
|
|
18
|
+
return (root) => {
|
|
19
19
|
const slugs = createSlugger();
|
|
20
|
-
|
|
21
|
-
const visitor: Visitor<Heading> = (headingNode) => {
|
|
20
|
+
visit(root, 'heading', (headingNode: Heading) => {
|
|
22
21
|
const data = headingNode.data || (headingNode.data = {});
|
|
23
22
|
const properties = (data.hProperties || (data.hProperties = {})) as {
|
|
24
23
|
id: string;
|
|
@@ -44,7 +43,8 @@ function headings(): Transformer {
|
|
|
44
43
|
|
|
45
44
|
if (parsedHeading.id) {
|
|
46
45
|
// When there's an id, it is always in the last child node
|
|
47
|
-
// Sometimes heading is in multiple "parts" (** syntax creates a child
|
|
46
|
+
// Sometimes heading is in multiple "parts" (** syntax creates a child
|
|
47
|
+
// node):
|
|
48
48
|
// ## part1 *part2* part3 {#id}
|
|
49
49
|
const lastNode = headingNode.children[
|
|
50
50
|
headingNode.children.length - 1
|
|
@@ -68,12 +68,6 @@ function headings(): Transformer {
|
|
|
68
68
|
|
|
69
69
|
data.id = id;
|
|
70
70
|
properties.id = id;
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
visit(ast, 'heading', visitor);
|
|
71
|
+
});
|
|
74
72
|
};
|
|
75
|
-
|
|
76
|
-
return transformer;
|
|
77
73
|
}
|
|
78
|
-
|
|
79
|
-
export default headings;
|
package/src/remark/toc/index.ts
CHANGED
|
@@ -9,10 +9,14 @@ import {parse, type ParserOptions} from '@babel/parser';
|
|
|
9
9
|
import type {Identifier} from '@babel/types';
|
|
10
10
|
import traverse from '@babel/traverse';
|
|
11
11
|
import stringifyObject from 'stringify-object';
|
|
12
|
-
import
|
|
13
|
-
import
|
|
12
|
+
import toString from 'mdast-util-to-string';
|
|
13
|
+
import visit from 'unist-util-visit';
|
|
14
|
+
import {toValue} from '../utils';
|
|
15
|
+
|
|
16
|
+
import type {TOCItem} from '@docusaurus/types';
|
|
14
17
|
import type {Node, Parent} from 'unist';
|
|
15
|
-
import type {Literal} from 'mdast';
|
|
18
|
+
import type {Heading, Literal} from 'mdast';
|
|
19
|
+
import type {Transformer} from 'unified';
|
|
16
20
|
|
|
17
21
|
const parseOptions: ParserOptions = {
|
|
18
22
|
plugins: ['jsx'],
|
|
@@ -66,12 +70,27 @@ const getOrCreateExistingTargetIndex = (children: Node[], name: string) => {
|
|
|
66
70
|
return targetIndex;
|
|
67
71
|
};
|
|
68
72
|
|
|
69
|
-
|
|
73
|
+
export default function plugin(options: PluginOptions = {}): Transformer {
|
|
70
74
|
const name = options.name || 'toc';
|
|
71
75
|
|
|
72
|
-
|
|
73
|
-
const headings =
|
|
74
|
-
|
|
76
|
+
return (root) => {
|
|
77
|
+
const headings: TOCItem[] = [];
|
|
78
|
+
|
|
79
|
+
visit(root, 'heading', (child: Heading, _index, parent) => {
|
|
80
|
+
const value = toString(child);
|
|
81
|
+
|
|
82
|
+
// depth:1 headings are titles and not included in the TOC
|
|
83
|
+
if (parent !== root || !value || child.depth < 2) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
headings.push({
|
|
88
|
+
value: toValue(child),
|
|
89
|
+
id: child.data!.id as string,
|
|
90
|
+
level: child.depth,
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
const {children} = root as Parent<Literal>;
|
|
75
94
|
const targetIndex = getOrCreateExistingTargetIndex(children, name);
|
|
76
95
|
|
|
77
96
|
if (headings && headings.length) {
|
|
@@ -80,8 +99,4 @@ const plugin: Plugin<[PluginOptions?]> = (options = {}) => {
|
|
|
80
99
|
)};`;
|
|
81
100
|
}
|
|
82
101
|
};
|
|
83
|
-
|
|
84
|
-
return transformer;
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export default plugin;
|
|
102
|
+
}
|
|
@@ -19,7 +19,7 @@ import fs from 'fs-extra';
|
|
|
19
19
|
import escapeHtml from 'escape-html';
|
|
20
20
|
import sizeOf from 'image-size';
|
|
21
21
|
import {promisify} from 'util';
|
|
22
|
-
import type {
|
|
22
|
+
import type {Transformer} from 'unified';
|
|
23
23
|
import type {Image, Literal} from 'mdast';
|
|
24
24
|
import logger from '@docusaurus/logger';
|
|
25
25
|
|
|
@@ -66,9 +66,9 @@ async function toImageRequireNode(
|
|
|
66
66
|
if (size.height) {
|
|
67
67
|
height = ` height="${size.height}"`;
|
|
68
68
|
}
|
|
69
|
-
} catch (
|
|
69
|
+
} catch (err) {
|
|
70
70
|
logger.error`The image at path=${imagePath} can't be read correctly. Please ensure it's a valid image.
|
|
71
|
-
${(
|
|
71
|
+
${(err as Error).message}`;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
Object.keys(jsxNode).forEach(
|
|
@@ -76,7 +76,7 @@ ${(e as Error).message}`;
|
|
|
76
76
|
);
|
|
77
77
|
|
|
78
78
|
(jsxNode as Literal).type = 'jsx';
|
|
79
|
-
jsxNode.value = `<img ${alt}src={${src}}${title}${width}${height} />`;
|
|
79
|
+
jsxNode.value = `<img loading="lazy" ${alt}src={${src}}${title}${width}${height} />`;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
|
|
@@ -116,14 +116,10 @@ async function getImageAbsolutePath(
|
|
|
116
116
|
}
|
|
117
117
|
return imageFilePath;
|
|
118
118
|
}
|
|
119
|
-
//
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
const imageFilePath = path.join(path.dirname(filePath), imagePath);
|
|
124
|
-
await ensureImageFileExist(imageFilePath, filePath);
|
|
125
|
-
return imageFilePath;
|
|
126
|
-
}
|
|
119
|
+
// relative paths are resolved against the source file's folder
|
|
120
|
+
const imageFilePath = path.join(path.dirname(filePath), imagePath);
|
|
121
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
122
|
+
return imageFilePath;
|
|
127
123
|
}
|
|
128
124
|
|
|
129
125
|
async function processImageNode(node: Image, context: Context) {
|
|
@@ -137,22 +133,22 @@ async function processImageNode(node: Image, context: Context) {
|
|
|
137
133
|
|
|
138
134
|
const parsedUrl = url.parse(node.url);
|
|
139
135
|
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
140
|
-
// pathname:// is an escape hatch,
|
|
141
|
-
//
|
|
142
|
-
// we don't have to document this for now,
|
|
143
|
-
// it's mostly to make next release less risky (2.0.0-alpha.59)
|
|
136
|
+
// pathname:// is an escape hatch, in case user does not want her images to
|
|
137
|
+
// be converted to require calls going through webpack loader
|
|
144
138
|
if (parsedUrl.protocol === 'pathname:') {
|
|
145
139
|
node.url = node.url.replace('pathname://', '');
|
|
146
140
|
}
|
|
147
141
|
return;
|
|
148
142
|
}
|
|
149
143
|
|
|
144
|
+
// We try to convert image urls without protocol to images with require calls
|
|
145
|
+
// going through webpack ensures that image assets exist at build time
|
|
150
146
|
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
|
|
151
147
|
await toImageRequireNode(node, imagePath, context.filePath);
|
|
152
148
|
}
|
|
153
149
|
|
|
154
|
-
|
|
155
|
-
|
|
150
|
+
export default function plugin(options: PluginOptions): Transformer {
|
|
151
|
+
return async (root, vfile) => {
|
|
156
152
|
const promises: Promise<void>[] = [];
|
|
157
153
|
visit(root, 'image', (node: Image) => {
|
|
158
154
|
promises.push(
|
|
@@ -161,7 +157,4 @@ const plugin: Plugin<[PluginOptions]> = (options) => {
|
|
|
161
157
|
});
|
|
162
158
|
await Promise.all(promises);
|
|
163
159
|
};
|
|
164
|
-
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
export default plugin;
|
|
160
|
+
}
|
|
@@ -18,7 +18,7 @@ import url from 'url';
|
|
|
18
18
|
import fs from 'fs-extra';
|
|
19
19
|
import escapeHtml from 'escape-html';
|
|
20
20
|
import {stringifyContent} from '../utils';
|
|
21
|
-
import type {
|
|
21
|
+
import type {Transformer} from 'unified';
|
|
22
22
|
import type {Link, Literal} from 'mdast';
|
|
23
23
|
|
|
24
24
|
const {
|
|
@@ -136,15 +136,12 @@ async function processLinkNode(node: Link, context: Context) {
|
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
|
|
140
|
-
|
|
139
|
+
export default function plugin(options: PluginOptions): Transformer {
|
|
140
|
+
return async (root, vfile) => {
|
|
141
141
|
const promises: Promise<void>[] = [];
|
|
142
142
|
visit(root, 'link', (node: Link) => {
|
|
143
143
|
promises.push(processLinkNode(node, {...options, filePath: vfile.path!}));
|
|
144
144
|
});
|
|
145
145
|
await Promise.all(promises);
|
|
146
146
|
};
|
|
147
|
-
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
export default plugin;
|
|
147
|
+
}
|
|
@@ -10,15 +10,16 @@ import type {Transformer, Processor} from 'unified';
|
|
|
10
10
|
import type {Code, Parent} from 'mdast';
|
|
11
11
|
|
|
12
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
|
|
15
|
-
//
|
|
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
|
|
16
17
|
// See https://github.com/facebook/docusaurus/pull/4278
|
|
17
|
-
function plugin(this: Processor): Transformer {
|
|
18
|
-
|
|
18
|
+
export default function plugin(this: Processor): Transformer {
|
|
19
|
+
return (root) => {
|
|
19
20
|
visit(root, 'code', (node: Code, _index, parent) => {
|
|
20
21
|
if (node.lang === 'mdx-code-block') {
|
|
21
|
-
const newChildren = (this
|
|
22
|
+
const newChildren = (this.parse(node.value) as Parent).children;
|
|
22
23
|
|
|
23
24
|
// Replace the mdx code block by its content, parsed
|
|
24
25
|
parent!.children.splice(
|
|
@@ -29,8 +30,4 @@ function plugin(this: Processor): Transformer {
|
|
|
29
30
|
}
|
|
30
31
|
});
|
|
31
32
|
};
|
|
32
|
-
|
|
33
|
-
return transformer;
|
|
34
33
|
}
|
|
35
|
-
|
|
36
|
-
export default plugin;
|
|
@@ -8,31 +8,29 @@
|
|
|
8
8
|
import escapeHtml from 'escape-html';
|
|
9
9
|
import toString from 'mdast-util-to-string';
|
|
10
10
|
import type {Parent} from 'unist';
|
|
11
|
-
import type {
|
|
11
|
+
import type {PhrasingContent, Heading} from 'mdast';
|
|
12
12
|
|
|
13
13
|
export function stringifyContent(node: Parent): string {
|
|
14
|
-
return (
|
|
15
|
-
.map(toValue)
|
|
16
|
-
.join('');
|
|
14
|
+
return (node.children as PhrasingContent[]).map(toValue).join('');
|
|
17
15
|
}
|
|
18
16
|
|
|
19
|
-
export function toValue(node:
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
17
|
+
export function toValue(node: PhrasingContent | Heading): string {
|
|
18
|
+
switch (node?.type) {
|
|
19
|
+
case 'text':
|
|
20
|
+
return escapeHtml(node.value);
|
|
21
|
+
case 'heading':
|
|
22
|
+
return stringifyContent(node);
|
|
23
|
+
case 'inlineCode':
|
|
24
|
+
return `<code>${escapeHtml(node.value)}</code>`;
|
|
25
|
+
case 'emphasis':
|
|
26
|
+
return `<em>${stringifyContent(node)}</em>`;
|
|
27
|
+
case 'strong':
|
|
28
|
+
return `<strong>${stringifyContent(node)}</strong>`;
|
|
29
|
+
case 'delete':
|
|
30
|
+
return `<del>${stringifyContent(node)}</del>`;
|
|
31
|
+
case 'link':
|
|
32
|
+
return stringifyContent(node);
|
|
33
|
+
default:
|
|
36
34
|
}
|
|
37
35
|
|
|
38
36
|
return toString(node);
|
|
@@ -1,14 +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
|
-
import type { TOCItem } from '@docusaurus/types';
|
|
8
|
-
import type { Node } from 'unist';
|
|
9
|
-
/**
|
|
10
|
-
*
|
|
11
|
-
* Generate a TOC AST from the raw Markdown contents
|
|
12
|
-
*/
|
|
13
|
-
export default function search(node: Node): TOCItem[];
|
|
14
|
-
//# sourceMappingURL=search.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"search.d.ts","sourceRoot":"","sources":["../../../src/remark/toc/search.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAKH,OAAO,KAAK,EAAC,OAAO,EAAC,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,OAAO,CAAC;AAUhC;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,EAAE,CAsDpD"}
|
package/lib/remark/toc/search.js
DELETED
|
@@ -1,66 +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 mdast_util_to_string_1 = (0, tslib_1.__importDefault)(require("mdast-util-to-string"));
|
|
11
|
-
const unist_util_visit_1 = (0, tslib_1.__importDefault)(require("unist-util-visit"));
|
|
12
|
-
const utils_1 = require("../utils");
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* Generate a TOC AST from the raw Markdown contents
|
|
16
|
-
*/
|
|
17
|
-
function search(node) {
|
|
18
|
-
const headings = [];
|
|
19
|
-
(0, unist_util_visit_1.default)(node, 'heading', (child, _index, parent) => {
|
|
20
|
-
const value = (0, mdast_util_to_string_1.default)(child);
|
|
21
|
-
// depth:1 headings are titles and not included in the TOC
|
|
22
|
-
if (parent !== node || !value || child.depth < 2) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
headings.push({
|
|
26
|
-
node: {
|
|
27
|
-
value: (0, utils_1.toValue)(child),
|
|
28
|
-
id: child.data.id,
|
|
29
|
-
children: [],
|
|
30
|
-
level: child.depth,
|
|
31
|
-
},
|
|
32
|
-
level: child.depth,
|
|
33
|
-
parentIndex: -1,
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
// Keep track of which previous index would be the current heading's direct parent.
|
|
37
|
-
// Each entry <i> is the last index of the `headings` array at heading level <i>.
|
|
38
|
-
// We will modify these indices as we iterate through all headings.
|
|
39
|
-
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
40
|
-
// indices 0 and 1 will remain unused.
|
|
41
|
-
const prevIndexForLevel = Array(7).fill(-1);
|
|
42
|
-
headings.forEach((curr, currIndex) => {
|
|
43
|
-
// take the last seen index for each ancestor level. the highest
|
|
44
|
-
// index will be the direct ancestor of the current heading.
|
|
45
|
-
const ancestorLevelIndexes = prevIndexForLevel.slice(2, curr.level);
|
|
46
|
-
curr.parentIndex = Math.max(...ancestorLevelIndexes);
|
|
47
|
-
// mark that curr.level was last seen at the current index
|
|
48
|
-
prevIndexForLevel[curr.level] = currIndex;
|
|
49
|
-
});
|
|
50
|
-
const rootNodeIndexes = [];
|
|
51
|
-
// For a given parentIndex, add each Node into that parent's `children` array
|
|
52
|
-
headings.forEach((heading, i) => {
|
|
53
|
-
if (heading.parentIndex >= 0) {
|
|
54
|
-
headings[heading.parentIndex].node.children.push(heading.node);
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
rootNodeIndexes.push(i);
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
const toc = headings
|
|
61
|
-
.filter((_, k) => rootNodeIndexes.includes(k)) // only return root nodes
|
|
62
|
-
.map((heading) => heading.node); // only return Node, no metadata
|
|
63
|
-
return toc;
|
|
64
|
-
}
|
|
65
|
-
exports.default = search;
|
|
66
|
-
//# sourceMappingURL=search.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"search.js","sourceRoot":"","sources":["../../../src/remark/toc/search.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6FAA4C;AAC5C,qFAAqC;AACrC,oCAAiC;AAYjC;;;GAGG;AACH,SAAwB,MAAM,CAAC,IAAU;IACvC,MAAM,QAAQ,GAAiB,EAAE,CAAC;IAElC,IAAA,0BAAK,EAAC,IAAI,EAAE,SAAS,EAAE,CAAC,KAAc,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE;QACxD,MAAM,KAAK,GAAG,IAAA,8BAAQ,EAAC,KAAK,CAAC,CAAC;QAE9B,0DAA0D;QAC1D,IAAI,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE;YAChD,OAAO;SACR;QAED,QAAQ,CAAC,IAAI,CAAC;YACZ,IAAI,EAAE;gBACJ,KAAK,EAAE,IAAA,eAAO,EAAC,KAAK,CAAC;gBACrB,EAAE,EAAE,KAAK,CAAC,IAAK,CAAC,EAAY;gBAC5B,QAAQ,EAAE,EAAE;gBACZ,KAA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AO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,gCAAgC;IACnE,OAAO,GAAG,CAAC;AACb,CAAC;AAtDD,yBAsDC"}
|
package/src/remark/toc/search.ts
DELETED
|
@@ -1,80 +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 toString from 'mdast-util-to-string';
|
|
9
|
-
import visit from 'unist-util-visit';
|
|
10
|
-
import {toValue} from '../utils';
|
|
11
|
-
import type {TOCItem} from '@docusaurus/types';
|
|
12
|
-
import type {Node} from 'unist';
|
|
13
|
-
import type {Heading} from 'mdast';
|
|
14
|
-
|
|
15
|
-
// Intermediate interface for TOC algorithm
|
|
16
|
-
interface SearchItem {
|
|
17
|
-
node: TOCItem;
|
|
18
|
-
level: number;
|
|
19
|
-
parentIndex: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* Generate a TOC AST from the raw Markdown contents
|
|
25
|
-
*/
|
|
26
|
-
export default function search(node: Node): TOCItem[] {
|
|
27
|
-
const headings: SearchItem[] = [];
|
|
28
|
-
|
|
29
|
-
visit(node, 'heading', (child: Heading, _index, parent) => {
|
|
30
|
-
const value = toString(child);
|
|
31
|
-
|
|
32
|
-
// depth:1 headings are titles and not included in the TOC
|
|
33
|
-
if (parent !== node || !value || child.depth < 2) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
headings.push({
|
|
38
|
-
node: {
|
|
39
|
-
value: toValue(child),
|
|
40
|
-
id: child.data!.id as string,
|
|
41
|
-
children: [],
|
|
42
|
-
level: child.depth,
|
|
43
|
-
},
|
|
44
|
-
level: child.depth,
|
|
45
|
-
parentIndex: -1,
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
// Keep track of which previous index would be the current heading's direct parent.
|
|
50
|
-
// Each entry <i> is the last index of the `headings` array at heading level <i>.
|
|
51
|
-
// We will modify these indices as we iterate through all headings.
|
|
52
|
-
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
53
|
-
// indices 0 and 1 will remain unused.
|
|
54
|
-
const prevIndexForLevel = Array(7).fill(-1);
|
|
55
|
-
|
|
56
|
-
headings.forEach((curr, currIndex) => {
|
|
57
|
-
// take the last seen index for each ancestor level. the highest
|
|
58
|
-
// index will be the direct ancestor of the current heading.
|
|
59
|
-
const ancestorLevelIndexes = prevIndexForLevel.slice(2, curr.level);
|
|
60
|
-
curr.parentIndex = Math.max(...ancestorLevelIndexes);
|
|
61
|
-
// mark that curr.level was last seen at the current index
|
|
62
|
-
prevIndexForLevel[curr.level] = currIndex;
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const rootNodeIndexes: number[] = [];
|
|
66
|
-
|
|
67
|
-
// For a given parentIndex, add each Node into that parent's `children` array
|
|
68
|
-
headings.forEach((heading, i) => {
|
|
69
|
-
if (heading.parentIndex >= 0) {
|
|
70
|
-
headings[heading.parentIndex].node.children.push(heading.node);
|
|
71
|
-
} else {
|
|
72
|
-
rootNodeIndexes.push(i);
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const toc = headings
|
|
77
|
-
.filter((_, k) => rootNodeIndexes.includes(k)) // only return root nodes
|
|
78
|
-
.map((heading) => heading.node); // only return Node, no metadata
|
|
79
|
-
return toc;
|
|
80
|
-
}
|