1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
extern crate micromark;
extern crate swc_common;
extern crate swc_ecma_ast;
extern crate swc_ecma_codegen;
mod test_utils;
use pretty_assertions::assert_eq;
use test_utils::mdx::{mdx, JsxRuntime, Options};
#[test]
fn mdx_test() -> Result<(), String> {
// To do: JSX should be compiled away.
assert_eq!(
mdx("", Some("example.mdx".into()), &Options::default())?,
"function _createMdxContent(props) {
return <></>;
}
function MDXContent(props = {}) {
const { wrapper: MDXLayout } = props.components || {};
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props}/></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
",
"should work",
);
// To do: JSX should be compiled away.
assert_eq!(
mdx("<A />", Some("example.mdx".into()), &Options {
development: true,
..Default::default()
})?,
"function _createMdxContent(props) {
const { A } = props.components || {};
if (!A) _missingMdxReference(\"A\", true, \"1:1-1:6\");
return <A />;
}
function MDXContent(props = {}) {
const { wrapper: MDXLayout } = props.components || {};
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props}/></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
function _missingMdxReference(id, component, place) {
throw new Error(\"Expected \" + (component ? \"component\" : \"object\") + \" `\" + id + \"` to be defined: you likely forgot to import, pass, or provide it.\" + (place ? \"\\nIt’s referenced in your code at `\" + place + \"` in `example.mdx`\" : \"\"));
}
",
"should support `options.development: true`",
);
// To do: JSX should be compiled away.
assert_eq!(
mdx("<A />", Some("example.mdx".into()), &Options {
provider_import_source: Some("@mdx-js/react".into()),
..Default::default()
})?,
"import { useMDXComponents as _provideComponents } from \"@mdx-js/react\";
function _createMdxContent(props) {
const { A } = Object.assign({}, _provideComponents(), props.components);
if (!A) _missingMdxReference(\"A\", true);
return <A />;
}
function MDXContent(props = {}) {
const { wrapper: MDXLayout } = Object.assign({}, _provideComponents(), props.components);
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props}/></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
function _missingMdxReference(id, component) {
throw new Error(\"Expected \" + (component ? \"component\" : \"object\") + \" `\" + id + \"` to be defined: you likely forgot to import, pass, or provide it.\");
}
",
"should support `options.provider_import_source`",
);
assert_eq!(
mdx("", Some("example.mdx".into()), &Options {
jsx: true,
..Default::default()
})?,
"function _createMdxContent(props) {
return <></>;
}
function MDXContent(props = {}) {
const { wrapper: MDXLayout } = props.components || {};
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props}/></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
",
"should support `options.jsx: true`",
);
// To do: JSX should be compiled away.
// To do: should use calls of `React.createElement` / `React.Fragment`.
assert_eq!(
mdx("", Some("example.mdx".into()), &Options {
jsx_runtime: Some(JsxRuntime::Classic),
..Default::default()
})?,
"import { React } from \"react\";
function _createMdxContent(props) {
return <></>;
}
function MDXContent(props = {}) {
const { wrapper: MDXLayout } = props.components || {};
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props}/></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
",
"should support `options.jsx_runtime: JsxRuntime::Classic`",
);
// To do: JSX should be compiled away.
// To do: should import `_jsx` and such.
// To do: should use calls of `_jsx`, etc.
assert_eq!(
mdx("", Some("example.mdx".into()), &Options {
jsx_import_source: Some("preact".into()),
..Default::default()
})?,
"function _createMdxContent(props) {
return <></>;
}
function MDXContent(props = {}) {
const { wrapper: MDXLayout } = props.components || {};
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props}/></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
",
"should support `options.jsx_import_source: Some(\"preact\".into())`",
);
// To do: JSX should be compiled away.
// To do: should use calls of `a.b`, symbol of `a.c`.
assert_eq!(
mdx("", Some("example.mdx".into()), &Options {
jsx_runtime: Some(JsxRuntime::Classic),
pragma: Some("a.b".into()),
pragma_frag: Some("a.c".into()),
pragma_import_source: Some("d".into()),
..Default::default()
})?,
"import { a } from \"d\";
function _createMdxContent(props) {
return <></>;
}
function MDXContent(props = {}) {
const { wrapper: MDXLayout } = props.components || {};
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props}/></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
",
"should support `options.pragma`, `options.pragma_frag`, `options.pragma_import_source`",
);
Ok(())
}
|