FSX / misaka

A Python binding for Hoedown.
http://misaka.61924.nl
MIT License
418 stars 65 forks source link

GFM how to #45

Closed samuelcolvin closed 7 years ago

samuelcolvin commented 9 years ago

GFM (github flavoured markdown) is very popular these days. It would be great if there was some help somewhere on how to get misaka to generate GFM.

I know it's never going to be identical but some pointers for the most important features would be useful.

Currently my solution to get something close to GFM's linebreak behavior is:

def markdown(md_str):
    render = HtmlRenderer()
    md = Markdown(render, extensions=misaka.EXT_NO_INTRA_EMPHASIS)
    md_str = re.sub(r'(?<!  )\n(?!\n)(?=.)', '  \n', md_str)
    return md.render(md_str)

The regular expression is required since gfm does:

"first sentence\nsecond sentence" --> "<p>first sentence<br />second sentence"

Whereas hoedown and misaka require two spaces before the \n to insert the <br />. I tried overwriting most of the render methods but couldn't find a solution to the above.

There are probably other differences (particularly on tables) but newlines are most people's first stumbling block with markdown.

(updated to regex to use lookahead/behinds properly).

samuelcolvin commented 9 years ago

looks like this might be useful too, but it used the old version of misaka and therefore EXT_LAX_HTML_BLOCKS which doesn't exist for version 2.x.

FSX commented 9 years ago

Goodmorning Samuel, I'll take a look at it.

FSX commented 9 years ago

EXT_LAX_HTML_BLOCKS is not available in Hoedown, the parser that Misaka uses. You can use HTML_HARD_WRAP to insert <br> on each linebreak.

samuelcolvin commented 9 years ago

I had realised that EXT_LAX_HTML_BLOCKS wasn't available, that's why I was looking for a better solution.

Thanks for pointing out HTML_HARD_WRAP, very useful.