aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: e60b67e927df9d7654323749fd82e3a90a7efa4e (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.25
  5. date: '2016-03-24'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions used for indicating formatting in email and
  12. usenet posts. It was developed in 2004 by John Gruber, who wrote
  13. the first Markdown-to-HTML converter in Perl, and it soon became
  14. ubiquitous. In the next decade, dozens of implementations were
  15. developed in many languages. Some extended the original
  16. Markdown syntax with conventions for footnotes, tables, and
  17. other document elements. Some allowed Markdown documents to be
  18. rendered in formats other than HTML. Websites like Reddit,
  19. StackOverflow, and GitHub had millions of people using Markdown.
  20. And Markdown started to be used beyond the web, to author books,
  21. articles, slide shows, letters, and lecture notes.
  22. What distinguishes Markdown from many other lightweight markup
  23. syntaxes, which are often easier to write, is its readability.
  24. As Gruber writes:
  25. > The overriding design goal for Markdown's formatting syntax is
  26. > to make it as readable as possible. The idea is that a
  27. > Markdown-formatted document should be publishable as-is, as
  28. > plain text, without looking like it's been marked up with tags
  29. > or formatting instructions.
  30. > (<http://daringfireball.net/projects/markdown/>)
  31. The point can be illustrated by comparing a sample of
  32. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  33. an equivalent sample of Markdown. Here is a sample of
  34. AsciiDoc from the AsciiDoc manual:
  35. ```
  36. 1. List item one.
  37. +
  38. List item one continued with a second paragraph followed by an
  39. Indented block.
  40. +
  41. .................
  42. $ ls *.sh
  43. $ mv *.sh ~/tmp
  44. .................
  45. +
  46. List item continued with a third paragraph.
  47. 2. List item two continued with an open block.
  48. +
  49. --
  50. This paragraph is part of the preceding list item.
  51. a. This list is nested and does not require explicit item
  52. continuation.
  53. +
  54. This paragraph is part of the preceding list item.
  55. b. List item b.
  56. This paragraph belongs to item two of the outer list.
  57. --
  58. ```
  59. And here is the equivalent in Markdown:
  60. ```
  61. 1. List item one.
  62. List item one continued with a second paragraph followed by an
  63. Indented block.
  64. $ ls *.sh
  65. $ mv *.sh ~/tmp
  66. List item continued with a third paragraph.
  67. 2. List item two continued with an open block.
  68. This paragraph is part of the preceding list item.
  69. 1. This list is nested and does not require explicit item continuation.
  70. This paragraph is part of the preceding list item.
  71. 2. List item b.
  72. This paragraph belongs to item two of the outer list.
  73. ```
  74. The AsciiDoc version is, arguably, easier to write. You don't need
  75. to worry about indentation. But the Markdown version is much easier
  76. to read. The nesting of list items is apparent to the eye in the
  77. source, not just in the processed document.
  78. ## Why is a spec needed?
  79. John Gruber's [canonical description of Markdown's
  80. syntax](http://daringfireball.net/projects/markdown/syntax)
  81. does not specify the syntax unambiguously. Here are some examples of
  82. questions it does not answer:
  83. 1. How much indentation is needed for a sublist? The spec says that
  84. continuation paragraphs need to be indented four spaces, but is
  85. not fully explicit about sublists. It is natural to think that
  86. they, too, must be indented four spaces, but `Markdown.pl` does
  87. not require that. This is hardly a "corner case," and divergences
  88. between implementations on this issue often lead to surprises for
  89. users in real documents. (See [this comment by John
  90. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  91. 2. Is a blank line needed before a block quote or heading?
  92. Most implementations do not require the blank line. However,
  93. this can lead to unexpected results in hard-wrapped text, and
  94. also to ambiguities in parsing (note that some implementations
  95. put the heading inside the blockquote, while others do not).
  96. (John Gruber has also spoken [in favor of requiring the blank
  97. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  98. 3. Is a blank line needed before an indented code block?
  99. (`Markdown.pl` requires it, but this is not mentioned in the
  100. documentation, and some implementations do not require it.)
  101. ``` markdown
  102. paragraph
  103. code?
  104. ```
  105. 4. What is the exact rule for determining when list items get
  106. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  107. "tight"? What should we do with a list like this?
  108. ``` markdown
  109. 1. one
  110. 2. two
  111. 3. three
  112. ```
  113. Or this?
  114. ``` markdown
  115. 1. one
  116. - a
  117. - b
  118. 2. two
  119. ```
  120. (There are some relevant comments by John Gruber
  121. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  122. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  123. ``` markdown
  124. 8. item 1
  125. 9. item 2
  126. 10. item 2a
  127. ```
  128. 6. Is this one list with a thematic break in its second item,
  129. or two lists separated by a thematic break?
  130. ``` markdown
  131. * a
  132. * * * * *
  133. * b
  134. ```
  135. 7. When list markers change from numbers to bullets, do we have
  136. two lists or one? (The Markdown syntax description suggests two,
  137. but the perl scripts and many other implementations produce one.)
  138. ``` markdown
  139. 1. fee
  140. 2. fie
  141. - foe
  142. - fum
  143. ```
  144. 8. What are the precedence rules for the markers of inline structure?
  145. For example, is the following a valid link, or does the code span
  146. take precedence ?
  147. ``` markdown
  148. [a backtick (`)](/url) and [another backtick (`)](/url).
  149. ```
  150. 9. What are the precedence rules for markers of emphasis and strong
  151. emphasis? For example, how should the following be parsed?
  152. ``` markdown
  153. *foo *bar* baz*
  154. ```
  155. 10. What are the precedence rules between block-level and inline-level
  156. structure? For example, how should the following be parsed?
  157. ``` markdown
  158. - `a long code span can contain a hyphen like this
  159. - and it can screw things up`
  160. ```
  161. 11. Can list items include section headings? (`Markdown.pl` does not
  162. allow this, but does allow blockquotes to include headings.)
  163. ``` markdown
  164. - # Heading
  165. ```
  166. 12. Can list items be empty?
  167. ``` markdown
  168. * a
  169. *
  170. * b
  171. ```
  172. 13. Can link references be defined inside block quotes or list items?
  173. ``` markdown
  174. > Blockquote [foo].
  175. >
  176. > [foo]: /url
  177. ```
  178. 14. If there are multiple definitions for the same reference, which takes
  179. precedence?
  180. ``` markdown
  181. [foo]: /url1
  182. [foo]: /url2
  183. [foo][]
  184. ```
  185. In the absence of a spec, early implementers consulted `Markdown.pl`
  186. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  187. gave manifestly bad results in many cases, so it was not a
  188. satisfactory replacement for a spec.
  189. Because there is no unambiguous spec, implementations have diverged
  190. considerably. As a result, users are often surprised to find that
  191. a document that renders one way on one system (say, a github wiki)
  192. renders differently on another (say, converting to docbook using
  193. pandoc). To make matters worse, because nothing in Markdown counts
  194. as a "syntax error," the divergence often isn't discovered right away.
  195. ## About this document
  196. This document attempts to specify Markdown syntax unambiguously.
  197. It contains many examples with side-by-side Markdown and
  198. HTML. These are intended to double as conformance tests. An
  199. accompanying script `spec_tests.py` can be used to run the tests
  200. against any Markdown program:
  201. python test/spec_tests.py --spec spec.txt --program PROGRAM
  202. Since this document describes how Markdown is to be parsed into
  203. an abstract syntax tree, it would have made sense to use an abstract
  204. representation of the syntax tree instead of HTML. But HTML is capable
  205. of representing the structural distinctions we need to make, and the
  206. choice of HTML for the tests makes it possible to run the tests against
  207. an implementation without writing an abstract syntax tree renderer.
  208. This document is generated from a text file, `spec.txt`, written
  209. in Markdown with a small extension for the side-by-side tests.
  210. The script `tools/makespec.py` can be used to convert `spec.txt` into
  211. HTML or CommonMark (which can then be converted into other formats).
  212. In the examples, the `→` character is used to represent tabs.
  213. # Preliminaries
  214. ## Characters and lines
  215. Any sequence of [characters] is a valid CommonMark
  216. document.
  217. A [character](@) is a Unicode code point. Although some
  218. code points (for example, combining accents) do not correspond to
  219. characters in an intuitive sense, all code points count as characters
  220. for purposes of this spec.
  221. This spec does not specify an encoding; it thinks of lines as composed
  222. of [characters] rather than bytes. A conforming parser may be limited
  223. to a certain encoding.
  224. A [line](@) is a sequence of zero or more [characters]
  225. other than newline (`U+000A`) or carriage return (`U+000D`),
  226. followed by a [line ending] or by the end of file.
  227. A [line ending](@) is a newline (`U+000A`), a carriage return
  228. (`U+000D`) not followed by a newline, or a carriage return and a
  229. following newline.
  230. A line containing no characters, or a line containing only spaces
  231. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  232. The following definitions of character classes will be used in this spec:
  233. A [whitespace character](@) is a space
  234. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  235. form feed (`U+000C`), or carriage return (`U+000D`).
  236. [Whitespace](@) is a sequence of one or more [whitespace
  237. characters].
  238. A [Unicode whitespace character](@) is
  239. any code point in the Unicode `Zs` class, or a tab (`U+0009`),
  240. carriage return (`U+000D`), newline (`U+000A`), or form feed
  241. (`U+000C`).
  242. [Unicode whitespace](@) is a sequence of one
  243. or more [Unicode whitespace characters].
  244. A [space](@) is `U+0020`.
  245. A [non-whitespace character](@) is any character
  246. that is not a [whitespace character].
  247. An [ASCII punctuation character](@)
  248. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  249. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  250. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  251. A [punctuation character](@) is an [ASCII
  252. punctuation character] or anything in
  253. the Unicode classes `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  254. ## Tabs
  255. Tabs in lines are not expanded to [spaces]. However,
  256. in contexts where indentation is significant for the
  257. document's structure, tabs behave as if they were replaced
  258. by spaces with a tab stop of 4 characters.
  259. ```````````````````````````````` example
  260. →foo→baz→→bim
  261. .
  262. <pre><code>foo→baz→→bim
  263. </code></pre>
  264. ````````````````````````````````
  265. ```````````````````````````````` example
  266. →foo→baz→→bim
  267. .
  268. <pre><code>foo→baz→→bim
  269. </code></pre>
  270. ````````````````````````````````
  271. ```````````````````````````````` example
  272. a→a
  273. ὐ→a
  274. .
  275. <pre><code>a→a
  276. ὐ→a
  277. </code></pre>
  278. ````````````````````````````````
  279. ```````````````````````````````` example
  280. - foo
  281. →bar
  282. .
  283. <ul>
  284. <li>
  285. <p>foo</p>
  286. <p>bar</p>
  287. </li>
  288. </ul>
  289. ````````````````````````````````
  290. ```````````````````````````````` example
  291. - foo
  292. →→bar
  293. .
  294. <ul>
  295. <li>
  296. <p>foo</p>
  297. <pre><code> bar
  298. </code></pre>
  299. </li>
  300. </ul>
  301. ````````````````````````````````
  302. ```````````````````````````````` example
  303. >→→foo
  304. .
  305. <blockquote>
  306. <pre><code> foo
  307. </code></pre>
  308. </blockquote>
  309. ````````````````````````````````
  310. ```````````````````````````````` example
  311. -→→foo
  312. .
  313. <ul>
  314. <li>
  315. <pre><code> foo
  316. </code></pre>
  317. </li>
  318. </ul>
  319. ````````````````````````````````
  320. ```````````````````````````````` example
  321. foo
  322. →bar
  323. .
  324. <pre><code>foo
  325. bar
  326. </code></pre>
  327. ````````````````````````````````
  328. ```````````````````````````````` example
  329. - foo
  330. - bar
  331. → - baz
  332. .
  333. <ul>
  334. <li>foo
  335. <ul>
  336. <li>bar
  337. <ul>
  338. <li>baz</li>
  339. </ul>
  340. </li>
  341. </ul>
  342. </li>
  343. </ul>
  344. ````````````````````````````````
  345. ## Insecure characters
  346. For security reasons, the Unicode character `U+0000` must be replaced
  347. with the REPLACEMENT CHARACTER (`U+FFFD`).
  348. # Blocks and inlines
  349. We can think of a document as a sequence of
  350. [blocks](@)---structural elements like paragraphs, block
  351. quotations, lists, headings, rules, and code blocks. Some blocks (like
  352. block quotes and list items) contain other blocks; others (like
  353. headings and paragraphs) contain [inline](@) content---text,
  354. links, emphasized text, images, code, and so on.
  355. ## Precedence
  356. Indicators of block structure always take precedence over indicators
  357. of inline structure. So, for example, the following is a list with
  358. two items, not a list with one item containing a code span:
  359. ```````````````````````````````` example
  360. - `one
  361. - two`
  362. .
  363. <ul>
  364. <li>`one</li>
  365. <li>two`</li>
  366. </ul>
  367. ````````````````````````````````
  368. This means that parsing can proceed in two steps: first, the block
  369. structure of the document can be discerned; second, text lines inside
  370. paragraphs, headings, and other block constructs can be parsed for inline
  371. structure. The second step requires information about link reference
  372. definitions that will be available only at the end of the first
  373. step. Note that the first step requires processing lines in sequence,
  374. but the second can be parallelized, since the inline parsing of
  375. one block element does not affect the inline parsing of any other.
  376. ## Container blocks and leaf blocks
  377. We can divide blocks into two types:
  378. [container block](@)s,
  379. which can contain other blocks, and [leaf block](@)s,
  380. which cannot.
  381. # Leaf blocks
  382. This section describes the different kinds of leaf block that make up a
  383. Markdown document.
  384. ## Thematic breaks
  385. A line consisting of 0-3 spaces of indentation, followed by a sequence
  386. of three or more matching `-`, `_`, or `*` characters, each followed
  387. optionally by any number of spaces, forms a
  388. [thematic break](@).
  389. ```````````````````````````````` example
  390. ***
  391. ---
  392. ___
  393. .
  394. <hr />
  395. <hr />
  396. <hr />
  397. ````````````````````````````````
  398. Wrong characters:
  399. ```````````````````````````````` example
  400. +++
  401. .
  402. <p>+++</p>
  403. ````````````````````````````````
  404. ```````````````````````````````` example
  405. ===
  406. .
  407. <p>===</p>
  408. ````````````````````````````````
  409. Not enough characters:
  410. ```````````````````````````````` example
  411. --
  412. **
  413. __
  414. .
  415. <p>--
  416. **
  417. __</p>
  418. ````````````````````````````````
  419. One to three spaces indent are allowed:
  420. ```````````````````````````````` example
  421. ***
  422. ***
  423. ***
  424. .
  425. <hr />
  426. <hr />
  427. <hr />
  428. ````````````````````````````````
  429. Four spaces is too many:
  430. ```````````````````````````````` example
  431. ***
  432. .
  433. <pre><code>***
  434. </code></pre>
  435. ````````````````````````````````
  436. ```````````````````````````````` example
  437. Foo
  438. ***
  439. .
  440. <p>Foo
  441. ***</p>
  442. ````````````````````````````````
  443. More than three characters may be used:
  444. ```````````````````````````````` example
  445. _____________________________________
  446. .
  447. <hr />
  448. ````````````````````````````````
  449. Spaces are allowed between the characters:
  450. ```````````````````````````````` example
  451. - - -
  452. .
  453. <hr />
  454. ````````````````````````````````
  455. ```````````````````````````````` example
  456. ** * ** * ** * **
  457. .
  458. <hr />
  459. ````````````````````````````````
  460. ```````````````````````````````` example
  461. - - - -
  462. .
  463. <hr />
  464. ````````````````````````````````
  465. Spaces are allowed at the end:
  466. ```````````````````````````````` example
  467. - - - -
  468. .
  469. <hr />
  470. ````````````````````````````````
  471. However, no other characters may occur in the line:
  472. ```````````````````````````````` example
  473. _ _ _ _ a
  474. a------
  475. ---a---
  476. .
  477. <p>_ _ _ _ a</p>
  478. <p>a------</p>
  479. <p>---a---</p>
  480. ````````````````````````````````
  481. It is required that all of the [non-whitespace characters] be the same.
  482. So, this is not a thematic break:
  483. ```````````````````````````````` example
  484. *-*
  485. .
  486. <p><em>-</em></p>
  487. ````````````````````````````````
  488. Thematic breaks do not need blank lines before or after:
  489. ```````````````````````````````` example
  490. - foo
  491. ***
  492. - bar
  493. .
  494. <ul>
  495. <li>foo</li>
  496. </ul>
  497. <hr />
  498. <ul>
  499. <li>bar</li>
  500. </ul>
  501. ````````````````````````````````
  502. Thematic breaks can interrupt a paragraph:
  503. ```````````````````````````````` example
  504. Foo
  505. ***
  506. bar
  507. .
  508. <p>Foo</p>
  509. <hr />
  510. <p>bar</p>
  511. ````````````````````````````````
  512. If a line of dashes that meets the above conditions for being a
  513. thematic break could also be interpreted as the underline of a [setext
  514. heading], the interpretation as a
  515. [setext heading] takes precedence. Thus, for example,
  516. this is a setext heading, not a paragraph followed by a thematic break:
  517. ```````````````````````````````` example
  518. Foo
  519. ---
  520. bar
  521. .
  522. <h2>Foo</h2>
  523. <p>bar</p>
  524. ````````````````````````````````
  525. When both a thematic break and a list item are possible
  526. interpretations of a line, the thematic break takes precedence:
  527. ```````````````````````````````` example
  528. * Foo
  529. * * *
  530. * Bar
  531. .
  532. <ul>
  533. <li>Foo</li>
  534. </ul>
  535. <hr />
  536. <ul>
  537. <li>Bar</li>
  538. </ul>
  539. ````````````````````````````````
  540. If you want a thematic break in a list item, use a different bullet:
  541. ```````````````````````````````` example
  542. - Foo
  543. - * * *
  544. .
  545. <ul>
  546. <li>Foo</li>
  547. <li>
  548. <hr />
  549. </li>
  550. </ul>
  551. ````````````````````````````````
  552. ## ATX headings
  553. An [ATX heading](@)
  554. consists of a string of characters, parsed as inline content, between an
  555. opening sequence of 1--6 unescaped `#` characters and an optional
  556. closing sequence of any number of unescaped `#` characters.
  557. The opening sequence of `#` characters must be followed by a
  558. [space] or by the end of line. The optional closing sequence of `#`s must be
  559. preceded by a [space] and may be followed by spaces only. The opening
  560. `#` character may be indented 0-3 spaces. The raw contents of the
  561. heading are stripped of leading and trailing spaces before being parsed
  562. as inline content. The heading level is equal to the number of `#`
  563. characters in the opening sequence.
  564. Simple headings:
  565. ```````````````````````````````` example
  566. # foo
  567. ## foo
  568. ### foo
  569. #### foo
  570. ##### foo
  571. ###### foo
  572. .
  573. <h1>foo</h1>
  574. <h2>foo</h2>
  575. <h3>foo</h3>
  576. <h4>foo</h4>
  577. <h5>foo</h5>
  578. <h6>foo</h6>
  579. ````````````````````````````````
  580. More than six `#` characters is not a heading:
  581. ```````````````````````````````` example
  582. ####### foo
  583. .
  584. <p>####### foo</p>
  585. ````````````````````````````````
  586. At least one space is required between the `#` characters and the
  587. heading's contents, unless the heading is empty. Note that many
  588. implementations currently do not require the space. However, the
  589. space was required by the
  590. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  591. and it helps prevent things like the following from being parsed as
  592. headings:
  593. ```````````````````````````````` example
  594. #5 bolt
  595. #hashtag
  596. .
  597. <p>#5 bolt</p>
  598. <p>#hashtag</p>
  599. ````````````````````````````````
  600. This is not a heading, because the first `#` is escaped:
  601. ```````````````````````````````` example
  602. \## foo
  603. .
  604. <p>## foo</p>
  605. ````````````````````````````````
  606. Contents are parsed as inlines:
  607. ```````````````````````````````` example
  608. # foo *bar* \*baz\*
  609. .
  610. <h1>foo <em>bar</em> *baz*</h1>
  611. ````````````````````````````````
  612. Leading and trailing blanks are ignored in parsing inline content:
  613. ```````````````````````````````` example
  614. # foo
  615. .
  616. <h1>foo</h1>
  617. ````````````````````````````````
  618. One to three spaces indentation are allowed:
  619. ```````````````````````````````` example
  620. ### foo
  621. ## foo
  622. # foo
  623. .
  624. <h3>foo</h3>
  625. <h2>foo</h2>
  626. <h1>foo</h1>
  627. ````````````````````````````````
  628. Four spaces are too much:
  629. ```````````````````````````````` example
  630. # foo
  631. .
  632. <pre><code># foo
  633. </code></pre>
  634. ````````````````````````````````
  635. ```````````````````````````````` example
  636. foo
  637. # bar
  638. .
  639. <p>foo
  640. # bar</p>
  641. ````````````````````````````````
  642. A closing sequence of `#` characters is optional:
  643. ```````````````````````````````` example
  644. ## foo ##
  645. ### bar ###
  646. .
  647. <h2>foo</h2>
  648. <h3>bar</h3>
  649. ````````````````````````````````
  650. It need not be the same length as the opening sequence:
  651. ```````````````````````````````` example
  652. # foo ##################################
  653. ##### foo ##
  654. .
  655. <h1>foo</h1>
  656. <h5>foo</h5>
  657. ````````````````````````````````
  658. Spaces are allowed after the closing sequence:
  659. ```````````````````````````````` example
  660. ### foo ###
  661. .
  662. <h3>foo</h3>
  663. ````````````````````````````````
  664. A sequence of `#` characters with anything but [spaces] following it
  665. is not a closing sequence, but counts as part of the contents of the
  666. heading:
  667. ```````````````````````````````` example
  668. ### foo ### b
  669. .
  670. <h3>foo ### b</h3>
  671. ````````````````````````````````
  672. The closing sequence must be preceded by a space:
  673. ```````````````````````````````` example
  674. # foo#
  675. .
  676. <h1>foo#</h1>
  677. ````````````````````````````````
  678. Backslash-escaped `#` characters do not count as part
  679. of the closing sequence:
  680. ```````````````````````````````` example
  681. ### foo \###
  682. ## foo #\##
  683. # foo \#
  684. .
  685. <h3>foo ###</h3>
  686. <h2>foo ###</h2>
  687. <h1>foo #</h1>
  688. ````````````````````````````````
  689. ATX headings need not be separated from surrounding content by blank
  690. lines, and they can interrupt paragraphs:
  691. ```````````````````````````````` example
  692. ****
  693. ## foo
  694. ****
  695. .
  696. <hr />
  697. <h2>foo</h2>
  698. <hr />
  699. ````````````````````````````````
  700. ```````````````````````````````` example
  701. Foo bar
  702. # baz
  703. Bar foo
  704. .
  705. <p>Foo bar</p>
  706. <h1>baz</h1>
  707. <p>Bar foo</p>
  708. ````````````````````````````````
  709. ATX headings can be empty:
  710. ```````````````````````````````` example
  711. ##
  712. #
  713. ### ###
  714. .
  715. <h2></h2>
  716. <h1></h1>
  717. <h3></h3>
  718. ````````````````````````````````
  719. ## Setext headings
  720. A [setext heading](@) consists of one or more
  721. lines of text, each containing at least one [non-whitespace
  722. character], with no more than 3 spaces indentation, followed by
  723. a [setext heading underline]. The lines of text must be such
  724. that, were they not followed by the setext heading underline,
  725. they would be interpreted as a paragraph: they cannot be
  726. interpretable as a [code fence], [ATX heading][ATX headings],
  727. [block quote][block quotes], [thematic break][thematic breaks],
  728. [list item][list items], or [HTML block][HTML blocks].
  729. A [setext heading underline](@) is a sequence of
  730. `=` characters or a sequence of `-` characters, with no more than 3
  731. spaces indentation and any number of trailing spaces. If a line
  732. containing a single `-` can be interpreted as an
  733. empty [list items], it should be interpreted this way
  734. and not as a [setext heading underline].
  735. The heading is a level 1 heading if `=` characters are used in
  736. the [setext heading underline], and a level 2 heading if `-`
  737. characters are used. The contents of the heading are the result
  738. of parsing the preceding lines of text as CommonMark inline
  739. content.
  740. In general, a setext heading need not be preceded or followed by a
  741. blank line. However, it cannot interrupt a paragraph, so when a
  742. setext heading comes after a paragraph, a blank line is needed between
  743. them.
  744. Simple examples:
  745. ```````````````````````````````` example
  746. Foo *bar*
  747. =========
  748. Foo *bar*
  749. ---------
  750. .
  751. <h1>Foo <em>bar</em></h1>
  752. <h2>Foo <em>bar</em></h2>
  753. ````````````````````````````````
  754. The content of the header may span more than one line:
  755. ```````````````````````````````` example
  756. Foo *bar
  757. baz*
  758. ====
  759. .
  760. <h1>Foo <em>bar
  761. baz</em></h1>
  762. ````````````````````````````````
  763. The underlining can be any length:
  764. ```````````````````````````````` example
  765. Foo
  766. -------------------------
  767. Foo
  768. =
  769. .
  770. <h2>Foo</h2>
  771. <h1>Foo</h1>
  772. ````````````````````````````````
  773. The heading content can be indented up to three spaces, and need
  774. not line up with the underlining:
  775. ```````````````````````````````` example
  776. Foo
  777. ---
  778. Foo
  779. -----
  780. Foo
  781. ===
  782. .
  783. <h2>Foo</h2>
  784. <h2>Foo</h2>
  785. <h1>Foo</h1>
  786. ````````````````````````````````
  787. Four spaces indent is too much:
  788. ```````````````````````````````` example
  789. Foo
  790. ---
  791. Foo
  792. ---
  793. .
  794. <pre><code>Foo
  795. ---
  796. Foo
  797. </code></pre>
  798. <hr />
  799. ````````````````````````````````
  800. The setext heading underline can be indented up to three spaces, and
  801. may have trailing spaces:
  802. ```````````````````````````````` example
  803. Foo
  804. ----
  805. .
  806. <h2>Foo</h2>
  807. ````````````````````````````````
  808. Four spaces is too much:
  809. ```````````````````````````````` example
  810. Foo
  811. ---
  812. .
  813. <p>Foo
  814. ---</p>
  815. ````````````````````````````````
  816. The setext heading underline cannot contain internal spaces:
  817. ```````````````````````````````` example
  818. Foo
  819. = =
  820. Foo
  821. --- -
  822. .
  823. <p>Foo
  824. = =</p>
  825. <p>Foo</p>
  826. <hr />
  827. ````````````````````````````````
  828. Trailing spaces in the content line do not cause a line break:
  829. ```````````````````````````````` example
  830. Foo
  831. -----
  832. .
  833. <h2>Foo</h2>
  834. ````````````````````````````````
  835. Nor does a backslash at the end:
  836. ```````````````````````````````` example
  837. Foo\
  838. ----
  839. .
  840. <h2>Foo\</h2>
  841. ````````````````````````````````
  842. Since indicators of block structure take precedence over
  843. indicators of inline structure, the following are setext headings:
  844. ```````````````````````````````` example
  845. `Foo
  846. ----
  847. `
  848. <a title="a lot
  849. ---
  850. of dashes"/>
  851. .
  852. <h2>`Foo</h2>
  853. <p>`</p>
  854. <h2>&lt;a title=&quot;a lot</h2>
  855. <p>of dashes&quot;/&gt;</p>
  856. ````````````````````````````````
  857. The setext heading underline cannot be a [lazy continuation
  858. line] in a list item or block quote:
  859. ```````````````````````````````` example
  860. > Foo
  861. ---
  862. .
  863. <blockquote>
  864. <p>Foo</p>
  865. </blockquote>
  866. <hr />
  867. ````````````````````````````````
  868. ```````````````````````````````` example
  869. > foo
  870. bar
  871. ===
  872. .
  873. <blockquote>
  874. <p>foo
  875. bar
  876. ===</p>
  877. </blockquote>
  878. ````````````````````````````````
  879. ```````````````````````````````` example
  880. - Foo
  881. ---
  882. .
  883. <ul>
  884. <li>Foo</li>
  885. </ul>
  886. <hr />
  887. ````````````````````````````````
  888. A blank line is needed between a paragraph and a following
  889. setext heading, since otherwise the paragraph becomes part
  890. of the heading's content:
  891. ```````````````````````````````` example
  892. Foo
  893. Bar
  894. ---
  895. .
  896. <h2>Foo
  897. Bar</h2>
  898. ````````````````````````````````
  899. But in general a blank line is not required before or after
  900. setext headings:
  901. ```````````````````````````````` example
  902. ---
  903. Foo
  904. ---
  905. Bar
  906. ---
  907. Baz
  908. .
  909. <hr />
  910. <h2>Foo</h2>
  911. <h2>Bar</h2>
  912. <p>Baz</p>
  913. ````````````````````````````````
  914. Setext headings cannot be empty:
  915. ```````````````````````````````` example
  916. ====
  917. .
  918. <p>====</p>
  919. ````````````````````````````````
  920. Setext heading text lines must not be interpretable as block
  921. constructs other than paragraphs. So, the line of dashes
  922. in these examples gets interpreted as a thematic break:
  923. ```````````````````````````````` example
  924. ---
  925. ---
  926. .
  927. <hr />
  928. <hr />
  929. ````````````````````````````````
  930. ```````````````````````````````` example
  931. - foo
  932. -----
  933. .
  934. <ul>
  935. <li>foo</li>
  936. </ul>
  937. <hr />
  938. ````````````````````````````````
  939. ```````````````````````````````` example
  940. foo
  941. ---
  942. .
  943. <pre><code>foo
  944. </code></pre>
  945. <hr />
  946. ````````````````````````````````
  947. ```````````````````````````````` example
  948. > foo
  949. -----
  950. .
  951. <blockquote>
  952. <p>foo</p>
  953. </blockquote>
  954. <hr />
  955. ````````````````````````````````
  956. If you want a heading with `> foo` as its literal text, you can
  957. use backslash escapes:
  958. ```````````````````````````````` example
  959. \> foo
  960. ------
  961. .
  962. <h2>&gt; foo</h2>
  963. ````````````````````````````````
  964. **Compatibility note:** Most existing Markdown implementations
  965. do not allow the text of setext headings to span multiple lines.
  966. But there is no consensus about how to interpret
  967. ``` markdown
  968. Foo
  969. bar
  970. ---
  971. baz
  972. ```
  973. One can find four different interpretations:
  974. 1. paragraph "Foo", heading "bar", paragraph "baz"
  975. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  976. 3. paragraph "Foo bar --- baz"
  977. 4. heading "Foo bar", paragraph "baz"
  978. We find interpretation 4 most natural, and interpretation 4
  979. increases the expressive power of CommonMark, by allowing
  980. multiline headings. Authors who want interpretation 1 can
  981. put a blank line after the first paragraph:
  982. ```````````````````````````````` example
  983. Foo
  984. bar
  985. ---
  986. baz
  987. .
  988. <p>Foo</p>
  989. <h2>bar</h2>
  990. <p>baz</p>
  991. ````````````````````````````````
  992. Authors who want interpretation 2 can put blank lines around
  993. the thematic break,
  994. ```````````````````````````````` example
  995. Foo
  996. bar
  997. ---
  998. baz
  999. .
  1000. <p>Foo
  1001. bar</p>
  1002. <hr />
  1003. <p>baz</p>
  1004. ````````````````````````````````
  1005. or use a thematic break that cannot count as a [setext heading
  1006. underline], such as
  1007. ```````````````````````````````` example
  1008. Foo
  1009. bar
  1010. * * *
  1011. baz
  1012. .
  1013. <p>Foo
  1014. bar</p>
  1015. <hr />
  1016. <p>baz</p>
  1017. ````````````````````````````````
  1018. Authors who want interpretation 3 can use backslash escapes:
  1019. ```````````````````````````````` example
  1020. Foo
  1021. bar
  1022. \---
  1023. baz
  1024. .
  1025. <p>Foo
  1026. bar
  1027. ---
  1028. baz</p>
  1029. ````````````````````````````````
  1030. ## Indented code blocks
  1031. An [indented code block](@) is composed of one or more
  1032. [indented chunks] separated by blank lines.
  1033. An [indented chunk](@) is a sequence of non-blank lines,
  1034. each indented four or more spaces. The contents of the code block are
  1035. the literal contents of the lines, including trailing
  1036. [line endings], minus four spaces of indentation.
  1037. An indented code block has no [info string].
  1038. An indented code block cannot interrupt a paragraph, so there must be
  1039. a blank line between a paragraph and a following indented code block.
  1040. (A blank line is not needed, however, between a code block and a following
  1041. paragraph.)
  1042. ```````````````````````````````` example
  1043. a simple
  1044. indented code block
  1045. .
  1046. <pre><code>a simple
  1047. indented code block
  1048. </code></pre>
  1049. ````````````````````````````````
  1050. If there is any ambiguity between an interpretation of indentation
  1051. as a code block and as indicating that material belongs to a [list
  1052. item][list items], the list item interpretation takes precedence:
  1053. ```````````````````````````````` example
  1054. - foo
  1055. bar
  1056. .
  1057. <ul>
  1058. <li>
  1059. <p>foo</p>
  1060. <p>bar</p>
  1061. </li>
  1062. </ul>
  1063. ````````````````````````````````
  1064. ```````````````````````````````` example
  1065. 1. foo
  1066. - bar
  1067. .
  1068. <ol>
  1069. <li>
  1070. <p>foo</p>
  1071. <ul>
  1072. <li>bar</li>
  1073. </ul>
  1074. </li>
  1075. </ol>
  1076. ````````````````````````````````
  1077. The contents of a code block are literal text, and do not get parsed
  1078. as Markdown:
  1079. ```````````````````````````````` example
  1080. <a/>
  1081. *hi*
  1082. - one
  1083. .
  1084. <pre><code>&lt;a/&gt;
  1085. *hi*
  1086. - one
  1087. </code></pre>
  1088. ````````````````````````````````
  1089. Here we have three chunks separated by blank lines:
  1090. ```````````````````````````````` example
  1091. chunk1
  1092. chunk2
  1093. chunk3
  1094. .
  1095. <pre><code>chunk1
  1096. chunk2
  1097. chunk3
  1098. </code></pre>
  1099. ````````````````````````````````
  1100. Any initial spaces beyond four will be included in the content, even
  1101. in interior blank lines:
  1102. ```````````````````````````````` example
  1103. chunk1
  1104. chunk2
  1105. .
  1106. <pre><code>chunk1
  1107. chunk2
  1108. </code></pre>
  1109. ````````````````````````````````
  1110. An indented code block cannot interrupt a paragraph. (This
  1111. allows hanging indents and the like.)
  1112. ```````````````````````````````` example
  1113. Foo
  1114. bar
  1115. .
  1116. <p>Foo
  1117. bar</p>
  1118. ````````````````````````````````
  1119. However, any non-blank line with fewer than four leading spaces ends
  1120. the code block immediately. So a paragraph may occur immediately
  1121. after indented code:
  1122. ```````````````````````````````` example
  1123. foo
  1124. bar
  1125. .
  1126. <pre><code>foo
  1127. </code></pre>
  1128. <p>bar</p>
  1129. ````````````````````````````````
  1130. And indented code can occur immediately before and after other kinds of
  1131. blocks:
  1132. ```````````````````````````````` example
  1133. # Heading
  1134. foo
  1135. Heading
  1136. ------
  1137. foo
  1138. ----
  1139. .
  1140. <h1>Heading</h1>
  1141. <pre><code>foo
  1142. </code></pre>
  1143. <h2>Heading</h2>
  1144. <pre><code>foo
  1145. </code></pre>
  1146. <hr />
  1147. ````````````````````````````````
  1148. The first line can be indented more than four spaces:
  1149. ```````````````````````````````` example
  1150. foo
  1151. bar
  1152. .
  1153. <pre><code> foo
  1154. bar
  1155. </code></pre>
  1156. ````````````````````````````````
  1157. Blank lines preceding or following an indented code block
  1158. are not included in it:
  1159. ```````````````````````````````` example
  1160. foo
  1161. .
  1162. <pre><code>foo
  1163. </code></pre>
  1164. ````````````````````````````````
  1165. Trailing spaces are included in the code block's content:
  1166. ```````````````````````````````` example
  1167. foo
  1168. .
  1169. <pre><code>foo
  1170. </code></pre>
  1171. ````````````````````````````````
  1172. ## Fenced code blocks
  1173. A [code fence](@) is a sequence
  1174. of at least three consecutive backtick characters (`` ` ``) or
  1175. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1176. A [fenced code block](@)
  1177. begins with a code fence, indented no more than three spaces.
  1178. The line with the opening code fence may optionally contain some text
  1179. following the code fence; this is trimmed of leading and trailing
  1180. spaces and called the [info string](@).
  1181. The [info string] may not contain any backtick
  1182. characters. (The reason for this restriction is that otherwise
  1183. some inline code would be incorrectly interpreted as the
  1184. beginning of a fenced code block.)
  1185. The content of the code block consists of all subsequent lines, until
  1186. a closing [code fence] of the same type as the code block
  1187. began with (backticks or tildes), and with at least as many backticks
  1188. or tildes as the opening code fence. If the leading code fence is
  1189. indented N spaces, then up to N spaces of indentation are removed from
  1190. each line of the content (if present). (If a content line is not
  1191. indented, it is preserved unchanged. If it is indented less than N
  1192. spaces, all of the indentation is removed.)
  1193. The closing code fence may be indented up to three spaces, and may be
  1194. followed only by spaces, which are ignored. If the end of the
  1195. containing block (or document) is reached and no closing code fence
  1196. has been found, the code block contains all of the lines after the
  1197. opening code fence until the end of the containing block (or
  1198. document). (An alternative spec would require backtracking in the
  1199. event that a closing code fence is not found. But this makes parsing
  1200. much less efficient, and there seems to be no real down side to the
  1201. behavior described here.)
  1202. A fenced code block may interrupt a paragraph, and does not require
  1203. a blank line either before or after.
  1204. The content of a code fence is treated as literal text, not parsed
  1205. as inlines. The first word of the [info string] is typically used to
  1206. specify the language of the code sample, and rendered in the `class`
  1207. attribute of the `code` tag. However, this spec does not mandate any
  1208. particular treatment of the [info string].
  1209. Here is a simple example with backticks:
  1210. ```````````````````````````````` example
  1211. ```
  1212. <
  1213. >
  1214. ```
  1215. .
  1216. <pre><code>&lt;
  1217. &gt;
  1218. </code></pre>
  1219. ````````````````````````````````
  1220. With tildes:
  1221. ```````````````````````````````` example
  1222. ~~~
  1223. <
  1224. >
  1225. ~~~
  1226. .
  1227. <pre><code>&lt;
  1228. &gt;
  1229. </code></pre>
  1230. ````````````````````````````````
  1231. The closing code fence must use the same character as the opening
  1232. fence:
  1233. ```````````````````````````````` example
  1234. ```
  1235. aaa
  1236. ~~~
  1237. ```
  1238. .
  1239. <pre><code>aaa
  1240. ~~~
  1241. </code></pre>
  1242. ````````````````````````````````
  1243. ```````````````````````````````` example
  1244. ~~~
  1245. aaa
  1246. ```
  1247. ~~~
  1248. .
  1249. <pre><code>aaa
  1250. ```
  1251. </code></pre>
  1252. ````````````````````````````````
  1253. The closing code fence must be at least as long as the opening fence:
  1254. ```````````````````````````````` example
  1255. ````
  1256. aaa
  1257. ```
  1258. ``````
  1259. .
  1260. <pre><code>aaa
  1261. ```
  1262. </code></pre>
  1263. ````````````````````````````````
  1264. ```````````````````````````````` example
  1265. ~~~~
  1266. aaa
  1267. ~~~
  1268. ~~~~
  1269. .
  1270. <pre><code>aaa
  1271. ~~~
  1272. </code></pre>
  1273. ````````````````````````````````
  1274. Unclosed code blocks are closed by the end of the document
  1275. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1276. ```````````````````````````````` example
  1277. ```
  1278. .
  1279. <pre><code></code></pre>
  1280. ````````````````````````````````
  1281. ```````````````````````````````` example
  1282. `````
  1283. ```
  1284. aaa
  1285. .
  1286. <pre><code>
  1287. ```
  1288. aaa
  1289. </code></pre>
  1290. ````````````````````````````````
  1291. ```````````````````````````````` example
  1292. > ```
  1293. > aaa
  1294. bbb
  1295. .
  1296. <blockquote>
  1297. <pre><code>aaa
  1298. </code></pre>
  1299. </blockquote>
  1300. <p>bbb</p>
  1301. ````````````````````````````````
  1302. A code block can have all empty lines as its content:
  1303. ```````````````````````````````` example
  1304. ```
  1305. ```
  1306. .
  1307. <pre><code>
  1308. </code></pre>
  1309. ````````````````````````````````
  1310. A code block can be empty:
  1311. ```````````````````````````````` example
  1312. ```
  1313. ```
  1314. .
  1315. <pre><code></code></pre>
  1316. ````````````````````````````````
  1317. Fences can be indented. If the opening fence is indented,
  1318. content lines will have equivalent opening indentation removed,
  1319. if present:
  1320. ```````````````````````````````` example
  1321. ```
  1322. aaa
  1323. aaa
  1324. ```
  1325. .
  1326. <pre><code>aaa
  1327. aaa
  1328. </code></pre>
  1329. ````````````````````````````````
  1330. ```````````````````````````````` example
  1331. ```
  1332. aaa
  1333. aaa
  1334. aaa
  1335. ```
  1336. .
  1337. <pre><code>aaa
  1338. aaa
  1339. aaa
  1340. </code></pre>
  1341. ````````````````````````````````
  1342. ```````````````````````````````` example
  1343. ```
  1344. aaa
  1345. aaa
  1346. aaa
  1347. ```
  1348. .
  1349. <pre><code>aaa
  1350. aaa
  1351. aaa
  1352. </code></pre>
  1353. ````````````````````````````````
  1354. Four spaces indentation produces an indented code block:
  1355. ```````````````````````````````` example
  1356. ```
  1357. aaa
  1358. ```
  1359. .
  1360. <pre><code>```
  1361. aaa
  1362. ```
  1363. </code></pre>
  1364. ````````````````````````````````
  1365. Closing fences may be indented by 0-3 spaces, and their indentation
  1366. need not match that of the opening fence:
  1367. ```````````````````````````````` example
  1368. ```
  1369. aaa
  1370. ```
  1371. .
  1372. <pre><code>aaa
  1373. </code></pre>
  1374. ````````````````````````````````
  1375. ```````````````````````````````` example
  1376. ```
  1377. aaa
  1378. ```
  1379. .
  1380. <pre><code>aaa
  1381. </code></pre>
  1382. ````````````````````````````````
  1383. This is not a closing fence, because it is indented 4 spaces:
  1384. ```````````````````````````````` example
  1385. ```
  1386. aaa
  1387. ```
  1388. .
  1389. <pre><code>aaa
  1390. ```
  1391. </code></pre>
  1392. ````````````````````````````````
  1393. Code fences (opening and closing) cannot contain internal spaces:
  1394. ```````````````````````````````` example
  1395. ``` ```
  1396. aaa
  1397. .
  1398. <p><code></code>
  1399. aaa</p>
  1400. ````````````````````````````````
  1401. ```````````````````````````````` example
  1402. ~~~~~~
  1403. aaa
  1404. ~~~ ~~
  1405. .
  1406. <pre><code>aaa
  1407. ~~~ ~~
  1408. </code></pre>
  1409. ````````````````````````````````
  1410. Fenced code blocks can interrupt paragraphs, and can be followed
  1411. directly by paragraphs, without a blank line between:
  1412. ```````````````````````````````` example
  1413. foo
  1414. ```
  1415. bar
  1416. ```
  1417. baz
  1418. .
  1419. <p>foo</p>
  1420. <pre><code>bar
  1421. </code></pre>
  1422. <p>baz</p>
  1423. ````````````````````````````````
  1424. Other blocks can also occur before and after fenced code blocks
  1425. without an intervening blank line:
  1426. ```````````````````````````````` example
  1427. foo
  1428. ---
  1429. ~~~
  1430. bar
  1431. ~~~
  1432. # baz
  1433. .
  1434. <h2>foo</h2>
  1435. <pre><code>bar
  1436. </code></pre>
  1437. <h1>baz</h1>
  1438. ````````````````````````````````
  1439. An [info string] can be provided after the opening code fence.
  1440. Opening and closing spaces will be stripped, and the first word, prefixed
  1441. with `language-`, is used as the value for the `class` attribute of the
  1442. `code` element within the enclosing `pre` element.
  1443. ```````````````````````````````` example
  1444. ```ruby
  1445. def foo(x)
  1446. return 3
  1447. end
  1448. ```
  1449. .
  1450. <pre><code class="language-ruby">def foo(x)
  1451. return 3
  1452. end
  1453. </code></pre>
  1454. ````````````````````````````````
  1455. ```````````````````````````````` example
  1456. ~~~~ ruby startline=3 $%@#$
  1457. def foo(x)
  1458. return 3
  1459. end
  1460. ~~~~~~~
  1461. .
  1462. <pre><code class="language-ruby">def foo(x)
  1463. return 3
  1464. end
  1465. </code></pre>
  1466. ````````````````````````````````
  1467. ```````````````````````````````` example
  1468. ````;
  1469. ````
  1470. .
  1471. <pre><code class="language-;"></code></pre>
  1472. ````````````````````````````````
  1473. [Info strings] for backtick code blocks cannot contain backticks:
  1474. ```````````````````````````````` example
  1475. ``` aa ```
  1476. foo
  1477. .
  1478. <p><code>aa</code>
  1479. foo</p>
  1480. ````````````````````````````````
  1481. Closing code fences cannot have [info strings]:
  1482. ```````````````````````````````` example
  1483. ```
  1484. ``` aaa
  1485. ```
  1486. .
  1487. <pre><code>``` aaa
  1488. </code></pre>
  1489. ````````````````````````````````
  1490. ## HTML blocks
  1491. An [HTML block](@) is a group of lines that is treated
  1492. as raw HTML (and will not be escaped in HTML output).
  1493. There are seven kinds of [HTML block], which can be defined
  1494. by their start and end conditions. The block begins with a line that
  1495. meets a [start condition](@) (after up to three spaces
  1496. optional indentation). It ends with the first subsequent line that
  1497. meets a matching [end condition](@), or the last line of
  1498. the document or other [container block](@), if no line is encountered that meets the
  1499. [end condition]. If the first line meets both the [start condition]
  1500. and the [end condition], the block will contain just that line.
  1501. 1. **Start condition:** line begins with the string `<script`,
  1502. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1503. the string `>`, or the end of the line.\
  1504. **End condition:** line contains an end tag
  1505. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1506. need not match the start tag).
  1507. 2. **Start condition:** line begins with the string `<!--`.\
  1508. **End condition:** line contains the string `-->`.
  1509. 3. **Start condition:** line begins with the string `<?`.\
  1510. **End condition:** line contains the string `?>`.
  1511. 4. **Start condition:** line begins with the string `<!`
  1512. followed by an uppercase ASCII letter.\
  1513. **End condition:** line contains the character `>`.
  1514. 5. **Start condition:** line begins with the string
  1515. `<![CDATA[`.\
  1516. **End condition:** line contains the string `]]>`.
  1517. 6. **Start condition:** line begins the string `<` or `</`
  1518. followed by one of the strings (case-insensitive) `address`,
  1519. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1520. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1521. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1522. `footer`, `form`, `frame`, `frameset`, `h1`, `head`, `header`, `hr`,
  1523. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1524. `meta`, `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1525. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1526. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1527. by [whitespace], the end of the line, the string `>`, or
  1528. the string `/>`.\
  1529. **End condition:** line is followed by a [blank line].
  1530. 7. **Start condition:** line begins with a complete [open tag]
  1531. or [closing tag] (with any [tag name] other than `script`,
  1532. `style`, or `pre`) followed only by [whitespace]
  1533. or the end of the line.\
  1534. **End condition:** line is followed by a [blank line].
  1535. All types of [HTML blocks] except type 7 may interrupt
  1536. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1537. (This restriction is intended to prevent unwanted interpretation
  1538. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1539. Some simple examples follow. Here are some basic HTML blocks
  1540. of type 6:
  1541. ```````````````````````````````` example
  1542. <table>
  1543. <tr>
  1544. <td>
  1545. hi
  1546. </td>
  1547. </tr>
  1548. </table>
  1549. okay.
  1550. .
  1551. <table>
  1552. <tr>
  1553. <td>
  1554. hi
  1555. </td>
  1556. </tr>
  1557. </table>
  1558. <p>okay.</p>
  1559. ````````````````````````````````
  1560. ```````````````````````````````` example
  1561. <div>
  1562. *hello*
  1563. <foo><a>
  1564. .
  1565. <div>
  1566. *hello*
  1567. <foo><a>
  1568. ````````````````````````````````
  1569. A block can also start with a closing tag:
  1570. ```````````````````````````````` example
  1571. </div>
  1572. *foo*
  1573. .
  1574. </div>
  1575. *foo*
  1576. ````````````````````````````````
  1577. Here we have two HTML blocks with a Markdown paragraph between them:
  1578. ```````````````````````````````` example
  1579. <DIV CLASS="foo">
  1580. *Markdown*
  1581. </DIV>
  1582. .
  1583. <DIV CLASS="foo">
  1584. <p><em>Markdown</em></p>
  1585. </DIV>
  1586. ````````````````````````````````
  1587. The tag on the first line can be partial, as long
  1588. as it is split where there would be whitespace:
  1589. ```````````````````````````````` example
  1590. <div id="foo"
  1591. class="bar">
  1592. </div>
  1593. .
  1594. <div id="foo"
  1595. class="bar">
  1596. </div>
  1597. ````````````````````````````````
  1598. ```````````````````````````````` example
  1599. <div id="foo" class="bar
  1600. baz">
  1601. </div>
  1602. .
  1603. <div id="foo" class="bar
  1604. baz">
  1605. </div>
  1606. ````````````````````````````````
  1607. An open tag need not be closed:
  1608. ```````````````````````````````` example
  1609. <div>
  1610. *foo*
  1611. *bar*
  1612. .
  1613. <div>
  1614. *foo*
  1615. <p><em>bar</em></p>
  1616. ````````````````````````````````
  1617. A partial tag need not even be completed (garbage
  1618. in, garbage out):
  1619. ```````````````````````````````` example
  1620. <div id="foo"
  1621. *hi*
  1622. .
  1623. <div id="foo"
  1624. *hi*
  1625. ````````````````````````````````
  1626. ```````````````````````````````` example
  1627. <div class
  1628. foo
  1629. .
  1630. <div class
  1631. foo
  1632. ````````````````````````````````
  1633. The initial tag doesn't even need to be a valid
  1634. tag, as long as it starts like one:
  1635. ```````````````````````````````` example
  1636. <div *???-&&&-<---
  1637. *foo*
  1638. .
  1639. <div *???-&&&-<---
  1640. *foo*
  1641. ````````````````````````````````
  1642. In type 6 blocks, the initial tag need not be on a line by
  1643. itself:
  1644. ```````````````````````````````` example
  1645. <div><a href="bar">*foo*</a></div>
  1646. .
  1647. <div><a href="bar">*foo*</a></div>
  1648. ````````````````````````````````
  1649. ```````````````````````````````` example
  1650. <table><tr><td>
  1651. foo
  1652. </td></tr></table>
  1653. .
  1654. <table><tr><td>
  1655. foo
  1656. </td></tr></table>
  1657. ````````````````````````````````
  1658. Everything until the next blank line or end of document
  1659. gets included in the HTML block. So, in the following
  1660. example, what looks like a Markdown code block
  1661. is actually part of the HTML block, which continues until a blank
  1662. line or the end of the document is reached:
  1663. ```````````````````````````````` example
  1664. <div></div>
  1665. ``` c
  1666. int x = 33;
  1667. ```
  1668. .
  1669. <div></div>
  1670. ``` c
  1671. int x = 33;
  1672. ```
  1673. ````````````````````````````````
  1674. To start an [HTML block] with a tag that is *not* in the
  1675. list of block-level tags in (6), you must put the tag by
  1676. itself on the first line (and it must be complete):
  1677. ```````````````````````````````` example
  1678. <a href="foo">
  1679. *bar*
  1680. </a>
  1681. .
  1682. <a href="foo">
  1683. *bar*
  1684. </a>
  1685. ````````````````````````````````
  1686. In type 7 blocks, the [tag name] can be anything:
  1687. ```````````````````````````````` example
  1688. <Warning>
  1689. *bar*
  1690. </Warning>
  1691. .
  1692. <Warning>
  1693. *bar*
  1694. </Warning>
  1695. ````````````````````````````````
  1696. ```````````````````````````````` example
  1697. <i class="foo">
  1698. *bar*
  1699. </i>
  1700. .
  1701. <i class="foo">
  1702. *bar*
  1703. </i>
  1704. ````````````````````````````````
  1705. ```````````````````````````````` example
  1706. </ins>
  1707. *bar*
  1708. .
  1709. </ins>
  1710. *bar*
  1711. ````````````````````````````````
  1712. These rules are designed to allow us to work with tags that
  1713. can function as either block-level or inline-level tags.
  1714. The `<del>` tag is a nice example. We can surround content with
  1715. `<del>` tags in three different ways. In this case, we get a raw
  1716. HTML block, because the `<del>` tag is on a line by itself:
  1717. ```````````````````````````````` example
  1718. <del>
  1719. *foo*
  1720. </del>
  1721. .
  1722. <del>
  1723. *foo*
  1724. </del>
  1725. ````````````````````````````````
  1726. In this case, we get a raw HTML block that just includes
  1727. the `<del>` tag (because it ends with the following blank
  1728. line). So the contents get interpreted as CommonMark:
  1729. ```````````````````````````````` example
  1730. <del>
  1731. *foo*
  1732. </del>
  1733. .
  1734. <del>
  1735. <p><em>foo</em></p>
  1736. </del>
  1737. ````````````````````````````````
  1738. Finally, in this case, the `<del>` tags are interpreted
  1739. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1740. the tag is not on a line by itself, we get inline HTML
  1741. rather than an [HTML block].)
  1742. ```````````````````````````````` example
  1743. <del>*foo*</del>
  1744. .
  1745. <p><del><em>foo</em></del></p>
  1746. ````````````````````````````````
  1747. HTML tags designed to contain literal content
  1748. (`script`, `style`, `pre`), comments, processing instructions,
  1749. and declarations are treated somewhat differently.
  1750. Instead of ending at the first blank line, these blocks
  1751. end at the first line containing a corresponding end tag.
  1752. As a result, these blocks can contain blank lines:
  1753. A pre tag (type 1):
  1754. ```````````````````````````````` example
  1755. <pre language="haskell"><code>
  1756. import Text.HTML.TagSoup
  1757. main :: IO ()
  1758. main = print $ parseTags tags
  1759. </code></pre>
  1760. okay
  1761. .
  1762. <pre language="haskell"><code>
  1763. import Text.HTML.TagSoup
  1764. main :: IO ()
  1765. main = print $ parseTags tags
  1766. </code></pre>
  1767. <p>okay</p>
  1768. ````````````````````````````````
  1769. A script tag (type 1):
  1770. ```````````````````````````````` example
  1771. <script type="text/javascript">
  1772. // JavaScript example
  1773. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1774. </script>
  1775. okay
  1776. .
  1777. <script type="text/javascript">
  1778. // JavaScript example
  1779. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1780. </script>
  1781. <p>okay</p>
  1782. ````````````````````````````````
  1783. A style tag (type 1):
  1784. ```````````````````````````````` example
  1785. <style
  1786. type="text/css">
  1787. h1 {color:red;}
  1788. p {color:blue;}
  1789. </style>
  1790. okay
  1791. .
  1792. <style
  1793. type="text/css">
  1794. h1 {color:red;}
  1795. p {color:blue;}
  1796. </style>
  1797. <p>okay</p>
  1798. ````````````````````````````````
  1799. If there is no matching end tag, the block will end at the
  1800. end of the document (or the enclosing [block quote][block quotes]
  1801. or [list item][list items]):
  1802. ```````````````````````````````` example
  1803. <style
  1804. type="text/css">
  1805. foo
  1806. .
  1807. <style
  1808. type="text/css">
  1809. foo
  1810. ````````````````````````````````
  1811. ```````````````````````````````` example
  1812. > <div>
  1813. > foo
  1814. bar
  1815. .
  1816. <blockquote>
  1817. <div>
  1818. foo
  1819. </blockquote>
  1820. <p>bar</p>
  1821. ````````````````````````````````
  1822. ```````````````````````````````` example
  1823. - <div>
  1824. - foo
  1825. .
  1826. <ul>
  1827. <li>
  1828. <div>
  1829. </li>
  1830. <li>foo</li>
  1831. </ul>
  1832. ````````````````````````````````
  1833. The end tag can occur on the same line as the start tag:
  1834. ```````````````````````````````` example
  1835. <style>p{color:red;}</style>
  1836. *foo*
  1837. .
  1838. <style>p{color:red;}</style>
  1839. <p><em>foo</em></p>
  1840. ````````````````````````````````
  1841. ```````````````````````````````` example
  1842. <!-- foo -->*bar*
  1843. *baz*
  1844. .
  1845. <!-- foo -->*bar*
  1846. <p><em>baz</em></p>
  1847. ````````````````````````````````
  1848. Note that anything on the last line after the
  1849. end tag will be included in the [HTML block]:
  1850. ```````````````````````````````` example
  1851. <script>
  1852. foo
  1853. </script>1. *bar*
  1854. .
  1855. <script>
  1856. foo
  1857. </script>1. *bar*
  1858. ````````````````````````````````
  1859. A comment (type 2):
  1860. ```````````````````````````````` example
  1861. <!-- Foo
  1862. bar
  1863. baz -->
  1864. okay
  1865. .
  1866. <!-- Foo
  1867. bar
  1868. baz -->
  1869. <p>okay</p>
  1870. ````````````````````````````````
  1871. A processing instruction (type 3):
  1872. ```````````````````````````````` example
  1873. <?php
  1874. echo '>';
  1875. ?>
  1876. okay
  1877. .
  1878. <?php
  1879. echo '>';
  1880. ?>
  1881. <p>okay</p>
  1882. ````````````````````````````````
  1883. A declaration (type 4):
  1884. ```````````````````````````````` example
  1885. <!DOCTYPE html>
  1886. .
  1887. <!DOCTYPE html>
  1888. ````````````````````````````````
  1889. CDATA (type 5):
  1890. ```````````````````````````````` example
  1891. <![CDATA[
  1892. function matchwo(a,b)
  1893. {
  1894. if (a < b && a < 0) then {
  1895. return 1;
  1896. } else {
  1897. return 0;
  1898. }
  1899. }
  1900. ]]>
  1901. okay
  1902. .
  1903. <![CDATA[
  1904. function matchwo(a,b)
  1905. {
  1906. if (a < b && a < 0) then {
  1907. return 1;
  1908. } else {
  1909. return 0;
  1910. }
  1911. }
  1912. ]]>
  1913. <p>okay</p>
  1914. ````````````````````````````````
  1915. The opening tag can be indented 1-3 spaces, but not 4:
  1916. ```````````````````````````````` example
  1917. <!-- foo -->
  1918. <!-- foo -->
  1919. .
  1920. <!-- foo -->
  1921. <pre><code>&lt;!-- foo --&gt;
  1922. </code></pre>
  1923. ````````````````````````````````
  1924. ```````````````````````````````` example
  1925. <div>
  1926. <div>
  1927. .
  1928. <div>
  1929. <pre><code>&lt;div&gt;
  1930. </code></pre>
  1931. ````````````````````````````````
  1932. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  1933. preceded by a blank line.
  1934. ```````````````````````````````` example
  1935. Foo
  1936. <div>
  1937. bar
  1938. </div>
  1939. .
  1940. <p>Foo</p>
  1941. <div>
  1942. bar
  1943. </div>
  1944. ````````````````````````````````
  1945. However, a following blank line is needed, except at the end of
  1946. a document, and except for blocks of types 1--5, above:
  1947. ```````````````````````````````` example
  1948. <div>
  1949. bar
  1950. </div>
  1951. *foo*
  1952. .
  1953. <div>
  1954. bar
  1955. </div>
  1956. *foo*
  1957. ````````````````````````````````
  1958. HTML blocks of type 7 cannot interrupt a paragraph:
  1959. ```````````````````````````````` example
  1960. Foo
  1961. <a href="bar">
  1962. baz
  1963. .
  1964. <p>Foo
  1965. <a href="bar">
  1966. baz</p>
  1967. ````````````````````````````````
  1968. This rule differs from John Gruber's original Markdown syntax
  1969. specification, which says:
  1970. > The only restrictions are that block-level HTML elements —
  1971. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  1972. > surrounding content by blank lines, and the start and end tags of the
  1973. > block should not be indented with tabs or spaces.
  1974. In some ways Gruber's rule is more restrictive than the one given
  1975. here:
  1976. - It requires that an HTML block be preceded by a blank line.
  1977. - It does not allow the start tag to be indented.
  1978. - It requires a matching end tag, which it also does not allow to
  1979. be indented.
  1980. Most Markdown implementations (including some of Gruber's own) do not
  1981. respect all of these restrictions.
  1982. There is one respect, however, in which Gruber's rule is more liberal
  1983. than the one given here, since it allows blank lines to occur inside
  1984. an HTML block. There are two reasons for disallowing them here.
  1985. First, it removes the need to parse balanced tags, which is
  1986. expensive and can require backtracking from the end of the document
  1987. if no matching end tag is found. Second, it provides a very simple
  1988. and flexible way of including Markdown content inside HTML tags:
  1989. simply separate the Markdown from the HTML using blank lines:
  1990. Compare:
  1991. ```````````````````````````````` example
  1992. <div>
  1993. *Emphasized* text.
  1994. </div>
  1995. .
  1996. <div>
  1997. <p><em>Emphasized</em> text.</p>
  1998. </div>
  1999. ````````````````````````````````
  2000. ```````````````````````````````` example
  2001. <div>
  2002. *Emphasized* text.
  2003. </div>
  2004. .
  2005. <div>
  2006. *Emphasized* text.
  2007. </div>
  2008. ````````````````````````````````
  2009. Some Markdown implementations have adopted a convention of
  2010. interpreting content inside tags as text if the open tag has
  2011. the attribute `markdown=1`. The rule given above seems a simpler and
  2012. more elegant way of achieving the same expressive power, which is also
  2013. much simpler to parse.
  2014. The main potential drawback is that one can no longer paste HTML
  2015. blocks into Markdown documents with 100% reliability. However,
  2016. *in most cases* this will work fine, because the blank lines in
  2017. HTML are usually followed by HTML block tags. For example:
  2018. ```````````````````````````````` example
  2019. <table>
  2020. <tr>
  2021. <td>
  2022. Hi
  2023. </td>
  2024. </tr>
  2025. </table>
  2026. .
  2027. <table>
  2028. <tr>
  2029. <td>
  2030. Hi
  2031. </td>
  2032. </tr>
  2033. </table>
  2034. ````````````````````````````````
  2035. There are problems, however, if the inner tags are indented
  2036. *and* separated by spaces, as then they will be interpreted as
  2037. an indented code block:
  2038. ```````````````````````````````` example
  2039. <table>
  2040. <tr>
  2041. <td>
  2042. Hi
  2043. </td>
  2044. </tr>
  2045. </table>
  2046. .
  2047. <table>
  2048. <tr>
  2049. <pre><code>&lt;td&gt;
  2050. Hi
  2051. &lt;/td&gt;
  2052. </code></pre>
  2053. </tr>
  2054. </table>
  2055. ````````````````````````````````
  2056. Fortunately, blank lines are usually not necessary and can be
  2057. deleted. The exception is inside `<pre>` tags, but as described
  2058. above, raw HTML blocks starting with `<pre>` *can* contain blank
  2059. lines.
  2060. ## Link reference definitions
  2061. A [link reference definition](@)
  2062. consists of a [link label], indented up to three spaces, followed
  2063. by a colon (`:`), optional [whitespace] (including up to one
  2064. [line ending]), a [link destination],
  2065. optional [whitespace] (including up to one
  2066. [line ending]), and an optional [link
  2067. title], which if it is present must be separated
  2068. from the [link destination] by [whitespace].
  2069. No further [non-whitespace characters] may occur on the line.
  2070. A [link reference definition]
  2071. does not correspond to a structural element of a document. Instead, it
  2072. defines a label which can be used in [reference links]
  2073. and reference-style [images] elsewhere in the document. [Link
  2074. reference definitions] can come either before or after the links that use
  2075. them.
  2076. ```````````````````````````````` example
  2077. [foo]: /url "title"
  2078. [foo]
  2079. .
  2080. <p><a href="/url" title="title">foo</a></p>
  2081. ````````````````````````````````
  2082. ```````````````````````````````` example
  2083. [foo]:
  2084. /url
  2085. 'the title'
  2086. [foo]
  2087. .
  2088. <p><a href="/url" title="the title">foo</a></p>
  2089. ````````````````````````````````
  2090. ```````````````````````````````` example
  2091. [Foo*bar\]]:my_(url) 'title (with parens)'
  2092. [Foo*bar\]]
  2093. .
  2094. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2095. ````````````````````````````````
  2096. ```````````````````````````````` example
  2097. [Foo bar]:
  2098. <my%20url>
  2099. 'title'
  2100. [Foo bar]
  2101. .
  2102. <p><a href="my%20url" title="title">Foo bar</a></p>
  2103. ````````````````````````````````
  2104. The title may extend over multiple lines:
  2105. ```````````````````````````````` example
  2106. [foo]: /url '
  2107. title
  2108. line1
  2109. line2
  2110. '
  2111. [foo]
  2112. .
  2113. <p><a href="/url" title="
  2114. title
  2115. line1
  2116. line2
  2117. ">foo</a></p>
  2118. ````````````````````````````````
  2119. However, it may not contain a [blank line]:
  2120. ```````````````````````````````` example
  2121. [foo]: /url 'title
  2122. with blank line'
  2123. [foo]
  2124. .
  2125. <p>[foo]: /url 'title</p>
  2126. <p>with blank line'</p>
  2127. <p>[foo]</p>
  2128. ````````````````````````````````
  2129. The title may be omitted:
  2130. ```````````````````````````````` example
  2131. [foo]:
  2132. /url
  2133. [foo]
  2134. .
  2135. <p><a href="/url">foo</a></p>
  2136. ````````````````````````````````
  2137. The link destination may not be omitted:
  2138. ```````````````````````````````` example
  2139. [foo]:
  2140. [foo]
  2141. .
  2142. <p>[foo]:</p>
  2143. <p>[foo]</p>
  2144. ````````````````````````````````
  2145. Both title and destination can contain backslash escapes
  2146. and literal backslashes:
  2147. ```````````````````````````````` example
  2148. [foo]: /url\bar\*baz "foo\"bar\baz"
  2149. [foo]
  2150. .
  2151. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2152. ````````````````````````````````
  2153. A link can come before its corresponding definition:
  2154. ```````````````````````````````` example
  2155. [foo]
  2156. [foo]: url
  2157. .
  2158. <p><a href="url">foo</a></p>
  2159. ````````````````````````````````
  2160. If there are several matching definitions, the first one takes
  2161. precedence:
  2162. ```````````````````````````````` example
  2163. [foo]
  2164. [foo]: first
  2165. [foo]: second
  2166. .
  2167. <p><a href="first">foo</a></p>
  2168. ````````````````````````````````
  2169. As noted in the section on [Links], matching of labels is
  2170. case-insensitive (see [matches]).
  2171. ```````````````````````````````` example
  2172. [FOO]: /url
  2173. [Foo]
  2174. .
  2175. <p><a href="/url">Foo</a></p>
  2176. ````````````````````````````````
  2177. ```````````````````````````````` example
  2178. [ΑΓΩ]: /φου
  2179. [αγω]
  2180. .
  2181. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2182. ````````````````````````````````
  2183. Here is a link reference definition with no corresponding link.
  2184. It contributes nothing to the document.
  2185. ```````````````````````````````` example
  2186. [foo]: /url
  2187. .
  2188. ````````````````````````````````
  2189. Here is another one:
  2190. ```````````````````````````````` example
  2191. [
  2192. foo
  2193. ]: /url
  2194. bar
  2195. .
  2196. <p>bar</p>
  2197. ````````````````````````````````
  2198. This is not a link reference definition, because there are
  2199. [non-whitespace characters] after the title:
  2200. ```````````````````````````````` example
  2201. [foo]: /url "title" ok
  2202. .
  2203. <p>[foo]: /url &quot;title&quot; ok</p>
  2204. ````````````````````````````````
  2205. This is a link reference definition, but it has no title:
  2206. ```````````````````````````````` example
  2207. [foo]: /url
  2208. "title" ok
  2209. .
  2210. <p>&quot;title&quot; ok</p>
  2211. ````````````````````````````````
  2212. This is not a link reference definition, because it is indented
  2213. four spaces:
  2214. ```````````````````````````````` example
  2215. [foo]: /url "title"
  2216. [foo]
  2217. .
  2218. <pre><code>[foo]: /url &quot;title&quot;
  2219. </code></pre>
  2220. <p>[foo]</p>
  2221. ````````````````````````````````
  2222. This is not a link reference definition, because it occurs inside
  2223. a code block:
  2224. ```````````````````````````````` example
  2225. ```
  2226. [foo]: /url
  2227. ```
  2228. [foo]
  2229. .
  2230. <pre><code>[foo]: /url
  2231. </code></pre>
  2232. <p>[foo]</p>
  2233. ````````````````````````````````
  2234. A [link reference definition] cannot interrupt a paragraph.
  2235. ```````````````````````````````` example
  2236. Foo
  2237. [bar]: /baz
  2238. [bar]
  2239. .
  2240. <p>Foo
  2241. [bar]: /baz</p>
  2242. <p>[bar]</p>
  2243. ````````````````````````````````
  2244. However, it can directly follow other block elements, such as headings
  2245. and thematic breaks, and it need not be followed by a blank line.
  2246. ```````````````````````````````` example
  2247. # [Foo]
  2248. [foo]: /url
  2249. > bar
  2250. .
  2251. <h1><a href="/url">Foo</a></h1>
  2252. <blockquote>
  2253. <p>bar</p>
  2254. </blockquote>
  2255. ````````````````````````````````
  2256. Several [link reference definitions]
  2257. can occur one after another, without intervening blank lines.
  2258. ```````````````````````````````` example
  2259. [foo]: /foo-url "foo"
  2260. [bar]: /bar-url
  2261. "bar"
  2262. [baz]: /baz-url
  2263. [foo],
  2264. [bar],
  2265. [baz]
  2266. .
  2267. <p><a href="/foo-url" title="foo">foo</a>,
  2268. <a href="/bar-url" title="bar">bar</a>,
  2269. <a href="/baz-url">baz</a></p>
  2270. ````````````````````````````````
  2271. [Link reference definitions] can occur
  2272. inside block containers, like lists and block quotations. They
  2273. affect the entire document, not just the container in which they
  2274. are defined:
  2275. ```````````````````````````````` example
  2276. [foo]
  2277. > [foo]: /url
  2278. .
  2279. <p><a href="/url">foo</a></p>
  2280. <blockquote>
  2281. </blockquote>
  2282. ````````````````````````````````
  2283. ## Paragraphs
  2284. A sequence of non-blank lines that cannot be interpreted as other
  2285. kinds of blocks forms a [paragraph](@).
  2286. The contents of the paragraph are the result of parsing the
  2287. paragraph's raw content as inlines. The paragraph's raw content
  2288. is formed by concatenating the lines and removing initial and final
  2289. [whitespace].
  2290. A simple example with two paragraphs:
  2291. ```````````````````````````````` example
  2292. aaa
  2293. bbb
  2294. .
  2295. <p>aaa</p>
  2296. <p>bbb</p>
  2297. ````````````````````````````````
  2298. Paragraphs can contain multiple lines, but no blank lines:
  2299. ```````````````````````````````` example
  2300. aaa
  2301. bbb
  2302. ccc
  2303. ddd
  2304. .
  2305. <p>aaa
  2306. bbb</p>
  2307. <p>ccc
  2308. ddd</p>
  2309. ````````````````````````````````
  2310. Multiple blank lines between paragraph have no effect:
  2311. ```````````````````````````````` example
  2312. aaa
  2313. bbb
  2314. .
  2315. <p>aaa</p>
  2316. <p>bbb</p>
  2317. ````````````````````````````````
  2318. Leading spaces are skipped:
  2319. ```````````````````````````````` example
  2320. aaa
  2321. bbb
  2322. .
  2323. <p>aaa
  2324. bbb</p>
  2325. ````````````````````````````````
  2326. Lines after the first may be indented any amount, since indented
  2327. code blocks cannot interrupt paragraphs.
  2328. ```````````````````````````````` example
  2329. aaa
  2330. bbb
  2331. ccc
  2332. .
  2333. <p>aaa
  2334. bbb
  2335. ccc</p>
  2336. ````````````````````````````````
  2337. However, the first line may be indented at most three spaces,
  2338. or an indented code block will be triggered:
  2339. ```````````````````````````````` example
  2340. aaa
  2341. bbb
  2342. .
  2343. <p>aaa
  2344. bbb</p>
  2345. ````````````````````````````````
  2346. ```````````````````````````````` example
  2347. aaa
  2348. bbb
  2349. .
  2350. <pre><code>aaa
  2351. </code></pre>
  2352. <p>bbb</p>
  2353. ````````````````````````````````
  2354. Final spaces are stripped before inline parsing, so a paragraph
  2355. that ends with two or more spaces will not end with a [hard line
  2356. break]:
  2357. ```````````````````````````````` example
  2358. aaa
  2359. bbb
  2360. .
  2361. <p>aaa<br />
  2362. bbb</p>
  2363. ````````````````````````````````
  2364. ## Blank lines
  2365. [Blank lines] between block-level elements are ignored,
  2366. except for the role they play in determining whether a [list]
  2367. is [tight] or [loose].
  2368. Blank lines at the beginning and end of the document are also ignored.
  2369. ```````````````````````````````` example
  2370. aaa
  2371. # aaa
  2372. .
  2373. <p>aaa</p>
  2374. <h1>aaa</h1>
  2375. ````````````````````````````````
  2376. # Container blocks
  2377. A [container block] is a block that has other
  2378. blocks as its contents. There are two basic kinds of container blocks:
  2379. [block quotes] and [list items].
  2380. [Lists] are meta-containers for [list items].
  2381. We define the syntax for container blocks recursively. The general
  2382. form of the definition is:
  2383. > If X is a sequence of blocks, then the result of
  2384. > transforming X in such-and-such a way is a container of type Y
  2385. > with these blocks as its content.
  2386. So, we explain what counts as a block quote or list item by explaining
  2387. how these can be *generated* from their contents. This should suffice
  2388. to define the syntax, although it does not give a recipe for *parsing*
  2389. these constructions. (A recipe is provided below in the section entitled
  2390. [A parsing strategy](#appendix-a-parsing-strategy).)
  2391. ## Block quotes
  2392. A [block quote marker](@)
  2393. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2394. with a following space, or (b) a single character `>` not followed by a space.
  2395. The following rules define [block quotes]:
  2396. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2397. of blocks *Bs*, then the result of prepending a [block quote
  2398. marker] to the beginning of each line in *Ls*
  2399. is a [block quote](#block-quotes) containing *Bs*.
  2400. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2401. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2402. the initial [block quote marker] from one or
  2403. more lines in which the next [non-whitespace character] after the [block
  2404. quote marker] is [paragraph continuation
  2405. text] is a block quote with *Bs* as its content.
  2406. [Paragraph continuation text](@) is text
  2407. that will be parsed as part of the content of a paragraph, but does
  2408. not occur at the beginning of the paragraph.
  2409. 3. **Consecutiveness.** A document cannot contain two [block
  2410. quotes] in a row unless there is a [blank line] between them.
  2411. Nothing else counts as a [block quote](#block-quotes).
  2412. Here is a simple example:
  2413. ```````````````````````````````` example
  2414. > # Foo
  2415. > bar
  2416. > baz
  2417. .
  2418. <blockquote>
  2419. <h1>Foo</h1>
  2420. <p>bar
  2421. baz</p>
  2422. </blockquote>
  2423. ````````````````````````````````
  2424. The spaces after the `>` characters can be omitted:
  2425. ```````````````````````````````` example
  2426. ># Foo
  2427. >bar
  2428. > baz
  2429. .
  2430. <blockquote>
  2431. <h1>Foo</h1>
  2432. <p>bar
  2433. baz</p>
  2434. </blockquote>
  2435. ````````````````````````````````
  2436. The `>` characters can be indented 1-3 spaces:
  2437. ```````````````````````````````` example
  2438. > # Foo
  2439. > bar
  2440. > baz
  2441. .
  2442. <blockquote>
  2443. <h1>Foo</h1>
  2444. <p>bar
  2445. baz</p>
  2446. </blockquote>
  2447. ````````````````````````````````
  2448. Four spaces gives us a code block:
  2449. ```````````````````````````````` example
  2450. > # Foo
  2451. > bar
  2452. > baz
  2453. .
  2454. <pre><code>&gt; # Foo
  2455. &gt; bar
  2456. &gt; baz
  2457. </code></pre>
  2458. ````````````````````````````````
  2459. The Laziness clause allows us to omit the `>` before
  2460. [paragraph continuation text]:
  2461. ```````````````````````````````` example
  2462. > # Foo
  2463. > bar
  2464. baz
  2465. .
  2466. <blockquote>
  2467. <h1>Foo</h1>
  2468. <p>bar
  2469. baz</p>
  2470. </blockquote>
  2471. ````````````````````````````````
  2472. A block quote can contain some lazy and some non-lazy
  2473. continuation lines:
  2474. ```````````````````````````````` example
  2475. > bar
  2476. baz
  2477. > foo
  2478. .
  2479. <blockquote>
  2480. <p>bar
  2481. baz
  2482. foo</p>
  2483. </blockquote>
  2484. ````````````````````````````````
  2485. Laziness only applies to lines that would have been continuations of
  2486. paragraphs had they been prepended with [block quote markers].
  2487. For example, the `> ` cannot be omitted in the second line of
  2488. ``` markdown
  2489. > foo
  2490. > ---
  2491. ```
  2492. without changing the meaning:
  2493. ```````````````````````````````` example
  2494. > foo
  2495. ---
  2496. .
  2497. <blockquote>
  2498. <p>foo</p>
  2499. </blockquote>
  2500. <hr />
  2501. ````````````````````````````````
  2502. Similarly, if we omit the `> ` in the second line of
  2503. ``` markdown
  2504. > - foo
  2505. > - bar
  2506. ```
  2507. then the block quote ends after the first line:
  2508. ```````````````````````````````` example
  2509. > - foo
  2510. - bar
  2511. .
  2512. <blockquote>
  2513. <ul>
  2514. <li>foo</li>
  2515. </ul>
  2516. </blockquote>
  2517. <ul>
  2518. <li>bar</li>
  2519. </ul>
  2520. ````````````````````````````````
  2521. For the same reason, we can't omit the `> ` in front of
  2522. subsequent lines of an indented or fenced code block:
  2523. ```````````````````````````````` example
  2524. > foo
  2525. bar
  2526. .
  2527. <blockquote>
  2528. <pre><code>foo
  2529. </code></pre>
  2530. </blockquote>
  2531. <pre><code>bar
  2532. </code></pre>
  2533. ````````````````````````````````
  2534. ```````````````````````````````` example
  2535. > ```
  2536. foo
  2537. ```
  2538. .
  2539. <blockquote>
  2540. <pre><code></code></pre>
  2541. </blockquote>
  2542. <p>foo</p>
  2543. <pre><code></code></pre>
  2544. ````````````````````````````````
  2545. Note that in the following case, we have a [lazy
  2546. continuation line]:
  2547. ```````````````````````````````` example
  2548. > foo
  2549. - bar
  2550. .
  2551. <blockquote>
  2552. <p>foo
  2553. - bar</p>
  2554. </blockquote>
  2555. ````````````````````````````````
  2556. To see why, note that in
  2557. ```markdown
  2558. > foo
  2559. > - bar
  2560. ```
  2561. the `- bar` is indented too far to start a list, and can't
  2562. be an indented code block because indented code blocks cannot
  2563. interrupt paragraphs, so it is [paragraph continuation text].
  2564. A block quote can be empty:
  2565. ```````````````````````````````` example
  2566. >
  2567. .
  2568. <blockquote>
  2569. </blockquote>
  2570. ````````````````````````````````
  2571. ```````````````````````````````` example
  2572. >
  2573. >
  2574. >
  2575. .
  2576. <blockquote>
  2577. </blockquote>
  2578. ````````````````````````````````
  2579. A block quote can have initial or final blank lines:
  2580. ```````````````````````````````` example
  2581. >
  2582. > foo
  2583. >
  2584. .
  2585. <blockquote>
  2586. <p>foo</p>
  2587. </blockquote>
  2588. ````````````````````````````````
  2589. A blank line always separates block quotes:
  2590. ```````````````````````````````` example
  2591. > foo
  2592. > bar
  2593. .
  2594. <blockquote>
  2595. <p>foo</p>
  2596. </blockquote>
  2597. <blockquote>
  2598. <p>bar</p>
  2599. </blockquote>
  2600. ````````````````````````````````
  2601. (Most current Markdown implementations, including John Gruber's
  2602. original `Markdown.pl`, will parse this example as a single block quote
  2603. with two paragraphs. But it seems better to allow the author to decide
  2604. whether two block quotes or one are wanted.)
  2605. Consecutiveness means that if we put these block quotes together,
  2606. we get a single block quote:
  2607. ```````````````````````````````` example
  2608. > foo
  2609. > bar
  2610. .
  2611. <blockquote>
  2612. <p>foo
  2613. bar</p>
  2614. </blockquote>
  2615. ````````````````````````````````
  2616. To get a block quote with two paragraphs, use:
  2617. ```````````````````````````````` example
  2618. > foo
  2619. >
  2620. > bar
  2621. .
  2622. <blockquote>
  2623. <p>foo</p>
  2624. <p>bar</p>
  2625. </blockquote>
  2626. ````````````````````````````````
  2627. Block quotes can interrupt paragraphs:
  2628. ```````````````````````````````` example
  2629. foo
  2630. > bar
  2631. .
  2632. <p>foo</p>
  2633. <blockquote>
  2634. <p>bar</p>
  2635. </blockquote>
  2636. ````````````````````````````````
  2637. In general, blank lines are not needed before or after block
  2638. quotes:
  2639. ```````````````````````````````` example
  2640. > aaa
  2641. ***
  2642. > bbb
  2643. .
  2644. <blockquote>
  2645. <p>aaa</p>
  2646. </blockquote>
  2647. <hr />
  2648. <blockquote>
  2649. <p>bbb</p>
  2650. </blockquote>
  2651. ````````````````````````````````
  2652. However, because of laziness, a blank line is needed between
  2653. a block quote and a following paragraph:
  2654. ```````````````````````````````` example
  2655. > bar
  2656. baz
  2657. .
  2658. <blockquote>
  2659. <p>bar
  2660. baz</p>
  2661. </blockquote>
  2662. ````````````````````````````````
  2663. ```````````````````````````````` example
  2664. > bar
  2665. baz
  2666. .
  2667. <blockquote>
  2668. <p>bar</p>
  2669. </blockquote>
  2670. <p>baz</p>
  2671. ````````````````````````````````
  2672. ```````````````````````````````` example
  2673. > bar
  2674. >
  2675. baz
  2676. .
  2677. <blockquote>
  2678. <p>bar</p>
  2679. </blockquote>
  2680. <p>baz</p>
  2681. ````````````````````````````````
  2682. It is a consequence of the Laziness rule that any number
  2683. of initial `>`s may be omitted on a continuation line of a
  2684. nested block quote:
  2685. ```````````````````````````````` example
  2686. > > > foo
  2687. bar
  2688. .
  2689. <blockquote>
  2690. <blockquote>
  2691. <blockquote>
  2692. <p>foo
  2693. bar</p>
  2694. </blockquote>
  2695. </blockquote>
  2696. </blockquote>
  2697. ````````````````````````````````
  2698. ```````````````````````````````` example
  2699. >>> foo
  2700. > bar
  2701. >>baz
  2702. .
  2703. <blockquote>
  2704. <blockquote>
  2705. <blockquote>
  2706. <p>foo
  2707. bar
  2708. baz</p>
  2709. </blockquote>
  2710. </blockquote>
  2711. </blockquote>
  2712. ````````````````````````````````
  2713. When including an indented code block in a block quote,
  2714. remember that the [block quote marker] includes
  2715. both the `>` and a following space. So *five spaces* are needed after
  2716. the `>`:
  2717. ```````````````````````````````` example
  2718. > code
  2719. > not code
  2720. .
  2721. <blockquote>
  2722. <pre><code>code
  2723. </code></pre>
  2724. </blockquote>
  2725. <blockquote>
  2726. <p>not code</p>
  2727. </blockquote>
  2728. ````````````````````````````````
  2729. ## List items
  2730. A [list marker](@) is a
  2731. [bullet list marker] or an [ordered list marker].
  2732. A [bullet list marker](@)
  2733. is a `-`, `+`, or `*` character.
  2734. An [ordered list marker](@)
  2735. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2736. `.` character or a `)` character. (The reason for the length
  2737. limit is that with 10 digits we start seeing integer overflows
  2738. in some browsers.)
  2739. The following rules define [list items]:
  2740. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2741. blocks *Bs* starting with a [non-whitespace character] and not separated
  2742. from each other by more than one blank line, and *M* is a list
  2743. marker of width *W* followed by 0 < *N* < 5 spaces, then the result
  2744. of prepending *M* and the following spaces to the first line of
  2745. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2746. list item with *Bs* as its contents. The type of the list item
  2747. (bullet or ordered) is determined by the type of its list marker.
  2748. If the list item is ordered, then it is also assigned a start
  2749. number, based on the ordered list marker.
  2750. For example, let *Ls* be the lines
  2751. ```````````````````````````````` example
  2752. A paragraph
  2753. with two lines.
  2754. indented code
  2755. > A block quote.
  2756. .
  2757. <p>A paragraph
  2758. with two lines.</p>
  2759. <pre><code>indented code
  2760. </code></pre>
  2761. <blockquote>
  2762. <p>A block quote.</p>
  2763. </blockquote>
  2764. ````````````````````````````````
  2765. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2766. that the following is an ordered list item with start number 1,
  2767. and the same contents as *Ls*:
  2768. ```````````````````````````````` example
  2769. 1. A paragraph
  2770. with two lines.
  2771. indented code
  2772. > A block quote.
  2773. .
  2774. <ol>
  2775. <li>
  2776. <p>A paragraph
  2777. with two lines.</p>
  2778. <pre><code>indented code
  2779. </code></pre>
  2780. <blockquote>
  2781. <p>A block quote.</p>
  2782. </blockquote>
  2783. </li>
  2784. </ol>
  2785. ````````````````````````````````
  2786. The most important thing to notice is that the position of
  2787. the text after the list marker determines how much indentation
  2788. is needed in subsequent blocks in the list item. If the list
  2789. marker takes up two spaces, and there are three spaces between
  2790. the list marker and the next [non-whitespace character], then blocks
  2791. must be indented five spaces in order to fall under the list
  2792. item.
  2793. Here are some examples showing how far content must be indented to be
  2794. put under the list item:
  2795. ```````````````````````````````` example
  2796. - one
  2797. two
  2798. .
  2799. <ul>
  2800. <li>one</li>
  2801. </ul>
  2802. <p>two</p>
  2803. ````````````````````````````````
  2804. ```````````````````````````````` example
  2805. - one
  2806. two
  2807. .
  2808. <ul>
  2809. <li>
  2810. <p>one</p>
  2811. <p>two</p>
  2812. </li>
  2813. </ul>
  2814. ````````````````````````````````
  2815. ```````````````````````````````` example
  2816. - one
  2817. two
  2818. .
  2819. <ul>
  2820. <li>one</li>
  2821. </ul>
  2822. <pre><code> two
  2823. </code></pre>
  2824. ````````````````````````````````
  2825. ```````````````````````````````` example
  2826. - one
  2827. two
  2828. .
  2829. <ul>
  2830. <li>
  2831. <p>one</p>
  2832. <p>two</p>
  2833. </li>
  2834. </ul>
  2835. ````````````````````````````````
  2836. It is tempting to think of this in terms of columns: the continuation
  2837. blocks must be indented at least to the column of the first
  2838. [non-whitespace character] after the list marker. However, that is not quite right.
  2839. The spaces after the list marker determine how much relative indentation
  2840. is needed. Which column this indentation reaches will depend on
  2841. how the list item is embedded in other constructions, as shown by
  2842. this example:
  2843. ```````````````````````````````` example
  2844. > > 1. one
  2845. >>
  2846. >> two
  2847. .
  2848. <blockquote>
  2849. <blockquote>
  2850. <ol>
  2851. <li>
  2852. <p>one</p>
  2853. <p>two</p>
  2854. </li>
  2855. </ol>
  2856. </blockquote>
  2857. </blockquote>
  2858. ````````````````````````````````
  2859. Here `two` occurs in the same column as the list marker `1.`,
  2860. but is actually contained in the list item, because there is
  2861. sufficient indentation after the last containing blockquote marker.
  2862. The converse is also possible. In the following example, the word `two`
  2863. occurs far to the right of the initial text of the list item, `one`, but
  2864. it is not considered part of the list item, because it is not indented
  2865. far enough past the blockquote marker:
  2866. ```````````````````````````````` example
  2867. >>- one
  2868. >>
  2869. > > two
  2870. .
  2871. <blockquote>
  2872. <blockquote>
  2873. <ul>
  2874. <li>one</li>
  2875. </ul>
  2876. <p>two</p>
  2877. </blockquote>
  2878. </blockquote>
  2879. ````````````````````````````````
  2880. Note that at least one space is needed between the list marker and
  2881. any following content, so these are not list items:
  2882. ```````````````````````````````` example
  2883. -one
  2884. 2.two
  2885. .
  2886. <p>-one</p>
  2887. <p>2.two</p>
  2888. ````````````````````````````````
  2889. A list item may not contain blocks that are separated by more than
  2890. one blank line. Thus, two blank lines will end a list, unless the
  2891. two blanks are contained in a [fenced code block].
  2892. ```````````````````````````````` example
  2893. - foo
  2894. bar
  2895. - foo
  2896. bar
  2897. - ```
  2898. foo
  2899. bar
  2900. ```
  2901. - baz
  2902. + ```
  2903. foo
  2904. bar
  2905. ```
  2906. .
  2907. <ul>
  2908. <li>
  2909. <p>foo</p>
  2910. <p>bar</p>
  2911. </li>
  2912. <li>
  2913. <p>foo</p>
  2914. </li>
  2915. </ul>
  2916. <p>bar</p>
  2917. <ul>
  2918. <li>
  2919. <pre><code>foo
  2920. bar
  2921. </code></pre>
  2922. </li>
  2923. <li>
  2924. <p>baz</p>
  2925. <ul>
  2926. <li>
  2927. <pre><code>foo
  2928. bar
  2929. </code></pre>
  2930. </li>
  2931. </ul>
  2932. </li>
  2933. </ul>
  2934. ````````````````````````````````
  2935. A list item may contain any kind of block:
  2936. ```````````````````````````````` example
  2937. 1. foo
  2938. ```
  2939. bar
  2940. ```
  2941. baz
  2942. > bam
  2943. .
  2944. <ol>
  2945. <li>
  2946. <p>foo</p>
  2947. <pre><code>bar
  2948. </code></pre>
  2949. <p>baz</p>
  2950. <blockquote>
  2951. <p>bam</p>
  2952. </blockquote>
  2953. </li>
  2954. </ol>
  2955. ````````````````````````````````
  2956. A list item that contains an indented code block will preserve
  2957. empty lines within the code block verbatim, unless there are two
  2958. or more empty lines in a row (since as described above, two
  2959. blank lines end the list):
  2960. ```````````````````````````````` example
  2961. - Foo
  2962. bar
  2963. baz
  2964. .
  2965. <ul>
  2966. <li>
  2967. <p>Foo</p>
  2968. <pre><code>bar
  2969. baz
  2970. </code></pre>
  2971. </li>
  2972. </ul>
  2973. ````````````````````````````````
  2974. ```````````````````````````````` example
  2975. - Foo
  2976. bar
  2977. baz
  2978. .
  2979. <ul>
  2980. <li>
  2981. <p>Foo</p>
  2982. <pre><code>bar
  2983. </code></pre>
  2984. </li>
  2985. </ul>
  2986. <pre><code> baz
  2987. </code></pre>
  2988. ````````````````````````````````
  2989. Note that ordered list start numbers must be nine digits or less:
  2990. ```````````````````````````````` example
  2991. 123456789. ok
  2992. .
  2993. <ol start="123456789">
  2994. <li>ok</li>
  2995. </ol>
  2996. ````````````````````````````````
  2997. ```````````````````````````````` example
  2998. 1234567890. not ok
  2999. .
  3000. <p>1234567890. not ok</p>
  3001. ````````````````````````````````
  3002. A start number may begin with 0s:
  3003. ```````````````````````````````` example
  3004. 0. ok
  3005. .
  3006. <ol start="0">
  3007. <li>ok</li>
  3008. </ol>
  3009. ````````````````````````````````
  3010. ```````````````````````````````` example
  3011. 003. ok
  3012. .
  3013. <ol start="3">
  3014. <li>ok</li>
  3015. </ol>
  3016. ````````````````````````````````
  3017. A start number may not be negative:
  3018. ```````````````````````````````` example
  3019. -1. not ok
  3020. .
  3021. <p>-1. not ok</p>
  3022. ````````````````````````````````
  3023. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3024. constitute a sequence of blocks *Bs* starting with an indented code
  3025. block and not separated from each other by more than one blank line,
  3026. and *M* is a list marker of width *W* followed by
  3027. one space, then the result of prepending *M* and the following
  3028. space to the first line of *Ls*, and indenting subsequent lines of
  3029. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3030. If a line is empty, then it need not be indented. The type of the
  3031. list item (bullet or ordered) is determined by the type of its list
  3032. marker. If the list item is ordered, then it is also assigned a
  3033. start number, based on the ordered list marker.
  3034. An indented code block will have to be indented four spaces beyond
  3035. the edge of the region where text will be included in the list item.
  3036. In the following case that is 6 spaces:
  3037. ```````````````````````````````` example
  3038. - foo
  3039. bar
  3040. .
  3041. <ul>
  3042. <li>
  3043. <p>foo</p>
  3044. <pre><code>bar
  3045. </code></pre>
  3046. </li>
  3047. </ul>
  3048. ````````````````````````````````
  3049. And in this case it is 11 spaces:
  3050. ```````````````````````````````` example
  3051. 10. foo
  3052. bar
  3053. .
  3054. <ol start="10">
  3055. <li>
  3056. <p>foo</p>
  3057. <pre><code>bar
  3058. </code></pre>
  3059. </li>
  3060. </ol>
  3061. ````````````````````````````````
  3062. If the *first* block in the list item is an indented code block,
  3063. then by rule #2, the contents must be indented *one* space after the
  3064. list marker:
  3065. ```````````````````````````````` example
  3066. indented code
  3067. paragraph
  3068. more code
  3069. .
  3070. <pre><code>indented code
  3071. </code></pre>
  3072. <p>paragraph</p>
  3073. <pre><code>more code
  3074. </code></pre>
  3075. ````````````````````````````````
  3076. ```````````````````````````````` example
  3077. 1. indented code
  3078. paragraph
  3079. more code
  3080. .
  3081. <ol>
  3082. <li>
  3083. <pre><code>indented code
  3084. </code></pre>
  3085. <p>paragraph</p>
  3086. <pre><code>more code
  3087. </code></pre>
  3088. </li>
  3089. </ol>
  3090. ````````````````````````````````
  3091. Note that an additional space indent is interpreted as space
  3092. inside the code block:
  3093. ```````````````````````````````` example
  3094. 1. indented code
  3095. paragraph
  3096. more code
  3097. .
  3098. <ol>
  3099. <li>
  3100. <pre><code> indented code
  3101. </code></pre>
  3102. <p>paragraph</p>
  3103. <pre><code>more code
  3104. </code></pre>
  3105. </li>
  3106. </ol>
  3107. ````````````````````````````````
  3108. Note that rules #1 and #2 only apply to two cases: (a) cases
  3109. in which the lines to be included in a list item begin with a
  3110. [non-whitespace character], and (b) cases in which
  3111. they begin with an indented code
  3112. block. In a case like the following, where the first block begins with
  3113. a three-space indent, the rules do not allow us to form a list item by
  3114. indenting the whole thing and prepending a list marker:
  3115. ```````````````````````````````` example
  3116. foo
  3117. bar
  3118. .
  3119. <p>foo</p>
  3120. <p>bar</p>
  3121. ````````````````````````````````
  3122. ```````````````````````````````` example
  3123. - foo
  3124. bar
  3125. .
  3126. <ul>
  3127. <li>foo</li>
  3128. </ul>
  3129. <p>bar</p>
  3130. ````````````````````````````````
  3131. This is not a significant restriction, because when a block begins
  3132. with 1-3 spaces indent, the indentation can always be removed without
  3133. a change in interpretation, allowing rule #1 to be applied. So, in
  3134. the above case:
  3135. ```````````````````````````````` example
  3136. - foo
  3137. bar
  3138. .
  3139. <ul>
  3140. <li>
  3141. <p>foo</p>
  3142. <p>bar</p>
  3143. </li>
  3144. </ul>
  3145. ````````````````````````````````
  3146. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3147. starting with a single [blank line] constitute a (possibly empty)
  3148. sequence of blocks *Bs*, not separated from each other by more than
  3149. one blank line, and *M* is a list marker of width *W*,
  3150. then the result of prepending *M* to the first line of *Ls*, and
  3151. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3152. item with *Bs* as its contents.
  3153. If a line is empty, then it need not be indented. The type of the
  3154. list item (bullet or ordered) is determined by the type of its list
  3155. marker. If the list item is ordered, then it is also assigned a
  3156. start number, based on the ordered list marker.
  3157. Here are some list items that start with a blank line but are not empty:
  3158. ```````````````````````````````` example
  3159. -
  3160. foo
  3161. -
  3162. ```
  3163. bar
  3164. ```
  3165. -
  3166. baz
  3167. .
  3168. <ul>
  3169. <li>foo</li>
  3170. <li>
  3171. <pre><code>bar
  3172. </code></pre>
  3173. </li>
  3174. <li>
  3175. <pre><code>baz
  3176. </code></pre>
  3177. </li>
  3178. </ul>
  3179. ````````````````````````````````
  3180. When the list item starts with a blank line, the number of spaces
  3181. following the list marker doesn't change the required indentation:
  3182. ```````````````````````````````` example
  3183. -
  3184. foo
  3185. .
  3186. <ul>
  3187. <li>foo</li>
  3188. </ul>
  3189. ````````````````````````````````
  3190. A list item can begin with at most one blank line.
  3191. In the following example, `foo` is not part of the list
  3192. item:
  3193. ```````````````````````````````` example
  3194. -
  3195. foo
  3196. .
  3197. <ul>
  3198. <li></li>
  3199. </ul>
  3200. <p>foo</p>
  3201. ````````````````````````````````
  3202. Here is an empty bullet list item:
  3203. ```````````````````````````````` example
  3204. - foo
  3205. -
  3206. - bar
  3207. .
  3208. <ul>
  3209. <li>foo</li>
  3210. <li></li>
  3211. <li>bar</li>
  3212. </ul>
  3213. ````````````````````````````````
  3214. It does not matter whether there are spaces following the [list marker]:
  3215. ```````````````````````````````` example
  3216. - foo
  3217. -
  3218. - bar
  3219. .
  3220. <ul>
  3221. <li>foo</li>
  3222. <li></li>
  3223. <li>bar</li>
  3224. </ul>
  3225. ````````````````````````````````
  3226. Here is an empty ordered list item:
  3227. ```````````````````````````````` example
  3228. 1. foo
  3229. 2.
  3230. 3. bar
  3231. .
  3232. <ol>
  3233. <li>foo</li>
  3234. <li></li>
  3235. <li>bar</li>
  3236. </ol>
  3237. ````````````````````````````````
  3238. A list may start or end with an empty list item:
  3239. ```````````````````````````````` example
  3240. *
  3241. .
  3242. <ul>
  3243. <li></li>
  3244. </ul>
  3245. ````````````````````````````````
  3246. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3247. according to rule #1, #2, or #3, then the result of indenting each line
  3248. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3249. list item with the same contents and attributes. If a line is
  3250. empty, then it need not be indented.
  3251. Indented one space:
  3252. ```````````````````````````````` example
  3253. 1. A paragraph
  3254. with two lines.
  3255. indented code
  3256. > A block quote.
  3257. .
  3258. <ol>
  3259. <li>
  3260. <p>A paragraph
  3261. with two lines.</p>
  3262. <pre><code>indented code
  3263. </code></pre>
  3264. <blockquote>
  3265. <p>A block quote.</p>
  3266. </blockquote>
  3267. </li>
  3268. </ol>
  3269. ````````````````````````````````
  3270. Indented two spaces:
  3271. ```````````````````````````````` example
  3272. 1. A paragraph
  3273. with two lines.
  3274. indented code
  3275. > A block quote.
  3276. .
  3277. <ol>
  3278. <li>
  3279. <p>A paragraph
  3280. with two lines.</p>
  3281. <pre><code>indented code
  3282. </code></pre>
  3283. <blockquote>
  3284. <p>A block quote.</p>
  3285. </blockquote>
  3286. </li>
  3287. </ol>
  3288. ````````````````````````````````
  3289. Indented three spaces:
  3290. ```````````````````````````````` example
  3291. 1. A paragraph
  3292. with two lines.
  3293. indented code
  3294. > A block quote.
  3295. .
  3296. <ol>
  3297. <li>
  3298. <p>A paragraph
  3299. with two lines.</p>
  3300. <pre><code>indented code
  3301. </code></pre>
  3302. <blockquote>
  3303. <p>A block quote.</p>
  3304. </blockquote>
  3305. </li>
  3306. </ol>
  3307. ````````````````````````````````
  3308. Four spaces indent gives a code block:
  3309. ```````````````````````````````` example
  3310. 1. A paragraph
  3311. with two lines.
  3312. indented code
  3313. > A block quote.
  3314. .
  3315. <pre><code>1. A paragraph
  3316. with two lines.
  3317. indented code
  3318. &gt; A block quote.
  3319. </code></pre>
  3320. ````````````````````````````````
  3321. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3322. item](#list-items) with contents *Bs*, then the result of deleting
  3323. some or all of the indentation from one or more lines in which the
  3324. next [non-whitespace character] after the indentation is
  3325. [paragraph continuation text] is a
  3326. list item with the same contents and attributes. The unindented
  3327. lines are called
  3328. [lazy continuation line](@)s.
  3329. Here is an example with [lazy continuation lines]:
  3330. ```````````````````````````````` example
  3331. 1. A paragraph
  3332. with two lines.
  3333. indented code
  3334. > A block quote.
  3335. .
  3336. <ol>
  3337. <li>
  3338. <p>A paragraph
  3339. with two lines.</p>
  3340. <pre><code>indented code
  3341. </code></pre>
  3342. <blockquote>
  3343. <p>A block quote.</p>
  3344. </blockquote>
  3345. </li>
  3346. </ol>
  3347. ````````````````````````````````
  3348. Indentation can be partially deleted:
  3349. ```````````````````````````````` example
  3350. 1. A paragraph
  3351. with two lines.
  3352. .
  3353. <ol>
  3354. <li>A paragraph
  3355. with two lines.</li>
  3356. </ol>
  3357. ````````````````````````````````
  3358. These examples show how laziness can work in nested structures:
  3359. ```````````````````````````````` example
  3360. > 1. > Blockquote
  3361. continued here.
  3362. .
  3363. <blockquote>
  3364. <ol>
  3365. <li>
  3366. <blockquote>
  3367. <p>Blockquote
  3368. continued here.</p>
  3369. </blockquote>
  3370. </li>
  3371. </ol>
  3372. </blockquote>
  3373. ````````````````````````````````
  3374. ```````````````````````````````` example
  3375. > 1. > Blockquote
  3376. > continued here.
  3377. .
  3378. <blockquote>
  3379. <ol>
  3380. <li>
  3381. <blockquote>
  3382. <p>Blockquote
  3383. continued here.</p>
  3384. </blockquote>
  3385. </li>
  3386. </ol>
  3387. </blockquote>
  3388. ````````````````````````````````
  3389. 6. **That's all.** Nothing that is not counted as a list item by rules
  3390. #1--5 counts as a [list item](#list-items).
  3391. The rules for sublists follow from the general rules above. A sublist
  3392. must be indented the same number of spaces a paragraph would need to be
  3393. in order to be included in the list item.
  3394. So, in this case we need two spaces indent:
  3395. ```````````````````````````````` example
  3396. - foo
  3397. - bar
  3398. - baz
  3399. - boo
  3400. .
  3401. <ul>
  3402. <li>foo
  3403. <ul>
  3404. <li>bar
  3405. <ul>
  3406. <li>baz
  3407. <ul>
  3408. <li>boo</li>
  3409. </ul>
  3410. </li>
  3411. </ul>
  3412. </li>
  3413. </ul>
  3414. </li>
  3415. </ul>
  3416. ````````````````````````````````
  3417. One is not enough:
  3418. ```````````````````````````````` example
  3419. - foo
  3420. - bar
  3421. - baz
  3422. - boo
  3423. .
  3424. <ul>
  3425. <li>foo</li>
  3426. <li>bar</li>
  3427. <li>baz</li>
  3428. <li>boo</li>
  3429. </ul>
  3430. ````````````````````````````````
  3431. Here we need four, because the list marker is wider:
  3432. ```````````````````````````````` example
  3433. 10) foo
  3434. - bar
  3435. .
  3436. <ol start="10">
  3437. <li>foo
  3438. <ul>
  3439. <li>bar</li>
  3440. </ul>
  3441. </li>
  3442. </ol>
  3443. ````````````````````````````````
  3444. Three is not enough:
  3445. ```````````````````````````````` example
  3446. 10) foo
  3447. - bar
  3448. .
  3449. <ol start="10">
  3450. <li>foo</li>
  3451. </ol>
  3452. <ul>
  3453. <li>bar</li>
  3454. </ul>
  3455. ````````````````````````````````
  3456. A list may be the first block in a list item:
  3457. ```````````````````````````````` example
  3458. - - foo
  3459. .
  3460. <ul>
  3461. <li>
  3462. <ul>
  3463. <li>foo</li>
  3464. </ul>
  3465. </li>
  3466. </ul>
  3467. ````````````````````````````````
  3468. ```````````````````````````````` example
  3469. 1. - 2. foo
  3470. .
  3471. <ol>
  3472. <li>
  3473. <ul>
  3474. <li>
  3475. <ol start="2">
  3476. <li>foo</li>
  3477. </ol>
  3478. </li>
  3479. </ul>
  3480. </li>
  3481. </ol>
  3482. ````````````````````````````````
  3483. A list item can contain a heading:
  3484. ```````````````````````````````` example
  3485. - # Foo
  3486. - Bar
  3487. ---
  3488. baz
  3489. .
  3490. <ul>
  3491. <li>
  3492. <h1>Foo</h1>
  3493. </li>
  3494. <li>
  3495. <h2>Bar</h2>
  3496. baz</li>
  3497. </ul>
  3498. ````````````````````````````````
  3499. ### Motivation
  3500. John Gruber's Markdown spec says the following about list items:
  3501. 1. "List markers typically start at the left margin, but may be indented
  3502. by up to three spaces. List markers must be followed by one or more
  3503. spaces or a tab."
  3504. 2. "To make lists look nice, you can wrap items with hanging indents....
  3505. But if you don't want to, you don't have to."
  3506. 3. "List items may consist of multiple paragraphs. Each subsequent
  3507. paragraph in a list item must be indented by either 4 spaces or one
  3508. tab."
  3509. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3510. but here again, Markdown will allow you to be lazy."
  3511. 5. "To put a blockquote within a list item, the blockquote's `>`
  3512. delimiters need to be indented."
  3513. 6. "To put a code block within a list item, the code block needs to be
  3514. indented twice — 8 spaces or two tabs."
  3515. These rules specify that a paragraph under a list item must be indented
  3516. four spaces (presumably, from the left margin, rather than the start of
  3517. the list marker, but this is not said), and that code under a list item
  3518. must be indented eight spaces instead of the usual four. They also say
  3519. that a block quote must be indented, but not by how much; however, the
  3520. example given has four spaces indentation. Although nothing is said
  3521. about other kinds of block-level content, it is certainly reasonable to
  3522. infer that *all* block elements under a list item, including other
  3523. lists, must be indented four spaces. This principle has been called the
  3524. *four-space rule*.
  3525. The four-space rule is clear and principled, and if the reference
  3526. implementation `Markdown.pl` had followed it, it probably would have
  3527. become the standard. However, `Markdown.pl` allowed paragraphs and
  3528. sublists to start with only two spaces indentation, at least on the
  3529. outer level. Worse, its behavior was inconsistent: a sublist of an
  3530. outer-level list needed two spaces indentation, but a sublist of this
  3531. sublist needed three spaces. It is not surprising, then, that different
  3532. implementations of Markdown have developed very different rules for
  3533. determining what comes under a list item. (Pandoc and python-Markdown,
  3534. for example, stuck with Gruber's syntax description and the four-space
  3535. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3536. followed `Markdown.pl`'s behavior more closely.)
  3537. Unfortunately, given the divergences between implementations, there
  3538. is no way to give a spec for list items that will be guaranteed not
  3539. to break any existing documents. However, the spec given here should
  3540. correctly handle lists formatted with either the four-space rule or
  3541. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3542. in a way that is natural for a human to read.
  3543. The strategy here is to let the width and indentation of the list marker
  3544. determine the indentation necessary for blocks to fall under the list
  3545. item, rather than having a fixed and arbitrary number. The writer can
  3546. think of the body of the list item as a unit which gets indented to the
  3547. right enough to fit the list marker (and any indentation on the list
  3548. marker). (The laziness rule, #5, then allows continuation lines to be
  3549. unindented if needed.)
  3550. This rule is superior, we claim, to any rule requiring a fixed level of
  3551. indentation from the margin. The four-space rule is clear but
  3552. unnatural. It is quite unintuitive that
  3553. ``` markdown
  3554. - foo
  3555. bar
  3556. - baz
  3557. ```
  3558. should be parsed as two lists with an intervening paragraph,
  3559. ``` html
  3560. <ul>
  3561. <li>foo</li>
  3562. </ul>
  3563. <p>bar</p>
  3564. <ul>
  3565. <li>baz</li>
  3566. </ul>
  3567. ```
  3568. as the four-space rule demands, rather than a single list,
  3569. ``` html
  3570. <ul>
  3571. <li>
  3572. <p>foo</p>
  3573. <p>bar</p>
  3574. <ul>
  3575. <li>baz</li>
  3576. </ul>
  3577. </li>
  3578. </ul>
  3579. ```
  3580. The choice of four spaces is arbitrary. It can be learned, but it is
  3581. not likely to be guessed, and it trips up beginners regularly.
  3582. Would it help to adopt a two-space rule? The problem is that such
  3583. a rule, together with the rule allowing 1--3 spaces indentation of the
  3584. initial list marker, allows text that is indented *less than* the
  3585. original list marker to be included in the list item. For example,
  3586. `Markdown.pl` parses
  3587. ``` markdown
  3588. - one
  3589. two
  3590. ```
  3591. as a single list item, with `two` a continuation paragraph:
  3592. ``` html
  3593. <ul>
  3594. <li>
  3595. <p>one</p>
  3596. <p>two</p>
  3597. </li>
  3598. </ul>
  3599. ```
  3600. and similarly
  3601. ``` markdown
  3602. > - one
  3603. >
  3604. > two
  3605. ```
  3606. as
  3607. ``` html
  3608. <blockquote>
  3609. <ul>
  3610. <li>
  3611. <p>one</p>
  3612. <p>two</p>
  3613. </li>
  3614. </ul>
  3615. </blockquote>
  3616. ```
  3617. This is extremely unintuitive.
  3618. Rather than requiring a fixed indent from the margin, we could require
  3619. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3620. may itself be indented). This proposal would remove the last anomaly
  3621. discussed. Unlike the spec presented above, it would count the following
  3622. as a list item with a subparagraph, even though the paragraph `bar`
  3623. is not indented as far as the first paragraph `foo`:
  3624. ``` markdown
  3625. 10. foo
  3626. bar
  3627. ```
  3628. Arguably this text does read like a list item with `bar` as a subparagraph,
  3629. which may count in favor of the proposal. However, on this proposal indented
  3630. code would have to be indented six spaces after the list marker. And this
  3631. would break a lot of existing Markdown, which has the pattern:
  3632. ``` markdown
  3633. 1. foo
  3634. indented code
  3635. ```
  3636. where the code is indented eight spaces. The spec above, by contrast, will
  3637. parse this text as expected, since the code block's indentation is measured
  3638. from the beginning of `foo`.
  3639. The one case that needs special treatment is a list item that *starts*
  3640. with indented code. How much indentation is required in that case, since
  3641. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3642. that in such cases, we require one space indentation from the list marker
  3643. (and then the normal four spaces for the indented code). This will match the
  3644. four-space rule in cases where the list marker plus its initial indentation
  3645. takes four spaces (a common case), but diverge in other cases.
  3646. ## Lists
  3647. A [list](@) is a sequence of one or more
  3648. list items [of the same type]. The list items
  3649. may be separated by single [blank lines], but two
  3650. blank lines end all containing lists.
  3651. Two list items are [of the same type](@)
  3652. if they begin with a [list marker] of the same type.
  3653. Two list markers are of the
  3654. same type if (a) they are bullet list markers using the same character
  3655. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3656. delimiter (either `.` or `)`).
  3657. A list is an [ordered list](@)
  3658. if its constituent list items begin with
  3659. [ordered list markers], and a
  3660. [bullet list](@) if its constituent list
  3661. items begin with [bullet list markers].
  3662. The [start number](@)
  3663. of an [ordered list] is determined by the list number of
  3664. its initial list item. The numbers of subsequent list items are
  3665. disregarded.
  3666. A list is [loose](@) if any of its constituent
  3667. list items are separated by blank lines, or if any of its constituent
  3668. list items directly contain two block-level elements with a blank line
  3669. between them. Otherwise a list is [tight](@).
  3670. (The difference in HTML output is that paragraphs in a loose list are
  3671. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3672. Changing the bullet or ordered list delimiter starts a new list:
  3673. ```````````````````````````````` example
  3674. - foo
  3675. - bar
  3676. + baz
  3677. .
  3678. <ul>
  3679. <li>foo</li>
  3680. <li>bar</li>
  3681. </ul>
  3682. <ul>
  3683. <li>baz</li>
  3684. </ul>
  3685. ````````````````````````````````
  3686. ```````````````````````````````` example
  3687. 1. foo
  3688. 2. bar
  3689. 3) baz
  3690. .
  3691. <ol>
  3692. <li>foo</li>
  3693. <li>bar</li>
  3694. </ol>
  3695. <ol start="3">
  3696. <li>baz</li>
  3697. </ol>
  3698. ````````````````````````````````
  3699. In CommonMark, a list can interrupt a paragraph. That is,
  3700. no blank line is needed to separate a paragraph from a following
  3701. list:
  3702. ```````````````````````````````` example
  3703. Foo
  3704. - bar
  3705. - baz
  3706. .
  3707. <p>Foo</p>
  3708. <ul>
  3709. <li>bar</li>
  3710. <li>baz</li>
  3711. </ul>
  3712. ````````````````````````````````
  3713. `Markdown.pl` does not allow this, through fear of triggering a list
  3714. via a numeral in a hard-wrapped line:
  3715. ```````````````````````````````` example
  3716. The number of windows in my house is
  3717. 14. The number of doors is 6.
  3718. .
  3719. <p>The number of windows in my house is</p>
  3720. <ol start="14">
  3721. <li>The number of doors is 6.</li>
  3722. </ol>
  3723. ````````````````````````````````
  3724. Oddly, `Markdown.pl` *does* allow a blockquote to interrupt a paragraph,
  3725. even though the same considerations might apply. We think that the two
  3726. cases should be treated the same. Here are two reasons for allowing
  3727. lists to interrupt paragraphs:
  3728. First, it is natural and not uncommon for people to start lists without
  3729. blank lines:
  3730. I need to buy
  3731. - new shoes
  3732. - a coat
  3733. - a plane ticket
  3734. Second, we are attracted to a
  3735. > [principle of uniformity](@):
  3736. > if a chunk of text has a certain
  3737. > meaning, it will continue to have the same meaning when put into a
  3738. > container block (such as a list item or blockquote).
  3739. (Indeed, the spec for [list items] and [block quotes] presupposes
  3740. this principle.) This principle implies that if
  3741. * I need to buy
  3742. - new shoes
  3743. - a coat
  3744. - a plane ticket
  3745. is a list item containing a paragraph followed by a nested sublist,
  3746. as all Markdown implementations agree it is (though the paragraph
  3747. may be rendered without `<p>` tags, since the list is "tight"),
  3748. then
  3749. I need to buy
  3750. - new shoes
  3751. - a coat
  3752. - a plane ticket
  3753. by itself should be a paragraph followed by a nested sublist.
  3754. Our adherence to the [principle of uniformity]
  3755. thus inclines us to think that there are two coherent packages:
  3756. 1. Require blank lines before *all* lists and blockquotes,
  3757. including lists that occur as sublists inside other list items.
  3758. 2. Require blank lines in none of these places.
  3759. [reStructuredText](http://docutils.sourceforge.net/rst.html) takes
  3760. the first approach, for which there is much to be said. But the second
  3761. seems more consistent with established practice with Markdown.
  3762. There can be blank lines between items, but two blank lines end
  3763. a list:
  3764. ```````````````````````````````` example
  3765. - foo
  3766. - bar
  3767. - baz
  3768. .
  3769. <ul>
  3770. <li>
  3771. <p>foo</p>
  3772. </li>
  3773. <li>
  3774. <p>bar</p>
  3775. </li>
  3776. </ul>
  3777. <ul>
  3778. <li>baz</li>
  3779. </ul>
  3780. ````````````````````````````````
  3781. As illustrated above in the section on [list items],
  3782. two blank lines between blocks *within* a list item will also end a
  3783. list:
  3784. ```````````````````````````````` example
  3785. - foo
  3786. bar
  3787. - baz
  3788. .
  3789. <ul>
  3790. <li>foo</li>
  3791. </ul>
  3792. <p>bar</p>
  3793. <ul>
  3794. <li>baz</li>
  3795. </ul>
  3796. ````````````````````````````````
  3797. Indeed, two blank lines will end *all* containing lists:
  3798. ```````````````````````````````` example
  3799. - foo
  3800. - bar
  3801. - baz
  3802. bim
  3803. .
  3804. <ul>
  3805. <li>foo
  3806. <ul>
  3807. <li>bar
  3808. <ul>
  3809. <li>baz</li>
  3810. </ul>
  3811. </li>
  3812. </ul>
  3813. </li>
  3814. </ul>
  3815. <pre><code> bim
  3816. </code></pre>
  3817. ````````````````````````````````
  3818. Thus, two blank lines can be used to separate consecutive lists of
  3819. the same type, or to separate a list from an indented code block
  3820. that would otherwise be parsed as a subparagraph of the final list
  3821. item:
  3822. ```````````````````````````````` example
  3823. - foo
  3824. - bar
  3825. - baz
  3826. - bim
  3827. .
  3828. <ul>
  3829. <li>foo</li>
  3830. <li>bar</li>
  3831. </ul>
  3832. <ul>
  3833. <li>baz</li>
  3834. <li>bim</li>
  3835. </ul>
  3836. ````````````````````````````````
  3837. ```````````````````````````````` example
  3838. - foo
  3839. notcode
  3840. - foo
  3841. code
  3842. .
  3843. <ul>
  3844. <li>
  3845. <p>foo</p>
  3846. <p>notcode</p>
  3847. </li>
  3848. <li>
  3849. <p>foo</p>
  3850. </li>
  3851. </ul>
  3852. <pre><code>code
  3853. </code></pre>
  3854. ````````````````````````````````
  3855. List items need not be indented to the same level. The following
  3856. list items will be treated as items at the same list level,
  3857. since none is indented enough to belong to the previous list
  3858. item:
  3859. ```````````````````````````````` example
  3860. - a
  3861. - b
  3862. - c
  3863. - d
  3864. - e
  3865. - f
  3866. - g
  3867. - h
  3868. - i
  3869. .
  3870. <ul>
  3871. <li>a</li>
  3872. <li>b</li>
  3873. <li>c</li>
  3874. <li>d</li>
  3875. <li>e</li>
  3876. <li>f</li>
  3877. <li>g</li>
  3878. <li>h</li>
  3879. <li>i</li>
  3880. </ul>
  3881. ````````````````````````````````
  3882. ```````````````````````````````` example
  3883. 1. a
  3884. 2. b
  3885. 3. c
  3886. .
  3887. <ol>
  3888. <li>
  3889. <p>a</p>
  3890. </li>
  3891. <li>
  3892. <p>b</p>
  3893. </li>
  3894. <li>
  3895. <p>c</p>
  3896. </li>
  3897. </ol>
  3898. ````````````````````````````````
  3899. This is a loose list, because there is a blank line between
  3900. two of the list items:
  3901. ```````````````````````````````` example
  3902. - a
  3903. - b
  3904. - c
  3905. .
  3906. <ul>
  3907. <li>
  3908. <p>a</p>
  3909. </li>
  3910. <li>
  3911. <p>b</p>
  3912. </li>
  3913. <li>
  3914. <p>c</p>
  3915. </li>
  3916. </ul>
  3917. ````````````````````````````````
  3918. So is this, with a empty second item:
  3919. ```````````````````````````````` example
  3920. * a
  3921. *
  3922. * c
  3923. .
  3924. <ul>
  3925. <li>
  3926. <p>a</p>
  3927. </li>
  3928. <li></li>
  3929. <li>
  3930. <p>c</p>
  3931. </li>
  3932. </ul>
  3933. ````````````````````````````````
  3934. These are loose lists, even though there is no space between the items,
  3935. because one of the items directly contains two block-level elements
  3936. with a blank line between them:
  3937. ```````````````````````````````` example
  3938. - a
  3939. - b
  3940. c
  3941. - d
  3942. .
  3943. <ul>
  3944. <li>
  3945. <p>a</p>
  3946. </li>
  3947. <li>
  3948. <p>b</p>
  3949. <p>c</p>
  3950. </li>
  3951. <li>
  3952. <p>d</p>
  3953. </li>
  3954. </ul>
  3955. ````````````````````````````````
  3956. ```````````````````````````````` example
  3957. - a
  3958. - b
  3959. [ref]: /url
  3960. - d
  3961. .
  3962. <ul>
  3963. <li>
  3964. <p>a</p>
  3965. </li>
  3966. <li>
  3967. <p>b</p>
  3968. </li>
  3969. <li>
  3970. <p>d</p>
  3971. </li>
  3972. </ul>
  3973. ````````````````````````````````
  3974. This is a tight list, because the blank lines are in a code block:
  3975. ```````````````````````````````` example
  3976. - a
  3977. - ```
  3978. b
  3979. ```
  3980. - c
  3981. .
  3982. <ul>
  3983. <li>a</li>
  3984. <li>
  3985. <pre><code>b
  3986. </code></pre>
  3987. </li>
  3988. <li>c</li>
  3989. </ul>
  3990. ````````````````````````````````
  3991. This is a tight list, because the blank line is between two
  3992. paragraphs of a sublist. So the sublist is loose while
  3993. the outer list is tight:
  3994. ```````````````````````````````` example
  3995. - a
  3996. - b
  3997. c
  3998. - d
  3999. .
  4000. <ul>
  4001. <li>a
  4002. <ul>
  4003. <li>
  4004. <p>b</p>
  4005. <p>c</p>
  4006. </li>
  4007. </ul>
  4008. </li>
  4009. <li>d</li>
  4010. </ul>
  4011. ````````````````````````````````
  4012. This is a tight list, because the blank line is inside the
  4013. block quote:
  4014. ```````````````````````````````` example
  4015. * a
  4016. > b
  4017. >
  4018. * c
  4019. .
  4020. <ul>
  4021. <li>a
  4022. <blockquote>
  4023. <p>b</p>
  4024. </blockquote>
  4025. </li>
  4026. <li>c</li>
  4027. </ul>
  4028. ````````````````````````````````
  4029. This list is tight, because the consecutive block elements
  4030. are not separated by blank lines:
  4031. ```````````````````````````````` example
  4032. - a
  4033. > b
  4034. ```
  4035. c
  4036. ```
  4037. - d
  4038. .
  4039. <ul>
  4040. <li>a
  4041. <blockquote>
  4042. <p>b</p>
  4043. </blockquote>
  4044. <pre><code>c
  4045. </code></pre>
  4046. </li>
  4047. <li>d</li>
  4048. </ul>
  4049. ````````````````````````````````
  4050. A single-paragraph list is tight:
  4051. ```````````````````````````````` example
  4052. - a
  4053. .
  4054. <ul>
  4055. <li>a</li>
  4056. </ul>
  4057. ````````````````````````````````
  4058. ```````````````````````````````` example
  4059. - a
  4060. - b
  4061. .
  4062. <ul>
  4063. <li>a
  4064. <ul>
  4065. <li>b</li>
  4066. </ul>
  4067. </li>
  4068. </ul>
  4069. ````````````````````````````````
  4070. This list is loose, because of the blank line between the
  4071. two block elements in the list item:
  4072. ```````````````````````````````` example
  4073. 1. ```
  4074. foo
  4075. ```
  4076. bar
  4077. .
  4078. <ol>
  4079. <li>
  4080. <pre><code>foo
  4081. </code></pre>
  4082. <p>bar</p>
  4083. </li>
  4084. </ol>
  4085. ````````````````````````````````
  4086. Here the outer list is loose, the inner list tight:
  4087. ```````````````````````````````` example
  4088. * foo
  4089. * bar
  4090. baz
  4091. .
  4092. <ul>
  4093. <li>
  4094. <p>foo</p>
  4095. <ul>
  4096. <li>bar</li>
  4097. </ul>
  4098. <p>baz</p>
  4099. </li>
  4100. </ul>
  4101. ````````````````````````````````
  4102. ```````````````````````````````` example
  4103. - a
  4104. - b
  4105. - c
  4106. - d
  4107. - e
  4108. - f
  4109. .
  4110. <ul>
  4111. <li>
  4112. <p>a</p>
  4113. <ul>
  4114. <li>b</li>
  4115. <li>c</li>
  4116. </ul>
  4117. </li>
  4118. <li>
  4119. <p>d</p>
  4120. <ul>
  4121. <li>e</li>
  4122. <li>f</li>
  4123. </ul>
  4124. </li>
  4125. </ul>
  4126. ````````````````````````````````
  4127. # Inlines
  4128. Inlines are parsed sequentially from the beginning of the character
  4129. stream to the end (left to right, in left-to-right languages).
  4130. Thus, for example, in
  4131. ```````````````````````````````` example
  4132. `hi`lo`
  4133. .
  4134. <p><code>hi</code>lo`</p>
  4135. ````````````````````````````````
  4136. `hi` is parsed as code, leaving the backtick at the end as a literal
  4137. backtick.
  4138. ## Backslash escapes
  4139. Any ASCII punctuation character may be backslash-escaped:
  4140. ```````````````````````````````` example
  4141. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4142. .
  4143. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4144. ````````````````````````````````
  4145. Backslashes before other characters are treated as literal
  4146. backslashes:
  4147. ```````````````````````````````` example
  4148. \→\A\a\ \3\φ\«
  4149. .
  4150. <p>\→\A\a\ \3\φ\«</p>
  4151. ````````````````````````````````
  4152. Escaped characters are treated as regular characters and do
  4153. not have their usual Markdown meanings:
  4154. ```````````````````````````````` example
  4155. \*not emphasized*
  4156. \<br/> not a tag
  4157. \[not a link](/foo)
  4158. \`not code`
  4159. 1\. not a list
  4160. \* not a list
  4161. \# not a heading
  4162. \[foo]: /url "not a reference"
  4163. .
  4164. <p>*not emphasized*
  4165. &lt;br/&gt; not a tag
  4166. [not a link](/foo)
  4167. `not code`
  4168. 1. not a list
  4169. * not a list
  4170. # not a heading
  4171. [foo]: /url &quot;not a reference&quot;</p>
  4172. ````````````````````````````````
  4173. If a backslash is itself escaped, the following character is not:
  4174. ```````````````````````````````` example
  4175. \\*emphasis*
  4176. .
  4177. <p>\<em>emphasis</em></p>
  4178. ````````````````````````````````
  4179. A backslash at the end of the line is a [hard line break]:
  4180. ```````````````````````````````` example
  4181. foo\
  4182. bar
  4183. .
  4184. <p>foo<br />
  4185. bar</p>
  4186. ````````````````````````````````
  4187. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4188. raw HTML:
  4189. ```````````````````````````````` example
  4190. `` \[\` ``
  4191. .
  4192. <p><code>\[\`</code></p>
  4193. ````````````````````````````````
  4194. ```````````````````````````````` example
  4195. \[\]
  4196. .
  4197. <pre><code>\[\]
  4198. </code></pre>
  4199. ````````````````````````````````
  4200. ```````````````````````````````` example
  4201. ~~~
  4202. \[\]
  4203. ~~~
  4204. .
  4205. <pre><code>\[\]
  4206. </code></pre>
  4207. ````````````````````````````````
  4208. ```````````````````````````````` example
  4209. <http://example.com?find=\*>
  4210. .
  4211. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4212. ````````````````````````````````
  4213. ```````````````````````````````` example
  4214. <a href="/bar\/)">
  4215. .
  4216. <a href="/bar\/)">
  4217. ````````````````````````````````
  4218. But they work in all other contexts, including URLs and link titles,
  4219. link references, and [info strings] in [fenced code blocks]:
  4220. ```````````````````````````````` example
  4221. [foo](/bar\* "ti\*tle")
  4222. .
  4223. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4224. ````````````````````````````````
  4225. ```````````````````````````````` example
  4226. [foo]
  4227. [foo]: /bar\* "ti\*tle"
  4228. .
  4229. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4230. ````````````````````````````````
  4231. ```````````````````````````````` example
  4232. ``` foo\+bar
  4233. foo
  4234. ```
  4235. .
  4236. <pre><code class="language-foo+bar">foo
  4237. </code></pre>
  4238. ````````````````````````````````
  4239. ## Entity and numeric character references
  4240. All valid HTML entity references and numeric character
  4241. references, except those occuring in code blocks and code spans,
  4242. are recognized as such and treated as equivalent to the
  4243. corresponding Unicode characters. Conforming CommonMark parsers
  4244. need not store information about whether a particular character
  4245. was represented in the source using a Unicode character or
  4246. an entity reference.
  4247. [Entity references](@) consist of `&` + any of the valid
  4248. HTML5 entity names + `;`. The
  4249. document <https://html.spec.whatwg.org/multipage/entities.json>
  4250. is used as an authoritative source for the valid entity
  4251. references and their corresponding code points.
  4252. ```````````````````````````````` example
  4253. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4254. &frac34; &HilbertSpace; &DifferentialD;
  4255. &ClockwiseContourIntegral; &ngE;
  4256. .
  4257. <p>  &amp; © Æ Ď
  4258. ¾ ℋ ⅆ
  4259. ∲ ≧̸</p>
  4260. ````````````````````````````````
  4261. [Decimal numeric character
  4262. references](@)
  4263. consist of `&#` + a string of 1--8 arabic digits + `;`. A
  4264. numeric character reference is parsed as the corresponding
  4265. Unicode character. Invalid Unicode code points will be replaced by
  4266. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4267. the code point `U+0000` will also be replaced by `U+FFFD`.
  4268. ```````````````````````````````` example
  4269. &#35; &#1234; &#992; &#98765432; &#0;
  4270. .
  4271. <p># Ӓ Ϡ � �</p>
  4272. ````````````````````````````````
  4273. [Hexadecimal numeric character
  4274. references](@) consist of `&#` +
  4275. either `X` or `x` + a string of 1-8 hexadecimal digits + `;`.
  4276. They too are parsed as the corresponding Unicode character (this
  4277. time specified with a hexadecimal numeral instead of decimal).
  4278. ```````````````````````````````` example
  4279. &#X22; &#XD06; &#xcab;
  4280. .
  4281. <p>&quot; ആ ಫ</p>
  4282. ````````````````````````````````
  4283. Here are some nonentities:
  4284. ```````````````````````````````` example
  4285. &nbsp &x; &#; &#x;
  4286. &ThisIsNotDefined; &hi?;
  4287. .
  4288. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4289. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4290. ````````````````````````````````
  4291. Although HTML5 does accept some entity references
  4292. without a trailing semicolon (such as `&copy`), these are not
  4293. recognized here, because it makes the grammar too ambiguous:
  4294. ```````````````````````````````` example
  4295. &copy
  4296. .
  4297. <p>&amp;copy</p>
  4298. ````````````````````````````````
  4299. Strings that are not on the list of HTML5 named entities are not
  4300. recognized as entity references either:
  4301. ```````````````````````````````` example
  4302. &MadeUpEntity;
  4303. .
  4304. <p>&amp;MadeUpEntity;</p>
  4305. ````````````````````````````````
  4306. Entity and numeric character references are recognized in any
  4307. context besides code spans or code blocks, including
  4308. URLs, [link titles], and [fenced code block][] [info strings]:
  4309. ```````````````````````````````` example
  4310. <a href="&ouml;&ouml;.html">
  4311. .
  4312. <a href="&ouml;&ouml;.html">
  4313. ````````````````````````````````
  4314. ```````````````````````````````` example
  4315. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4316. .
  4317. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4318. ````````````````````````````````
  4319. ```````````````````````````````` example
  4320. [foo]
  4321. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4322. .
  4323. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4324. ````````````````````````````````
  4325. ```````````````````````````````` example
  4326. ``` f&ouml;&ouml;
  4327. foo
  4328. ```
  4329. .
  4330. <pre><code class="language-föö">foo
  4331. </code></pre>
  4332. ````````````````````````````````
  4333. Entity and numeric character references are treated as literal
  4334. text in code spans and code blocks:
  4335. ```````````````````````````````` example
  4336. `f&ouml;&ouml;`
  4337. .
  4338. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4339. ````````````````````````````````
  4340. ```````````````````````````````` example
  4341. f&ouml;f&ouml;
  4342. .
  4343. <pre><code>f&amp;ouml;f&amp;ouml;
  4344. </code></pre>
  4345. ````````````````````````````````
  4346. ## Code spans
  4347. A [backtick string](@)
  4348. is a string of one or more backtick characters (`` ` ``) that is neither
  4349. preceded nor followed by a backtick.
  4350. A [code span](@) begins with a backtick string and ends with
  4351. a backtick string of equal length. The contents of the code span are
  4352. the characters between the two backtick strings, with leading and
  4353. trailing spaces and [line endings] removed, and
  4354. [whitespace] collapsed to single spaces.
  4355. This is a simple code span:
  4356. ```````````````````````````````` example
  4357. `foo`
  4358. .
  4359. <p><code>foo</code></p>
  4360. ````````````````````````````````
  4361. Here two backticks are used, because the code contains a backtick.
  4362. This example also illustrates stripping of leading and trailing spaces:
  4363. ```````````````````````````````` example
  4364. `` foo ` bar ``
  4365. .
  4366. <p><code>foo ` bar</code></p>
  4367. ````````````````````````````````
  4368. This example shows the motivation for stripping leading and trailing
  4369. spaces:
  4370. ```````````````````````````````` example
  4371. ` `` `
  4372. .
  4373. <p><code>``</code></p>
  4374. ````````````````````````````````
  4375. [Line endings] are treated like spaces:
  4376. ```````````````````````````````` example
  4377. ``
  4378. foo
  4379. ``
  4380. .
  4381. <p><code>foo</code></p>
  4382. ````````````````````````````````
  4383. Interior spaces and [line endings] are collapsed into
  4384. single spaces, just as they would be by a browser:
  4385. ```````````````````````````````` example
  4386. `foo bar
  4387. baz`
  4388. .
  4389. <p><code>foo bar baz</code></p>
  4390. ````````````````````````````````
  4391. Q: Why not just leave the spaces, since browsers will collapse them
  4392. anyway? A: Because we might be targeting a non-HTML format, and we
  4393. shouldn't rely on HTML-specific rendering assumptions.
  4394. (Existing implementations differ in their treatment of internal
  4395. spaces and [line endings]. Some, including `Markdown.pl` and
  4396. `showdown`, convert an internal [line ending] into a
  4397. `<br />` tag. But this makes things difficult for those who like to
  4398. hard-wrap their paragraphs, since a line break in the midst of a code
  4399. span will cause an unintended line break in the output. Others just
  4400. leave internal spaces as they are, which is fine if only HTML is being
  4401. targeted.)
  4402. ```````````````````````````````` example
  4403. `foo `` bar`
  4404. .
  4405. <p><code>foo `` bar</code></p>
  4406. ````````````````````````````````
  4407. Note that backslash escapes do not work in code spans. All backslashes
  4408. are treated literally:
  4409. ```````````````````````````````` example
  4410. `foo\`bar`
  4411. .
  4412. <p><code>foo\</code>bar`</p>
  4413. ````````````````````````````````
  4414. Backslash escapes are never needed, because one can always choose a
  4415. string of *n* backtick characters as delimiters, where the code does
  4416. not contain any strings of exactly *n* backtick characters.
  4417. Code span backticks have higher precedence than any other inline
  4418. constructs except HTML tags and autolinks. Thus, for example, this is
  4419. not parsed as emphasized text, since the second `*` is part of a code
  4420. span:
  4421. ```````````````````````````````` example
  4422. *foo`*`
  4423. .
  4424. <p>*foo<code>*</code></p>
  4425. ````````````````````````````````
  4426. And this is not parsed as a link:
  4427. ```````````````````````````````` example
  4428. [not a `link](/foo`)
  4429. .
  4430. <p>[not a <code>link](/foo</code>)</p>
  4431. ````````````````````````````````
  4432. Code spans, HTML tags, and autolinks have the same precedence.
  4433. Thus, this is code:
  4434. ```````````````````````````````` example
  4435. `<a href="`">`
  4436. .
  4437. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4438. ````````````````````````````````
  4439. But this is an HTML tag:
  4440. ```````````````````````````````` example
  4441. <a href="`">`
  4442. .
  4443. <p><a href="`">`</p>
  4444. ````````````````````````````````
  4445. And this is code:
  4446. ```````````````````````````````` example
  4447. `<http://foo.bar.`baz>`
  4448. .
  4449. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4450. ````````````````````````````````
  4451. But this is an autolink:
  4452. ```````````````````````````````` example
  4453. <http://foo.bar.`baz>`
  4454. .
  4455. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4456. ````````````````````````````````
  4457. When a backtick string is not closed by a matching backtick string,
  4458. we just have literal backticks:
  4459. ```````````````````````````````` example
  4460. ```foo``
  4461. .
  4462. <p>```foo``</p>
  4463. ````````````````````````````````
  4464. ```````````````````````````````` example
  4465. `foo
  4466. .
  4467. <p>`foo</p>
  4468. ````````````````````````````````
  4469. ## Emphasis and strong emphasis
  4470. John Gruber's original [Markdown syntax
  4471. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4472. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4473. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4474. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4475. > tag.
  4476. This is enough for most users, but these rules leave much undecided,
  4477. especially when it comes to nested emphasis. The original
  4478. `Markdown.pl` test suite makes it clear that triple `***` and
  4479. `___` delimiters can be used for strong emphasis, and most
  4480. implementations have also allowed the following patterns:
  4481. ``` markdown
  4482. ***strong emph***
  4483. ***strong** in emph*
  4484. ***emph* in strong**
  4485. **in strong *emph***
  4486. *in emph **strong***
  4487. ```
  4488. The following patterns are less widely supported, but the intent
  4489. is clear and they are useful (especially in contexts like bibliography
  4490. entries):
  4491. ``` markdown
  4492. *emph *with emph* in it*
  4493. **strong **with strong** in it**
  4494. ```
  4495. Many implementations have also restricted intraword emphasis to
  4496. the `*` forms, to avoid unwanted emphasis in words containing
  4497. internal underscores. (It is best practice to put these in code
  4498. spans, but users often do not.)
  4499. ``` markdown
  4500. internal emphasis: foo*bar*baz
  4501. no emphasis: foo_bar_baz
  4502. ```
  4503. The rules given below capture all of these patterns, while allowing
  4504. for efficient parsing strategies that do not backtrack.
  4505. First, some definitions. A [delimiter run](@) is either
  4506. a sequence of one or more `*` characters that is not preceded or
  4507. followed by a `*` character, or a sequence of one or more `_`
  4508. characters that is not preceded or followed by a `_` character.
  4509. A [left-flanking delimiter run](@) is
  4510. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4511. and (b) either not followed by a [punctuation character], or
  4512. preceded by [Unicode whitespace] or a [punctuation character].
  4513. For purposes of this definition, the beginning and the end of
  4514. the line count as Unicode whitespace.
  4515. A [right-flanking delimiter run](@) is
  4516. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4517. and (b) either not preceded by a [punctuation character], or
  4518. followed by [Unicode whitespace] or a [punctuation character].
  4519. For purposes of this definition, the beginning and the end of
  4520. the line count as Unicode whitespace.
  4521. Here are some examples of delimiter runs.
  4522. - left-flanking but not right-flanking:
  4523. ```
  4524. ***abc
  4525. _abc
  4526. **"abc"
  4527. _"abc"
  4528. ```
  4529. - right-flanking but not left-flanking:
  4530. ```
  4531. abc***
  4532. abc_
  4533. "abc"**
  4534. "abc"_
  4535. ```
  4536. - Both left and right-flanking:
  4537. ```
  4538. abc***def
  4539. "abc"_"def"
  4540. ```
  4541. - Neither left nor right-flanking:
  4542. ```
  4543. abc *** def
  4544. a _ b
  4545. ```
  4546. (The idea of distinguishing left-flanking and right-flanking
  4547. delimiter runs based on the character before and the character
  4548. after comes from Roopesh Chander's
  4549. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4550. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4551. run," and its rules for distinguishing left- and right-flanking runs
  4552. are a bit more complex than the ones given here.)
  4553. The following rules define emphasis and strong emphasis:
  4554. 1. A single `*` character [can open emphasis](@)
  4555. iff (if and only if) it is part of a [left-flanking delimiter run].
  4556. 2. A single `_` character [can open emphasis] iff
  4557. it is part of a [left-flanking delimiter run]
  4558. and either (a) not part of a [right-flanking delimiter run]
  4559. or (b) part of a [right-flanking delimiter run]
  4560. preceded by punctuation.
  4561. 3. A single `*` character [can close emphasis](@)
  4562. iff it is part of a [right-flanking delimiter run].
  4563. 4. A single `_` character [can close emphasis] iff
  4564. it is part of a [right-flanking delimiter run]
  4565. and either (a) not part of a [left-flanking delimiter run]
  4566. or (b) part of a [left-flanking delimiter run]
  4567. followed by punctuation.
  4568. 5. A double `**` [can open strong emphasis](@)
  4569. iff it is part of a [left-flanking delimiter run].
  4570. 6. A double `__` [can open strong emphasis] iff
  4571. it is part of a [left-flanking delimiter run]
  4572. and either (a) not part of a [right-flanking delimiter run]
  4573. or (b) part of a [right-flanking delimiter run]
  4574. preceded by punctuation.
  4575. 7. A double `**` [can close strong emphasis](@)
  4576. iff it is part of a [right-flanking delimiter run].
  4577. 8. A double `__` [can close strong emphasis]
  4578. it is part of a [right-flanking delimiter run]
  4579. and either (a) not part of a [left-flanking delimiter run]
  4580. or (b) part of a [left-flanking delimiter run]
  4581. followed by punctuation.
  4582. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4583. with a delimiter that [can close emphasis], and that uses the same
  4584. character (`_` or `*`) as the opening delimiter. The
  4585. opening and closing delimiters must belong to separate
  4586. [delimiter runs]. If one of the delimiters can both
  4587. open and close emphasis, then the sum of the lengths of the
  4588. delimiter runs containing the opening and closing delimiters
  4589. must not be a multiple of 3.
  4590. 10. Strong emphasis begins with a delimiter that
  4591. [can open strong emphasis] and ends with a delimiter that
  4592. [can close strong emphasis], and that uses the same character
  4593. (`_` or `*`) as the opening delimiter. The
  4594. opening and closing delimiters must belong to separate
  4595. [delimiter runs]. If one of the delimiters can both open
  4596. and close strong emphasis, then the sum of the lengths of
  4597. the delimiter runs containing the opening and closing
  4598. delimiters must not be a multiple of 3.
  4599. 11. A literal `*` character cannot occur at the beginning or end of
  4600. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4601. is backslash-escaped.
  4602. 12. A literal `_` character cannot occur at the beginning or end of
  4603. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4604. is backslash-escaped.
  4605. Where rules 1--12 above are compatible with multiple parsings,
  4606. the following principles resolve ambiguity:
  4607. 13. The number of nestings should be minimized. Thus, for example,
  4608. an interpretation `<strong>...</strong>` is always preferred to
  4609. `<em><em>...</em></em>`.
  4610. 14. An interpretation `<strong><em>...</em></strong>` is always
  4611. preferred to `<em><strong>..</strong></em>`.
  4612. 15. When two potential emphasis or strong emphasis spans overlap,
  4613. so that the second begins before the first ends and ends after
  4614. the first ends, the first takes precedence. Thus, for example,
  4615. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4616. than `*foo <em>bar* baz</em>`.
  4617. 16. When there are two potential emphasis or strong emphasis spans
  4618. with the same closing delimiter, the shorter one (the one that
  4619. opens later) takes precedence. Thus, for example,
  4620. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4621. rather than `<strong>foo **bar baz</strong>`.
  4622. 17. Inline code spans, links, images, and HTML tags group more tightly
  4623. than emphasis. So, when there is a choice between an interpretation
  4624. that contains one of these elements and one that does not, the
  4625. former always wins. Thus, for example, `*[foo*](bar)` is
  4626. parsed as `*<a href="bar">foo*</a>` rather than as
  4627. `<em>[foo</em>](bar)`.
  4628. These rules can be illustrated through a series of examples.
  4629. Rule 1:
  4630. ```````````````````````````````` example
  4631. *foo bar*
  4632. .
  4633. <p><em>foo bar</em></p>
  4634. ````````````````````````````````
  4635. This is not emphasis, because the opening `*` is followed by
  4636. whitespace, and hence not part of a [left-flanking delimiter run]:
  4637. ```````````````````````````````` example
  4638. a * foo bar*
  4639. .
  4640. <p>a * foo bar*</p>
  4641. ````````````````````````````````
  4642. This is not emphasis, because the opening `*` is preceded
  4643. by an alphanumeric and followed by punctuation, and hence
  4644. not part of a [left-flanking delimiter run]:
  4645. ```````````````````````````````` example
  4646. a*"foo"*
  4647. .
  4648. <p>a*&quot;foo&quot;*</p>
  4649. ````````````````````````````````
  4650. Unicode nonbreaking spaces count as whitespace, too:
  4651. ```````````````````````````````` example
  4652. * a *
  4653. .
  4654. <p>* a *</p>
  4655. ````````````````````````````````
  4656. Intraword emphasis with `*` is permitted:
  4657. ```````````````````````````````` example
  4658. foo*bar*
  4659. .
  4660. <p>foo<em>bar</em></p>
  4661. ````````````````````````````````
  4662. ```````````````````````````````` example
  4663. 5*6*78
  4664. .
  4665. <p>5<em>6</em>78</p>
  4666. ````````````````````````````````
  4667. Rule 2:
  4668. ```````````````````````````````` example
  4669. _foo bar_
  4670. .
  4671. <p><em>foo bar</em></p>
  4672. ````````````````````````````````
  4673. This is not emphasis, because the opening `_` is followed by
  4674. whitespace:
  4675. ```````````````````````````````` example
  4676. _ foo bar_
  4677. .
  4678. <p>_ foo bar_</p>
  4679. ````````````````````````````````
  4680. This is not emphasis, because the opening `_` is preceded
  4681. by an alphanumeric and followed by punctuation:
  4682. ```````````````````````````````` example
  4683. a_"foo"_
  4684. .
  4685. <p>a_&quot;foo&quot;_</p>
  4686. ````````````````````````````````
  4687. Emphasis with `_` is not allowed inside words:
  4688. ```````````````````````````````` example
  4689. foo_bar_
  4690. .
  4691. <p>foo_bar_</p>
  4692. ````````````````````````````````
  4693. ```````````````````````````````` example
  4694. 5_6_78
  4695. .
  4696. <p>5_6_78</p>
  4697. ````````````````````````````````
  4698. ```````````````````````````````` example
  4699. пристаням_стремятся_
  4700. .
  4701. <p>пристаням_стремятся_</p>
  4702. ````````````````````````````````
  4703. Here `_` does not generate emphasis, because the first delimiter run
  4704. is right-flanking and the second left-flanking:
  4705. ```````````````````````````````` example
  4706. aa_"bb"_cc
  4707. .
  4708. <p>aa_&quot;bb&quot;_cc</p>
  4709. ````````````````````````````````
  4710. This is emphasis, even though the opening delimiter is
  4711. both left- and right-flanking, because it is preceded by
  4712. punctuation:
  4713. ```````````````````````````````` example
  4714. foo-_(bar)_
  4715. .
  4716. <p>foo-<em>(bar)</em></p>
  4717. ````````````````````````````````
  4718. Rule 3:
  4719. This is not emphasis, because the closing delimiter does
  4720. not match the opening delimiter:
  4721. ```````````````````````````````` example
  4722. _foo*
  4723. .
  4724. <p>_foo*</p>
  4725. ````````````````````````````````
  4726. This is not emphasis, because the closing `*` is preceded by
  4727. whitespace:
  4728. ```````````````````````````````` example
  4729. *foo bar *
  4730. .
  4731. <p>*foo bar *</p>
  4732. ````````````````````````````````
  4733. A newline also counts as whitespace:
  4734. ```````````````````````````````` example
  4735. *foo bar
  4736. *
  4737. .
  4738. <p>*foo bar</p>
  4739. <ul>
  4740. <li></li>
  4741. </ul>
  4742. ````````````````````````````````
  4743. This is not emphasis, because the second `*` is
  4744. preceded by punctuation and followed by an alphanumeric
  4745. (hence it is not part of a [right-flanking delimiter run]:
  4746. ```````````````````````````````` example
  4747. *(*foo)
  4748. .
  4749. <p>*(*foo)</p>
  4750. ````````````````````````````````
  4751. The point of this restriction is more easily appreciated
  4752. with this example:
  4753. ```````````````````````````````` example
  4754. *(*foo*)*
  4755. .
  4756. <p><em>(<em>foo</em>)</em></p>
  4757. ````````````````````````````````
  4758. Intraword emphasis with `*` is allowed:
  4759. ```````````````````````````````` example
  4760. *foo*bar
  4761. .
  4762. <p><em>foo</em>bar</p>
  4763. ````````````````````````````````
  4764. Rule 4:
  4765. This is not emphasis, because the closing `_` is preceded by
  4766. whitespace:
  4767. ```````````````````````````````` example
  4768. _foo bar _
  4769. .
  4770. <p>_foo bar _</p>
  4771. ````````````````````````````````
  4772. This is not emphasis, because the second `_` is
  4773. preceded by punctuation and followed by an alphanumeric:
  4774. ```````````````````````````````` example
  4775. _(_foo)
  4776. .
  4777. <p>_(_foo)</p>
  4778. ````````````````````````````````
  4779. This is emphasis within emphasis:
  4780. ```````````````````````````````` example
  4781. _(_foo_)_
  4782. .
  4783. <p><em>(<em>foo</em>)</em></p>
  4784. ````````````````````````````````
  4785. Intraword emphasis is disallowed for `_`:
  4786. ```````````````````````````````` example
  4787. _foo_bar
  4788. .
  4789. <p>_foo_bar</p>
  4790. ````````````````````````````````
  4791. ```````````````````````````````` example
  4792. _пристаням_стремятся
  4793. .
  4794. <p>_пристаням_стремятся</p>
  4795. ````````````````````````````````
  4796. ```````````````````````````````` example
  4797. _foo_bar_baz_
  4798. .
  4799. <p><em>foo_bar_baz</em></p>
  4800. ````````````````````````````````
  4801. This is emphasis, even though the closing delimiter is
  4802. both left- and right-flanking, because it is followed by
  4803. punctuation:
  4804. ```````````````````````````````` example
  4805. _(bar)_.
  4806. .
  4807. <p><em>(bar)</em>.</p>
  4808. ````````````````````````````````
  4809. Rule 5:
  4810. ```````````````````````````````` example
  4811. **foo bar**
  4812. .
  4813. <p><strong>foo bar</strong></p>
  4814. ````````````````````````````````
  4815. This is not strong emphasis, because the opening delimiter is
  4816. followed by whitespace:
  4817. ```````````````````````````````` example
  4818. ** foo bar**
  4819. .
  4820. <p>** foo bar**</p>
  4821. ````````````````````````````````
  4822. This is not strong emphasis, because the opening `**` is preceded
  4823. by an alphanumeric and followed by punctuation, and hence
  4824. not part of a [left-flanking delimiter run]:
  4825. ```````````````````````````````` example
  4826. a**"foo"**
  4827. .
  4828. <p>a**&quot;foo&quot;**</p>
  4829. ````````````````````````````````
  4830. Intraword strong emphasis with `**` is permitted:
  4831. ```````````````````````````````` example
  4832. foo**bar**
  4833. .
  4834. <p>foo<strong>bar</strong></p>
  4835. ````````````````````````````````
  4836. Rule 6:
  4837. ```````````````````````````````` example
  4838. __foo bar__
  4839. .
  4840. <p><strong>foo bar</strong></p>
  4841. ````````````````````````````````
  4842. This is not strong emphasis, because the opening delimiter is
  4843. followed by whitespace:
  4844. ```````````````````````````````` example
  4845. __ foo bar__
  4846. .
  4847. <p>__ foo bar__</p>
  4848. ````````````````````````````````
  4849. A newline counts as whitespace:
  4850. ```````````````````````````````` example
  4851. __
  4852. foo bar__
  4853. .
  4854. <p>__
  4855. foo bar__</p>
  4856. ````````````````````````````````
  4857. This is not strong emphasis, because the opening `__` is preceded
  4858. by an alphanumeric and followed by punctuation:
  4859. ```````````````````````````````` example
  4860. a__"foo"__
  4861. .
  4862. <p>a__&quot;foo&quot;__</p>
  4863. ````````````````````````````````
  4864. Intraword strong emphasis is forbidden with `__`:
  4865. ```````````````````````````````` example
  4866. foo__bar__
  4867. .
  4868. <p>foo__bar__</p>
  4869. ````````````````````````````````
  4870. ```````````````````````````````` example
  4871. 5__6__78
  4872. .
  4873. <p>5__6__78</p>
  4874. ````````````````````````````````
  4875. ```````````````````````````````` example
  4876. пристаням__стремятся__
  4877. .
  4878. <p>пристаням__стремятся__</p>
  4879. ````````````````````````````````
  4880. ```````````````````````````````` example
  4881. __foo, __bar__, baz__
  4882. .
  4883. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4884. ````````````````````````````````
  4885. This is strong emphasis, even though the opening delimiter is
  4886. both left- and right-flanking, because it is preceded by
  4887. punctuation:
  4888. ```````````````````````````````` example
  4889. foo-__(bar)__
  4890. .
  4891. <p>foo-<strong>(bar)</strong></p>
  4892. ````````````````````````````````
  4893. Rule 7:
  4894. This is not strong emphasis, because the closing delimiter is preceded
  4895. by whitespace:
  4896. ```````````````````````````````` example
  4897. **foo bar **
  4898. .
  4899. <p>**foo bar **</p>
  4900. ````````````````````````````````
  4901. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4902. Rule 11.)
  4903. This is not strong emphasis, because the second `**` is
  4904. preceded by punctuation and followed by an alphanumeric:
  4905. ```````````````````````````````` example
  4906. **(**foo)
  4907. .
  4908. <p>**(**foo)</p>
  4909. ````````````````````````````````
  4910. The point of this restriction is more easily appreciated
  4911. with these examples:
  4912. ```````````````````````````````` example
  4913. *(**foo**)*
  4914. .
  4915. <p><em>(<strong>foo</strong>)</em></p>
  4916. ````````````````````````````````
  4917. ```````````````````````````````` example
  4918. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4919. *Asclepias physocarpa*)**
  4920. .
  4921. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4922. <em>Asclepias physocarpa</em>)</strong></p>
  4923. ````````````````````````````````
  4924. ```````````````````````````````` example
  4925. **foo "*bar*" foo**
  4926. .
  4927. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4928. ````````````````````````````````
  4929. Intraword emphasis:
  4930. ```````````````````````````````` example
  4931. **foo**bar
  4932. .
  4933. <p><strong>foo</strong>bar</p>
  4934. ````````````````````````````````
  4935. Rule 8:
  4936. This is not strong emphasis, because the closing delimiter is
  4937. preceded by whitespace:
  4938. ```````````````````````````````` example
  4939. __foo bar __
  4940. .
  4941. <p>__foo bar __</p>
  4942. ````````````````````````````````
  4943. This is not strong emphasis, because the second `__` is
  4944. preceded by punctuation and followed by an alphanumeric:
  4945. ```````````````````````````````` example
  4946. __(__foo)
  4947. .
  4948. <p>__(__foo)</p>
  4949. ````````````````````````````````
  4950. The point of this restriction is more easily appreciated
  4951. with this example:
  4952. ```````````````````````````````` example
  4953. _(__foo__)_
  4954. .
  4955. <p><em>(<strong>foo</strong>)</em></p>
  4956. ````````````````````````````````
  4957. Intraword strong emphasis is forbidden with `__`:
  4958. ```````````````````````````````` example
  4959. __foo__bar
  4960. .
  4961. <p>__foo__bar</p>
  4962. ````````````````````````````````
  4963. ```````````````````````````````` example
  4964. __пристаням__стремятся
  4965. .
  4966. <p>__пристаням__стремятся</p>
  4967. ````````````````````````````````
  4968. ```````````````````````````````` example
  4969. __foo__bar__baz__
  4970. .
  4971. <p><strong>foo__bar__baz</strong></p>
  4972. ````````````````````````````````
  4973. This is strong emphasis, even though the closing delimiter is
  4974. both left- and right-flanking, because it is followed by
  4975. punctuation:
  4976. ```````````````````````````````` example
  4977. __(bar)__.
  4978. .
  4979. <p><strong>(bar)</strong>.</p>
  4980. ````````````````````````````````
  4981. Rule 9:
  4982. Any nonempty sequence of inline elements can be the contents of an
  4983. emphasized span.
  4984. ```````````````````````````````` example
  4985. *foo [bar](/url)*
  4986. .
  4987. <p><em>foo <a href="/url">bar</a></em></p>
  4988. ````````````````````````````````
  4989. ```````````````````````````````` example
  4990. *foo
  4991. bar*
  4992. .
  4993. <p><em>foo
  4994. bar</em></p>
  4995. ````````````````````````````````
  4996. In particular, emphasis and strong emphasis can be nested
  4997. inside emphasis:
  4998. ```````````````````````````````` example
  4999. _foo __bar__ baz_
  5000. .
  5001. <p><em>foo <strong>bar</strong> baz</em></p>
  5002. ````````````````````````````````
  5003. ```````````````````````````````` example
  5004. _foo _bar_ baz_
  5005. .
  5006. <p><em>foo <em>bar</em> baz</em></p>
  5007. ````````````````````````````````
  5008. ```````````````````````````````` example
  5009. __foo_ bar_
  5010. .
  5011. <p><em><em>foo</em> bar</em></p>
  5012. ````````````````````````````````
  5013. ```````````````````````````````` example
  5014. *foo *bar**
  5015. .
  5016. <p><em>foo <em>bar</em></em></p>
  5017. ````````````````````````````````
  5018. ```````````````````````````````` example
  5019. *foo **bar** baz*
  5020. .
  5021. <p><em>foo <strong>bar</strong> baz</em></p>
  5022. ````````````````````````````````
  5023. ```````````````````````````````` example
  5024. *foo**bar**baz*
  5025. .
  5026. <p><em>foo<strong>bar</strong>baz</em></p>
  5027. ````````````````````````````````
  5028. Note that in the preceding case, the interpretation
  5029. ``` markdown
  5030. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5031. ```
  5032. is precluded by the condition that a delimiter that
  5033. can both open and close (like the `*` after `foo`
  5034. cannot form emphasis if the sum of the lengths of
  5035. the delimiter runs containing the opening and
  5036. closing delimiters is a multiple of 3.
  5037. The same condition ensures that the following
  5038. cases are all strong emphasis nested inside
  5039. emphasis, even when the interior spaces are
  5040. omitted:
  5041. ```````````````````````````````` example
  5042. ***foo** bar*
  5043. .
  5044. <p><em><strong>foo</strong> bar</em></p>
  5045. ````````````````````````````````
  5046. ```````````````````````````````` example
  5047. *foo **bar***
  5048. .
  5049. <p><em>foo <strong>bar</strong></em></p>
  5050. ````````````````````````````````
  5051. ```````````````````````````````` example
  5052. *foo**bar***
  5053. .
  5054. <p><em>foo<strong>bar</strong></em></p>
  5055. ````````````````````````````````
  5056. ```````````````````````````````` example
  5057. *foo**bar***
  5058. .
  5059. <p><em>foo<strong>bar</strong></em></p>
  5060. ````````````````````````````````
  5061. Indefinite levels of nesting are possible:
  5062. ```````````````````````````````` example
  5063. *foo **bar *baz* bim** bop*
  5064. .
  5065. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5066. ````````````````````````````````
  5067. ```````````````````````````````` example
  5068. *foo [*bar*](/url)*
  5069. .
  5070. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5071. ````````````````````````````````
  5072. There can be no empty emphasis or strong emphasis:
  5073. ```````````````````````````````` example
  5074. ** is not an empty emphasis
  5075. .
  5076. <p>** is not an empty emphasis</p>
  5077. ````````````````````````````````
  5078. ```````````````````````````````` example
  5079. **** is not an empty strong emphasis
  5080. .
  5081. <p>**** is not an empty strong emphasis</p>
  5082. ````````````````````````````````
  5083. Rule 10:
  5084. Any nonempty sequence of inline elements can be the contents of an
  5085. strongly emphasized span.
  5086. ```````````````````````````````` example
  5087. **foo [bar](/url)**
  5088. .
  5089. <p><strong>foo <a href="/url">bar</a></strong></p>
  5090. ````````````````````````````````
  5091. ```````````````````````````````` example
  5092. **foo
  5093. bar**
  5094. .
  5095. <p><strong>foo
  5096. bar</strong></p>
  5097. ````````````````````````````````
  5098. In particular, emphasis and strong emphasis can be nested
  5099. inside strong emphasis:
  5100. ```````````````````````````````` example
  5101. __foo _bar_ baz__
  5102. .
  5103. <p><strong>foo <em>bar</em> baz</strong></p>
  5104. ````````````````````````````````
  5105. ```````````````````````````````` example
  5106. __foo __bar__ baz__
  5107. .
  5108. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5109. ````````````````````````````````
  5110. ```````````````````````````````` example
  5111. ____foo__ bar__
  5112. .
  5113. <p><strong><strong>foo</strong> bar</strong></p>
  5114. ````````````````````````````````
  5115. ```````````````````````````````` example
  5116. **foo **bar****
  5117. .
  5118. <p><strong>foo <strong>bar</strong></strong></p>
  5119. ````````````````````````````````
  5120. ```````````````````````````````` example
  5121. **foo *bar* baz**
  5122. .
  5123. <p><strong>foo <em>bar</em> baz</strong></p>
  5124. ````````````````````````````````
  5125. ```````````````````````````````` example
  5126. **foo*bar*baz**
  5127. .
  5128. <p><strong>foo<em>bar</em>baz</strong></p>
  5129. ````````````````````````````````
  5130. ```````````````````````````````` example
  5131. ***foo* bar**
  5132. .
  5133. <p><strong><em>foo</em> bar</strong></p>
  5134. ````````````````````````````````
  5135. ```````````````````````````````` example
  5136. **foo *bar***
  5137. .
  5138. <p><strong>foo <em>bar</em></strong></p>
  5139. ````````````````````````````````
  5140. Indefinite levels of nesting are possible:
  5141. ```````````````````````````````` example
  5142. **foo *bar **baz**
  5143. bim* bop**
  5144. .
  5145. <p><strong>foo <em>bar <strong>baz</strong>
  5146. bim</em> bop</strong></p>
  5147. ````````````````````````````````
  5148. ```````````````````````````````` example
  5149. **foo [*bar*](/url)**
  5150. .
  5151. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5152. ````````````````````````````````
  5153. There can be no empty emphasis or strong emphasis:
  5154. ```````````````````````````````` example
  5155. __ is not an empty emphasis
  5156. .
  5157. <p>__ is not an empty emphasis</p>
  5158. ````````````````````````````````
  5159. ```````````````````````````````` example
  5160. ____ is not an empty strong emphasis
  5161. .
  5162. <p>____ is not an empty strong emphasis</p>
  5163. ````````````````````````````````
  5164. Rule 11:
  5165. ```````````````````````````````` example
  5166. foo ***
  5167. .
  5168. <p>foo ***</p>
  5169. ````````````````````````````````
  5170. ```````````````````````````````` example
  5171. foo *\**
  5172. .
  5173. <p>foo <em>*</em></p>
  5174. ````````````````````````````````
  5175. ```````````````````````````````` example
  5176. foo *_*
  5177. .
  5178. <p>foo <em>_</em></p>
  5179. ````````````````````````````````
  5180. ```````````````````````````````` example
  5181. foo *****
  5182. .
  5183. <p>foo *****</p>
  5184. ````````````````````````````````
  5185. ```````````````````````````````` example
  5186. foo **\***
  5187. .
  5188. <p>foo <strong>*</strong></p>
  5189. ````````````````````````````````
  5190. ```````````````````````````````` example
  5191. foo **_**
  5192. .
  5193. <p>foo <strong>_</strong></p>
  5194. ````````````````````````````````
  5195. Note that when delimiters do not match evenly, Rule 11 determines
  5196. that the excess literal `*` characters will appear outside of the
  5197. emphasis, rather than inside it:
  5198. ```````````````````````````````` example
  5199. **foo*
  5200. .
  5201. <p>*<em>foo</em></p>
  5202. ````````````````````````````````
  5203. ```````````````````````````````` example
  5204. *foo**
  5205. .
  5206. <p><em>foo</em>*</p>
  5207. ````````````````````````````````
  5208. ```````````````````````````````` example
  5209. ***foo**
  5210. .
  5211. <p>*<strong>foo</strong></p>
  5212. ````````````````````````````````
  5213. ```````````````````````````````` example
  5214. ****foo*
  5215. .
  5216. <p>***<em>foo</em></p>
  5217. ````````````````````````````````
  5218. ```````````````````````````````` example
  5219. **foo***
  5220. .
  5221. <p><strong>foo</strong>*</p>
  5222. ````````````````````````````````
  5223. ```````````````````````````````` example
  5224. *foo****
  5225. .
  5226. <p><em>foo</em>***</p>
  5227. ````````````````````````````````
  5228. Rule 12:
  5229. ```````````````````````````````` example
  5230. foo ___
  5231. .
  5232. <p>foo ___</p>
  5233. ````````````````````````````````
  5234. ```````````````````````````````` example
  5235. foo _\__
  5236. .
  5237. <p>foo <em>_</em></p>
  5238. ````````````````````````````````
  5239. ```````````````````````````````` example
  5240. foo _*_
  5241. .
  5242. <p>foo <em>*</em></p>
  5243. ````````````````````````````````
  5244. ```````````````````````````````` example
  5245. foo _____
  5246. .
  5247. <p>foo _____</p>
  5248. ````````````````````````````````
  5249. ```````````````````````````````` example
  5250. foo __\___
  5251. .
  5252. <p>foo <strong>_</strong></p>
  5253. ````````````````````````````````
  5254. ```````````````````````````````` example
  5255. foo __*__
  5256. .
  5257. <p>foo <strong>*</strong></p>
  5258. ````````````````````````````````
  5259. ```````````````````````````````` example
  5260. __foo_
  5261. .
  5262. <p>_<em>foo</em></p>
  5263. ````````````````````````````````
  5264. Note that when delimiters do not match evenly, Rule 12 determines
  5265. that the excess literal `_` characters will appear outside of the
  5266. emphasis, rather than inside it:
  5267. ```````````````````````````````` example
  5268. _foo__
  5269. .
  5270. <p><em>foo</em>_</p>
  5271. ````````````````````````````````
  5272. ```````````````````````````````` example
  5273. ___foo__
  5274. .
  5275. <p>_<strong>foo</strong></p>
  5276. ````````````````````````````````
  5277. ```````````````````````````````` example
  5278. ____foo_
  5279. .
  5280. <p>___<em>foo</em></p>
  5281. ````````````````````````````````
  5282. ```````````````````````````````` example
  5283. __foo___
  5284. .
  5285. <p><strong>foo</strong>_</p>
  5286. ````````````````````````````````
  5287. ```````````````````````````````` example
  5288. _foo____
  5289. .
  5290. <p><em>foo</em>___</p>
  5291. ````````````````````````````````
  5292. Rule 13 implies that if you want emphasis nested directly inside
  5293. emphasis, you must use different delimiters:
  5294. ```````````````````````````````` example
  5295. **foo**
  5296. .
  5297. <p><strong>foo</strong></p>
  5298. ````````````````````````````````
  5299. ```````````````````````````````` example
  5300. *_foo_*
  5301. .
  5302. <p><em><em>foo</em></em></p>
  5303. ````````````````````````````````
  5304. ```````````````````````````````` example
  5305. __foo__
  5306. .
  5307. <p><strong>foo</strong></p>
  5308. ````````````````````````````````
  5309. ```````````````````````````````` example
  5310. _*foo*_
  5311. .
  5312. <p><em><em>foo</em></em></p>
  5313. ````````````````````````````````
  5314. However, strong emphasis within strong emphasis is possible without
  5315. switching delimiters:
  5316. ```````````````````````````````` example
  5317. ****foo****
  5318. .
  5319. <p><strong><strong>foo</strong></strong></p>
  5320. ````````````````````````````````
  5321. ```````````````````````````````` example
  5322. ____foo____
  5323. .
  5324. <p><strong><strong>foo</strong></strong></p>
  5325. ````````````````````````````````
  5326. Rule 13 can be applied to arbitrarily long sequences of
  5327. delimiters:
  5328. ```````````````````````````````` example
  5329. ******foo******
  5330. .
  5331. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5332. ````````````````````````````````
  5333. Rule 14:
  5334. ```````````````````````````````` example
  5335. ***foo***
  5336. .
  5337. <p><strong><em>foo</em></strong></p>
  5338. ````````````````````````````````
  5339. ```````````````````````````````` example
  5340. _____foo_____
  5341. .
  5342. <p><strong><strong><em>foo</em></strong></strong></p>
  5343. ````````````````````````````````
  5344. Rule 15:
  5345. ```````````````````````````````` example
  5346. *foo _bar* baz_
  5347. .
  5348. <p><em>foo _bar</em> baz_</p>
  5349. ````````````````````````````````
  5350. ```````````````````````````````` example
  5351. *foo __bar *baz bim__ bam*
  5352. .
  5353. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5354. ````````````````````````````````
  5355. Rule 16:
  5356. ```````````````````````````````` example
  5357. **foo **bar baz**
  5358. .
  5359. <p>**foo <strong>bar baz</strong></p>
  5360. ````````````````````````````````
  5361. ```````````````````````````````` example
  5362. *foo *bar baz*
  5363. .
  5364. <p>*foo <em>bar baz</em></p>
  5365. ````````````````````````````````
  5366. Rule 17:
  5367. ```````````````````````````````` example
  5368. *[bar*](/url)
  5369. .
  5370. <p>*<a href="/url">bar*</a></p>
  5371. ````````````````````````````````
  5372. ```````````````````````````````` example
  5373. _foo [bar_](/url)
  5374. .
  5375. <p>_foo <a href="/url">bar_</a></p>
  5376. ````````````````````````````````
  5377. ```````````````````````````````` example
  5378. *<img src="foo" title="*"/>
  5379. .
  5380. <p>*<img src="foo" title="*"/></p>
  5381. ````````````````````````````````
  5382. ```````````````````````````````` example
  5383. **<a href="**">
  5384. .
  5385. <p>**<a href="**"></p>
  5386. ````````````````````````````````
  5387. ```````````````````````````````` example
  5388. __<a href="__">
  5389. .
  5390. <p>__<a href="__"></p>
  5391. ````````````````````````````````
  5392. ```````````````````````````````` example
  5393. *a `*`*
  5394. .
  5395. <p><em>a <code>*</code></em></p>
  5396. ````````````````````````````````
  5397. ```````````````````````````````` example
  5398. _a `_`_
  5399. .
  5400. <p><em>a <code>_</code></em></p>
  5401. ````````````````````````````````
  5402. ```````````````````````````````` example
  5403. **a<http://foo.bar/?q=**>
  5404. .
  5405. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5406. ````````````````````````````````
  5407. ```````````````````````````````` example
  5408. __a<http://foo.bar/?q=__>
  5409. .
  5410. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5411. ````````````````````````````````
  5412. ## Links
  5413. A link contains [link text] (the visible text), a [link destination]
  5414. (the URI that is the link destination), and optionally a [link title].
  5415. There are two basic kinds of links in Markdown. In [inline links] the
  5416. destination and title are given immediately after the link text. In
  5417. [reference links] the destination and title are defined elsewhere in
  5418. the document.
  5419. A [link text](@) consists of a sequence of zero or more
  5420. inline elements enclosed by square brackets (`[` and `]`). The
  5421. following rules apply:
  5422. - Links may not contain other links, at any level of nesting. If
  5423. multiple otherwise valid link definitions appear nested inside each
  5424. other, the inner-most definition is used.
  5425. - Brackets are allowed in the [link text] only if (a) they
  5426. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5427. with an open bracket `[`, a sequence of zero or more inlines, and
  5428. a close bracket `]`.
  5429. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5430. than the brackets in link text. Thus, for example,
  5431. `` [foo`]` `` could not be a link text, since the second `]`
  5432. is part of a code span.
  5433. - The brackets in link text bind more tightly than markers for
  5434. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5435. A [link destination](@) consists of either
  5436. - a sequence of zero or more characters between an opening `<` and a
  5437. closing `>` that contains no spaces, line breaks, or unescaped
  5438. `<` or `>` characters, or
  5439. - a nonempty sequence of characters that does not include
  5440. ASCII space or control characters, and includes parentheses
  5441. only if (a) they are backslash-escaped or (b) they are part of
  5442. a balanced pair of unescaped parentheses that is not itself
  5443. inside a balanced pair of unescaped parentheses.
  5444. A [link title](@) consists of either
  5445. - a sequence of zero or more characters between straight double-quote
  5446. characters (`"`), including a `"` character only if it is
  5447. backslash-escaped, or
  5448. - a sequence of zero or more characters between straight single-quote
  5449. characters (`'`), including a `'` character only if it is
  5450. backslash-escaped, or
  5451. - a sequence of zero or more characters between matching parentheses
  5452. (`(...)`), including a `)` character only if it is backslash-escaped.
  5453. Although [link titles] may span multiple lines, they may not contain
  5454. a [blank line].
  5455. An [inline link](@) consists of a [link text] followed immediately
  5456. by a left parenthesis `(`, optional [whitespace], an optional
  5457. [link destination], an optional [link title] separated from the link
  5458. destination by [whitespace], optional [whitespace], and a right
  5459. parenthesis `)`. The link's text consists of the inlines contained
  5460. in the [link text] (excluding the enclosing square brackets).
  5461. The link's URI consists of the link destination, excluding enclosing
  5462. `<...>` if present, with backslash-escapes in effect as described
  5463. above. The link's title consists of the link title, excluding its
  5464. enclosing delimiters, with backslash-escapes in effect as described
  5465. above.
  5466. Here is a simple inline link:
  5467. ```````````````````````````````` example
  5468. [link](/uri "title")
  5469. .
  5470. <p><a href="/uri" title="title">link</a></p>
  5471. ````````````````````````````````
  5472. The title may be omitted:
  5473. ```````````````````````````````` example
  5474. [link](/uri)
  5475. .
  5476. <p><a href="/uri">link</a></p>
  5477. ````````````````````````````````
  5478. Both the title and the destination may be omitted:
  5479. ```````````````````````````````` example
  5480. [link]()
  5481. .
  5482. <p><a href="">link</a></p>
  5483. ````````````````````````````````
  5484. ```````````````````````````````` example
  5485. [link](<>)
  5486. .
  5487. <p><a href="">link</a></p>
  5488. ````````````````````````````````
  5489. The destination cannot contain spaces or line breaks,
  5490. even if enclosed in pointy brackets:
  5491. ```````````````````````````````` example
  5492. [link](/my uri)
  5493. .
  5494. <p>[link](/my uri)</p>
  5495. ````````````````````````````````
  5496. ```````````````````````````````` example
  5497. [link](</my uri>)
  5498. .
  5499. <p>[link](&lt;/my uri&gt;)</p>
  5500. ````````````````````````````````
  5501. ```````````````````````````````` example
  5502. [link](foo
  5503. bar)
  5504. .
  5505. <p>[link](foo
  5506. bar)</p>
  5507. ````````````````````````````````
  5508. ```````````````````````````````` example
  5509. [link](<foo
  5510. bar>)
  5511. .
  5512. <p>[link](<foo
  5513. bar>)</p>
  5514. ````````````````````````````````
  5515. Parentheses inside the link destination may be escaped:
  5516. ```````````````````````````````` example
  5517. [link](\(foo\))
  5518. .
  5519. <p><a href="(foo)">link</a></p>
  5520. ````````````````````````````````
  5521. One level of balanced parentheses is allowed without escaping:
  5522. ```````````````````````````````` example
  5523. [link]((foo)and(bar))
  5524. .
  5525. <p><a href="(foo)and(bar)">link</a></p>
  5526. ````````````````````````````````
  5527. However, if you have parentheses within parentheses, you need to escape
  5528. or use the `<...>` form:
  5529. ```````````````````````````````` example
  5530. [link](foo(and(bar)))
  5531. .
  5532. <p>[link](foo(and(bar)))</p>
  5533. ````````````````````````````````
  5534. ```````````````````````````````` example
  5535. [link](foo(and\(bar\)))
  5536. .
  5537. <p><a href="foo(and(bar))">link</a></p>
  5538. ````````````````````````````````
  5539. ```````````````````````````````` example
  5540. [link](<foo(and(bar))>)
  5541. .
  5542. <p><a href="foo(and(bar))">link</a></p>
  5543. ````````````````````````````````
  5544. Parentheses and other symbols can also be escaped, as usual
  5545. in Markdown:
  5546. ```````````````````````````````` example
  5547. [link](foo\)\:)
  5548. .
  5549. <p><a href="foo):">link</a></p>
  5550. ````````````````````````````````
  5551. A link can contain fragment identifiers and queries:
  5552. ```````````````````````````````` example
  5553. [link](#fragment)
  5554. [link](http://example.com#fragment)
  5555. [link](http://example.com?foo=3#frag)
  5556. .
  5557. <p><a href="#fragment">link</a></p>
  5558. <p><a href="http://example.com#fragment">link</a></p>
  5559. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5560. ````````````````````````````````
  5561. Note that a backslash before a non-escapable character is
  5562. just a backslash:
  5563. ```````````````````````````````` example
  5564. [link](foo\bar)
  5565. .
  5566. <p><a href="foo%5Cbar">link</a></p>
  5567. ````````````````````````````````
  5568. URL-escaping should be left alone inside the destination, as all
  5569. URL-escaped characters are also valid URL characters. Entity and
  5570. numerical character references in the destination will be parsed
  5571. into the corresponding Unicode code points, as usual. These may
  5572. be optionally URL-escaped when written as HTML, but this spec
  5573. does not enforce any particular policy for rendering URLs in
  5574. HTML or other formats. Renderers may make different decisions
  5575. about how to escape or normalize URLs in the output.
  5576. ```````````````````````````````` example
  5577. [link](foo%20b&auml;)
  5578. .
  5579. <p><a href="foo%20b%C3%A4">link</a></p>
  5580. ````````````````````````````````
  5581. Note that, because titles can often be parsed as destinations,
  5582. if you try to omit the destination and keep the title, you'll
  5583. get unexpected results:
  5584. ```````````````````````````````` example
  5585. [link]("title")
  5586. .
  5587. <p><a href="%22title%22">link</a></p>
  5588. ````````````````````````````````
  5589. Titles may be in single quotes, double quotes, or parentheses:
  5590. ```````````````````````````````` example
  5591. [link](/url "title")
  5592. [link](/url 'title')
  5593. [link](/url (title))
  5594. .
  5595. <p><a href="/url" title="title">link</a>
  5596. <a href="/url" title="title">link</a>
  5597. <a href="/url" title="title">link</a></p>
  5598. ````````````````````````````````
  5599. Backslash escapes and entity and numeric character references
  5600. may be used in titles:
  5601. ```````````````````````````````` example
  5602. [link](/url "title \"&quot;")
  5603. .
  5604. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5605. ````````````````````````````````
  5606. Nested balanced quotes are not allowed without escaping:
  5607. ```````````````````````````````` example
  5608. [link](/url "title "and" title")
  5609. .
  5610. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5611. ````````````````````````````````
  5612. But it is easy to work around this by using a different quote type:
  5613. ```````````````````````````````` example
  5614. [link](/url 'title "and" title')
  5615. .
  5616. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5617. ````````````````````````````````
  5618. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5619. title, and its test suite included a test demonstrating this.
  5620. But it is hard to see a good rationale for the extra complexity this
  5621. brings, since there are already many ways---backslash escaping,
  5622. entity and numeric character references, or using a different
  5623. quote type for the enclosing title---to write titles containing
  5624. double quotes. `Markdown.pl`'s handling of titles has a number
  5625. of other strange features. For example, it allows single-quoted
  5626. titles in inline links, but not reference links. And, in
  5627. reference links but not inline links, it allows a title to begin
  5628. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5629. titles with no closing quotation mark, though 1.0.2b8 does not.
  5630. It seems preferable to adopt a simple, rational rule that works
  5631. the same way in inline links and link reference definitions.)
  5632. [Whitespace] is allowed around the destination and title:
  5633. ```````````````````````````````` example
  5634. [link]( /uri
  5635. "title" )
  5636. .
  5637. <p><a href="/uri" title="title">link</a></p>
  5638. ````````````````````````````````
  5639. But it is not allowed between the link text and the
  5640. following parenthesis:
  5641. ```````````````````````````````` example
  5642. [link] (/uri)
  5643. .
  5644. <p>[link] (/uri)</p>
  5645. ````````````````````````````````
  5646. The link text may contain balanced brackets, but not unbalanced ones,
  5647. unless they are escaped:
  5648. ```````````````````````````````` example
  5649. [link [foo [bar]]](/uri)
  5650. .
  5651. <p><a href="/uri">link [foo [bar]]</a></p>
  5652. ````````````````````````````````
  5653. ```````````````````````````````` example
  5654. [link] bar](/uri)
  5655. .
  5656. <p>[link] bar](/uri)</p>
  5657. ````````````````````````````````
  5658. ```````````````````````````````` example
  5659. [link [bar](/uri)
  5660. .
  5661. <p>[link <a href="/uri">bar</a></p>
  5662. ````````````````````````````````
  5663. ```````````````````````````````` example
  5664. [link \[bar](/uri)
  5665. .
  5666. <p><a href="/uri">link [bar</a></p>
  5667. ````````````````````````````````
  5668. The link text may contain inline content:
  5669. ```````````````````````````````` example
  5670. [link *foo **bar** `#`*](/uri)
  5671. .
  5672. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5673. ````````````````````````````````
  5674. ```````````````````````````````` example
  5675. [![moon](moon.jpg)](/uri)
  5676. .
  5677. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5678. ````````````````````````````````
  5679. However, links may not contain other links, at any level of nesting.
  5680. ```````````````````````````````` example
  5681. [foo [bar](/uri)](/uri)
  5682. .
  5683. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5684. ````````````````````````````````
  5685. ```````````````````````````````` example
  5686. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5687. .
  5688. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5689. ````````````````````````````````
  5690. ```````````````````````````````` example
  5691. ![[[foo](uri1)](uri2)](uri3)
  5692. .
  5693. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5694. ````````````````````````````````
  5695. These cases illustrate the precedence of link text grouping over
  5696. emphasis grouping:
  5697. ```````````````````````````````` example
  5698. *[foo*](/uri)
  5699. .
  5700. <p>*<a href="/uri">foo*</a></p>
  5701. ````````````````````````````````
  5702. ```````````````````````````````` example
  5703. [foo *bar](baz*)
  5704. .
  5705. <p><a href="baz*">foo *bar</a></p>
  5706. ````````````````````````````````
  5707. Note that brackets that *aren't* part of links do not take
  5708. precedence:
  5709. ```````````````````````````````` example
  5710. *foo [bar* baz]
  5711. .
  5712. <p><em>foo [bar</em> baz]</p>
  5713. ````````````````````````````````
  5714. These cases illustrate the precedence of HTML tags, code spans,
  5715. and autolinks over link grouping:
  5716. ```````````````````````````````` example
  5717. [foo <bar attr="](baz)">
  5718. .
  5719. <p>[foo <bar attr="](baz)"></p>
  5720. ````````````````````````````````
  5721. ```````````````````````````````` example
  5722. [foo`](/uri)`
  5723. .
  5724. <p>[foo<code>](/uri)</code></p>
  5725. ````````````````````````````````
  5726. ```````````````````````````````` example
  5727. [foo<http://example.com/?search=](uri)>
  5728. .
  5729. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5730. ````````````````````````````````
  5731. There are three kinds of [reference link](@)s:
  5732. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5733. and [shortcut](#shortcut-reference-link).
  5734. A [full reference link](@)
  5735. consists of a [link text] immediately followed by a [link label]
  5736. that [matches] a [link reference definition] elsewhere in the document.
  5737. A [link label](@) begins with a left bracket (`[`) and ends
  5738. with the first right bracket (`]`) that is not backslash-escaped.
  5739. Between these brackets there must be at least one [non-whitespace character].
  5740. Unescaped square bracket characters are not allowed in
  5741. [link labels]. A link label can have at most 999
  5742. characters inside the square brackets.
  5743. One label [matches](@)
  5744. another just in case their normalized forms are equal. To normalize a
  5745. label, perform the *Unicode case fold* and collapse consecutive internal
  5746. [whitespace] to a single space. If there are multiple
  5747. matching reference link definitions, the one that comes first in the
  5748. document is used. (It is desirable in such cases to emit a warning.)
  5749. The contents of the first link label are parsed as inlines, which are
  5750. used as the link's text. The link's URI and title are provided by the
  5751. matching [link reference definition].
  5752. Here is a simple example:
  5753. ```````````````````````````````` example
  5754. [foo][bar]
  5755. [bar]: /url "title"
  5756. .
  5757. <p><a href="/url" title="title">foo</a></p>
  5758. ````````````````````````````````
  5759. The rules for the [link text] are the same as with
  5760. [inline links]. Thus:
  5761. The link text may contain balanced brackets, but not unbalanced ones,
  5762. unless they are escaped:
  5763. ```````````````````````````````` example
  5764. [link [foo [bar]]][ref]
  5765. [ref]: /uri
  5766. .
  5767. <p><a href="/uri">link [foo [bar]]</a></p>
  5768. ````````````````````````````````
  5769. ```````````````````````````````` example
  5770. [link \[bar][ref]
  5771. [ref]: /uri
  5772. .
  5773. <p><a href="/uri">link [bar</a></p>
  5774. ````````````````````````````````
  5775. The link text may contain inline content:
  5776. ```````````````````````````````` example
  5777. [link *foo **bar** `#`*][ref]
  5778. [ref]: /uri
  5779. .
  5780. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5781. ````````````````````````````````
  5782. ```````````````````````````````` example
  5783. [![moon](moon.jpg)][ref]
  5784. [ref]: /uri
  5785. .
  5786. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5787. ````````````````````````````````
  5788. However, links may not contain other links, at any level of nesting.
  5789. ```````````````````````````````` example
  5790. [foo [bar](/uri)][ref]
  5791. [ref]: /uri
  5792. .
  5793. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5794. ````````````````````````````````
  5795. ```````````````````````````````` example
  5796. [foo *bar [baz][ref]*][ref]
  5797. [ref]: /uri
  5798. .
  5799. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5800. ````````````````````````````````
  5801. (In the examples above, we have two [shortcut reference links]
  5802. instead of one [full reference link].)
  5803. The following cases illustrate the precedence of link text grouping over
  5804. emphasis grouping:
  5805. ```````````````````````````````` example
  5806. *[foo*][ref]
  5807. [ref]: /uri
  5808. .
  5809. <p>*<a href="/uri">foo*</a></p>
  5810. ````````````````````````````````
  5811. ```````````````````````````````` example
  5812. [foo *bar][ref]
  5813. [ref]: /uri
  5814. .
  5815. <p><a href="/uri">foo *bar</a></p>
  5816. ````````````````````````````````
  5817. These cases illustrate the precedence of HTML tags, code spans,
  5818. and autolinks over link grouping:
  5819. ```````````````````````````````` example
  5820. [foo <bar attr="][ref]">
  5821. [ref]: /uri
  5822. .
  5823. <p>[foo <bar attr="][ref]"></p>
  5824. ````````````````````````````````
  5825. ```````````````````````````````` example
  5826. [foo`][ref]`
  5827. [ref]: /uri
  5828. .
  5829. <p>[foo<code>][ref]</code></p>
  5830. ````````````````````````````````
  5831. ```````````````````````````````` example
  5832. [foo<http://example.com/?search=][ref]>
  5833. [ref]: /uri
  5834. .
  5835. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5836. ````````````````````````````````
  5837. Matching is case-insensitive:
  5838. ```````````````````````````````` example
  5839. [foo][BaR]
  5840. [bar]: /url "title"
  5841. .
  5842. <p><a href="/url" title="title">foo</a></p>
  5843. ````````````````````````````````
  5844. Unicode case fold is used:
  5845. ```````````````````````````````` example
  5846. [Толпой][Толпой] is a Russian word.
  5847. [ТОЛПОЙ]: /url
  5848. .
  5849. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5850. ````````````````````````````````
  5851. Consecutive internal [whitespace] is treated as one space for
  5852. purposes of determining matching:
  5853. ```````````````````````````````` example
  5854. [Foo
  5855. bar]: /url
  5856. [Baz][Foo bar]
  5857. .
  5858. <p><a href="/url">Baz</a></p>
  5859. ````````````````````````````````
  5860. No [whitespace] is allowed between the [link text] and the
  5861. [link label]:
  5862. ```````````````````````````````` example
  5863. [foo] [bar]
  5864. [bar]: /url "title"
  5865. .
  5866. <p>[foo] <a href="/url" title="title">bar</a></p>
  5867. ````````````````````````````````
  5868. ```````````````````````````````` example
  5869. [foo]
  5870. [bar]
  5871. [bar]: /url "title"
  5872. .
  5873. <p>[foo]
  5874. <a href="/url" title="title">bar</a></p>
  5875. ````````````````````````````````
  5876. This is a departure from John Gruber's original Markdown syntax
  5877. description, which explicitly allows whitespace between the link
  5878. text and the link label. It brings reference links in line with
  5879. [inline links], which (according to both original Markdown and
  5880. this spec) cannot have whitespace after the link text. More
  5881. importantly, it prevents inadvertent capture of consecutive
  5882. [shortcut reference links]. If whitespace is allowed between the
  5883. link text and the link label, then in the following we will have
  5884. a single reference link, not two shortcut reference links, as
  5885. intended:
  5886. ``` markdown
  5887. [foo]
  5888. [bar]
  5889. [foo]: /url1
  5890. [bar]: /url2
  5891. ```
  5892. (Note that [shortcut reference links] were introduced by Gruber
  5893. himself in a beta version of `Markdown.pl`, but never included
  5894. in the official syntax description. Without shortcut reference
  5895. links, it is harmless to allow space between the link text and
  5896. link label; but once shortcut references are introduced, it is
  5897. too dangerous to allow this, as it frequently leads to
  5898. unintended results.)
  5899. When there are multiple matching [link reference definitions],
  5900. the first is used:
  5901. ```````````````````````````````` example
  5902. [foo]: /url1
  5903. [foo]: /url2
  5904. [bar][foo]
  5905. .
  5906. <p><a href="/url1">bar</a></p>
  5907. ````````````````````````````````
  5908. Note that matching is performed on normalized strings, not parsed
  5909. inline content. So the following does not match, even though the
  5910. labels define equivalent inline content:
  5911. ```````````````````````````````` example
  5912. [bar][foo\!]
  5913. [foo!]: /url
  5914. .
  5915. <p>[bar][foo!]</p>
  5916. ````````````````````````````````
  5917. [Link labels] cannot contain brackets, unless they are
  5918. backslash-escaped:
  5919. ```````````````````````````````` example
  5920. [foo][ref[]
  5921. [ref[]: /uri
  5922. .
  5923. <p>[foo][ref[]</p>
  5924. <p>[ref[]: /uri</p>
  5925. ````````````````````````````````
  5926. ```````````````````````````````` example
  5927. [foo][ref[bar]]
  5928. [ref[bar]]: /uri
  5929. .
  5930. <p>[foo][ref[bar]]</p>
  5931. <p>[ref[bar]]: /uri</p>
  5932. ````````````````````````````````
  5933. ```````````````````````````````` example
  5934. [[[foo]]]
  5935. [[[foo]]]: /url
  5936. .
  5937. <p>[[[foo]]]</p>
  5938. <p>[[[foo]]]: /url</p>
  5939. ````````````````````````````````
  5940. ```````````````````````````````` example
  5941. [foo][ref\[]
  5942. [ref\[]: /uri
  5943. .
  5944. <p><a href="/uri">foo</a></p>
  5945. ````````````````````````````````
  5946. Note that in this example `]` is not backslash-escaped:
  5947. ```````````````````````````````` example
  5948. [bar\\]: /uri
  5949. [bar\\]
  5950. .
  5951. <p><a href="/uri">bar\</a></p>
  5952. ````````````````````````````````
  5953. A [link label] must contain at least one [non-whitespace character]:
  5954. ```````````````````````````````` example
  5955. []
  5956. []: /uri
  5957. .
  5958. <p>[]</p>
  5959. <p>[]: /uri</p>
  5960. ````````````````````````````````
  5961. ```````````````````````````````` example
  5962. [
  5963. ]
  5964. [
  5965. ]: /uri
  5966. .
  5967. <p>[
  5968. ]</p>
  5969. <p>[
  5970. ]: /uri</p>
  5971. ````````````````````````````````
  5972. A [collapsed reference link](@)
  5973. consists of a [link label] that [matches] a
  5974. [link reference definition] elsewhere in the
  5975. document, followed by the string `[]`.
  5976. The contents of the first link label are parsed as inlines,
  5977. which are used as the link's text. The link's URI and title are
  5978. provided by the matching reference link definition. Thus,
  5979. `[foo][]` is equivalent to `[foo][foo]`.
  5980. ```````````````````````````````` example
  5981. [foo][]
  5982. [foo]: /url "title"
  5983. .
  5984. <p><a href="/url" title="title">foo</a></p>
  5985. ````````````````````````````````
  5986. ```````````````````````````````` example
  5987. [*foo* bar][]
  5988. [*foo* bar]: /url "title"
  5989. .
  5990. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5991. ````````````````````````````````
  5992. The link labels are case-insensitive:
  5993. ```````````````````````````````` example
  5994. [Foo][]
  5995. [foo]: /url "title"
  5996. .
  5997. <p><a href="/url" title="title">Foo</a></p>
  5998. ````````````````````````````````
  5999. As with full reference links, [whitespace] is not
  6000. allowed between the two sets of brackets:
  6001. ```````````````````````````````` example
  6002. [foo]
  6003. []
  6004. [foo]: /url "title"
  6005. .
  6006. <p><a href="/url" title="title">foo</a>
  6007. []</p>
  6008. ````````````````````````````````
  6009. A [shortcut reference link](@)
  6010. consists of a [link label] that [matches] a
  6011. [link reference definition] elsewhere in the
  6012. document and is not followed by `[]` or a link label.
  6013. The contents of the first link label are parsed as inlines,
  6014. which are used as the link's text. The link's URI and title
  6015. are provided by the matching link reference definition.
  6016. Thus, `[foo]` is equivalent to `[foo][]`.
  6017. ```````````````````````````````` example
  6018. [foo]
  6019. [foo]: /url "title"
  6020. .
  6021. <p><a href="/url" title="title">foo</a></p>
  6022. ````````````````````````````````
  6023. ```````````````````````````````` example
  6024. [*foo* bar]
  6025. [*foo* bar]: /url "title"
  6026. .
  6027. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6028. ````````````````````````````````
  6029. ```````````````````````````````` example
  6030. [[*foo* bar]]
  6031. [*foo* bar]: /url "title"
  6032. .
  6033. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6034. ````````````````````````````````
  6035. ```````````````````````````````` example
  6036. [[bar [foo]
  6037. [foo]: /url
  6038. .
  6039. <p>[[bar <a href="/url">foo</a></p>
  6040. ````````````````````````````````
  6041. The link labels are case-insensitive:
  6042. ```````````````````````````````` example
  6043. [Foo]
  6044. [foo]: /url "title"
  6045. .
  6046. <p><a href="/url" title="title">Foo</a></p>
  6047. ````````````````````````````````
  6048. A space after the link text should be preserved:
  6049. ```````````````````````````````` example
  6050. [foo] bar
  6051. [foo]: /url
  6052. .
  6053. <p><a href="/url">foo</a> bar</p>
  6054. ````````````````````````````````
  6055. If you just want bracketed text, you can backslash-escape the
  6056. opening bracket to avoid links:
  6057. ```````````````````````````````` example
  6058. \[foo]
  6059. [foo]: /url "title"
  6060. .
  6061. <p>[foo]</p>
  6062. ````````````````````````````````
  6063. Note that this is a link, because a link label ends with the first
  6064. following closing bracket:
  6065. ```````````````````````````````` example
  6066. [foo*]: /url
  6067. *[foo*]
  6068. .
  6069. <p>*<a href="/url">foo*</a></p>
  6070. ````````````````````````````````
  6071. Full references take precedence over shortcut references:
  6072. ```````````````````````````````` example
  6073. [foo][bar]
  6074. [foo]: /url1
  6075. [bar]: /url2
  6076. .
  6077. <p><a href="/url2">foo</a></p>
  6078. ````````````````````````````````
  6079. In the following case `[bar][baz]` is parsed as a reference,
  6080. `[foo]` as normal text:
  6081. ```````````````````````````````` example
  6082. [foo][bar][baz]
  6083. [baz]: /url
  6084. .
  6085. <p>[foo]<a href="/url">bar</a></p>
  6086. ````````````````````````````````
  6087. Here, though, `[foo][bar]` is parsed as a reference, since
  6088. `[bar]` is defined:
  6089. ```````````````````````````````` example
  6090. [foo][bar][baz]
  6091. [baz]: /url1
  6092. [bar]: /url2
  6093. .
  6094. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6095. ````````````````````````````````
  6096. Here `[foo]` is not parsed as a shortcut reference, because it
  6097. is followed by a link label (even though `[bar]` is not defined):
  6098. ```````````````````````````````` example
  6099. [foo][bar][baz]
  6100. [baz]: /url1
  6101. [foo]: /url2
  6102. .
  6103. <p>[foo]<a href="/url1">bar</a></p>
  6104. ````````````````````````````````
  6105. ## Images
  6106. Syntax for images is like the syntax for links, with one
  6107. difference. Instead of [link text], we have an
  6108. [image description](@). The rules for this are the
  6109. same as for [link text], except that (a) an
  6110. image description starts with `![` rather than `[`, and
  6111. (b) an image description may contain links.
  6112. An image description has inline elements
  6113. as its contents. When an image is rendered to HTML,
  6114. this is standardly used as the image's `alt` attribute.
  6115. ```````````````````````````````` example
  6116. ![foo](/url "title")
  6117. .
  6118. <p><img src="/url" alt="foo" title="title" /></p>
  6119. ````````````````````````````````
  6120. ```````````````````````````````` example
  6121. ![foo *bar*]
  6122. [foo *bar*]: train.jpg "train & tracks"
  6123. .
  6124. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6125. ````````````````````````````````
  6126. ```````````````````````````````` example
  6127. ![foo ![bar](/url)](/url2)
  6128. .
  6129. <p><img src="/url2" alt="foo bar" /></p>
  6130. ````````````````````````````````
  6131. ```````````````````````````````` example
  6132. ![foo [bar](/url)](/url2)
  6133. .
  6134. <p><img src="/url2" alt="foo bar" /></p>
  6135. ````````````````````````````````
  6136. Though this spec is concerned with parsing, not rendering, it is
  6137. recommended that in rendering to HTML, only the plain string content
  6138. of the [image description] be used. Note that in
  6139. the above example, the alt attribute's value is `foo bar`, not `foo
  6140. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6141. content is rendered, without formatting.
  6142. ```````````````````````````````` example
  6143. ![foo *bar*][]
  6144. [foo *bar*]: train.jpg "train & tracks"
  6145. .
  6146. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6147. ````````````````````````````````
  6148. ```````````````````````````````` example
  6149. ![foo *bar*][foobar]
  6150. [FOOBAR]: train.jpg "train & tracks"
  6151. .
  6152. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6153. ````````````````````````````````
  6154. ```````````````````````````````` example
  6155. ![foo](train.jpg)
  6156. .
  6157. <p><img src="train.jpg" alt="foo" /></p>
  6158. ````````````````````````````````
  6159. ```````````````````````````````` example
  6160. My ![foo bar](/path/to/train.jpg "title" )
  6161. .
  6162. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6163. ````````````````````````````````
  6164. ```````````````````````````````` example
  6165. ![foo](<url>)
  6166. .
  6167. <p><img src="url" alt="foo" /></p>
  6168. ````````````````````````````````
  6169. ```````````````````````````````` example
  6170. ![](/url)
  6171. .
  6172. <p><img src="/url" alt="" /></p>
  6173. ````````````````````````````````
  6174. Reference-style:
  6175. ```````````````````````````````` example
  6176. ![foo][bar]
  6177. [bar]: /url
  6178. .
  6179. <p><img src="/url" alt="foo" /></p>
  6180. ````````````````````````````````
  6181. ```````````````````````````````` example
  6182. ![foo][bar]
  6183. [BAR]: /url
  6184. .
  6185. <p><img src="/url" alt="foo" /></p>
  6186. ````````````````````````````````
  6187. Collapsed:
  6188. ```````````````````````````````` example
  6189. ![foo][]
  6190. [foo]: /url "title"
  6191. .
  6192. <p><img src="/url" alt="foo" title="title" /></p>
  6193. ````````````````````````````````
  6194. ```````````````````````````````` example
  6195. ![*foo* bar][]
  6196. [*foo* bar]: /url "title"
  6197. .
  6198. <p><img src="/url" alt="foo bar" title="title" /></p>
  6199. ````````````````````````````````
  6200. The labels are case-insensitive:
  6201. ```````````````````````````````` example
  6202. ![Foo][]
  6203. [foo]: /url "title"
  6204. .
  6205. <p><img src="/url" alt="Foo" title="title" /></p>
  6206. ````````````````````````````````
  6207. As with reference links, [whitespace] is not allowed
  6208. between the two sets of brackets:
  6209. ```````````````````````````````` example
  6210. ![foo]
  6211. []
  6212. [foo]: /url "title"
  6213. .
  6214. <p><img src="/url" alt="foo" title="title" />
  6215. []</p>
  6216. ````````````````````````````````
  6217. Shortcut:
  6218. ```````````````````````````````` example
  6219. ![foo]
  6220. [foo]: /url "title"
  6221. .
  6222. <p><img src="/url" alt="foo" title="title" /></p>
  6223. ````````````````````````````````
  6224. ```````````````````````````````` example
  6225. ![*foo* bar]
  6226. [*foo* bar]: /url "title"
  6227. .
  6228. <p><img src="/url" alt="foo bar" title="title" /></p>
  6229. ````````````````````````````````
  6230. Note that link labels cannot contain unescaped brackets:
  6231. ```````````````````````````````` example
  6232. ![[foo]]
  6233. [[foo]]: /url "title"
  6234. .
  6235. <p>![[foo]]</p>
  6236. <p>[[foo]]: /url &quot;title&quot;</p>
  6237. ````````````````````````````````
  6238. The link labels are case-insensitive:
  6239. ```````````````````````````````` example
  6240. ![Foo]
  6241. [foo]: /url "title"
  6242. .
  6243. <p><img src="/url" alt="Foo" title="title" /></p>
  6244. ````````````````````````````````
  6245. If you just want bracketed text, you can backslash-escape the
  6246. opening `!` and `[`:
  6247. ```````````````````````````````` example
  6248. \!\[foo]
  6249. [foo]: /url "title"
  6250. .
  6251. <p>![foo]</p>
  6252. ````````````````````````````````
  6253. If you want a link after a literal `!`, backslash-escape the
  6254. `!`:
  6255. ```````````````````````````````` example
  6256. \![foo]
  6257. [foo]: /url "title"
  6258. .
  6259. <p>!<a href="/url" title="title">foo</a></p>
  6260. ````````````````````````````````
  6261. ## Autolinks
  6262. [Autolink](@)s are absolute URIs and email addresses inside
  6263. `<` and `>`. They are parsed as links, with the URL or email address
  6264. as the link label.
  6265. A [URI autolink](@) consists of `<`, followed by an
  6266. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6267. a link to the URI, with the URI as the link's label.
  6268. An [absolute URI](@),
  6269. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6270. followed by zero or more characters other than ASCII
  6271. [whitespace] and control characters, `<`, and `>`. If
  6272. the URI includes these characters, they must be percent-encoded
  6273. (e.g. `%20` for a space).
  6274. For purposes of this spec, a [scheme](@) is any sequence
  6275. of 2--32 characters beginning with an ASCII letter and followed
  6276. by any combination of ASCII letters, digits, or the symbols plus
  6277. ("+"), period ("."), or hyphen ("-").
  6278. Here are some valid autolinks:
  6279. ```````````````````````````````` example
  6280. <http://foo.bar.baz>
  6281. .
  6282. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6283. ````````````````````````````````
  6284. ```````````````````````````````` example
  6285. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6286. .
  6287. <p><a href="http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean">http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean</a></p>
  6288. ````````````````````````````````
  6289. ```````````````````````````````` example
  6290. <irc://foo.bar:2233/baz>
  6291. .
  6292. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6293. ````````````````````````````````
  6294. Uppercase is also fine:
  6295. ```````````````````````````````` example
  6296. <MAILTO:FOO@BAR.BAZ>
  6297. .
  6298. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6299. ````````````````````````````````
  6300. Note that many strings that count as [absolute URIs] for
  6301. purposes of this spec are not valid URIs, because their
  6302. schemes are not registered or because of other problems
  6303. with their syntax:
  6304. ```````````````````````````````` example
  6305. <a+b+c:d>
  6306. .
  6307. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6308. ````````````````````````````````
  6309. ```````````````````````````````` example
  6310. <made-up-scheme://foo,bar>
  6311. .
  6312. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6313. ````````````````````````````````
  6314. ```````````````````````````````` example
  6315. <http://../>
  6316. .
  6317. <p><a href="http://../">http://../</a></p>
  6318. ````````````````````````````````
  6319. ```````````````````````````````` example
  6320. <localhost:5001/foo>
  6321. .
  6322. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6323. ````````````````````````````````
  6324. Spaces are not allowed in autolinks:
  6325. ```````````````````````````````` example
  6326. <http://foo.bar/baz bim>
  6327. .
  6328. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6329. ````````````````````````````````
  6330. Backslash-escapes do not work inside autolinks:
  6331. ```````````````````````````````` example
  6332. <http://example.com/\[\>
  6333. .
  6334. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6335. ````````````````````````````````
  6336. An [email autolink](@)
  6337. consists of `<`, followed by an [email address],
  6338. followed by `>`. The link's label is the email address,
  6339. and the URL is `mailto:` followed by the email address.
  6340. An [email address](@),
  6341. for these purposes, is anything that matches
  6342. the [non-normative regex from the HTML5
  6343. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6344. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6345. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6346. Examples of email autolinks:
  6347. ```````````````````````````````` example
  6348. <foo@bar.example.com>
  6349. .
  6350. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6351. ````````````````````````````````
  6352. ```````````````````````````````` example
  6353. <foo+special@Bar.baz-bar0.com>
  6354. .
  6355. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6356. ````````````````````````````````
  6357. Backslash-escapes do not work inside email autolinks:
  6358. ```````````````````````````````` example
  6359. <foo\+@bar.example.com>
  6360. .
  6361. <p>&lt;foo+@bar.example.com&gt;</p>
  6362. ````````````````````````````````
  6363. These are not autolinks:
  6364. ```````````````````````````````` example
  6365. <>
  6366. .
  6367. <p>&lt;&gt;</p>
  6368. ````````````````````````````````
  6369. ```````````````````````````````` example
  6370. < http://foo.bar >
  6371. .
  6372. <p>&lt; http://foo.bar &gt;</p>
  6373. ````````````````````````````````
  6374. ```````````````````````````````` example
  6375. <m:abc>
  6376. .
  6377. <p>&lt;m:abc&gt;</p>
  6378. ````````````````````````````````
  6379. ```````````````````````````````` example
  6380. <foo.bar.baz>
  6381. .
  6382. <p>&lt;foo.bar.baz&gt;</p>
  6383. ````````````````````````````````
  6384. ```````````````````````````````` example
  6385. http://example.com
  6386. .
  6387. <p>http://example.com</p>
  6388. ````````````````````````````````
  6389. ```````````````````````````````` example
  6390. foo@bar.example.com
  6391. .
  6392. <p>foo@bar.example.com</p>
  6393. ````````````````````````````````
  6394. ## Raw HTML
  6395. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6396. raw HTML tag and will be rendered in HTML without escaping.
  6397. Tag and attribute names are not limited to current HTML tags,
  6398. so custom tags (and even, say, DocBook tags) may be used.
  6399. Here is the grammar for tags:
  6400. A [tag name](@) consists of an ASCII letter
  6401. followed by zero or more ASCII letters, digits, or
  6402. hyphens (`-`).
  6403. An [attribute](@) consists of [whitespace],
  6404. an [attribute name], and an optional
  6405. [attribute value specification].
  6406. An [attribute name](@)
  6407. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6408. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6409. specification restricted to ASCII. HTML5 is laxer.)
  6410. An [attribute value specification](@)
  6411. consists of optional [whitespace],
  6412. a `=` character, optional [whitespace], and an [attribute
  6413. value].
  6414. An [attribute value](@)
  6415. consists of an [unquoted attribute value],
  6416. a [single-quoted attribute value], or a [double-quoted attribute value].
  6417. An [unquoted attribute value](@)
  6418. is a nonempty string of characters not
  6419. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6420. A [single-quoted attribute value](@)
  6421. consists of `'`, zero or more
  6422. characters not including `'`, and a final `'`.
  6423. A [double-quoted attribute value](@)
  6424. consists of `"`, zero or more
  6425. characters not including `"`, and a final `"`.
  6426. An [open tag](@) consists of a `<` character, a [tag name],
  6427. zero or more [attributes], optional [whitespace], an optional `/`
  6428. character, and a `>` character.
  6429. A [closing tag](@) consists of the string `</`, a
  6430. [tag name], optional [whitespace], and the character `>`.
  6431. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6432. where *text* does not start with `>` or `->`, does not end with `-`,
  6433. and does not contain `--`. (See the
  6434. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6435. A [processing instruction](@)
  6436. consists of the string `<?`, a string
  6437. of characters not including the string `?>`, and the string
  6438. `?>`.
  6439. A [declaration](@) consists of the
  6440. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6441. [whitespace], a string of characters not including the
  6442. character `>`, and the character `>`.
  6443. A [CDATA section](@) consists of
  6444. the string `<![CDATA[`, a string of characters not including the string
  6445. `]]>`, and the string `]]>`.
  6446. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6447. an [HTML comment], a [processing instruction], a [declaration],
  6448. or a [CDATA section].
  6449. Here are some simple open tags:
  6450. ```````````````````````````````` example
  6451. <a><bab><c2c>
  6452. .
  6453. <p><a><bab><c2c></p>
  6454. ````````````````````````````````
  6455. Empty elements:
  6456. ```````````````````````````````` example
  6457. <a/><b2/>
  6458. .
  6459. <p><a/><b2/></p>
  6460. ````````````````````````````````
  6461. [Whitespace] is allowed:
  6462. ```````````````````````````````` example
  6463. <a /><b2
  6464. data="foo" >
  6465. .
  6466. <p><a /><b2
  6467. data="foo" ></p>
  6468. ````````````````````````````````
  6469. With attributes:
  6470. ```````````````````````````````` example
  6471. <a foo="bar" bam = 'baz <em>"</em>'
  6472. _boolean zoop:33=zoop:33 />
  6473. .
  6474. <p><a foo="bar" bam = 'baz <em>"</em>'
  6475. _boolean zoop:33=zoop:33 /></p>
  6476. ````````````````````````````````
  6477. Custom tag names can be used:
  6478. ```````````````````````````````` example
  6479. Foo <responsive-image src="foo.jpg" />
  6480. .
  6481. <p>Foo <responsive-image src="foo.jpg" /></p>
  6482. ````````````````````````````````
  6483. Illegal tag names, not parsed as HTML:
  6484. ```````````````````````````````` example
  6485. <33> <__>
  6486. .
  6487. <p>&lt;33&gt; &lt;__&gt;</p>
  6488. ````````````````````````````````
  6489. Illegal attribute names:
  6490. ```````````````````````````````` example
  6491. <a h*#ref="hi">
  6492. .
  6493. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6494. ````````````````````````````````
  6495. Illegal attribute values:
  6496. ```````````````````````````````` example
  6497. <a href="hi'> <a href=hi'>
  6498. .
  6499. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6500. ````````````````````````````````
  6501. Illegal [whitespace]:
  6502. ```````````````````````````````` example
  6503. < a><
  6504. foo><bar/ >
  6505. .
  6506. <p>&lt; a&gt;&lt;
  6507. foo&gt;&lt;bar/ &gt;</p>
  6508. ````````````````````````````````
  6509. Missing [whitespace]:
  6510. ```````````````````````````````` example
  6511. <a href='bar'title=title>
  6512. .
  6513. <p>&lt;a href='bar'title=title&gt;</p>
  6514. ````````````````````````````````
  6515. Closing tags:
  6516. ```````````````````````````````` example
  6517. </a></foo >
  6518. .
  6519. <p></a></foo ></p>
  6520. ````````````````````````````````
  6521. Illegal attributes in closing tag:
  6522. ```````````````````````````````` example
  6523. </a href="foo">
  6524. .
  6525. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6526. ````````````````````````````````
  6527. Comments:
  6528. ```````````````````````````````` example
  6529. foo <!-- this is a
  6530. comment - with hyphen -->
  6531. .
  6532. <p>foo <!-- this is a
  6533. comment - with hyphen --></p>
  6534. ````````````````````````````````
  6535. ```````````````````````````````` example
  6536. foo <!-- not a comment -- two hyphens -->
  6537. .
  6538. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6539. ````````````````````````````````
  6540. Not comments:
  6541. ```````````````````````````````` example
  6542. foo <!--> foo -->
  6543. foo <!-- foo--->
  6544. .
  6545. <p>foo &lt;!--&gt; foo --&gt;</p>
  6546. <p>foo &lt;!-- foo---&gt;</p>
  6547. ````````````````````````````````
  6548. Processing instructions:
  6549. ```````````````````````````````` example
  6550. foo <?php echo $a; ?>
  6551. .
  6552. <p>foo <?php echo $a; ?></p>
  6553. ````````````````````````````````
  6554. Declarations:
  6555. ```````````````````````````````` example
  6556. foo <!ELEMENT br EMPTY>
  6557. .
  6558. <p>foo <!ELEMENT br EMPTY></p>
  6559. ````````````````````````````````
  6560. CDATA sections:
  6561. ```````````````````````````````` example
  6562. foo <![CDATA[>&<]]>
  6563. .
  6564. <p>foo <![CDATA[>&<]]></p>
  6565. ````````````````````````````````
  6566. Entity and numeric character references are preserved in HTML
  6567. attributes:
  6568. ```````````````````````````````` example
  6569. foo <a href="&ouml;">
  6570. .
  6571. <p>foo <a href="&ouml;"></p>
  6572. ````````````````````````````````
  6573. Backslash escapes do not work in HTML attributes:
  6574. ```````````````````````````````` example
  6575. foo <a href="\*">
  6576. .
  6577. <p>foo <a href="\*"></p>
  6578. ````````````````````````````````
  6579. ```````````````````````````````` example
  6580. <a href="\"">
  6581. .
  6582. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6583. ````````````````````````````````
  6584. ## Hard line breaks
  6585. A line break (not in a code span or HTML tag) that is preceded
  6586. by two or more spaces and does not occur at the end of a block
  6587. is parsed as a [hard line break](@) (rendered
  6588. in HTML as a `<br />` tag):
  6589. ```````````````````````````````` example
  6590. foo
  6591. baz
  6592. .
  6593. <p>foo<br />
  6594. baz</p>
  6595. ````````````````````````````````
  6596. For a more visible alternative, a backslash before the
  6597. [line ending] may be used instead of two spaces:
  6598. ```````````````````````````````` example
  6599. foo\
  6600. baz
  6601. .
  6602. <p>foo<br />
  6603. baz</p>
  6604. ````````````````````````````````
  6605. More than two spaces can be used:
  6606. ```````````````````````````````` example
  6607. foo
  6608. baz
  6609. .
  6610. <p>foo<br />
  6611. baz</p>
  6612. ````````````````````````````````
  6613. Leading spaces at the beginning of the next line are ignored:
  6614. ```````````````````````````````` example
  6615. foo
  6616. bar
  6617. .
  6618. <p>foo<br />
  6619. bar</p>
  6620. ````````````````````````````````
  6621. ```````````````````````````````` example
  6622. foo\
  6623. bar
  6624. .
  6625. <p>foo<br />
  6626. bar</p>
  6627. ````````````````````````````````
  6628. Line breaks can occur inside emphasis, links, and other constructs
  6629. that allow inline content:
  6630. ```````````````````````````````` example
  6631. *foo
  6632. bar*
  6633. .
  6634. <p><em>foo<br />
  6635. bar</em></p>
  6636. ````````````````````````````````
  6637. ```````````````````````````````` example
  6638. *foo\
  6639. bar*
  6640. .
  6641. <p><em>foo<br />
  6642. bar</em></p>
  6643. ````````````````````````````````
  6644. Line breaks do not occur inside code spans
  6645. ```````````````````````````````` example
  6646. `code
  6647. span`
  6648. .
  6649. <p><code>code span</code></p>
  6650. ````````````````````````````````
  6651. ```````````````````````````````` example
  6652. `code\
  6653. span`
  6654. .
  6655. <p><code>code\ span</code></p>
  6656. ````````````````````````````````
  6657. or HTML tags:
  6658. ```````````````````````````````` example
  6659. <a href="foo
  6660. bar">
  6661. .
  6662. <p><a href="foo
  6663. bar"></p>
  6664. ````````````````````````````````
  6665. ```````````````````````````````` example
  6666. <a href="foo\
  6667. bar">
  6668. .
  6669. <p><a href="foo\
  6670. bar"></p>
  6671. ````````````````````````````````
  6672. Hard line breaks are for separating inline content within a block.
  6673. Neither syntax for hard line breaks works at the end of a paragraph or
  6674. other block element:
  6675. ```````````````````````````````` example
  6676. foo\
  6677. .
  6678. <p>foo\</p>
  6679. ````````````````````````````````
  6680. ```````````````````````````````` example
  6681. foo
  6682. .
  6683. <p>foo</p>
  6684. ````````````````````````````````
  6685. ```````````````````````````````` example
  6686. ### foo\
  6687. .
  6688. <h3>foo\</h3>
  6689. ````````````````````````````````
  6690. ```````````````````````````````` example
  6691. ### foo
  6692. .
  6693. <h3>foo</h3>
  6694. ````````````````````````````````
  6695. ## Soft line breaks
  6696. A regular line break (not in a code span or HTML tag) that is not
  6697. preceded by two or more spaces or a backslash is parsed as a
  6698. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6699. [line ending] or as a space. The result will be the same in
  6700. browsers. In the examples here, a [line ending] will be used.)
  6701. ```````````````````````````````` example
  6702. foo
  6703. baz
  6704. .
  6705. <p>foo
  6706. baz</p>
  6707. ````````````````````````````````
  6708. Spaces at the end of the line and beginning of the next line are
  6709. removed:
  6710. ```````````````````````````````` example
  6711. foo
  6712. baz
  6713. .
  6714. <p>foo
  6715. baz</p>
  6716. ````````````````````````````````
  6717. A conforming parser may render a soft line break in HTML either as a
  6718. line break or as a space.
  6719. A renderer may also provide an option to render soft line breaks
  6720. as hard line breaks.
  6721. ## Textual content
  6722. Any characters not given an interpretation by the above rules will
  6723. be parsed as plain textual content.
  6724. ```````````````````````````````` example
  6725. hello $.;'there
  6726. .
  6727. <p>hello $.;'there</p>
  6728. ````````````````````````````````
  6729. ```````````````````````````````` example
  6730. Foo χρῆν
  6731. .
  6732. <p>Foo χρῆν</p>
  6733. ````````````````````````````````
  6734. Internal spaces are preserved verbatim:
  6735. ```````````````````````````````` example
  6736. Multiple spaces
  6737. .
  6738. <p>Multiple spaces</p>
  6739. ````````````````````````````````
  6740. <!-- END TESTS -->
  6741. # Appendix: A parsing strategy
  6742. In this appendix we describe some features of the parsing strategy
  6743. used in the CommonMark reference implementations.
  6744. ## Overview
  6745. Parsing has two phases:
  6746. 1. In the first phase, lines of input are consumed and the block
  6747. structure of the document---its division into paragraphs, block quotes,
  6748. list items, and so on---is constructed. Text is assigned to these
  6749. blocks but not parsed. Link reference definitions are parsed and a
  6750. map of links is constructed.
  6751. 2. In the second phase, the raw text contents of paragraphs and headings
  6752. are parsed into sequences of Markdown inline elements (strings,
  6753. code spans, links, emphasis, and so on), using the map of link
  6754. references constructed in phase 1.
  6755. At each point in processing, the document is represented as a tree of
  6756. **blocks**. The root of the tree is a `document` block. The `document`
  6757. may have any number of other blocks as **children**. These children
  6758. may, in turn, have other blocks as children. The last child of a block
  6759. is normally considered **open**, meaning that subsequent lines of input
  6760. can alter its contents. (Blocks that are not open are **closed**.)
  6761. Here, for example, is a possible document tree, with the open blocks
  6762. marked by arrows:
  6763. ``` tree
  6764. -> document
  6765. -> block_quote
  6766. paragraph
  6767. "Lorem ipsum dolor\nsit amet."
  6768. -> list (type=bullet tight=true bullet_char=-)
  6769. list_item
  6770. paragraph
  6771. "Qui *quodsi iracundia*"
  6772. -> list_item
  6773. -> paragraph
  6774. "aliquando id"
  6775. ```
  6776. ## Phase 1: block structure
  6777. Each line that is processed has an effect on this tree. The line is
  6778. analyzed and, depending on its contents, the document may be altered
  6779. in one or more of the following ways:
  6780. 1. One or more open blocks may be closed.
  6781. 2. One or more new blocks may be created as children of the
  6782. last open block.
  6783. 3. Text may be added to the last (deepest) open block remaining
  6784. on the tree.
  6785. Once a line has been incorporated into the tree in this way,
  6786. it can be discarded, so input can be read in a stream.
  6787. For each line, we follow this procedure:
  6788. 1. First we iterate through the open blocks, starting with the
  6789. root document, and descending through last children down to the last
  6790. open block. Each block imposes a condition that the line must satisfy
  6791. if the block is to remain open. For example, a block quote requires a
  6792. `>` character. A paragraph requires a non-blank line.
  6793. In this phase we may match all or just some of the open
  6794. blocks. But we cannot close unmatched blocks yet, because we may have a
  6795. [lazy continuation line].
  6796. 2. Next, after consuming the continuation markers for existing
  6797. blocks, we look for new block starts (e.g. `>` for a block quote.
  6798. If we encounter a new block start, we close any blocks unmatched
  6799. in step 1 before creating the new block as a child of the last
  6800. matched block.
  6801. 3. Finally, we look at the remainder of the line (after block
  6802. markers like `>`, list markers, and indentation have been consumed).
  6803. This is text that can be incorporated into the last open
  6804. block (a paragraph, code block, heading, or raw HTML).
  6805. Setext headings are formed when we see a line of a paragraph
  6806. that is a [setext heading underline].
  6807. Reference link definitions are detected when a paragraph is closed;
  6808. the accumulated text lines are parsed to see if they begin with
  6809. one or more reference link definitions. Any remainder becomes a
  6810. normal paragraph.
  6811. We can see how this works by considering how the tree above is
  6812. generated by four lines of Markdown:
  6813. ``` markdown
  6814. > Lorem ipsum dolor
  6815. sit amet.
  6816. > - Qui *quodsi iracundia*
  6817. > - aliquando id
  6818. ```
  6819. At the outset, our document model is just
  6820. ``` tree
  6821. -> document
  6822. ```
  6823. The first line of our text,
  6824. ``` markdown
  6825. > Lorem ipsum dolor
  6826. ```
  6827. causes a `block_quote` block to be created as a child of our
  6828. open `document` block, and a `paragraph` block as a child of
  6829. the `block_quote`. Then the text is added to the last open
  6830. block, the `paragraph`:
  6831. ``` tree
  6832. -> document
  6833. -> block_quote
  6834. -> paragraph
  6835. "Lorem ipsum dolor"
  6836. ```
  6837. The next line,
  6838. ``` markdown
  6839. sit amet.
  6840. ```
  6841. is a "lazy continuation" of the open `paragraph`, so it gets added
  6842. to the paragraph's text:
  6843. ``` tree
  6844. -> document
  6845. -> block_quote
  6846. -> paragraph
  6847. "Lorem ipsum dolor\nsit amet."
  6848. ```
  6849. The third line,
  6850. ``` markdown
  6851. > - Qui *quodsi iracundia*
  6852. ```
  6853. causes the `paragraph` block to be closed, and a new `list` block
  6854. opened as a child of the `block_quote`. A `list_item` is also
  6855. added as a child of the `list`, and a `paragraph` as a child of
  6856. the `list_item`. The text is then added to the new `paragraph`:
  6857. ``` tree
  6858. -> document
  6859. -> block_quote
  6860. paragraph
  6861. "Lorem ipsum dolor\nsit amet."
  6862. -> list (type=bullet tight=true bullet_char=-)
  6863. -> list_item
  6864. -> paragraph
  6865. "Qui *quodsi iracundia*"
  6866. ```
  6867. The fourth line,
  6868. ``` markdown
  6869. > - aliquando id
  6870. ```
  6871. causes the `list_item` (and its child the `paragraph`) to be closed,
  6872. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6873. is added as a child of the new `list_item`, to contain the text.
  6874. We thus obtain the final tree:
  6875. ``` tree
  6876. -> document
  6877. -> block_quote
  6878. paragraph
  6879. "Lorem ipsum dolor\nsit amet."
  6880. -> list (type=bullet tight=true bullet_char=-)
  6881. list_item
  6882. paragraph
  6883. "Qui *quodsi iracundia*"
  6884. -> list_item
  6885. -> paragraph
  6886. "aliquando id"
  6887. ```
  6888. ## Phase 2: inline structure
  6889. Once all of the input has been parsed, all open blocks are closed.
  6890. We then "walk the tree," visiting every node, and parse raw
  6891. string contents of paragraphs and headings as inlines. At this
  6892. point we have seen all the link reference definitions, so we can
  6893. resolve reference links as we go.
  6894. ``` tree
  6895. document
  6896. block_quote
  6897. paragraph
  6898. str "Lorem ipsum dolor"
  6899. softbreak
  6900. str "sit amet."
  6901. list (type=bullet tight=true bullet_char=-)
  6902. list_item
  6903. paragraph
  6904. str "Qui "
  6905. emph
  6906. str "quodsi iracundia"
  6907. list_item
  6908. paragraph
  6909. str "aliquando id"
  6910. ```
  6911. Notice how the [line ending] in the first paragraph has
  6912. been parsed as a `softbreak`, and the asterisks in the first list item
  6913. have become an `emph`.
  6914. ### An algorithm for parsing nested emphasis and links
  6915. By far the trickiest part of inline parsing is handling emphasis,
  6916. strong emphasis, links, and images. This is done using the following
  6917. algorithm.
  6918. When we're parsing inlines and we hit either
  6919. - a run of `*` or `_` characters, or
  6920. - a `[` or `![`
  6921. we insert a text node with these symbols as its literal content, and we
  6922. add a pointer to this text node to the [delimiter stack](@).
  6923. The [delimiter stack] is a doubly linked list. Each
  6924. element contains a pointer to a text node, plus information about
  6925. - the type of delimiter (`[`, `![`, `*`, `_`)
  6926. - the number of delimiters,
  6927. - whether the delimiter is "active" (all are active to start), and
  6928. - whether the delimiter is a potential opener, a potential closer,
  6929. or both (which depends on what sort of characters precede
  6930. and follow the delimiters).
  6931. When we hit a `]` character, we call the *look for link or image*
  6932. procedure (see below).
  6933. When we hit the end of the input, we call the *process emphasis*
  6934. procedure (see below), with `stack_bottom` = NULL.
  6935. #### *look for link or image*
  6936. Starting at the top of the delimiter stack, we look backwards
  6937. through the stack for an opening `[` or `![` delimiter.
  6938. - If we don't find one, we return a literal text node `]`.
  6939. - If we do find one, but it's not *active*, we remove the inactive
  6940. delimiter from the stack, and return a literal text node `]`.
  6941. - If we find one and it's active, then we parse ahead to see if
  6942. we have an inline link/image, reference link/image, compact reference
  6943. link/image, or shortcut reference link/image.
  6944. + If we don't, then we remove the opening delimiter from the
  6945. delimiter stack and return a literal text node `]`.
  6946. + If we do, then
  6947. * We return a link or image node whose children are the inlines
  6948. after the text node pointed to by the opening delimiter.
  6949. * We run *process emphasis* on these inlines, with the `[` opener
  6950. as `stack_bottom`.
  6951. * We remove the opening delimiter.
  6952. * If we have a link (and not an image), we also set all
  6953. `[` delimiters before the opening delimiter to *inactive*. (This
  6954. will prevent us from getting links within links.)
  6955. #### *process emphasis*
  6956. Parameter `stack_bottom` sets a lower bound to how far we
  6957. descend in the [delimiter stack]. If it is NULL, we can
  6958. go all the way to the bottom. Otherwise, we stop before
  6959. visiting `stack_bottom`.
  6960. Let `current_position` point to the element on the [delimiter stack]
  6961. just above `stack_bottom` (or the first element if `stack_bottom`
  6962. is NULL).
  6963. We keep track of the `openers_bottom` for each delimiter
  6964. type (`*`, `_`). Initialize this to `stack_bottom`.
  6965. Then we repeat the following until we run out of potential
  6966. closers:
  6967. - Move `current_position` forward in the delimiter stack (if needed)
  6968. until we find the first potential closer with delimiter `*` or `_`.
  6969. (This will be the potential closer closest
  6970. to the beginning of the input -- the first one in parse order.)
  6971. - Now, look back in the stack (staying above `stack_bottom` and
  6972. the `openers_bottom` for this delimiter type) for the
  6973. first matching potential opener ("matching" means same delimiter).
  6974. - If one is found:
  6975. + Figure out whether we have emphasis or strong emphasis:
  6976. if both closer and opener spans have length >= 2, we have
  6977. strong, otherwise regular.
  6978. + Insert an emph or strong emph node accordingly, after
  6979. the text node corresponding to the opener.
  6980. + Remove any delimiters between the opener and closer from
  6981. the delimiter stack.
  6982. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  6983. from the opening and closing text nodes. If they become empty
  6984. as a result, remove them and remove the corresponding element
  6985. of the delimiter stack. If the closing node is removed, reset
  6986. `current_position` to the next element in the stack.
  6987. - If none in found:
  6988. + Set `openers_bottom` to the element before `current_position`.
  6989. (We know that there are no openers for this kind of closer up to and
  6990. including this point, so this puts a lower bound on future searches.)
  6991. + If the closer at `current_position` is not a potential opener,
  6992. remove it from the delimiter stack (since we know it can't
  6993. be a closer either).
  6994. + Advance `current_position` to the next element in the stack.
  6995. After we're done, we remove all delimiters above `stack_bottom` from the
  6996. delimiter stack.