emanote-0.8.0.0: src/Emanote/Pandoc/BuiltinFilters.hs
module Emanote.Pandoc.BuiltinFilters
( prepareNoteDoc,
preparePandoc,
)
where
import Emanote.Model.Note qualified as N
import Emanote.Pandoc.Markdown.Syntax.HashTag qualified as HT
import Emanote.Route (encodeRoute)
import Emanote.Route.SiteRoute.Type (encodeTagIndexR)
import Optics.Core ((^.))
import Relude
import Text.Pandoc.Definition qualified as B
import Text.Pandoc.Walk qualified as W
-- TODO: Run this in `parseNote`?
prepareNoteDoc :: N.Note -> B.Pandoc
prepareNoteDoc note =
preparePandoc $ note ^. N.noteDoc
preparePandoc :: W.Walkable B.Inline b => b -> b
preparePandoc =
linkifyInlineTags
>>> fixEmojiFontFamily
-- HashTag.hs generates a Span for inline tags.
-- Here, we must link them to the special tag index page.
linkifyInlineTags :: W.Walkable B.Inline b => b -> b
linkifyInlineTags =
W.walk $ \case
inline@(B.Span attr is) ->
if
| Just inlineTag <- HT.getTagFromInline inline ->
B.Span attr [B.Link mempty is (tagUrl inlineTag, "Tag")]
| otherwise ->
inline
x ->
x
where
tagUrl =
toText . encodeRoute . encodeTagIndexR . toList . HT.deconstructTag
-- Undo font-familly on emoji spans, so the browser uses an emoji font.
-- Ref: https://github.com/jgm/commonmark-hs/blob/3d545d7afa6c91820b4eebf3efeeb80bf1b27128/commonmark-extensions/src/Commonmark/Extensions/Emoji.hs#L30-L33
fixEmojiFontFamily :: W.Walkable B.Inline b => b -> b
fixEmojiFontFamily =
W.walk $ \case
B.Span (id', classes, attrs) is
| classes == ["emoji"] ->
let emojiFontAttr = ("style", "font-family: emoji")
newAttrs = attrs <> one emojiFontAttr
in B.Span (id', classes, newAttrs) is
x -> x