From 8aa18bfcf7ff789e0be10b116f7f780ec405c0bd Mon Sep 17 00:00:00 2001 From: staticvoid Date: Mon, 27 Jun 2022 20:25:02 +0300 Subject: [PATCH] Handle autolinks --- app/Polysemy/Commonmark/AST.hs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/Polysemy/Commonmark/AST.hs b/app/Polysemy/Commonmark/AST.hs index 122089b..2493dc5 100644 --- a/app/Polysemy/Commonmark/AST.hs +++ b/app/Polysemy/Commonmark/AST.hs @@ -33,8 +33,8 @@ data InlinePiece = Str Text | Strong Inline | Underline Inline | Strikethrough Inline - -- | Autolink { dest :: Text } - | Link { dest, title :: Text + | Autolink { dest :: Text } + | Link { dest :: Text , descr :: Inline } | Code Text @@ -50,7 +50,8 @@ mapText f (Inline pieces) = Inline (mapText' <$> pieces) where mapText' (Strong i) = Strong (mapText f i) mapText' (Underline i) = Underline (mapText f i) mapText' (Strikethrough i) = Strikethrough (mapText f i) - mapText' Link{dest, title, descr} = Link { dest, title, descr = mapText f descr } + mapText' (Autolink t) = Link {dest = t, descr = str (f t)} + mapText' Link{dest, descr} = Link { dest, descr = mapText f descr } mapText' (Code t) = Code t instance Semigroup Inline where @@ -80,6 +81,7 @@ instance IsInline Inline where escapedChar c = str $ "\\" <> one c emph = Inline . one . Emph strong = Inline . one . Strong + link dest _title (Inline (Str descr :<| Empty)) | dest == descr = Inline $ one Autolink {dest} link dest _title descr = descr <> fromList [ Str " (" , Underline (str dest) @@ -148,6 +150,9 @@ instance PrintColor Inline where where mkStrikethrough :: (IsString s, ToString s) => s -> s mkStrikethrough s = fromString do { c <- toString s; [c, chr 822] } + printPiece Autolink{dest} = do + withSgr [ANSI.SetUnderlining ANSI.SingleUnderline] do + putTextO dest printPiece Link{dest, descr} = do printColor descr withSgr [ANSI.SetUnderlining ANSI.SingleUnderline] do