7

I was answering a question on Stack Overflow and required to use a trailing space in one of my code snippets.

I formatted it like so:

`<a `

And it renders without the trailing space: <a

Using the <code> tag does account for trailing spaces, though in my case it busts because of the <. Here's my formatting with the <code> tag:

<code><a </code>

Here's how it renders: (Yes, you should see nothing there).

This is a pretty annoying bug. Can we get it fixed?

0

2 Answers 2

5

Is there some reason you can't just use the Unicode character U+00A0 (NO-BREAK SPACE) for that trailing space? It should work in comments too. To type the actual character on Windows, you can use the key combination ALT+0160.

Here's how it looks: <a 

5
  • Using Alt+0160 works.
    – Someone
    Commented Feb 27, 2012 at 2:31
  • Good, I thought it shoudl. Of course, that’s only if you have your keyboard configured for Unicode input. I usually leave mine on the U.S. or U.S. International keyboard, and only switch to the Unicode input keyboard for entering a bunch of character by their code point number. Otherwise, I just use murine snarf-n-barf.
    – tchrist
    Commented Feb 27, 2012 at 2:33
  • Uh, I use the US keyboard layout, but Alt+0160 works just fine for me...
    – Cody Gray
    Commented Feb 27, 2012 at 7:13
  • @CodyGray You must be a prisoner of bill, then, right?
    – tchrist
    Commented Feb 27, 2012 at 11:45
  • Reference to Bill Gates? Not really. I'm actually a Mac user and have been for a long time. But I also do Windows programming, so yeah I use Windows for that. I've never used Alt+xxx keyboard shortcuts on the Mac, so I assumed you were talking about Windows. I guess they work in Ubuntu et al?
    – Cody Gray
    Commented Feb 27, 2012 at 17:06
8

Markdown strips leading and trailing whitespace in code spans:

http://code.google.com/p/pagedown/source/browse/Markdown.Converter.js#1011

http://code.google.com/p/markdownsharp/source/browse/MarkdownSharp/Markdown.cs#1358

The same is true for John Gruber's original Perl version. The comment before the function _DoCodeSpans also explains why:

#   *   You can use spaces to get literal backticks at the edges:
#     
#         ... type `` `bar` `` ...
#     

When you start a code span with, say, three backticks, you also have to end it with precisely three backticks. If the trailing whitespace was significant, it would be impossible to have a backtick at the end, since

```backtick: ````

is not a legal code span.

By the way, the reason that your <code><a </code> doesn't display anything is simple: It's broken HTML. You're opening an <a> tag, but you're not putting a closing angle bracket. So the sanitizer will remove the illegal HTML "tag" <a </code>, and then the tag balancer will remove the opening <code> tag, since it doesn't have a closing </code> anymore.

When you write your own HTML, Markdown won't do any encoding for you; you'l have to replace <a by &lt;a yourself:

foo <code>&lt;a </code> bar -> foo <a bar

foo <code>&lt;a&nbsp;</code> bar -> foo <a  bar

Between the last two, note the tiny difference in whitespace after the code, resulting from the browser's whitespace condensing.

6
  • Hmm, the fact that the original implementation uses [ \t]* seems inconsistent with the syntax guide's statement that "The backtick delimiters surrounding a code span may include spaces — one after the opening, one before the closing" (emphasis mine). Of course, even with the example in this question I still don't understand the use case that benefits from leaving extra spaces in, so the implementation makes more sense to me anyway.
    – Tim Stone
    Commented Feb 27, 2012 at 7:40
  • See this question for a use case of trailing spaces. Commented May 29, 2012 at 5:09
  • @MartijnPieters Quite the opposite. This answer explains how this behavior matches original Markdown and why it behaves that way. And your quote even says that the space is part of the delimiter -- and so it shouldn't be rendered.
    – balpha StaffMod
    Commented Feb 16, 2018 at 13:31
  • Right, but in this case the space caused issues and the backticks did not get parsed correctly. Perhaps I missed something there. Commented Feb 16, 2018 at 13:44
  • And you are right, I misread your answer here. I thought the spaces caused the backticks to be ignored, mea culpa. Commented Feb 16, 2018 at 13:45
  • Even worse, this post isn't even about comments. :-P Commented Feb 16, 2018 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .