git.iko.soy/typeable/pandoc
on master help sign in
Implement --toc in commonmark/gfm writers.

Closes #5172.

commitc4c3fca50009ef5d8b3b34aaca64007ebe70f0a8 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 4 Jan 2019, 21:20 (8 years ago) parent0d609a7
2 files changed+14−3
src/Text/Pandoc/Writers/CommonMark.hs+13−2 src/Text/Pandoc/Writers/Markdown.hs+1−1
src/Text/Pandoc/Writers/CommonMark.hs+13−2
@@ -47,7 +47,7 @@ import Text.Pandoc.Class (PandocMonad)
4747import Text.Pandoc.Definition
4848import Text.Pandoc.Options
4949import Text.Pandoc.Shared (isTightList, taskListItemToAscii, linesToPara,
50− substitute, capitalize)
50+ substitute, capitalize, isHeaderBlock)
5151import Text.Pandoc.Templates (renderTemplate')
5252import Text.Pandoc.Walk (query, walk, walkM)
5353import Text.Pandoc.Writers.HTML (writeHtml5String, tagWithAttributes)
@@ -57,6 +57,12 @@ import Text.Pandoc.XML (toHtml5Entities)
5757-- | Convert Pandoc to CommonMark.
5858writeCommonMark :: PandocMonad m => WriterOptions -> Pandoc -> m Text
5959writeCommonMark opts (Pandoc meta blocks) = do
60+ let headerBlocks = filter isHeaderBlock blocks
61+ toc <- if writerTableOfContents opts
62+ then blocksToCommonMark opts
63+ [ toTableOfContents opts headerBlocks ]
64+ else return mempty
65+
6066 let (blocks', notes) = runState (walkM processNotes blocks) []
6167 notes' = if null notes
6268 then []
@@ -66,7 +72,12 @@ writeCommonMark opts (Pandoc meta blocks) = do
6672 (blocksToCommonMark opts)
6773 (inlinesToCommonMark opts)
6874 meta
69− let context = defField "body" main metadata
75+ let context =
76+ -- for backwards compatibility we populate toc
77+ -- with the contents of the toc, rather than a boolean:
78+ defField "toc" toc
79+ $ defField "table-of-contents" toc
80+ $ defField "body" main metadata
7081 case writerTemplate opts of
7182 Nothing -> return main
7283 Just tpl -> renderTemplate' tpl context
src/Text/Pandoc/Writers/Markdown.hs+1−1
@@ -213,7 +213,7 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
213213 let headerBlocks = filter isHeaderBlock blocks
214214 toc <- if writerTableOfContents opts
215215 then render' <$> blockToMarkdown opts
216− ( toTableOfContents opts $ headerBlocks )
216+ ( toTableOfContents opts headerBlocks )
217217 else return ""
218218 -- Strip off final 'references' header if markdown citations enabled
219219 let blocks' = if isEnabled Ext_citations opts