From e406ac6db9d3ead179d071a8293df7aa2bbacafc Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 16 Oct 2025 16:44:58 +0800 Subject: [PATCH] markdown_preview: Fix block quote last child bottom padding (#40343) Release Notes: - Fixed block quote last child bottom padding in Markdown preview. | Before | After | | --- | --- | | image | SCR-20251016-okfv | --- .../markdown_preview/src/markdown_renderer.rs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/crates/markdown_preview/src/markdown_renderer.rs b/crates/markdown_preview/src/markdown_renderer.rs index a873771e001f594149acc83ea46ce45608a9ed87..489ce532f0060d29436d008413be1391044ab3e3 100644 --- a/crates/markdown_preview/src/markdown_renderer.rs +++ b/crates/markdown_preview/src/markdown_renderer.rs @@ -61,6 +61,7 @@ pub struct RenderContext { syntax_theme: Arc, indent: usize, checkbox_clicked_callback: Option, + is_last_child: bool, } impl RenderContext { @@ -94,6 +95,7 @@ impl RenderContext { code_block_background_color: theme.colors().surface_background, code_span_background_color: theme.colors().editor_document_highlight_read_background, checkbox_clicked_callback: None, + is_last_child: false, } } @@ -135,12 +137,25 @@ impl RenderContext { /// We give padding between "This is a block quote." /// and "And this is the next paragraph." fn with_common_p(&self, element: Div) -> Div { - if self.indent > 0 { + if self.indent > 0 && !self.is_last_child { element.pb(self.scaled_rems(0.75)) } else { element } } + + /// The is used to indicate that the current element is the last child or not of its parent. + /// + /// Then we can avoid adding padding to the bottom of the last child. + fn with_last_child(&mut self, is_last: bool, render: R) -> AnyElement + where + R: FnOnce(&mut Self) -> AnyElement, + { + self.is_last_child = is_last; + let element = render(self); + self.is_last_child = false; + element + } } pub fn render_parsed_markdown( @@ -585,7 +600,12 @@ fn render_markdown_block_quote( let children: Vec = parsed .children .iter() - .map(|child| render_markdown_block(child, cx)) + .enumerate() + .map(|(ix, child)| { + cx.with_last_child(ix + 1 == parsed.children.len(), |cx| { + render_markdown_block(child, cx) + }) + }) .collect(); cx.indent -= 1;