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
#![deny(clippy::pedantic)]
#![allow(clippy::must_use_candidate)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_precision_loss)]
extern crate markdown;
mod configuration;
mod hast;
mod hast_util_to_swc;
mod mdast_util_to_hast;
mod mdx_plugin_recma_document;
mod mdx_plugin_recma_jsx_rewrite;
mod swc;
mod swc_util_build_jsx;
mod swc_utils;
use crate::{
hast_util_to_swc::hast_util_to_swc,
mdast_util_to_hast::mdast_util_to_hast,
mdx_plugin_recma_document::{mdx_plugin_recma_document, Options as DocumentOptions},
mdx_plugin_recma_jsx_rewrite::{mdx_plugin_recma_jsx_rewrite, Options as RewriteOptions},
swc::{parse_esm, parse_expression, serialize},
swc_util_build_jsx::{swc_util_build_jsx, Options as BuildOptions},
};
use markdown::{to_mdast, Constructs, Location, ParseOptions};
pub use crate::configuration::{MdxConstructs, MdxParseOptions, Options};
pub use crate::mdx_plugin_recma_document::JsxRuntime;
pub fn compile(value: &str, options: &Options) -> Result<String, String> {
let parse_options = ParseOptions {
constructs: Constructs {
attention: options.parse.constructs.attention,
autolink: false,
block_quote: options.parse.constructs.block_quote,
character_escape: options.parse.constructs.character_escape,
character_reference: options.parse.constructs.character_reference,
code_fenced: options.parse.constructs.code_fenced,
code_indented: false,
code_text: options.parse.constructs.code_text,
definition: options.parse.constructs.definition,
frontmatter: options.parse.constructs.frontmatter,
gfm_autolink_literal: options.parse.constructs.gfm_autolink_literal,
gfm_footnote_definition: options.parse.constructs.gfm_footnote_definition,
gfm_label_start_footnote: options.parse.constructs.gfm_label_start_footnote,
gfm_strikethrough: options.parse.constructs.gfm_strikethrough,
gfm_table: options.parse.constructs.gfm_table,
gfm_task_list_item: options.parse.constructs.gfm_task_list_item,
hard_break_escape: options.parse.constructs.hard_break_escape,
hard_break_trailing: options.parse.constructs.hard_break_trailing,
html_flow: false,
html_text: false,
heading_atx: options.parse.constructs.heading_atx,
heading_setext: options.parse.constructs.heading_setext,
label_start_image: options.parse.constructs.label_start_image,
label_start_link: options.parse.constructs.label_start_link,
label_end: options.parse.constructs.label_end,
list_item: options.parse.constructs.list_item,
math_flow: options.parse.constructs.math_flow,
math_text: options.parse.constructs.math_text,
mdx_esm: true,
mdx_expression_flow: true,
mdx_expression_text: true,
mdx_jsx_flow: true,
mdx_jsx_text: true,
thematic_break: options.parse.constructs.thematic_break,
},
gfm_strikethrough_single_tilde: options.parse.gfm_strikethrough_single_tilde,
math_text_single_dollar: options.parse.math_text_single_dollar,
mdx_esm_parse: Some(Box::new(parse_esm)),
mdx_expression_parse: Some(Box::new(parse_expression)),
};
let document_options = DocumentOptions {
pragma: options.pragma.clone(),
pragma_frag: options.pragma_frag.clone(),
pragma_import_source: options.pragma_import_source.clone(),
jsx_import_source: options.jsx_import_source.clone(),
jsx_runtime: options.jsx_runtime,
};
let rewrite_options = RewriteOptions {
development: options.development,
provider_import_source: options.provider_import_source.clone(),
};
let build_options = BuildOptions {
development: options.development,
};
let location = Location::new(value.as_bytes());
let mdast = to_mdast(value, &parse_options)?;
let hast = mdast_util_to_hast(&mdast);
let mut program = hast_util_to_swc(&hast, options.filepath.clone(), Some(&location))?;
mdx_plugin_recma_document(&mut program, &document_options, Some(&location))?;
mdx_plugin_recma_jsx_rewrite(&mut program, &rewrite_options, Some(&location));
if !options.jsx {
swc_util_build_jsx(&mut program, &build_options, Some(&location))?;
}
Ok(serialize(&mut program.module, Some(&program.comments)))
}