Mardkown writer: fix spacing issues with definition lists.

- Properly handle the case where the first item is an indented
  code block.  (Closes #11542.)
- Use correct indentation when `four_space_rule` extension is
  disabled.
This commit is contained in:
John MacFarlane
2026-03-20 11:24:27 +01:00
parent f1e061478d
commit 8ce63ffe28
8 changed files with 117 additions and 65 deletions
+8 -3
View File
@@ -578,7 +578,10 @@ blockToMarkdown' opts (CodeBlock attribs str) = do
tildes <> attrs <> cr <> literal str <> cr <> tildes <> blankline tildes <> attrs <> cr <> literal str <> cr <> tildes <> blankline
_ | variant == Markua -> blankline <> attrsToMarkua opts attribs <> cr <> backticks <> cr <> _ | variant == Markua -> blankline <> attrsToMarkua opts attribs <> cr <> backticks <> cr <>
literal str <> cr <> backticks <> cr <> blankline literal str <> cr <> backticks <> cr <> blankline
| otherwise -> nest (writerTabStop opts) (literal str) <> blankline | otherwise -> -- don't use nest: see #11542
let addIndent "" = "\n"
addIndent x = (T.replicate (writerTabStop opts) " ") <> x <> "\n"
in literal (mconcat $ map addIndent (T.lines str)) $$ blankline
where where
endlineLen c = maybe 3 ((+1) . maximum) $ nonEmpty endlineLen c = maybe 3 ((+1) . maximum) $ nonEmpty
[T.length ln [T.length ln
@@ -862,8 +865,10 @@ definitionListItemToMarkdown opts (label, defs) = do
_ -> ":" _ -> ":"
let leadingChars = case tabStop of let leadingChars = case tabStop of
-- Always use two leading characters for Markua -- Always use two leading characters for Markua
n | n >= 2 && variant /= Markua -> n n | variant == Markua -> 2
_ -> 2 | isEnabled Ext_four_space_rule opts
, n >= 2 -> n
| otherwise -> 2
let sps = literal $ T.replicate (leadingChars - 1) " " let sps = literal $ T.replicate (leadingChars - 1) " "
let isTight = case defs of let isTight = case defs of
((Plain _ : _): _) -> True ((Plain _ : _): _) -> True
+47
View File
@@ -0,0 +1,47 @@
```
% pandoc -f native -t markdown
[ DefinitionList
[ ( [ Str "Input" ]
, [ [ CodeBlock ( "" , [] , [] ) "Term\n\n : Def" ] ]
)
]
]
^D
Input
: Term
: Def
```
```
% pandoc -f markdown -t native
Input
: Term
: Def
^D
[ DefinitionList
[ ( [ Str "Input" ]
, [ [ CodeBlock ( "" , [] , [] ) "Term\n\n : Def" ] ]
)
]
]
```
```
% pandoc -f native -t markdown+four_space_rule
[ DefinitionList
[ ( [ Str "Input" ]
, [ [ CodeBlock ( "" , [] , [] ) "Term\n\n : Def" ] ]
)
]
]
^D
Input
: Term
: Def
```
+1 -1
View File
@@ -4,5 +4,5 @@
: description : description
^D ^D
1\. item 1\. item
: description : description
``` ```
+4 -4
View File
@@ -27,13 +27,13 @@ Module FvwmAnimate \[ModuleAlias\]
\*FvwmAnimate: Color color \*FvwmAnimate: Color color
: Tells **FvwmAnimate** what color to draw with. The color is : Tells **FvwmAnimate** what color to draw with. The color is
\"XOR\'ed\" (exclusive ORed) onto the background. \"XOR\'ed\" (exclusive ORed) onto the background.
\*FvwmAnimate: Pixmap pixmap \*FvwmAnimate: Pixmap pixmap
: Tells **FvwmAnimate** to use **pixmap** to draw with. This can be : Tells **FvwmAnimate** to use **pixmap** to draw with. This can be
useful if **\*FvwmAnimate: Color** gives poor results. useful if **\*FvwmAnimate: Color** gives poor results.
``` ```
``` ```
+2 -2
View File
@@ -12,7 +12,7 @@ This is line two.
**\--help** **\--help**
: This is line one. : This is line one.
This is line two. This is line two.
``` ```
+27 -27
View File
@@ -261,93 +261,93 @@ B. Williams
Tight using spaces: Tight using spaces:
apple apple
: red fruit : red fruit
orange orange
: orange fruit : orange fruit
banana banana
: yellow fruit : yellow fruit
Tight using tabs: Tight using tabs:
apple apple
: red fruit : red fruit
orange orange
: orange fruit : orange fruit
banana banana
: yellow fruit : yellow fruit
Loose: Loose:
apple apple
: red fruit : red fruit
orange orange
: orange fruit : orange fruit
banana banana
: yellow fruit : yellow fruit
Multiple blocks with italics: Multiple blocks with italics:
*apple* *apple*
: red fruit : red fruit
contains seeds, crisp, pleasant to taste contains seeds, crisp, pleasant to taste
*orange* *orange*
: orange fruit : orange fruit
{ orange code block } { orange code block }
> orange block quote > orange block quote
Multiple definitions, tight: Multiple definitions, tight:
apple apple
: red fruit : red fruit
: computer : computer
orange orange
: orange fruit : orange fruit
: bank : bank
Multiple definitions, loose: Multiple definitions, loose:
apple apple
: red fruit : red fruit
: computer : computer
orange orange
: orange fruit : orange fruit
: bank : bank
Blank line after term, indented marker, alternate markers: Blank line after term, indented marker, alternate markers:
apple apple
: red fruit : red fruit
: computer : computer
orange orange
: orange fruit : orange fruit
1. sublist 1. sublist
2. sublist 2. sublist
# HTML Blocks # HTML Blocks
+1 -1
View File
@@ -42,7 +42,7 @@
<outline text="Fancy list markers" _note="(2) begins with 2&#10;&#10;(3) and now 3&#10;&#10; with a continuation&#10;&#10; iv. sublist with roman numerals, starting with 4&#10; v. more items&#10; (A) a subsublist&#10; (B) a subsublist&#10;&#10;Nesting:&#10;&#10;A. Upper Alpha&#10; I. Upper Roman.&#10; (6) Decimal start with 6&#10; c) Lower alpha with paren&#10;&#10;Autonumbering:&#10;&#10;1. Autonumber.&#10;2. More.&#10; 1. Nested.&#10;&#10;Should not be a list item:&#10;&#10;M.A. 2007&#10;&#10;B. Williams&#10;&#10;--------------------------------------------------------------------------------"> <outline text="Fancy list markers" _note="(2) begins with 2&#10;&#10;(3) and now 3&#10;&#10; with a continuation&#10;&#10; iv. sublist with roman numerals, starting with 4&#10; v. more items&#10; (A) a subsublist&#10; (B) a subsublist&#10;&#10;Nesting:&#10;&#10;A. Upper Alpha&#10; I. Upper Roman.&#10; (6) Decimal start with 6&#10; c) Lower alpha with paren&#10;&#10;Autonumbering:&#10;&#10;1. Autonumber.&#10;2. More.&#10; 1. Nested.&#10;&#10;Should not be a list item:&#10;&#10;M.A. 2007&#10;&#10;B. Williams&#10;&#10;--------------------------------------------------------------------------------">
</outline> </outline>
</outline> </outline>
<outline text="Definition Lists" _note="Tight using spaces:&#10;&#10;apple&#10;: red fruit&#10;&#10;orange&#10;: orange fruit&#10;&#10;banana&#10;: yellow fruit&#10;&#10;Tight using tabs:&#10;&#10;apple&#10;: red fruit&#10;&#10;orange&#10;: orange fruit&#10;&#10;banana&#10;: yellow fruit&#10;&#10;Loose:&#10;&#10;apple&#10;&#10;: red fruit&#10;&#10;orange&#10;&#10;: orange fruit&#10;&#10;banana&#10;&#10;: yellow fruit&#10;&#10;Multiple blocks with italics:&#10;&#10;*apple*&#10;&#10;: red fruit&#10;&#10; contains seeds, crisp, pleasant to taste&#10;&#10;*orange*&#10;&#10;: orange fruit&#10;&#10; { orange code block }&#10;&#10; &gt; orange block quote&#10;&#10;Multiple definitions, tight:&#10;&#10;apple&#10;: red fruit&#10;: computer&#10;&#10;orange&#10;: orange fruit&#10;: bank&#10;&#10;Multiple definitions, loose:&#10;&#10;apple&#10;&#10;: red fruit&#10;&#10;: computer&#10;&#10;orange&#10;&#10;: orange fruit&#10;&#10;: bank&#10;&#10;Blank line after term, indented marker, alternate markers:&#10;&#10;apple&#10;&#10;: red fruit&#10;&#10;: computer&#10;&#10;orange&#10;&#10;: orange fruit&#10;&#10; 1. sublist&#10; 2. sublist"> <outline text="Definition Lists" _note="Tight using spaces:&#10;&#10;apple&#10;: red fruit&#10;&#10;orange&#10;: orange fruit&#10;&#10;banana&#10;: yellow fruit&#10;&#10;Tight using tabs:&#10;&#10;apple&#10;: red fruit&#10;&#10;orange&#10;: orange fruit&#10;&#10;banana&#10;: yellow fruit&#10;&#10;Loose:&#10;&#10;apple&#10;&#10;: red fruit&#10;&#10;orange&#10;&#10;: orange fruit&#10;&#10;banana&#10;&#10;: yellow fruit&#10;&#10;Multiple blocks with italics:&#10;&#10;*apple*&#10;&#10;: red fruit&#10;&#10; contains seeds, crisp, pleasant to taste&#10;&#10;*orange*&#10;&#10;: orange fruit&#10;&#10; { orange code block }&#10;&#10; &gt; orange block quote&#10;&#10;Multiple definitions, tight:&#10;&#10;apple&#10;: red fruit&#10;: computer&#10;&#10;orange&#10;: orange fruit&#10;: bank&#10;&#10;Multiple definitions, loose:&#10;&#10;apple&#10;&#10;: red fruit&#10;&#10;: computer&#10;&#10;orange&#10;&#10;: orange fruit&#10;&#10;: bank&#10;&#10;Blank line after term, indented marker, alternate markers:&#10;&#10;apple&#10;&#10;: red fruit&#10;&#10;: computer&#10;&#10;orange&#10;&#10;: orange fruit&#10;&#10; 1. sublist&#10; 2. sublist">
</outline> </outline>
<outline text="HTML Blocks" _note="Simple block on one line:&#10;&#10;::: {}&#10;foo&#10;:::&#10;&#10;And nested without indentation:&#10;&#10;:::::: {}&#10;:::: {}&#10;::: {}&#10;foo&#10;:::&#10;::::&#10;&#10;::: {}&#10;bar&#10;:::&#10;::::::&#10;&#10;Interpreted markdown in a table:&#10;&#10;&lt;table&gt;&#10;&lt;tr&gt;&#10;&lt;td&gt;&#10;This is *emphasized*&#10;&lt;/td&gt;&#10;&lt;td&gt;&#10;And this is **strong**&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;/table&gt;&#10;&lt;script type=&quot;text/javascript&quot;&gt;document.write('This *should not* be interpreted as markdown');&lt;/script&gt;&#10;&#10;Here's a simple block:&#10;&#10;::: {}&#10;foo&#10;:::&#10;&#10;This should be a code block, though:&#10;&#10; &lt;div&gt;&#10; foo&#10; &lt;/div&gt;&#10;&#10;As should this:&#10;&#10; &lt;div&gt;foo&lt;/div&gt;&#10;&#10;Now, nested:&#10;&#10;::::: {}&#10;:::: {}&#10;::: {}&#10;foo&#10;:::&#10;::::&#10;:::::&#10;&#10;This should just be an HTML comment:&#10;&#10;&lt;!-- Comment --&gt;&#10;&#10;Multiline:&#10;&#10;&lt;!--&#10;Blah&#10;Blah&#10;--&gt;&#10;&lt;!--&#10; This is another comment.&#10;--&gt;&#10;&#10;Code block:&#10;&#10; &lt;!-- Comment --&gt;&#10;&#10;Just plain comment, with trailing spaces on the line:&#10;&#10;&lt;!-- foo --&gt;&#10;&#10;Code:&#10;&#10; &lt;hr /&gt;&#10;&#10;Hr's:&#10;&#10;&lt;hr&gt;&#10;&lt;hr /&gt;&#10;&lt;hr /&gt;&#10;&lt;hr&gt;&#10;&lt;hr /&gt;&#10;&lt;hr /&gt;&#10;&lt;hr class=&quot;foo&quot; id=&quot;bar&quot; /&gt;&#10;&lt;hr class=&quot;foo&quot; id=&quot;bar&quot; /&gt;&#10;&lt;hr class=&quot;foo&quot; id=&quot;bar&quot;&gt;&#10;&#10;--------------------------------------------------------------------------------"> <outline text="HTML Blocks" _note="Simple block on one line:&#10;&#10;::: {}&#10;foo&#10;:::&#10;&#10;And nested without indentation:&#10;&#10;:::::: {}&#10;:::: {}&#10;::: {}&#10;foo&#10;:::&#10;::::&#10;&#10;::: {}&#10;bar&#10;:::&#10;::::::&#10;&#10;Interpreted markdown in a table:&#10;&#10;&lt;table&gt;&#10;&lt;tr&gt;&#10;&lt;td&gt;&#10;This is *emphasized*&#10;&lt;/td&gt;&#10;&lt;td&gt;&#10;And this is **strong**&#10;&lt;/td&gt;&#10;&lt;/tr&gt;&#10;&lt;/table&gt;&#10;&lt;script type=&quot;text/javascript&quot;&gt;document.write('This *should not* be interpreted as markdown');&lt;/script&gt;&#10;&#10;Here's a simple block:&#10;&#10;::: {}&#10;foo&#10;:::&#10;&#10;This should be a code block, though:&#10;&#10; &lt;div&gt;&#10; foo&#10; &lt;/div&gt;&#10;&#10;As should this:&#10;&#10; &lt;div&gt;foo&lt;/div&gt;&#10;&#10;Now, nested:&#10;&#10;::::: {}&#10;:::: {}&#10;::: {}&#10;foo&#10;:::&#10;::::&#10;:::::&#10;&#10;This should just be an HTML comment:&#10;&#10;&lt;!-- Comment --&gt;&#10;&#10;Multiline:&#10;&#10;&lt;!--&#10;Blah&#10;Blah&#10;--&gt;&#10;&lt;!--&#10; This is another comment.&#10;--&gt;&#10;&#10;Code block:&#10;&#10; &lt;!-- Comment --&gt;&#10;&#10;Just plain comment, with trailing spaces on the line:&#10;&#10;&lt;!-- foo --&gt;&#10;&#10;Code:&#10;&#10; &lt;hr /&gt;&#10;&#10;Hr's:&#10;&#10;&lt;hr&gt;&#10;&lt;hr /&gt;&#10;&lt;hr /&gt;&#10;&lt;hr&gt;&#10;&lt;hr /&gt;&#10;&lt;hr /&gt;&#10;&lt;hr class=&quot;foo&quot; id=&quot;bar&quot; /&gt;&#10;&lt;hr class=&quot;foo&quot; id=&quot;bar&quot; /&gt;&#10;&lt;hr class=&quot;foo&quot; id=&quot;bar&quot;&gt;&#10;&#10;--------------------------------------------------------------------------------">
</outline> </outline>
+27 -27
View File
@@ -257,93 +257,93 @@ Definition Lists
Tight using spaces: Tight using spaces:
apple apple
red fruit red fruit
orange orange
orange fruit orange fruit
banana banana
yellow fruit yellow fruit
Tight using tabs: Tight using tabs:
apple apple
red fruit red fruit
orange orange
orange fruit orange fruit
banana banana
yellow fruit yellow fruit
Loose: Loose:
apple apple
red fruit red fruit
orange orange
orange fruit orange fruit
banana banana
yellow fruit yellow fruit
Multiple blocks with italics: Multiple blocks with italics:
apple apple
red fruit red fruit
contains seeds, crisp, pleasant to taste contains seeds, crisp, pleasant to taste
orange orange
orange fruit orange fruit
{ orange code block } { orange code block }
orange block quote orange block quote
Multiple definitions, tight: Multiple definitions, tight:
apple apple
red fruit red fruit
computer computer
orange orange
orange fruit orange fruit
bank bank
Multiple definitions, loose: Multiple definitions, loose:
apple apple
red fruit red fruit
computer computer
orange orange
orange fruit orange fruit
bank bank
Blank line after term, indented marker, alternate markers: Blank line after term, indented marker, alternate markers:
apple apple
red fruit red fruit
computer computer
orange orange
orange fruit orange fruit
1. sublist 1. sublist
2. sublist 2. sublist
HTML Blocks HTML Blocks