diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-01-19 19:58:14 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-01-19 19:58:14 +1300 |
commit | f67606cbdb43ea6e46372feb80c49e91db05725b (patch) | |
tree | 1da6ebe3c39f0561a7b77989b401b23e5f649de2 | |
parent | e36eb90957231c70593477175befc89c41ebfe8e (diff) | |
download | markdown-f67606cbdb43ea6e46372feb80c49e91db05725b.zip |
Implement Math block
A math block is a line that begins and ends with a pair of dollar-signs.
-rw-r--r-- | src/block.rs | 1 | ||||
-rw-r--r-- | src/lib.rs | 18 |
2 files changed, 16 insertions, 3 deletions
diff --git a/src/block.rs b/src/block.rs index 1d6ac60..3ae39bf 100644 --- a/src/block.rs +++ b/src/block.rs @@ -13,6 +13,7 @@ pub enum Block { Paragraph(Line), List(Vec<Line>), Note(Vec<Line>), + Math(String), Table(Table), Break, Embed { label: String, path: String }, @@ -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<Block> { return None; } +fn parse_math(line: &str) -> Option<Block> { + 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; +} |