From f67606cbdb43ea6e46372feb80c49e91db05725b Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 19 Jan 2025 19:58:14 +1300 Subject: Implement Math block A math block is a line that begins and ends with a pair of dollar-signs. --- src/lib.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index e75221c..ac00e3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -108,9 +108,12 @@ impl MarkdownDocument { Block::Heading { level, line: Line::from_str(&line) }), BlockLine::Break => blocks.push(Block::Break), BlockLine::BlankLine => (), - BlockLine::Paragraph(line) => match parse_embed(&line) { - Some(embed) => blocks.push(embed), - None => blocks.push(Block::Paragraph(Line::from_str(&line))), + BlockLine::Paragraph(line) => if let Some(embed) = parse_embed(&line) { + blocks.push(embed) + } else if let Some(math) = parse_math(&line) { + blocks.push(math) + } else { + blocks.push(Block::Paragraph(Line::from_str(&line))) } } } @@ -157,3 +160,12 @@ fn parse_embed(line: &str) -> Option { return None; } +fn parse_math(line: &str) -> Option { + let line = line.trim(); + if let Some(stripped) = line.strip_prefix("$$") { + if let Some(stripped) = stripped.strip_suffix("$$") { + return Some(Block::Math(stripped.trim().to_string())) + } + } + return None; +} -- cgit v1.2.3-70-g09d2