diff --git a/Cargo.toml b/Cargo.toml index 5462ac32..7dba4cf1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ criterion = "0.5" env_logger = "0.11" pretty_assertions = { workspace = true } serde_json = { version = "1" } -swc_core = { version = "22", features = [ +swc_core = { version = "32", features = [ "common", "ecma_ast", "ecma_parser", diff --git a/src/configuration.rs b/src/configuration.rs index 4f0281f3..e2696827 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -992,6 +992,45 @@ pub struct CompileOptions { /// * [*ยง 6.1 Disallowed Raw HTML (extension)* in GFM](https://github.github.com/gfm/#disallowed-raw-html-extension-) /// * [`cmark-gfm#extensions/tagfilter.c`](https://github.com/github/cmark-gfm/blob/master/extensions/tagfilter.c) pub gfm_tagfilter: bool, + + /// Whether to not use `

` elements to surround free text + /// + /// The default is `false` + /// + /// ## Examples + /// + /// ``` + /// use markdown::{to_html_with_options, CompileOptions, Options, ParseOptions}; + /// # fn main() -> Result<(), markdown::message::Message> { + /// + /// // With `no_paragraphs` set to `false`, normal text is surrounded with `

` elements + /// assert_eq!( + /// to_html_with_options( + /// "Hello", + /// &Options::default(), + /// )?, + /// "

Hello

" + /// ); + /// + /// // Pass `no_paragraphs: true` to remove them + /// assert_eq!( + /// to_html_with_options( + /// "Hello", + /// &Options { + /// parse: ParseOptions::default(), + /// compile: CompileOptions { + /// no_paragraphs: true, + /// ..CompileOptions::default() + /// } + /// } + /// )?, + /// "Hello" + /// ); + /// # Ok(()) + /// # } + /// ``` + /// + pub no_paragraphs: bool, } impl CompileOptions { diff --git a/src/to_html.rs b/src/to_html.rs index b50ec912..e9fdffc6 100644 --- a/src/to_html.rs +++ b/src/to_html.rs @@ -679,7 +679,7 @@ fn on_enter_list_item_marker(context: &mut CompileContext) { fn on_enter_paragraph(context: &mut CompileContext) { let tight = context.tight_stack.last().unwrap_or(&false); - if !tight { + if !tight && !context.options.no_paragraphs { context.line_ending_if_needed(); context.push("

"); } @@ -1519,7 +1519,7 @@ fn on_exit_media(context: &mut CompileContext) { fn on_exit_paragraph(context: &mut CompileContext) { let tight = context.tight_stack.last().unwrap_or(&false); - if *tight { + if *tight || context.options.no_paragraphs { context.slurp_one_line_ending = true; } else { context.push("

"); diff --git a/tests/mdx_expression_text.rs b/tests/mdx_expression_text.rs index f24e4fba..d7b9dcea 100644 --- a/tests/mdx_expression_text.rs +++ b/tests/mdx_expression_text.rs @@ -144,7 +144,7 @@ fn mdx_expression() -> Result<(), message::Message> { .err() .unwrap() .to_string(), - "2:8: Could not parse expression with swc: Unexpected eof (mdx:swc)", + "2:8: Could not parse expression with swc: Expression expected (mdx:swc)", "should crash on incorrect expressions in containers (1)" ); @@ -270,7 +270,7 @@ fn mdx_expression_text_gnostic() -> Result<(), message::Message> { .err() .unwrap() .to_string(), - "1:9: Could not parse expression with swc: Unexpected eof (mdx:swc)", + "1:9: Could not parse expression with swc: Expression expected (mdx:swc)", "should crash on an incorrect expression" ); diff --git a/tests/test_utils/swc.rs b/tests/test_utils/swc.rs index a446d95d..536bc256 100644 --- a/tests/test_utils/swc.rs +++ b/tests/test_utils/swc.rs @@ -269,7 +269,7 @@ fn create_config(source: String) -> (SourceFile, Syntax, EsVersion) { FileName::Anon.into(), false, FileName::Anon.into(), - source, + source.into(), BytePos::from_usize(1), ), // Syntax.