pulldown-cmark-to-cmark icon indicating copy to clipboard operation
pulldown-cmark-to-cmark copied to clipboard

`\\` is translated inconsitently

Open LuckyTurtleDev opened this issue 2 years ago • 2 comments

I have try to prase markdown with the pulldown-cmark crate and to convert it back, using this crate. But \\ is translated back inconsistent. At some point \\ is converted to \ and at other points it is still \\. For Example This line occurs in my markdown files multiple times \\( \int x dx = \frac{x^2}{2} + C \\). The first time it stay the same, all other time it result in \( \int x dx = \frac{x^2}{2} + C \), so the mathjax expression are not render by the mdbook. image

code:

        let markdown_copy = markdown.clone();
        eprintln!("{markdown_copy}");
        let parser = pulldown_cmark::Parser::new(&markdown_copy);
        let mut in_code_block = false;
        let iter = parser.into_iter().map(|f| {
            eprintln!("in  {f:?}");
            let f =match f {
                Event::Text(text) => Event::Text(CowStr::from(text.replace("=>", "⇒"))),
                Event::Start(Tag::CodeBlock(value)) => {in_code_block = true; Event::Start(Tag::CodeBlock(value))},
                Event::End(Tag::CodeBlock(value)) => {in_code_block = false; Event::Start(Tag::CodeBlock(value))}
                f => f 
            };
            eprintln!("out {f:?}");
            f
        });
        let mut output = String::new();
        pulldown_cmark_to_cmark::cmark(iter, &mut output).unwrap();
        eprintln!("{output}");

output: input.md

in  Start(Heading(H1, None, []))
out Start(Heading(H1, None, []))
in  Text(Borrowed("Chapter 1"))
out Text(Boxed("Chapter 1"))
in  End(Heading(H1, None, []))
out End(Heading(H1, None, []))
in  Start(Paragraph)
out Start(Paragraph)
in  Text(Borrowed("hello world."))
out Text(Boxed("hello world."))
in  SoftBreak
out SoftBreak
in  Text(Borrowed("This is a "))
out Text(Boxed("This is a "))
in  Start(Link(Inline, Borrowed("https://duckduckgo.com"), Borrowed("")))
out Start(Link(Inline, Borrowed("https://duckduckgo.com"), Borrowed("")))
in  Text(Borrowed("duck"))
out Text(Boxed("duck"))
in  End(Link(Inline, Borrowed("https://duckduckgo.com"), Borrowed("")))
out End(Link(Inline, Borrowed("https://duckduckgo.com"), Borrowed("")))
in  SoftBreak
out SoftBreak
in  Text(Borrowed("arrows: -> = >"))
out Text(Boxed("arrows: -> = >"))
in  SoftBreak
out SoftBreak
in  Text(Borrowed("ee"))
out Text(Boxed("ee"))
in  End(Paragraph)
out End(Paragraph)
in  Start(Paragraph)
out Start(Paragraph)
in  Text(Borrowed("\\( \\int x dx = \\frac{x^2}{2} + C "))
out Text(Boxed("\\( \\int x dx = \\frac{x^2}{2} + C "))
in  Text(Borrowed("\\)"))
out Text(Boxed("\\)"))
in  End(Paragraph)
out End(Paragraph)
in  Start(CodeBlock(Fenced(Borrowed(""))))
out Start(CodeBlock(Fenced(Borrowed(""))))
in  Text(Borrowed("this arrows should not be replaced:\n-> =>\n"))
out Text(Boxed("this arrows should not be replaced:\n-> ⇒\n"))
in  End(CodeBlock(Fenced(Borrowed(""))))
out Start(CodeBlock(Fenced(Borrowed(""))))
in  Start(Paragraph)
out Start(Paragraph)
in  Text(Borrowed("\\( \\int x dx = \\frac{x^2}{2} + C "))
out Text(Boxed("\\( \\int x dx = \\frac{x^2}{2} + C "))
in  Text(Borrowed("\\)"))
out Text(Boxed("\\)"))
in  End(Paragraph)
out End(Paragraph)
in  Start(Paragraph)
out Start(Paragraph)
in  Text(Borrowed("this should also not be replaced "))
out Text(Boxed("this should also not be replaced "))
in  Code(Borrowed("->"))
out Code(Borrowed("->"))
in  Text(Borrowed(" "))
out Text(Boxed(" "))
in  Code(Borrowed("=>"))
out Code(Borrowed("=>"))
in  Text(Borrowed(". "))
out Text(Boxed(". "))
in  End(Paragraph)
out End(Paragraph)
in  Start(Paragraph)
out Start(Paragraph)
in  Text(Borrowed("\\"))
out Text(Boxed("\\"))
in  Text(Borrowed("["))
out Text(Boxed("["))
in  Text(Borrowed(" \\mu = \\frac{1}{N} \\sum"))
out Text(Boxed(" \\mu = \\frac{1}{N} \\sum"))
in  Text(Borrowed("_"))
out Text(Boxed("_"))
in  Text(Borrowed("{i=0} x_i \\Rightarrow"))
out Text(Boxed("{i=0} x_i \\Rightarrow"))
in  Text(Borrowed("\\"))
out Text(Boxed("\\"))
in  Text(Borrowed("]"))
out Text(Boxed("]"))
in  End(Paragraph)
out End(Paragraph)

output.md

I war forced to upload the markdowns as file, because otherwise github has try to interpret it.

LuckyTurtleDev avatar Nov 24 '23 16:11 LuckyTurtleDev

Thanks for posting!

Assuming you are using the latest version, I can imagine something like this to happen for many reasons, and believe it would be best if you'd go ahead and add test-cases for the markdown that turned out to be problematic. From there it should be possible for you to figure out where escapes are handled incorrectly.

Sometimes, the problem will be here, other times it's in the markdown parser itself which degenerates information. A PR with a failing test, and maybe even a fix, is definitely welcome if there is a solution for this problem.

Byron avatar Nov 24 '23 16:11 Byron

other times it's in the markdown parser itself which degenerates information.

I think the markdown is parse right. Because the generated events looks fine and like expected.

LuckyTurtleDev avatar Nov 28 '23 13:11 LuckyTurtleDev