Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 158 Stack Exchange communities.

What is meta?
Here's how it works:
  1. Any Stack Exchange user can ask a question
  2. The community provides support, votes on ideas, and reports bugs
  3. Your voice helps shape the way Stack Exchange operates

I noticed the other day that two nearly identical answers to a question were slightly different.

The first one (the accepted answer) uses four spaces. The second answer (highest non-accepted answer, at time of this writing) uses <code> tags. I'm pretty sure this is why it doesn't highlight.

It seems to me that the two should use the same engine for rendering, but it doesn't appear to be that way.

share|improve this question
Related: How do I format my code blocks? – Arjan Feb 11 '13 at 22:42
up vote 4 down vote accepted

No, they don't use the same engine. For one, the markdown syntax adds a <pre class="..."> tag around the <code> tags with language highlighting metadata.

Markdown happens to support inline HTML for 'manual' formatting, but once you move to such manual formatting, you also loose the possibility to attach richer metadata such as the language highlighter to use.

If you must use <code> add a <pre> tag around it (no class or style information, that'll cause it to be filtered out) and optionally add a highlight hint:

<!-- language: lang-xml -->

    &lt;bogus>&lt;xml />&lt;/bogus>


    <bogus><xml /></bogus>

but you have to do all the escaping by hand. :-)

share|improve this answer
So <code> isn't part of Markdown, it's actually part of html? – corsiKa Feb 11 '13 at 22:27
@corsiKa: exactly. It's a HTML 5 element. – Martijn Pieters Feb 11 '13 at 22:28
Well then, I'd say it's hardly a bug... thanks! – corsiKa Feb 11 '13 at 22:29
@corsiKa: nope, this is status-bydesign. – Martijn Pieters Feb 11 '13 at 22:30
<pre><code> works just fine. – Arjan Feb 11 '13 at 22:33
@Arjan: Ah, it's the class and style attributes that cause it to be filtered out. – Martijn Pieters Feb 11 '13 at 22:35
Not sure what you mean by that. Plain <pre><code> is also enhanced by the SE JavaScript, which then makes Prettify highlight it. Still then, Markdown 4-spaces is so much easier, if only as one does not have to encode any < and > characters. – Arjan Feb 11 '13 at 22:38
...and the highlight hint is not needed if the question is tagged with some language ;-) – Arjan Feb 11 '13 at 22:43
@Arjan: I mean that if you add in <pre class="..."> the pre tag is removed again. I was updating my answer with the same info already. :) – Martijn Pieters Feb 11 '13 at 22:43
@Arjan: Ah, added a 'optionally' in there. – Martijn Pieters Feb 11 '13 at 22:44
Hmm that sure does seem like a lot of work. I guess it was just by a user who didn't know about Markdown. Thanks for the thorough analysis! – corsiKa Feb 12 '13 at 15:37

The highlighting doesn't work because it's specified in the surround <pre> tag, not the <code> tag:

<pre class="lang-xml prettyprint" style="">

Using <pre><code> works just fine too:

Look ma, <em style="xyz">highlighting</em>!

Using <code> alone is bad practice. If you must use HTML instead of Markdown, you should use <pre><code>, which is what indentation by four spaces generates.

share|improve this answer
prettyprinted is added by Prettify. – Arjan Feb 11 '13 at 22:28
I just ninja-edited the answer to add <pre> tags. It prettifies. – Dennis Feb 11 '13 at 22:29
(I meant the "prettyprinted" CSS class; but I edited it out.) – Arjan Feb 11 '13 at 22:35

You must log in to answer this question.

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