{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Text.MMark.Util
( asPlainText
, headerId
, headerFragment )
where
import Data.Char (isSpace, isAlphaNum)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Text (Text)
import Text.MMark.Type
import Text.URI (URI (..))
import qualified Data.Text as T
import qualified Text.URI as URI
asPlainText :: NonEmpty Inline -> Text
asPlainText = foldMap $ \case
Plain txt -> txt
LineBreak -> "\n"
Emphasis xs -> asPlainText xs
Strong xs -> asPlainText xs
Strikeout xs -> asPlainText xs
Subscript xs -> asPlainText xs
Superscript xs -> asPlainText xs
CodeSpan txt -> txt
Link xs _ _ -> asPlainText xs
Image xs _ _ -> asPlainText xs
headerId :: NonEmpty Inline -> Text
headerId = T.intercalate "-"
. T.words
. T.filter (\x -> isAlphaNum x || isSpace x)
. T.toLower
. asPlainText
headerFragment :: Text -> URI
headerFragment fragment = URI
{ uriScheme = Nothing
, uriAuthority = Left False
, uriPath = Nothing
, uriQuery = []
, uriFragment = URI.mkFragment fragment }