-- | -- Module : Text.MMark.Extension.ObfuscateEmail -- Copyright : © 2018 Mark Karpov -- License : BSD 3 clause -- -- Maintainer : Mark Karpov -- Stability : experimental -- Portability : portable -- -- Obfuscate email addresses. {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} module Text.MMark.Extension.ObfuscateEmail ( obfuscateEmail ) where import Data.List.NonEmpty (NonEmpty (..)) import Data.Text (Text) import Lucid import Text.MMark.Extension (Extension, Inline (..)) import Text.URI.QQ (scheme, uri) import qualified Text.MMark.Extension as Ext import qualified Text.URI as URI -- | This extension makes email addresses in autolinks be rendered as -- something like this: -- -- > data-email="something@example.org" -- > href="javascript:void(0)">Enable JavaScript to see this email -- -- You'll also need to include jQuery and this bit of JS code for the magic -- to work: -- -- > $(document).ready(function () { -- > $(".protected-email").each(function () { -- > var item = $(this); -- > var email = item.data('email'); -- > item.attr('href', 'mailto:' + email); -- > item.html(email); -- > }); -- > }); obfuscateEmail :: Text -- ^ Name of class to assign to the links, e.g. @\"protected-email\"@ -> Extension obfuscateEmail class' = Ext.inlineRender $ \old inline -> case inline of l@(Link _ email mtitle) -> if URI.uriScheme email == Just [scheme|mailto|] then let txt = Plain "Enable JavaScript to see this email" :| [] js = [uri|javascript:void(0)|] in with (old (Link txt js mtitle)) [ class_ class' , data_ "email" (URI.render email { URI.uriScheme = Nothing }) ] else old l other -> old other