git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: Ensure the code fence is long enough.

Previously too few backticks were used when the code block contained an indented line of backticks. (Ditto tildes.) Cloess #5519.

commitaef71894ceedb774c49b18db15fbde34396cfb85 authorJohn MacFarlane <jgm@berkeley.edu> dateWed 22 May 2019, 15:21 (7 years ago) parentc17ace8
2 files changed+24−11
src/Text/Pandoc/Writers/Markdown.hs+10−11 test/command/5519.md+14
src/Text/Pandoc/Writers/Markdown.hs+10−11
@@ -23,7 +23,8 @@ import Control.Monad.State.Strict
2323import Data.Char (isPunctuation, isSpace, isAlphaNum)
2424import Data.Default
2525import qualified Data.HashMap.Strict as H
26−import Data.List (find, group, intersperse, sortBy, stripPrefix, transpose)
26+import Data.List (find, group, intersperse, sortBy, stripPrefix, transpose,
27+ isPrefixOf)
2728import qualified Data.Map as M
2829import Data.Maybe (fromMaybe)
2930import Data.Monoid (Any (..))
@@ -544,16 +545,14 @@ blockToMarkdown' opts (CodeBlock attribs str) = return $
544545 | isEnabled Ext_fenced_code_blocks opts ->
545546 tildes <> attrs <> cr <> text str <> cr <> tildes <> blankline
546547 _ -> nest (writerTabStop opts) (text str) <> blankline
547− where tildes = text $ case [ln | ln <- lines str, all (=='~') ln] of
548− [] -> "~~~~"
549− xs -> case maximum $ map length xs of
550− n | n < 3 -> "~~~~"
551− | otherwise -> replicate (n+1) '~'
552− backticks = text $ case [ln | ln <- lines str, all (=='`') ln] of
553− [] -> "```"
554− xs -> case maximum $ map length xs of
555− n | n < 3 -> "```"
556− | otherwise -> replicate (n+1) '`'
548+ where endline c = text $ case [length ln
549+ | ln <- map trim (lines str)
550+ , [c,c,c] `isPrefixOf` ln
551+ , all (== c) ln] of
552+ [] -> replicate 3 c
553+ xs -> replicate (maximum xs + 1) c
554+ backticks = endline '`'
555+ tildes = endline '~'
557556 attrs = if isEnabled Ext_fenced_code_attributes opts
558557 then nowrap $ " " <> attrsToMarkdown attribs
559558 else case attribs of
test/command/5519.mdnew file+14
@@ -0,0 +1,14 @@
1+~~~
2+% pandoc -t markdown
3+``````{.attr}
4+ ```
5+ code
6+ ```
7+``````
8+^D
9+```` {.attr}
10+ ```
11+ code
12+ ```
13+````
14+~~~