Skip to content

Commit

Permalink
Fix line break handling in markdown and wrap (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamRodri authored Jan 22, 2025
1 parent 0589f4e commit c5e308b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

* Fix `Wrap!` ignoring child `Text!` that is only a line-break.
* Fix soft/hard breaks in `Markdown!`.
* Improve cache of font data to use less memory.

# 0.13.7
Expand Down
38 changes: 23 additions & 15 deletions crates/zng-wgt-markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ fn markdown_view_fn<'a>(md: &'a str) -> impl UiNode {
let mut last_txt_end = '\0';

for item in Parser::new_with_broken_link_callback(md, Options::all(), Some(&mut |b: BrokenLink<'a>| Some((b.reference, "".into())))) {
let item = match item {
Event::SoftBreak => Event::Text(pulldown_cmark::CowStr::Borrowed(" ")),
Event::HardBreak => Event::Text(pulldown_cmark::CowStr::Borrowed("\n")),
item => item,
};
match item {
Event::Start(tag) => match tag {
Tag::Paragraph => {
Expand Down Expand Up @@ -502,24 +507,27 @@ fn markdown_view_fn<'a>(md: &'a str) -> impl UiNode {
// apply `WhiteSpace::MergeAll` across texts.
let txt_end = txt.chars().next_back().unwrap();

let starts_with_space = txt.chars().next().unwrap().is_whitespace();
match WhiteSpace::MergeAll.transform(&txt) {
std::borrow::Cow::Borrowed(_) => {
if starts_with_space && last_txt_end != '\0' || !txt.is_empty() && last_txt_end.is_whitespace() {
txt.to_mut().insert(0, ' ');
}
txt.end_mut();
last_txt_end = txt_end;
}
std::borrow::Cow::Owned(t) => {
txt = t;
if !txt.is_empty() {
if txt != " " && txt != "\n" {
// not Soft/HardBreak
let starts_with_space = txt.chars().next().unwrap().is_whitespace();
match WhiteSpace::MergeAll.transform(&txt) {
std::borrow::Cow::Borrowed(_) => {
if starts_with_space && last_txt_end != '\0' || !txt.is_empty() && last_txt_end.is_whitespace() {
txt.to_mut().insert(0, ' ');
txt.end_mut();
}
txt.end_mut();
last_txt_end = txt_end;
}
std::borrow::Cow::Owned(t) => {
txt = t;
if !txt.is_empty() {
if starts_with_space && last_txt_end != '\0' || !txt.is_empty() && last_txt_end.is_whitespace() {
txt.to_mut().insert(0, ' ');
txt.end_mut();
}
last_txt_end = txt_end;
}
}
}
}

Expand Down Expand Up @@ -578,8 +586,6 @@ fn markdown_view_fn<'a>(md: &'a str) -> impl UiNode {
inlines.push(txt);
}
}
Event::SoftBreak => {}
Event::HardBreak => {}
Event::Rule => {
blocks.push(rule_view(RuleFnArgs {}));
}
Expand All @@ -590,6 +596,8 @@ fn markdown_view_fn<'a>(md: &'a str) -> impl UiNode {
}
Event::InlineMath(_) => {}
Event::DisplayMath(_) => {}
// handled early
Event::SoftBreak | Event::HardBreak => unreachable!(),
}
}

Expand Down
7 changes: 6 additions & 1 deletion crates/zng-wgt-wrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,12 @@ impl InlineLayout {

let (inline, size) = wm.measure_inline(inline_constrain, row.size.height - spacing.row, child);

if size.is_empty() {
let can_collapse = size.is_empty()
&& match &inline {
Some(i) => i.first_segs.is_empty(),
None => true,
};
if can_collapse {
row.item_segs.push(ItemSegsInfo::new_collapsed());
// collapsed, continue.
return;
Expand Down

0 comments on commit c5e308b

Please sign in to comment.