This repository was archived by the owner on Dec 5, 2024. It is now read-only.
Commit e12e821 1 parent ce6665a commit e12e821 Copy full SHA for e12e821
File tree 3 files changed +87
-2
lines changed
3 files changed +87
-2
lines changed Original file line number Diff line number Diff line change @@ -608,6 +608,53 @@ export default function MDXContent({
608
608
MDXContent.isMDXComponent = true;"
609
609
`;
610
610
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
+
611
658
exports[`fusuma-loader should convert mermaid 1`] = `
612
659
"/* @jsx mdx */
613
660
Original file line number Diff line number Diff line change @@ -135,6 +135,14 @@ first
135
135
\`\`\`ts line="10-100"
136
136
second
137
137
\`\`\`
138
+ ` ;
139
+ expect ( await transformToJS ( src ) ) . toMatchSnapshot ( ) ;
140
+ } ) ;
141
+
142
+ test ( 'should convert markdown-syntax image to JSX' , async ( ) => {
143
+ const src = `
144
+ 
145
+ 
138
146
` ;
139
147
expect ( await transformToJS ( src ) ) . toMatchSnapshot ( ) ;
140
148
} ) ;
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
3
+ const visit = require ( 'unist-util-visit' ) ;
4
+
3
5
const mdxAstToMdxHast = require ( '@mdx-js/mdx/mdx-ast-to-mdx-hast' ) ;
4
6
const { toJSX } = require ( '@mdx-js/mdx/mdx-hast-to-jsx' ) ;
5
7
@@ -52,6 +54,25 @@ function createFusumaProps(nodes) {
52
54
. join ( ',' ) } }`;
53
55
}
54
56
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 ( / < i m g \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
+
55
76
function mdxPlugin ( ) {
56
77
return ( tree , file ) => {
57
78
const slides = [ ] ;
@@ -115,6 +136,17 @@ function mdxPlugin() {
115
136
} ) ;
116
137
}
117
138
} 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
+
118
150
slide . push ( n ) ;
119
151
120
152
if ( n . type === 'jsx' ) {
@@ -134,10 +166,8 @@ function mdxPlugin() {
134
166
children : slide
135
167
} ) ;
136
168
const mdxJSX = toJSX ( hash ) ;
137
-
138
169
// jsx variable is established, so we don't use babel/parser
139
170
const jsx = mdxJSX . match ( / < M D X L a y o u t .+ ?> ( [ \s \S ] * ) < \/ M D X L a y o u t > / m) ;
140
-
141
171
if ( jsx ) {
142
172
const template = `
143
173
(props) => (
You can’t perform that action at this time.
0 commit comments