Skip to content

Commit

Permalink
Implement --toc in commonmark/gfm writers.
Browse files Browse the repository at this point in the history
Closes #5172.
  • Loading branch information
jgm committed Jan 5, 2019
1 parent 0d609a7 commit c4c3fca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/Text/Pandoc/Writers/CommonMark.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 []
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Text/Pandoc/Writers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c4c3fca

Please sign in to comment.