# 容器块 A [container block](#container-blocks) is a block that has other blocks as its contents. There are two basic kinds of container blocks: [block quotes] and [list items]. [Lists] are meta-containers for [list items]. We define the syntax for container blocks recursively. The general form of the definition is: > If X is a sequence of blocks, then the result of > transforming X in such-and-such a way is a container of type Y > with these blocks as its content. So, we explain what counts as a block quote or list item by explaining how these can be *generated* from their contents. This should suffice to define the syntax, although it does not give a recipe for *parsing* these constructions. (A recipe is provided below in the section entitled [A parsing strategy](#appendix-a-parsing-strategy).) ## Block quotes A [block quote marker](#@), optionally preceded by up to three spaces of indentation, consists of (a) the character `>` together with a following space of indentation, or (b) a single character `>` not followed by a space of indentation. The following rules define [block quotes]: 1. **Basic case.** If a string of lines *Ls* constitute a sequence of blocks *Bs*, then the result of prepending a [block quote marker] to the beginning of each line in *Ls* is a [block quote](#block-quotes) containing *Bs*. 2. **Laziness.** If a string of lines *Ls* constitute a [block quote](#block-quotes) with contents *Bs*, then the result of deleting the initial [block quote marker] from one or more lines in which the next character other than a space or tab after the [block quote marker] is [paragraph continuation text] is a block quote with *Bs* as its content. [Paragraph continuation text](#@) is text that will be parsed as part of the content of a paragraph, but does not occur at the beginning of the paragraph. 3. **Consecutiveness.** A document cannot contain two [block quotes] in a row unless there is a [blank line] between them. Nothing else counts as a [block quote](#block-quotes). Here is a simple{panels}: ````````````````````````````````{panels} ```` > # Foo > bar > baz ```` --- ````
```` ```````````````````````````````` The space or tab after the `>` characters can be omitted: ````````````````````````````````{panels} ```` ># Foo >bar > baz ```` --- ````Foo
bar baz
```` ```````````````````````````````` The `>` characters can be preceded by up to three spaces of indentation: ````````````````````````````````{panels} ```` > # Foo > bar > baz ```` --- ````Foo
bar baz
```` ```````````````````````````````` Four spaces of indentation is too many: ````````````````````````````````{panels} ```` > # Foo > bar > baz ```` --- ````Foo
bar baz
> # Foo
> bar
> baz
````
````````````````````````````````
The Laziness clause allows us to omit the `>` before
paragraph continuation text:
````````````````````````````````{panels}
````
> # Foo
> bar
baz
````
---
````
``` ```````````````````````````````` A block quote can contain some lazy and some non-lazy continuation lines: ````````````````````````````````{panels} > bar baz > foo .Foo
bar baz
```````````````````````````````` Laziness only applies to lines that would have been continuations of paragraphs had they been prepended with [block quote markers]. For{panels}, the `> ` cannot be omitted in the second line of ``` markdown > foo > --- ``` without changing the meaning: ````````````````````````````````{panels} > foo --- .bar baz foo
foo
- foo
foo
bar
````````````````````````````````
````````````````````````````````{panels}
> ```
foo
```
.
foo
````````````````````````````````
Note that in the following case, we have a [lazy
continuation line]:
````````````````````````````````{panels}
> foo
- bar
.
```````````````````````````````` To see why, note that in ```markdown > foo > - bar ``` the `- bar` is indented too far to start a list, and can't be an indented code block because indented code blocks cannot interrupt paragraphs, so it is [paragraph continuation text]. A block quote can be empty: ````````````````````````````````{panels} > .foo - bar
```````````````````````````````` ````````````````````````````````{panels} > > > .
```````````````````````````````` A block quote can have initial or final blank lines: ````````````````````````````````{panels} > > foo > .
```````````````````````````````` A blank line always separates block quotes: ````````````````````````````````{panels} > foo > bar .foo
foo
```````````````````````````````` (Most current Markdown implementations, including John Gruber's original `Markdown.pl`, will parse this{panels} as a single block quote with two paragraphs. But it seems better to allow the author to decide whether two block quotes or one are wanted.) Consecutiveness means that if we put these block quotes together, we get a single block quote: ````````````````````````````````{panels} > foo > bar .bar
```````````````````````````````` To get a block quote with two paragraphs, use: ````````````````````````````````{panels} > foo > > bar .foo bar
```````````````````````````````` Block quotes can interrupt paragraphs: ````````````````````````````````{panels} foo > bar .foo
bar
foo
```````````````````````````````` In general, blank lines are not needed before or after block quotes: ````````````````````````````````{panels} > aaa *** > bbb .bar
aaa
```````````````````````````````` However, because of laziness, a blank line is needed between a block quote and a following paragraph: ````````````````````````````````{panels} > bar baz .bbb
```````````````````````````````` ````````````````````````````````{panels} > bar baz .bar baz
bar
baz
```````````````````````````````` ````````````````````````````````{panels} > bar > baz .bar
baz
```````````````````````````````` It is a consequence of the Laziness rule that any number of initial `>`s may be omitted on a continuation line of a nested block quote: ````````````````````````````````{panels} > > > foo bar .```````````````````````````````` ````````````````````````````````{panels} >>> foo > bar >>baz .foo bar
```````````````````````````````` When including an indented code block in a block quote, remember that the [block quote marker] includes both the `>` and a following space of indentation. So *five spaces* are needed after the `>`: ````````````````````````````````{panels} > code > not code .foo bar baz
code
```````````````````````````````` ## List items A [list marker](#@) is a [bullet list marker] or an [ordered list marker]. A [bullet list marker](#@) is a `-`, `+`, or `*` character. An [ordered list marker](#@) is a sequence of 1--9 arabic digits (`0-9`), followed by either a `.` character or a `)` character. (The reason for the length limit is that with 10 digits we start seeing integer overflows in some browsers.) The following rules define [list items]: 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of blocks *Bs* starting with a character other than a space or tab, and *M* is a list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces of indentation, then the result of prepending *M* and the following spaces to the first line of Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a list item with *Bs* as its contents. The type of the list item (bullet or ordered) is determined by the type of its list marker. If the list item is ordered, then it is also assigned a start number, based on the ordered list marker. Exceptions: 1. When the first list item in a [list] interrupts a paragraph---that is, when it starts on a line that would otherwise count as [paragraph continuation text]---then (a) the lines *Ls* must not begin with a blank line, and (b) if the list item is ordered, the start number must be 1. 2. If any line is a [thematic break][thematic breaks] then that line is not a list item. For{panels}, let *Ls* be the lines ````````````````````````````````{panels} A paragraph with two lines. indented code > A block quote. .not code
A paragraph with two lines.
indented code
```````````````````````````````` And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says that the following is an ordered list item with start number 1, and the same contents as *Ls*: ````````````````````````````````{panels} 1. A paragraph with two lines. indented code > A block quote. .A block quote.
A paragraph with two lines.
indented code
A block quote.
two
```````````````````````````````` ````````````````````````````````{panels} - one two .one
two
two
````````````````````````````````
````````````````````````````````{panels}
- one
two
.
one
two
```````````````````````````````` Here `two` occurs in the same column as the list marker `1.`, but is actually contained in the list item, because there is sufficient indentation after the last containing blockquote marker. The converse is also possible. In the following{panels}, the word `two` occurs far to the right of the initial text of the list item, `one`, but it is not considered part of the list item, because it is not indented far enough past the blockquote marker: ````````````````````````````````{panels} >>- one >> > > two .
one
two
```````````````````````````````` Note that at least one space or tab is needed between the list marker and any following content, so these are not list items: ````````````````````````````````{panels} -one 2.two .
- one
two
-one
2.two
```````````````````````````````` A list item may contain blocks that are separated by more than one blank line. ````````````````````````````````{panels} - foo bar .foo
bar
foo
bar
baz
bam
Foo
bar
baz
1234567890. not ok
```````````````````````````````` A start number may begin with 0s: ````````````````````````````````{panels} 0. ok .-1. not ok
```````````````````````````````` 2. **Item starting with indented code.** If a sequence of lines *Ls* constitute a sequence of blocks *Bs* starting with an indented code block, and *M* is a list marker of width *W* followed by one space of indentation, then the result of prepending *M* and the following space to the first line of *Ls*, and indenting subsequent lines of *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents. If a line is empty, then it need not be indented. The type of the list item (bullet or ordered) is determined by the type of its list marker. If the list item is ordered, then it is also assigned a start number, based on the ordered list marker. An indented code block will have to be preceded by four spaces of indentation beyond the edge of the region where text will be included in the list item. In the following case that is 6 spaces: ````````````````````````````````{panels} - foo bar .foo
bar
foo
bar
indented code
paragraph
more code
````````````````````````````````
````````````````````````````````{panels}
1. indented code
paragraph
more code
.
indented code
paragraph
more code
indented code
paragraph
more code
foo
bar
```````````````````````````````` ````````````````````````````````{panels} - foo bar .bar
```````````````````````````````` This is not a significant restriction, because when a block is preceded by up to three spaces of indentation, the indentation can always be removed without a change in interpretation, allowing rule #1 to be applied. So, in the above case: ````````````````````````````````{panels} - foo bar .foo
bar
bar
baz
foo
```````````````````````````````` Here is an empty bullet list item: ````````````````````````````````{panels} - foo - - bar .foo *
foo 1.
```````````````````````````````` 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item according to rule #1, #2, or #3, then the result of preceding each line of *Ls* by up to three spaces of indentation (the same for each line) also constitutes a list item with the same contents and attributes. If a line is empty, then it need not be indented. Indented one space: ````````````````````````````````{panels} 1. A paragraph with two lines. indented code > A block quote. .A paragraph with two lines.
indented code
A block quote.
A paragraph with two lines.
indented code
A block quote.
A paragraph with two lines.
indented code
A block quote.
1. A paragraph
with two lines.
indented code
> A block quote.
````````````````````````````````
5. **Laziness.** If a string of lines *Ls* constitute a [list
item](#list-items) with contents *Bs*, then the result of deleting
some or all of the indentation from one or more lines in which the
next character other than a space or tab after the indentation is
[paragraph continuation text] is a
list item with the same contents and attributes. The unindented
lines are called
[lazy continuation line](#@)s.
Here is an{panels} with [lazy continuation lines]:
````````````````````````````````{panels}
1. A paragraph
with two lines.
indented code
> A block quote.
.
A paragraph with two lines.
indented code
A block quote.
```````````````````````````````` ````````````````````````````````{panels} > 1. > Blockquote > continued here. .
Blockquote continued here.
```````````````````````````````` 6. **That's all.** Nothing that is not counted as a list item by rules #1--5 counts as a [list item](#list-items). The rules for sublists follow from the general rules [above][List items]. A sublist must be indented the same number of spaces of indentation a paragraph would need to be in order to be included in the list item. So, in this case we need two spaces indent: ````````````````````````````````{panels} - foo - bar - baz - boo .
Blockquote continued here.
bar
foo
bar
one
two
``` This is extremely unintuitive. Rather than requiring a fixed indent from the margin, we could require a fixed indent (say, two spaces, or even one space) from the list marker (which may itself be indented). This proposal would remove the last anomaly discussed. Unlike the spec presented above, it would count the following as a list item with a subparagraph, even though the paragraph `bar` is not indented as far as the first paragraph `foo`: ``` markdown 10. foo bar ``` Arguably this text does read like a list item with `bar` as a subparagraph, which may count in favor of the proposal. However, on this proposal indented code would have to be indented six spaces after the list marker. And this would break a lot of existing Markdown, which has the pattern: ``` markdown 1. foo indented code ``` where the code is indented eight spaces. The spec above, by contrast, will parse this text as expected, since the code block's indentation is measured from the beginning of `foo`. The one case that needs special treatment is a list item that *starts* with indented code. How much indentation is required in that case, since we don't have a "first paragraph" to measure from? Rule #2 simply stipulates that in such cases, we require one space indentation from the list marker (and then the normal four spaces for the indented code). This will match the four-space rule in cases where the list marker plus its initial indentation takes four spaces (a common case), but diverge in other cases. ## Lists A [list](#@) is a sequence of one or more list items [of the same type]. The list items may be separated by any number of blank lines. Two list items are [of the same type](#@) if they begin with a [list marker] of the same type. Two list markers are of the same type if (a) they are bullet list markers using the same character (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same delimiter (either `.` or `)`). A list is an [ordered list](#@) if its constituent list items begin with [ordered list markers], and a [bullet list](#@) if its constituent list items begin with [bullet list markers]. The [start number](#@) of an [ordered list] is determined by the list number of its initial list item. The numbers of subsequent list items are disregarded. A list is [loose](#@) if any of its constituent list items are separated by blank lines, or if any of its constituent list items directly contain two block-level elements with a blank line between them. Otherwise a list is [tight](#@). (The difference in HTML output is that paragraphs in a loose list are wrapped in `
one
two
` tags, while paragraphs in a tight list are not.) Changing the bullet or ordered list delimiter starts a new list: ````````````````````````````````{panels} - foo - bar + baz .
Foo
` tags, since the list is "tight"), then ``` markdown I need to buy - new shoes - a coat - a plane ticket ``` by itself should be a paragraph followed by a nested sublist. Since it is well established Markdown practice to allow lists to interrupt paragraphs inside list items, the [principle of uniformity] requires us to allow this outside list items as well. ([reStructuredText](http://docutils.sourceforge.net/rst.html) takes a different approach, requiring blank lines before lists even inside other list items.) In order to solve of unwanted lists in paragraphs with hard-wrapped numerals, we allow only lists starting with `1` to interrupt paragraphs. Thus, ````````````````````````````````{panels} The number of windows in my house is 14. The number of doors is 6. .
The number of windows in my house is 14. The number of doors is 6.
```````````````````````````````` We may still get an unintended result in cases like ````````````````````````````````{panels} The number of windows in my house is 1. The number of doors is 6. .The number of windows in my house is
foo
bar
baz
baz
bim
foo
notcode
foo
code
````````````````````````````````
List items need not be indented to the same level. The following
list items will be treated as items at the same list level,
since none is indented enough to belong to the previous list
item:
````````````````````````````````{panels}
- a
- b
- c
- d
- e
- f
- g
.
a
b
c
a
b
3. c
````````````````````````````````
This is a loose list, because there is a blank line between
two of the list items:
````````````````````````````````{panels}
- a
- b
- c
.
a
b
c
a
c
a
b
c
d
a
b
d
b
b
c
b
b
c
foo
bar
foo
baz
a
d