-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlib.rs
184 lines (169 loc) · 5.23 KB
/
lib.rs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Public API of `mdxjs-rs`.
//!
//! This module exposes primarily [`compile()`][].
//!
//! * [`compile()`][]
//! — turn MDX into JavaScript
#![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_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, serialize},
swc_util_build_jsx::{swc_util_build_jsx, Options as BuildOptions},
};
use hast;
use markdown::{to_mdast, Constructs, Location, ParseOptions};
use mdx_plugin_container::mdx_plugin_container;
use mdx_plugin_external_link::mdx_plugin_external_link;
use mdx_plugin_frontmatter::mdx_plugin_frontmatter;
use mdx_plugin_header_anchor::mdx_plugin_header_anchor;
use mdx_plugin_html::mdx_plugin_html;
use mdx_plugin_normalize_link::mdx_plugin_normalize_link;
use mdx_plugin_toc::{mdx_plugin_toc, TocItem};
pub use crate::mdx_plugin_recma_document::JsxRuntime;
pub struct CompileOptions {
pub value: String,
/// The root directory of the project.
pub root: String,
/// File path to the source file.
pub filepath: String,
/// Whether to add extra information to error messages in generated code
pub development: bool,
/// Whether to keep JSX (default: `true`).
pub jsx: bool,
}
impl Default for CompileOptions {
fn default() -> Self {
Self {
value: "".to_string(),
root: "".to_string(),
filepath: "".to_string(),
development: true,
jsx: true,
}
}
}
pub struct CompileResult {
pub code: String,
pub links: Vec<String>,
pub html: String,
pub title: String,
pub toc: Vec<TocItem>,
pub frontmatter: String,
}
pub fn compile(options: CompileOptions) -> CompileResult {
let CompileOptions {
value,
filepath,
development,
root,
jsx,
} = options;
let is_mdx = filepath.ends_with(".mdx");
let parse_options = ParseOptions {
constructs: Constructs {
frontmatter: true,
// Enable GFM Grammer
gfm_autolink_literal: true,
gfm_label_start_footnote: true,
gfm_footnote_definition: true,
gfm_strikethrough: true,
gfm_table: true,
gfm_task_list_item: true,
// If is_mdx is true, use mdx constructs, or use markdown constructs
..if is_mdx {
Constructs::mdx()
} else {
Constructs::default()
}
},
gfm_strikethrough_single_tilde: false,
math_text_single_dollar: false,
mdx_esm_parse: Some(Box::new(parse_esm)),
mdx_expression_parse: None,
};
let document_options = DocumentOptions {
pragma: Some("React.createElement".to_string()),
pragma_frag: Some("React.Fragment".to_string()),
pragma_import_source: Some("react".to_string()),
jsx_import_source: Some("react".to_string()),
jsx_runtime: Some(JsxRuntime::Automatic),
};
let rewrite_options = RewriteOptions {
development,
provider_import_source: Some("@mdx-js/react".to_string()),
};
let build_options: BuildOptions = BuildOptions { development };
let location = Location::new(value.as_bytes());
let mut mdast = to_mdast(value.as_str(), &parse_options).unwrap_or_else(|error| {
eprintln!("File: {:?}\nError: {:?}", filepath, error);
// Provide a default value or handle the error here
return to_mdast("", &parse_options).unwrap();
});
let toc_result = mdx_plugin_toc(&mut mdast);
let frontmatter = mdx_plugin_frontmatter(&mut mdast);
let mut hast = mdast_util_to_hast(&mdast);
mdx_plugin_header_anchor(&mut hast);
mdx_plugin_container(&mut hast);
mdx_plugin_external_link(&mut hast);
let links = mdx_plugin_normalize_link(&mut hast, &root, &filepath);
let html = mdx_plugin_html(&hast);
let mut program = hast_util_to_swc(&hast, Some(filepath.to_string()), Some(&location))
.unwrap_or_else(|error| {
eprintln!("File: {:?}\nError: {:?}", filepath, error);
hast_util_to_swc(
&hast::Node::Root(hast::Root {
children: vec![],
position: None,
}),
Some(filepath.to_string()),
Some(&location),
)
.unwrap()
});
mdx_plugin_recma_document(&mut program, &document_options, Some(&location)).unwrap_or_else(
|_| {
eprintln!("Failed to process file: {}", filepath);
},
);
mdx_plugin_recma_jsx_rewrite(&mut program, &rewrite_options, Some(&location));
if !jsx {
swc_util_build_jsx(&mut program, &build_options, Some(&location)).unwrap();
}
let code = serialize(&mut program.module, Some(&program.comments));
CompileResult {
code,
links,
html,
title: toc_result.title,
toc: toc_result.toc,
frontmatter,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_collect_title_in_mdast() {
compile(CompileOptions {
value: "## Container Title {#custom-title}".to_string(),
..Default::default()
});
}
}