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

Commit e12e821

Browse files
arayaryomahiroppy
arayaryoma
authored andcommitted
fix: the image wrttien in markdown-syntax ![]() is not rendered
1 parent ce6665a commit e12e821

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

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

+47
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,53 @@ export default function MDXContent({
608608
MDXContent.isMDXComponent = true;"
609609
`;
610610

611+
exports[`fusuma-loader should convert markdown-syntax image to JSX 1`] = `
612+
"/* @jsx mdx */
613+
614+
615+
import React from 'react';
616+
import { mdx } from '@mdx-js/react';
617+
export const slides = [
618+
(props) => (
619+
<>
620+
621+
<p><img src={require('/tmp/withAlt.jpg')} {...{
622+
\\"alt\\": \\"Alt\\"
623+
}}></img><br parentName=\\"p\\"></br>{\`
624+
\`}<img src={require('/tmp/withoutAlt.jpg')} {...{
625+
\\"alt\\": null
626+
}}></img></p>
627+
628+
</>
629+
)];
630+
export const fusumaProps = [{}];
631+
const makeShortcode = name => function MDXDefaultShortcode(props) {
632+
console.warn(\\"Component \\" + name + \\" was not imported, exported, or provided by MDXProvider as global scope\\")
633+
return <div {...props}/>
634+
};
635+
636+
const layoutProps = {
637+
slides
638+
};
639+
const MDXLayout = \\"wrapper\\"
640+
export default function MDXContent({
641+
components,
642+
...props
643+
}) {
644+
return <MDXLayout {...layoutProps} {...props} components={components} mdxType=\\"MDXLayout\\">
645+
<p><img src={require('/tmp/withAlt.jpg')} {...{
646+
\\"alt\\": \\"Alt\\"
647+
}}></img><br parentName=\\"p\\"></br>{\`
648+
\`}<img src={require('/tmp/withoutAlt.jpg')} {...{
649+
\\"alt\\": null
650+
}}></img></p>
651+
652+
</MDXLayout>;
653+
}
654+
655+
MDXContent.isMDXComponent = true;"
656+
`;
657+
611658
exports[`fusuma-loader should convert mermaid 1`] = `
612659
"/* @jsx mdx */
613660

packages/mdx-loader/__tests__/index.js

+8
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ first
135135
\`\`\`ts line="10-100"
136136
second
137137
\`\`\`
138+
`;
139+
expect(await transformToJS(src)).toMatchSnapshot();
140+
});
141+
142+
test('should convert markdown-syntax image to JSX', async () => {
143+
const src = `
144+
![Alt](/tmp/withAlt.jpg)
145+
![](/tmp/withoutAlt.jpg)
138146
`;
139147
expect(await transformToJS(src)).toMatchSnapshot();
140148
});

packages/mdx-loader/src/mdxPlugin.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const visit = require('unist-util-visit');
4+
35
const mdxAstToMdxHast = require('@mdx-js/mdx/mdx-ast-to-mdx-hast');
46
const { toJSX } = require('@mdx-js/mdx/mdx-hast-to-jsx');
57

@@ -52,6 +54,25 @@ function createFusumaProps(nodes) {
5254
.join(',')}}`;
5355
}
5456

57+
function transferMarkdownImageNodeToJSX(node) {
58+
const hash = mdxAstToMdxHast()(node);
59+
const { src } = hash.properties;
60+
let jsx;
61+
62+
// Do not resolve remote url as a module
63+
if (src.indexOf('http') < 0) {
64+
delete hash.properties.src;
65+
jsx = toJSX(hash).replace(/<img\s(.*)>/, `<img src={require('${src}')} $1>`);
66+
} else {
67+
jsx = toJSX(hash);
68+
}
69+
70+
return {
71+
type: 'jsx',
72+
value: jsx
73+
};
74+
}
75+
5576
function mdxPlugin() {
5677
return (tree, file) => {
5778
const slides = [];
@@ -115,6 +136,17 @@ function mdxPlugin() {
115136
});
116137
}
117138
} else {
139+
visit(n, null, (node) => {
140+
if (node.type === 'image' || node.type === 'img') {
141+
const { type, value } = transferMarkdownImageNodeToJSX(node);
142+
node.type = type;
143+
node.value = value;
144+
delete node.alt;
145+
delete node.title;
146+
delete node.url;
147+
}
148+
});
149+
118150
slide.push(n);
119151

120152
if (n.type === 'jsx') {
@@ -134,10 +166,8 @@ function mdxPlugin() {
134166
children: slide
135167
});
136168
const mdxJSX = toJSX(hash);
137-
138169
// jsx variable is established, so we don't use babel/parser
139170
const jsx = mdxJSX.match(/<MDXLayout.+?>([\s\S]*)<\/MDXLayout>/m);
140-
141171
if (jsx) {
142172
const template = `
143173
(props) => (

0 commit comments

Comments
 (0)