-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathplugins.rs
108 lines (97 loc) · 3.18 KB
/
plugins.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
extern crate mdxjs;
use markdown::mdast;
use mdxjs::hast;
use mdxjs::{HastNode, MdastNode, Options, PluginOptions, RecmaProgram};
use std::rc::Rc;
use swc_core::common::{Span, SyntaxContext};
use swc_core::ecma::ast as estree;
use swc_core::ecma::atoms::JsWord;
/// Example that compiles the example MDX document from <https://mdxjs.com>
/// to JavaScript.
fn main() -> Result<(), String> {
println!(
"{}",
mdxjs::compile_with_plugins(
"# test",
&Options {
..Default::default()
},
&PluginOptions {
experimental_mdast_transforms: Some(vec![Rc::new(|root: &mut MdastNode| {
mdast_visit_mut(root, |n| {
if let mdast::Node::Text(text) = n {
text.value = "Hello World!".into();
}
});
Ok(())
})]),
experimental_hast_transforms: Some(vec![Rc::new(|root: &mut HastNode| {
hast_visit_mut(root, |n| {
if let hast::Node::Element(e) = n {
if e.tag_name == "h1" {
e.tag_name = "h2".into();
}
};
});
Ok(())
})]),
experimental_recma_transforms: Some(vec![Rc::new(|program: &mut RecmaProgram| {
let body = &mut program.module.body;
body.push(estree::ModuleItem::Stmt(estree::Stmt::Expr(
estree::ExprStmt {
expr: Box::new(estree::Expr::Ident(estree::Ident::from((
JsWord::from("hello"),
SyntaxContext::empty(),
)))),
span: Span::default(),
},
)));
Ok(())
})])
}
)?
);
Ok(())
}
fn mdast_visit_mut<Visitor>(node: &mut mdast::Node, visitor: Visitor)
where
Visitor: FnMut(&mut mdast::Node),
{
mdast_visit_mut_impl(node, visitor);
}
fn mdast_visit_mut_impl<Visitor>(node: &mut mdast::Node, mut visitor: Visitor) -> Visitor
where
Visitor: FnMut(&mut mdast::Node),
{
visitor(node);
if let Some(children) = node.children_mut() {
let mut index = 0;
while index < children.len() {
let child = &mut children[index];
visitor = mdast_visit_mut_impl(child, visitor);
index += 1;
}
}
visitor
}
fn hast_visit_mut<Visitor>(node: &mut hast::Node, visitor: Visitor)
where
Visitor: FnMut(&mut hast::Node),
{
hast_visit_mut_impl(node, visitor);
}
fn hast_visit_mut_impl<Visitor>(node: &mut hast::Node, mut visitor: Visitor) -> Visitor
where
Visitor: FnMut(&mut hast::Node),
{
visitor(node);
if let Some(children) = node.children_mut() {
let mut index = 0;
while index < children.len() {
let child = &mut children[index];
visitor = hast_visit_mut_impl(child, visitor);
index += 1;
}
}
visitor
}