Skip to content

Instantly share code, notes, and snippets.

@ehamberg
Created August 25, 2016 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ehamberg/d08a22bdf2eb5e15264f34f0511f6bd3 to your computer and use it in GitHub Desktop.
Save ehamberg/d08a22bdf2eb5e15264f34f0511f6bd3 to your computer and use it in GitHub Desktop.
{-# Language ForeignFunctionInterface #-}
{-# Language OverloadedStrings #-}
module Main where
import Text.Pandoc
import GHCJS.Marshal
import GHCJS.Foreign.Callback
import JavaScript.Object
import JavaScript.Object.Internal (Object(..))
convert :: String -> String
convert s = do
let readerOpts = def { readerSmart = True }
let Right doc = readMarkdown readerOpts s
let writerOptions = def { writerHtml5 = True }
writeHtmlString writerOptions doc
foreign import javascript unsafe "convert_ = $1"
js_set_convert :: Callback a -> IO ()
main :: IO ()
main = do
putStrLn "Haskell convert core starting..."
callback <- syncCallback1 ThrowWouldBlock $ \jsval -> do
let o = Object jsval
getProp "in" o
>>= fromJSValUnchecked
>>= pure . convert
>>= toJSVal
>>= \v -> setProp "out" v o
js_set_convert callback
putStr "Haskell convert callback initialized."
@ehamberg
Copy link
Author

ehamberg commented Aug 25, 2016

convert = function(s) {
  x = {};
  x.in = s;
  convert_(x);
  console.log(x.out);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment