diff options
author | John MacFarlane <jgm@berkeley.edu> | 2019-01-04 21:20:21 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2019-01-04 21:20:21 -0800 |
commit | c4c3fca50009ef5d8b3b34aaca64007ebe70f0a8 (patch) | |
tree | 9682df2e4fa94b091bc1083d478a3e562895d2b8 /src/Text/Pandoc/Writers | |
parent | 0d609a72fddc2251a3d943fc0ebc46aa2f0e6b3f (diff) | |
download | pandoc-c4c3fca50009ef5d8b3b34aaca64007ebe70f0a8.tar.gz |
Implement --toc in commonmark/gfm writers.
Closes #5172.
Diffstat (limited to 'src/Text/Pandoc/Writers')
-rw-r--r-- | src/Text/Pandoc/Writers/CommonMark.hs | 15 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/Markdown.hs | 2 |
2 files changed, 14 insertions, 3 deletions
diff --git a/src/Text/Pandoc/Writers/CommonMark.hs b/src/Text/Pandoc/Writers/CommonMark.hs index e28fa71a9..b8ed8987f 100644 --- a/src/Text/Pandoc/Writers/CommonMark.hs +++ b/src/Text/Pandoc/Writers/CommonMark.hs @@ -47,7 +47,7 @@ import Text.Pandoc.Class (PandocMonad) import Text.Pandoc.Definition import Text.Pandoc.Options import Text.Pandoc.Shared (isTightList, taskListItemToAscii, linesToPara, - substitute, capitalize) + substitute, capitalize, isHeaderBlock) import Text.Pandoc.Templates (renderTemplate') import Text.Pandoc.Walk (query, walk, walkM) import Text.Pandoc.Writers.HTML (writeHtml5String, tagWithAttributes) @@ -57,6 +57,12 @@ import Text.Pandoc.XML (toHtml5Entities) -- | 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 ] + else return mempty + let (blocks', notes) = runState (walkM processNotes blocks) [] notes' = if null notes then [] @@ -66,7 +72,12 @@ writeCommonMark opts (Pandoc meta blocks) = do (blocksToCommonMark opts) (inlinesToCommonMark opts) meta - let context = defField "body" main metadata + let context = + -- for backwards compatibility we populate toc + -- with the contents of the toc, rather than a boolean: + defField "toc" toc + $ defField "table-of-contents" toc + $ defField "body" main metadata case writerTemplate opts of Nothing -> return main Just tpl -> renderTemplate' tpl context diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index 4da2c2ef0..f19f35588 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -213,7 +213,7 @@ pandocToMarkdown opts (Pandoc meta blocks) = do let headerBlocks = filter isHeaderBlock blocks toc <- if writerTableOfContents opts then render' <$> blockToMarkdown opts - ( toTableOfContents opts $ headerBlocks ) + ( toTableOfContents opts headerBlocks ) else return "" -- Strip off final 'references' header if markdown citations enabled let blocks' = if isEnabled Ext_citations opts |