diff --git a/example_todo.md b/example_todo.md index 1870df4..a9465c0 100644 --- a/example_todo.md +++ b/example_todo.md @@ -1,3 +1,4 @@ +# Example TODO List - [ ] ((2024-06-20 21:00)) Take out the trash - [x] ((2024-06-20 16:00)) Get groceries - [ ] ((2024-06-20 20:00)) Do the dishes diff --git a/src/md.rs b/src/md.rs index 0f40768..4e6952c 100644 --- a/src/md.rs +++ b/src/md.rs @@ -136,6 +136,23 @@ impl Markdown for Document { fn from_md(md: String) -> anyhow::Result { let mut document = Document::default(); for line in md.lines() { + if document.title.is_none() { + let mut chars = line.chars().peekable(); + let mut count = 0; + while chars.peek().is_some_and(|c| c.is_ascii_whitespace()) { + chars.next(); + }; + while chars.peek().is_some_and(|&c| c == '#') { + count += 1; + chars.next(); + }; + while chars.peek().is_some_and(|c| c.is_ascii_whitespace()) { + chars.next(); + }; + if count == 1 { + document.title = Some(chars.collect()); + } + } if let Ok(entry) = Entry::from_md(line.to_string()) { document.entries.push(entry); } @@ -143,3 +160,4 @@ impl Markdown for Document { Ok(document) } } +