1
1
import { describe , expect , test } from 'vitest' ;
2
2
import { createMdxTransformer } from './mdx' ;
3
3
4
- describe ( 'mdx' , ( ) => {
5
- test ( 'convert mdx' , async ( ) => {
6
- const ctx = {
7
- frontmatter : new Map ( ) ,
8
- opts : {
9
- mdx : {
10
- remarkPlugins : [ ] ,
11
- rehypePlugins : [ ] ,
12
- } ,
13
- mdxPlugins : { } ,
4
+ // It could be that new MDX versions change the output used for snapshot matching. Change as needed.
5
+ describe ( 'mdx' , async ( ) => {
6
+ const ctx = {
7
+ frontmatter : new Map ( ) ,
8
+ opts : {
9
+ mdx : {
10
+ remarkPlugins : [ ] ,
11
+ rehypePlugins : [ ] ,
14
12
} ,
15
- } ;
16
- const transformer = await createMdxTransformer ( ctx as any ) ;
13
+ mdxPlugins : { } ,
14
+ } ,
15
+ } ;
16
+
17
+ const transformer = await createMdxTransformer ( ctx as any ) ;
18
+
19
+ test ( 'convert flat mdx' , async ( ) => {
17
20
const mdx = `
18
21
# Hello
19
22
<a href="http://example.com">Hello</a>
20
23
<div>World</div>
21
24
` ;
22
25
const result = await transformer ( mdx , 'file.mdx' ) ;
23
- // It could be that new mdx versions change this output, make sure it still makes sense
26
+
24
27
expect ( result ) . toMatchInlineSnapshot ( `
25
28
{
26
- "code": "import { _jsxC, RenderOnce } from '@builder.io/qwik';
29
+ "code": "import { jsx } from '@builder.io/qwik';
27
30
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "@builder.io/qwik/jsx-runtime";
28
31
export const headings = [{
29
32
"text": "Hello",
@@ -59,7 +62,8 @@ describe('mdx', () => {
59
62
}
60
63
61
64
const WrappedMdxContent = () => {
62
- return _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, "eB2HIyA1");
65
+ const content = _createMdxContent({});
66
+ return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content;
63
67
};
64
68
export default WrappedMdxContent;
65
69
",
@@ -75,4 +79,81 @@ describe('mdx', () => {
75
79
}
76
80
` ) ;
77
81
} ) ;
82
+
83
+ test ( 'convert layout mdx' , async ( ) => {
84
+ const mdx = `
85
+ # Hello
86
+
87
+ export default function Layout({ children: content }) {
88
+ return <main>{content}</main>;
89
+ }
90
+
91
+ <a href="http://example.com">Hello</a>
92
+ <div>World</div>
93
+ ` ;
94
+ const result = await transformer ( mdx , 'file.mdx' ) ;
95
+
96
+ expect ( result ) . toMatchInlineSnapshot ( `
97
+ {
98
+ "code": "import { jsx } from '@builder.io/qwik';
99
+ import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "@builder.io/qwik/jsx-runtime";
100
+ export const headings = [{
101
+ "text": "Hello",
102
+ "id": "hello",
103
+ "level": 1
104
+ }];
105
+ export const frontmatter = undefined;
106
+ const MDXLayout = function Layout({children: content}) {
107
+ return _jsx("main", {
108
+ children: content
109
+ });
110
+ };
111
+ function _createMdxContent(props) {
112
+ const _components = {
113
+ a: "a",
114
+ h1: "h1",
115
+ span: "span",
116
+ ...props.components
117
+ };
118
+ return _jsxs(_Fragment, {
119
+ children: [_jsxs(_components.h1, {
120
+ id: "hello",
121
+ children: [_jsx(_components.a, {
122
+ "aria-hidden": "true",
123
+ tabindex: "-1",
124
+ href: "#hello",
125
+ children: _jsx(_components.span, {
126
+ class: "icon icon-link"
127
+ })
128
+ }), "Hello"]
129
+ }), "\\n", "\\n", _jsx("a", {
130
+ href: "http://example.com",
131
+ children: "Hello"
132
+ }), "\\n", _jsx("div", {
133
+ children: "World"
134
+ })]
135
+ });
136
+ }
137
+
138
+ const WrappedMdxContent = () => {
139
+ const content = _createMdxContent({});
140
+ return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content;
141
+ };
142
+ export default WrappedMdxContent;
143
+ ",
144
+ "map": {
145
+ "file": "file.mdx",
146
+ "mappings": ";;;;;;;kBAGe,iBAAkBA,UAAUC;cACjC;cAAMA;;;;;;;;;;;;;;;;;;;;UAHd;;;gBAM2B;;gBACxB",
147
+ "names": [
148
+ "children",
149
+ "content",
150
+ ],
151
+ "sources": [
152
+ "file.mdx",
153
+ ],
154
+ "version": 3,
155
+ },
156
+ }
157
+ ` ) ;
158
+ } ) ;
78
159
} ) ;
0 commit comments