forked from alexmingoia/purescript-markdown-smolder
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Smolder.purs
57 lines (49 loc) · 1.8 KB
/
Smolder.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
module Text.Markdown.SlamDown.Smolder
( defaultToMarkupOptions
, toMarkup
, toMarkup'
, module Block
, module Type
) where
import Prelude
import Control.Monad.Reader (runReader)
import Data.Foldable (foldMap, foldl)
import Data.List ((:))
import Data.Map (unions)
import Data.Map as Map
import Text.Markdown.SlamDown (Block(..), SlamDownP(..))
import Text.Markdown.Smolder.Internal.Block (renderFencedCodeBlock, toElements, toElement) as Block
import Text.Markdown.Smolder.Internal.Type (ToMarkupOptions, ReferenceLinks)
import Text.Markdown.Smolder.Internal.Type (ReferenceLinks, ToMarkupOptions, ToMarkupReaderData, ToReaderMarkup) as Type
import Text.Smolder.Markup (Markup)
-- | Default convert options
defaultToMarkupOptions :: forall e. ToMarkupOptions e
defaultToMarkupOptions =
{ hideHeadingId: false
, renderFencedCodeBlock: Block.renderFencedCodeBlock
, olClass: ""
, ulClass: ""
, hClasses: []
, pClass: ""
}
-- | Convert Smolder markup from Slamdown Markdown without options
toMarkup :: forall a e. SlamDownP a -> Markup e
toMarkup = toMarkup' defaultToMarkupOptions
-- | Convert Smolder markup from Slamdown Markdown with options
toMarkup' :: forall a e. ToMarkupOptions e -> SlamDownP a -> Markup e
toMarkup' options (SlamDown bs) = runReader (Block.toElements bs)
{ referenceLinks
, options
}
where
referenceLinks = foldMap getBlockLinkRefs bs
-- Get all reference links from markdown
getBlockLinkRefs :: forall a. Block a -> ReferenceLinks
getBlockLinkRefs = getBlockLinkRefs' Map.empty
where
getBlockLinkRefs' links block =
case block of
(LinkReference k url) -> Map.insert k url links
(Blockquote bs) -> foldl getBlockLinkRefs' links bs
(Lst _ bss) -> unions $ links : map (foldl getBlockLinkRefs' Map.empty) bss
_ -> links