Skip to content

Commit 89fae14

Browse files
authored
fix: task list marker indentation in paragraph (#116)
1 parent fe0d8db commit 89fae14

File tree

4 files changed

+83
-18
lines changed

4 files changed

+83
-18
lines changed

src/generation/generate.rs

+35-18
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ fn gen_paragraph(paragraph: &Paragraph, context: &mut Context) -> PrintItems {
281281
}
282282
}
283283

284-
items.extend(gen_nodes(&paragraph.children, context));
284+
items.extend(gen_task_list_marker_children(
285+
&paragraph.children,
286+
paragraph.marker.as_ref(),
287+
context,
288+
));
285289
items
286290
}
287291

@@ -736,42 +740,55 @@ fn gen_item(item: &Item, context: &mut Context) -> PrintItems {
736740
}
737741
}
738742

743+
items.extend(gen_task_list_marker_children(
744+
&item.children,
745+
item.marker.as_ref(),
746+
context,
747+
));
748+
749+
if !item.sub_lists.is_empty() {
750+
items.push_signal(Signal::NewLine);
751+
if utils::has_leading_blankline(item.sub_lists.first().unwrap().range().start, context.file_text) {
752+
items.push_signal(Signal::NewLine);
753+
}
754+
items.extend(gen_nodes(&item.sub_lists, context));
755+
}
756+
757+
items
758+
}
759+
760+
fn gen_task_list_marker_children(
761+
children: &[Node],
762+
marker: Option<&TaskListMarker>,
763+
context: &mut Context,
764+
) -> PrintItems {
765+
let mut items = PrintItems::new();
739766
// indent the children to beyond the task list marker
740-
let marker_indent = if item.marker.is_some() { 4 } else { 0 };
767+
let marker_indent = if marker.is_some() { 4 } else { 0 };
741768
context.raw_indent_level += marker_indent;
742-
let indent_child_index_end = item
743-
.children
769+
let indent_child_index_end = children
744770
.iter()
745771
.position(|c| {
746772
matches!(
747773
c,
748774
Node::List(_) | Node::CodeBlock(_) | Node::BlockQuote(_) | Node::Heading(_) | Node::Table(_)
749775
) || utils::has_leading_blankline(c.range().start, context.file_text)
750776
})
751-
.unwrap_or(item.children.len());
777+
.unwrap_or(children.len());
752778
items.extend(with_indent_times(
753-
gen_nodes(&item.children[..indent_child_index_end], context),
779+
gen_nodes(&children[..indent_child_index_end], context),
754780
marker_indent,
755781
));
756782
context.raw_indent_level -= marker_indent;
757783

758784
// insert the remaining children without indent
759-
if indent_child_index_end > 0 && indent_child_index_end != item.children.len() {
760-
items.push_signal(Signal::NewLine);
761-
if utils::has_leading_blankline(item.children[indent_child_index_end].range().start, context.file_text) {
762-
items.push_signal(Signal::NewLine);
763-
}
764-
}
765-
items.extend(gen_nodes(&item.children[indent_child_index_end..], context));
766-
767-
if !item.sub_lists.is_empty() {
785+
if indent_child_index_end > 0 && indent_child_index_end != children.len() {
768786
items.push_signal(Signal::NewLine);
769-
if utils::has_leading_blankline(item.sub_lists.first().unwrap().range().start, context.file_text) {
787+
if utils::has_leading_blankline(children[indent_child_index_end].range().start, context.file_text) {
770788
items.push_signal(Signal::NewLine);
771789
}
772-
items.extend(gen_nodes(&item.sub_lists, context));
773790
}
774-
791+
items.extend(gen_nodes(&children[indent_child_index_end..], context));
775792
items
776793
}
777794

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
~~ deno: true ~~
2+
!! should indent the task list marker's second line !!
3+
## Test
4+
5+
- [ ] Go to the "cargo_publish" workflow in the CLI repo's actions:
6+
https://github.com/denoland/deno/actions/workflows/cargo_publish.yml
7+
1. Test
8+
9+
<details></details>
10+
11+
[expect]
12+
## Test
13+
14+
- [ ] Go to the "cargo_publish" workflow in the CLI repo's actions:
15+
https://github.com/denoland/deno/actions/workflows/cargo_publish.yml
16+
1. Test
17+
18+
<details></details>

tests/specs/TaskListMarker/TaskListMarker_All.txt

+14
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,17 @@
7171
```ts
7272
test
7373
```
74+
75+
!! should format across multiple lines !!
76+
### Some text
77+
78+
- [ ] Some text goes here. Testing testing testing testing testing testing testing.
79+
Here is some text on the next line
80+
1. Tesitng this out
81+
82+
[expect]
83+
### Some text
84+
85+
- [ ] Some text goes here. Testing testing testing testing testing testing testing.
86+
Here is some text on the next line
87+
1. Tesitng this out

tests/specs/TaskListMarker/TaskListMarker_TextWrap_Always.txt

+16
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,19 @@
1111
testing testing
1212
- [ ] some more text that will also wrap
1313
beyond the line width
14+
15+
!! should format across multiple lines !!
16+
### Some text
17+
18+
- [ ] Some text goes here. Testing testing testing testing testing testing testing.
19+
Here is some text on the next line
20+
1. Tesitng this out
21+
22+
[expect]
23+
### Some text
24+
25+
- [ ] Some text goes here. Testing
26+
testing testing testing testing
27+
testing testing. Here is some text
28+
on the next line
29+
1. Tesitng this out

0 commit comments

Comments
 (0)