1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-23 12:15:06 +03:00

Rename MarkdownBlockWithTOC to MarkdownTree

This commit is contained in:
Artyom 2017-04-25 14:30:30 +03:00
parent 62310d22e9
commit dc9de413d7
No known key found for this signature in database
GPG Key ID: B8E35A33FF522710
4 changed files with 28 additions and 25 deletions

View File

@ -13,7 +13,7 @@ module Guide.Markdown
-- * Types -- * Types
MarkdownInline(..), MarkdownInline(..),
MarkdownBlock(..), MarkdownBlock(..),
MarkdownBlockWithTOC(..), MarkdownTree(..),
-- * Lenses -- * Lenses
mdHtml, mdHtml,
@ -26,7 +26,7 @@ module Guide.Markdown
-- * Converting text to Markdown -- * Converting text to Markdown
toMarkdownInline, toMarkdownInline,
toMarkdownBlock, toMarkdownBlock,
toMarkdownBlockWithTOC, toMarkdownTree,
-- * Misc -- * Misc
renderMD, renderMD,
@ -77,15 +77,15 @@ data MarkdownBlock = MarkdownBlock {
markdownBlockMdHtml :: ByteString, markdownBlockMdHtml :: ByteString,
markdownBlockMdMarkdown :: ![MD.Node] } markdownBlockMdMarkdown :: ![MD.Node] }
data MarkdownBlockWithTOC = MarkdownBlockWithTOC { data MarkdownTree = MarkdownTree {
markdownBlockWithTOCMdText :: Text, markdownTreeMdText :: Text,
markdownBlockWithTOCMdTree :: !(Document Text ByteString), markdownTreeMdTree :: !(Document Text ByteString),
markdownBlockWithTOCMdIdPrefix :: Text, markdownTreeMdIdPrefix :: Text,
markdownBlockWithTOCMdTOC :: Forest ([MD.Node], Text) } markdownTreeMdTOC :: Forest ([MD.Node], Text) }
makeFields ''MarkdownInline makeFields ''MarkdownInline
makeFields ''MarkdownBlock makeFields ''MarkdownBlock
makeFields ''MarkdownBlockWithTOC makeFields ''MarkdownTree
parseMD :: Text -> [MD.Node] parseMD :: Text -> [MD.Node]
parseMD s = parseMD s =
@ -141,7 +141,11 @@ stringify = T.concat . map go
HTML_INLINE _ -> "" HTML_INLINE _ -> ""
-- | Extract everything before the first heading. -- | Extract everything before the first heading.
extractPreface :: MarkdownBlockWithTOC -> MarkdownBlock --
-- Note that if you render 'mdText' of the produced Markdown block, it won't
-- necessarily parse into 'mdHtml' from the same block. It's because rendered
-- Markdown might depend on links that are defined further in the tree.
extractPreface :: MarkdownTree -> MarkdownBlock
extractPreface = mkBlock . preface . view mdTree extractPreface = mkBlock . preface . view mdTree
where where
mkBlock x = MarkdownBlock { mkBlock x = MarkdownBlock {
@ -246,12 +250,12 @@ toMarkdownBlock s = MarkdownBlock {
doc = parseMD s doc = parseMD s
html = renderMD doc html = renderMD doc
toMarkdownBlockWithTOC :: Text -> Text -> MarkdownBlockWithTOC toMarkdownTree :: Text -> Text -> MarkdownTree
toMarkdownBlockWithTOC idPrefix s = MarkdownBlockWithTOC { toMarkdownTree idPrefix s = MarkdownTree {
markdownBlockWithTOCMdText = s, markdownTreeMdText = s,
markdownBlockWithTOCMdIdPrefix = idPrefix, markdownTreeMdIdPrefix = idPrefix,
markdownBlockWithTOCMdTree = tree, markdownTreeMdTree = tree,
markdownBlockWithTOCMdTOC = toc } markdownTreeMdTOC = toc }
where where
blocks :: [MD.Node] blocks :: [MD.Node]
blocks = parseMD s blocks = parseMD s
@ -291,7 +295,7 @@ instance Show MarkdownInline where
show = show . view mdText show = show . view mdText
instance Show MarkdownBlock where instance Show MarkdownBlock where
show = show . view mdText show = show . view mdText
instance Show MarkdownBlockWithTOC where instance Show MarkdownTree where
show = show . view mdText show = show . view mdText
instance A.ToJSON MarkdownInline where instance A.ToJSON MarkdownInline where
@ -302,7 +306,7 @@ instance A.ToJSON MarkdownBlock where
toJSON md = A.object [ toJSON md = A.object [
"text" A..= (md^.mdText), "text" A..= (md^.mdText),
"html" A..= T.decodeUtf8 (md^.mdHtml) ] "html" A..= T.decodeUtf8 (md^.mdHtml) ]
instance A.ToJSON MarkdownBlockWithTOC where instance A.ToJSON MarkdownTree where
toJSON md = A.object [ toJSON md = A.object [
"text" A..= (md^.mdText) ] "text" A..= (md^.mdText) ]
@ -312,7 +316,7 @@ instance ToHtml MarkdownInline where
instance ToHtml MarkdownBlock where instance ToHtml MarkdownBlock where
toHtmlRaw = toHtml toHtmlRaw = toHtml
toHtml = toHtmlRaw . view mdHtml toHtml = toHtmlRaw . view mdHtml
instance ToHtml MarkdownBlockWithTOC where instance ToHtml MarkdownTree where
toHtmlRaw = toHtml toHtmlRaw = toHtml
toHtml = toHtmlRaw . renderDoc . view mdTree toHtml = toHtmlRaw . renderDoc . view mdTree
where where
@ -340,14 +344,14 @@ instance SafeCopy MarkdownBlock where
kind = base kind = base
putCopy = contain . safePut . view mdText putCopy = contain . safePut . view mdText
getCopy = contain $ toMarkdownBlock <$> safeGet getCopy = contain $ toMarkdownBlock <$> safeGet
instance SafeCopy MarkdownBlockWithTOC where instance SafeCopy MarkdownTree where
version = 0 version = 0
kind = base kind = base
putCopy md = contain $ do putCopy md = contain $ do
safePut (md ^. mdIdPrefix) safePut (md ^. mdIdPrefix)
safePut (md ^. mdText) safePut (md ^. mdText)
getCopy = contain $ getCopy = contain $
toMarkdownBlockWithTOC <$> safeGet <*> safeGet toMarkdownTree <$> safeGet <*> safeGet
-- | Is a piece of Markdown empty? -- | Is a piece of Markdown empty?
markdownNull :: HasMdText a Text => a -> Bool markdownNull :: HasMdText a Text => a -> Bool

View File

@ -288,7 +288,7 @@ addItem catId itemId name' created' kind' = do
_itemConsDeleted = [], _itemConsDeleted = [],
_itemEcosystem = toMarkdownBlock "", _itemEcosystem = toMarkdownBlock "",
_itemNotes = let pref = "item-notes-" <> uidToText itemId <> "-" _itemNotes = let pref = "item-notes-" <> uidToText itemId <> "-"
in toMarkdownBlockWithTOC pref "", in toMarkdownTree pref "",
_itemLink = Nothing, _itemLink = Nothing,
_itemKind = kind' } _itemKind = kind' }
categoryById catId . items %= (++ [newItem]) categoryById catId . items %= (++ [newItem])
@ -425,7 +425,7 @@ setItemNotes :: Uid Item -> Text -> Acid.Update GlobalState (Edit, Item)
setItemNotes itemId notes' = do setItemNotes itemId notes' = do
let pref = "item-notes-" <> uidToText itemId <> "-" let pref = "item-notes-" <> uidToText itemId <> "-"
oldNotes <- itemById itemId . notes <<.= oldNotes <- itemById itemId . notes <<.=
toMarkdownBlockWithTOC pref notes' toMarkdownTree pref notes'
let edit = Edit'SetItemNotes itemId (oldNotes ^. mdText) notes' let edit = Edit'SetItemNotes itemId (oldNotes ^. mdText) notes'
(edit,) <$> use (itemById itemId) (edit,) <$> use (itemById itemId)

View File

@ -165,7 +165,7 @@ data Item = Item {
_itemCons :: [Trait], _itemCons :: [Trait],
_itemConsDeleted :: [Trait], _itemConsDeleted :: [Trait],
_itemEcosystem :: MarkdownBlock, _itemEcosystem :: MarkdownBlock,
_itemNotes :: MarkdownBlockWithTOC, _itemNotes :: MarkdownTree,
_itemLink :: Maybe Url, _itemLink :: Maybe Url,
_itemKind :: ItemKind } _itemKind :: ItemKind }
deriving (Show, Generic) deriving (Show, Generic)

View File

@ -702,8 +702,7 @@ renderSearchResult r = do
a_ [class_ "category-link", href_ (categoryLink cat)] $ a_ [class_ "category-link", href_ (categoryLink cat)] $
toHtml (cat^.title) toHtml (cat^.title)
div_ [class_ "category-description notes-like"] $ div_ [class_ "category-description notes-like"] $
toHtml (extractPreface $ toHtml (extractPreface $ toMarkdownTree "" $ cat^.notes.mdText)
toMarkdownBlockWithTOC "" $ cat^.notes.mdText)
SRItem cat item -> do SRItem cat item -> do
a_ [class_ "category-link in-item-sr", href_ (categoryLink cat)] $ a_ [class_ "category-link in-item-sr", href_ (categoryLink cat)] $
toHtml (cat^.title) toHtml (cat^.title)