From 66636c89b03e75c76ed17904b9adc0a1772f419b Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Sat, 8 Jan 2022 23:21:15 -0800
Subject: [PATCH] Org writer: fix list items starting with a code block...

or other non-paragraph content.

Closes #7810.
---
 src/Text/Pandoc/Writers/Org.hs | 16 +++++++++++--
 test/command/7810.md           | 43 ++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 2 deletions(-)
 create mode 100644 test/command/7810.md

diff --git a/src/Text/Pandoc/Writers/Org.hs b/src/Text/Pandoc/Writers/Org.hs
index 22c229de9..6d4dfa1b5 100644
--- a/src/Text/Pandoc/Writers/Org.hs
+++ b/src/Text/Pandoc/Writers/Org.hs
@@ -219,7 +219,13 @@ bulletListItemToOrg :: PandocMonad m => [Block] -> Org m (Doc Text)
 bulletListItemToOrg items = do
   exts <- gets $ writerExtensions . stOptions
   contents <- blockListToOrg (taskListItemToOrg exts items)
-  return $ hang 2 "- " (chomp contents) $$
+  -- if list item starts with non-paragraph, it must go on
+  -- the next line:
+  let contents' = (case items of
+                    Plain{}:_ -> mempty
+                    Para{}:_ -> mempty
+                    _ -> cr) <> chomp contents
+  return $ hang 2 "- " contents' $$
           if null items || endsWithPlain items
              then cr
              else blankline
@@ -233,11 +239,17 @@ orderedListItemToOrg :: PandocMonad m
 orderedListItemToOrg marker counter items = do
   exts <- gets $ writerExtensions . stOptions
   contents <- blockListToOrg (taskListItemToOrg exts items)
+  -- if list item starts with non-paragraph, it must go on
+  -- the next line:
+  let contents' = (case items of
+                    Plain{}:_ -> mempty
+                    Para{}:_ -> mempty
+                    _ -> cr) <> chomp contents
   let cookie = maybe empty
                (\n -> space <> literal "[@" <> literal (tshow n) <> literal "]")
                counter
   return $ hang (T.length marker + 1)
-                (literal marker <> cookie <> space) (chomp contents) $$
+                (literal marker <> cookie <> space) contents' $$
           if null items || endsWithPlain items
              then cr
              else blankline
diff --git a/test/command/7810.md b/test/command/7810.md
new file mode 100644
index 000000000..389aa733b
--- /dev/null
+++ b/test/command/7810.md
@@ -0,0 +1,43 @@
+```
+% pandoc -f org -t org
+- 
+  #+begin_example
+  aa
+  #+end_example
+- test
+^D
+- 
+  #+begin_example
+  aa
+  #+end_example
+
+- test
+```
+
+```
+% pandoc -f org -t org
+- a
+- 
+- a
+- a
+^D
+- a
+- 
+- a
+- a
+```
+
+```
+% pandoc -f org -t org
+- a
+  - b
+  - b
+- a
+- a
+^D
+- a
+  - b
+  - b
+- a
+- a
+```