Inlines are read from the text of paragraphs, headings and table cells, once all the blocks are known. This page covers section 6 of the CommonMark specification, version 0.31.2. GFM adds strikethrough and extended autolinks.
6.1 Code spans
A backtick string is a run of backticks not next to another backtick.
A code span begins with one and ends with the next backtick string of equal length; the
text between is the code. Line endings in it become spaces. If the result both begins
and ends with a space, and holds something other than spaces, one space is removed from
each end, so that `` ` `` can show a backtick.
Backslash escapes do not work in code spans. A code span binds more tightly than any other inline except raw HTML and autolinks, whichever began first.
6.2 Emphasis and strong emphasis
A delimiter run is a run of * not next to another
*, or a run of _ likewise. It is:
- Left-flanking if it is not followed by Unicode whitespace, and either not followed by punctuation, or followed by punctuation and preceded by whitespace or punctuation.
- Right-flanking if it is not preceded by Unicode whitespace, and either not preceded by punctuation, or preceded by punctuation and followed by whitespace or punctuation.
The start and end of a line count as whitespace. The rules:
- A single
*can open emphasis if it is part of a left-flanking delimiter run. - A single
_can open emphasis if it is left-flanking and either not right-flanking, or right-flanking and preceded by punctuation. - A single
*can close emphasis if it is right-flanking. - A single
_can close emphasis if it is right-flanking and either not left-flanking, or left-flanking and followed by punctuation. - A double
**can open strong emphasis if it is left-flanking. - A double
__can open strong emphasis under the same condition as rule 2. - A double
**can close strong emphasis if it is right-flanking. - A double
__can close strong emphasis under the same condition as rule 4. - Emphasis begins with a delimiter that can open it and ends with one that can close it, using the same character. The two must be from different runs. If one of them can both open and close emphasis, the sum of the lengths of the runs holding them must not be a multiple of 3, unless both lengths are multiples of 3.
- Strong emphasis follows the same pattern, and the same rule of 3.
- A literal
*cannot come at the start or end of*-delimited emphasis unless backslash-escaped. Likewise for_. - A literal
**cannot come at the start or end of**-delimited strong emphasis, likewise for__. - The number of nestings should be minimised, so
**…**is one strong emphasis rather than emphasis within emphasis. - An interpretation of the form
<em><strong>…</strong></em>is always preferred to<strong><em>…</em></strong>. - When two possible spans overlap, the one that starts first wins.
- When two spans have the same closing delimiter, the shorter one wins.
- Inline code spans, links, images and HTML tags group more tightly than emphasis.
*foo bar* emphasis
a * foo bar* not: the opener is followed by a space
foo*bar* emphasis inside a word
foo_bar_ not: _ does not work inside a word
***bold em*** <em><strong>…</strong></em>
6.3 Links
A link has link text, a destination and optionally a title. Link text is written in square brackets and may hold other inlines, but no other link, at any depth. Its brackets must be balanced, or escaped. Code spans, autolinks and raw HTML bind more tightly than the brackets. There are two kinds of link.
An inline link is link text followed directly by
(, optional white space, an optional destination, optional white space and
title, optional white space, and ).
| Part | Forms |
|---|---|
| Destination | Between < and >, with no line ending and no unescaped angle bracket; or, not starting with <, a run with no ASCII control characters or spaces, in which parentheses are balanced or escaped (at least three levels deep are supported). |
| Title | Between " or ', or between ( and ). The closing mark, and in the parenthesised form an opening one, must be backslash-escaped inside. A title may run over several lines but holds no blank line. |
| Label | Between [ and the first unescaped ]; at least one non-space character, at most 999 characters. See link reference definitions. |
Escapes and references are read in the destination and title. A reference link uses a definition elsewhere in the document, in one of three forms:
| Form | Written | Rule |
|---|---|---|
| Full | [text][label] | The label matches a definition. No white space may come between the two brackets. |
| Collapsed | [label][] | The text is also the label, and is shown as the link text. |
| Shortcut | [label] | The same, with no second brackets. Not followed by [] or another label. |
Full and collapsed references take precedence over a shortcut reference, and an inline link takes precedence over a reference.
6.4 Images
An image is written like a link with a ! in front: ,
or the full, collapsed or shortcut reference forms. The text is the image’s
description. It may contain other inlines, including links, and the
alternative text in the output is only its plain text, with no formatting.
6.5 Autolinks
An autolink is written between angle brackets, and the text is shown as the link.
| Kind | Form |
|---|---|
| URI autolink | A scheme of 2 to 32 characters, starting with an ASCII letter and continuing with letters, digits, +, . or -; then a colon; then a run with no ASCII control characters, spaces, < or >. |
| E-mail autolink | An address matching the pattern in the HTML standard’s definition of a valid e-mail address. The link goes to mailto: and the address. |
The extended autolinks in GFM need no angle brackets.
6.6 Raw HTML
Text between < and > that looks like an HTML tag is
passed through to the output unescaped. Line endings are allowed within it.
| Part | Form |
|---|---|
| Tag name | An ASCII letter, then letters, digits or hyphens. |
| Attribute name | An ASCII letter, _ or :, then letters, digits, _, ., : or -. |
| Attribute value | Unquoted (no spaces, tabs, line endings, ", ', =, <, > or `), or in single or double quotes. |
| Open tag | <, a tag name, attributes separated by white space, optional white space, optional /, >. |
| Closing tag | </, a tag name, optional white space, >. |
| Comment | <!-- to -->; also the short forms <!--> and <!--->. |
| Processing instruction | <? to ?>. |
| Declaration | <!, an ASCII letter, then anything but >, then >. |
| CDATA section | <![CDATA[ to ]]>. |
6.7 Hard line breaks
A line ending preceded by two or more spaces, or by a backslash, inside a block of
inlines. In HTML this is <br />. Spaces at the start of the next line
are ignored. A break does not occur at the end of a block, or inside code spans or
raw HTML.
6.8 Soft line breaks
Any other line ending in a block of inlines. It is rendered as a line ending or a space in HTML, which browsers show as a space; other renderers may use one or the other. Spaces at the end of the line and the start of the next are removed.
6.9 Textual content
Any characters not given a meaning by the rules above are plain text. Internal spaces are kept as they are; a viewer in HTML will collapse them when it displays the page.
In the viewer
Each inline is a node with the source’s bytes: code spans with their backticks, emphasis with its delimiter runs, links with their text, destination and title or label, raw HTML with its tag and attributes, and each line break as its own node.