@docusaurus/mdx-loader 2.0.0-beta.14 → 2.0.0-beta.15
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 +8 -3
- package/lib/index.js.map +1 -1
- package/lib/remark/headings/index.js.map +1 -1
- package/lib/remark/toc/index.js.map +1 -1
- package/lib/remark/toc/search.d.ts.map +1 -1
- package/lib/remark/toc/search.js +3 -4
- package/lib/remark/toc/search.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 +62 -42
- package/lib/remark/transformImage/index.js.map +1 -1
- package/lib/remark/transformLinks/index.d.ts +2 -3
- package/lib/remark/transformLinks/index.d.ts.map +1 -1
- package/lib/remark/transformLinks/index.js +51 -55
- package/lib/remark/transformLinks/index.js.map +1 -1
- package/package.json +6 -7
- package/src/index.ts +8 -3
- package/src/remark/headings/index.ts +1 -1
- package/src/remark/toc/index.ts +1 -1
- package/src/remark/toc/search.ts +4 -6
- package/src/remark/transformImage/index.ts +101 -68
- package/src/remark/transformLinks/index.ts +76 -91
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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,EAAE,OAAO,CAAC,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAwDF,wBAA8B,SAAS,CACrC,IAAI,EAAE,aAAa,CAAC,OAAO,CAAC,EAC5B,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,IAAI,CAAC,CAgHf"}
|
package/lib/index.js
CHANGED
|
@@ -84,12 +84,17 @@ async function mdxLoader(fileString) {
|
|
|
84
84
|
remarkPlugins: [
|
|
85
85
|
...(reqOptions.beforeDefaultRemarkPlugins || []),
|
|
86
86
|
...DEFAULT_OPTIONS.remarkPlugins,
|
|
87
|
-
[
|
|
87
|
+
[
|
|
88
|
+
transformImage_1.default,
|
|
89
|
+
{
|
|
90
|
+
staticDirs: reqOptions.staticDirs,
|
|
91
|
+
siteDir: reqOptions.siteDir,
|
|
92
|
+
},
|
|
93
|
+
],
|
|
88
94
|
[
|
|
89
95
|
transformLinks_1.default,
|
|
90
96
|
{
|
|
91
97
|
staticDirs: reqOptions.staticDirs,
|
|
92
|
-
filePath,
|
|
93
98
|
siteDir: reqOptions.siteDir,
|
|
94
99
|
},
|
|
95
100
|
],
|
|
@@ -110,7 +115,7 @@ async function mdxLoader(fileString) {
|
|
|
110
115
|
return callback(err);
|
|
111
116
|
}
|
|
112
117
|
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
113
|
-
// Partial are not expected to have an associated metadata file or
|
|
118
|
+
// Partial are not expected to have an associated metadata file or front matter
|
|
114
119
|
const isMDXPartial = options.isMDXPartial && options.isMDXPartial(filePath);
|
|
115
120
|
if (isMDXPartial && hasFrontMatter) {
|
|
116
121
|
const errorMessage = `Docusaurus MDX partial files should not contain FrontMatter.
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,uCAAkC;AAClC,mEAA8B;AAC9B,6EAAwC;AACxC,6EAAiC;AACj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AC,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;AAnHD,4BAmHC"}
|
|
@@ -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,qFAAqD;AACrD,6FAA4C;AAK5C,SAAS,QAAQ;IACf,MAAM,WAAW,GAAgB,CAAC,GAAG,EAAE,EAAE;QACvC,MAAM,KAAK,GAAG,IAAA,qBAAa,GAAE,CAAC;QAE9B,MAAM,OAAO,GAAqB,CAAC,WAAW,EAAE,EAAE;YAChD,MAAM,IAAI,GAAG,WAAW,CAAC,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;YACzD,MAAM,UAAU,GAAG,CAAC,IAAI,CAAC,WAAW,IAAI,CAAC,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC,CAE9D,CAAC;YACF,I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M;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;QAEF,IAAA,0BAAK,EAAC,GAAG,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;IACjC,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC;AAED,kBAAe,QAAQ,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/toc/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,
|
|
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,mEAA8B;AAK9B,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,IA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gBAAM,EAAC,IAAI,CAAC,CAAC;QAC9B,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;IAEF,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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
CHANGED
|
@@ -16,7 +16,7 @@ const utils_1 = require("../utils");
|
|
|
16
16
|
*/
|
|
17
17
|
function search(node) {
|
|
18
18
|
const headings = [];
|
|
19
|
-
|
|
19
|
+
(0, unist_util_visit_1.default)(node, 'heading', (child, _index, parent) => {
|
|
20
20
|
const value = (0, mdast_util_to_string_1.default)(child);
|
|
21
21
|
// depth:1 headings are titles and not included in the TOC
|
|
22
22
|
if (parent !== node || !value || child.depth < 2) {
|
|
@@ -32,9 +32,8 @@ function search(node) {
|
|
|
32
32
|
level: child.depth,
|
|
33
33
|
parentIndex: -1,
|
|
34
34
|
});
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
// Keep track of which previous index would be the current heading's direcy parent.
|
|
35
|
+
});
|
|
36
|
+
// Keep track of which previous index would be the current heading's direct parent.
|
|
38
37
|
// Each entry <i> is the last index of the `headings` array at heading level <i>.
|
|
39
38
|
// We will modify these indices as we iterate through all headings.
|
|
40
39
|
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"search.js","sourceRoot":"","sources":["../../../src/remark/toc/search.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6FAA4C;AAC5C,
|
|
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;gB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,eAAe,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,yBAAyB;SACvE,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,gCAAgC;IACnE,OAAO,GAAG,CAAC;AACb,CAAC;AAtDD,yBAsDC"}
|
|
@@ -5,10 +5,10 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
import type { Plugin } from 'unified';
|
|
8
|
-
|
|
9
|
-
filePath: string;
|
|
8
|
+
declare type PluginOptions = {
|
|
10
9
|
staticDirs: string[];
|
|
11
|
-
|
|
10
|
+
siteDir: string;
|
|
11
|
+
};
|
|
12
12
|
declare const plugin: Plugin<[PluginOptions]>;
|
|
13
13
|
export default plugin;
|
|
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;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAgBH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AAQjD,aAAK,aAAa,GAAG;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAyHF,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CAWnC,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -7,52 +7,84 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
|
+
const utils_1 = require("@docusaurus/utils");
|
|
10
11
|
const unist_util_visit_1 = (0, tslib_1.__importDefault)(require("unist-util-visit"));
|
|
11
12
|
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
12
13
|
const url_1 = (0, tslib_1.__importDefault)(require("url"));
|
|
13
14
|
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
14
15
|
const escape_html_1 = (0, tslib_1.__importDefault)(require("escape-html"));
|
|
15
|
-
const
|
|
16
|
+
const image_size_1 = (0, tslib_1.__importDefault)(require("image-size"));
|
|
17
|
+
const util_1 = require("util");
|
|
18
|
+
const logger_1 = (0, tslib_1.__importDefault)(require("@docusaurus/logger"));
|
|
16
19
|
const { loaders: { inlineMarkdownImageFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
|
|
17
|
-
|
|
20
|
+
async function toImageRequireNode(node, imagePath, filePath) {
|
|
21
|
+
var _a, _b;
|
|
18
22
|
const jsxNode = node;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
let relativeImagePath = (0, utils_1.posixPath)(path_1.default.relative(path_1.default.dirname(filePath), imagePath));
|
|
24
|
+
relativeImagePath = `./${relativeImagePath}`;
|
|
25
|
+
const parsedUrl = url_1.default.parse(node.url);
|
|
26
|
+
const hash = (_a = parsedUrl.hash) !== null && _a !== void 0 ? _a : '';
|
|
27
|
+
const search = (_b = parsedUrl.search) !== null && _b !== void 0 ? _b : '';
|
|
28
|
+
const alt = node.alt ? `alt={"${(0, escape_html_1.default)(node.alt)}"} ` : '';
|
|
29
|
+
const src = `require("${inlineMarkdownImageFileLoader}${(0, utils_1.escapePath)(relativeImagePath) + search}").default${hash ? ` + '${hash}'` : ''}`;
|
|
30
|
+
const title = node.title ? ` title="${(0, escape_html_1.default)(node.title)}"` : '';
|
|
31
|
+
let width = '';
|
|
32
|
+
let height = '';
|
|
33
|
+
try {
|
|
34
|
+
const size = (await (0, util_1.promisify)(image_size_1.default)(imagePath));
|
|
35
|
+
if (size.width) {
|
|
36
|
+
width = ` width="${size.width}"`;
|
|
37
|
+
}
|
|
38
|
+
if (size.height) {
|
|
39
|
+
height = ` height="${size.height}"`;
|
|
40
|
+
}
|
|
28
41
|
}
|
|
29
|
-
|
|
30
|
-
|
|
42
|
+
catch (e) {
|
|
43
|
+
logger_1.default.error `The image at path=${imagePath} can't be read correctly. Please ensure it's a valid image.
|
|
44
|
+
${e.message}`;
|
|
31
45
|
}
|
|
32
|
-
|
|
46
|
+
Object.keys(jsxNode).forEach((key) => delete jsxNode[key]);
|
|
47
|
+
jsxNode.type = 'jsx';
|
|
48
|
+
jsxNode.value = `<img ${alt}src={${src}}${title}${width}${height} />`;
|
|
49
|
+
}
|
|
33
50
|
async function ensureImageFileExist(imagePath, sourceFilePath) {
|
|
34
51
|
const imageExists = await fs_extra_1.default.pathExists(imagePath);
|
|
35
52
|
if (!imageExists) {
|
|
36
53
|
throw new Error(`Image ${(0, utils_1.toMessageRelativeFilePath)(imagePath)} used in ${(0, utils_1.toMessageRelativeFilePath)(sourceFilePath)} not found.`);
|
|
37
54
|
}
|
|
38
55
|
}
|
|
39
|
-
async function
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
56
|
+
async function getImageAbsolutePath(imagePath, { siteDir, filePath, staticDirs }) {
|
|
57
|
+
if (imagePath.startsWith('@site/')) {
|
|
58
|
+
const imageFilePath = path_1.default.join(siteDir, imagePath.replace('@site/', ''));
|
|
59
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
60
|
+
return imageFilePath;
|
|
61
|
+
}
|
|
62
|
+
else if (path_1.default.isAbsolute(imagePath)) {
|
|
63
|
+
// absolute paths are expected to exist in the static folder
|
|
64
|
+
const possiblePaths = staticDirs.map((dir) => path_1.default.join(dir, imagePath));
|
|
65
|
+
const imageFilePath = await (0, utils_1.findAsyncSequential)(possiblePaths, fs_extra_1.default.pathExists);
|
|
66
|
+
if (!imageFilePath) {
|
|
67
|
+
throw new Error(`Image ${possiblePaths
|
|
68
|
+
.map((p) => (0, utils_1.toMessageRelativeFilePath)(p))
|
|
69
|
+
.join(' or ')} used in ${(0, utils_1.toMessageRelativeFilePath)(filePath)} not found.`);
|
|
44
70
|
}
|
|
71
|
+
return imageFilePath;
|
|
72
|
+
}
|
|
73
|
+
// We try to convert image urls without protocol to images with require calls
|
|
74
|
+
// going through webpack ensures that image assets exist at build time
|
|
75
|
+
else {
|
|
76
|
+
// relative paths are resolved against the source file's folder
|
|
77
|
+
const imageFilePath = path_1.default.join(path_1.default.dirname(filePath), imagePath);
|
|
78
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
79
|
+
return imageFilePath;
|
|
45
80
|
}
|
|
46
|
-
throw new Error(`Image ${possiblePaths
|
|
47
|
-
.map((p) => (0, utils_1.toMessageRelativeFilePath)(p))
|
|
48
|
-
.join(' or ')} used in ${(0, utils_1.toMessageRelativeFilePath)(sourceFilePath)} not found.`);
|
|
49
81
|
}
|
|
50
|
-
async function processImageNode(node,
|
|
82
|
+
async function processImageNode(node, context) {
|
|
51
83
|
if (!node.url) {
|
|
52
|
-
throw new Error(`Markdown image URL is mandatory in "${(0, utils_1.toMessageRelativeFilePath)(filePath)}" file`);
|
|
84
|
+
throw new Error(`Markdown image URL is mandatory in "${(0, utils_1.toMessageRelativeFilePath)(context.filePath)}" file`);
|
|
53
85
|
}
|
|
54
86
|
const parsedUrl = url_1.default.parse(node.url);
|
|
55
|
-
if (parsedUrl.protocol) {
|
|
87
|
+
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
56
88
|
// pathname:// is an escape hatch,
|
|
57
89
|
// in case user does not want his images to be converted to require calls going through webpack loader
|
|
58
90
|
// we don't have to document this for now,
|
|
@@ -60,28 +92,16 @@ async function processImageNode(node, { filePath, staticDirs }) {
|
|
|
60
92
|
if (parsedUrl.protocol === 'pathname:') {
|
|
61
93
|
node.url = node.url.replace('pathname://', '');
|
|
62
94
|
}
|
|
95
|
+
return;
|
|
63
96
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
// absolute paths are expected to exist in the static folder
|
|
67
|
-
const possibleImagePaths = staticDirs.map((dir) => path_1.default.join(dir, node.url));
|
|
68
|
-
const imagePath = await findImage(possibleImagePaths, filePath);
|
|
69
|
-
createJSX(node, (0, utils_1.posixPath)(imagePath));
|
|
70
|
-
}
|
|
71
|
-
// We try to convert image urls without protocol to images with require calls
|
|
72
|
-
// going through webpack ensures that image assets exist at build time
|
|
73
|
-
else {
|
|
74
|
-
// relative paths are resolved against the source file's folder
|
|
75
|
-
const expectedImagePath = path_1.default.join(path_1.default.dirname(filePath), node.url);
|
|
76
|
-
await ensureImageFileExist(expectedImagePath, filePath);
|
|
77
|
-
createJSX(node, node.url.startsWith('./') ? node.url : `./${node.url}`);
|
|
78
|
-
}
|
|
97
|
+
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
|
|
98
|
+
await toImageRequireNode(node, imagePath, context.filePath);
|
|
79
99
|
}
|
|
80
100
|
const plugin = (options) => {
|
|
81
|
-
const transformer = async (root) => {
|
|
101
|
+
const transformer = async (root, vfile) => {
|
|
82
102
|
const promises = [];
|
|
83
103
|
(0, unist_util_visit_1.default)(root, 'image', (node) => {
|
|
84
|
-
promises.push(processImageNode(node, options));
|
|
104
|
+
promises.push(processImageNode(node, { ...options, filePath: vfile.path }));
|
|
85
105
|
});
|
|
86
106
|
await Promise.all(promises);
|
|
87
107
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,qFAAqC;AACrC,6DAAwB;AACxB,2DAAsB;AACtB,qEAA0B;AAC1B,2EAAqC;AACrC,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6CAM2B;AAC3B,qFAAqC;AACrC,6DAAwB;AACxB,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I,SAAS,CAAC,QAAQ,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE;QAC7C,kCAAkC;QAClC,sGAAsG;QACtG,0CAA0C;QAC1C,+DAA+D;QAC/D,IAAI,SAAS,CAAC,QAAQ,KAAK,WAAW,EAAE;YACtC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,aAAa,EAAE,EAAE,CAAC,CAAC;SAChD;QACD,OAAO;KACR;IAED,MAAM,SAAS,GAAG,MAAM,oBAAoB,CAAC,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC1E,MAAM,kBAAkB,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;AAC9D,CAAC;AAED,MAAM,MAAM,GAA4B,CAAC,OAAO,EAAE,EAAE;IAClD,MAAM,WAAW,GAAgB,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;QACrD,MAAM,QAAQ,GAAoB,EAAE,CAAC;QACrC,IAAA,0BAAK,EAAC,IAAI,EAAE,OAAO,EAAE,CAAC,IAAW,EAAE,EAAE;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;IACF,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
|
@@ -5,11 +5,10 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
import type { Plugin } from 'unified';
|
|
8
|
-
|
|
9
|
-
filePath: string;
|
|
8
|
+
declare type PluginOptions = {
|
|
10
9
|
staticDirs: string[];
|
|
11
10
|
siteDir: string;
|
|
12
|
-
}
|
|
11
|
+
};
|
|
13
12
|
declare const plugin: Plugin<[PluginOptions]>;
|
|
14
13
|
export default plugin;
|
|
15
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;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AAOjD,aAAK,aAAa,GAAG;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AA4GF,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CASnC,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -15,89 +15,85 @@ const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
|
15
15
|
const escape_html_1 = (0, tslib_1.__importDefault)(require("escape-html"));
|
|
16
16
|
const utils_2 = require("../utils");
|
|
17
17
|
const { loaders: { inlineMarkdownLinkFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
|
|
18
|
-
const hashRegex = /#.*$/;
|
|
19
|
-
async function ensureAssetFileExist(fileSystemAssetPath, sourceFilePath) {
|
|
20
|
-
const assetExists = await fs_extra_1.default.pathExists(fileSystemAssetPath);
|
|
21
|
-
if (!assetExists) {
|
|
22
|
-
throw new Error(`Asset ${(0, utils_1.toMessageRelativeFilePath)(fileSystemAssetPath)} used in ${(0, utils_1.toMessageRelativeFilePath)(sourceFilePath)} not found.`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
18
|
// transform the link node to a jsx link with a require() call
|
|
26
|
-
function toAssetRequireNode(
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const href = `require('${
|
|
19
|
+
function toAssetRequireNode(node, assetPath, filePath) {
|
|
20
|
+
var _a, _b;
|
|
21
|
+
const jsxNode = node;
|
|
22
|
+
let relativeAssetPath = (0, utils_1.posixPath)(path_1.default.relative(path_1.default.dirname(filePath), assetPath));
|
|
23
|
+
// require("assets/file.pdf") means requiring from a package called assets
|
|
24
|
+
relativeAssetPath = `./${relativeAssetPath}`;
|
|
25
|
+
const parsedUrl = url_1.default.parse(node.url);
|
|
26
|
+
const hash = (_a = parsedUrl.hash) !== null && _a !== void 0 ? _a : '';
|
|
27
|
+
const search = (_b = parsedUrl.search) !== null && _b !== void 0 ? _b : '';
|
|
28
|
+
const href = `require('${
|
|
29
|
+
// A hack to stop Webpack from using its built-in loader to parse JSON
|
|
30
|
+
path_1.default.extname(relativeAssetPath) === '.json'
|
|
31
|
+
? `${relativeAssetPath.replace('.json', '.raw')}!=`
|
|
32
|
+
: ''}${inlineMarkdownLinkFileLoader}${(0, utils_1.escapePath)(relativeAssetPath) + search}').default${hash ? ` + '${hash}'` : ''}`;
|
|
36
33
|
const children = (0, utils_2.stringifyContent)(node);
|
|
37
34
|
const title = node.title ? ` title="${(0, escape_html_1.default)(node.title)}"` : '';
|
|
38
|
-
|
|
39
|
-
|
|
35
|
+
Object.keys(jsxNode).forEach((key) => delete jsxNode[key]);
|
|
36
|
+
jsxNode.type = 'jsx';
|
|
37
|
+
jsxNode.value = `<a target="_blank" href={${href}}${title}>${children}</a>`;
|
|
40
38
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const assetPath = node.url.replace(hashRegex, '');
|
|
46
|
-
const hasSiteAlias = assetPath.startsWith('@site/');
|
|
47
|
-
const hasAssetLikeExtension = path_1.default.extname(assetPath) && !assetPath.match(/#|.md|.mdx|.html/);
|
|
48
|
-
const looksLikeAssetLink = hasSiteAlias || hasAssetLikeExtension;
|
|
49
|
-
if (!looksLikeAssetLink) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
function toAssetLinkNode(requireAssetPath) {
|
|
53
|
-
toAssetRequireNode({
|
|
54
|
-
node,
|
|
55
|
-
filePath,
|
|
56
|
-
requireAssetPath,
|
|
57
|
-
});
|
|
39
|
+
async function ensureAssetFileExist(assetPath, sourceFilePath) {
|
|
40
|
+
const assetExists = await fs_extra_1.default.pathExists(assetPath);
|
|
41
|
+
if (!assetExists) {
|
|
42
|
+
throw new Error(`Asset ${(0, utils_1.toMessageRelativeFilePath)(assetPath)} used in ${(0, utils_1.toMessageRelativeFilePath)(sourceFilePath)} not found.`);
|
|
58
43
|
}
|
|
44
|
+
}
|
|
45
|
+
async function getAssetAbsolutePath(assetPath, { siteDir, filePath, staticDirs }) {
|
|
59
46
|
if (assetPath.startsWith('@site/')) {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
47
|
+
const assetFilePath = path_1.default.join(siteDir, assetPath.replace('@site/', ''));
|
|
48
|
+
// The @site alias is the only way to believe that the user wants an asset.
|
|
49
|
+
// Everything else can just be a link URL
|
|
50
|
+
await ensureAssetFileExist(assetFilePath, filePath);
|
|
51
|
+
return assetFilePath;
|
|
63
52
|
}
|
|
64
53
|
else if (path_1.default.isAbsolute(assetPath)) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (await fs_extra_1.default.pathExists(fileSystemAssetPath)) {
|
|
69
|
-
toAssetLinkNode(fileSystemAssetPath);
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
54
|
+
const assetFilePath = await (0, utils_1.findAsyncSequential)(staticDirs.map((dir) => path_1.default.join(dir, assetPath)), fs_extra_1.default.pathExists);
|
|
55
|
+
if (assetFilePath) {
|
|
56
|
+
return assetFilePath;
|
|
72
57
|
}
|
|
73
58
|
}
|
|
74
59
|
else {
|
|
75
|
-
const
|
|
76
|
-
if (await fs_extra_1.default.pathExists(
|
|
77
|
-
|
|
60
|
+
const assetFilePath = path_1.default.join(path_1.default.dirname(filePath), assetPath);
|
|
61
|
+
if (await fs_extra_1.default.pathExists(assetFilePath)) {
|
|
62
|
+
return assetFilePath;
|
|
78
63
|
}
|
|
79
64
|
}
|
|
65
|
+
return null;
|
|
80
66
|
}
|
|
81
|
-
async function processLinkNode(node,
|
|
67
|
+
async function processLinkNode(node, context) {
|
|
82
68
|
var _a, _b, _c;
|
|
83
69
|
if (!node.url) {
|
|
84
70
|
// try to improve error feedback
|
|
85
71
|
// see https://github.com/facebook/docusaurus/issues/3309#issuecomment-690371675
|
|
86
72
|
const title = node.title || ((_a = node.children[0]) === null || _a === void 0 ? void 0 : _a.value) || '?';
|
|
87
73
|
const line = ((_c = (_b = node === null || node === void 0 ? void 0 : node.position) === null || _b === void 0 ? void 0 : _b.start) === null || _c === void 0 ? void 0 : _c.line) || '?';
|
|
88
|
-
throw new Error(`Markdown link URL is mandatory in "${(0, utils_1.toMessageRelativeFilePath)(
|
|
74
|
+
throw new Error(`Markdown link URL is mandatory in "${(0, utils_1.toMessageRelativeFilePath)(context.filePath)}" file (title: ${title}, line: ${line}).`);
|
|
89
75
|
}
|
|
90
76
|
const parsedUrl = url_1.default.parse(node.url);
|
|
91
|
-
if (parsedUrl.protocol) {
|
|
77
|
+
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
78
|
+
// Don't process pathname:// here, it's used by the <Link> component
|
|
92
79
|
return;
|
|
93
80
|
}
|
|
94
|
-
|
|
81
|
+
const hasSiteAlias = parsedUrl.pathname.startsWith('@site/');
|
|
82
|
+
const hasAssetLikeExtension = path_1.default.extname(parsedUrl.pathname) &&
|
|
83
|
+
!parsedUrl.pathname.match(/\.(?:mdx?|html)(?:#|$)/);
|
|
84
|
+
if (!hasSiteAlias && !hasAssetLikeExtension) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const assetPath = await getAssetAbsolutePath(parsedUrl.pathname, context);
|
|
88
|
+
if (assetPath) {
|
|
89
|
+
toAssetRequireNode(node, assetPath, context.filePath);
|
|
90
|
+
}
|
|
95
91
|
}
|
|
96
92
|
const plugin = (options) => {
|
|
97
|
-
const transformer = async (root) => {
|
|
93
|
+
const transformer = async (root, vfile) => {
|
|
98
94
|
const promises = [];
|
|
99
95
|
(0, unist_util_visit_1.default)(root, 'link', (node) => {
|
|
100
|
-
promises.push(processLinkNode(node, options));
|
|
96
|
+
promises.push(processLinkNode(node, { ...options, filePath: vfile.path }));
|
|
101
97
|
});
|
|
102
98
|
await Promise.all(promises);
|
|
103
99
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,
|
|
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,EAA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AAC;QAC5E,CAAC,CAAC,CAAC;QACH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC,CAAC;IACF,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC;AAEF,kBAAe,MAAM,CAAC"}
|
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.15",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "src/mdx-loader.d.ts",
|
|
@@ -20,14 +20,13 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@babel/parser": "^7.16.4",
|
|
22
22
|
"@babel/traverse": "^7.16.3",
|
|
23
|
-
"@docusaurus/logger": "2.0.0-beta.
|
|
24
|
-
"@docusaurus/utils": "2.0.0-beta.
|
|
23
|
+
"@docusaurus/logger": "2.0.0-beta.15",
|
|
24
|
+
"@docusaurus/utils": "2.0.0-beta.15",
|
|
25
25
|
"@mdx-js/mdx": "^1.6.21",
|
|
26
|
-
"@mdx-js/react": "^1.6.21",
|
|
27
26
|
"escape-html": "^1.0.3",
|
|
28
27
|
"file-loader": "^6.2.0",
|
|
29
28
|
"fs-extra": "^10.0.0",
|
|
30
|
-
"
|
|
29
|
+
"image-size": "^1.0.1",
|
|
31
30
|
"mdast-util-to-string": "^2.0.0",
|
|
32
31
|
"remark-emoji": "^2.1.0",
|
|
33
32
|
"stringify-object": "^3.3.0",
|
|
@@ -37,7 +36,7 @@
|
|
|
37
36
|
"webpack": "^5.61.0"
|
|
38
37
|
},
|
|
39
38
|
"devDependencies": {
|
|
40
|
-
"@docusaurus/types": "2.0.0-beta.
|
|
39
|
+
"@docusaurus/types": "2.0.0-beta.15",
|
|
41
40
|
"@types/escape-html": "^1.0.1",
|
|
42
41
|
"@types/mdast": "^3.0.7",
|
|
43
42
|
"@types/stringify-object": "^3.3.1",
|
|
@@ -55,5 +54,5 @@
|
|
|
55
54
|
"engines": {
|
|
56
55
|
"node": ">=14"
|
|
57
56
|
},
|
|
58
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "32ec84ef3c0a238436e913b2026ab809e5750fa8"
|
|
59
58
|
}
|
package/src/index.ts
CHANGED
|
@@ -124,12 +124,17 @@ export default async function mdxLoader(
|
|
|
124
124
|
remarkPlugins: [
|
|
125
125
|
...(reqOptions.beforeDefaultRemarkPlugins || []),
|
|
126
126
|
...DEFAULT_OPTIONS.remarkPlugins,
|
|
127
|
-
[
|
|
127
|
+
[
|
|
128
|
+
transformImage,
|
|
129
|
+
{
|
|
130
|
+
staticDirs: reqOptions.staticDirs,
|
|
131
|
+
siteDir: reqOptions.siteDir,
|
|
132
|
+
},
|
|
133
|
+
],
|
|
128
134
|
[
|
|
129
135
|
transformLinks,
|
|
130
136
|
{
|
|
131
137
|
staticDirs: reqOptions.staticDirs,
|
|
132
|
-
filePath,
|
|
133
138
|
siteDir: reqOptions.siteDir,
|
|
134
139
|
},
|
|
135
140
|
],
|
|
@@ -151,7 +156,7 @@ export default async function mdxLoader(
|
|
|
151
156
|
}
|
|
152
157
|
|
|
153
158
|
// MDX partials are MDX files starting with _ or in a folder starting with _
|
|
154
|
-
// Partial are not expected to have an associated metadata file or
|
|
159
|
+
// Partial are not expected to have an associated metadata file or front matter
|
|
155
160
|
const isMDXPartial = options.isMDXPartial && options.isMDXPartial(filePath);
|
|
156
161
|
if (isMDXPartial && hasFrontMatter) {
|
|
157
162
|
const errorMessage = `Docusaurus MDX partial files should not contain FrontMatter.
|
|
@@ -8,7 +8,7 @@
|
|
|
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, {Visitor} from 'unist-util-visit';
|
|
11
|
+
import visit, {type Visitor} 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';
|
package/src/remark/toc/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 {parse, ParserOptions} from '@babel/parser';
|
|
8
|
+
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';
|
package/src/remark/toc/search.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
import toString from 'mdast-util-to-string';
|
|
9
|
-
import visit
|
|
9
|
+
import visit from 'unist-util-visit';
|
|
10
10
|
import {toValue} from '../utils';
|
|
11
11
|
import type {TOCItem} from '@docusaurus/types';
|
|
12
12
|
import type {Node} from 'unist';
|
|
@@ -26,7 +26,7 @@ interface SearchItem {
|
|
|
26
26
|
export default function search(node: Node): TOCItem[] {
|
|
27
27
|
const headings: SearchItem[] = [];
|
|
28
28
|
|
|
29
|
-
|
|
29
|
+
visit(node, 'heading', (child: Heading, _index, parent) => {
|
|
30
30
|
const value = toString(child);
|
|
31
31
|
|
|
32
32
|
// depth:1 headings are titles and not included in the TOC
|
|
@@ -44,11 +44,9 @@ export default function search(node: Node): TOCItem[] {
|
|
|
44
44
|
level: child.depth,
|
|
45
45
|
parentIndex: -1,
|
|
46
46
|
});
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
visit(node, 'heading', visitor);
|
|
47
|
+
});
|
|
50
48
|
|
|
51
|
-
// Keep track of which previous index would be the current heading's
|
|
49
|
+
// Keep track of which previous index would be the current heading's direct parent.
|
|
52
50
|
// Each entry <i> is the last index of the `headings` array at heading level <i>.
|
|
53
51
|
// We will modify these indices as we iterate through all headings.
|
|
54
52
|
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
@@ -5,53 +5,80 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import visit from 'unist-util-visit';
|
|
9
|
-
import path from 'path';
|
|
10
|
-
import url from 'url';
|
|
11
|
-
import fs from 'fs-extra';
|
|
12
|
-
import escapeHtml from 'escape-html';
|
|
13
8
|
import {
|
|
9
|
+
toMessageRelativeFilePath,
|
|
14
10
|
posixPath,
|
|
15
11
|
escapePath,
|
|
16
|
-
toMessageRelativeFilePath,
|
|
17
12
|
getFileLoaderUtils,
|
|
13
|
+
findAsyncSequential,
|
|
18
14
|
} from '@docusaurus/utils';
|
|
15
|
+
import visit from 'unist-util-visit';
|
|
16
|
+
import path from 'path';
|
|
17
|
+
import url from 'url';
|
|
18
|
+
import fs from 'fs-extra';
|
|
19
|
+
import escapeHtml from 'escape-html';
|
|
20
|
+
import sizeOf from 'image-size';
|
|
21
|
+
import {promisify} from 'util';
|
|
19
22
|
import type {Plugin, Transformer} from 'unified';
|
|
20
23
|
import type {Image, Literal} from 'mdast';
|
|
24
|
+
import logger from '@docusaurus/logger';
|
|
21
25
|
|
|
22
26
|
const {
|
|
23
27
|
loaders: {inlineMarkdownImageFileLoader},
|
|
24
28
|
} = getFileLoaderUtils();
|
|
25
29
|
|
|
26
|
-
|
|
27
|
-
filePath: string;
|
|
30
|
+
type PluginOptions = {
|
|
28
31
|
staticDirs: string[];
|
|
29
|
-
|
|
32
|
+
siteDir: string;
|
|
33
|
+
};
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
(jsxNode as unknown as Literal).type = 'jsx';
|
|
34
|
-
(jsxNode as unknown as Literal).value = `<img ${
|
|
35
|
-
node.alt ? `alt={"${escapeHtml(node.alt)}"} ` : ''
|
|
36
|
-
}${
|
|
37
|
-
node.url
|
|
38
|
-
? `src={require("${inlineMarkdownImageFileLoader}${escapePath(
|
|
39
|
-
pathUrl,
|
|
40
|
-
)}").default}`
|
|
41
|
-
: ''
|
|
42
|
-
}${node.title ? ` title="${escapeHtml(node.title)}"` : ''} />`;
|
|
43
|
-
|
|
44
|
-
if (jsxNode.url) {
|
|
45
|
-
delete (jsxNode as Partial<Image>).url;
|
|
46
|
-
}
|
|
47
|
-
if (jsxNode.alt) {
|
|
48
|
-
delete jsxNode.alt;
|
|
49
|
-
}
|
|
50
|
-
if (jsxNode.title) {
|
|
51
|
-
delete jsxNode.title;
|
|
52
|
-
}
|
|
35
|
+
type Context = PluginOptions & {
|
|
36
|
+
filePath: string;
|
|
53
37
|
};
|
|
54
38
|
|
|
39
|
+
async function toImageRequireNode(
|
|
40
|
+
node: Image,
|
|
41
|
+
imagePath: string,
|
|
42
|
+
filePath: string,
|
|
43
|
+
) {
|
|
44
|
+
const jsxNode = node as Literal & Partial<Image>;
|
|
45
|
+
let relativeImagePath = posixPath(
|
|
46
|
+
path.relative(path.dirname(filePath), imagePath),
|
|
47
|
+
);
|
|
48
|
+
relativeImagePath = `./${relativeImagePath}`;
|
|
49
|
+
|
|
50
|
+
const parsedUrl = url.parse(node.url);
|
|
51
|
+
const hash = parsedUrl.hash ?? '';
|
|
52
|
+
const search = parsedUrl.search ?? '';
|
|
53
|
+
|
|
54
|
+
const alt = node.alt ? `alt={"${escapeHtml(node.alt)}"} ` : '';
|
|
55
|
+
const src = `require("${inlineMarkdownImageFileLoader}${
|
|
56
|
+
escapePath(relativeImagePath) + search
|
|
57
|
+
}").default${hash ? ` + '${hash}'` : ''}`;
|
|
58
|
+
const title = node.title ? ` title="${escapeHtml(node.title)}"` : '';
|
|
59
|
+
let width = '';
|
|
60
|
+
let height = '';
|
|
61
|
+
try {
|
|
62
|
+
const size = (await promisify(sizeOf)(imagePath))!;
|
|
63
|
+
if (size.width) {
|
|
64
|
+
width = ` width="${size.width}"`;
|
|
65
|
+
}
|
|
66
|
+
if (size.height) {
|
|
67
|
+
height = ` height="${size.height}"`;
|
|
68
|
+
}
|
|
69
|
+
} catch (e) {
|
|
70
|
+
logger.error`The image at path=${imagePath} can't be read correctly. Please ensure it's a valid image.
|
|
71
|
+
${(e as Error).message}`;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
Object.keys(jsxNode).forEach(
|
|
75
|
+
(key) => delete jsxNode[key as keyof typeof jsxNode],
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
(jsxNode as Literal).type = 'jsx';
|
|
79
|
+
jsxNode.value = `<img ${alt}src={${src}}${title}${width}${height} />`;
|
|
80
|
+
}
|
|
81
|
+
|
|
55
82
|
async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
|
|
56
83
|
const imageExists = await fs.pathExists(imagePath);
|
|
57
84
|
if (!imageExists) {
|
|
@@ -63,36 +90,53 @@ async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
|
|
|
63
90
|
}
|
|
64
91
|
}
|
|
65
92
|
|
|
66
|
-
async function
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
93
|
+
async function getImageAbsolutePath(
|
|
94
|
+
imagePath: string,
|
|
95
|
+
{siteDir, filePath, staticDirs}: Context,
|
|
96
|
+
) {
|
|
97
|
+
if (imagePath.startsWith('@site/')) {
|
|
98
|
+
const imageFilePath = path.join(siteDir, imagePath.replace('@site/', ''));
|
|
99
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
100
|
+
return imageFilePath;
|
|
101
|
+
} else if (path.isAbsolute(imagePath)) {
|
|
102
|
+
// absolute paths are expected to exist in the static folder
|
|
103
|
+
const possiblePaths = staticDirs.map((dir) => path.join(dir, imagePath));
|
|
104
|
+
const imageFilePath = await findAsyncSequential(
|
|
105
|
+
possiblePaths,
|
|
106
|
+
fs.pathExists,
|
|
107
|
+
);
|
|
108
|
+
if (!imageFilePath) {
|
|
109
|
+
throw new Error(
|
|
110
|
+
`Image ${possiblePaths
|
|
111
|
+
.map((p) => toMessageRelativeFilePath(p))
|
|
112
|
+
.join(' or ')} used in ${toMessageRelativeFilePath(
|
|
113
|
+
filePath,
|
|
114
|
+
)} not found.`,
|
|
115
|
+
);
|
|
71
116
|
}
|
|
117
|
+
return imageFilePath;
|
|
118
|
+
}
|
|
119
|
+
// We try to convert image urls without protocol to images with require calls
|
|
120
|
+
// going through webpack ensures that image assets exist at build time
|
|
121
|
+
else {
|
|
122
|
+
// relative paths are resolved against the source file's folder
|
|
123
|
+
const imageFilePath = path.join(path.dirname(filePath), imagePath);
|
|
124
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
125
|
+
return imageFilePath;
|
|
72
126
|
}
|
|
73
|
-
throw new Error(
|
|
74
|
-
`Image ${possiblePaths
|
|
75
|
-
.map((p) => toMessageRelativeFilePath(p))
|
|
76
|
-
.join(' or ')} used in ${toMessageRelativeFilePath(
|
|
77
|
-
sourceFilePath,
|
|
78
|
-
)} not found.`,
|
|
79
|
-
);
|
|
80
127
|
}
|
|
81
128
|
|
|
82
|
-
async function processImageNode(
|
|
83
|
-
node: Image,
|
|
84
|
-
{filePath, staticDirs}: PluginOptions,
|
|
85
|
-
) {
|
|
129
|
+
async function processImageNode(node: Image, context: Context) {
|
|
86
130
|
if (!node.url) {
|
|
87
131
|
throw new Error(
|
|
88
132
|
`Markdown image URL is mandatory in "${toMessageRelativeFilePath(
|
|
89
|
-
filePath,
|
|
133
|
+
context.filePath,
|
|
90
134
|
)}" file`,
|
|
91
135
|
);
|
|
92
136
|
}
|
|
93
137
|
|
|
94
138
|
const parsedUrl = url.parse(node.url);
|
|
95
|
-
if (parsedUrl.protocol) {
|
|
139
|
+
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
96
140
|
// pathname:// is an escape hatch,
|
|
97
141
|
// in case user does not want his images to be converted to require calls going through webpack loader
|
|
98
142
|
// we don't have to document this for now,
|
|
@@ -100,31 +144,20 @@ async function processImageNode(
|
|
|
100
144
|
if (parsedUrl.protocol === 'pathname:') {
|
|
101
145
|
node.url = node.url.replace('pathname://', '');
|
|
102
146
|
}
|
|
147
|
+
return;
|
|
103
148
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const possibleImagePaths = staticDirs.map((dir) =>
|
|
108
|
-
path.join(dir, node.url),
|
|
109
|
-
);
|
|
110
|
-
const imagePath = await findImage(possibleImagePaths, filePath);
|
|
111
|
-
createJSX(node, posixPath(imagePath));
|
|
112
|
-
}
|
|
113
|
-
// We try to convert image urls without protocol to images with require calls
|
|
114
|
-
// going through webpack ensures that image assets exist at build time
|
|
115
|
-
else {
|
|
116
|
-
// relative paths are resolved against the source file's folder
|
|
117
|
-
const expectedImagePath = path.join(path.dirname(filePath), node.url);
|
|
118
|
-
await ensureImageFileExist(expectedImagePath, filePath);
|
|
119
|
-
createJSX(node, node.url.startsWith('./') ? node.url : `./${node.url}`);
|
|
120
|
-
}
|
|
149
|
+
|
|
150
|
+
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
|
|
151
|
+
await toImageRequireNode(node, imagePath, context.filePath);
|
|
121
152
|
}
|
|
122
153
|
|
|
123
154
|
const plugin: Plugin<[PluginOptions]> = (options) => {
|
|
124
|
-
const transformer: Transformer = async (root) => {
|
|
155
|
+
const transformer: Transformer = async (root, vfile) => {
|
|
125
156
|
const promises: Promise<void>[] = [];
|
|
126
157
|
visit(root, 'image', (node: Image) => {
|
|
127
|
-
promises.push(
|
|
158
|
+
promises.push(
|
|
159
|
+
processImageNode(node, {...options, filePath: vfile.path!}),
|
|
160
|
+
);
|
|
128
161
|
});
|
|
129
162
|
await Promise.all(promises);
|
|
130
163
|
};
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
posixPath,
|
|
11
11
|
escapePath,
|
|
12
12
|
getFileLoaderUtils,
|
|
13
|
+
findAsyncSequential,
|
|
13
14
|
} from '@docusaurus/utils';
|
|
14
15
|
import visit from 'unist-util-visit';
|
|
15
16
|
import path from 'path';
|
|
@@ -23,114 +24,87 @@ import type {Link, Literal} from 'mdast';
|
|
|
23
24
|
const {
|
|
24
25
|
loaders: {inlineMarkdownLinkFileLoader},
|
|
25
26
|
} = getFileLoaderUtils();
|
|
26
|
-
const hashRegex = /#.*$/;
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
filePath: string;
|
|
28
|
+
type PluginOptions = {
|
|
30
29
|
staticDirs: string[];
|
|
31
30
|
siteDir: string;
|
|
32
|
-
}
|
|
31
|
+
};
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
) {
|
|
38
|
-
const assetExists = await fs.pathExists(fileSystemAssetPath);
|
|
39
|
-
if (!assetExists) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
`Asset ${toMessageRelativeFilePath(
|
|
42
|
-
fileSystemAssetPath,
|
|
43
|
-
)} used in ${toMessageRelativeFilePath(sourceFilePath)} not found.`,
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
33
|
+
type Context = PluginOptions & {
|
|
34
|
+
filePath: string;
|
|
35
|
+
};
|
|
47
36
|
|
|
48
37
|
// transform the link node to a jsx link with a require() call
|
|
49
|
-
function toAssetRequireNode({
|
|
50
|
-
node
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}: {
|
|
54
|
-
node: Link;
|
|
55
|
-
filePath: string;
|
|
56
|
-
requireAssetPath: string;
|
|
57
|
-
}) {
|
|
58
|
-
let relativeRequireAssetPath = posixPath(
|
|
59
|
-
path.relative(path.dirname(filePath), requireAssetPath),
|
|
38
|
+
function toAssetRequireNode(node: Link, assetPath: string, filePath: string) {
|
|
39
|
+
const jsxNode = node as Literal & Partial<Link>;
|
|
40
|
+
let relativeAssetPath = posixPath(
|
|
41
|
+
path.relative(path.dirname(filePath), assetPath),
|
|
60
42
|
);
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
43
|
+
// require("assets/file.pdf") means requiring from a package called assets
|
|
44
|
+
relativeAssetPath = `./${relativeAssetPath}`;
|
|
45
|
+
|
|
46
|
+
const parsedUrl = url.parse(node.url);
|
|
47
|
+
const hash = parsedUrl.hash ?? '';
|
|
48
|
+
const search = parsedUrl.search ?? '';
|
|
49
|
+
|
|
50
|
+
const href = `require('${
|
|
51
|
+
// A hack to stop Webpack from using its built-in loader to parse JSON
|
|
52
|
+
path.extname(relativeAssetPath) === '.json'
|
|
53
|
+
? `${relativeAssetPath.replace('.json', '.raw')}!=`
|
|
54
|
+
: ''
|
|
55
|
+
}${inlineMarkdownLinkFileLoader}${
|
|
56
|
+
escapePath(relativeAssetPath) + search
|
|
57
|
+
}').default${hash ? ` + '${hash}'` : ''}`;
|
|
73
58
|
const children = stringifyContent(node);
|
|
74
59
|
const title = node.title ? ` title="${escapeHtml(node.title)}"` : '';
|
|
75
60
|
|
|
76
|
-
(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
).value = `<a target="_blank" href={${href}}${title}>${children}</a>`;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// If the link looks like an asset link, we'll link to the asset,
|
|
83
|
-
// and use a require("assetUrl") (using webpack url-loader/file-loader)
|
|
84
|
-
// instead of navigating to such link
|
|
85
|
-
async function convertToAssetLinkIfNeeded(
|
|
86
|
-
node: Link,
|
|
87
|
-
{filePath, siteDir, staticDirs}: PluginOptions,
|
|
88
|
-
) {
|
|
89
|
-
const assetPath = node.url.replace(hashRegex, '');
|
|
90
|
-
|
|
91
|
-
const hasSiteAlias = assetPath.startsWith('@site/');
|
|
92
|
-
const hasAssetLikeExtension =
|
|
93
|
-
path.extname(assetPath) && !assetPath.match(/#|.md|.mdx|.html/);
|
|
94
|
-
|
|
95
|
-
const looksLikeAssetLink = hasSiteAlias || hasAssetLikeExtension;
|
|
61
|
+
Object.keys(jsxNode).forEach(
|
|
62
|
+
(key) => delete jsxNode[key as keyof typeof jsxNode],
|
|
63
|
+
);
|
|
96
64
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
65
|
+
(jsxNode as Literal).type = 'jsx';
|
|
66
|
+
jsxNode.value = `<a target="_blank" href={${href}}${title}>${children}</a>`;
|
|
67
|
+
}
|
|
100
68
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
69
|
+
async function ensureAssetFileExist(assetPath: string, sourceFilePath: string) {
|
|
70
|
+
const assetExists = await fs.pathExists(assetPath);
|
|
71
|
+
if (!assetExists) {
|
|
72
|
+
throw new Error(
|
|
73
|
+
`Asset ${toMessageRelativeFilePath(
|
|
74
|
+
assetPath,
|
|
75
|
+
)} used in ${toMessageRelativeFilePath(sourceFilePath)} not found.`,
|
|
76
|
+
);
|
|
107
77
|
}
|
|
78
|
+
}
|
|
108
79
|
|
|
80
|
+
async function getAssetAbsolutePath(
|
|
81
|
+
assetPath: string,
|
|
82
|
+
{siteDir, filePath, staticDirs}: Context,
|
|
83
|
+
) {
|
|
109
84
|
if (assetPath.startsWith('@site/')) {
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
toAssetLinkNode(fileSystemAssetPath);
|
|
85
|
+
const assetFilePath = path.join(siteDir, assetPath.replace('@site/', ''));
|
|
86
|
+
// The @site alias is the only way to believe that the user wants an asset.
|
|
87
|
+
// Everything else can just be a link URL
|
|
88
|
+
await ensureAssetFileExist(assetFilePath, filePath);
|
|
89
|
+
return assetFilePath;
|
|
116
90
|
} else if (path.isAbsolute(assetPath)) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
91
|
+
const assetFilePath = await findAsyncSequential(
|
|
92
|
+
staticDirs.map((dir) => path.join(dir, assetPath)),
|
|
93
|
+
fs.pathExists,
|
|
94
|
+
);
|
|
95
|
+
if (assetFilePath) {
|
|
96
|
+
return assetFilePath;
|
|
124
97
|
}
|
|
125
98
|
} else {
|
|
126
|
-
const
|
|
127
|
-
if (await fs.pathExists(
|
|
128
|
-
|
|
99
|
+
const assetFilePath = path.join(path.dirname(filePath), assetPath);
|
|
100
|
+
if (await fs.pathExists(assetFilePath)) {
|
|
101
|
+
return assetFilePath;
|
|
129
102
|
}
|
|
130
103
|
}
|
|
104
|
+
return null;
|
|
131
105
|
}
|
|
132
106
|
|
|
133
|
-
async function processLinkNode(node: Link,
|
|
107
|
+
async function processLinkNode(node: Link, context: Context) {
|
|
134
108
|
if (!node.url) {
|
|
135
109
|
// try to improve error feedback
|
|
136
110
|
// see https://github.com/facebook/docusaurus/issues/3309#issuecomment-690371675
|
|
@@ -138,24 +112,35 @@ async function processLinkNode(node: Link, options: PluginOptions) {
|
|
|
138
112
|
const line = node?.position?.start?.line || '?';
|
|
139
113
|
throw new Error(
|
|
140
114
|
`Markdown link URL is mandatory in "${toMessageRelativeFilePath(
|
|
141
|
-
|
|
115
|
+
context.filePath,
|
|
142
116
|
)}" file (title: ${title}, line: ${line}).`,
|
|
143
117
|
);
|
|
144
118
|
}
|
|
145
119
|
|
|
146
120
|
const parsedUrl = url.parse(node.url);
|
|
147
|
-
if (parsedUrl.protocol) {
|
|
121
|
+
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
122
|
+
// Don't process pathname:// here, it's used by the <Link> component
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const hasSiteAlias = parsedUrl.pathname.startsWith('@site/');
|
|
126
|
+
const hasAssetLikeExtension =
|
|
127
|
+
path.extname(parsedUrl.pathname) &&
|
|
128
|
+
!parsedUrl.pathname.match(/\.(?:mdx?|html)(?:#|$)/);
|
|
129
|
+
if (!hasSiteAlias && !hasAssetLikeExtension) {
|
|
148
130
|
return;
|
|
149
131
|
}
|
|
150
132
|
|
|
151
|
-
await
|
|
133
|
+
const assetPath = await getAssetAbsolutePath(parsedUrl.pathname, context);
|
|
134
|
+
if (assetPath) {
|
|
135
|
+
toAssetRequireNode(node, assetPath, context.filePath);
|
|
136
|
+
}
|
|
152
137
|
}
|
|
153
138
|
|
|
154
139
|
const plugin: Plugin<[PluginOptions]> = (options) => {
|
|
155
|
-
const transformer: Transformer = async (root) => {
|
|
140
|
+
const transformer: Transformer = async (root, vfile) => {
|
|
156
141
|
const promises: Promise<void>[] = [];
|
|
157
142
|
visit(root, 'link', (node: Link) => {
|
|
158
|
-
promises.push(processLinkNode(node, options));
|
|
143
|
+
promises.push(processLinkNode(node, {...options, filePath: vfile.path!}));
|
|
159
144
|
});
|
|
160
145
|
await Promise.all(promises);
|
|
161
146
|
};
|