kivikakk / cmark-gfm-hs

Haskell bindings to libcmark-gfm GitHub Flavored Markdown parser
Other
13 stars 6 forks source link

C code crashes when rendering strikethrough #29

Open brandonchinn178 opened 1 year ago

brandonchinn178 commented 1 year ago

This works:

commonmarkToHtml [] [extStrikethrough] "a ~b~ c"

But this fails:

nodeToHtml [] [extStrikethrough] $ commonmarkToNode [] [extStrikethrough] "a ~b~ c"
Assertion failed: (false), function S_render_node, file html.c, line 445.
kivikakk commented 1 year ago

We need to use the cmark_node_new_with_ext variant to create the strikethrough node. I have the start of something here:

diff --git a/CMarkGFM.hsc b/CMarkGFM.hsc
index db2a194..103aee4 100644
--- a/CMarkGFM.hsc
+++ b/CMarkGFM.hsc
@@ -500,7 +500,10 @@ fromNode (Node _ nodeType children) = do
                      return n
             SOFTBREAK   -> c_cmark_node_new (#const CMARK_NODE_SOFTBREAK)
             LINEBREAK   -> c_cmark_node_new (#const CMARK_NODE_LINEBREAK)
-            STRIKETHROUGH -> c_cmark_node_new (fromIntegral . Unsafe.unsafePerformIO $ peek c_CMARK_NODE_STRIKETHROUGH)
+            STRIKETHROUGH -> do
+                     [ext] <- resolveExts [extStrikethrough]
+                     nodeType <- fromIntegral <$> peek c_CMARK_NODE_STRIKETHROUGH
+                     c_cmark_node_new_with_ext nodeType ext
             TABLE _             -> error "constructing table not supported"
             TABLE_ROW           -> error "constructing table row not supported"
             TABLE_CELL          -> error "constructing table cell not supported"
@@ -525,6 +528,9 @@ foreign import ccall "string.h strlen"
 foreign import ccall "cmark-gfm.h cmark_node_new"
     c_cmark_node_new :: Int -> IO NodePtr

+foreign import ccall "cmark-gfm.h cmark_node_new_with_ext"
+    c_cmark_node_new_with_ext :: Int -> ExtensionPtr -> IO NodePtr
+
 foreign import ccall "cmark-gfm.h cmark_render_html"
     c_cmark_render_html :: NodePtr -> CInt -> LlistPtr ExtensionPtr -> IO CString

diff --git a/test/test-cmark.hs b/test/test-cmark.hs
index 6f53c3a..bdb3333 100644
--- a/test/test-cmark.hs
+++ b/test/test-cmark.hs
@@ -34,5 +34,6 @@ tests = TestList [
   , "&lt;xmp>\n" ~=? commonmarkToHtml [optUnsafe] [extTagfilter] "<xmp>"
   , "<ul>\n<li><input type=\"checkbox\" disabled=\"\" /> foo</li>\n<li><input type=\"checkbox\" checked=\"\" disabled=\"\" /> bar</li>\n</ul>\n" ~=? commonmarkToHtml [] [extTaskList] "- [ ] foo\n- [x] bar"
   , "<p>Here is footnote<sup class=\"footnote-ref\"><a href=\"#fn-1\" id=\"fnref-1\" data-footnote-ref>1</a></sup></p>\n<section class=\"footnotes\" data-footnotes>\n<ol>\n<li id=\"fn-1\">\n<p>abc <a href=\"#fnref-1\" class=\"footnote-backref\" data-footnote-backref aria-label=\"Back to content\">↩</a></p>\n</li>\n</ol>\n</section>\n" ~=? commonmarkToHtml [optFootnotes] [] "Here is footnote[^1]\n\n[^1]: abc"
+  , "<p>lol</p>" ~=? nodeToHtml [] [extStrikethrough] $ commonmarkToNode [] [extStrikethrough] "a ~b~ c"
   ]

Unfortunately I haven't the time to get a development environment for this setup again presently, so I leave this open to a contributor.