Here is my take on rephrasing subsection ā6.3āCode spansā in the CommonMark specification, and adapting the example HTML output by ābrain-parsingā the input according to the modified rules.
The main purpose was to only trim leading and trailing SPACE from code span content when necessary (and even that could be done more parsimonious, and more ācorrectā, by removing at most one SPACE at each end!).
I also took the liberty to change the treatment of āinner white spaceā in code spans a bit, see the rephrased āQ&Aā fragment below: only line breaks are ānormalizedā, but in-line strings of multiple SPACE persist.
The example input and output fragments are simply placed into code blocks here, with just a blank line in between. The MIDDLE DOT takes the role of a āvisible SPACEā, as in the specification.
The example labels are here used as headings, so that the pertaining comment follows immediately after a heading giving the example number.
6.3āCode spans
A backtick string is a string of one or more backtick characters ("`
" U+0060 GRAVE ACCENT) that is neither preceded nor followed by another backtick character.
A code span begins with a backtick string and ends with a backtick string of equal length n, thereby enclosing a non-empty content string. There may be backtick characters in the content string, but not another backtick string of length n.
The character content in the result of parsing the code span is the content string after
-
removing spaces and line breaks that precede the first backtick in the content string (if any), and
-
removing spaces and line break that follow the last backtick in the content string (if any), and
-
replacing line breaks inside the content string, along with adjacent white space, with a single SPACE.
Example 302
This is a simple code span:
`foo`
<p><code>foo</code></p>
Example 303
Here two backticks are used, because the code contains a backtick. This example also illustrates that leading and trailing spaces are not stripped in this case:
``Ā·fooĀ·`Ā·bar ``
<p><code>Ā·fooĀ·`Ā·barĀ·</code></p>
Example 304
This example shows a case where both the leading and trailing space characters are trimmed, because they precede or follow a backtick character in the content string:
`Ā·``Ā·`
<p><code>``</code></p>
Example 305
Line breaks are ānormalizedā to spaces:
``
foo
``
<p><code>Ā·fooĀ·</code></p>
Example 306
Interior line breaks and surrounding white space are ānormalizedā into single spaces:
`fooĀ·Ā·Ā·bar
Ā·Ā·baz`
<p><code>fooĀ·Ā·Ā·barĀ·baz</code></p>
Q: Why not ācollapseā the inner spaces between foo
and bar
too, although browsers will collapse them in many cases anyway?
A: Because this depends on the style sheet used for rendering HTML, and we shouldnāt rely on any specific rendering assumptions.
Example 307
(Existing implementations differ in their treatment of internal spaces and line endings. Some, including Markdown.pl
and showdown
, convert an internal line break into a <BR>
element. But this makes things difficult for those who like to hard-wrap their paragraphs, since a line break in the midst of a code span will cause an unintended line break in the output. Others just leave internal spaces as they are, which is fine if only HTML is being targeted.)
`fooĀ·``Ā·bar`
<p><code>fooĀ·``Ā·bar</code></p>
Example 308
Note that backslash escapes do not work in code spans. All backslashes are treated literally:
`foo\`bar`
<p><code>foo\</code>bar`</p>
Backslash escapes are never needed, because one can always choose a backtick string of length n to delimit code that does not contain any string of n backtick characters.