From 91444cba3aaa28e447e4c8ebe70e185657be687d Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Mon, 30 Oct 2023 13:49:12 -0700 Subject: [PATCH] Do not accept footnote definitions with line breaks This change brings commonmark-hs into alignment with [markdown-it] and hugo/goldmark. It's close to GitHub, but GitHub [doesn't parse the backslashes as hard line breaks][gist]. It's odd that link reference definitions are allowed to have line breaks and footnote definitions aren't, but it's not worth it to be gratuitously different. [markdown-it]: https://markdown-it.github.io/#md3=%7B%22source%22%3A%22%5B%5Efoo%5C%5C%5Cnbar%5D%3A%20not%20a%20footnote%20definition%5Cn%5Cn%5Bbaz%5C%5C%5Cnquux%5D%3A%20https%3A%2F%2Frust-lang.org%5Cn%5Cn%5Bfirst%5Cnsecond%5D%3A%20https%3A%2F%2Frust-lang.org%5Cn%5Cn%5B%5Ethird%5Cnfourth%5D%3A%20not%20a%20footnote%20definition%5Cn%5Cn%5Bbaz%5C%5C%5Cnquux%5D%5Cn%5B%5Efoo%5C%5C%5Cnbar%5D%5Cn%5Bfirst%5Cnsecond%5D%5Cn%5B%5Ethird%5Cnfourth%5D%22%2C%22defaults%22%3A%7B%22html%22%3Afalse%2C%22xhtmlOut%22%3Afalse%2C%22breaks%22%3Afalse%2C%22langPrefix%22%3A%22language-%22%2C%22linkify%22%3Atrue%2C%22typographer%22%3Atrue%2C%22_highlight%22%3Atrue%2C%22_strict%22%3Afalse%2C%22_view%22%3A%22html%22%7D%7D [gist]: https://gist.github.com/notriddle/7fd2410a2585123ad0f894b08c58b21f --- .../src/Commonmark/Extensions/Footnote.hs | 7 +++- commonmark-extensions/test/footnotes.md | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/commonmark-extensions/src/Commonmark/Extensions/Footnote.hs b/commonmark-extensions/src/Commonmark/Extensions/Footnote.hs index b5120b4..c2d11b1 100644 --- a/commonmark-extensions/src/Commonmark/Extensions/Footnote.hs +++ b/commonmark-extensions/src/Commonmark/Extensions/Footnote.hs @@ -101,9 +101,12 @@ footnoteBlockSpec = BlockSpec pFootnoteLabel :: Monad m => ParsecT [Tok] u m Text pFootnoteLabel = try $ do - lab <- pLinkLabel + lab <- untokenize + <$> try (between (symbol '[') (symbol ']') + (snd <$> withRaw (many + (pEscaped <|> noneOfToks [Symbol ']', Symbol '[', LineEnd])))) case T.uncons lab of - Just ('^', t') | T.any (\x -> x /= ' ' && x /= '\t' && x /= '\r' && x /= '\n') t' + Just ('^', t') | T.any (\x -> x /= ' ' && x /= '\t') t' && T.all (\x -> x /= '\n' && x /= '\r') t' -> return $! t' _ -> mzero diff --git a/commonmark-extensions/test/footnotes.md b/commonmark-extensions/test/footnotes.md index 99cf4c5..148e5f9 100644 --- a/commonmark-extensions/test/footnotes.md +++ b/commonmark-extensions/test/footnotes.md @@ -158,3 +158,41 @@ Test [^] link [^ ]

[^ ]: not a footnote

```````````````````````````````` + +Footnote labels cannot contain line breaks, even where link labels can. + +```````````````````````````````` example +[^foo\ +bar]: not a footnote definition + +[baz\ +quux]: https://haskell.org + +[first +second]: https://haskell.org + +[^third +fourth]: not a footnote definition + +[baz\ +quux] +[^foo\ +bar] +[first +second] +[^third +fourth] +. +

[^foo
+bar]: not a footnote definition

+

[^third +fourth]: not a footnote definition

+

baz
+quux
+[^foo
+bar] +first +second +[^third +fourth]

+````````````````````````````````