-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlib.rs
122 lines (106 loc) · 2.7 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
#[cfg(not(all(target_os = "linux", target_env = "musl", target_arch = "aarch64")))]
#[global_allocator]
static ALLOC: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc;
use mdx_plugin_toc::TocItem;
use mdx_rs::{self, CompileResult};
#[macro_use]
extern crate napi_derive;
use napi::{
bindgen_prelude::{AsyncTask, Result, Task},
JsObject,
};
#[napi(object)]
pub struct Toc {
pub text: String,
pub id: String,
pub depth: u8,
}
#[napi(object)]
pub struct Output {
pub code: String,
pub links: Vec<String>,
pub html: String,
pub title: String,
pub toc: Vec<Toc>,
pub frontmatter: String,
}
#[napi(object)]
pub struct CompileOptions {
pub value: String,
pub filepath: String,
pub development: bool,
pub root: String,
pub jsx: Option<bool>,
}
impl From<TocItem> for Toc {
fn from(item: TocItem) -> Self {
Self {
text: item.text,
id: item.id,
depth: item.depth,
}
}
}
impl From<CompileResult> for Output {
fn from(res: CompileResult) -> Self {
Self {
code: res.code,
links: res.links,
html: res.html,
title: res.title,
toc: res.toc.into_iter().map(|item| item.into()).collect(),
frontmatter: res.frontmatter,
}
}
}
impl Task for Compiler {
type Output = CompileResult;
type JsValue = JsObject;
fn compute(&mut self) -> Result<Self::Output> {
Ok(self.compile())
}
fn resolve(&mut self, env: napi::Env, output: CompileResult) -> Result<Self::JsValue> {
let mut obj = env.create_object()?;
obj.set_named_property("code", output.code)?;
obj.set_named_property("links", output.links)?;
obj.set_named_property("html", output.html)?;
obj.set_named_property("title", output.title)?;
obj.set_named_property(
"toc",
output
.toc
.into_iter()
.map(|item| item.into())
.collect::<Vec<Toc>>(),
)?;
obj.set_named_property("frontmatter", output.frontmatter)?;
Ok(obj)
}
}
pub struct Compiler {
options: CompileOptions,
}
impl Compiler {
pub fn new(options: CompileOptions) -> Self {
Self { options }
}
fn compile(&mut self) -> CompileResult {
mdx_rs::compile(mdx_rs::CompileOptions {
value: self.options.value.clone(),
filepath: self.options.filepath.clone(),
development: self.options.development,
root: self.options.root.clone(),
jsx: self.options.jsx.unwrap_or(true),
})
}
}
/// Turn MDX into JavaScript.
#[napi(ts_return_type = "Promise<Output>")]
pub fn compile(options: CompileOptions) -> AsyncTask<Compiler> {
AsyncTask::new(Compiler::new(options))
}
#[napi]
pub fn compile_sync(options: CompileOptions) -> Output {
let mut compiler = Compiler::new(options);
compiler.compile().into()
}