From df57d0930bf4c6b369b1cd35a98bd8b7238da0d1 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 26 Sep 2021 21:48:08 -0700 Subject: RST writer: properly handle anchors to ids... with spaces or leading underscore. In this cases we need the quoted form, e.g. ``` .. _`foo bar`: .. _`_foo`: ``` Side note: rST will "normalize" these identifiers anyway, ignoring the underscore: https://docutils.sourceforge.io/docs/ref/rst/directives.html#identifier-normalization Closes #7593. --- src/Text/Pandoc/Writers/RST.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/Text/Pandoc/Writers') diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index 983ef412a..8b2002851 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -270,7 +270,12 @@ blockToRST (Header level (name,classes,_) inlines) = do let headerChar = if level > 5 then ' ' else "=-~^'" !! (level - 1) let border = literal $ T.replicate (offset contents) $ T.singleton headerChar let anchor | T.null name || name == autoId = empty - | otherwise = ".. _" <> literal name <> ":" $$ blankline + | otherwise = ".. _" <> + (if T.any (==':') name || + T.take 1 name == "_" + then "`" <> literal name <> "`" + else literal name) <> + ":" $$ blankline return $ nowrap $ anchor $$ contents $$ border $$ blankline else do let rub = "rubric:: " <> contents -- cgit v1.2.3