cmark-gfm-hs icon indicating copy to clipboard operation
cmark-gfm-hs copied to clipboard

Any way to make library safer?

Open MichaelXavier opened this issue 3 years ago • 4 comments

I was trying to write a test generator that generated some random markdown. I decided to start with the Node type since it seemed easier than generating valid markdown text first and parsing it. I found that if you construct an apparently invalid markdown: Node Nothing PARAGRAPH [Node Nothing DOCUMENT mempty], running nodeToHtml against it results in a SIGSEGV and crashes the entire program.

  1. Is there any possible way this can be reified into the library so that any rendering function that could segfault could instead return an Either?
  2. I don't know much about the markdown standard but is there a way to make invalid nodes inexpressable in the types?

MichaelXavier avatar Sep 01 '21 19:09 MichaelXavier

If you want something safer and more Haskelly I'd recommend my pure Haskell commonmark library (commonmark on Hackage; commonmark-extensions will give you GitHub extensions).

jgm/commonmark-hs on GitHub

Since writing that I haven't had much use for the cmark wrapper, so I'm not personally inclined to put much time into it.

jgm avatar Sep 01 '21 20:09 jgm

I would be inclined to follow @jgm's lead here! :)

kivikakk avatar Sep 01 '21 23:09 kivikakk

That said, my guess is that there's probably a simple fix possible for the issue noted here. I just don't have time to look into it now.

jgm avatar Sep 02 '21 15:09 jgm

I just threw together this library, which aims to have a more correct-by-construction AST that's easy to transform (which isn't very easy in commonmark: https://github.com/jgm/commonmark-hs/pull/103). It'll still fail when rendering an ill-formatted AST, but it should be good enough for my use-case.

https://github.com/brandonchinn178/cmark-gfm-simple

brandonchinn178 avatar Feb 05 '23 04:02 brandonchinn178