From 14e8f6dcb11e1a3e98114017eebd31005cac7508 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Sat, 25 Jan 2025 10:26:36 +1300 Subject: [PATCH] Strip trailing newlines in unhighlighted code blocks Signed-off-by: Nico Burns --- src/html.rs | 5 ++++- src/parser/mod.rs | 12 ++++++------ src/tests/core.rs | 5 +---- src/tests/footnotes.rs | 3 +-- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/html.rs b/src/html.rs index 1f5d9210..c4bbde59 100644 --- a/src/html.rs +++ b/src/html.rs @@ -582,7 +582,10 @@ where let mut code_attributes: HashMap = HashMap::new(); let code_attr: String; - let literal = &ncb.literal.as_bytes(); + let literal = &ncb + .literal + .trim_end_matches(|c| c == '\r' || c == '\n') + .as_bytes(); let info = &ncb.info.as_bytes(); if !info.is_empty() { diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 07369bad..c8781384 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -699,11 +699,11 @@ pub struct ParseOptions<'c> { /// # use comrak::{markdown_to_html, Options}; /// let mut options = Options::default(); /// assert_eq!(markdown_to_html("```\nfn hello();\n```\n", &options), - /// "
fn hello();\n
\n"); + /// "
fn hello();
\n"); /// /// options.parse.default_info_string = Some("rust".into()); /// assert_eq!(markdown_to_html("```\nfn hello();\n```\n", &options), - /// "
fn hello();\n
\n"); + /// "
fn hello();
\n"); /// ``` pub default_info_string: Option, @@ -789,11 +789,11 @@ pub struct RenderOptions { /// # use comrak::{markdown_to_html, Options}; /// let mut options = Options::default(); /// assert_eq!(markdown_to_html("``` rust\nfn hello();\n```\n", &options), - /// "
fn hello();\n
\n"); + /// "
fn hello();
\n"); /// /// options.render.github_pre_lang = true; /// assert_eq!(markdown_to_html("``` rust\nfn hello();\n```\n", &options), - /// "
fn hello();\n
\n"); + /// "
fn hello();
\n"); /// ``` #[cfg_attr(feature = "bon", builder(default))] pub github_pre_lang: bool, @@ -804,7 +804,7 @@ pub struct RenderOptions { /// # use comrak::{markdown_to_html, Options}; /// let mut options = Options::default(); /// assert_eq!(markdown_to_html("``` rust extra info\nfn hello();\n```\n", &options), - /// "
fn hello();\n
\n"); + /// "
fn hello();
\n"); /// /// options.render.full_info_string = true; /// let html = markdown_to_html("``` rust extra info\nfn hello();\n```\n", &options); @@ -1110,7 +1110,7 @@ pub struct RenderPlugins<'p> { /// let input = "```rust\nfn main<'a>();\n```"; /// /// assert_eq!(markdown_to_html_with_plugins(input, &options, &plugins), - /// "
fn main<'a>();\n
\n"); + /// "
fn main<'a>();
\n"); /// /// pub struct MockAdapter {} /// impl SyntaxHighlighterAdapter for MockAdapter { diff --git a/src/tests/core.rs b/src/tests/core.rs index 714ced6b..05e31d4a 100644 --- a/src/tests/core.rs +++ b/src/tests/core.rs @@ -32,10 +32,7 @@ fn basic() { fn codefence() { html( concat!("``` rust yum\n", "fn main<'a>();\n", "```\n"), - concat!( - "
fn main<'a>();\n",
-            "
\n" - ), + concat!("
fn main<'a>();
\n"), ); } diff --git a/src/tests/footnotes.rs b/src/tests/footnotes.rs index 81776ddd..e875d304 100644 --- a/src/tests/footnotes.rs +++ b/src/tests/footnotes.rs @@ -40,8 +40,7 @@ fn footnotes() { "
  • \n", "

    Here's one with multiple blocks.

    \n", "

    Subsequent paragraphs are indented.

    \n", - "
    code\n",
    -            "
    \n", + "
    code
    \n", " \ 2\n", "
  • \n",