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.
This is a simple code span:
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:
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:
Line breaks are “normalized” to spaces:
Interior line breaks and surrounding white space are “normalized” into single spaces:
Q: Why not “collapse” the inner spaces between
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.
(Existing implementations differ in their treatment of internal spaces and line endings. Some, including
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.)
Note that backslash escapes do not work in code spans. All backslashes are treated literally:
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.