Skip to content

Commit f717d1b

Browse files
committedMar 17, 2025·
Markdown writer: avoid spaces after/before open/close delimiters.
E.g. instead of rendering `x<em> space </em>y` as `x* space *y` we render it as `x *space* y`. Closes #10696.
1 parent 6af1459 commit f717d1b

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed
 

‎src/Text/Pandoc/Writers/Markdown/Inline.hs

+9-9
Original file line numberDiff line numberDiff line change
@@ -372,17 +372,17 @@ inlineToMarkdown opts (Emph lst) = do
372372
contents <- inlineListToMarkdown opts lst
373373
return $ case variant of
374374
PlainText
375-
| isEnabled Ext_gutenberg opts -> "_" <> contents <> "_"
375+
| isEnabled Ext_gutenberg opts -> surroundInlines "_" "_" contents
376376
| otherwise -> contents
377-
_ -> "*" <> contents <> "*"
377+
_ -> surroundInlines "*" "*" contents
378378
inlineToMarkdown _ (Underline []) = return empty
379379
inlineToMarkdown opts (Underline lst) = do
380380
variant <- asks envVariant
381381
contents <- inlineListToMarkdown opts lst
382382
case variant of
383383
PlainText -> return contents
384384
_ | isEnabled Ext_bracketed_spans opts ->
385-
return $ "[" <> contents <> "]" <> "{.underline}"
385+
return $ surroundInlines "[" "]{.underline}" contents
386386
| isEnabled Ext_native_spans opts ->
387387
return $ tagWithAttrs "span" ("", ["underline"], [])
388388
<> contents
@@ -401,12 +401,12 @@ inlineToMarkdown opts (Strong lst) = do
401401
else lst
402402
_ -> do
403403
contents <- inlineListToMarkdown opts lst
404-
return $ "**" <> contents <> "**"
404+
return $ surroundInlines "**" "**" contents
405405
inlineToMarkdown _ (Strikeout []) = return empty
406406
inlineToMarkdown opts (Strikeout lst) = do
407407
contents <- inlineListToMarkdown opts lst
408408
return $ if isEnabled Ext_strikeout opts
409-
then "~~" <> contents <> "~~"
409+
then surroundInlines "~~" "~~" contents
410410
else if isEnabled Ext_raw_html opts
411411
then "<s>" <> contents <> "</s>"
412412
else contents
@@ -415,7 +415,7 @@ inlineToMarkdown opts (Superscript lst) =
415415
local (\env -> env {envEscapeSpaces = envVariant env == Markdown}) $ do
416416
contents <- inlineListToMarkdown opts lst
417417
if isEnabled Ext_superscript opts
418-
then return $ "^" <> contents <> "^"
418+
then return $ surroundInlines "^" "^" contents
419419
else if isEnabled Ext_raw_html opts
420420
then return $ "<sup>" <> contents <> "</sup>"
421421
else
@@ -433,7 +433,7 @@ inlineToMarkdown opts (Subscript lst) =
433433
local (\env -> env {envEscapeSpaces = envVariant env == Markdown}) $ do
434434
contents <- inlineListToMarkdown opts lst
435435
if isEnabled Ext_subscript opts
436-
then return $ "~" <> contents <> "~"
436+
then return $ surroundInlines "~" "~" contents
437437
else if isEnabled Ext_raw_html opts
438438
then return $ "<sub>" <> contents <> "</sub>"
439439
else
@@ -511,7 +511,7 @@ inlineToMarkdown opts (Math InlineMath str) = do
511511
_ | isEnabled Ext_tex_math_gfm opts ->
512512
return $ "$`" <> literal str <> "`$"
513513
| isEnabled Ext_tex_math_dollars opts ->
514-
return $ "$" <> literal str <> "$"
514+
return $ surroundInlines "$" "$" (literal str)
515515
| isEnabled Ext_tex_math_single_backslash opts ->
516516
return $ "\\(" <> literal str <> "\\)"
517517
| isEnabled Ext_tex_math_double_backslash opts ->
@@ -540,7 +540,7 @@ inlineToMarkdown opts (Math DisplayMath str) = do
540540
$$ literal str
541541
$$ literal "```") <> cr
542542
| isEnabled Ext_tex_math_dollars opts ->
543-
return $ "$$" <> literal str <> "$$"
543+
return $ surroundInlines "$$" "$$" (literal str)
544544
| isEnabled Ext_tex_math_single_backslash opts ->
545545
return $ "\\[" <> literal str <> "\\]"
546546
| isEnabled Ext_tex_math_double_backslash opts ->

‎test/Tests/Writers/Markdown.hs

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ tests = [ "indented code after list"
4646
=: bulletList [ plain "foo" <> bulletList [ plain "bar" ],
4747
plain "baz" ]
4848
=?> "- foo\n - bar\n- baz\n"
49+
, "emph/strong with spaces (#10696)"
50+
=: emph (str "f" <> strong (space <> str "d" <> space)) <> str "l" =?>
51+
"*f **d*** l"
4952
] ++ [noteTests] ++ [shortcutLinkRefsTests]
5053

5154
{-

0 commit comments

Comments
 (0)
Please sign in to comment.