aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-15 02:44:19 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2023-12-15 02:44:19 +0100
commit01a14ef0d873e99cf8d284d2bb07ddf2389a6e4c (patch)
treef64b8f8a8b48d6e87584cafcb9b7e4484f9bcfa0
parentb7a7ac48b457909d080ea710c6ecf69ea4efc217 (diff)
fix: fix spacing between markdown nodes
-rw-r--r--app/Render.hs27
1 files changed, 15 insertions, 12 deletions
diff --git a/app/Render.hs b/app/Render.hs
index 6cb0373..f4293c2 100644
--- a/app/Render.hs
+++ b/app/Render.hs
@@ -129,15 +129,6 @@ instance Render Markdown where
instance Render [D.Node] where
render = render . D.Node Nothing D.DOCUMENT
--- TODO Fix spacing between markdown nodes
---
--- The following code suffers from the problem that inline code such as `foo` is not separated correctly when surrounded by non-whitespace characters, ie. `foo`, or `foo`s.
---
--- The reason for that is that we generally trim words within `TEXT` nodes, and then add the spaces back.
---
--- Thus, we should not trim words. But we should still replace whitespace by `P.softline`s (ie. `P.fillSep`) for automatic paragraph wrapping.
---
--- @topic markdown
instance Render D.Node where
render = maybe emptyDoc go . rec
where
@@ -146,7 +137,7 @@ instance Render D.Node where
go (D.Node _ D.DOCUMENT ns) = vsep (intersperse (pretty ("" :: T.Text)) (map go ns))
go (D.Node _ D.THEMATIC_BREAK _) = pretty ("***" :: T.Text)
- go (D.Node _ D.PARAGRAPH ns) = fillSep $ map go ns
+ go (D.Node _ D.PARAGRAPH ns) = hcat $ map go ns
go (D.Node _ D.BLOCK_QUOTE ns) =
styled [color Black] . fillSep $
pretty (">" :: T.Text)
@@ -181,8 +172,20 @@ instance Render D.Node where
hsep (map ((pretty ("- " :: T.Text)) <>) (map go ns))
go (D.Node _ D.ITEM ns) = fillSep (map go ns)
go (D.Node _ (D.TEXT s) _) =
- fillSep . map (pretty . T.strip) $
- T.words s
+ hcat
+ . intersperse softline
+ . map pretty
+ . ( \xs ->
+ -- first and last `T.null`-elements must be preserved as it may represents whitespace between sibling `D.Node`s.
+ if length xs >= 2
+ then
+ head xs
+ : filter (not . T.null) (tail . init $ xs)
+ ++ [last xs]
+ else xs
+ )
+ . T.split (== ' ')
+ $ s
go (D.Node _ D.LINEBREAK _) = hardline
go (D.Node _ (D.HTML_INLINE s) _) =
styled [color Yellow] $ pretty (T.strip s)