@@ -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