From fe618ff6e38ec0ed4da72a3935fd9ea64ee1cef5 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 19 Sep 2022 11:17:26 +0200 Subject: Add support for parsing MDX ESM, expressions This commit adds support for hooks that lets a user integrate another parser with `micromark-rs`, to parse ESM and expressions according to a particular grammar (such as a programming language, typically JavaScript). For an example integrating with SWC, see `tests/test_utils/mod.rs`. The integration occurs with two functions passed in `options`: `mdx_expression_parse` and `mdx_esm_parse`. The can signal back to micromark when they are successful, whether there is an error at the end (in which case micromark will try to parse more), or whether there is a syntax error (in which case micromark will crash). --- tests/mdx_swc.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/mdx_swc.rs (limited to 'tests/mdx_swc.rs') diff --git a/tests/mdx_swc.rs b/tests/mdx_swc.rs new file mode 100644 index 0000000..c9a2a61 --- /dev/null +++ b/tests/mdx_swc.rs @@ -0,0 +1,47 @@ +extern crate micromark; +mod test_utils; +use micromark::{micromark_with_options, Constructs, Options}; +use pretty_assertions::assert_eq; +use test_utils::{parse_esm, parse_expression}; + +#[test] +fn mdx_swc() -> Result<(), String> { + let mdx = Options { + constructs: Constructs::mdx(), + mdx_esm_parse: Some(Box::new(parse_esm)), + mdx_expression_parse: Some(Box::new(parse_expression)), + ..Options::default() + }; + + assert_eq!( + micromark_with_options("{'}'}", &mdx)?, + "", + "should support JavaScript-aware flow expressions w/ `mdx_expression_parse`" + ); + + assert_eq!( + micromark_with_options("a {'}'} b", &mdx)?, + "

a b

", + "should support JavaScript-aware text expressions w/ `mdx_expression_parse`" + ); + + assert_eq!( + micromark_with_options("", &mdx)?, + "", + "should support JavaScript-aware attribute expressions w/ `mdx_expression_parse`" + ); + + assert_eq!( + micromark_with_options("", &mdx)?, + "", + "should support JavaScript-aware attribute value expressions w/ `mdx_expression_parse`" + ); + + assert_eq!( + micromark_with_options("import a from 'b'\n\nexport {a}\n\n# c", &mdx)?, + "

c

", + "should support JavaScript-aware ESM w/ `mdx_esm_parse`" + ); + + Ok(()) +} -- cgit