13
13
14
14
extern crate markdown;
15
15
mod configuration;
16
- mod hast;
16
+ pub mod hast;
17
17
mod hast_util_to_swc;
18
18
mod mdast_util_to_hast;
19
19
mod mdx_plugin_recma_document;
@@ -23,17 +23,25 @@ mod swc_util_build_jsx;
23
23
mod swc_utils;
24
24
25
25
use crate :: {
26
- hast_util_to_swc:: hast_util_to_swc,
27
- mdast_util_to_hast:: mdast_util_to_hast,
28
- mdx_plugin_recma_document:: { mdx_plugin_recma_document, Options as DocumentOptions } ,
29
- mdx_plugin_recma_jsx_rewrite:: { mdx_plugin_recma_jsx_rewrite, Options as RewriteOptions } ,
26
+ hast_util_to_swc:: hast_util_to_swc as to_swc,
27
+ mdx_plugin_recma_document:: {
28
+ mdx_plugin_recma_document as wrap_document, Options as DocumentOptions ,
29
+ } ,
30
+ mdx_plugin_recma_jsx_rewrite:: {
31
+ mdx_plugin_recma_jsx_rewrite as jsx_rewrite, Options as RewriteOptions ,
32
+ } ,
30
33
swc:: { parse_esm, parse_expression, serialize} ,
31
34
swc_util_build_jsx:: { swc_util_build_jsx, Options as BuildOptions } ,
32
35
} ;
33
- use markdown:: { message, to_mdast, Constructs , Location , ParseOptions } ;
34
- use swc_core:: alloc:: collections:: FxHashSet ;
36
+ use hast_util_to_swc:: Program ;
37
+ use markdown:: {
38
+ message:: { self , Message } ,
39
+ to_mdast, Constructs , Location , ParseOptions ,
40
+ } ;
41
+ use swc_core:: { alloc:: collections:: FxHashSet , common:: Span } ;
35
42
36
43
pub use crate :: configuration:: { MdxConstructs , MdxParseOptions , Options } ;
44
+ pub use crate :: mdast_util_to_hast:: mdast_util_to_hast;
37
45
pub use crate :: mdx_plugin_recma_document:: JsxRuntime ;
38
46
39
47
/// Turn MDX into JavaScript.
@@ -54,6 +62,40 @@ pub use crate::mdx_plugin_recma_document::JsxRuntime;
54
62
/// This project errors for many different reasons, such as syntax errors in
55
63
/// the MDX format or misconfiguration.
56
64
pub fn compile ( value : & str , options : & Options ) -> Result < String , message:: Message > {
65
+ let mdast = mdast_util_from_mdx ( value, options) ?;
66
+ let hast = mdast_util_to_hast ( & mdast) ;
67
+ let location = Location :: new ( value. as_bytes ( ) ) ;
68
+ let mut explicit_jsxs = FxHashSet :: default ( ) ;
69
+ let mut program = hast_util_to_swc ( & hast, options, Some ( & location) , & mut explicit_jsxs) ?;
70
+ mdx_plugin_recma_document ( & mut program, options, Some ( & location) ) ?;
71
+ mdx_plugin_recma_jsx_rewrite ( & mut program, options, Some ( & location) , & explicit_jsxs) ?;
72
+ Ok ( serialize ( & mut program. module , Some ( & program. comments ) ) )
73
+ }
74
+
75
+ /// Turn markdown into a syntax tree.
76
+ ///
77
+ /// ## Errors
78
+ ///
79
+ /// There are several errors that can occur with how
80
+ /// JSX, expressions, or ESM are written.
81
+ ///
82
+ /// ## Examples
83
+ ///
84
+ /// ```
85
+ /// use mdxjs::{mdast_util_from_mdx, Options};
86
+ /// # fn main() -> Result<(), markdown::message::Message> {
87
+ ///
88
+ /// let tree = mdast_util_from_mdx("# Hey, *you*!", &Options::default())?;
89
+ ///
90
+ /// println!("{:?}", tree);
91
+ /// // => Root { children: [Heading { children: [Text { value: "Hey, ", position: Some(1:3-1:8 (2-7)) }, Emphasis { children: [Text { value: "you", position: Some(1:9-1:12 (8-11)) }], position: Some(1:8-1:13 (7-12)) }, Text { value: "!", position: Some(1:13-1:14 (12-13)) }], position: Some(1:1-1:14 (0-13)), depth: 1 }], position: Some(1:1-1:14 (0-13)) }
92
+ /// # Ok(())
93
+ /// # }
94
+ /// ```
95
+ pub fn mdast_util_from_mdx (
96
+ value : & str ,
97
+ options : & Options ,
98
+ ) -> Result < markdown:: mdast:: Node , Message > {
57
99
let parse_options = ParseOptions {
58
100
constructs : Constructs {
59
101
attention : options. parse . constructs . attention ,
@@ -96,43 +138,73 @@ pub fn compile(value: &str, options: &Options) -> Result<String, message::Messag
96
138
mdx_esm_parse : Some ( Box :: new ( parse_esm) ) ,
97
139
mdx_expression_parse : Some ( Box :: new ( parse_expression) ) ,
98
140
} ;
141
+
142
+ to_mdast ( value, & parse_options)
143
+ }
144
+
145
+ /// Compile hast into SWC’s ES AST.
146
+ ///
147
+ /// ## Errors
148
+ ///
149
+ /// This project errors for many different reasons, such as syntax errors in
150
+ /// the MDX format or misconfiguration.
151
+ pub fn hast_util_to_swc (
152
+ hast : & hast:: Node ,
153
+ options : & Options ,
154
+ location : Option < & Location > ,
155
+ explicit_jsxs : & mut FxHashSet < Span > ,
156
+ ) -> Result < Program , markdown:: message:: Message > {
157
+ to_swc ( hast, options. filepath . clone ( ) , location, explicit_jsxs)
158
+ }
159
+
160
+ /// Wrap the SWC ES AST nodes coming from hast into a whole document.
161
+ ///
162
+ /// ## Errors
163
+ ///
164
+ /// This project errors for many different reasons, such as syntax errors in
165
+ /// the MDX format or misconfiguration.
166
+ pub fn mdx_plugin_recma_document (
167
+ program : & mut Program ,
168
+ options : & Options ,
169
+ location : Option < & Location > ,
170
+ ) -> Result < ( ) , markdown:: message:: Message > {
99
171
let document_options = DocumentOptions {
100
172
pragma : options. pragma . clone ( ) ,
101
173
pragma_frag : options. pragma_frag . clone ( ) ,
102
174
pragma_import_source : options. pragma_import_source . clone ( ) ,
103
175
jsx_import_source : options. jsx_import_source . clone ( ) ,
104
176
jsx_runtime : options. jsx_runtime ,
105
177
} ;
178
+ wrap_document ( program, & document_options, location)
179
+ }
180
+
181
+ /// Rewrite JSX in an MDX file so that components can be passed in and provided.
182
+ /// Also compiles JSX to function calls unless `options.jsx` is true.
183
+ ///
184
+ /// ## Errors
185
+ ///
186
+ /// This project errors for many different reasons, such as syntax errors in
187
+ /// the MDX format or misconfiguration.
188
+ pub fn mdx_plugin_recma_jsx_rewrite (
189
+ program : & mut Program ,
190
+ options : & Options ,
191
+ location : Option < & Location > ,
192
+ explicit_jsxs : & FxHashSet < Span > ,
193
+ ) -> Result < ( ) , markdown:: message:: Message > {
106
194
let rewrite_options = RewriteOptions {
107
195
development : options. development ,
108
196
provider_import_source : options. provider_import_source . clone ( ) ,
109
197
} ;
110
- let build_options = BuildOptions {
111
- development : options. development ,
112
- } ;
113
198
114
- let mut explicit_jsxs = FxHashSet :: default ( ) ;
115
-
116
- let location = Location :: new ( value. as_bytes ( ) ) ;
117
- let mdast = to_mdast ( value, & parse_options) ?;
118
- let hast = mdast_util_to_hast ( & mdast) ;
119
- let mut program = hast_util_to_swc (
120
- & hast,
121
- options. filepath . clone ( ) ,
122
- Some ( & location) ,
123
- & mut explicit_jsxs,
124
- ) ?;
125
- mdx_plugin_recma_document ( & mut program, & document_options, Some ( & location) ) ?;
126
- mdx_plugin_recma_jsx_rewrite (
127
- & mut program,
128
- & rewrite_options,
129
- Some ( & location) ,
130
- explicit_jsxs,
131
- ) ;
199
+ jsx_rewrite ( program, & rewrite_options, location, explicit_jsxs) ;
132
200
133
201
if !options. jsx {
134
- swc_util_build_jsx ( & mut program, & build_options, Some ( & location) ) ?;
202
+ let build_options = BuildOptions {
203
+ development : options. development ,
204
+ } ;
205
+
206
+ swc_util_build_jsx ( program, & build_options, location) ?;
135
207
}
136
208
137
- Ok ( serialize ( & mut program . module , Some ( & program . comments ) ) )
209
+ Ok ( ( ) )
138
210
}
0 commit comments