From 8a52ac149379a3524fab37ecec75ec2dc6dfbc66 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 12 Jun 2026 20:43:29 +0200 Subject: [PATCH] Markdown writer: improve identification of code language class... ...by looking it up in the syntax map. (Previously we just used the first, excepting `sourceCode`.) Closes #11701 (together with previous commit). --- src/Text/Pandoc/Writers/Markdown.hs | 11 ++++++----- test/command/11701.md | 9 +++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 test/command/11701.md diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index ed4c27077..946a35e10 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -28,7 +28,7 @@ import Data.Default import Data.List (intersperse, sortOn, union, find) import Data.List.NonEmpty (nonEmpty, NonEmpty(..)) import qualified Data.Map as M -import Data.Maybe (fromMaybe, mapMaybe, isNothing) +import Data.Maybe (fromMaybe, mapMaybe, isNothing, isJust) import qualified Data.Set as Set import Data.Text (Text) import Data.Char (isSpace) @@ -55,6 +55,7 @@ import Text.Pandoc.Writers.Markdown.Types (MarkdownVariant(..), WriterState(..), WriterEnv(..), Ref, Refs, MD, evalMD) +import Skylighting (lookupSyntax) -- | Convert Pandoc to Markdown. writeMarkdown :: PandocMonad m => WriterOptions -> Pandoc -> m Text @@ -596,7 +597,7 @@ blockToMarkdown' opts (CodeBlock attribs str) = do then nowrap $ " " <> classOrAttrsToMarkdown opts attribs else let (_,cls,_) = attribs - in case getLangFromClasses cls of + in case getLangFromClasses opts cls of Just l -> " " <> literal l Nothing -> empty blockToMarkdown' opts (BlockQuote blocks) = do @@ -958,11 +959,11 @@ computeDivNestingLevel = foldr go 0 -- Identify the class in a list of classes that corresponds to -- the language syntax. language-X turns to X. -getLangFromClasses :: [Text] -> Maybe Text -getLangFromClasses cs = +getLangFromClasses :: WriterOptions -> [Text] -> Maybe Text +getLangFromClasses opts cs = case find ("language-" `T.isPrefixOf`) cs of Just x -> Just (T.drop 9 x) Nothing -> - case filter (/= "sourceCode") cs of + case [x | x <- cs, isJust (lookupSyntax x (writerSyntaxMap opts))] of (x:_) -> Just x [] -> Nothing diff --git a/test/command/11701.md b/test/command/11701.md new file mode 100644 index 000000000..c4bcb9e26 --- /dev/null +++ b/test/command/11701.md @@ -0,0 +1,9 @@ +```` +% pandoc -f html -t gfm +
echo hello
+
+^D +``` bash +echo hello +``` +````