Skip to content

Commit

Permalink
add centering to mermaid svg
Browse files Browse the repository at this point in the history
  • Loading branch information
falgon committed Sep 11, 2024
1 parent 40cd291 commit 71950e5
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Media/SVG.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import Control.Monad.Trans (MonadTrans (..))
import Control.Monad.Trans.Maybe (hoistMaybe, runMaybeT)
import Data.Functor ((<&>))
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Hakyll
import Lucid.Base (HtmlT, renderText, toHtmlRaw)
import Lucid.Html5
import System.Exit (ExitCode (..))
import System.Process (proc, readCreateProcessWithExitCode)
import Text.Pandoc (Block (..), Format (..),
Expand All @@ -24,10 +27,10 @@ optimizeSVGCompiler :: [String] -> Compiler (Item String)
optimizeSVGCompiler opts = getResourceString
>>= withItemBody (unixFilter "npx" $ ["svgo", "-i", "-", "-o", "-"] ++ opts)

execMmdc :: (MonadIO m, MonadThrow m) => T.Text -> m String
execMmdc :: (MonadIO m, Monad n, MonadThrow m) => T.Text -> m (HtmlT n ())
execMmdc = liftIO . readCreateProcessWithExitCode (proc "npx" args) . T.unpack >=> \case
(ExitFailure _, _, err) -> throwString err
(ExitSuccess, out, _) -> pure out
(ExitSuccess, out, _) -> pure $ toHtmlRaw $ T.pack out
where
args = ["mmdc", "-i", "/dev/stdin", "-e", "svg", "-o", "-"]

Expand All @@ -38,8 +41,10 @@ mermaidCodeBlock cb@(CodeBlock (_, _, t) contents) = maybe cb id <$>
mermaidCodeBlock' =
ifM ((/="mermaid") . T.toLower <$> hoistMaybe (lookup "lang" $ map (first $ T.unpack . T.toLower) t))
mzero $
lift $ unsafeCompiler (execMmdc contents)
<&> Plain . (:[]) . RawInline (Format "html") . T.pack
lift $ unsafeCompiler (execMmdc contents)
<&> Plain . (:[]) . RawInline (Format "html")
. TL.toStrict . renderText
. div_ [class_ "has-text-centered"]
mermaidCodeBlock x = pure x

-- | When a code block starts in @```{lang=mermaid}@,
Expand Down

0 comments on commit 71950e5

Please sign in to comment.