From d0918627cafff1ce96c51df9963e3a25ce149cce Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Tue, 17 Dec 2019 11:59:29 -0800
Subject: [PATCH] Improved --toc generation.

---
 src/Text/Pandoc/Writers/CommonMark.hs |  8 ++--
 src/Text/Pandoc/Writers/Markdown.hs   |  3 +-
 src/Text/Pandoc/Writers/RTF.hs        |  3 +-
 test/command/toc.md                   | 56 +++++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 9 deletions(-)
 create mode 100644 test/command/toc.md

diff --git a/src/Text/Pandoc/Writers/CommonMark.hs b/src/Text/Pandoc/Writers/CommonMark.hs
index e2d2b8e4d..e189336b2 100644
--- a/src/Text/Pandoc/Writers/CommonMark.hs
+++ b/src/Text/Pandoc/Writers/CommonMark.hs
@@ -28,7 +28,7 @@ import Network.HTTP (urlEncode)
 import Text.Pandoc.Class (PandocMonad)
 import Text.Pandoc.Definition
 import Text.Pandoc.Options
-import Text.Pandoc.Shared (capitalize, isHeaderBlock, isTightList,
+import Text.Pandoc.Shared (capitalize, isTightList,
     linesToPara, onlySimpleTableCells, taskListItemToAscii, tshow)
 import Text.Pandoc.Templates (renderTemplate)
 import Text.Pandoc.Walk (walk, walkM)
@@ -40,12 +40,10 @@ import Text.DocLayout (literal, render)
 -- | Convert Pandoc to CommonMark.
 writeCommonMark :: PandocMonad m => WriterOptions -> Pandoc -> m Text
 writeCommonMark opts (Pandoc meta blocks) = do
-  let headerBlocks = filter isHeaderBlock blocks
   toc <- if writerTableOfContents opts
-            then blocksToCommonMark opts
-                  [ toTableOfContents opts headerBlocks ]
+            then blocksToCommonMark opts [ toTableOfContents opts blocks ]
             else return mempty
- 
+
   let (blocks', notes) = runState (walkM processNotes blocks) []
       notes' = if null notes
                then []
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 2b6084255..e6d7579d3 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -208,9 +208,8 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
                                    mmdTitleBlock metadata
                                | otherwise -> empty
                         Nothing -> empty
-  let headerBlocks = filter isHeaderBlock blocks
   toc <- if writerTableOfContents opts
-         then blockToMarkdown opts ( toTableOfContents opts headerBlocks )
+         then blockToMarkdown opts ( toTableOfContents opts blocks )
          else return mempty
   -- Strip off final 'references' header if markdown citations enabled
   let blocks' = if isEnabled Ext_citations opts
diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs
index 08f0df0f8..6a40f75e2 100644
--- a/src/Text/Pandoc/Writers/RTF.hs
+++ b/src/Text/Pandoc/Writers/RTF.hs
@@ -103,8 +103,7 @@ writeRTF options doc = do
               (fmap literal . inlinesToRTF)
               meta'
   body <- blocksToRTF 0 AlignDefault blocks
-  toc <- blocksToRTF 0 AlignDefault
-          [toTableOfContents options $ filter isHeaderBlock blocks]
+  toc <- blocksToRTF 0 AlignDefault [toTableOfContents options blocks]
   let context = defField "body" body
               $ defField "spacer" spacer
               $(if writerTableOfContents options
diff --git a/test/command/toc.md b/test/command/toc.md
new file mode 100644
index 000000000..9185257de
--- /dev/null
+++ b/test/command/toc.md
@@ -0,0 +1,56 @@
+```
+% pandoc -s --toc -t markdown
+# A
+## b
+# B
+## b
+
+::: interior
+# C
+## cc
+# D
+:::
+
+::: blue
+# E
+## e
+:::
+^D
+-   [A](#a)
+    -   [b](#b)
+-   [B](#b-1)
+    -   [b](#b-2)
+-   [E](#e)
+    -   [e](#e-1)
+
+A
+=
+
+b
+-
+
+B
+=
+
+b
+-
+
+::: {.interior}
+C
+=
+
+cc
+--
+
+D
+=
+:::
+
+::: {.blue}
+E
+=
+
+e
+-
+:::
+```