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 217 forks source link

Code block not detected? #223

Closed shred closed 8 years ago

shred commented 8 years ago

This example code:

StringBuilder sb = new StringBuilder();
sb.append("```java\n");
sb.append("String foo = \"123\";\n");
sb.append("```\n");
System.out.println(new PegDownProcessor().markdownToHtml(sb.toString()));

returns the following HTML:

<p><code>java
String foo = &quot;123&quot;;
</code></p>

It should be <pre> instead of <p><code>, and "java" should not be visible in the output.

I'm using pegdown 1.6.0.

vsch commented 8 years ago

@shred do you have Extensions.FENCED_CODE_BLOCKS option enabled?, without it pegdown does not recognize code fenced blocks.

shred commented 8 years ago

I completely missed the Extensions... It works now.

Thank you! And I'm sorry for the noise. :-)

Clay-Ferguson commented 8 years ago

It works if you do this:

proc = new PegDownProcessor(Extensions.FENCED_CODE_BLOCKS);

but all that does is put in the class="javascript" for example. Where would I find the javascript CSS, or CSS for all the languages ?

vsch commented 8 years ago

@Clay-Ferguson, you need to include your favourite JavaScript syntax highlighter with all that it requires to get the benefit of classes assigned to <code> tags in your generated HTML.

I use https://highlightjs.org/ with my idea-multimarkdown plugin for preview syntax highlighting in fenced code.

pegdown does the grunt work of markdown parsing. Basic AST to HTML serialization is provided. HTML page specifics and more complex HTML generation schemes are left as an exercise to the user.

Clay-Ferguson commented 8 years ago

@vsch Thanks for the help! I have been using google prettify: https://github.com/google/code-prettify and it is awesome but it needs something of this format: <pre class='prettyprint' lang-js> As you can see the class is always 'prettyprint' and the language is specified as attribute.

Update: See my comment in this thread: https://github.com/google/code-prettify/issues/224

Essentially this pattern works (using google prettyPrint):

<?prettify lang=js?>
<pre class='prettyprint'>
var x = 10;
var y = "this is a string";
</pre>