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).
This commit is contained in:
John MacFarlane
2026-06-12 20:43:29 +02:00
parent 6049ab9c09
commit 8a52ac1493
2 changed files with 15 additions and 5 deletions
+6 -5
View File
@@ -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
+9
View File
@@ -0,0 +1,9 @@
````
% pandoc -f html -t gfm
<pre style="white-space: pre-wrap"><code class="hljs language-bash">echo hello
</code></pre>
^D
``` bash
echo hello
```
````