@docusaurus/mdx-loader 0.0.0-4438 → 0.0.0-4439
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.
|
@@ -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,UAAU,aAAa;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,CAAC;CACjB;AAqHD,QAAA,MAAM,MAAM,EAAE,MAAM,CAAC,CAAC,aAAa,CAAC,CASnC,CAAC;AAEF,eAAe,MAAM,CAAC"}
|
|
@@ -13,8 +13,11 @@ const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
|
13
13
|
const url_1 = (0, tslib_1.__importDefault)(require("url"));
|
|
14
14
|
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
|
+
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
|
-
function toImageRequireNode(node, imagePath, filePath) {
|
|
20
|
+
async function toImageRequireNode(node, imagePath, filePath) {
|
|
18
21
|
var _a, _b;
|
|
19
22
|
const jsxNode = node;
|
|
20
23
|
let relativeImagePath = (0, utils_1.posixPath)(path_1.default.relative(path_1.default.dirname(filePath), imagePath));
|
|
@@ -25,9 +28,24 @@ function toImageRequireNode(node, imagePath, filePath) {
|
|
|
25
28
|
const alt = node.alt ? `alt={"${(0, escape_html_1.default)(node.alt)}"} ` : '';
|
|
26
29
|
const src = `require("${inlineMarkdownImageFileLoader}${(0, utils_1.escapePath)(relativeImagePath) + search}").default${hash ? ` + '${hash}'` : ''}`;
|
|
27
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
|
+
}
|
|
41
|
+
}
|
|
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}`;
|
|
45
|
+
}
|
|
28
46
|
Object.keys(jsxNode).forEach((key) => delete jsxNode[key]);
|
|
29
47
|
jsxNode.type = 'jsx';
|
|
30
|
-
jsxNode.value = `<img ${alt}src={${src}}${title} />`;
|
|
48
|
+
jsxNode.value = `<img ${alt}src={${src}}${title}${width}${height} />`;
|
|
31
49
|
}
|
|
32
50
|
async function ensureImageFileExist(imagePath, sourceFilePath) {
|
|
33
51
|
const imageExists = await fs_extra_1.default.pathExists(imagePath);
|
|
@@ -77,7 +95,7 @@ async function processImageNode(node, options) {
|
|
|
77
95
|
return;
|
|
78
96
|
}
|
|
79
97
|
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, options);
|
|
80
|
-
toImageRequireNode(node, imagePath, options.filePath);
|
|
98
|
+
await toImageRequireNode(node, imagePath, options.filePath);
|
|
81
99
|
}
|
|
82
100
|
const plugin = (options) => {
|
|
83
101
|
const transformer = async (root) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/remark/transformImage/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAEH,6CAM2B;AAC3B,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,6CAM2B;AAC3B,qFAAqC;AACrC,6DAAwB;AACxB,2DAAsB;AACtB,qEAA0B;AAC1B,2EAAqC;AACrC,yEAAgC;AAChC,+BAA+B;AAG/B,6EAAwC;AAExC,MAAM,EACJ,OAAO,EAAE,EAAC,6BAA6B,EAAC,GACzC,GAAG,IAAA,0BAAkB,GAAE,CAAC;AAQzB,KAAK,UAAU,kBAAkB,CAC/B,IAAW,EACX,SAAiB,EACjB,QAAgB;;IAEhB,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,EAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;IAC/D,MAAM,GAAG,GAAG,YAAY,6BAA6B,GACnD,IAAA,kBAAU,EAAC,iBAA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B,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-4439",
|
|
4
4
|
"description": "Docusaurus Loader for MDX",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "src/mdx-loader.d.ts",
|
|
@@ -20,14 +20,15 @@
|
|
|
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-4439",
|
|
24
|
+
"@docusaurus/utils": "0.0.0-4439",
|
|
25
25
|
"@mdx-js/mdx": "^1.6.21",
|
|
26
26
|
"@mdx-js/react": "^1.6.21",
|
|
27
27
|
"escape-html": "^1.0.3",
|
|
28
28
|
"file-loader": "^6.2.0",
|
|
29
29
|
"fs-extra": "^10.0.0",
|
|
30
30
|
"gray-matter": "^4.0.3",
|
|
31
|
+
"image-size": "^1.0.1",
|
|
31
32
|
"mdast-util-to-string": "^2.0.0",
|
|
32
33
|
"remark-emoji": "^2.1.0",
|
|
33
34
|
"stringify-object": "^3.3.0",
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
"webpack": "^5.61.0"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
|
-
"@docusaurus/types": "0.0.0-
|
|
41
|
+
"@docusaurus/types": "0.0.0-4439",
|
|
41
42
|
"@types/escape-html": "^1.0.1",
|
|
42
43
|
"@types/mdast": "^3.0.7",
|
|
43
44
|
"@types/stringify-object": "^3.3.1",
|
|
@@ -55,5 +56,5 @@
|
|
|
55
56
|
"engines": {
|
|
56
57
|
"node": ">=14"
|
|
57
58
|
},
|
|
58
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "b7f060678d66a34460788300fc57ade9558e3807"
|
|
59
60
|
}
|
|
@@ -17,8 +17,11 @@ import path from 'path';
|
|
|
17
17
|
import url from 'url';
|
|
18
18
|
import fs from 'fs-extra';
|
|
19
19
|
import escapeHtml from 'escape-html';
|
|
20
|
+
import sizeOf from 'image-size';
|
|
21
|
+
import {promisify} from 'util';
|
|
20
22
|
import type {Plugin, Transformer} from 'unified';
|
|
21
23
|
import type {Image, Literal} from 'mdast';
|
|
24
|
+
import logger from '@docusaurus/logger';
|
|
22
25
|
|
|
23
26
|
const {
|
|
24
27
|
loaders: {inlineMarkdownImageFileLoader},
|
|
@@ -30,7 +33,11 @@ interface PluginOptions {
|
|
|
30
33
|
siteDir: string;
|
|
31
34
|
}
|
|
32
35
|
|
|
33
|
-
function toImageRequireNode(
|
|
36
|
+
async function toImageRequireNode(
|
|
37
|
+
node: Image,
|
|
38
|
+
imagePath: string,
|
|
39
|
+
filePath: string,
|
|
40
|
+
) {
|
|
34
41
|
const jsxNode = node as Literal & Partial<Image>;
|
|
35
42
|
let relativeImagePath = posixPath(
|
|
36
43
|
path.relative(path.dirname(filePath), imagePath),
|
|
@@ -46,13 +53,27 @@ function toImageRequireNode(node: Image, imagePath: string, filePath: string) {
|
|
|
46
53
|
escapePath(relativeImagePath) + search
|
|
47
54
|
}").default${hash ? ` + '${hash}'` : ''}`;
|
|
48
55
|
const title = node.title ? ` title="${escapeHtml(node.title)}"` : '';
|
|
56
|
+
let width = '';
|
|
57
|
+
let height = '';
|
|
58
|
+
try {
|
|
59
|
+
const size = (await promisify(sizeOf)(imagePath))!;
|
|
60
|
+
if (size.width) {
|
|
61
|
+
width = ` width="${size.width}"`;
|
|
62
|
+
}
|
|
63
|
+
if (size.height) {
|
|
64
|
+
height = ` height="${size.height}"`;
|
|
65
|
+
}
|
|
66
|
+
} catch (e) {
|
|
67
|
+
logger.error`The image at path=${imagePath} can't be read correctly. Please ensure it's a valid image.
|
|
68
|
+
${(e as Error).message}`;
|
|
69
|
+
}
|
|
49
70
|
|
|
50
71
|
Object.keys(jsxNode).forEach(
|
|
51
72
|
(key) => delete jsxNode[key as keyof typeof jsxNode],
|
|
52
73
|
);
|
|
53
74
|
|
|
54
75
|
(jsxNode as Literal).type = 'jsx';
|
|
55
|
-
jsxNode.value = `<img ${alt}src={${src}}${title} />`;
|
|
76
|
+
jsxNode.value = `<img ${alt}src={${src}}${title}${width}${height} />`;
|
|
56
77
|
}
|
|
57
78
|
|
|
58
79
|
async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
|
|
@@ -124,7 +145,7 @@ async function processImageNode(node: Image, options: PluginOptions) {
|
|
|
124
145
|
}
|
|
125
146
|
|
|
126
147
|
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, options);
|
|
127
|
-
toImageRequireNode(node, imagePath, options.filePath);
|
|
148
|
+
await toImageRequireNode(node, imagePath, options.filePath);
|
|
128
149
|
}
|
|
129
150
|
|
|
130
151
|
const plugin: Plugin<[PluginOptions]> = (options) => {
|