Skip to content

Commit 8602004

Browse files
committed
Fix incorrect newline in loose lists
Closes #79
1 parent be855e7 commit 8602004

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## Unreleased
44

5+
6+
### Fixed
7+
8+
- Loose lists would get a newline between the bullet point and the text when it
9+
is the first markdown element.
10+
511
## 0.21.0 - 2025-07-10
612

713
### Changed

egui_commonmark/examples/mixing.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ fn main() -> eframe::Result<()> {
4949
- baa
5050
"#,
5151
r#"
52+
1. Loose list
53+
54+
2. aaa
55+
"#,
56+
r#"
5257
```rust
5358
let x = 3;
5459
```

egui_commonmark/src/parsers/pulldown.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ use pulldown_cmark::{CowStr, HeadingLevel};
1515
/// All elements try to insert a newline before them (if they are allowed)
1616
/// and end their own line.
1717
struct Newline {
18+
/// Whether a newline should not be inserted before a widget. This is only for
19+
/// the first widget.
20+
should_not_start_newline_forced: bool,
1821
/// Whether an element should insert a newline before it
1922
should_start_newline: bool,
2023
/// Whether an element should end it's own line using a newline
@@ -28,8 +31,8 @@ struct Newline {
2831
impl Default for Newline {
2932
fn default() -> Self {
3033
Self {
31-
// Default as false as the first line should not have a newline above it
32-
should_start_newline: false,
34+
should_not_start_newline_forced: true,
35+
should_start_newline: true,
3336
should_end_newline: true,
3437
should_end_newline_forced: true,
3538
}
@@ -42,11 +45,11 @@ impl Newline {
4245
}
4346

4447
pub fn can_insert_start(&self) -> bool {
45-
self.should_start_newline
48+
self.should_start_newline && !self.should_not_start_newline_forced
4649
}
4750

4851
pub fn try_insert_start(&self, ui: &mut Ui) {
49-
if self.should_start_newline {
52+
if self.can_insert_start() {
5053
newline(ui);
5154
}
5255
}
@@ -172,7 +175,7 @@ impl CommonMarkViewerInternal {
172175
}
173176

174177
if index == 0 {
175-
self.line.should_start_newline = true;
178+
self.line.should_not_start_newline_forced = false;
176179
}
177180
}
178181

@@ -266,7 +269,7 @@ impl CommonMarkViewerInternal {
266269
self.process_event(ui, &mut events, e, src_span, cache, options, max_width);
267270

268271
if i == 0 {
269-
self.line.should_start_newline = true;
272+
self.line.should_not_start_newline_forced = false;
270273
}
271274
}
272275
});
@@ -410,7 +413,7 @@ impl CommonMarkViewerInternal {
410413
// Currently the blockquotes are made in such a way that they need a newline at the end
411414
// and the start so when this is the first element in the markdown the newline must be
412415
// manually enabled
413-
self.line.should_start_newline = true;
416+
self.line.should_not_start_newline_forced = false;
414417
if let Some(alert) = parse_alerts(&options.alerts, &mut collected_events) {
415418
egui_commonmark_backend::alert_ui(alert, ui, |ui| {
416419
for (event, src_span) in collected_events {

egui_commonmark_macros/src/generator.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use quote::quote;
1010
use syn::Expr;
1111

1212
struct Newline {
13+
/// Whether a newline should not be inserted before a widget. This is only for
14+
/// the first widget.
15+
should_not_start_newline_forced: bool,
1316
/// Whether an element should insert a newline before it
1417
should_start_newline: bool,
1518
/// Whether an element should end it's own line using a newline
@@ -23,8 +26,8 @@ struct Newline {
2326
impl Default for Newline {
2427
fn default() -> Self {
2528
Self {
26-
// Default as false as the first line should not have a newline above it
27-
should_start_newline: false,
29+
should_not_start_newline_forced: true,
30+
should_start_newline: true,
2831
should_end_newline: true,
2932
should_end_newline_forced: true,
3033
}
@@ -37,12 +40,12 @@ impl Newline {
3740
}
3841

3942
pub fn can_insert_start(&self) -> bool {
40-
self.should_start_newline
43+
self.should_start_newline && !self.should_not_start_newline_forced
4144
}
4245

4346
#[must_use]
4447
pub fn try_insert_start(&self) -> TokenStream {
45-
if self.should_start_newline {
48+
if self.can_insert_start() {
4649
quote!(egui_commonmark_backend::newline(ui);)
4750
} else {
4851
TokenStream::new()
@@ -219,7 +222,7 @@ impl CommonMarkViewerInternal {
219222
let e = self.process_event(&mut events, e, &cache, &options);
220223

221224
if i == 0 {
222-
self.line.should_start_newline = true;
225+
self.line.should_not_start_newline_forced = false;
223226
}
224227

225228
event_stream.extend(e);
@@ -376,7 +379,8 @@ impl CommonMarkViewerInternal {
376379
});
377380
stream.extend(self.line.try_insert_start());
378381

379-
self.line.should_start_newline = true;
382+
// See non proc macro version for reaseon
383+
self.line.should_not_start_newline_forced = false;
380384
if let Some(alert) = parse_alerts(&options.alerts, &mut collected_events) {
381385
let Alert {
382386
accent_color,

0 commit comments

Comments
 (0)