diff options
| author | Albert Krewinkel <[email protected]> | 2022-12-20 17:10:20 +0100 |
|---|---|---|
| committer | Albert Krewinkel <[email protected]> | 2022-12-20 18:21:16 +0100 |
| commit | c954dfab59fd1d93a838e4023c9f365280e63b30 (patch) | |
| tree | f08836689df4d4fd8f7408ca919768c1df053506 | |
| parent | 98667b7b2fe89b54296667bed3b260131041adda (diff) | |
Markdown writer: avoid HTML fallbacks in the generated TOC.
The generated table of contents usually has IDs for each TOC link,
allowing to link back to specific parts of the TOC. However, this leads
to unidiomatic markup in formats like gfm, which do not support
attributes on links and hence fall back to HTML. The IDs on TOC items
are now removed in that case, leading to more aesthetic TOCs.
Closes: #8131
| -rw-r--r-- | src/Text/Pandoc/Writers/Markdown.hs | 8 | ||||
| -rw-r--r-- | test/command/8131.md | 41 |
2 files changed, 48 insertions, 1 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index ce508f08d..2769181e6 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -233,8 +233,14 @@ pandocToMarkdown opts (Pandoc meta blocks) = do mmdTitleBlock metadata | otherwise -> empty Nothing -> empty + let modifyTOC = + if isEnabled Ext_link_attributes opts || isEnabled Ext_attributes opts + then id + else walk $ \inln -> case inln of + Link _attr contents tgt -> Link nullAttr contents tgt + _ -> inln toc <- if writerTableOfContents opts - then blockToMarkdown opts ( toTableOfContents opts blocks ) + then blockToMarkdown opts . modifyTOC $ 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/test/command/8131.md b/test/command/8131.md new file mode 100644 index 000000000..6820542f6 --- /dev/null +++ b/test/command/8131.md @@ -0,0 +1,41 @@ +# TOC in gfm contains no HTML by default +``` +% pandoc --to=gfm --toc --standalone +# Head + +Content +^D +- [Head](#head) + +# Head + +Content +``` + +# Same in Markdown if link_attributes extension is disabled +``` +% pandoc --to=markdown-link_attributes --toc --standalone +# Head + +Content +^D +- [Head](#head) + +# Head + +Content +``` + +# IDs are added to TOC with the "attributes" CommonMark extension +``` +% pandoc --to=commonmark+gfm_auto_identifiers+attributes --toc -s +# Nam a sapien + +Lorem ipsum dolor sit amet, consectetuer adipiscing elit. +^D +- [Nam a sapien](#nam-a-sapien){#toc-nam-a-sapien} + +# Nam a sapien + +Lorem ipsum dolor sit amet, consectetuer adipiscing elit. +``` |
