Leading/trailing whitespace in inline <code>

Dan markdown-discuss at drmoose.net
Fri Aug 24 09:21:22 EDT 2018


I'm trying to document a codebase in markdown, and finding it difficult to include links inside my code examples. If I were writing HTML, I'd write:

<code>perl <a href="https://daringfireball.net/projects/markdown/">Markdown.pl</a> < input.md</code>

But, there doesn't seem to be a way to achieve this in Markdown, because any markup i include inside backticks gets HTML encoded (as it should). The closest I can do in markdown is:

`perl` [`Markdown.pl`](https://daringfireball.net/projects/markdown/) `< input.md`

Which renders as

<p><code>perl</code> <a href="https://daringfireball.net/projects/markdown/"><code>Markdown.pl</code></a> <code>< input.md</code></p>

but is fraught with style issues. Not only is a space a different width outside <code>, but the theme I'm using puts a background and borders on code tags.

As a workaround, would you be willing to allow _DoCodeSpans to preserve leading & trailing white space inside the backticks?

--- Markdown.pl.orig	2018-08-24 08:47:13.603707569 -0400
+++ Markdown.pl	2018-08-24 08:48:46.949349781 -0400
@@ -983,8 +983,6 @@
 			(?!`)
 		@
  			my $c = "$2";
- 			$c =~ s/^[ \t]*//g; # leading whitespace
- 			$c =~ s/[ \t]*$//g; # trailing whitespace
  			$c = _EncodeCode($c);
 			"<code>$c</code>";
 		@egsx;

With this patch, markdown like this:

`perl `[`Markdown.pl`](https://daringfireball.net/projects/markdown/)` < input.md`

renders as 

<p><code>perl </code><a href="https://daringfireball.net/projects/markdown/"><code>Markdown.pl</code></a><code> < input.md</code></p>

with the whitespace inside the <code> and no space between the </code> and the <a>, making it easier to clean up the look of the resulting webpage with CSS.

Thanks very much for your time & for Markdown.
-Dan


More information about the Markdown-Discuss mailing list