From 384bac2d79ad137a29ff45913d63dd29d1104747 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 14 Jan 2025 19:09:59 +1300 Subject: Parse checkbox lists If a list item line begins with `[ ]` or `[x]`, the token is interpreted as a checkbox and replaced with a real HTML checkbox. --- src/generate_html.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/generate_html.rs b/src/generate_html.rs index d48e3fe..129e2dd 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -124,21 +124,32 @@ pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Webs let mut depth = 0; let mut prev = '\0'; let mut output = String::new(); + let mut class = String::new(); for c in line_to_html!(line).chars() { output.push(c); if c == '<' { depth += 1; } else if c == '/' && prev == '<' { - depth -= 2; + depth -= 2; // 2 because prev was a '<' as well. } else if c == ':' && depth == 0 { - output.pop(); output.push_str("
"); depth += 99; + output.pop(); output.push_str("
"); + class.push_str("extended"); depth += 99; } prev = c; } - match output.contains("
") { - true => html!("
  • {output}
  • "), - false => html!("
  • {output}
  • "), + // Replace a leading checkbox with a real checkbox. + if let Some(stripped) = output.strip_prefix("[ ]") { + output = format!("{stripped}"); + class.push_str(" checkbox"); + } else if let Some(stripped) = output.strip_prefix("[x]") { + output = format!("{stripped}"); + class.push_str(" checkbox"); + } else if let Some(stripped) = output.strip_prefix("[X]") { + output = format!("{stripped}"); + class.push_str(" checkbox"); } + let class = class.trim(); + html!("
  • {output}
  • ") }), Block::Note(lines) => wrap!("aside", for line in lines { tag!("p", line) }), Block::Embedded { label, path } => match path.rsplit_once('.') { -- cgit v1.2.3-70-g09d2