From e28a6b01f47e2eebee2d5121507dc59b9f7e85e4 Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Thu, 16 May 2024 08:09:40 -0600 Subject: [PATCH] infra: fix rendering bug in mdbook-trpl-note Previously, this was only pushing in the opening paragraph tag before getting to text, so it was accidentally dropping cases where the text started after some other opening tag, e.g. strong emphasis. --- packages/mdbook-trpl-note/src/lib.rs | 41 ++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/mdbook-trpl-note/src/lib.rs b/packages/mdbook-trpl-note/src/lib.rs index 7a671be1ce..774dc82b55 100644 --- a/packages/mdbook-trpl-note/src/lib.rs +++ b/packages/mdbook-trpl-note/src/lib.rs @@ -34,7 +34,11 @@ impl Preprocessor for TrplNote { "simple-note-preprocessor" } - fn run(&self, _ctx: &PreprocessorContext, mut book: Book) -> Result { + fn run( + &self, + _ctx: &PreprocessorContext, + mut book: Book, + ) -> Result { book.for_each_mut(|item| { if let BookItem::Chapter(ref mut chapter) = item { chapter.content = rewrite(&chapter.content); @@ -75,7 +79,9 @@ pub fn rewrite(text: &str) -> String { events.extend([ SoftBreak, SoftBreak, - Html(r#"
"#.into()), + Html( + r#"
"#.into(), + ), SoftBreak, SoftBreak, Start(Tag::Paragraph), @@ -89,7 +95,10 @@ pub fn rewrite(text: &str) -> String { } } - (StartingBlockquote(_blockquote_events), heading @ Start(Tag::Heading { .. })) => { + ( + StartingBlockquote(_blockquote_events), + heading @ Start(Tag::Heading { .. }), + ) => { events.extend([ SoftBreak, SoftBreak, @@ -101,14 +110,18 @@ pub fn rewrite(text: &str) -> String { state = InNote; } - (StartingBlockquote(ref mut events), Start(Tag::Paragraph)) => { - events.push(Start(Tag::Paragraph)); + (StartingBlockquote(ref mut events), Start(tag)) => { + events.push(Start(tag)); } (InNote, End(TagEnd::BlockQuote)) => { // As with the start of the block HTML, the closing HTML must be // separated from the Markdown text by two newlines. - events.extend([SoftBreak, SoftBreak, Html("
".into())]); + events.extend([ + SoftBreak, + SoftBreak, + Html("
".into()), + ]); state = Default; } @@ -258,7 +271,8 @@ mod tests { #[test] fn h1_then_blockquote() { - let text = "> # Header\n > And then some note content.\n\n> This is quoted."; + let text = + "> # Header\n > And then some note content.\n\n> This is quoted."; let processed = rewrite(text); assert_eq!( render_markdown(&processed), @@ -268,7 +282,8 @@ mod tests { #[test] fn blockquote_then_h1_note() { - let text = "> This is quoted.\n\n> # Header\n > And then some note content."; + let text = + "> This is quoted.\n\n> # Header\n > And then some note content."; let processed = rewrite(text); assert_eq!( render_markdown(&processed), @@ -276,6 +291,16 @@ mod tests { ); } + #[test] + fn blockquote_with_strong() { + let text = "> **Bold text in a paragraph.**"; + let processed = rewrite(text); + assert_eq!( + render_markdown(&processed), + "
\n

Bold text in a paragraph.

\n
\n" + ); + } + fn render_markdown(text: &str) -> String { let parser = Parser::new(text); let mut buf = String::new();