jgm/doclayout

Recent versions of pandoc swallow the leading blank line of fenced code blocks when converting from markdown to latex

Closed this issue · 5 comments

When converting a fenced code block with a leading blank line from markdown to latex, this one line doesn't appear in the output. For example,

```

This is the second line.
```

produces

\begin{verbatim}
This is the second line.
\end{verbatim}

This bug appears in version 2.5 and later. However, version 2.2.3.2 works and preserves the leading blank line as is.

jgm commented

2.5 isn't exactly "recent"! It's over 4 years old.

Interestingly, the initial newline is preserved in the AST:

% pandoc -t native  
```
test
```
^D
[ CodeBlock ( "" , [] , [] ) "test" ]
% pandoc -t native
```

test
```
^D
[ CodeBlock ( "" , [] , [] ) "\ntest" ]
```

But the distinction is lost in converting not just to latex, but to markdown:

% pandoc -f native -t latex
[ CodeBlock ( "" , [] , [] ) "\ntest", CodeBlock ( "", [] , [] ) "test" ]
^D
\begin{verbatim}
test
\end{verbatim}

\begin{verbatim}
test
\end{verbatim}

% pandoc -f native -t markdown
[ CodeBlock ( "" , [] , [] ) "\ntest", CodeBlock ( "", [] , [] ) "test" ]
^D
    test

    test

In HTML output, by contrast, it is preserved:

% pandoc -f native -t html    
[ CodeBlock ( "" , [] , [] ) "\ntest", CodeBlock ( "", [] , [] ) "test" ]
^D
<pre><code>
test</code></pre>
<pre><code>test</code></pre>
jgm commented

Looking at the diffs in the LaTeX writer between 2.2.3.2 and 2.5, I don't see anything that could account for this.

jgm commented

OK, I think the newline is probably getting collapsed by Text.Pandoc.Pretty (now doclayout)...

jgm commented

The issue demonstrated using just doclayout:

Prelude Text.DocLayout> render Nothing $ literal "hi" $$ literal "\nok" $$ literal "done"
"hi\nok\ndone"
Prelude Text.DocLayout> render Nothing $ literal "hi" $$ literal "ok" $$ literal "done"
"hi\nok\ndone"
jgm commented

This explains why it affects the latex and markdown writers (which use doclayout) and not HTML (which uses blaze-html for layout).