{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module Text.MMark.Extension.LinkTarget
( linkTarget )
where
import Data.Foldable (asum)
import Data.Maybe (fromMaybe)
import Lucid
import Text.MMark.Extension (Extension, Inline (..))
import qualified Data.Text as T
import qualified Text.MMark.Extension as Ext
linkTarget :: Extension
linkTarget = Ext.inlineRender $ \old inline ->
case inline of
l@(Link txt url (Just title)) -> fromMaybe (old l) $ do
let f prefix = (prefix,) . T.stripStart
<$> T.stripPrefix prefix title
(prefix, title') <- asum $
f <$> ["_blank", "_self", "_parent", "_top"]
let mtitle = if T.null title' then Nothing else Just title'
return $ with (old (Link txt url mtitle)) [target_ prefix]
other -> old other