git.iko.soy/typeable/pandoc
on master help sign in
Improved --toc generation.
commitd0918627cafff1ce96c51df9963e3a25ce149cce authorJohn MacFarlane <jgm@berkeley.edu> dateTue 17 Dec 2019, 11:59 (7 years ago) parent80728c8
4 files changed+61−9
src/Text/Pandoc/Writers/CommonMark.hs+3−5 src/Text/Pandoc/Writers/Markdown.hs+1−2 src/Text/Pandoc/Writers/RTF.hs+1−2 test/command/toc.md+56
src/Text/Pandoc/Writers/CommonMark.hs+3−5
@@ -28,7 +28,7 @@ import Network.HTTP (urlEncode)
2828import Text.Pandoc.Class (PandocMonad)
2929import Text.Pandoc.Definition
3030import Text.Pandoc.Options
31−import Text.Pandoc.Shared (capitalize, isHeaderBlock, isTightList,
31+import Text.Pandoc.Shared (capitalize, isTightList,
3232 linesToPara, onlySimpleTableCells, taskListItemToAscii, tshow)
3333import Text.Pandoc.Templates (renderTemplate)
3434import Text.Pandoc.Walk (walk, walkM)
@@ -40,12 +40,10 @@ import Text.DocLayout (literal, render)
4040-- | Convert Pandoc to CommonMark.
4141writeCommonMark :: PandocMonad m => WriterOptions -> Pandoc -> m Text
4242writeCommonMark opts (Pandoc meta blocks) = do
43− let headerBlocks = filter isHeaderBlock blocks
4443 toc <- if writerTableOfContents opts
45− then blocksToCommonMark opts
46− [ toTableOfContents opts headerBlocks ]
44+ then blocksToCommonMark opts [ toTableOfContents opts blocks ]
4745 else return mempty
48−
46+
4947 let (blocks', notes) = runState (walkM processNotes blocks) []
5048 notes' = if null notes
5149 then []
src/Text/Pandoc/Writers/Markdown.hs+1−2
@@ -208,9 +208,8 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
208208 mmdTitleBlock metadata
209209 | otherwise -> empty
210210 Nothing -> empty
211− let headerBlocks = filter isHeaderBlock blocks
212211 toc <- if writerTableOfContents opts
213− then blockToMarkdown opts ( toTableOfContents opts headerBlocks )
212+ then blockToMarkdown opts ( toTableOfContents opts blocks )
214213 else return mempty
215214 -- Strip off final 'references' header if markdown citations enabled
216215 let blocks' = if isEnabled Ext_citations opts
src/Text/Pandoc/Writers/RTF.hs+1−2
@@ -103,8 +103,7 @@ writeRTF options doc = do
103103 (fmap literal . inlinesToRTF)
104104 meta'
105105 body <- blocksToRTF 0 AlignDefault blocks
106− toc <- blocksToRTF 0 AlignDefault
107− [toTableOfContents options $ filter isHeaderBlock blocks]
106+ toc <- blocksToRTF 0 AlignDefault [toTableOfContents options blocks]
108107 let context = defField "body" body
109108 $ defField "spacer" spacer
110109 $(if writerTableOfContents options
test/command/toc.mdnew file+56
@@ -0,0 +1,56 @@
1+```
2+% pandoc -s --toc -t markdown
3+# A
4+## b
5+# B
6+## b
7+
8+::: interior
9+# C
10+## cc
11+# D
12+:::
13+
14+::: blue
15+# E
16+## e
17+:::
18+^D
19+- [A](#a)
20+ - [b](#b)
21+- [B](#b-1)
22+ - [b](#b-2)
23+- [E](#e)
24+ - [e](#e-1)
25+
26+A
27+=
28+
29+b
30+-
31+
32+B
33+=
34+
35+b
36+-
37+
38+::: {.interior}
39+C
40+=
41+
42+cc
43+--
44+
45+D
46+=
47+:::
48+
49+::: {.blue}
50+E
51+=
52+
53+e
54+-
55+:::
56+```