Markdown writer: in YAML metadata, put double quotes around...

...strings that are numbers beginning with 0 or ending with 0 and
having a decimal point.  Otherwise they will read as YAML numbers
and potentially be modified (e.g. 3.10 -> 3.1).

It would be better to fix this on the reader side, but since
we use a standard YAML parser it's hard to see how.

Closes #11715.
This commit is contained in:
John MacFarlane
2026-06-21 13:21:40 +02:00
parent 4f73087589
commit 58929eb70c
2 changed files with 20 additions and 1 deletions
+10 -1
View File
@@ -31,6 +31,7 @@ import qualified Data.Map as M
import Data.Maybe (fromMaybe, mapMaybe, isNothing, isJust)
import qualified Data.Set as Set
import Data.Text (Text)
import Text.Read (readMaybe)
import Data.Char (isSpace)
import qualified Data.Text as T
import Text.HTML.TagSoup (Tag (..), isTagText, parseTags)
@@ -178,12 +179,20 @@ valToYaml (SimpleVal x)
if hasNewlines x
then hang 0 ("|" <> cr) x
else case x of
Text _ t | isSpecialString t ->
Text _ t | isSpecialString t || looksLikeNumber t ->
"\"" <> fmap escapeInDoubleQuotes x <> "\""
_ | isNothing (foldM needsDoubleQuotes True x) ->
"\"" <> fmap escapeInDoubleQuotes x <> "\""
| otherwise -> x
where
-- we need to put quotes around numbers that begin with 0
-- or have decimal points, because their YAML renderings
-- may differ. See #11715.
looksLikeNumber t = case readMaybe (T.unpack t) of
Nothing -> False
Just (_ :: Double) ->
(T.any (== '.') t && T.takeEnd 1 t == "0") ||
T.take 1 t == "0"
isSpecialString t = Set.member t specialStrings
specialStrings = Set.fromList
["y", "Y", "yes", "Yes", "YES", "n", "N",
+10
View File
@@ -0,0 +1,10 @@
```
% pandoc -f native -t markdown -s
Pandoc Meta {unMeta = fromList [ ("format_version", MetaString "3.10") ] } []
^D
---
format_version: "3.10"
---
```