Parser: add "title" field and set it with the first heading found

This commit is contained in:
Nathan Fisher 2023-06-02 10:03:02 -04:00
parent a9fcab7006
commit 1dd66684bf

View file

@ -112,6 +112,7 @@ impl<'a> GemtextNode {
#[derive(Debug)] #[derive(Debug)]
pub struct Parser<'a> { pub struct Parser<'a> {
state: State<'a>, state: State<'a>,
title: Option<String>,
lines: Vec<GemtextNode>, lines: Vec<GemtextNode>,
} }
@ -119,6 +120,7 @@ impl<'a> Parser<'a> {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
state: State::Normal, state: State::Normal,
title: None,
lines: vec![] lines: vec![]
} }
} }
@ -139,7 +141,14 @@ impl<'a> Parser<'a> {
} }
fn heading(&mut self, line: &'a str) { fn heading(&mut self, line: &'a str) {
self.lines.push(GemtextNode::parse_heading(line)); let line = GemtextNode::parse_heading(line);
if self.title.is_none() {
match &line {
GemtextNode::Heading1(t) => self.title = Some(t.clone()),
_ => {},
}
}
self.lines.push(line);
} }
fn list_item(&mut self, line: &'a str) { fn list_item(&mut self, line: &'a str) {