Skip to content

Commit

Permalink
lists can now have a depth (0,1,2,3)
Browse files Browse the repository at this point in the history
Just put 1, 2, or 3 spaces before the bullet to have
a deeper list item
  • Loading branch information
Canop committed Mar 2, 2023
1 parent 695286e commit b7a4b0e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minimad"
version = "0.10.0"
version = "0.11.0"
authors = ["dystroy <denys.seguret@gmail.com>"]
repository = "https://github.com/Canop/minimad"
description = "light Markdown parser"
Expand Down
2 changes: 1 addition & 1 deletion src/markdown/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::*;
pub enum CompositeStyle {
Paragraph,
Header(u8), // never 0, and <= MAX_HEADER_DEPTH
ListItem,
ListItem(u8), // can't be built > 3 by parsing
Code,
Quote,
}
Expand Down
4 changes: 2 additions & 2 deletions src/markdown/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl Line<'_> {
compounds,
})
}
pub fn new_list_item(compounds: Vec<Compound<'_>>) -> Line<'_> {
pub fn new_list_item(depth: u8, compounds: Vec<Compound<'_>>) -> Line<'_> {
Line::Normal(Composite {
style: CompositeStyle::ListItem,
style: CompositeStyle::ListItem(depth),
compounds,
})
}
Expand Down
55 changes: 52 additions & 3 deletions src/parser/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,16 @@ impl<'s> LineParser<'s> {
self.idx += 1;
let style = if self.src[self.idx..].starts_with("* ") {
self.idx += 2;
CompositeStyle::ListItem
CompositeStyle::ListItem(0)
} else if self.src[self.idx..].starts_with(" * ") {
self.idx += 3;
CompositeStyle::ListItem(1)
} else if self.src[self.idx..].starts_with(" * ") {
self.idx += 4;
CompositeStyle::ListItem(2)
} else if self.src[self.idx..].starts_with(" * ") {
self.idx += 5;
CompositeStyle::ListItem(3)
} else if self.src[self.idx..].starts_with("> ") {
self.idx += 2;
CompositeStyle::Quote
Expand Down Expand Up @@ -246,7 +255,19 @@ impl<'s> LineParser<'s> {
}
if self.src.starts_with("* ") {
self.idx = 2;
return Line::new_list_item(self.parse_compounds(false));
return Line::new_list_item(0, self.parse_compounds(false));
}
if self.src.starts_with(" * ") {
self.idx = 3;
return Line::new_list_item(1, self.parse_compounds(false));
}
if self.src.starts_with(" * ") {
self.idx = 4;
return Line::new_list_item(2, self.parse_compounds(false));
}
if self.src.starts_with(" * ") {
self.idx = 5;
return Line::new_list_item(3, self.parse_compounds(false));
}
if self.src == ">" {
return Line::new_quote(Vec::new());
Expand Down Expand Up @@ -473,11 +494,39 @@ mod tests {
fn list_item() {
assert_eq!(
Line::from("* *list* item"),
Line::new_list_item(vec![
Line::new_list_item(0, vec![
Compound::raw_str("list").italic(),
Compound::raw_str(" item"),
])
);
}

#[test]
fn deep_list_items() {
assert_eq!(
Line::from(" * *list* item"),
Line::new_list_item(1, vec![
Compound::raw_str("list").italic(),
Compound::raw_str(" item"),
])
);
assert_eq!(
Line::from(" * deeper"),
Line::new_list_item(2, vec![
Compound::raw_str("deeper"),
])
);
assert_eq!(
Line::from(" * even **deeper**"),
Line::new_list_item(3, vec![
Compound::raw_str("even "),
Compound::raw_str("deeper").bold(),
])
);
assert_eq!(
Line::from(" * but not this one..."),
Line::new_code(Compound::raw_str("* but not this one...").code()),
);
}

#[test]
Expand Down

0 comments on commit b7a4b0e

Please sign in to comment.