From cd3fcd5c0e3382097bce81b9836a6a340ec9931b Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Tue, 29 Aug 2023 19:14:50 -0700 Subject: [PATCH] Markdown reader: fix dropped `!` before nonexistent reference. In e.g. `![foo]` the `!` would be silently dropped if `[foo]` wasn't a reference link label. Closes #9038. --- src/Text/Pandoc/Readers/Markdown.hs | 13 +++++++++---- test/command/9038.md | 6 ++++++ 2 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 test/command/9038.md diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index baf911b07..e2e30be25 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -1931,15 +1931,20 @@ referenceLink constructor (lab, raw) = do try ((guardDisabled Ext_spaced_reference_links <|> spnl) >> reference) when (raw' == "") $ guardEnabled Ext_shortcut_reference_links let !labIsRef = raw' == "" || raw' == "[]" - let !key = toKey $ if labIsRef then raw else raw' + let (rawprefix, rawsuffix) = + case T.uncons raw of + Just ('!', x) -> ("!", x) + _ -> ("", raw) + let !key = toKey $ if labIsRef then rawsuffix else raw' parsedRaw <- parseFromString' inlines raw' - fallback <- parseFromString' inlines $ dropBrackets raw + fallback <- parseFromString' inlines $ dropBrackets rawsuffix implicitHeaderRefs <- option False $ True <$ guardEnabled Ext_implicit_header_references let makeFallback = do parsedRaw' <- parsedRaw fallback' <- fallback - return $ B.str "[" <> fallback' <> B.str "]" <> + return $ rawprefix <> + B.str "[" <> fallback' <> B.str "]" <> (if sp && not (T.null raw) then B.space else mempty) <> parsedRaw' return $ do @@ -2014,7 +2019,7 @@ image = try $ do "" -> B.imageWith attr' (T.pack $ addExtension (T.unpack src) $ T.unpack defaultExt) _ -> B.imageWith attr' src - regLink constructor lab <|> referenceLink constructor (lab,raw) + regLink constructor lab <|> referenceLink constructor (lab, "!" <> raw) note :: PandocMonad m => MarkdownParser m (F Inlines) note = try $ do diff --git a/test/command/9038.md b/test/command/9038.md new file mode 100644 index 000000000..3c786aaa1 --- /dev/null +++ b/test/command/9038.md @@ -0,0 +1,6 @@ +``` +% pandoc -t native +![foo] +^D +[ Para [ Str "![foo]" ] ] +```