Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit b2a7002

Browse files
committed
feat(mdx): add image props(w, h)
1 parent ba5888b commit b2a7002

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

packages/mdx-loader/__tests__/__snapshots__/index.js.snap

+16-2
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,14 @@ export const slides = [props => <>
802802
<p><img src={require('/tmp/withAlt.jpg')} {...{
803803
\\"alt\\": \\"Alt\\"
804804
}}></img>{\`
805-
\`}<img src={require('/tmp/withoutAlt.jpg')}></img></p>
805+
\`}<img src={require('/tmp/withoutAlt.jpg')}></img>{\`
806+
\`}<img src={require('/tmp/style.jpg')} style={{
807+
\\"height\\": \\"100%\\"
808+
}}></img>{\`
809+
\`}<img src={require('/tmp/style.jpg?dont=delete')} style={{
810+
\\"height\\": \\"100px\\",
811+
\\"width\\": \\"100%\\"
812+
}}></img></p>
806813

807814
</>];
808815
export const backgrounds = [0];
@@ -820,7 +827,14 @@ export default function MDXContent({
820827
<p><img src={require('/tmp/withAlt.jpg')} {...{
821828
\\"alt\\": \\"Alt\\"
822829
}}></img>{\`
823-
\`}<img src={require('/tmp/withoutAlt.jpg')}></img></p>
830+
\`}<img src={require('/tmp/withoutAlt.jpg')}></img>{\`
831+
\`}<img src={require('/tmp/style.jpg')} style={{
832+
\\"height\\": \\"100%\\"
833+
}}></img>{\`
834+
\`}<img src={require('/tmp/style.jpg?dont=delete')} style={{
835+
\\"height\\": \\"100px\\",
836+
\\"width\\": \\"100%\\"
837+
}}></img></p>
824838

825839
</MDXLayout>;
826840
}

packages/mdx-loader/__tests__/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ second
142142
const src = `
143143
![Alt](/tmp/withAlt.jpg)
144144
![](/tmp/withoutAlt.jpg)
145+
![](/tmp/style.jpg?h=100%)
146+
![](/tmp/style.jpg?dont=delete&h=100px&w=100%)
145147
`;
146148
expect(await transformToJS(src)).toMatchSnapshot();
147149
});

packages/mdx-loader/src/transformers/transformMarkdownImageNodeToJSX.js

+35-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ const mdxAstToMdxHast = require('@mdx-js/mdx/mdx-ast-to-mdx-hast');
55

66
function transformMarkdownImageNodeToJSX(node) {
77
const hash = mdxAstToMdxHast()(node);
8-
const { src, alt } = hash.properties;
8+
let { src, alt } = hash.properties;
99
let jsx;
10+
const style = {};
1011

1112
if (alt === null || alt === undefined) {
1213
delete hash.properties.alt;
@@ -15,7 +16,39 @@ function transformMarkdownImageNodeToJSX(node) {
1516
// Do not resolve remote url as a module
1617
if (!src.includes('http')) {
1718
delete hash.properties.src;
18-
jsx = toJSX(hash).replace(/<img(\s?.*)>/, `<img src={require('${src}')} $1>`);
19+
20+
const match = src.match(/(\?|&)?([w,h]=.+?(px|%))&?/gm);
21+
22+
if (match) {
23+
// has.properties.src attaches `25` when fusuma uses %
24+
src = src.replace(/(\?|&)?([w,h]=.+?(px|%))&?/gm, '').replace(/(25)+$/g, '');
25+
26+
match.forEach((v) => {
27+
const [key, value] = v.split('=').map((item, i) => {
28+
if (i === 0) {
29+
let k = item.replace(/[?|&]/g, '');
30+
31+
if (k === 'w') {
32+
return 'width';
33+
}
34+
if (k === 'h') {
35+
return 'height';
36+
}
37+
}
38+
if (i === 1) {
39+
return item.replace('&', '');
40+
}
41+
});
42+
style[key] = value;
43+
});
44+
}
45+
46+
jsx = toJSX(hash).replace(
47+
/<img(\s?.*)>/,
48+
`<img src={require('${src}')} ${
49+
Object.keys(style).length ? `style={${JSON.stringify(style)}}` : ''
50+
} $1>`
51+
);
1952
} else {
2053
jsx = toJSX(hash);
2154
}

samples/debug/slides/02-file-loader.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
### markdown
88

9-
![js](../assets/js.jpg)
9+
![js](../assets/js.jpg?w=150px&h=150px)
1010

1111
<!-- block-end -->
1212
<!-- block-start: column -->
1313

1414
### html
1515

16-
<p><img src="../assets/js.jpg" alt="js" /></p>
16+
<p><img src="../assets/js.jpg" alt="js" style={{width:150, height:150}} /></p>
1717

1818
<!-- block-end -->
1919
<!-- block-end -->

0 commit comments

Comments
 (0)