From 6a00cfcc4a52faf8680b2e91c5e1c9ada524df61 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 10 Mar 2025 22:53:53 +0800 Subject: [PATCH] text_view: Fix HTML render to support blockquote. (#703) image --- crates/story/examples/html.html | 6 ++++++ crates/ui/src/text/element.rs | 11 ++++------- crates/ui/src/text/html.rs | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/crates/story/examples/html.html b/crates/story/examples/html.html index 7452b52a..3f6de6ea 100644 --- a/crates/story/examples/html.html +++ b/crates/story/examples/html.html @@ -10,6 +10,12 @@ >, bold, italic, and code text.

+

This is a text before blockquote.

+
+ This is before paragraph in blockquote. +

This is a second blockquote paragraph.

+

This is after paragraph in blockquote.

+

div() - .children({ - let children_len = children.len(); - - children.into_iter().enumerate().map(move |(ix, c)| { - let is_last_child = ix == children_len - 1; - c.render(None, is_last_child, text_view_style, window, cx) + .children( + children.into_iter().map(move |c| { + c.render(None, false, text_view_style, window, cx) }) - }) + ) .into_any_element(), Node::Paragraph(paragraph) => div().mb(mb).child(paragraph).into_any_element(), Node::Heading { level, children } => { diff --git a/crates/ui/src/text/html.rs b/crates/ui/src/text/html.rs index bd97eabc..ed26a1c8 100644 --- a/crates/ui/src/text/html.rs +++ b/crates/ui/src/text/html.rs @@ -499,6 +499,7 @@ fn parse_paragraph( title: title.map(Into::into), }); } + _ => { // All unknown tags to as text let mut child_paragraph = Paragraph::default(); @@ -697,6 +698,24 @@ fn parse_node(node: &Rc, paragraph: &mut Paragraph) -> element::Node { table } } + local_name!("blockquote") => { + let mut children = vec![]; + if !paragraph.is_empty() { + children.push(element::Node::Paragraph(paragraph.clone())); + paragraph.clear(); + } + + let mut blockquote = Paragraph::default(); + for (i, child) in node.children.borrow().iter().enumerate() { + if i > 0 { + blockquote.push_str("\n"); + } + parse_paragraph(&mut blockquote, child); + } + children.push(element::Node::Blockquote(blockquote)); + + element::Node::Root { children: children } + } _ => { if BLOCK_ELEMENTS.contains(&name.local.trim()) { let mut children: Vec = vec![];