diff --git a/src/lib.rs b/src/lib.rs index 126af60..4882c66 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -234,13 +234,21 @@ pub enum Atom { } impl<'s> Container<'s> { - fn from_leaf_block(content: &str, l: block::Leaf) -> Self { + fn from_leaf_block(content: &'s str, l: block::Leaf) -> Self { match l { block::Leaf::Paragraph => Self::Paragraph, block::Leaf::Heading => Self::Heading { level: content.len(), }, - block::Leaf::CodeBlock => panic!(), + block::Leaf::CodeBlock => { + if let Some(format) = content.strip_prefix('=') { + Self::RawBlock { format } + } else { + Self::CodeBlock { + lang: (!content.is_empty()).then(|| content), + } + } + } _ => todo!(), } } @@ -373,12 +381,7 @@ impl<'s> Parser<'s> { block::Node::Leaf(l) => { self.inlines.set_spans(self.tree.inlines()); self.inline_parser = Some(inline::Parser::new(self.inlines.chars())); - let container = match l { - block::Leaf::CodeBlock { .. } => Container::CodeBlock { - lang: (!ev.span.is_empty()).then(|| content), - }, - _ => Container::from_leaf_block(content, l), - }; + let container = Container::from_leaf_block(content, l); Event::Start(container, attributes) } block::Node::Container(c) => { @@ -561,6 +564,17 @@ mod test { ); } + #[test] + fn raw_block() { + test_parse!( + "``` =html\n\n```", + Start(RawBlock { format: "html" }, Attributes::new()), + Str("
".into()), + Atom(Softbreak), + End(RawBlock { format: "html" }), + ); + } + #[test] fn link_inline() { test_parse!(