fixed weird block parsing issues
All checks were successful
Test the running changes / Test (push) Successful in 35s
All checks were successful
Test the running changes / Test (push) Successful in 35s
This commit is contained in:
@@ -3,6 +3,7 @@ use nom::{
|
||||
IResult, Parser,
|
||||
branch::alt,
|
||||
bytes::complete::{tag, take_until},
|
||||
combinator::peek,
|
||||
multi::{many_m_n, many0, many1},
|
||||
sequence::{delimited, terminated},
|
||||
};
|
||||
@@ -14,6 +15,7 @@ pub fn blocks(input: &str) -> IResult<&str, Vec<Block>, MarkdownParseError> {
|
||||
}
|
||||
|
||||
pub fn block(input: &str) -> IResult<&str, Block, MarkdownParseError> {
|
||||
//alt((heading_block, code_block, quote_block, paragraph_block)).parse(input)
|
||||
terminated(
|
||||
alt((heading_block, code_block, quote_block, paragraph_block)),
|
||||
tag("\n"),
|
||||
@@ -45,7 +47,7 @@ fn code_block(input: &str) -> IResult<&str, Block, MarkdownParseError> {
|
||||
delimited(
|
||||
tag("```"),
|
||||
(take_until("\n"), tag("\n"), take_until("```\n")),
|
||||
tag("```\n"),
|
||||
(tag("```"), peek(tag("\n"))),
|
||||
)
|
||||
.parse(input)
|
||||
.map(|(rem, (lang, _, code))| {
|
||||
@@ -83,10 +85,10 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn single_paragraph() {
|
||||
let md = "Hello markdown!!";
|
||||
let md = "Hello markdown!!\n";
|
||||
let (rem, block) = paragraph_block(md).unwrap();
|
||||
|
||||
assert_eq!(rem, "");
|
||||
assert_eq!(rem, "\n");
|
||||
assert_eq!(
|
||||
block,
|
||||
Block::Paragraph {
|
||||
@@ -107,7 +109,7 @@ fn main() {
|
||||
";
|
||||
let (rem, block) = code_block(md).unwrap();
|
||||
|
||||
assert_eq!(rem, "");
|
||||
assert_eq!(rem, "\n");
|
||||
assert_eq!(
|
||||
block,
|
||||
Block::Code {
|
||||
@@ -125,7 +127,7 @@ echo \"hello world\"
|
||||
";
|
||||
let (rem, block) = code_block(md).unwrap();
|
||||
|
||||
assert_eq!(rem, "");
|
||||
assert_eq!(rem, "\n");
|
||||
assert_eq!(
|
||||
block,
|
||||
Block::Code {
|
||||
@@ -145,11 +147,11 @@ echo hello
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn level_1_heading() {
|
||||
let md = "## Heading2";
|
||||
fn level_2_heading() {
|
||||
let md = "## Heading2\n";
|
||||
let (rem, block) = heading_block(md).unwrap();
|
||||
|
||||
assert_eq!(rem, "");
|
||||
assert_eq!(rem, "\n");
|
||||
assert_eq!(
|
||||
block,
|
||||
Block::Heading {
|
||||
@@ -163,16 +165,16 @@ echo hello
|
||||
|
||||
#[test]
|
||||
fn heading_no_space() {
|
||||
let md = "#heading";
|
||||
let md = "#heading\n";
|
||||
assert!(heading_block(md).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn level_6_heading() {
|
||||
let md = "###### Heading6";
|
||||
let md = "###### Heading6\n";
|
||||
let (rem, block) = heading_block(md).unwrap();
|
||||
|
||||
assert_eq!(rem, "");
|
||||
assert_eq!(rem, "\n");
|
||||
assert_eq!(
|
||||
block,
|
||||
Block::Heading {
|
||||
@@ -186,7 +188,7 @@ echo hello
|
||||
|
||||
#[test]
|
||||
fn no_level_7_heading() {
|
||||
let md = "####### Heading7";
|
||||
let md = "####### Heading7\n";
|
||||
assert!(heading_block(md).is_err());
|
||||
}
|
||||
|
||||
|
||||
@@ -85,10 +85,10 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn single_text() {
|
||||
let md = "hello normal inline";
|
||||
let md = "hello normal inline\n";
|
||||
let (rem, parsed) = text_inline(md).unwrap();
|
||||
|
||||
assert_eq!(rem, "");
|
||||
assert_eq!(rem, "\n");
|
||||
assert_eq!(
|
||||
parsed,
|
||||
Inline::Text {
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
//! A weird markdown parser. Please don't forget to add a newline in the end of a file or it won't
|
||||
//! work :)
|
||||
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
pub mod block;
|
||||
|
||||
Reference in New Issue
Block a user