-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
317 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//! JS equivalent: https://github.com/syntax-tree/mdast-util-mdx-expression/blob/main/lib/index.js#L42 | ||
use super::Handle; | ||
use crate::state::{Info, State}; | ||
use alloc::{format, string::String}; | ||
use markdown::{ | ||
mdast::{MdxFlowExpression, MdxTextExpression, Node}, | ||
message::Message, | ||
}; | ||
|
||
impl Handle for MdxFlowExpression { | ||
fn handle( | ||
&self, | ||
state: &mut State, | ||
_info: &Info, | ||
_parent: Option<&Node>, | ||
_node: &Node, | ||
) -> Result<alloc::string::String, Message> { | ||
Ok(handle_mdx_expression(&self.value, state)) | ||
} | ||
} | ||
|
||
impl Handle for MdxTextExpression { | ||
fn handle( | ||
&self, | ||
state: &mut State, | ||
_info: &Info, | ||
_parent: Option<&Node>, | ||
_node: &Node, | ||
) -> Result<alloc::string::String, Message> { | ||
Ok(handle_mdx_expression(&self.value, state)) | ||
} | ||
} | ||
|
||
fn handle_mdx_expression(value: &str, state: &State) -> String { | ||
let result = state.indent_lines(value, |line, index, blank| { | ||
let space = if index == 0 || blank { "" } else { " " }; | ||
let mut results = String::with_capacity(space.len() + line.len()); | ||
results.push_str(space); | ||
results.push_str(line); | ||
results | ||
}); | ||
|
||
format!("{{{}}}", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//! JS equivalent: https://github.com/syntax-tree/mdast-util-mdxjs-esm/blob/main/lib/index.js#L79 | ||
use super::Handle; | ||
use crate::state::{Info, State}; | ||
use markdown::{ | ||
mdast::{MdxjsEsm, Node}, | ||
message::Message, | ||
}; | ||
|
||
impl Handle for MdxjsEsm { | ||
fn handle( | ||
&self, | ||
_state: &mut State, | ||
_info: &Info, | ||
_parent: Option<&Node>, | ||
_node: &Node, | ||
) -> Result<alloc::string::String, Message> { | ||
Ok(self.value.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//! JS equivalent: https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/handle/text.js | ||
use super::Handle; | ||
use crate::{ | ||
construct_name::ConstructName, | ||
state::{Info, State}, | ||
}; | ||
use alloc::{string::String, vec::Vec}; | ||
use markdown::{ | ||
mdast::{AttributeContent, MdxJsxFlowElement, Node}, | ||
message::Message, | ||
unist::Position, | ||
}; | ||
|
||
#[allow(dead_code)] | ||
trait MdxJsx { | ||
fn children(&self) -> &Vec<Node>; | ||
fn name(&self) -> &Option<String>; | ||
fn attributes(&self) -> &Vec<AttributeContent>; | ||
fn position(&self) -> &Option<Position>; | ||
} | ||
|
||
impl MdxJsx for MdxJsxFlowElement { | ||
fn children(&self) -> &Vec<Node> { | ||
&self.children | ||
} | ||
|
||
fn name(&self) -> &Option<String> { | ||
&self.name | ||
} | ||
|
||
fn attributes(&self) -> &Vec<AttributeContent> { | ||
&self.attributes | ||
} | ||
|
||
fn position(&self) -> &Option<Position> { | ||
&self.position | ||
} | ||
} | ||
|
||
impl Handle for MdxJsxFlowElement { | ||
fn handle( | ||
&self, | ||
_state: &mut State, | ||
_info: &Info, | ||
_parent: Option<&Node>, | ||
_node: &Node, | ||
) -> Result<String, Message> { | ||
Ok(String::new()) | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn create_indent(depth: usize) -> String { | ||
" ".repeat(depth) | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn infer_depth(state: &State) -> usize { | ||
let mut depth: usize = 0; | ||
|
||
for construct_name in state.stack.iter().rev() { | ||
if matches!( | ||
construct_name, | ||
ConstructName::Blockquote | ConstructName::ListItem | ||
) { | ||
break; | ||
} else { | ||
depth += 1; | ||
} | ||
} | ||
|
||
depth | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
use markdown::{ | ||
mdast::{ | ||
Definition, MdxFlowExpression, MdxTextExpression, MdxjsEsm, Node, Paragraph, Root, Text, | ||
}, | ||
to_mdast as from, ParseOptions, | ||
}; | ||
use mdast_util_to_markdown::to_markdown as to; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn mdx() { | ||
assert_eq!( | ||
to(&Node::Root(Root { | ||
children: vec![ | ||
Node::MdxFlowExpression(MdxFlowExpression { | ||
value: "a + b".to_string(), | ||
position: None, | ||
stops: vec![] | ||
}), | ||
Node::MdxFlowExpression(MdxFlowExpression { | ||
value: String::from("\nc +\n1\n"), | ||
position: None, | ||
stops: vec![] | ||
}), | ||
Node::MdxFlowExpression(MdxFlowExpression { | ||
value: String::new(), | ||
position: None, | ||
stops: vec![] | ||
}), | ||
Node::Paragraph(Paragraph { | ||
children: vec![Node::Text(Text { | ||
value: "d".to_string(), | ||
position: None | ||
})], | ||
position: None | ||
}) | ||
], | ||
position: None | ||
})) | ||
.unwrap(), | ||
"{a + b}\n\n{\n c +\n 1\n}\n\n{}\n\nd\n", | ||
"should serialize flow expressions" | ||
); | ||
|
||
assert_eq!( | ||
to(&Node::Paragraph(Paragraph { | ||
children: vec![ | ||
Node::Text(Text { | ||
value: "a ".to_string(), | ||
position: None | ||
}), | ||
Node::MdxTextExpression(MdxTextExpression { | ||
value: "b + c".to_string(), | ||
position: None, | ||
stops: vec![] | ||
}), | ||
Node::Text(Text { | ||
value: ", d ".to_string(), | ||
position: None | ||
}), | ||
Node::MdxTextExpression(MdxTextExpression { | ||
value: "e + 1".to_string(), | ||
position: None, | ||
stops: vec![] | ||
}), | ||
Node::Text(Text { | ||
value: ", f ".to_string(), | ||
position: None | ||
}), | ||
Node::MdxTextExpression(MdxTextExpression { | ||
value: String::new(), | ||
position: None, | ||
stops: vec![] | ||
}), | ||
Node::Text(Text { | ||
value: ".".to_string(), | ||
position: None | ||
}), | ||
], | ||
position: None | ||
})) | ||
.unwrap(), | ||
"a {b + c}, d {e + 1}, f {}.\n", | ||
"should serialize text expressions" | ||
); | ||
|
||
assert_eq!( | ||
to(&Node::Paragraph(Paragraph { | ||
children: vec![Node::Text(Text { | ||
value: "a { b".to_string(), | ||
position: None | ||
})], | ||
position: None | ||
})) | ||
.unwrap(), | ||
"a \\{ b\n", | ||
"should escape `{{` in text" | ||
); | ||
|
||
assert_eq!( | ||
to(&Node::Definition(Definition { | ||
position: None, | ||
url: "x".to_string(), | ||
title: "a\n{\nb".to_string().into(), | ||
identifier: "a".to_string(), | ||
label: None | ||
})) | ||
.unwrap(), | ||
"[a]: x \"a\n\\{\nb\"\n", | ||
"should escape `{{` at the start of a line" | ||
); | ||
|
||
assert_eq!( | ||
to(&from(" {`\n a\n `}", &ParseOptions::mdx()).unwrap()).unwrap(), | ||
"{`\n a\n `}\n", | ||
"should strip superfluous whitespace depending on the opening prefix, when roundtripping expressions (flow)" | ||
); | ||
|
||
// This require changing to match the js tests when https://github.com/wooorm/markdown-rs/issues/150 is resolved | ||
//assert_eq!( | ||
// to(&from(" {`\n a\n `}", &ParseOptions::mdx()).unwrap()).unwrap(), | ||
// "{`\n a\n `}\n", | ||
// "should *not* strip superfluous whitespace (if there is more) when roundtripping expressions (flow)" | ||
//); | ||
|
||
//// This require changing to match the js tests when https://github.com/wooorm/markdown-rs/issues/150 is resolved | ||
//assert_eq!( | ||
// to(&from("a {`\n b\n `} c", &ParseOptions::mdx()).unwrap()).unwrap(), | ||
// "a {`\n b\n `} c\n", | ||
// "should not strip consecutive lines in expressions (text)" | ||
//); | ||
|
||
assert_eq!( | ||
to(&Node::Root(Root { | ||
children: vec![ | ||
Node::MdxjsEsm(MdxjsEsm { | ||
value: String::from("import a from \"b\"\nexport var c = \"\""), | ||
position: None, | ||
stops: Vec::new() | ||
}), | ||
Node::Paragraph(Paragraph { | ||
children: vec![Node::Text(Text { | ||
value: "d".to_string(), | ||
position: None | ||
})], | ||
position: None | ||
}) | ||
], | ||
position: None | ||
})) | ||
.unwrap(), | ||
"import a from \"b\"\nexport var c = \"\"\n\nd\n" | ||
) | ||
} |