From 556c6c2c6ded6da4e8fd28e5f57fb55df8625373 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Tue, 31 Oct 2017 10:48:13 -0700
Subject: [PATCH] Markdown reader:  make sure fenced div closers work in lists.

Previously the following failed:

    ::: {.class}
    1. one
    2. two
    :::

and you needed a blank line before the closing `:::`.
---
 src/Text/Pandoc/Readers/Markdown.hs | 14 +++++++++++---
 src/Text/Pandoc/Writers/Markdown.hs |  2 +-
 test/command/3596.md                |  1 -
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs
index 2a88b39ec..98552e65d 100644
--- a/src/Text/Pandoc/Readers/Markdown.hs
+++ b/src/Text/Pandoc/Readers/Markdown.hs
@@ -846,6 +846,7 @@ listLine continuationIndent = try $ do
                      skipMany spaceChar
                      listStart)
   notFollowedByHtmlCloser
+  notFollowedByDivCloser
   optional (() <$ gobbleSpaces continuationIndent)
   listLineCommon
 
@@ -883,16 +884,24 @@ listContinuation continuationIndent = try $ do
   x <- try $ do
          notFollowedBy blankline
          notFollowedByHtmlCloser
+         notFollowedByDivCloser
          gobbleSpaces continuationIndent
          anyLineNewline
   xs <- many $ try $ do
          notFollowedBy blankline
          notFollowedByHtmlCloser
+         notFollowedByDivCloser
          gobbleSpaces continuationIndent <|> notFollowedBy' listStart
          anyLineNewline
   blanks <- many blankline
   return $ concat (x:xs) ++ blanks
 
+notFollowedByDivCloser :: PandocMonad m => MarkdownParser m ()
+notFollowedByDivCloser = do
+  guardDisabled Ext_fenced_divs <|>
+    do divLevel <- stateFencedDivLevel <$> getState
+       guard (divLevel < 1) <|> notFollowedBy divFenceEnd
+
 notFollowedByHtmlCloser :: PandocMonad m => MarkdownParser m ()
 notFollowedByHtmlCloser = do
   inHtmlBlock <- stateInHtmlBlock <$> getState
@@ -965,6 +974,7 @@ defRawBlock compact = try $ do
   let dline = try
                ( do notFollowedBy blankline
                     notFollowedByHtmlCloser
+                    notFollowedByDivCloser
                     if compact -- laziness not compatible with compact
                        then () <$ indentSpaces
                        else (() <$ indentSpaces)
@@ -1688,10 +1698,8 @@ endline = try $ do
   guardEnabled Ext_blank_before_header <|> (notFollowedBy . char =<< atxChar) -- atx header
   guardDisabled Ext_backtick_code_blocks <|>
      notFollowedBy (() <$ (lookAhead (char '`') >> codeBlockFenced))
-  guardDisabled Ext_fenced_divs <|>
-    do divLevel <- stateFencedDivLevel <$> getState
-       guard (divLevel < 1) <|> notFollowedBy divFenceEnd
   notFollowedByHtmlCloser
+  notFollowedByDivCloser
   (eof >> return mempty)
     <|> (guardEnabled Ext_hard_line_breaks >> return (return B.linebreak))
     <|> (guardEnabled Ext_ignore_line_breaks >> return mempty)
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index c109385d8..a1f30cb0e 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -402,7 +402,7 @@ blockToMarkdown' opts (Div attrs ils) = do
          _ | isEnabled Ext_fenced_divs opts &&
              attrs /= nullAttr ->
                 nowrap (text ":::" <+> attrsToMarkdown attrs) $$
-                contents $$
+                chomp contents $$
                 text ":::" <> blankline
            | isEnabled Ext_native_divs opts ||
              (isEnabled Ext_raw_html opts &&
diff --git a/test/command/3596.md b/test/command/3596.md
index 1ae730740..01a871e1b 100644
--- a/test/command/3596.md
+++ b/test/command/3596.md
@@ -52,7 +52,6 @@
 
 -   ::: {#id}
     bar
-
     :::
 
 -   baz