module Text.MMark.Extension.Skylighting
( skylighting )
where
import Lucid (toHtmlRaw)
import Text.Blaze.Html.Renderer.Text
import Text.MMark.Extension (Extension, Block (..))
import qualified Data.Text as T
import qualified Skylighting as S
import qualified Text.MMark.Extension as Ext
skylighting
:: S.FormatOptions
-> Extension
skylighting fmtOpts = Ext.blockRender $ \old block ->
case block of
cb@(CodeBlock (Just infoString') txt) ->
let tokenizerConfig = S.TokenizerConfig
{ S.syntaxMap = S.defaultSyntaxMap
, S.traceOutput = False }
infoString = T.replace "-" " " infoString'
in case S.lookupSyntax infoString S.defaultSyntaxMap of
Nothing -> old cb
Just syntax ->
case S.tokenize tokenizerConfig syntax txt of
Left _ -> old cb
Right sourceLines -> toHtmlRaw . renderHtml $
S.formatHtmlBlock fmtOpts sourceLines
other -> old other