sirthias / pegdown

A pure-Java Markdown processor based on a parboiled PEG parser supporting a number of extensions
http://pegdown.org
Apache License 2.0
1.29k stars 218 forks source link

About Code Block #157

Closed takkuumi closed 9 years ago

takkuumi commented 9 years ago

When i use pegdown to parse markdown with Code Block,i have found it print two diffrenent result.

1. Input:

String html = "1111\n" +
                "```c\n" +
                "int main(){\n" +
                "    retirm 0;\n" +
                "}\n" +
                "```";

Output:

<p>1111<br/><code>c
int main(){
    retirm 0;
}
</code></p>

2. Input:

String html = "1111\n" +
                "\n" +
                "```c\n" +
                "int main(){\n" +
                "    retirm 0;\n" +
                "}\n" +
                "```";

Output:

<p>1111</p>
<pre><code class="c">int main(){
    retirm 0;
}
</code></pre>

If no world before ```c,it output:

<pre><code class="c">int main(){
    retirm 0;
}
</code></pre>

I just want the Code Block result like

<pre><code class="c">int main(){
    retirm 0;
}
</code></pre>

@sirthias How to do it?

This is my config:

  PegDownPlugins plugins = PegDownPlugins.builder().withPlugin(EmojiParser.class).build();
  PegDownProcessor remark = new PegDownProcessor(Extensions.ALL, plugins);
  String md = remark.markdownToHtml(html);

EmojiParser is my own plugin.

thanks!

vsch commented 9 years ago

Current parser needs a blank line before the code fence as you have in input 2 or the para block is not closed as you have for input 1.

It is a bug.

Need to change parser Rule to insert a test for fenced code (regression tests pass):

    public Rule NormalEndline() {
        return Sequence(
                Sp(), Newline(),
                TestNot(
                        FirstOf(
                                BlankLine(),
                                '>',
                                AtxStart(),
                                Sequence(ZeroOrMore(NotNewline(), ANY), Newline(),
                                        FirstOf(NOrMore('=', 3), NOrMore('-', 3)), Newline()),
                                // test for fenced code 
                                FencedCodeBlock()
                        )
                ),
                ext(HARDWRAPS) ? toRule(push(new SimpleNode(Type.Linebreak))) : toRule(push(new TextNode(" ")))
        );
    }