From 47537d26db29b9dd0810d039933497d4db4ed813 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Fri, 8 Feb 2019 23:16:01 -0800
Subject: [PATCH] Improve tight/loose list handling.

Closes #5285. Previously the algorithm allowed list items
with a mix of Para and Plain, which is never wanted.

compactify in T.P.Shared has been modified so that, if
a list's items contain (at the top level) Para elements
(aside from perhaps at the very end), ALL Plains are
converted to Paras.
---
 src/Text/Pandoc/Shared.hs | 11 +++++++++--
 test/command/5285.md      | 15 +++++++++++++++
 2 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 test/command/5285.md

diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index b1ae9eeed..a945c9355 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -416,7 +416,8 @@ capitalize = walk go
         go x       = x
 
 -- | Change final list item from @Para@ to @Plain@ if the list contains
--- no other @Para@ blocks.
+-- no other @Para@ blocks.  Otherwise (if the list items contain @Para@
+-- blocks besides possibly at the end), turn any @Plain@s into @Para@s (#5285).
 compactify :: [Blocks]  -- ^ List of list items (each a list of blocks)
            -> [Blocks]
 compactify [] = []
@@ -426,9 +427,15 @@ compactify items =
            (Para a:xs) -> case [Para x | Para x <- concatMap B.toList items] of
                             -- if this is only Para, change to Plain
                             [_] -> others ++ [B.fromList (reverse $ Plain a : xs)]
-                            _   -> items
+                            -- if other Paras, it's a loose list, change
+                            -- all Plain to Para
+                            _   -> map (fmap plainToPara) items
            _      -> items
 
+plainToPara :: Block -> Block
+plainToPara (Plain ils) = Para ils
+plainToPara x = x
+
 -- | Like @compactify@, but acts on items of definition lists.
 compactifyDL :: [(Inlines, [Blocks])] -> [(Inlines, [Blocks])]
 compactifyDL items =
diff --git a/test/command/5285.md b/test/command/5285.md
new file mode 100644
index 000000000..780812761
--- /dev/null
+++ b/test/command/5285.md
@@ -0,0 +1,15 @@
+```
+% pandoc -t native
+- a
+
+  b
+- a
+
+- b
+^D
+[BulletList
+ [[Para [Str "a"]
+  ,Para [Str "b"]]
+ ,[Para [Str "a"]]
+ ,[Para [Str "b"]]]]
+```