Skip to content

Commit

Permalink
fix to RawInline
Browse files Browse the repository at this point in the history
  • Loading branch information
falgon committed Sep 10, 2024
1 parent c433afb commit 15e2b55
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/Media/SVG.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE LambdaCase, OverloadedStrings #-}
module Media.SVG (
optimizeSVGCompiler
, mermaidTransform
Expand All @@ -12,23 +12,24 @@ import qualified Data.Text as T
import Hakyll
import System.Exit (ExitCode (..))
import System.Process (proc, readCreateProcessWithExitCode)
import Text.Pandoc (Block (..), Pandoc)
import Text.Pandoc (Block (..), Format (..),
Inline (..), Pandoc)
import Text.Pandoc.Walk (walkM)

optimizeSVGCompiler :: [String] -> Compiler (Item String)
optimizeSVGCompiler opts = getResourceString >>=
withItemBody (unixFilter "npx" $ ["svgo", "-i", "-", "-o", "-"] ++ opts)

codeBlock :: Block -> Compiler Block
codeBlock cb@(CodeBlock attr@(_, _, t) contents) = runMaybeT codeBlock'
codeBlock cb@(CodeBlock (_, _, t) contents) = runMaybeT codeBlock'
>>= maybe (pure cb) pure
where
codeBlock' = do
lang <- T.unpack . T.toLower <$> hoistMaybe (lookup "lang" $ map (first $ T.unpack . T.toLower) t)
if lang /= "mermaid" then mzero else do
lift (unsafeCompiler $ readCreateProcessWithExitCode (proc "npx" args) $ T.unpack contents) >>= \case
(ExitFailure _, _, err) -> lift $ fail err
(ExitSuccess, out, _) -> pure $ CodeBlock attr $ T.pack out
(ExitSuccess, out, _) -> pure $ Plain [RawInline (Format "html") $ T.pack out]
args = ["mmdc", "-i", "/dev/stdin", "-e", "svg", "-o", "-"]
codeBlock x = pure x

Expand Down

0 comments on commit 15e2b55

Please sign in to comment.