@docusaurus/mdx-loader 0.0.0-4388 → 0.0.0-4395
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 -1
- package/lib/index.js.map +1 -1
- package/lib/remark/toc/search.js +1 -1
- package/lib/remark/transformImage/index.d.ts +1 -0
- package/lib/remark/transformImage/index.d.ts.map +1 -1
- package/lib/remark/transformImage/index.js +46 -39
- package/lib/remark/transformImage/index.js.map +1 -1
- package/lib/remark/transformLinks/index.d.ts.map +1 -1
- package/lib/remark/transformLinks/index.js +42 -47
- package/lib/remark/transformLinks/index.js.map +1 -1
- package/package.json +5 -5
- package/src/index.ts +8 -1
- package/src/remark/toc/search.ts +1 -1
- package/src/remark/transformImage/index.ts +69 -59
- package/src/remark/transformLinks/index.ts +56 -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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/lib/index.js
CHANGED
|
@@ -84,7 +84,14 @@ 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
|
+
filePath,
|
|
92
|
+
siteDir: reqOptions.siteDir,
|
|
93
|
+
},
|
|
94
|
+
],
|
|
88
95
|
[
|
|
89
96
|
transformLinks_1.default,
|
|
90
97
|
{
|
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;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,uGAAuG;AACvG,kEAAkE;AAClE,6EAA6E;AAC7E,KAAK,UAAU,gBAAgB,CAAC,YAAoB;IAClD,IAAI;QACF,OAAO,MAAM,IAAA,mBAAQ,EAAC,YAAY,EAAE,MAAM,CAAC,CAAC;KAC7C;IAAC,OAAO,CAAC,EAAE;QACV,MAAM,IAAI,KAAK,CACb,oDAAoD,YAAY,4DAA4D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
parentIndex: -1,
|
|
34
34
|
});
|
|
35
35
|
});
|
|
36
|
-
// Keep track of which previous index would be the current heading's
|
|
36
|
+
// Keep track of which previous index would be the current heading's direct parent.
|
|
37
37
|
// Each entry <i> is the last index of the `headings` array at heading level <i>.
|
|
38
38
|
// We will modify these indices as we iterate through all headings.
|
|
39
39
|
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AAOjD,UAAU,aAAa;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAaH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AAOjD,UAAU,aAAa;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB;AAmGD,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CASnC,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -7,50 +7,69 @@
|
|
|
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 utils_1 = require("@docusaurus/utils");
|
|
16
16
|
const { loaders: { inlineMarkdownImageFileLoader }, } = (0, utils_1.getFileLoaderUtils)();
|
|
17
|
-
|
|
17
|
+
function toImageRequireNode(node, imagePath, filePath) {
|
|
18
|
+
var _a, _b;
|
|
18
19
|
const jsxNode = node;
|
|
20
|
+
let relativeImagePath = (0, utils_1.posixPath)(path_1.default.relative(path_1.default.dirname(filePath), imagePath));
|
|
21
|
+
relativeImagePath = `./${relativeImagePath}`;
|
|
22
|
+
const parsedUrl = url_1.default.parse(node.url);
|
|
23
|
+
const hash = (_a = parsedUrl.hash) !== null && _a !== void 0 ? _a : '';
|
|
24
|
+
const search = (_b = parsedUrl.search) !== null && _b !== void 0 ? _b : '';
|
|
25
|
+
const alt = node.alt ? `alt={"${(0, escape_html_1.default)(node.alt)}"} ` : '';
|
|
26
|
+
const src = `require("${inlineMarkdownImageFileLoader}${(0, utils_1.escapePath)(relativeImagePath) + search}").default${hash ? ` + '${hash}'` : ''}`;
|
|
27
|
+
const title = node.title ? ` title="${(0, escape_html_1.default)(node.title)}"` : '';
|
|
28
|
+
Object.keys(jsxNode).forEach((key) => delete jsxNode[key]);
|
|
19
29
|
jsxNode.type = 'jsx';
|
|
20
|
-
jsxNode.value = `<img ${
|
|
21
|
-
|
|
22
|
-
delete jsxNode.url;
|
|
23
|
-
}
|
|
24
|
-
if (jsxNode.alt) {
|
|
25
|
-
delete jsxNode.alt;
|
|
26
|
-
}
|
|
27
|
-
if (jsxNode.title) {
|
|
28
|
-
delete jsxNode.title;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
30
|
+
jsxNode.value = `<img ${alt}src={${src}}${title} />`;
|
|
31
|
+
}
|
|
31
32
|
async function ensureImageFileExist(imagePath, sourceFilePath) {
|
|
32
33
|
const imageExists = await fs_extra_1.default.pathExists(imagePath);
|
|
33
34
|
if (!imageExists) {
|
|
34
35
|
throw new Error(`Image ${(0, utils_1.toMessageRelativeFilePath)(imagePath)} used in ${(0, utils_1.toMessageRelativeFilePath)(sourceFilePath)} not found.`);
|
|
35
36
|
}
|
|
36
37
|
}
|
|
37
|
-
async function
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
38
|
+
async function getImageAbsolutePath(imagePath, { siteDir, filePath, staticDirs }) {
|
|
39
|
+
if (imagePath.startsWith('@site/')) {
|
|
40
|
+
const imageFilePath = path_1.default.join(siteDir, imagePath.replace('@site/', ''));
|
|
41
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
42
|
+
return imageFilePath;
|
|
43
|
+
}
|
|
44
|
+
else if (path_1.default.isAbsolute(imagePath)) {
|
|
45
|
+
// absolute paths are expected to exist in the static folder
|
|
46
|
+
const possiblePaths = staticDirs.map((dir) => path_1.default.join(dir, imagePath));
|
|
47
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
48
|
+
for (const possiblePath of possiblePaths) {
|
|
49
|
+
const imageFilePath = possiblePath;
|
|
50
|
+
if (await fs_extra_1.default.pathExists(imageFilePath)) {
|
|
51
|
+
return imageFilePath;
|
|
52
|
+
}
|
|
42
53
|
}
|
|
54
|
+
throw new Error(`Image ${possiblePaths
|
|
55
|
+
.map((p) => (0, utils_1.toMessageRelativeFilePath)(p))
|
|
56
|
+
.join(' or ')} used in ${(0, utils_1.toMessageRelativeFilePath)(filePath)} not found.`);
|
|
57
|
+
}
|
|
58
|
+
// We try to convert image urls without protocol to images with require calls
|
|
59
|
+
// going through webpack ensures that image assets exist at build time
|
|
60
|
+
else {
|
|
61
|
+
// relative paths are resolved against the source file's folder
|
|
62
|
+
const imageFilePath = path_1.default.join(path_1.default.dirname(filePath), imagePath);
|
|
63
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
64
|
+
return imageFilePath;
|
|
43
65
|
}
|
|
44
|
-
throw new Error(`Image ${possiblePaths
|
|
45
|
-
.map((p) => (0, utils_1.toMessageRelativeFilePath)(p))
|
|
46
|
-
.join(' or ')} used in ${(0, utils_1.toMessageRelativeFilePath)(sourceFilePath)} not found.`);
|
|
47
66
|
}
|
|
48
|
-
async function processImageNode(node,
|
|
67
|
+
async function processImageNode(node, options) {
|
|
49
68
|
if (!node.url) {
|
|
50
|
-
throw new Error(`Markdown image URL is mandatory in "${(0, utils_1.toMessageRelativeFilePath)(filePath)}" file`);
|
|
69
|
+
throw new Error(`Markdown image URL is mandatory in "${(0, utils_1.toMessageRelativeFilePath)(options.filePath)}" file`);
|
|
51
70
|
}
|
|
52
71
|
const parsedUrl = url_1.default.parse(node.url);
|
|
53
|
-
if (parsedUrl.protocol) {
|
|
72
|
+
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
54
73
|
// pathname:// is an escape hatch,
|
|
55
74
|
// in case user does not want his images to be converted to require calls going through webpack loader
|
|
56
75
|
// we don't have to document this for now,
|
|
@@ -58,22 +77,10 @@ async function processImageNode(node, { filePath, staticDirs }) {
|
|
|
58
77
|
if (parsedUrl.protocol === 'pathname:') {
|
|
59
78
|
node.url = node.url.replace('pathname://', '');
|
|
60
79
|
}
|
|
80
|
+
return;
|
|
61
81
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
// absolute paths are expected to exist in the static folder
|
|
65
|
-
const possibleImagePaths = staticDirs.map((dir) => path_1.default.join(dir, node.url));
|
|
66
|
-
const imagePath = await findImage(possibleImagePaths, filePath);
|
|
67
|
-
createJSX(node, (0, utils_1.posixPath)(imagePath));
|
|
68
|
-
}
|
|
69
|
-
// We try to convert image urls without protocol to images with require calls
|
|
70
|
-
// going through webpack ensures that image assets exist at build time
|
|
71
|
-
else {
|
|
72
|
-
// relative paths are resolved against the source file's folder
|
|
73
|
-
const expectedImagePath = path_1.default.join(path_1.default.dirname(filePath), node.url);
|
|
74
|
-
await ensureImageFileExist(expectedImagePath, filePath);
|
|
75
|
-
createJSX(node, node.url.startsWith('./') ? node.url : `./${node.url}`);
|
|
76
|
-
}
|
|
82
|
+
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, options);
|
|
83
|
+
toImageRequireNode(node, imagePath, options.filePath);
|
|
77
84
|
}
|
|
78
85
|
const plugin = (options) => {
|
|
79
86
|
const transformer = async (root) => {
|
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6CAK2B;AAC3B,qFAAqC;AACrC,6DAAwB;AACxB,2DAAsB;AACtB,qEAA0B;AAC1B,2EAAqC;AAIrC,MAAM,EACJ,OAAO,EAAE,EAAC,6BAA6B,EAAC,GACzC,GAAG,IAAA,0BAAkB,GAAE,CAAC;AAQzB,SAAS,kBAAkB,CAAC,IAAW,EAAE,SAAiB,EAAE,QAAgB;;IAC1E,MAAM,OAAO,GAAG,IAAgC,CAAC;IACjD,IAAI,iBAAiB,GAAG,IAAA,iBAAS,EAC/B,cAAI,CAAC,QAAQ,CAAC,cAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC,CACjD,CAAC;IACF,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,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,IAAA,qBAAU,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AAI,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,kBAAkB,CAAC,IAAI,EAAE,SAAS,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;AACxD,CAAC;AAED,MAAM,MAAM,GAA4B,CAAC,OAAO,EAAE,EAAE;IAClD,MAAM,WAAW,GAAgB,KAAK,EAAE,IAAI,EAAE,EAAE;QAC9C,MAAM,QAAQ,GAAoB,EAAE,CAAC;QACrC,IAAA,0BAAK,EAAC,IAAI,EAAE,OAAO,EAAE,CAAC,IAAW,EAAE,EAAE;YACnC,QAAQ,CAAC,IAAI,CAAC,gBAAgB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;QACjD,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"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAcH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAcH,OAAO,KAAK,EAAC,MAAM,EAAc,MAAM,SAAS,CAAC;AAOjD,UAAU,aAAa;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB;AAmGD,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CASnC,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -15,68 +15,53 @@ 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
|
-
: '';
|
|
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));
|
|
31
23
|
// require("assets/file.pdf") means requiring from a package called assets
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
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('${inlineMarkdownLinkFileLoader}${(0, utils_1.escapePath)(relativeAssetPath) + search}').default${hash ? ` + '${hash}'` : ''}`;
|
|
36
29
|
const children = (0, utils_2.stringifyContent)(node);
|
|
37
30
|
const title = node.title ? ` title="${(0, escape_html_1.default)(node.title)}"` : '';
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
Object.keys(jsxNode).forEach((key) => delete jsxNode[key]);
|
|
32
|
+
jsxNode.type = 'jsx';
|
|
33
|
+
jsxNode.value = `<a target="_blank" href={${href}}${title}>${children}</a>`;
|
|
40
34
|
}
|
|
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
|
-
});
|
|
35
|
+
async function ensureAssetFileExist(assetPath, sourceFilePath) {
|
|
36
|
+
const assetExists = await fs_extra_1.default.pathExists(assetPath);
|
|
37
|
+
if (!assetExists) {
|
|
38
|
+
throw new Error(`Asset ${(0, utils_1.toMessageRelativeFilePath)(assetPath)} used in ${(0, utils_1.toMessageRelativeFilePath)(sourceFilePath)} not found.`);
|
|
58
39
|
}
|
|
40
|
+
}
|
|
41
|
+
async function getAssetAbsolutePath(assetPath, { siteDir, filePath, staticDirs }) {
|
|
59
42
|
if (assetPath.startsWith('@site/')) {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
43
|
+
const assetFilePath = path_1.default.join(siteDir, assetPath.replace('@site/', ''));
|
|
44
|
+
// The @site alias is the only way to believe that the user wants an asset.
|
|
45
|
+
// Everything else can just be a link URL
|
|
46
|
+
await ensureAssetFileExist(assetFilePath, filePath);
|
|
47
|
+
return assetFilePath;
|
|
63
48
|
}
|
|
64
49
|
else if (path_1.default.isAbsolute(assetPath)) {
|
|
65
50
|
// eslint-disable-next-line no-restricted-syntax
|
|
66
51
|
for (const staticDir of staticDirs) {
|
|
67
|
-
const
|
|
68
|
-
if (await fs_extra_1.default.pathExists(
|
|
69
|
-
|
|
70
|
-
return;
|
|
52
|
+
const assetFilePath = path_1.default.join(staticDir, assetPath);
|
|
53
|
+
if (await fs_extra_1.default.pathExists(assetFilePath)) {
|
|
54
|
+
return assetFilePath;
|
|
71
55
|
}
|
|
72
56
|
}
|
|
73
57
|
}
|
|
74
58
|
else {
|
|
75
|
-
const
|
|
76
|
-
if (await fs_extra_1.default.pathExists(
|
|
77
|
-
|
|
59
|
+
const assetFilePath = path_1.default.join(path_1.default.dirname(filePath), assetPath);
|
|
60
|
+
if (await fs_extra_1.default.pathExists(assetFilePath)) {
|
|
61
|
+
return assetFilePath;
|
|
78
62
|
}
|
|
79
63
|
}
|
|
64
|
+
return null;
|
|
80
65
|
}
|
|
81
66
|
async function processLinkNode(node, options) {
|
|
82
67
|
var _a, _b, _c;
|
|
@@ -88,10 +73,20 @@ async function processLinkNode(node, options) {
|
|
|
88
73
|
throw new Error(`Markdown link URL is mandatory in "${(0, utils_1.toMessageRelativeFilePath)(options.filePath)}" file (title: ${title}, line: ${line}).`);
|
|
89
74
|
}
|
|
90
75
|
const parsedUrl = url_1.default.parse(node.url);
|
|
91
|
-
if (parsedUrl.protocol) {
|
|
76
|
+
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
77
|
+
// Don't process pathname:// here, it's used by the <Link> component
|
|
92
78
|
return;
|
|
93
79
|
}
|
|
94
|
-
|
|
80
|
+
const hasSiteAlias = parsedUrl.pathname.startsWith('@site/');
|
|
81
|
+
const hasAssetLikeExtension = path_1.default.extname(parsedUrl.pathname) &&
|
|
82
|
+
!parsedUrl.pathname.match(/\.(?:mdx?|html)(?:#|$)/);
|
|
83
|
+
if (!hasSiteAlias && !hasAssetLikeExtension) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const assetPath = await getAssetAbsolutePath(parsedUrl.pathname, options);
|
|
87
|
+
if (assetPath) {
|
|
88
|
+
toAssetRequireNode(node, assetPath, options.filePath);
|
|
89
|
+
}
|
|
95
90
|
}
|
|
96
91
|
const plugin = (options) => {
|
|
97
92
|
const transformer = async (root) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6CAK2B;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;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformLinks/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6CAK2B;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;AAQzB,8DAA8D;AAC9D,SAAS,kBAAkB,CAAC,IAAU,EAAE,SAAiB,EAAE,QAAgB;;IACzE,MAAM,OAAO,GAAG,IAA+B,CAAC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AAE;YACjC,QAAQ,CAAC,IAAI,CAAC,eAAe,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAC;QAChD,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": "0.0.0-
|
|
3
|
+
"version": "0.0.0-4395",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "src/mdx-loader.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"@babel/parser": "^7.16.4",
|
|
22
22
|
"@babel/traverse": "^7.16.3",
|
|
23
|
-
"@docusaurus/logger": "0.0.0-
|
|
24
|
-
"@docusaurus/utils": "0.0.0-
|
|
23
|
+
"@docusaurus/logger": "0.0.0-4395",
|
|
24
|
+
"@docusaurus/utils": "0.0.0-4395",
|
|
25
25
|
"@mdx-js/mdx": "^1.6.21",
|
|
26
26
|
"@mdx-js/react": "^1.6.21",
|
|
27
27
|
"escape-html": "^1.0.3",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"webpack": "^5.61.0"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@docusaurus/types": "0.0.0-
|
|
40
|
+
"@docusaurus/types": "0.0.0-4395",
|
|
41
41
|
"@types/escape-html": "^1.0.1",
|
|
42
42
|
"@types/mdast": "^3.0.7",
|
|
43
43
|
"@types/stringify-object": "^3.3.1",
|
|
@@ -55,5 +55,5 @@
|
|
|
55
55
|
"engines": {
|
|
56
56
|
"node": ">=14"
|
|
57
57
|
},
|
|
58
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "860ece1feda34ed569ebbafb64a5fc6dcb2de10b"
|
|
59
59
|
}
|
package/src/index.ts
CHANGED
|
@@ -124,7 +124,14 @@ 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
|
+
filePath,
|
|
132
|
+
siteDir: reqOptions.siteDir,
|
|
133
|
+
},
|
|
134
|
+
],
|
|
128
135
|
[
|
|
129
136
|
transformLinks,
|
|
130
137
|
{
|
package/src/remark/toc/search.ts
CHANGED
|
@@ -46,7 +46,7 @@ export default function search(node: Node): TOCItem[] {
|
|
|
46
46
|
});
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
-
// 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.
|
|
50
50
|
// Each entry <i> is the last index of the `headings` array at heading level <i>.
|
|
51
51
|
// We will modify these indices as we iterate through all headings.
|
|
52
52
|
// e.g. if an ### H3 was last seen at index 2, then prevIndexForLevel[3] === 2
|
|
@@ -5,17 +5,17 @@
|
|
|
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,
|
|
18
13
|
} from '@docusaurus/utils';
|
|
14
|
+
import visit from 'unist-util-visit';
|
|
15
|
+
import path from 'path';
|
|
16
|
+
import url from 'url';
|
|
17
|
+
import fs from 'fs-extra';
|
|
18
|
+
import escapeHtml from 'escape-html';
|
|
19
19
|
import type {Plugin, Transformer} from 'unified';
|
|
20
20
|
import type {Image, Literal} from 'mdast';
|
|
21
21
|
|
|
@@ -26,27 +26,33 @@ const {
|
|
|
26
26
|
interface PluginOptions {
|
|
27
27
|
filePath: string;
|
|
28
28
|
staticDirs: string[];
|
|
29
|
+
siteDir: string;
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
const jsxNode = node
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
pathUrl,
|
|
38
|
-
)}").default}`}${node.title ? ` title="${escapeHtml(node.title)}"` : ''} />`;
|
|
32
|
+
function toImageRequireNode(node: Image, imagePath: string, filePath: string) {
|
|
33
|
+
const jsxNode = node as Literal & Partial<Image>;
|
|
34
|
+
let relativeImagePath = posixPath(
|
|
35
|
+
path.relative(path.dirname(filePath), imagePath),
|
|
36
|
+
);
|
|
37
|
+
relativeImagePath = `./${relativeImagePath}`;
|
|
39
38
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
39
|
+
const parsedUrl = url.parse(node.url);
|
|
40
|
+
const hash = parsedUrl.hash ?? '';
|
|
41
|
+
const search = parsedUrl.search ?? '';
|
|
42
|
+
|
|
43
|
+
const alt = node.alt ? `alt={"${escapeHtml(node.alt)}"} ` : '';
|
|
44
|
+
const src = `require("${inlineMarkdownImageFileLoader}${
|
|
45
|
+
escapePath(relativeImagePath) + search
|
|
46
|
+
}").default${hash ? ` + '${hash}'` : ''}`;
|
|
47
|
+
const title = node.title ? ` title="${escapeHtml(node.title)}"` : '';
|
|
48
|
+
|
|
49
|
+
Object.keys(jsxNode).forEach(
|
|
50
|
+
(key) => delete jsxNode[key as keyof typeof jsxNode],
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
(jsxNode as Literal).type = 'jsx';
|
|
54
|
+
jsxNode.value = `<img ${alt}src={${src}}${title} />`;
|
|
55
|
+
}
|
|
50
56
|
|
|
51
57
|
async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
|
|
52
58
|
const imageExists = await fs.pathExists(imagePath);
|
|
@@ -59,36 +65,53 @@ async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
|
|
|
59
65
|
}
|
|
60
66
|
}
|
|
61
67
|
|
|
62
|
-
async function
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
async function getImageAbsolutePath(
|
|
69
|
+
imagePath: string,
|
|
70
|
+
{siteDir, filePath, staticDirs}: PluginOptions,
|
|
71
|
+
) {
|
|
72
|
+
if (imagePath.startsWith('@site/')) {
|
|
73
|
+
const imageFilePath = path.join(siteDir, imagePath.replace('@site/', ''));
|
|
74
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
75
|
+
return imageFilePath;
|
|
76
|
+
} else if (path.isAbsolute(imagePath)) {
|
|
77
|
+
// absolute paths are expected to exist in the static folder
|
|
78
|
+
const possiblePaths = staticDirs.map((dir) => path.join(dir, imagePath));
|
|
79
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
80
|
+
for (const possiblePath of possiblePaths) {
|
|
81
|
+
const imageFilePath = possiblePath;
|
|
82
|
+
if (await fs.pathExists(imageFilePath)) {
|
|
83
|
+
return imageFilePath;
|
|
84
|
+
}
|
|
67
85
|
}
|
|
86
|
+
throw new Error(
|
|
87
|
+
`Image ${possiblePaths
|
|
88
|
+
.map((p) => toMessageRelativeFilePath(p))
|
|
89
|
+
.join(' or ')} used in ${toMessageRelativeFilePath(
|
|
90
|
+
filePath,
|
|
91
|
+
)} not found.`,
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
// We try to convert image urls without protocol to images with require calls
|
|
95
|
+
// going through webpack ensures that image assets exist at build time
|
|
96
|
+
else {
|
|
97
|
+
// relative paths are resolved against the source file's folder
|
|
98
|
+
const imageFilePath = path.join(path.dirname(filePath), imagePath);
|
|
99
|
+
await ensureImageFileExist(imageFilePath, filePath);
|
|
100
|
+
return imageFilePath;
|
|
68
101
|
}
|
|
69
|
-
throw new Error(
|
|
70
|
-
`Image ${possiblePaths
|
|
71
|
-
.map((p) => toMessageRelativeFilePath(p))
|
|
72
|
-
.join(' or ')} used in ${toMessageRelativeFilePath(
|
|
73
|
-
sourceFilePath,
|
|
74
|
-
)} not found.`,
|
|
75
|
-
);
|
|
76
102
|
}
|
|
77
103
|
|
|
78
|
-
async function processImageNode(
|
|
79
|
-
node: Image,
|
|
80
|
-
{filePath, staticDirs}: PluginOptions,
|
|
81
|
-
) {
|
|
104
|
+
async function processImageNode(node: Image, options: PluginOptions) {
|
|
82
105
|
if (!node.url) {
|
|
83
106
|
throw new Error(
|
|
84
107
|
`Markdown image URL is mandatory in "${toMessageRelativeFilePath(
|
|
85
|
-
filePath,
|
|
108
|
+
options.filePath,
|
|
86
109
|
)}" file`,
|
|
87
110
|
);
|
|
88
111
|
}
|
|
89
112
|
|
|
90
113
|
const parsedUrl = url.parse(node.url);
|
|
91
|
-
if (parsedUrl.protocol) {
|
|
114
|
+
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
92
115
|
// pathname:// is an escape hatch,
|
|
93
116
|
// in case user does not want his images to be converted to require calls going through webpack loader
|
|
94
117
|
// we don't have to document this for now,
|
|
@@ -96,24 +119,11 @@ async function processImageNode(
|
|
|
96
119
|
if (parsedUrl.protocol === 'pathname:') {
|
|
97
120
|
node.url = node.url.replace('pathname://', '');
|
|
98
121
|
}
|
|
122
|
+
return;
|
|
99
123
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const possibleImagePaths = staticDirs.map((dir) =>
|
|
104
|
-
path.join(dir, node.url),
|
|
105
|
-
);
|
|
106
|
-
const imagePath = await findImage(possibleImagePaths, filePath);
|
|
107
|
-
createJSX(node, posixPath(imagePath));
|
|
108
|
-
}
|
|
109
|
-
// We try to convert image urls without protocol to images with require calls
|
|
110
|
-
// going through webpack ensures that image assets exist at build time
|
|
111
|
-
else {
|
|
112
|
-
// relative paths are resolved against the source file's folder
|
|
113
|
-
const expectedImagePath = path.join(path.dirname(filePath), node.url);
|
|
114
|
-
await ensureImageFileExist(expectedImagePath, filePath);
|
|
115
|
-
createJSX(node, node.url.startsWith('./') ? node.url : `./${node.url}`);
|
|
116
|
-
}
|
|
124
|
+
|
|
125
|
+
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, options);
|
|
126
|
+
toImageRequireNode(node, imagePath, options.filePath);
|
|
117
127
|
}
|
|
118
128
|
|
|
119
129
|
const plugin: Plugin<[PluginOptions]> = (options) => {
|
|
@@ -23,7 +23,6 @@ import type {Link, Literal} from 'mdast';
|
|
|
23
23
|
const {
|
|
24
24
|
loaders: {inlineMarkdownLinkFileLoader},
|
|
25
25
|
} = getFileLoaderUtils();
|
|
26
|
-
const hashRegex = /#.*$/;
|
|
27
26
|
|
|
28
27
|
interface PluginOptions {
|
|
29
28
|
filePath: string;
|
|
@@ -31,103 +30,69 @@ interface PluginOptions {
|
|
|
31
30
|
siteDir: string;
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
async function ensureAssetFileExist(
|
|
35
|
-
fileSystemAssetPath: string,
|
|
36
|
-
sourceFilePath: string,
|
|
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
|
-
}
|
|
47
|
-
|
|
48
33
|
// 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),
|
|
34
|
+
function toAssetRequireNode(node: Link, assetPath: string, filePath: string) {
|
|
35
|
+
const jsxNode = node as Literal & Partial<Link>;
|
|
36
|
+
let relativeAssetPath = posixPath(
|
|
37
|
+
path.relative(path.dirname(filePath), assetPath),
|
|
60
38
|
);
|
|
61
|
-
const hash = hashRegex.test(node.url)
|
|
62
|
-
? node.url.substring(node.url.indexOf('#'))
|
|
63
|
-
: '';
|
|
64
|
-
|
|
65
39
|
// require("assets/file.pdf") means requiring from a package called assets
|
|
66
|
-
|
|
67
|
-
? relativeRequireAssetPath
|
|
68
|
-
: `./${relativeRequireAssetPath}`;
|
|
40
|
+
relativeAssetPath = `./${relativeAssetPath}`;
|
|
69
41
|
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
|
|
42
|
+
const parsedUrl = url.parse(node.url);
|
|
43
|
+
const hash = parsedUrl.hash ?? '';
|
|
44
|
+
const search = parsedUrl.search ?? '';
|
|
45
|
+
|
|
46
|
+
const href = `require('${inlineMarkdownLinkFileLoader}${
|
|
47
|
+
escapePath(relativeAssetPath) + search
|
|
48
|
+
}').default${hash ? ` + '${hash}'` : ''}`;
|
|
73
49
|
const children = stringifyContent(node);
|
|
74
50
|
const title = node.title ? ` title="${escapeHtml(node.title)}"` : '';
|
|
75
51
|
|
|
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;
|
|
52
|
+
Object.keys(jsxNode).forEach(
|
|
53
|
+
(key) => delete jsxNode[key as keyof typeof jsxNode],
|
|
54
|
+
);
|
|
96
55
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
56
|
+
(jsxNode as Literal).type = 'jsx';
|
|
57
|
+
jsxNode.value = `<a target="_blank" href={${href}}${title}>${children}</a>`;
|
|
58
|
+
}
|
|
100
59
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
60
|
+
async function ensureAssetFileExist(assetPath: string, sourceFilePath: string) {
|
|
61
|
+
const assetExists = await fs.pathExists(assetPath);
|
|
62
|
+
if (!assetExists) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Asset ${toMessageRelativeFilePath(
|
|
65
|
+
assetPath,
|
|
66
|
+
)} used in ${toMessageRelativeFilePath(sourceFilePath)} not found.`,
|
|
67
|
+
);
|
|
107
68
|
}
|
|
69
|
+
}
|
|
108
70
|
|
|
71
|
+
async function getAssetAbsolutePath(
|
|
72
|
+
assetPath: string,
|
|
73
|
+
{siteDir, filePath, staticDirs}: PluginOptions,
|
|
74
|
+
) {
|
|
109
75
|
if (assetPath.startsWith('@site/')) {
|
|
110
|
-
const
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
toAssetLinkNode(fileSystemAssetPath);
|
|
76
|
+
const assetFilePath = path.join(siteDir, assetPath.replace('@site/', ''));
|
|
77
|
+
// The @site alias is the only way to believe that the user wants an asset.
|
|
78
|
+
// Everything else can just be a link URL
|
|
79
|
+
await ensureAssetFileExist(assetFilePath, filePath);
|
|
80
|
+
return assetFilePath;
|
|
116
81
|
} else if (path.isAbsolute(assetPath)) {
|
|
117
82
|
// eslint-disable-next-line no-restricted-syntax
|
|
118
83
|
for (const staticDir of staticDirs) {
|
|
119
|
-
const
|
|
120
|
-
if (await fs.pathExists(
|
|
121
|
-
|
|
122
|
-
return;
|
|
84
|
+
const assetFilePath = path.join(staticDir, assetPath);
|
|
85
|
+
if (await fs.pathExists(assetFilePath)) {
|
|
86
|
+
return assetFilePath;
|
|
123
87
|
}
|
|
124
88
|
}
|
|
125
89
|
} else {
|
|
126
|
-
const
|
|
127
|
-
if (await fs.pathExists(
|
|
128
|
-
|
|
90
|
+
const assetFilePath = path.join(path.dirname(filePath), assetPath);
|
|
91
|
+
if (await fs.pathExists(assetFilePath)) {
|
|
92
|
+
return assetFilePath;
|
|
129
93
|
}
|
|
130
94
|
}
|
|
95
|
+
return null;
|
|
131
96
|
}
|
|
132
97
|
|
|
133
98
|
async function processLinkNode(node: Link, options: PluginOptions) {
|
|
@@ -144,11 +109,22 @@ async function processLinkNode(node: Link, options: PluginOptions) {
|
|
|
144
109
|
}
|
|
145
110
|
|
|
146
111
|
const parsedUrl = url.parse(node.url);
|
|
147
|
-
if (parsedUrl.protocol) {
|
|
112
|
+
if (parsedUrl.protocol || !parsedUrl.pathname) {
|
|
113
|
+
// Don't process pathname:// here, it's used by the <Link> component
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const hasSiteAlias = parsedUrl.pathname.startsWith('@site/');
|
|
117
|
+
const hasAssetLikeExtension =
|
|
118
|
+
path.extname(parsedUrl.pathname) &&
|
|
119
|
+
!parsedUrl.pathname.match(/\.(?:mdx?|html)(?:#|$)/);
|
|
120
|
+
if (!hasSiteAlias && !hasAssetLikeExtension) {
|
|
148
121
|
return;
|
|
149
122
|
}
|
|
150
123
|
|
|
151
|
-
await
|
|
124
|
+
const assetPath = await getAssetAbsolutePath(parsedUrl.pathname, options);
|
|
125
|
+
if (assetPath) {
|
|
126
|
+
toAssetRequireNode(node, assetPath, options.filePath);
|
|
127
|
+
}
|
|
152
128
|
}
|
|
153
129
|
|
|
154
130
|
const plugin: Plugin<[PluginOptions]> = (options) => {
|