Setext headings
A setext heading consists of one or more lines of text, each containing at least one non-whitespace character, with no more than 3 spaces indentation, followed by a setext heading underline. The lines of text must be such that, were they not followed by the setext heading underline, they would be interpreted as a paragraph: they cannot be interpretable as a code fence, ATX heading, block quote, thematic break, list item, or HTML block.
A setext heading underline is a sequence of =
characters or a sequence of -
characters, with no more than 3 spaces indentation and any number of trailing spaces. If a line containing a single -
can be interpreted as an empty list items, it should be interpreted this way and not as a setext heading underline.
The heading is a level 1 heading if =
characters are used in the setext heading underline, and a level 2 heading if -
characters are used. The contents of the heading are the result of parsing the preceding lines of text as CommonMark inline content.
In general, a setext heading need not be preceded or followed by a blank line. However, it cannot interrupt a paragraph, so when a setext heading comes after a paragraph, a blank line is needed between them.
Simple examples:
The content of the header may span more than one line:
The underlining can be any length:
The heading content can be indented up to three spaces, and need not line up with the underlining:
Four spaces indent is too much:
The setext heading underline can be indented up to three spaces, and may have trailing spaces:
Four spaces is too much:
The setext heading underline cannot contain internal spaces:
Trailing spaces in the content line do not cause a line break:
Nor does a backslash at the end:
Since indicators of block structure take precedence over indicators of inline structure, the following are setext headings:
The setext heading underline cannot be a lazy continuation line in a list item or block quote:
A blank line is needed between a paragraph and a following setext heading, since otherwise the paragraph becomes part of the heading’s content:
But in general a blank line is not required before or after setext headings:
Setext headings cannot be empty:
Setext heading text lines must not be interpretable as block constructs other than paragraphs. So, the line of dashes in these examples gets interpreted as a thematic break:
If you want a heading with > foo
as its literal text, you can use backslash escapes:
Compatibility note: Most existing Markdown implementations do not allow the text of setext headings to span multiple lines. But there is no consensus about how to interpret
Foo
bar
---
baz
One can find four different interpretations:
- paragraph “Foo”, heading “bar”, paragraph “baz”
- paragraph “Foo bar”, thematic break, paragraph “baz”
- paragraph “Foo bar — baz”
- heading “Foo bar”, paragraph “baz”
We find interpretation 4 most natural, and interpretation 4 increases the expressive power of CommonMark, by allowing multiline headings. Authors who want interpretation 1 can put a blank line after the first paragraph:
Authors who want interpretation 2 can put blank lines around the thematic break,
or use a thematic break that cannot count as a setext heading underline, such as
Authors who want interpretation 3 can use backslash escapes: