Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(website): Add MMark extension to render table with styles #100

Merged
merged 3 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Neuron/Zettelkasten/Markdown.hs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE NoImplicitPrelude #-}

module Neuron.Zettelkasten.Markdown
( neuronMMarkExts,
)
where

import Neuron.Zettelkasten.Config (Config (..))
import Neuron.Zettelkasten.Markdown.Extension (setTableClass)
import Relude
import qualified Text.MMark as MMark
import qualified Text.MMark.Extension.Common as Ext
Expand All @@ -24,5 +25,6 @@ defaultExts =
Ext.kbd,
Ext.linkTarget,
Ext.punctuationPrettifier,
Ext.skylighting
Ext.skylighting,
setTableClass "ui celled table"
]
48 changes: 48 additions & 0 deletions src/Neuron/Zettelkasten/Markdown/Extension.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}

module Neuron.Zettelkasten.Markdown.Extension
( setTableClass,
)
where

import qualified Data.List.NonEmpty as NE
srid marked this conversation as resolved.
Show resolved Hide resolved
import Lucid
import Relude
import Text.MMark.Extension (Block (Table), Extension)
import qualified Text.MMark.Extension as Ext

newline :: Html ()
newline = "\n"

setTableClass :: Text -> Extension
setTableClass tableClass = Ext.blockRender $ \old block ->
case block of
-- https://github.com/mmark-md/mmark/blob/8f5534d8068c2b7a139b893639ee5920bcaedd84/Text/MMark/Render.hs#L111-L136
(Table calign (hs :| rows)) -> do
table_ [class_ tableClass] $ do
newline
thead_ $ do
newline
tr_
$ forM_ (NE.zip calign hs)
$ \(a, h) ->
th_ (alignStyle a) (snd h)
newline
newline
tbody_ $ do
newline
forM_ rows $ \row -> do
tr_
$ forM_ (NE.zip calign row)
$ \(a, h) ->
td_ (alignStyle a) (snd h)
newline
newline
newline
other -> old other
where
alignStyle Ext.CellAlignDefault = []
alignStyle Ext.CellAlignLeft = [style_ "text-align:left"]
alignStyle Ext.CellAlignRight = [style_ "text-align:right"]
alignStyle Ext.CellAlignCenter = [style_ "text-align:center"]
srid marked this conversation as resolved.
Show resolved Hide resolved