Skip to content

Commit

Permalink
add support for unordered list (#44)
Browse files Browse the repository at this point in the history
Co-authored-by: Josh McKinney <[email protected]>
  • Loading branch information
mxple and joshka authored Sep 20, 2024
1 parent 9da437d commit d332442
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion markdown-reader/TEST.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ This is a ~~complicated~~ *very simple* example.
### Unordered

- Item 1
- Item 2 (currently buggy)
- Item 2
- Item 2a
- Item 2b

Expand Down
20 changes: 13 additions & 7 deletions tui-markdown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct TextWriter<'a, I> {
style: Style,

/// Current list index as a stack of indices.
list_index: Vec<u64>,
list_index: Vec<Option<u64>>,
}

impl<'a, I> TextWriter<'a, I>
Expand Down Expand Up @@ -94,16 +94,22 @@ where
Tag::CodeBlock(kind) => self.start_codeblock(kind),
Tag::HtmlBlock => todo!(),
Tag::List(start_index) => {
// TODO handle unordered lists properly (start_index is None for unordered lists)
self.start_list(start_index.unwrap_or(0));
if let Some(line) = self.line.take() {
self.text.lines.push(line);
}
self.start_list(start_index);
}
Tag::Item => {
let width = self.list_index.len() * 4 - 3;
if let Some(index) = self.list_index.last_mut() {
let span =
Span::styled(format!("{:width$}. ", index), Style::new().light_blue());
let span = match index {
None => Span::from(" ".repeat(width - 1) + "- "),
Some(index) => {
*index += 1;
format!("{:width$}. ", *index - 1).light_blue()
}
};
self.line = Some(span.into());
*index += 1;
}
}
Tag::FootnoteDefinition(_) => todo!(),
Expand Down Expand Up @@ -225,7 +231,7 @@ where
}
}

fn start_list(&mut self, index: u64) {
fn start_list(&mut self, index: Option<u64>) {
self.list_index.push(index);
}

Expand Down

0 comments on commit d332442

Please sign in to comment.