@@ -688,7 +688,8 @@ splitSentences xs =
688688
689689-- | Process ipynb output cells. If mode is Nothing,
690690-- remove all output. If mode is Just format, select
691−-- best output for the format.
691+-- best output for the format. If format is not ipynb,
692+-- strip out ANSI escape sequences from CodeBlocks (see #5633).
692693filterIpynbOutput :: Maybe Format -> Pandoc -> Pandoc
693694filterIpynbOutput mode = walk go
694695 where go (Div (ident, ("output":os), kvs) bs) =
@@ -699,6 +700,7 @@ filterIpynbOutput mode = walk go
699700 | fmt == Format "ipynb"
700701 -> Div (ident, ("output":os), kvs) bs
701702 | otherwise -> Div (ident, ("output":os), kvs) $
703+ walk removeANSI $
702704 take 1 $ sortBy (comparing rank) bs
703705 where
704706 rank (RawBlock (Format "html") _)
@@ -714,6 +716,13 @@ filterIpynbOutput mode = walk go
714716 | otherwise = 3
715717 rank (Para [Image{}]) = 1
716718 rank _ = 2
719+ removeANSI (CodeBlock attr code) =
720+ CodeBlock attr (removeANSIEscapes code)
721+ removeANSI x = x
722+ removeANSIEscapes [] = []
723+ removeANSIEscapes ('\x1b':'[':cs) =
724+ removeANSIEscapes (drop 1 $ dropWhile (/='m') cs)
725+ removeANSIEscapes (c:cs) = c : removeANSIEscapes cs
717726 go x = x
718727
719728--