trailing spaces in code block

John Gruber gruber at
Thu Jun 22 13:46:20 EDT 2006

John MacFarlane <jgm at> wrote on 6/14/06 at 3:12 PM:

> It seems that Markdown 1.0.1 strips off trailing spaces at the end of

> code blocks. Is this really what it's supposed to do? There might be

> reasons to include trailing spaces in code displayed on a web page --

> for example, in demonstrating how to do a hard line break in markdown.

> Even if the trailing spaces aren't visible, they can be cut and

> pasted. Besides, Markdown 1.0.1 preserves trailing spaces on

> every line except the last line of a code block. Is there a reason

> to treat the last line differently?

No, I don't think so. If there is a reason that I implemented it
this way, I have forgotten it, and I can't think of any reason it
should act like this, so I'm calling it a bug in (And
in Markdown.php, too, for obvious reasons.)

The fix is easy. Inside _DoCodeBlocks(), change this:

$codeblock =~ s/\s+\z//; # trim trailing whitespace

to this:

$codeblock =~ s/\n+\z//; # trim trailing newlines

> The reason I ask is that I've written an implementation of markdown in

> haskell. It passes all of the tests in the test suite except "Markdown

> Documentation - Syntax.text", which it fails because it retains the

> trailing spaces in the code block at line 572. I could of course change

> it to remove trailing spaces in code blocks, but if this is a bug and

> not a feature, I'd rather see the test suite changed. Any thoughts?

Yup. Just remove the spurious spaces at the end of line 572 in
"Markdown > Documentation - Syntax.text".

I'll also add a test to test this "preserve spaces on the last
line of a code block" fix.

Thanks for reporting this John. When (if?) you release your
Haskell port, be sure to add it to the list of implementations


More information about the Markdown-Discuss mailing list