The official Markdown syntax reference page says (emphasis added):

The only restrictions are that block-level HTML elements — e.g. <div>, <table>, <pre>, <p>, etc. — must be separated from surrounding content by blank lines, and the start and end tags of the block should not be indented with tabs or spaces.

Partly prompted by the recent question Bug when rendering "<" inside a <pre> tag, but also from observing some of the tricks people use to add 'creative' formatting to code blocks, I have observed that indenting <pre> with a single space avoids having to encode HTML entities manually, and allows markdown formatting:

Example 1

Adding a leading space before the <pre><code> example used in the linked question:

 <pre><code># here we are in a pre/code tag
low < high</code></pre>

Renders (without encoding HTML entities) as:

# here we are in a pre/code tag
low < high

Some start-of line markdown must be \ escaped (e.g. '#'), so:

 <pre><code># here we are in a pre/code tag
low < high
\# end of pre/code tag</code></pre>

displays as:

# here we are in a pre/code tag
low < high
# end of pre/code tag

Example 2

People already use HTML to emphasise code with syntax highlighting, so:

<pre><code>CREATE SEQUENCE dbo.Sequence1
AS integer 
START WITH 1 
INCREMENT BY 1 
<b>MAXVALUE 100</b> 
CYCLE;</code></pre>

becomes:

CREATE SEQUENCE dbo.Sequence1
AS integer 
START WITH 1 
INCREMENT BY 1 
MAXVALUE 100 
CYCLE;

...which I presume is fine as a way around the limitations of markdown code formatting.

Example 3

Using a space before <pre> without <code> allows markdown, including links:

 <pre>[CREATE SEQUENCE][2] dbo.Sequence1
AS integer
*START WITH 1*
INCREMENT BY 1
**MAXVALUE 100**
CYCLE;</pre>

is shown as:

CREATE SEQUENCE dbo.Sequence1
AS integer
START WITH 1
INCREMENT BY 1
MAXVALUE 100
CYCLE;

...which uses a keyword hyperlink (to the documentation, not Rick Astley) and markdown formatting, at the expense of losing syntax highlighting. The same thing can be achieved in HTML using <a>.

Questions

  1. Why does adding a space before <pre> work this way?
  2. Is it an unreliable hack/misuse that should be avoided/prevented?
  3. Should code formatting like this be converted to HTML by editors?

You must log in to answer this question.

Browse other questions tagged .