aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 3e8d968573fd4ee21827183009a48ec60461e97b (plain)
  1. ---
  2. title: CommonMark Spec
  3. author:
  4. - John MacFarlane
  5. version: 0.12
  6. date: 2014-11-10
  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. widely used in websites. By 2014 there were dozens of
  15. implementations in many languages. Some of them extended basic
  16. Markdown syntax with conventions for footnotes, definition lists,
  17. tables, and other constructs, and some allowed output not just in
  18. HTML but in LaTeX and many other formats.
  19. ## Why is a spec needed?
  20. John Gruber's [canonical description of Markdown's
  21. syntax](http://daringfireball.net/projects/markdown/syntax)
  22. does not specify the syntax unambiguously. Here are some examples of
  23. questions it does not answer:
  24. 1. How much indentation is needed for a sublist? The spec says that
  25. continuation paragraphs need to be indented four spaces, but is
  26. not fully explicit about sublists. It is natural to think that
  27. they, too, must be indented four spaces, but `Markdown.pl` does
  28. not require that. This is hardly a "corner case," and divergences
  29. between implementations on this issue often lead to surprises for
  30. users in real documents. (See [this comment by John
  31. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  32. 2. Is a blank line needed before a block quote or header?
  33. Most implementations do not require the blank line. However,
  34. this can lead to unexpected results in hard-wrapped text, and
  35. also to ambiguities in parsing (note that some implementations
  36. put the header inside the blockquote, while others do not).
  37. (John Gruber has also spoken [in favor of requiring the blank
  38. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  39. 3. Is a blank line needed before an indented code block?
  40. (`Markdown.pl` requires it, but this is not mentioned in the
  41. documentation, and some implementations do not require it.)
  42. ``` markdown
  43. paragraph
  44. code?
  45. ```
  46. 4. What is the exact rule for determining when list items get
  47. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  48. "tight"? What should we do with a list like this?
  49. ``` markdown
  50. 1. one
  51. 2. two
  52. 3. three
  53. ```
  54. Or this?
  55. ``` markdown
  56. 1. one
  57. - a
  58. - b
  59. 2. two
  60. ```
  61. (There are some relevant comments by John Gruber
  62. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  63. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  64. ``` markdown
  65. 8. item 1
  66. 9. item 2
  67. 10. item 2a
  68. ```
  69. 6. Is this one list with a horizontal rule in its second item,
  70. or two lists separated by a horizontal rule?
  71. ``` markdown
  72. * a
  73. * * * * *
  74. * b
  75. ```
  76. 7. When list markers change from numbers to bullets, do we have
  77. two lists or one? (The Markdown syntax description suggests two,
  78. but the perl scripts and many other implementations produce one.)
  79. ``` markdown
  80. 1. fee
  81. 2. fie
  82. - foe
  83. - fum
  84. ```
  85. 8. What are the precedence rules for the markers of inline structure?
  86. For example, is the following a valid link, or does the code span
  87. take precedence ?
  88. ``` markdown
  89. [a backtick (`)](/url) and [another backtick (`)](/url).
  90. ```
  91. 9. What are the precedence rules for markers of emphasis and strong
  92. emphasis? For example, how should the following be parsed?
  93. ``` markdown
  94. *foo *bar* baz*
  95. ```
  96. 10. What are the precedence rules between block-level and inline-level
  97. structure? For example, how should the following be parsed?
  98. ``` markdown
  99. - `a long code span can contain a hyphen like this
  100. - and it can screw things up`
  101. ```
  102. 11. Can list items include headers? (`Markdown.pl` does not allow this,
  103. but headers can occur in blockquotes.)
  104. ``` markdown
  105. - # Heading
  106. ```
  107. 12. Can link references be defined inside block quotes or list items?
  108. ``` markdown
  109. > Blockquote [foo].
  110. >
  111. > [foo]: /url
  112. ```
  113. 13. If there are multiple definitions for the same reference, which takes
  114. precedence?
  115. ``` markdown
  116. [foo]: /url1
  117. [foo]: /url2
  118. [foo][]
  119. ```
  120. In the absence of a spec, early implementers consulted `Markdown.pl`
  121. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  122. gave manifestly bad results in many cases, so it was not a
  123. satisfactory replacement for a spec.
  124. Because there is no unambiguous spec, implementations have diverged
  125. considerably. As a result, users are often surprised to find that
  126. a document that renders one way on one system (say, a github wiki)
  127. renders differently on another (say, converting to docbook using
  128. pandoc). To make matters worse, because nothing in Markdown counts
  129. as a "syntax error," the divergence often isn't discovered right away.
  130. ## About this document
  131. This document attempts to specify Markdown syntax unambiguously.
  132. It contains many examples with side-by-side Markdown and
  133. HTML. These are intended to double as conformance tests. An
  134. accompanying script `spec_tests.py` can be used to run the tests
  135. against any Markdown program:
  136. python spec_tests.py --spec spec.txt --program PROGRAM
  137. Since this document describes how Markdown is to be parsed into
  138. an abstract syntax tree, it would have made sense to use an abstract
  139. representation of the syntax tree instead of HTML. But HTML is capable
  140. of representing the structural distinctions we need to make, and the
  141. choice of HTML for the tests makes it possible to run the tests against
  142. an implementation without writing an abstract syntax tree renderer.
  143. This document is generated from a text file, `spec.txt`, written
  144. in Markdown with a small extension for the side-by-side tests.
  145. The script `spec2md.pl` can be used to turn `spec.txt` into pandoc
  146. Markdown, which can then be converted into other formats.
  147. In the examples, the `→` character is used to represent tabs.
  148. # Preprocessing
  149. A [line](@line)
  150. is a sequence of zero or more [characters](#character) followed by a
  151. line ending (CR, LF, or CRLF) or by the end of file.
  152. A [character](@character) is a unicode code point.
  153. This spec does not specify an encoding; it thinks of lines as composed
  154. of characters rather than bytes. A conforming parser may be limited
  155. to a certain encoding.
  156. Tabs in lines are expanded to spaces, with a tab stop of 4 characters:
  157. .
  158. →foo→baz→→bim
  159. .
  160. <pre><code>foo baz bim
  161. </code></pre>
  162. .
  163. .
  164. a→a
  165. ὐ→a
  166. .
  167. <pre><code>a a
  168. ὐ a
  169. </code></pre>
  170. .
  171. Line endings are replaced by newline characters (LF).
  172. A line containing no characters, or a line containing only spaces (after
  173. tab expansion), is called a [blank line](@blank-line).
  174. For security reasons, a conforming parser must strip or replace the
  175. Unicode character `U+0000`.
  176. # Blocks and inlines
  177. We can think of a document as a sequence of
  178. [blocks](@block)---structural
  179. elements like paragraphs, block quotations,
  180. lists, headers, rules, and code blocks. Blocks can contain other
  181. blocks, or they can contain [inline](@inline) content:
  182. words, spaces, links, emphasized text, images, and inline code.
  183. ## Precedence
  184. Indicators of block structure always take precedence over indicators
  185. of inline structure. So, for example, the following is a list with
  186. two items, not a list with one item containing a code span:
  187. .
  188. - `one
  189. - two`
  190. .
  191. <ul>
  192. <li>`one</li>
  193. <li>two`</li>
  194. </ul>
  195. .
  196. This means that parsing can proceed in two steps: first, the block
  197. structure of the document can be discerned; second, text lines inside
  198. paragraphs, headers, and other block constructs can be parsed for inline
  199. structure. The second step requires information about link reference
  200. definitions that will be available only at the end of the first
  201. step. Note that the first step requires processing lines in sequence,
  202. but the second can be parallelized, since the inline parsing of
  203. one block element does not affect the inline parsing of any other.
  204. ## Container blocks and leaf blocks
  205. We can divide blocks into two types:
  206. [container blocks](@container-block),
  207. which can contain other blocks, and [leaf blocks](@leaf-block),
  208. which cannot.
  209. # Leaf blocks
  210. This section describes the different kinds of leaf block that make up a
  211. Markdown document.
  212. ## Horizontal rules
  213. A line consisting of 0-3 spaces of indentation, followed by a sequence
  214. of three or more matching `-`, `_`, or `*` characters, each followed
  215. optionally by any number of spaces, forms a [horizontal
  216. rule](@horizontal-rule).
  217. .
  218. ***
  219. ---
  220. ___
  221. .
  222. <hr />
  223. <hr />
  224. <hr />
  225. .
  226. Wrong characters:
  227. .
  228. +++
  229. .
  230. <p>+++</p>
  231. .
  232. .
  233. ===
  234. .
  235. <p>===</p>
  236. .
  237. Not enough characters:
  238. .
  239. --
  240. **
  241. __
  242. .
  243. <p>--
  244. **
  245. __</p>
  246. .
  247. One to three spaces indent are allowed:
  248. .
  249. ***
  250. ***
  251. ***
  252. .
  253. <hr />
  254. <hr />
  255. <hr />
  256. .
  257. Four spaces is too many:
  258. .
  259. ***
  260. .
  261. <pre><code>***
  262. </code></pre>
  263. .
  264. .
  265. Foo
  266. ***
  267. .
  268. <p>Foo
  269. ***</p>
  270. .
  271. More than three characters may be used:
  272. .
  273. _____________________________________
  274. .
  275. <hr />
  276. .
  277. Spaces are allowed between the characters:
  278. .
  279. - - -
  280. .
  281. <hr />
  282. .
  283. .
  284. ** * ** * ** * **
  285. .
  286. <hr />
  287. .
  288. .
  289. - - - -
  290. .
  291. <hr />
  292. .
  293. Spaces are allowed at the end:
  294. .
  295. - - - -
  296. .
  297. <hr />
  298. .
  299. However, no other characters may occur in the line:
  300. .
  301. _ _ _ _ a
  302. a------
  303. ---a---
  304. .
  305. <p>_ _ _ _ a</p>
  306. <p>a------</p>
  307. <p>---a---</p>
  308. .
  309. It is required that all of the non-space characters be the same.
  310. So, this is not a horizontal rule:
  311. .
  312. *-*
  313. .
  314. <p><em>-</em></p>
  315. .
  316. Horizontal rules do not need blank lines before or after:
  317. .
  318. - foo
  319. ***
  320. - bar
  321. .
  322. <ul>
  323. <li>foo</li>
  324. </ul>
  325. <hr />
  326. <ul>
  327. <li>bar</li>
  328. </ul>
  329. .
  330. Horizontal rules can interrupt a paragraph:
  331. .
  332. Foo
  333. ***
  334. bar
  335. .
  336. <p>Foo</p>
  337. <hr />
  338. <p>bar</p>
  339. .
  340. If a line of dashes that meets the above conditions for being a
  341. horizontal rule could also be interpreted as the underline of a [setext
  342. header](#setext-header), the interpretation as a
  343. [setext-header](#setext-header) takes precedence. Thus, for example,
  344. this is a setext header, not a paragraph followed by a horizontal rule:
  345. .
  346. Foo
  347. ---
  348. bar
  349. .
  350. <h2>Foo</h2>
  351. <p>bar</p>
  352. .
  353. When both a horizontal rule and a list item are possible
  354. interpretations of a line, the horizontal rule is preferred:
  355. .
  356. * Foo
  357. * * *
  358. * Bar
  359. .
  360. <ul>
  361. <li>Foo</li>
  362. </ul>
  363. <hr />
  364. <ul>
  365. <li>Bar</li>
  366. </ul>
  367. .
  368. If you want a horizontal rule in a list item, use a different bullet:
  369. .
  370. - Foo
  371. - * * *
  372. .
  373. <ul>
  374. <li>Foo</li>
  375. <li>
  376. <hr />
  377. </li>
  378. </ul>
  379. .
  380. ## ATX headers
  381. An [ATX header](@atx-header)
  382. consists of a string of characters, parsed as inline content, between an
  383. opening sequence of 1--6 unescaped `#` characters and an optional
  384. closing sequence of any number of `#` characters. The opening sequence
  385. of `#` characters cannot be followed directly by a nonspace character.
  386. The optional closing sequence of `#`s must be preceded by a space and may be
  387. followed by spaces only. The opening `#` character may be indented 0-3
  388. spaces. The raw contents of the header are stripped of leading and
  389. trailing spaces before being parsed as inline content. The header level
  390. is equal to the number of `#` characters in the opening sequence.
  391. Simple headers:
  392. .
  393. # foo
  394. ## foo
  395. ### foo
  396. #### foo
  397. ##### foo
  398. ###### foo
  399. .
  400. <h1>foo</h1>
  401. <h2>foo</h2>
  402. <h3>foo</h3>
  403. <h4>foo</h4>
  404. <h5>foo</h5>
  405. <h6>foo</h6>
  406. .
  407. More than six `#` characters is not a header:
  408. .
  409. ####### foo
  410. .
  411. <p>####### foo</p>
  412. .
  413. A space is required between the `#` characters and the header's
  414. contents. Note that many implementations currently do not require
  415. the space. However, the space was required by the [original ATX
  416. implementation](http://www.aaronsw.com/2002/atx/atx.py), and it helps
  417. prevent things like the following from being parsed as headers:
  418. .
  419. #5 bolt
  420. .
  421. <p>#5 bolt</p>
  422. .
  423. This is not a header, because the first `#` is escaped:
  424. .
  425. \## foo
  426. .
  427. <p>## foo</p>
  428. .
  429. Contents are parsed as inlines:
  430. .
  431. # foo *bar* \*baz\*
  432. .
  433. <h1>foo <em>bar</em> *baz*</h1>
  434. .
  435. Leading and trailing blanks are ignored in parsing inline content:
  436. .
  437. # foo
  438. .
  439. <h1>foo</h1>
  440. .
  441. One to three spaces indentation are allowed:
  442. .
  443. ### foo
  444. ## foo
  445. # foo
  446. .
  447. <h3>foo</h3>
  448. <h2>foo</h2>
  449. <h1>foo</h1>
  450. .
  451. Four spaces are too much:
  452. .
  453. # foo
  454. .
  455. <pre><code># foo
  456. </code></pre>
  457. .
  458. .
  459. foo
  460. # bar
  461. .
  462. <p>foo
  463. # bar</p>
  464. .
  465. A closing sequence of `#` characters is optional:
  466. .
  467. ## foo ##
  468. ### bar ###
  469. .
  470. <h2>foo</h2>
  471. <h3>bar</h3>
  472. .
  473. It need not be the same length as the opening sequence:
  474. .
  475. # foo ##################################
  476. ##### foo ##
  477. .
  478. <h1>foo</h1>
  479. <h5>foo</h5>
  480. .
  481. Spaces are allowed after the closing sequence:
  482. .
  483. ### foo ###
  484. .
  485. <h3>foo</h3>
  486. .
  487. A sequence of `#` characters with a nonspace character following it
  488. is not a closing sequence, but counts as part of the contents of the
  489. header:
  490. .
  491. ### foo ### b
  492. .
  493. <h3>foo ### b</h3>
  494. .
  495. The closing sequence must be preceded by a space:
  496. .
  497. # foo#
  498. .
  499. <h1>foo#</h1>
  500. .
  501. Backslash-escaped `#` characters do not count as part
  502. of the closing sequence:
  503. .
  504. ### foo \###
  505. ## foo #\##
  506. # foo \#
  507. .
  508. <h3>foo ###</h3>
  509. <h2>foo ###</h2>
  510. <h1>foo #</h1>
  511. .
  512. ATX headers need not be separated from surrounding content by blank
  513. lines, and they can interrupt paragraphs:
  514. .
  515. ****
  516. ## foo
  517. ****
  518. .
  519. <hr />
  520. <h2>foo</h2>
  521. <hr />
  522. .
  523. .
  524. Foo bar
  525. # baz
  526. Bar foo
  527. .
  528. <p>Foo bar</p>
  529. <h1>baz</h1>
  530. <p>Bar foo</p>
  531. .
  532. ATX headers can be empty:
  533. .
  534. ##
  535. #
  536. ### ###
  537. .
  538. <h2></h2>
  539. <h1></h1>
  540. <h3></h3>
  541. .
  542. ## Setext headers
  543. A [setext header](@setext-header)
  544. consists of a line of text, containing at least one nonspace character,
  545. with no more than 3 spaces indentation, followed by a [setext header
  546. underline](#setext-header-underline). The line of text must be
  547. one that, were it not followed by the setext header underline,
  548. would be interpreted as part of a paragraph: it cannot be a code
  549. block, header, blockquote, horizontal rule, or list. A [setext header
  550. underline](@setext-header-underline)
  551. is a sequence of `=` characters or a sequence of `-` characters, with no
  552. more than 3 spaces indentation and any number of trailing
  553. spaces. The header is a level 1 header if `=` characters are used, and
  554. a level 2 header if `-` characters are used. The contents of the header
  555. are the result of parsing the first line as Markdown inline content.
  556. In general, a setext header need not be preceded or followed by a
  557. blank line. However, it cannot interrupt a paragraph, so when a
  558. setext header comes after a paragraph, a blank line is needed between
  559. them.
  560. Simple examples:
  561. .
  562. Foo *bar*
  563. =========
  564. Foo *bar*
  565. ---------
  566. .
  567. <h1>Foo <em>bar</em></h1>
  568. <h2>Foo <em>bar</em></h2>
  569. .
  570. The underlining can be any length:
  571. .
  572. Foo
  573. -------------------------
  574. Foo
  575. =
  576. .
  577. <h2>Foo</h2>
  578. <h1>Foo</h1>
  579. .
  580. The header content can be indented up to three spaces, and need
  581. not line up with the underlining:
  582. .
  583. Foo
  584. ---
  585. Foo
  586. -----
  587. Foo
  588. ===
  589. .
  590. <h2>Foo</h2>
  591. <h2>Foo</h2>
  592. <h1>Foo</h1>
  593. .
  594. Four spaces indent is too much:
  595. .
  596. Foo
  597. ---
  598. Foo
  599. ---
  600. .
  601. <pre><code>Foo
  602. ---
  603. Foo
  604. </code></pre>
  605. <hr />
  606. .
  607. The setext header underline can be indented up to three spaces, and
  608. may have trailing spaces:
  609. .
  610. Foo
  611. ----
  612. .
  613. <h2>Foo</h2>
  614. .
  615. Four spaces is too much:
  616. .
  617. Foo
  618. ---
  619. .
  620. <p>Foo
  621. ---</p>
  622. .
  623. The setext header underline cannot contain internal spaces:
  624. .
  625. Foo
  626. = =
  627. Foo
  628. --- -
  629. .
  630. <p>Foo
  631. = =</p>
  632. <p>Foo</p>
  633. <hr />
  634. .
  635. Trailing spaces in the content line do not cause a line break:
  636. .
  637. Foo
  638. -----
  639. .
  640. <h2>Foo</h2>
  641. .
  642. Nor does a backslash at the end:
  643. .
  644. Foo\
  645. ----
  646. .
  647. <h2>Foo\</h2>
  648. .
  649. Since indicators of block structure take precedence over
  650. indicators of inline structure, the following are setext headers:
  651. .
  652. `Foo
  653. ----
  654. `
  655. <a title="a lot
  656. ---
  657. of dashes"/>
  658. .
  659. <h2>`Foo</h2>
  660. <p>`</p>
  661. <h2>&lt;a title=&quot;a lot</h2>
  662. <p>of dashes&quot;/&gt;</p>
  663. .
  664. The setext header underline cannot be a [lazy continuation
  665. line](#lazy-continuation-line) in a list item or block quote:
  666. .
  667. > Foo
  668. ---
  669. .
  670. <blockquote>
  671. <p>Foo</p>
  672. </blockquote>
  673. <hr />
  674. .
  675. .
  676. - Foo
  677. ---
  678. .
  679. <ul>
  680. <li>Foo</li>
  681. </ul>
  682. <hr />
  683. .
  684. A setext header cannot interrupt a paragraph:
  685. .
  686. Foo
  687. Bar
  688. ---
  689. Foo
  690. Bar
  691. ===
  692. .
  693. <p>Foo
  694. Bar</p>
  695. <hr />
  696. <p>Foo
  697. Bar
  698. ===</p>
  699. .
  700. But in general a blank line is not required before or after:
  701. .
  702. ---
  703. Foo
  704. ---
  705. Bar
  706. ---
  707. Baz
  708. .
  709. <hr />
  710. <h2>Foo</h2>
  711. <h2>Bar</h2>
  712. <p>Baz</p>
  713. .
  714. Setext headers cannot be empty:
  715. .
  716. ====
  717. .
  718. <p>====</p>
  719. .
  720. Setext header text lines must not be interpretable as block
  721. constructs other than paragraphs. So, the line of dashes
  722. in these examples gets interpreted as a horizontal rule:
  723. .
  724. ---
  725. ---
  726. .
  727. <hr />
  728. <hr />
  729. .
  730. .
  731. - foo
  732. -----
  733. .
  734. <ul>
  735. <li>foo</li>
  736. </ul>
  737. <hr />
  738. .
  739. .
  740. foo
  741. ---
  742. .
  743. <pre><code>foo
  744. </code></pre>
  745. <hr />
  746. .
  747. .
  748. > foo
  749. -----
  750. .
  751. <blockquote>
  752. <p>foo</p>
  753. </blockquote>
  754. <hr />
  755. .
  756. If you want a header with `> foo` as its literal text, you can
  757. use backslash escapes:
  758. .
  759. \> foo
  760. ------
  761. .
  762. <h2>&gt; foo</h2>
  763. .
  764. ## Indented code blocks
  765. An [indented code block](@indented-code-block)
  766. is composed of one or more
  767. [indented chunks](#indented-chunk) separated by blank lines.
  768. An [indented chunk](@indented-chunk)
  769. is a sequence of non-blank lines, each indented four or more
  770. spaces. An indented code block cannot interrupt a paragraph, so
  771. if it occurs before or after a paragraph, there must be an
  772. intervening blank line. The contents of the code block are
  773. the literal contents of the lines, including trailing newlines,
  774. minus four spaces of indentation. An indented code block has no
  775. attributes.
  776. .
  777. a simple
  778. indented code block
  779. .
  780. <pre><code>a simple
  781. indented code block
  782. </code></pre>
  783. .
  784. The contents are literal text, and do not get parsed as Markdown:
  785. .
  786. <a/>
  787. *hi*
  788. - one
  789. .
  790. <pre><code>&lt;a/&gt;
  791. *hi*
  792. - one
  793. </code></pre>
  794. .
  795. Here we have three chunks separated by blank lines:
  796. .
  797. chunk1
  798. chunk2
  799. chunk3
  800. .
  801. <pre><code>chunk1
  802. chunk2
  803. chunk3
  804. </code></pre>
  805. .
  806. Any initial spaces beyond four will be included in the content, even
  807. in interior blank lines:
  808. .
  809. chunk1
  810. chunk2
  811. .
  812. <pre><code>chunk1
  813. chunk2
  814. </code></pre>
  815. .
  816. An indented code block cannot interrupt a paragraph. (This
  817. allows hanging indents and the like.)
  818. .
  819. Foo
  820. bar
  821. .
  822. <p>Foo
  823. bar</p>
  824. .
  825. However, any non-blank line with fewer than four leading spaces ends
  826. the code block immediately. So a paragraph may occur immediately
  827. after indented code:
  828. .
  829. foo
  830. bar
  831. .
  832. <pre><code>foo
  833. </code></pre>
  834. <p>bar</p>
  835. .
  836. And indented code can occur immediately before and after other kinds of
  837. blocks:
  838. .
  839. # Header
  840. foo
  841. Header
  842. ------
  843. foo
  844. ----
  845. .
  846. <h1>Header</h1>
  847. <pre><code>foo
  848. </code></pre>
  849. <h2>Header</h2>
  850. <pre><code>foo
  851. </code></pre>
  852. <hr />
  853. .
  854. The first line can be indented more than four spaces:
  855. .
  856. foo
  857. bar
  858. .
  859. <pre><code> foo
  860. bar
  861. </code></pre>
  862. .
  863. Blank lines preceding or following an indented code block
  864. are not included in it:
  865. .
  866. foo
  867. .
  868. <pre><code>foo
  869. </code></pre>
  870. .
  871. Trailing spaces are included in the code block's content:
  872. .
  873. foo
  874. .
  875. <pre><code>foo
  876. </code></pre>
  877. .
  878. ## Fenced code blocks
  879. A [code fence](@code-fence) is a sequence
  880. of at least three consecutive backtick characters (`` ` ``) or
  881. tildes (`~`). (Tildes and backticks cannot be mixed.)
  882. A [fenced code block](@fenced-code-block)
  883. begins with a code fence, indented no more than three spaces.
  884. The line with the opening code fence may optionally contain some text
  885. following the code fence; this is trimmed of leading and trailing
  886. spaces and called the [info string](@info-string).
  887. The info string may not contain any backtick
  888. characters. (The reason for this restriction is that otherwise
  889. some inline code would be incorrectly interpreted as the
  890. beginning of a fenced code block.)
  891. The content of the code block consists of all subsequent lines, until
  892. a closing [code fence](#code-fence) of the same type as the code block
  893. began with (backticks or tildes), and with at least as many backticks
  894. or tildes as the opening code fence. If the leading code fence is
  895. indented N spaces, then up to N spaces of indentation are removed from
  896. each line of the content (if present). (If a content line is not
  897. indented, it is preserved unchanged. If it is indented less than N
  898. spaces, all of the indentation is removed.)
  899. The closing code fence may be indented up to three spaces, and may be
  900. followed only by spaces, which are ignored. If the end of the
  901. containing block (or document) is reached and no closing code fence
  902. has been found, the code block contains all of the lines after the
  903. opening code fence until the end of the containing block (or
  904. document). (An alternative spec would require backtracking in the
  905. event that a closing code fence is not found. But this makes parsing
  906. much less efficient, and there seems to be no real down side to the
  907. behavior described here.)
  908. A fenced code block may interrupt a paragraph, and does not require
  909. a blank line either before or after.
  910. The content of a code fence is treated as literal text, not parsed
  911. as inlines. The first word of the info string is typically used to
  912. specify the language of the code sample, and rendered in the `class`
  913. attribute of the `code` tag. However, this spec does not mandate any
  914. particular treatment of the info string.
  915. Here is a simple example with backticks:
  916. .
  917. ```
  918. <
  919. >
  920. ```
  921. .
  922. <pre><code>&lt;
  923. &gt;
  924. </code></pre>
  925. .
  926. With tildes:
  927. .
  928. ~~~
  929. <
  930. >
  931. ~~~
  932. .
  933. <pre><code>&lt;
  934. &gt;
  935. </code></pre>
  936. .
  937. The closing code fence must use the same character as the opening
  938. fence:
  939. .
  940. ```
  941. aaa
  942. ~~~
  943. ```
  944. .
  945. <pre><code>aaa
  946. ~~~
  947. </code></pre>
  948. .
  949. .
  950. ~~~
  951. aaa
  952. ```
  953. ~~~
  954. .
  955. <pre><code>aaa
  956. ```
  957. </code></pre>
  958. .
  959. The closing code fence must be at least as long as the opening fence:
  960. .
  961. ````
  962. aaa
  963. ```
  964. ``````
  965. .
  966. <pre><code>aaa
  967. ```
  968. </code></pre>
  969. .
  970. .
  971. ~~~~
  972. aaa
  973. ~~~
  974. ~~~~
  975. .
  976. <pre><code>aaa
  977. ~~~
  978. </code></pre>
  979. .
  980. Unclosed code blocks are closed by the end of the document:
  981. .
  982. ```
  983. .
  984. <pre><code></code></pre>
  985. .
  986. .
  987. `````
  988. ```
  989. aaa
  990. .
  991. <pre><code>
  992. ```
  993. aaa
  994. </code></pre>
  995. .
  996. A code block can have all empty lines as its content:
  997. .
  998. ```
  999. ```
  1000. .
  1001. <pre><code>
  1002. </code></pre>
  1003. .
  1004. A code block can be empty:
  1005. .
  1006. ```
  1007. ```
  1008. .
  1009. <pre><code></code></pre>
  1010. .
  1011. Fences can be indented. If the opening fence is indented,
  1012. content lines will have equivalent opening indentation removed,
  1013. if present:
  1014. .
  1015. ```
  1016. aaa
  1017. aaa
  1018. ```
  1019. .
  1020. <pre><code>aaa
  1021. aaa
  1022. </code></pre>
  1023. .
  1024. .
  1025. ```
  1026. aaa
  1027. aaa
  1028. aaa
  1029. ```
  1030. .
  1031. <pre><code>aaa
  1032. aaa
  1033. aaa
  1034. </code></pre>
  1035. .
  1036. .
  1037. ```
  1038. aaa
  1039. aaa
  1040. aaa
  1041. ```
  1042. .
  1043. <pre><code>aaa
  1044. aaa
  1045. aaa
  1046. </code></pre>
  1047. .
  1048. Four spaces indentation produces an indented code block:
  1049. .
  1050. ```
  1051. aaa
  1052. ```
  1053. .
  1054. <pre><code>```
  1055. aaa
  1056. ```
  1057. </code></pre>
  1058. .
  1059. Closing fences may be indented by 0-3 spaces, and their indentation
  1060. need not match that of the opening fence:
  1061. .
  1062. ```
  1063. aaa
  1064. ```
  1065. .
  1066. <pre><code>aaa
  1067. </code></pre>
  1068. .
  1069. .
  1070. ```
  1071. aaa
  1072. ```
  1073. .
  1074. <pre><code>aaa
  1075. </code></pre>
  1076. .
  1077. This is not a closing fence, because it is indented 4 spaces:
  1078. .
  1079. ```
  1080. aaa
  1081. ```
  1082. .
  1083. <pre><code>aaa
  1084. ```
  1085. </code></pre>
  1086. .
  1087. Code fences (opening and closing) cannot contain internal spaces:
  1088. .
  1089. ``` ```
  1090. aaa
  1091. .
  1092. <p><code></code>
  1093. aaa</p>
  1094. .
  1095. .
  1096. ~~~~~~
  1097. aaa
  1098. ~~~ ~~
  1099. .
  1100. <pre><code>aaa
  1101. ~~~ ~~
  1102. </code></pre>
  1103. .
  1104. Fenced code blocks can interrupt paragraphs, and can be followed
  1105. directly by paragraphs, without a blank line between:
  1106. .
  1107. foo
  1108. ```
  1109. bar
  1110. ```
  1111. baz
  1112. .
  1113. <p>foo</p>
  1114. <pre><code>bar
  1115. </code></pre>
  1116. <p>baz</p>
  1117. .
  1118. Other blocks can also occur before and after fenced code blocks
  1119. without an intervening blank line:
  1120. .
  1121. foo
  1122. ---
  1123. ~~~
  1124. bar
  1125. ~~~
  1126. # baz
  1127. .
  1128. <h2>foo</h2>
  1129. <pre><code>bar
  1130. </code></pre>
  1131. <h1>baz</h1>
  1132. .
  1133. An [info string](#info-string) can be provided after the opening code fence.
  1134. Opening and closing spaces will be stripped, and the first word, prefixed
  1135. with `language-`, is used as the value for the `class` attribute of the
  1136. `code` element within the enclosing `pre` element.
  1137. .
  1138. ```ruby
  1139. def foo(x)
  1140. return 3
  1141. end
  1142. ```
  1143. .
  1144. <pre><code class="language-ruby">def foo(x)
  1145. return 3
  1146. end
  1147. </code></pre>
  1148. .
  1149. .
  1150. ~~~~ ruby startline=3 $%@#$
  1151. def foo(x)
  1152. return 3
  1153. end
  1154. ~~~~~~~
  1155. .
  1156. <pre><code class="language-ruby">def foo(x)
  1157. return 3
  1158. end
  1159. </code></pre>
  1160. .
  1161. .
  1162. ````;
  1163. ````
  1164. .
  1165. <pre><code class="language-;"></code></pre>
  1166. .
  1167. Info strings for backtick code blocks cannot contain backticks:
  1168. .
  1169. ``` aa ```
  1170. foo
  1171. .
  1172. <p><code>aa</code>
  1173. foo</p>
  1174. .
  1175. Closing code fences cannot have info strings:
  1176. .
  1177. ```
  1178. ``` aaa
  1179. ```
  1180. .
  1181. <pre><code>``` aaa
  1182. </code></pre>
  1183. .
  1184. ## HTML blocks
  1185. An [HTML block tag](@html-block-tag) is
  1186. an [open tag](#open-tag) or [closing tag](#closing-tag) whose tag
  1187. name is one of the following (case-insensitive):
  1188. `article`, `header`, `aside`, `hgroup`, `blockquote`, `hr`, `iframe`,
  1189. `body`, `li`, `map`, `button`, `object`, `canvas`, `ol`, `caption`,
  1190. `output`, `col`, `p`, `colgroup`, `pre`, `dd`, `progress`, `div`,
  1191. `section`, `dl`, `table`, `td`, `dt`, `tbody`, `embed`, `textarea`,
  1192. `fieldset`, `tfoot`, `figcaption`, `th`, `figure`, `thead`, `footer`,
  1193. `tr`, `form`, `ul`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `video`,
  1194. `script`, `style`.
  1195. An [HTML block](@html-block) begins with an
  1196. [HTML block tag](#html-block-tag), [HTML comment](#html-comment),
  1197. [processing instruction](#processing-instruction),
  1198. [declaration](#declaration), or [CDATA section](#cdata-section).
  1199. It ends when a [blank line](#blank-line) or the end of the
  1200. input is encountered. The initial line may be indented up to three
  1201. spaces, and subsequent lines may have any indentation. The contents
  1202. of the HTML block are interpreted as raw HTML, and will not be escaped
  1203. in HTML output.
  1204. Some simple examples:
  1205. .
  1206. <table>
  1207. <tr>
  1208. <td>
  1209. hi
  1210. </td>
  1211. </tr>
  1212. </table>
  1213. okay.
  1214. .
  1215. <table>
  1216. <tr>
  1217. <td>
  1218. hi
  1219. </td>
  1220. </tr>
  1221. </table>
  1222. <p>okay.</p>
  1223. .
  1224. .
  1225. <div>
  1226. *hello*
  1227. <foo><a>
  1228. .
  1229. <div>
  1230. *hello*
  1231. <foo><a>
  1232. .
  1233. Here we have two HTML blocks with a Markdown paragraph between them:
  1234. .
  1235. <DIV CLASS="foo">
  1236. *Markdown*
  1237. </DIV>
  1238. .
  1239. <DIV CLASS="foo">
  1240. <p><em>Markdown</em></p>
  1241. </DIV>
  1242. .
  1243. In the following example, what looks like a Markdown code block
  1244. is actually part of the HTML block, which continues until a blank
  1245. line or the end of the document is reached:
  1246. .
  1247. <div></div>
  1248. ``` c
  1249. int x = 33;
  1250. ```
  1251. .
  1252. <div></div>
  1253. ``` c
  1254. int x = 33;
  1255. ```
  1256. .
  1257. A comment:
  1258. .
  1259. <!-- Foo
  1260. bar
  1261. baz -->
  1262. .
  1263. <!-- Foo
  1264. bar
  1265. baz -->
  1266. .
  1267. A processing instruction:
  1268. .
  1269. <?php
  1270. echo '>';
  1271. ?>
  1272. .
  1273. <?php
  1274. echo '>';
  1275. ?>
  1276. .
  1277. CDATA:
  1278. .
  1279. <![CDATA[
  1280. function matchwo(a,b)
  1281. {
  1282. if (a < b && a < 0) then
  1283. {
  1284. return 1;
  1285. }
  1286. else
  1287. {
  1288. return 0;
  1289. }
  1290. }
  1291. ]]>
  1292. .
  1293. <![CDATA[
  1294. function matchwo(a,b)
  1295. {
  1296. if (a < b && a < 0) then
  1297. {
  1298. return 1;
  1299. }
  1300. else
  1301. {
  1302. return 0;
  1303. }
  1304. }
  1305. ]]>
  1306. .
  1307. The opening tag can be indented 1-3 spaces, but not 4:
  1308. .
  1309. <!-- foo -->
  1310. <!-- foo -->
  1311. .
  1312. <!-- foo -->
  1313. <pre><code>&lt;!-- foo --&gt;
  1314. </code></pre>
  1315. .
  1316. An HTML block can interrupt a paragraph, and need not be preceded
  1317. by a blank line.
  1318. .
  1319. Foo
  1320. <div>
  1321. bar
  1322. </div>
  1323. .
  1324. <p>Foo</p>
  1325. <div>
  1326. bar
  1327. </div>
  1328. .
  1329. However, a following blank line is always needed, except at the end of
  1330. a document:
  1331. .
  1332. <div>
  1333. bar
  1334. </div>
  1335. *foo*
  1336. .
  1337. <div>
  1338. bar
  1339. </div>
  1340. *foo*
  1341. .
  1342. An incomplete HTML block tag may also start an HTML block:
  1343. .
  1344. <div class
  1345. foo
  1346. .
  1347. <div class
  1348. foo
  1349. .
  1350. This rule differs from John Gruber's original Markdown syntax
  1351. specification, which says:
  1352. > The only restrictions are that block-level HTML elements —
  1353. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  1354. > surrounding content by blank lines, and the start and end tags of the
  1355. > block should not be indented with tabs or spaces.
  1356. In some ways Gruber's rule is more restrictive than the one given
  1357. here:
  1358. - It requires that an HTML block be preceded by a blank line.
  1359. - It does not allow the start tag to be indented.
  1360. - It requires a matching end tag, which it also does not allow to
  1361. be indented.
  1362. Indeed, most Markdown implementations, including some of Gruber's
  1363. own perl implementations, do not impose these restrictions.
  1364. There is one respect, however, in which Gruber's rule is more liberal
  1365. than the one given here, since it allows blank lines to occur inside
  1366. an HTML block. There are two reasons for disallowing them here.
  1367. First, it removes the need to parse balanced tags, which is
  1368. expensive and can require backtracking from the end of the document
  1369. if no matching end tag is found. Second, it provides a very simple
  1370. and flexible way of including Markdown content inside HTML tags:
  1371. simply separate the Markdown from the HTML using blank lines:
  1372. .
  1373. <div>
  1374. *Emphasized* text.
  1375. </div>
  1376. .
  1377. <div>
  1378. <p><em>Emphasized</em> text.</p>
  1379. </div>
  1380. .
  1381. Compare:
  1382. .
  1383. <div>
  1384. *Emphasized* text.
  1385. </div>
  1386. .
  1387. <div>
  1388. *Emphasized* text.
  1389. </div>
  1390. .
  1391. Some Markdown implementations have adopted a convention of
  1392. interpreting content inside tags as text if the open tag has
  1393. the attribute `markdown=1`. The rule given above seems a simpler and
  1394. more elegant way of achieving the same expressive power, which is also
  1395. much simpler to parse.
  1396. The main potential drawback is that one can no longer paste HTML
  1397. blocks into Markdown documents with 100% reliability. However,
  1398. *in most cases* this will work fine, because the blank lines in
  1399. HTML are usually followed by HTML block tags. For example:
  1400. .
  1401. <table>
  1402. <tr>
  1403. <td>
  1404. Hi
  1405. </td>
  1406. </tr>
  1407. </table>
  1408. .
  1409. <table>
  1410. <tr>
  1411. <td>
  1412. Hi
  1413. </td>
  1414. </tr>
  1415. </table>
  1416. .
  1417. Moreover, blank lines are usually not necessary and can be
  1418. deleted. The exception is inside `<pre>` tags; here, one can
  1419. replace the blank lines with `&#10;` entities.
  1420. So there is no important loss of expressive power with the new rule.
  1421. ## Link reference definitions
  1422. A [link reference definition](@link-reference-definition)
  1423. consists of a [link
  1424. label](#link-label), indented up to three spaces, followed
  1425. by a colon (`:`), optional blank space (including up to one
  1426. newline), a [link destination](#link-destination), optional
  1427. blank space (including up to one newline), and an optional [link
  1428. title](#link-title), which if it is present must be separated
  1429. from the [link destination](#link-destination) by whitespace.
  1430. No further non-space characters may occur on the line.
  1431. A [link reference-definition](#link-reference-definition)
  1432. does not correspond to a structural element of a document. Instead, it
  1433. defines a label which can be used in [reference links](#reference-link)
  1434. and reference-style [images](#image) elsewhere in the document. [Link
  1435. reference definitions] can come either before or after the links that use
  1436. them.
  1437. .
  1438. [foo]: /url "title"
  1439. [foo]
  1440. .
  1441. <p><a href="/url" title="title">foo</a></p>
  1442. .
  1443. .
  1444. [foo]:
  1445. /url
  1446. 'the title'
  1447. [foo]
  1448. .
  1449. <p><a href="/url" title="the title">foo</a></p>
  1450. .
  1451. .
  1452. [Foo*bar\]]:my_(url) 'title (with parens)'
  1453. [Foo*bar\]]
  1454. .
  1455. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  1456. .
  1457. .
  1458. [Foo bar]:
  1459. <my url>
  1460. 'title'
  1461. [Foo bar]
  1462. .
  1463. <p><a href="my%20url" title="title">Foo bar</a></p>
  1464. .
  1465. The title may be omitted:
  1466. .
  1467. [foo]:
  1468. /url
  1469. [foo]
  1470. .
  1471. <p><a href="/url">foo</a></p>
  1472. .
  1473. The link destination may not be omitted:
  1474. .
  1475. [foo]:
  1476. [foo]
  1477. .
  1478. <p>[foo]:</p>
  1479. <p>[foo]</p>
  1480. .
  1481. A link can come before its corresponding definition:
  1482. .
  1483. [foo]
  1484. [foo]: url
  1485. .
  1486. <p><a href="url">foo</a></p>
  1487. .
  1488. If there are several matching definitions, the first one takes
  1489. precedence:
  1490. .
  1491. [foo]
  1492. [foo]: first
  1493. [foo]: second
  1494. .
  1495. <p><a href="first">foo</a></p>
  1496. .
  1497. As noted in the section on [Links], matching of labels is
  1498. case-insensitive (see [matches](#matches)).
  1499. .
  1500. [FOO]: /url
  1501. [Foo]
  1502. .
  1503. <p><a href="/url">Foo</a></p>
  1504. .
  1505. .
  1506. [ΑΓΩ]: /φου
  1507. [αγω]
  1508. .
  1509. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  1510. .
  1511. Here is a link reference definition with no corresponding link.
  1512. It contributes nothing to the document.
  1513. .
  1514. [foo]: /url
  1515. .
  1516. .
  1517. This is not a link reference definition, because there are
  1518. non-space characters after the title:
  1519. .
  1520. [foo]: /url "title" ok
  1521. .
  1522. <p>[foo]: /url &quot;title&quot; ok</p>
  1523. .
  1524. This is not a link reference definition, because it is indented
  1525. four spaces:
  1526. .
  1527. [foo]: /url "title"
  1528. [foo]
  1529. .
  1530. <pre><code>[foo]: /url &quot;title&quot;
  1531. </code></pre>
  1532. <p>[foo]</p>
  1533. .
  1534. This is not a link reference definition, because it occurs inside
  1535. a code block:
  1536. .
  1537. ```
  1538. [foo]: /url
  1539. ```
  1540. [foo]
  1541. .
  1542. <pre><code>[foo]: /url
  1543. </code></pre>
  1544. <p>[foo]</p>
  1545. .
  1546. A [link reference definition](#link-reference-definition) cannot
  1547. interrupt a paragraph.
  1548. .
  1549. Foo
  1550. [bar]: /baz
  1551. [bar]
  1552. .
  1553. <p>Foo
  1554. [bar]: /baz</p>
  1555. <p>[bar]</p>
  1556. .
  1557. However, it can directly follow other block elements, such as headers
  1558. and horizontal rules, and it need not be followed by a blank line.
  1559. .
  1560. # [Foo]
  1561. [foo]: /url
  1562. > bar
  1563. .
  1564. <h1><a href="/url">Foo</a></h1>
  1565. <blockquote>
  1566. <p>bar</p>
  1567. </blockquote>
  1568. .
  1569. Several [link references](#link-reference) can occur one after another,
  1570. without intervening blank lines.
  1571. .
  1572. [foo]: /foo-url "foo"
  1573. [bar]: /bar-url
  1574. "bar"
  1575. [baz]: /baz-url
  1576. [foo],
  1577. [bar],
  1578. [baz]
  1579. .
  1580. <p><a href="/foo-url" title="foo">foo</a>,
  1581. <a href="/bar-url" title="bar">bar</a>,
  1582. <a href="/baz-url">baz</a></p>
  1583. .
  1584. [Link reference definitions](#link-reference-definition) can occur
  1585. inside block containers, like lists and block quotations. They
  1586. affect the entire document, not just the container in which they
  1587. are defined:
  1588. .
  1589. [foo]
  1590. > [foo]: /url
  1591. .
  1592. <p><a href="/url">foo</a></p>
  1593. <blockquote>
  1594. </blockquote>
  1595. .
  1596. ## Paragraphs
  1597. A sequence of non-blank lines that cannot be interpreted as other
  1598. kinds of blocks forms a [paragraph](@paragraph).
  1599. The contents of the paragraph are the result of parsing the
  1600. paragraph's raw content as inlines. The paragraph's raw content
  1601. is formed by concatenating the lines and removing initial and final
  1602. spaces.
  1603. A simple example with two paragraphs:
  1604. .
  1605. aaa
  1606. bbb
  1607. .
  1608. <p>aaa</p>
  1609. <p>bbb</p>
  1610. .
  1611. Paragraphs can contain multiple lines, but no blank lines:
  1612. .
  1613. aaa
  1614. bbb
  1615. ccc
  1616. ddd
  1617. .
  1618. <p>aaa
  1619. bbb</p>
  1620. <p>ccc
  1621. ddd</p>
  1622. .
  1623. Multiple blank lines between paragraph have no effect:
  1624. .
  1625. aaa
  1626. bbb
  1627. .
  1628. <p>aaa</p>
  1629. <p>bbb</p>
  1630. .
  1631. Leading spaces are skipped:
  1632. .
  1633. aaa
  1634. bbb
  1635. .
  1636. <p>aaa
  1637. bbb</p>
  1638. .
  1639. Lines after the first may be indented any amount, since indented
  1640. code blocks cannot interrupt paragraphs.
  1641. .
  1642. aaa
  1643. bbb
  1644. ccc
  1645. .
  1646. <p>aaa
  1647. bbb
  1648. ccc</p>
  1649. .
  1650. However, the first line may be indented at most three spaces,
  1651. or an indented code block will be triggered:
  1652. .
  1653. aaa
  1654. bbb
  1655. .
  1656. <p>aaa
  1657. bbb</p>
  1658. .
  1659. .
  1660. aaa
  1661. bbb
  1662. .
  1663. <pre><code>aaa
  1664. </code></pre>
  1665. <p>bbb</p>
  1666. .
  1667. Final spaces are stripped before inline parsing, so a paragraph
  1668. that ends with two or more spaces will not end with a [hard line
  1669. break](#hard-line-break):
  1670. .
  1671. aaa
  1672. bbb
  1673. .
  1674. <p>aaa<br />
  1675. bbb</p>
  1676. .
  1677. ## Blank lines
  1678. [Blank lines](#blank-line) between block-level elements are ignored,
  1679. except for the role they play in determining whether a [list](#list)
  1680. is [tight](#tight) or [loose](#loose).
  1681. Blank lines at the beginning and end of the document are also ignored.
  1682. .
  1683. aaa
  1684. # aaa
  1685. .
  1686. <p>aaa</p>
  1687. <h1>aaa</h1>
  1688. .
  1689. # Container blocks
  1690. A [container block](#container-block) is a block that has other
  1691. blocks as its contents. There are two basic kinds of container blocks:
  1692. [block quotes](#block-quote) and [list items](#list-item).
  1693. [Lists](#list) are meta-containers for [list items](#list-item).
  1694. We define the syntax for container blocks recursively. The general
  1695. form of the definition is:
  1696. > If X is a sequence of blocks, then the result of
  1697. > transforming X in such-and-such a way is a container of type Y
  1698. > with these blocks as its content.
  1699. So, we explain what counts as a block quote or list item by explaining
  1700. how these can be *generated* from their contents. This should suffice
  1701. to define the syntax, although it does not give a recipe for *parsing*
  1702. these constructions. (A recipe is provided below in the section entitled
  1703. [A parsing strategy](#appendix-a-a-parsing-strategy).)
  1704. ## Block quotes
  1705. A [block quote marker](@block-quote-marker)
  1706. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  1707. with a following space, or (b) a single character `>` not followed by a space.
  1708. The following rules define [block quotes](@block-quote):
  1709. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  1710. of blocks *Bs*, then the result of prepending a [block quote
  1711. marker](#block-quote-marker) to the beginning of each line in *Ls*
  1712. is a [block quote](#block-quote) containing *Bs*.
  1713. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  1714. quote](#block-quote) with contents *Bs*, then the result of deleting
  1715. the initial [block quote marker](#block-quote-marker) from one or
  1716. more lines in which the next non-space character after the [block
  1717. quote marker](#block-quote-marker) is [paragraph continuation
  1718. text](#paragraph-continuation-text) is a block quote with *Bs* as
  1719. its content.
  1720. [Paragraph continuation text](@paragraph-continuation-text) is text
  1721. that will be parsed as part of the content of a paragraph, but does
  1722. not occur at the beginning of the paragraph.
  1723. 3. **Consecutiveness.** A document cannot contain two [block
  1724. quotes](#block-quote) in a row unless there is a [blank
  1725. line](#blank-line) between them.
  1726. Nothing else counts as a [block quote](#block-quote).
  1727. Here is a simple example:
  1728. .
  1729. > # Foo
  1730. > bar
  1731. > baz
  1732. .
  1733. <blockquote>
  1734. <h1>Foo</h1>
  1735. <p>bar
  1736. baz</p>
  1737. </blockquote>
  1738. .
  1739. The spaces after the `>` characters can be omitted:
  1740. .
  1741. ># Foo
  1742. >bar
  1743. > baz
  1744. .
  1745. <blockquote>
  1746. <h1>Foo</h1>
  1747. <p>bar
  1748. baz</p>
  1749. </blockquote>
  1750. .
  1751. The `>` characters can be indented 1-3 spaces:
  1752. .
  1753. > # Foo
  1754. > bar
  1755. > baz
  1756. .
  1757. <blockquote>
  1758. <h1>Foo</h1>
  1759. <p>bar
  1760. baz</p>
  1761. </blockquote>
  1762. .
  1763. Four spaces gives us a code block:
  1764. .
  1765. > # Foo
  1766. > bar
  1767. > baz
  1768. .
  1769. <pre><code>&gt; # Foo
  1770. &gt; bar
  1771. &gt; baz
  1772. </code></pre>
  1773. .
  1774. The Laziness clause allows us to omit the `>` before a
  1775. paragraph continuation line:
  1776. .
  1777. > # Foo
  1778. > bar
  1779. baz
  1780. .
  1781. <blockquote>
  1782. <h1>Foo</h1>
  1783. <p>bar
  1784. baz</p>
  1785. </blockquote>
  1786. .
  1787. A block quote can contain some lazy and some non-lazy
  1788. continuation lines:
  1789. .
  1790. > bar
  1791. baz
  1792. > foo
  1793. .
  1794. <blockquote>
  1795. <p>bar
  1796. baz
  1797. foo</p>
  1798. </blockquote>
  1799. .
  1800. Laziness only applies to lines that are continuations of
  1801. paragraphs. Lines containing characters or indentation that indicate
  1802. block structure cannot be lazy.
  1803. .
  1804. > foo
  1805. ---
  1806. .
  1807. <blockquote>
  1808. <p>foo</p>
  1809. </blockquote>
  1810. <hr />
  1811. .
  1812. .
  1813. > - foo
  1814. - bar
  1815. .
  1816. <blockquote>
  1817. <ul>
  1818. <li>foo</li>
  1819. </ul>
  1820. </blockquote>
  1821. <ul>
  1822. <li>bar</li>
  1823. </ul>
  1824. .
  1825. .
  1826. > foo
  1827. bar
  1828. .
  1829. <blockquote>
  1830. <pre><code>foo
  1831. </code></pre>
  1832. </blockquote>
  1833. <pre><code>bar
  1834. </code></pre>
  1835. .
  1836. .
  1837. > ```
  1838. foo
  1839. ```
  1840. .
  1841. <blockquote>
  1842. <pre><code></code></pre>
  1843. </blockquote>
  1844. <p>foo</p>
  1845. <pre><code></code></pre>
  1846. .
  1847. A block quote can be empty:
  1848. .
  1849. >
  1850. .
  1851. <blockquote>
  1852. </blockquote>
  1853. .
  1854. .
  1855. >
  1856. >
  1857. >
  1858. .
  1859. <blockquote>
  1860. </blockquote>
  1861. .
  1862. A block quote can have initial or final blank lines:
  1863. .
  1864. >
  1865. > foo
  1866. >
  1867. .
  1868. <blockquote>
  1869. <p>foo</p>
  1870. </blockquote>
  1871. .
  1872. A blank line always separates block quotes:
  1873. .
  1874. > foo
  1875. > bar
  1876. .
  1877. <blockquote>
  1878. <p>foo</p>
  1879. </blockquote>
  1880. <blockquote>
  1881. <p>bar</p>
  1882. </blockquote>
  1883. .
  1884. (Most current Markdown implementations, including John Gruber's
  1885. original `Markdown.pl`, will parse this example as a single block quote
  1886. with two paragraphs. But it seems better to allow the author to decide
  1887. whether two block quotes or one are wanted.)
  1888. Consecutiveness means that if we put these block quotes together,
  1889. we get a single block quote:
  1890. .
  1891. > foo
  1892. > bar
  1893. .
  1894. <blockquote>
  1895. <p>foo
  1896. bar</p>
  1897. </blockquote>
  1898. .
  1899. To get a block quote with two paragraphs, use:
  1900. .
  1901. > foo
  1902. >
  1903. > bar
  1904. .
  1905. <blockquote>
  1906. <p>foo</p>
  1907. <p>bar</p>
  1908. </blockquote>
  1909. .
  1910. Block quotes can interrupt paragraphs:
  1911. .
  1912. foo
  1913. > bar
  1914. .
  1915. <p>foo</p>
  1916. <blockquote>
  1917. <p>bar</p>
  1918. </blockquote>
  1919. .
  1920. In general, blank lines are not needed before or after block
  1921. quotes:
  1922. .
  1923. > aaa
  1924. ***
  1925. > bbb
  1926. .
  1927. <blockquote>
  1928. <p>aaa</p>
  1929. </blockquote>
  1930. <hr />
  1931. <blockquote>
  1932. <p>bbb</p>
  1933. </blockquote>
  1934. .
  1935. However, because of laziness, a blank line is needed between
  1936. a block quote and a following paragraph:
  1937. .
  1938. > bar
  1939. baz
  1940. .
  1941. <blockquote>
  1942. <p>bar
  1943. baz</p>
  1944. </blockquote>
  1945. .
  1946. .
  1947. > bar
  1948. baz
  1949. .
  1950. <blockquote>
  1951. <p>bar</p>
  1952. </blockquote>
  1953. <p>baz</p>
  1954. .
  1955. .
  1956. > bar
  1957. >
  1958. baz
  1959. .
  1960. <blockquote>
  1961. <p>bar</p>
  1962. </blockquote>
  1963. <p>baz</p>
  1964. .
  1965. It is a consequence of the Laziness rule that any number
  1966. of initial `>`s may be omitted on a continuation line of a
  1967. nested block quote:
  1968. .
  1969. > > > foo
  1970. bar
  1971. .
  1972. <blockquote>
  1973. <blockquote>
  1974. <blockquote>
  1975. <p>foo
  1976. bar</p>
  1977. </blockquote>
  1978. </blockquote>
  1979. </blockquote>
  1980. .
  1981. .
  1982. >>> foo
  1983. > bar
  1984. >>baz
  1985. .
  1986. <blockquote>
  1987. <blockquote>
  1988. <blockquote>
  1989. <p>foo
  1990. bar
  1991. baz</p>
  1992. </blockquote>
  1993. </blockquote>
  1994. </blockquote>
  1995. .
  1996. When including an indented code block in a block quote,
  1997. remember that the [block quote marker](#block-quote-marker) includes
  1998. both the `>` and a following space. So *five spaces* are needed after
  1999. the `>`:
  2000. .
  2001. > code
  2002. > not code
  2003. .
  2004. <blockquote>
  2005. <pre><code>code
  2006. </code></pre>
  2007. </blockquote>
  2008. <blockquote>
  2009. <p>not code</p>
  2010. </blockquote>
  2011. .
  2012. ## List items
  2013. A [list marker](@list-marker) is a
  2014. [bullet list marker](#bullet-list-marker) or an [ordered list
  2015. marker](#ordered-list-marker).
  2016. A [bullet list marker](@bullet-list-marker)
  2017. is a `-`, `+`, or `*` character.
  2018. An [ordered list marker](@ordered-list-marker)
  2019. is a sequence of one of more digits (`0-9`), followed by either a
  2020. `.` character or a `)` character.
  2021. The following rules define [list items](@list-item):
  2022. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2023. blocks *Bs* starting with a non-space character and not separated
  2024. from each other by more than one blank line, and *M* is a list
  2025. marker *M* of width *W* followed by 0 < *N* < 5 spaces, then the result
  2026. of prepending *M* and the following spaces to the first line of
  2027. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2028. list item with *Bs* as its contents. The type of the list item
  2029. (bullet or ordered) is determined by the type of its list marker.
  2030. If the list item is ordered, then it is also assigned a start
  2031. number, based on the ordered list marker.
  2032. For example, let *Ls* be the lines
  2033. .
  2034. A paragraph
  2035. with two lines.
  2036. indented code
  2037. > A block quote.
  2038. .
  2039. <p>A paragraph
  2040. with two lines.</p>
  2041. <pre><code>indented code
  2042. </code></pre>
  2043. <blockquote>
  2044. <p>A block quote.</p>
  2045. </blockquote>
  2046. .
  2047. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2048. that the following is an ordered list item with start number 1,
  2049. and the same contents as *Ls*:
  2050. .
  2051. 1. A paragraph
  2052. with two lines.
  2053. indented code
  2054. > A block quote.
  2055. .
  2056. <ol>
  2057. <li>
  2058. <p>A paragraph
  2059. with two lines.</p>
  2060. <pre><code>indented code
  2061. </code></pre>
  2062. <blockquote>
  2063. <p>A block quote.</p>
  2064. </blockquote>
  2065. </li>
  2066. </ol>
  2067. .
  2068. The most important thing to notice is that the position of
  2069. the text after the list marker determines how much indentation
  2070. is needed in subsequent blocks in the list item. If the list
  2071. marker takes up two spaces, and there are three spaces between
  2072. the list marker and the next nonspace character, then blocks
  2073. must be indented five spaces in order to fall under the list
  2074. item.
  2075. Here are some examples showing how far content must be indented to be
  2076. put under the list item:
  2077. .
  2078. - one
  2079. two
  2080. .
  2081. <ul>
  2082. <li>one</li>
  2083. </ul>
  2084. <p>two</p>
  2085. .
  2086. .
  2087. - one
  2088. two
  2089. .
  2090. <ul>
  2091. <li>
  2092. <p>one</p>
  2093. <p>two</p>
  2094. </li>
  2095. </ul>
  2096. .
  2097. .
  2098. - one
  2099. two
  2100. .
  2101. <ul>
  2102. <li>one</li>
  2103. </ul>
  2104. <pre><code> two
  2105. </code></pre>
  2106. .
  2107. .
  2108. - one
  2109. two
  2110. .
  2111. <ul>
  2112. <li>
  2113. <p>one</p>
  2114. <p>two</p>
  2115. </li>
  2116. </ul>
  2117. .
  2118. It is tempting to think of this in terms of columns: the continuation
  2119. blocks must be indented at least to the column of the first nonspace
  2120. character after the list marker. However, that is not quite right.
  2121. The spaces after the list marker determine how much relative indentation
  2122. is needed. Which column this indentation reaches will depend on
  2123. how the list item is embedded in other constructions, as shown by
  2124. this example:
  2125. .
  2126. > > 1. one
  2127. >>
  2128. >> two
  2129. .
  2130. <blockquote>
  2131. <blockquote>
  2132. <ol>
  2133. <li>
  2134. <p>one</p>
  2135. <p>two</p>
  2136. </li>
  2137. </ol>
  2138. </blockquote>
  2139. </blockquote>
  2140. .
  2141. Here `two` occurs in the same column as the list marker `1.`,
  2142. but is actually contained in the list item, because there is
  2143. sufficent indentation after the last containing blockquote marker.
  2144. The converse is also possible. In the following example, the word `two`
  2145. occurs far to the right of the initial text of the list item, `one`, but
  2146. it is not considered part of the list item, because it is not indented
  2147. far enough past the blockquote marker:
  2148. .
  2149. >>- one
  2150. >>
  2151. > > two
  2152. .
  2153. <blockquote>
  2154. <blockquote>
  2155. <ul>
  2156. <li>one</li>
  2157. </ul>
  2158. <p>two</p>
  2159. </blockquote>
  2160. </blockquote>
  2161. .
  2162. A list item may not contain blocks that are separated by more than
  2163. one blank line. Thus, two blank lines will end a list, unless the
  2164. two blanks are contained in a [fenced code block](#fenced-code-block).
  2165. .
  2166. - foo
  2167. bar
  2168. - foo
  2169. bar
  2170. - ```
  2171. foo
  2172. bar
  2173. ```
  2174. .
  2175. <ul>
  2176. <li>
  2177. <p>foo</p>
  2178. <p>bar</p>
  2179. </li>
  2180. <li>
  2181. <p>foo</p>
  2182. </li>
  2183. </ul>
  2184. <p>bar</p>
  2185. <ul>
  2186. <li>
  2187. <pre><code>foo
  2188. bar
  2189. </code></pre>
  2190. </li>
  2191. </ul>
  2192. .
  2193. A list item may contain any kind of block:
  2194. .
  2195. 1. foo
  2196. ```
  2197. bar
  2198. ```
  2199. baz
  2200. > bam
  2201. .
  2202. <ol>
  2203. <li>
  2204. <p>foo</p>
  2205. <pre><code>bar
  2206. </code></pre>
  2207. <p>baz</p>
  2208. <blockquote>
  2209. <p>bam</p>
  2210. </blockquote>
  2211. </li>
  2212. </ol>
  2213. .
  2214. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  2215. constitute a sequence of blocks *Bs* starting with an indented code
  2216. block and not separated from each other by more than one blank line,
  2217. and *M* is a list marker *M* of width *W* followed by
  2218. one space, then the result of prepending *M* and the following
  2219. space to the first line of *Ls*, and indenting subsequent lines of
  2220. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  2221. If a line is empty, then it need not be indented. The type of the
  2222. list item (bullet or ordered) is determined by the type of its list
  2223. marker. If the list item is ordered, then it is also assigned a
  2224. start number, based on the ordered list marker.
  2225. An indented code block will have to be indented four spaces beyond
  2226. the edge of the region where text will be included in the list item.
  2227. In the following case that is 6 spaces:
  2228. .
  2229. - foo
  2230. bar
  2231. .
  2232. <ul>
  2233. <li>
  2234. <p>foo</p>
  2235. <pre><code>bar
  2236. </code></pre>
  2237. </li>
  2238. </ul>
  2239. .
  2240. And in this case it is 11 spaces:
  2241. .
  2242. 10. foo
  2243. bar
  2244. .
  2245. <ol start="10">
  2246. <li>
  2247. <p>foo</p>
  2248. <pre><code>bar
  2249. </code></pre>
  2250. </li>
  2251. </ol>
  2252. .
  2253. If the *first* block in the list item is an indented code block,
  2254. then by rule #2, the contents must be indented *one* space after the
  2255. list marker:
  2256. .
  2257. indented code
  2258. paragraph
  2259. more code
  2260. .
  2261. <pre><code>indented code
  2262. </code></pre>
  2263. <p>paragraph</p>
  2264. <pre><code>more code
  2265. </code></pre>
  2266. .
  2267. .
  2268. 1. indented code
  2269. paragraph
  2270. more code
  2271. .
  2272. <ol>
  2273. <li>
  2274. <pre><code>indented code
  2275. </code></pre>
  2276. <p>paragraph</p>
  2277. <pre><code>more code
  2278. </code></pre>
  2279. </li>
  2280. </ol>
  2281. .
  2282. Note that an additional space indent is interpreted as space
  2283. inside the code block:
  2284. .
  2285. 1. indented code
  2286. paragraph
  2287. more code
  2288. .
  2289. <ol>
  2290. <li>
  2291. <pre><code> indented code
  2292. </code></pre>
  2293. <p>paragraph</p>
  2294. <pre><code>more code
  2295. </code></pre>
  2296. </li>
  2297. </ol>
  2298. .
  2299. Note that rules #1 and #2 only apply to two cases: (a) cases
  2300. in which the lines to be included in a list item begin with a nonspace
  2301. character, and (b) cases in which they begin with an indented code
  2302. block. In a case like the following, where the first block begins with
  2303. a three-space indent, the rules do not allow us to form a list item by
  2304. indenting the whole thing and prepending a list marker:
  2305. .
  2306. foo
  2307. bar
  2308. .
  2309. <p>foo</p>
  2310. <p>bar</p>
  2311. .
  2312. .
  2313. - foo
  2314. bar
  2315. .
  2316. <ul>
  2317. <li>foo</li>
  2318. </ul>
  2319. <p>bar</p>
  2320. .
  2321. This is not a significant restriction, because when a block begins
  2322. with 1-3 spaces indent, the indentation can always be removed without
  2323. a change in interpretation, allowing rule #1 to be applied. So, in
  2324. the above case:
  2325. .
  2326. - foo
  2327. bar
  2328. .
  2329. <ul>
  2330. <li>
  2331. <p>foo</p>
  2332. <p>bar</p>
  2333. </li>
  2334. </ul>
  2335. .
  2336. 3. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  2337. according to rule #1 or #2, then the result of indenting each line
  2338. of *L* by 1-3 spaces (the same for each line) also constitutes a
  2339. list item with the same contents and attributes. If a line is
  2340. empty, then it need not be indented.
  2341. Indented one space:
  2342. .
  2343. 1. A paragraph
  2344. with two lines.
  2345. indented code
  2346. > A block quote.
  2347. .
  2348. <ol>
  2349. <li>
  2350. <p>A paragraph
  2351. with two lines.</p>
  2352. <pre><code>indented code
  2353. </code></pre>
  2354. <blockquote>
  2355. <p>A block quote.</p>
  2356. </blockquote>
  2357. </li>
  2358. </ol>
  2359. .
  2360. Indented two spaces:
  2361. .
  2362. 1. A paragraph
  2363. with two lines.
  2364. indented code
  2365. > A block quote.
  2366. .
  2367. <ol>
  2368. <li>
  2369. <p>A paragraph
  2370. with two lines.</p>
  2371. <pre><code>indented code
  2372. </code></pre>
  2373. <blockquote>
  2374. <p>A block quote.</p>
  2375. </blockquote>
  2376. </li>
  2377. </ol>
  2378. .
  2379. Indented three spaces:
  2380. .
  2381. 1. A paragraph
  2382. with two lines.
  2383. indented code
  2384. > A block quote.
  2385. .
  2386. <ol>
  2387. <li>
  2388. <p>A paragraph
  2389. with two lines.</p>
  2390. <pre><code>indented code
  2391. </code></pre>
  2392. <blockquote>
  2393. <p>A block quote.</p>
  2394. </blockquote>
  2395. </li>
  2396. </ol>
  2397. .
  2398. Four spaces indent gives a code block:
  2399. .
  2400. 1. A paragraph
  2401. with two lines.
  2402. indented code
  2403. > A block quote.
  2404. .
  2405. <pre><code>1. A paragraph
  2406. with two lines.
  2407. indented code
  2408. &gt; A block quote.
  2409. </code></pre>
  2410. .
  2411. 4. **Laziness.** If a string of lines *Ls* constitute a [list
  2412. item](#list-item) with contents *Bs*, then the result of deleting
  2413. some or all of the indentation from one or more lines in which the
  2414. next non-space character after the indentation is
  2415. [paragraph continuation text](#paragraph-continuation-text) is a
  2416. list item with the same contents and attributes. The unindented
  2417. lines are called
  2418. [lazy continuation lines](@lazy-continuation-line).
  2419. Here is an example with [lazy continuation
  2420. lines](#lazy-continuation-line):
  2421. .
  2422. 1. A paragraph
  2423. with two lines.
  2424. indented code
  2425. > A block quote.
  2426. .
  2427. <ol>
  2428. <li>
  2429. <p>A paragraph
  2430. with two lines.</p>
  2431. <pre><code>indented code
  2432. </code></pre>
  2433. <blockquote>
  2434. <p>A block quote.</p>
  2435. </blockquote>
  2436. </li>
  2437. </ol>
  2438. .
  2439. Indentation can be partially deleted:
  2440. .
  2441. 1. A paragraph
  2442. with two lines.
  2443. .
  2444. <ol>
  2445. <li>A paragraph
  2446. with two lines.</li>
  2447. </ol>
  2448. .
  2449. These examples show how laziness can work in nested structures:
  2450. .
  2451. > 1. > Blockquote
  2452. continued here.
  2453. .
  2454. <blockquote>
  2455. <ol>
  2456. <li>
  2457. <blockquote>
  2458. <p>Blockquote
  2459. continued here.</p>
  2460. </blockquote>
  2461. </li>
  2462. </ol>
  2463. </blockquote>
  2464. .
  2465. .
  2466. > 1. > Blockquote
  2467. > continued here.
  2468. .
  2469. <blockquote>
  2470. <ol>
  2471. <li>
  2472. <blockquote>
  2473. <p>Blockquote
  2474. continued here.</p>
  2475. </blockquote>
  2476. </li>
  2477. </ol>
  2478. </blockquote>
  2479. .
  2480. 5. **That's all.** Nothing that is not counted as a list item by rules
  2481. #1--4 counts as a [list item](#list-item).
  2482. The rules for sublists follow from the general rules above. A sublist
  2483. must be indented the same number of spaces a paragraph would need to be
  2484. in order to be included in the list item.
  2485. So, in this case we need two spaces indent:
  2486. .
  2487. - foo
  2488. - bar
  2489. - baz
  2490. .
  2491. <ul>
  2492. <li>foo
  2493. <ul>
  2494. <li>bar
  2495. <ul>
  2496. <li>baz</li>
  2497. </ul>
  2498. </li>
  2499. </ul>
  2500. </li>
  2501. </ul>
  2502. .
  2503. One is not enough:
  2504. .
  2505. - foo
  2506. - bar
  2507. - baz
  2508. .
  2509. <ul>
  2510. <li>foo</li>
  2511. <li>bar</li>
  2512. <li>baz</li>
  2513. </ul>
  2514. .
  2515. Here we need four, because the list marker is wider:
  2516. .
  2517. 10) foo
  2518. - bar
  2519. .
  2520. <ol start="10">
  2521. <li>foo
  2522. <ul>
  2523. <li>bar</li>
  2524. </ul>
  2525. </li>
  2526. </ol>
  2527. .
  2528. Three is not enough:
  2529. .
  2530. 10) foo
  2531. - bar
  2532. .
  2533. <ol start="10">
  2534. <li>foo</li>
  2535. </ol>
  2536. <ul>
  2537. <li>bar</li>
  2538. </ul>
  2539. .
  2540. A list may be the first block in a list item:
  2541. .
  2542. - - foo
  2543. .
  2544. <ul>
  2545. <li>
  2546. <ul>
  2547. <li>foo</li>
  2548. </ul>
  2549. </li>
  2550. </ul>
  2551. .
  2552. .
  2553. 1. - 2. foo
  2554. .
  2555. <ol>
  2556. <li>
  2557. <ul>
  2558. <li>
  2559. <ol start="2">
  2560. <li>foo</li>
  2561. </ol>
  2562. </li>
  2563. </ul>
  2564. </li>
  2565. </ol>
  2566. .
  2567. A list item may be empty:
  2568. .
  2569. - foo
  2570. -
  2571. - bar
  2572. .
  2573. <ul>
  2574. <li>foo</li>
  2575. <li></li>
  2576. <li>bar</li>
  2577. </ul>
  2578. .
  2579. .
  2580. -
  2581. .
  2582. <ul>
  2583. <li></li>
  2584. </ul>
  2585. .
  2586. A list item can contain a header:
  2587. .
  2588. - # Foo
  2589. - Bar
  2590. ---
  2591. baz
  2592. .
  2593. <ul>
  2594. <li>
  2595. <h1>Foo</h1>
  2596. </li>
  2597. <li>
  2598. <h2>Bar</h2>
  2599. baz</li>
  2600. </ul>
  2601. .
  2602. ### Motivation
  2603. John Gruber's Markdown spec says the following about list items:
  2604. 1. "List markers typically start at the left margin, but may be indented
  2605. by up to three spaces. List markers must be followed by one or more
  2606. spaces or a tab."
  2607. 2. "To make lists look nice, you can wrap items with hanging indents....
  2608. But if you don't want to, you don't have to."
  2609. 3. "List items may consist of multiple paragraphs. Each subsequent
  2610. paragraph in a list item must be indented by either 4 spaces or one
  2611. tab."
  2612. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  2613. but here again, Markdown will allow you to be lazy."
  2614. 5. "To put a blockquote within a list item, the blockquote's `>`
  2615. delimiters need to be indented."
  2616. 6. "To put a code block within a list item, the code block needs to be
  2617. indented twice — 8 spaces or two tabs."
  2618. These rules specify that a paragraph under a list item must be indented
  2619. four spaces (presumably, from the left margin, rather than the start of
  2620. the list marker, but this is not said), and that code under a list item
  2621. must be indented eight spaces instead of the usual four. They also say
  2622. that a block quote must be indented, but not by how much; however, the
  2623. example given has four spaces indentation. Although nothing is said
  2624. about other kinds of block-level content, it is certainly reasonable to
  2625. infer that *all* block elements under a list item, including other
  2626. lists, must be indented four spaces. This principle has been called the
  2627. *four-space rule*.
  2628. The four-space rule is clear and principled, and if the reference
  2629. implementation `Markdown.pl` had followed it, it probably would have
  2630. become the standard. However, `Markdown.pl` allowed paragraphs and
  2631. sublists to start with only two spaces indentation, at least on the
  2632. outer level. Worse, its behavior was inconsistent: a sublist of an
  2633. outer-level list needed two spaces indentation, but a sublist of this
  2634. sublist needed three spaces. It is not surprising, then, that different
  2635. implementations of Markdown have developed very different rules for
  2636. determining what comes under a list item. (Pandoc and python-Markdown,
  2637. for example, stuck with Gruber's syntax description and the four-space
  2638. rule, while discount, redcarpet, marked, PHP Markdown, and others
  2639. followed `Markdown.pl`'s behavior more closely.)
  2640. Unfortunately, given the divergences between implementations, there
  2641. is no way to give a spec for list items that will be guaranteed not
  2642. to break any existing documents. However, the spec given here should
  2643. correctly handle lists formatted with either the four-space rule or
  2644. the more forgiving `Markdown.pl` behavior, provided they are laid out
  2645. in a way that is natural for a human to read.
  2646. The strategy here is to let the width and indentation of the list marker
  2647. determine the indentation necessary for blocks to fall under the list
  2648. item, rather than having a fixed and arbitrary number. The writer can
  2649. think of the body of the list item as a unit which gets indented to the
  2650. right enough to fit the list marker (and any indentation on the list
  2651. marker). (The laziness rule, #4, then allows continuation lines to be
  2652. unindented if needed.)
  2653. This rule is superior, we claim, to any rule requiring a fixed level of
  2654. indentation from the margin. The four-space rule is clear but
  2655. unnatural. It is quite unintuitive that
  2656. ``` markdown
  2657. - foo
  2658. bar
  2659. - baz
  2660. ```
  2661. should be parsed as two lists with an intervening paragraph,
  2662. ``` html
  2663. <ul>
  2664. <li>foo</li>
  2665. </ul>
  2666. <p>bar</p>
  2667. <ul>
  2668. <li>baz</li>
  2669. </ul>
  2670. ```
  2671. as the four-space rule demands, rather than a single list,
  2672. ``` html
  2673. <ul>
  2674. <li>
  2675. <p>foo</p>
  2676. <p>bar</p>
  2677. <ul>
  2678. <li>baz</li>
  2679. </ul>
  2680. </li>
  2681. </ul>
  2682. ```
  2683. The choice of four spaces is arbitrary. It can be learned, but it is
  2684. not likely to be guessed, and it trips up beginners regularly.
  2685. Would it help to adopt a two-space rule? The problem is that such
  2686. a rule, together with the rule allowing 1--3 spaces indentation of the
  2687. initial list marker, allows text that is indented *less than* the
  2688. original list marker to be included in the list item. For example,
  2689. `Markdown.pl` parses
  2690. ``` markdown
  2691. - one
  2692. two
  2693. ```
  2694. as a single list item, with `two` a continuation paragraph:
  2695. ``` html
  2696. <ul>
  2697. <li>
  2698. <p>one</p>
  2699. <p>two</p>
  2700. </li>
  2701. </ul>
  2702. ```
  2703. and similarly
  2704. ``` markdown
  2705. > - one
  2706. >
  2707. > two
  2708. ```
  2709. as
  2710. ``` html
  2711. <blockquote>
  2712. <ul>
  2713. <li>
  2714. <p>one</p>
  2715. <p>two</p>
  2716. </li>
  2717. </ul>
  2718. </blockquote>
  2719. ```
  2720. This is extremely unintuitive.
  2721. Rather than requiring a fixed indent from the margin, we could require
  2722. a fixed indent (say, two spaces, or even one space) from the list marker (which
  2723. may itself be indented). This proposal would remove the last anomaly
  2724. discussed. Unlike the spec presented above, it would count the following
  2725. as a list item with a subparagraph, even though the paragraph `bar`
  2726. is not indented as far as the first paragraph `foo`:
  2727. ``` markdown
  2728. 10. foo
  2729. bar
  2730. ```
  2731. Arguably this text does read like a list item with `bar` as a subparagraph,
  2732. which may count in favor of the proposal. However, on this proposal indented
  2733. code would have to be indented six spaces after the list marker. And this
  2734. would break a lot of existing Markdown, which has the pattern:
  2735. ``` markdown
  2736. 1. foo
  2737. indented code
  2738. ```
  2739. where the code is indented eight spaces. The spec above, by contrast, will
  2740. parse this text as expected, since the code block's indentation is measured
  2741. from the beginning of `foo`.
  2742. The one case that needs special treatment is a list item that *starts*
  2743. with indented code. How much indentation is required in that case, since
  2744. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  2745. that in such cases, we require one space indentation from the list marker
  2746. (and then the normal four spaces for the indented code). This will match the
  2747. four-space rule in cases where the list marker plus its initial indentation
  2748. takes four spaces (a common case), but diverge in other cases.
  2749. ## Lists
  2750. A [list](@list) is a sequence of one or more
  2751. list items [of the same type](#of-the-same-type). The list items
  2752. may be separated by single [blank lines](#blank-line), but two
  2753. blank lines end all containing lists.
  2754. Two list items are [of the same type](@of-the-same-type)
  2755. if they begin with a [list
  2756. marker](#list-marker) of the same type. Two list markers are of the
  2757. same type if (a) they are bullet list markers using the same character
  2758. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  2759. delimiter (either `.` or `)`).
  2760. A list is an [ordered list](@ordered-list)
  2761. if its constituent list items begin with
  2762. [ordered list markers](#ordered-list-marker), and a [bullet
  2763. list](@bullet-list) if its constituent list
  2764. items begin with [bullet list markers](#bullet-list-marker).
  2765. The [start number](@start-number)
  2766. of an [ordered list](#ordered-list) is determined by the list number of
  2767. its initial list item. The numbers of subsequent list items are
  2768. disregarded.
  2769. A list is [loose](@loose) if it any of its constituent
  2770. list items are separated by blank lines, or if any of its constituent
  2771. list items directly contain two block-level elements with a blank line
  2772. between them. Otherwise a list is [tight](@tight).
  2773. (The difference in HTML output is that paragraphs in a loose list are
  2774. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  2775. Changing the bullet or ordered list delimiter starts a new list:
  2776. .
  2777. - foo
  2778. - bar
  2779. + baz
  2780. .
  2781. <ul>
  2782. <li>foo</li>
  2783. <li>bar</li>
  2784. </ul>
  2785. <ul>
  2786. <li>baz</li>
  2787. </ul>
  2788. .
  2789. .
  2790. 1. foo
  2791. 2. bar
  2792. 3) baz
  2793. .
  2794. <ol>
  2795. <li>foo</li>
  2796. <li>bar</li>
  2797. </ol>
  2798. <ol start="3">
  2799. <li>baz</li>
  2800. </ol>
  2801. .
  2802. In CommonMark, a list can interrupt a paragraph. That is,
  2803. no blank line is needed to separate a paragraph from a following
  2804. list:
  2805. .
  2806. Foo
  2807. - bar
  2808. - baz
  2809. .
  2810. <p>Foo</p>
  2811. <ul>
  2812. <li>bar</li>
  2813. <li>baz</li>
  2814. </ul>
  2815. .
  2816. `Markdown.pl` does not allow this, through fear of triggering a list
  2817. via a numeral in a hard-wrapped line:
  2818. .
  2819. The number of windows in my house is
  2820. 14. The number of doors is 6.
  2821. .
  2822. <p>The number of windows in my house is</p>
  2823. <ol start="14">
  2824. <li>The number of doors is 6.</li>
  2825. </ol>
  2826. .
  2827. Oddly, `Markdown.pl` *does* allow a blockquote to interrupt a paragraph,
  2828. even though the same considerations might apply. We think that the two
  2829. cases should be treated the same. Here are two reasons for allowing
  2830. lists to interrupt paragraphs:
  2831. First, it is natural and not uncommon for people to start lists without
  2832. blank lines:
  2833. I need to buy
  2834. - new shoes
  2835. - a coat
  2836. - a plane ticket
  2837. Second, we are attracted to a
  2838. > [principle of uniformity](@principle-of-uniformity):
  2839. > if a span of text has a certain
  2840. > meaning, it will continue to have the same meaning when put into a list
  2841. > item.
  2842. (Indeed, the spec for [list items](#list-item) presupposes this.)
  2843. This principle implies that if
  2844. * I need to buy
  2845. - new shoes
  2846. - a coat
  2847. - a plane ticket
  2848. is a list item containing a paragraph followed by a nested sublist,
  2849. as all Markdown implementations agree it is (though the paragraph
  2850. may be rendered without `<p>` tags, since the list is "tight"),
  2851. then
  2852. I need to buy
  2853. - new shoes
  2854. - a coat
  2855. - a plane ticket
  2856. by itself should be a paragraph followed by a nested sublist.
  2857. Our adherence to the [principle of uniformity](#principle-of-uniformity)
  2858. thus inclines us to think that there are two coherent packages:
  2859. 1. Require blank lines before *all* lists and blockquotes,
  2860. including lists that occur as sublists inside other list items.
  2861. 2. Require blank lines in none of these places.
  2862. [reStructuredText](http://docutils.sourceforge.net/rst.html) takes
  2863. the first approach, for which there is much to be said. But the second
  2864. seems more consistent with established practice with Markdown.
  2865. There can be blank lines between items, but two blank lines end
  2866. a list:
  2867. .
  2868. - foo
  2869. - bar
  2870. - baz
  2871. .
  2872. <ul>
  2873. <li>
  2874. <p>foo</p>
  2875. </li>
  2876. <li>
  2877. <p>bar</p>
  2878. </li>
  2879. </ul>
  2880. <ul>
  2881. <li>baz</li>
  2882. </ul>
  2883. .
  2884. As illustrated above in the section on [list items](#list-item),
  2885. two blank lines between blocks *within* a list item will also end a
  2886. list:
  2887. .
  2888. - foo
  2889. bar
  2890. - baz
  2891. .
  2892. <ul>
  2893. <li>foo</li>
  2894. </ul>
  2895. <p>bar</p>
  2896. <ul>
  2897. <li>baz</li>
  2898. </ul>
  2899. .
  2900. Indeed, two blank lines will end *all* containing lists:
  2901. .
  2902. - foo
  2903. - bar
  2904. - baz
  2905. bim
  2906. .
  2907. <ul>
  2908. <li>foo
  2909. <ul>
  2910. <li>bar
  2911. <ul>
  2912. <li>baz</li>
  2913. </ul>
  2914. </li>
  2915. </ul>
  2916. </li>
  2917. </ul>
  2918. <pre><code> bim
  2919. </code></pre>
  2920. .
  2921. Thus, two blank lines can be used to separate consecutive lists of
  2922. the same type, or to separate a list from an indented code block
  2923. that would otherwise be parsed as a subparagraph of the final list
  2924. item:
  2925. .
  2926. - foo
  2927. - bar
  2928. - baz
  2929. - bim
  2930. .
  2931. <ul>
  2932. <li>foo</li>
  2933. <li>bar</li>
  2934. </ul>
  2935. <ul>
  2936. <li>baz</li>
  2937. <li>bim</li>
  2938. </ul>
  2939. .
  2940. .
  2941. - foo
  2942. notcode
  2943. - foo
  2944. code
  2945. .
  2946. <ul>
  2947. <li>
  2948. <p>foo</p>
  2949. <p>notcode</p>
  2950. </li>
  2951. <li>
  2952. <p>foo</p>
  2953. </li>
  2954. </ul>
  2955. <pre><code>code
  2956. </code></pre>
  2957. .
  2958. List items need not be indented to the same level. The following
  2959. list items will be treated as items at the same list level,
  2960. since none is indented enough to belong to the previous list
  2961. item:
  2962. .
  2963. - a
  2964. - b
  2965. - c
  2966. - d
  2967. - e
  2968. - f
  2969. - g
  2970. .
  2971. <ul>
  2972. <li>a</li>
  2973. <li>b</li>
  2974. <li>c</li>
  2975. <li>d</li>
  2976. <li>e</li>
  2977. <li>f</li>
  2978. <li>g</li>
  2979. </ul>
  2980. .
  2981. This is a loose list, because there is a blank line between
  2982. two of the list items:
  2983. .
  2984. - a
  2985. - b
  2986. - c
  2987. .
  2988. <ul>
  2989. <li>
  2990. <p>a</p>
  2991. </li>
  2992. <li>
  2993. <p>b</p>
  2994. </li>
  2995. <li>
  2996. <p>c</p>
  2997. </li>
  2998. </ul>
  2999. .
  3000. So is this, with a empty second item:
  3001. .
  3002. * a
  3003. *
  3004. * c
  3005. .
  3006. <ul>
  3007. <li>
  3008. <p>a</p>
  3009. </li>
  3010. <li></li>
  3011. <li>
  3012. <p>c</p>
  3013. </li>
  3014. </ul>
  3015. .
  3016. These are loose lists, even though there is no space between the items,
  3017. because one of the items directly contains two block-level elements
  3018. with a blank line between them:
  3019. .
  3020. - a
  3021. - b
  3022. c
  3023. - d
  3024. .
  3025. <ul>
  3026. <li>
  3027. <p>a</p>
  3028. </li>
  3029. <li>
  3030. <p>b</p>
  3031. <p>c</p>
  3032. </li>
  3033. <li>
  3034. <p>d</p>
  3035. </li>
  3036. </ul>
  3037. .
  3038. .
  3039. - a
  3040. - b
  3041. [ref]: /url
  3042. - d
  3043. .
  3044. <ul>
  3045. <li>
  3046. <p>a</p>
  3047. </li>
  3048. <li>
  3049. <p>b</p>
  3050. </li>
  3051. <li>
  3052. <p>d</p>
  3053. </li>
  3054. </ul>
  3055. .
  3056. This is a tight list, because the blank lines are in a code block:
  3057. .
  3058. - a
  3059. - ```
  3060. b
  3061. ```
  3062. - c
  3063. .
  3064. <ul>
  3065. <li>a</li>
  3066. <li>
  3067. <pre><code>b
  3068. </code></pre>
  3069. </li>
  3070. <li>c</li>
  3071. </ul>
  3072. .
  3073. This is a tight list, because the blank line is between two
  3074. paragraphs of a sublist. So the sublist is loose while
  3075. the outer list is tight:
  3076. .
  3077. - a
  3078. - b
  3079. c
  3080. - d
  3081. .
  3082. <ul>
  3083. <li>a
  3084. <ul>
  3085. <li>
  3086. <p>b</p>
  3087. <p>c</p>
  3088. </li>
  3089. </ul>
  3090. </li>
  3091. <li>d</li>
  3092. </ul>
  3093. .
  3094. This is a tight list, because the blank line is inside the
  3095. block quote:
  3096. .
  3097. * a
  3098. > b
  3099. >
  3100. * c
  3101. .
  3102. <ul>
  3103. <li>a
  3104. <blockquote>
  3105. <p>b</p>
  3106. </blockquote>
  3107. </li>
  3108. <li>c</li>
  3109. </ul>
  3110. .
  3111. This list is tight, because the consecutive block elements
  3112. are not separated by blank lines:
  3113. .
  3114. - a
  3115. > b
  3116. ```
  3117. c
  3118. ```
  3119. - d
  3120. .
  3121. <ul>
  3122. <li>a
  3123. <blockquote>
  3124. <p>b</p>
  3125. </blockquote>
  3126. <pre><code>c
  3127. </code></pre>
  3128. </li>
  3129. <li>d</li>
  3130. </ul>
  3131. .
  3132. A single-paragraph list is tight:
  3133. .
  3134. - a
  3135. .
  3136. <ul>
  3137. <li>a</li>
  3138. </ul>
  3139. .
  3140. .
  3141. - a
  3142. - b
  3143. .
  3144. <ul>
  3145. <li>a
  3146. <ul>
  3147. <li>b</li>
  3148. </ul>
  3149. </li>
  3150. </ul>
  3151. .
  3152. Here the outer list is loose, the inner list tight:
  3153. .
  3154. * foo
  3155. * bar
  3156. baz
  3157. .
  3158. <ul>
  3159. <li>
  3160. <p>foo</p>
  3161. <ul>
  3162. <li>bar</li>
  3163. </ul>
  3164. <p>baz</p>
  3165. </li>
  3166. </ul>
  3167. .
  3168. .
  3169. - a
  3170. - b
  3171. - c
  3172. - d
  3173. - e
  3174. - f
  3175. .
  3176. <ul>
  3177. <li>
  3178. <p>a</p>
  3179. <ul>
  3180. <li>b</li>
  3181. <li>c</li>
  3182. </ul>
  3183. </li>
  3184. <li>
  3185. <p>d</p>
  3186. <ul>
  3187. <li>e</li>
  3188. <li>f</li>
  3189. </ul>
  3190. </li>
  3191. </ul>
  3192. .
  3193. # Inlines
  3194. Inlines are parsed sequentially from the beginning of the character
  3195. stream to the end (left to right, in left-to-right languages).
  3196. Thus, for example, in
  3197. .
  3198. `hi`lo`
  3199. .
  3200. <p><code>hi</code>lo`</p>
  3201. .
  3202. `hi` is parsed as code, leaving the backtick at the end as a literal
  3203. backtick.
  3204. ## Backslash escapes
  3205. Any ASCII punctuation character may be backslash-escaped:
  3206. .
  3207. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  3208. .
  3209. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  3210. .
  3211. Backslashes before other characters are treated as literal
  3212. backslashes:
  3213. .
  3214. \→\A\a\ \3\φ\«
  3215. .
  3216. <p>\ \A\a\ \3\φ\«</p>
  3217. .
  3218. Escaped characters are treated as regular characters and do
  3219. not have their usual Markdown meanings:
  3220. .
  3221. \*not emphasized*
  3222. \<br/> not a tag
  3223. \[not a link](/foo)
  3224. \`not code`
  3225. 1\. not a list
  3226. \* not a list
  3227. \# not a header
  3228. \[foo]: /url "not a reference"
  3229. .
  3230. <p>*not emphasized*
  3231. &lt;br/&gt; not a tag
  3232. [not a link](/foo)
  3233. `not code`
  3234. 1. not a list
  3235. * not a list
  3236. # not a header
  3237. [foo]: /url &quot;not a reference&quot;</p>
  3238. .
  3239. If a backslash is itself escaped, the following character is not:
  3240. .
  3241. \\*emphasis*
  3242. .
  3243. <p>\<em>emphasis</em></p>
  3244. .
  3245. A backslash at the end of the line is a [hard line
  3246. break](#hard-line-break):
  3247. .
  3248. foo\
  3249. bar
  3250. .
  3251. <p>foo<br />
  3252. bar</p>
  3253. .
  3254. Backslash escapes do not work in code blocks, code spans, autolinks, or
  3255. raw HTML:
  3256. .
  3257. `` \[\` ``
  3258. .
  3259. <p><code>\[\`</code></p>
  3260. .
  3261. .
  3262. \[\]
  3263. .
  3264. <pre><code>\[\]
  3265. </code></pre>
  3266. .
  3267. .
  3268. ~~~
  3269. \[\]
  3270. ~~~
  3271. .
  3272. <pre><code>\[\]
  3273. </code></pre>
  3274. .
  3275. .
  3276. <http://example.com?find=\*>
  3277. .
  3278. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  3279. .
  3280. .
  3281. <a href="/bar\/)">
  3282. .
  3283. <p><a href="/bar\/)"></p>
  3284. .
  3285. But they work in all other contexts, including URLs and link titles,
  3286. link references, and info strings in [fenced code
  3287. blocks](#fenced-code-block):
  3288. .
  3289. [foo](/bar\* "ti\*tle")
  3290. .
  3291. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3292. .
  3293. .
  3294. [foo]
  3295. [foo]: /bar\* "ti\*tle"
  3296. .
  3297. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3298. .
  3299. .
  3300. ``` foo\+bar
  3301. foo
  3302. ```
  3303. .
  3304. <pre><code class="language-foo+bar">foo
  3305. </code></pre>
  3306. .
  3307. ## Entities
  3308. With the goal of making this standard as HTML-agnostic as possible, all
  3309. valid HTML entities in any context are recognized as such and
  3310. converted into unicode characters before they are stored in the AST.
  3311. This allows implementations that target HTML output to trivially escape
  3312. the entities when generating HTML, and simplifies the job of
  3313. implementations targetting other languages, as these will only need to
  3314. handle the unicode chars and need not be HTML-entity aware.
  3315. [Named entities](@name-entities) consist of `&`
  3316. + any of the valid HTML5 entity names + `;`. The
  3317. [following document](http://www.whatwg.org/specs/web-apps/current-work/multipage/entities.json)
  3318. is used as an authoritative source of the valid entity names and their
  3319. corresponding codepoints.
  3320. Conforming implementations that target HTML don't need to generate
  3321. entities for all the valid named entities that exist, with the exception
  3322. of `"` (`&quot;`), `&` (`&amp;`), `<` (`&lt;`) and `>` (`&gt;`), which
  3323. always need to be written as entities for security reasons.
  3324. .
  3325. &nbsp; &amp; &copy; &AElig; &Dcaron; &frac34; &HilbertSpace; &DifferentialD; &ClockwiseContourIntegral;
  3326. .
  3327. <p>  &amp; © Æ Ď ¾ ℋ ⅆ ∲</p>
  3328. .
  3329. [Decimal entities](@decimal-entities)
  3330. consist of `&#` + a string of 1--8 arabic digits + `;`. Again, these
  3331. entities need to be recognised and tranformed into their corresponding
  3332. UTF8 codepoints. Invalid Unicode codepoints will be written as the
  3333. "unknown codepoint" character (`0xFFFD`)
  3334. .
  3335. &#35; &#1234; &#992; &#98765432;
  3336. .
  3337. <p># Ӓ Ϡ �</p>
  3338. .
  3339. [Hexadecimal entities](@hexadecimal-entities)
  3340. consist of `&#` + either `X` or `x` + a string of 1-8 hexadecimal digits
  3341. + `;`. They will also be parsed and turned into their corresponding UTF8 values in the AST.
  3342. .
  3343. &#X22; &#XD06; &#xcab;
  3344. .
  3345. <p>&quot; ആ ಫ</p>
  3346. .
  3347. Here are some nonentities:
  3348. .
  3349. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3350. .
  3351. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3352. .
  3353. Although HTML5 does accept some entities without a trailing semicolon
  3354. (such as `&copy`), these are not recognized as entities here, because it
  3355. makes the grammar too ambiguous:
  3356. .
  3357. &copy
  3358. .
  3359. <p>&amp;copy</p>
  3360. .
  3361. Strings that are not on the list of HTML5 named entities are not
  3362. recognized as entities either:
  3363. .
  3364. &MadeUpEntity;
  3365. .
  3366. <p>&amp;MadeUpEntity;</p>
  3367. .
  3368. Entities are recognized in any context besides code spans or
  3369. code blocks, including raw HTML, URLs, [link titles](#link-title), and
  3370. [fenced code block](#fenced-code-block) info strings:
  3371. .
  3372. <a href="&ouml;&ouml;.html">
  3373. .
  3374. <p><a href="&ouml;&ouml;.html"></p>
  3375. .
  3376. .
  3377. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3378. .
  3379. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3380. .
  3381. .
  3382. [foo]
  3383. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3384. .
  3385. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3386. .
  3387. .
  3388. ``` f&ouml;&ouml;
  3389. foo
  3390. ```
  3391. .
  3392. <pre><code class="language-föö">foo
  3393. </code></pre>
  3394. .
  3395. Entities are treated as literal text in code spans and code blocks:
  3396. .
  3397. `f&ouml;&ouml;`
  3398. .
  3399. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3400. .
  3401. .
  3402. f&ouml;f&ouml;
  3403. .
  3404. <pre><code>f&amp;ouml;f&amp;ouml;
  3405. </code></pre>
  3406. .
  3407. ## Code span
  3408. A [backtick string](@backtick-string)
  3409. is a string of one or more backtick characters (`` ` ``) that is neither
  3410. preceded nor followed by a backtick.
  3411. A [code span](@code-span) begins with a backtick string and ends with a backtick
  3412. string of equal length. The contents of the code span are the
  3413. characters between the two backtick strings, with leading and trailing
  3414. spaces and newlines removed, and consecutive spaces and newlines
  3415. collapsed to single spaces.
  3416. This is a simple code span:
  3417. .
  3418. `foo`
  3419. .
  3420. <p><code>foo</code></p>
  3421. .
  3422. Here two backticks are used, because the code contains a backtick.
  3423. This example also illustrates stripping of leading and trailing spaces:
  3424. .
  3425. `` foo ` bar ``
  3426. .
  3427. <p><code>foo ` bar</code></p>
  3428. .
  3429. This example shows the motivation for stripping leading and trailing
  3430. spaces:
  3431. .
  3432. ` `` `
  3433. .
  3434. <p><code>``</code></p>
  3435. .
  3436. Newlines are treated like spaces:
  3437. .
  3438. ``
  3439. foo
  3440. ``
  3441. .
  3442. <p><code>foo</code></p>
  3443. .
  3444. Interior spaces and newlines are collapsed into single spaces, just
  3445. as they would be by a browser:
  3446. .
  3447. `foo bar
  3448. baz`
  3449. .
  3450. <p><code>foo bar baz</code></p>
  3451. .
  3452. Q: Why not just leave the spaces, since browsers will collapse them
  3453. anyway? A: Because we might be targeting a non-HTML format, and we
  3454. shouldn't rely on HTML-specific rendering assumptions.
  3455. (Existing implementations differ in their treatment of internal
  3456. spaces and newlines. Some, including `Markdown.pl` and
  3457. `showdown`, convert an internal newline into a `<br />` tag.
  3458. But this makes things difficult for those who like to hard-wrap
  3459. their paragraphs, since a line break in the midst of a code
  3460. span will cause an unintended line break in the output. Others
  3461. just leave internal spaces as they are, which is fine if only
  3462. HTML is being targeted.)
  3463. .
  3464. `foo `` bar`
  3465. .
  3466. <p><code>foo `` bar</code></p>
  3467. .
  3468. Note that backslash escapes do not work in code spans. All backslashes
  3469. are treated literally:
  3470. .
  3471. `foo\`bar`
  3472. .
  3473. <p><code>foo\</code>bar`</p>
  3474. .
  3475. Backslash escapes are never needed, because one can always choose a
  3476. string of *n* backtick characters as delimiters, where the code does
  3477. not contain any strings of exactly *n* backtick characters.
  3478. Code span backticks have higher precedence than any other inline
  3479. constructs except HTML tags and autolinks. Thus, for example, this is
  3480. not parsed as emphasized text, since the second `*` is part of a code
  3481. span:
  3482. .
  3483. *foo`*`
  3484. .
  3485. <p>*foo<code>*</code></p>
  3486. .
  3487. And this is not parsed as a link:
  3488. .
  3489. [not a `link](/foo`)
  3490. .
  3491. <p>[not a <code>link](/foo</code>)</p>
  3492. .
  3493. But this is a link:
  3494. .
  3495. <http://foo.bar.`baz>`
  3496. .
  3497. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3498. .
  3499. And this is an HTML tag:
  3500. .
  3501. <a href="`">`
  3502. .
  3503. <p><a href="`">`</p>
  3504. .
  3505. When a backtick string is not closed by a matching backtick string,
  3506. we just have literal backticks:
  3507. .
  3508. ```foo``
  3509. .
  3510. <p>```foo``</p>
  3511. .
  3512. .
  3513. `foo
  3514. .
  3515. <p>`foo</p>
  3516. .
  3517. ## Emphasis and strong emphasis
  3518. John Gruber's original [Markdown syntax
  3519. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3520. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3521. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3522. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3523. > tag.
  3524. This is enough for most users, but these rules leave much undecided,
  3525. especially when it comes to nested emphasis. The original
  3526. `Markdown.pl` test suite makes it clear that triple `***` and
  3527. `___` delimiters can be used for strong emphasis, and most
  3528. implementations have also allowed the following patterns:
  3529. ``` markdown
  3530. ***strong emph***
  3531. ***strong** in emph*
  3532. ***emph* in strong**
  3533. **in strong *emph***
  3534. *in emph **strong***
  3535. ```
  3536. The following patterns are less widely supported, but the intent
  3537. is clear and they are useful (especially in contexts like bibliography
  3538. entries):
  3539. ``` markdown
  3540. *emph *with emph* in it*
  3541. **strong **with strong** in it**
  3542. ```
  3543. Many implementations have also restricted intraword emphasis to
  3544. the `*` forms, to avoid unwanted emphasis in words containing
  3545. internal underscores. (It is best practice to put these in code
  3546. spans, but users often do not.)
  3547. ``` markdown
  3548. internal emphasis: foo*bar*baz
  3549. no emphasis: foo_bar_baz
  3550. ```
  3551. The following rules capture all of these patterns, while allowing
  3552. for efficient parsing strategies that do not backtrack:
  3553. 1. A single `*` character [can open emphasis](@can-open-emphasis)
  3554. iff it is not followed by
  3555. whitespace.
  3556. 2. A single `_` character [can open emphasis](#can-open-emphasis) iff
  3557. it is not followed by whitespace and it is not preceded by an
  3558. ASCII alphanumeric character.
  3559. 3. A single `*` character [can close emphasis](@can-close-emphasis)
  3560. iff it is not preceded by whitespace.
  3561. 4. A single `_` character [can close emphasis](#can-close-emphasis) iff
  3562. it is not preceded by whitespace and it is not followed by an
  3563. ASCII alphanumeric character.
  3564. 5. A double `**` [can open strong emphasis](@can-open-strong-emphasis)
  3565. iff it is not followed by
  3566. whitespace.
  3567. 6. A double `__` [can open strong emphasis](#can-open-strong-emphasis)
  3568. iff it is not followed by whitespace and it is not preceded by an
  3569. ASCII alphanumeric character.
  3570. 7. A double `**` [can close strong emphasis](@can-close-strong-emphasis)
  3571. iff it is not preceded by
  3572. whitespace.
  3573. 8. A double `__` [can close strong emphasis](#can-close-strong-emphasis)
  3574. iff it is not preceded by whitespace and it is not followed by an
  3575. ASCII alphanumeric character.
  3576. 9. Emphasis begins with a delimiter that [can open
  3577. emphasis](#can-open-emphasis) and ends with a delimiter that [can close
  3578. emphasis](#can-close-emphasis), and that uses the same
  3579. character (`_` or `*`) as the opening delimiter. There must
  3580. be a nonempty sequence of inlines between the open delimiter
  3581. and the closing delimiter; these form the contents of the emphasis
  3582. inline.
  3583. 10. Strong emphasis begins with a delimiter that [can open strong
  3584. emphasis](#can-open-strong-emphasis) and ends with a delimiter that
  3585. [can close strong emphasis](#can-close-strong-emphasis), and that
  3586. uses the same character (`_` or `*`) as the opening delimiter.
  3587. There must be a nonempty sequence of inlines between the open
  3588. delimiter and the closing delimiter; these form the contents of
  3589. the strong emphasis inline.
  3590. 11. A literal `*` character cannot occur at the beginning or end of
  3591. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  3592. is backslash-escaped.
  3593. 12. A literal `_` character cannot occur at the beginning or end of
  3594. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3595. is backslash-escaped.
  3596. Where rules 1--12 above are compatible with multiple parsings,
  3597. the following principles resolve ambiguity:
  3598. 13. The number of nestings should be minimized. Thus, for example,
  3599. an interpretation `<strong>...</strong>` is always preferred to
  3600. `<em><em>...</em></em>`.
  3601. 14. An interpretation `<strong><em>...</em></strong>` is always
  3602. preferred to `<em><strong>..</strong></em>`.
  3603. 15. When two potential emphasis or strong emphasis spans overlap,
  3604. so that the second begins before the first ends and ends after
  3605. the first ends, the first is preferred. Thus, for example,
  3606. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3607. than `*foo <em>bar* baz</em>`. For the same reason,
  3608. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3609. rather than `<strong>foo*bar</strong>`.
  3610. 16. When there are two potential emphasis or strong emphasis spans
  3611. with the same closing delimiter, the shorter one (the one that
  3612. opens later) is preferred. Thus, for example,
  3613. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3614. rather than `<strong>foo **bar baz</strong>`.
  3615. 17. Inline code spans, links, images, and HTML tags group more tightly
  3616. than emphasis. So, when there is a choice between an interpretation
  3617. that contains one of these elements and one that does not, the
  3618. former always wins. Thus, for example, `*[foo*](bar)` is
  3619. parsed as `*<a href="bar">foo*</a>` rather than as
  3620. `<em>[foo</em>](bar)`.
  3621. These rules can be illustrated through a series of examples.
  3622. Rule 1:
  3623. .
  3624. *foo bar*
  3625. .
  3626. <p><em>foo bar</em></p>
  3627. .
  3628. This is not emphasis, because the opening `*` is followed by
  3629. whitespace:
  3630. .
  3631. a * foo bar*
  3632. .
  3633. <p>a * foo bar*</p>
  3634. .
  3635. Intraword emphasis with `*` is permitted:
  3636. .
  3637. foo*bar*
  3638. .
  3639. <p>foo<em>bar</em></p>
  3640. .
  3641. .
  3642. 5*6*78
  3643. .
  3644. <p>5<em>6</em>78</p>
  3645. .
  3646. Rule 2:
  3647. .
  3648. _foo bar_
  3649. .
  3650. <p><em>foo bar</em></p>
  3651. .
  3652. This is not emphasis, because the opening `*` is followed by
  3653. whitespace:
  3654. .
  3655. _ foo bar_
  3656. .
  3657. <p>_ foo bar_</p>
  3658. .
  3659. Emphasis with `_` is not allowed inside ASCII words:
  3660. .
  3661. foo_bar_
  3662. .
  3663. <p>foo_bar_</p>
  3664. .
  3665. .
  3666. 5_6_78
  3667. .
  3668. <p>5_6_78</p>
  3669. .
  3670. But it is permitted inside non-ASCII words:
  3671. .
  3672. пристаням_стремятся_
  3673. .
  3674. <p>пристаням<em>стремятся</em></p>
  3675. .
  3676. Rule 3:
  3677. This is not emphasis, because the closing `*` is preceded by
  3678. whitespace:
  3679. .
  3680. *foo bar *
  3681. .
  3682. <p>*foo bar *</p>
  3683. .
  3684. Intraword emphasis with `*` is allowed:
  3685. .
  3686. *foo*bar
  3687. .
  3688. <p><em>foo</em>bar</p>
  3689. .
  3690. Rule 4:
  3691. This is not emphasis, because the closing `_` is preceded by
  3692. whitespace:
  3693. .
  3694. _foo bar _
  3695. .
  3696. <p>_foo bar _</p>
  3697. .
  3698. Intraword emphasis:
  3699. .
  3700. _foo_bar
  3701. .
  3702. <p>_foo_bar</p>
  3703. .
  3704. .
  3705. _пристаням_стремятся
  3706. .
  3707. <p><em>пристаням</em>стремятся</p>
  3708. .
  3709. .
  3710. _foo_bar_baz_
  3711. .
  3712. <p><em>foo_bar_baz</em></p>
  3713. .
  3714. Rule 5:
  3715. .
  3716. **foo bar**
  3717. .
  3718. <p><strong>foo bar</strong></p>
  3719. .
  3720. This is not strong emphasis, because the opening delimiter is
  3721. followed by whitespace:
  3722. .
  3723. ** foo bar**
  3724. .
  3725. <p>** foo bar**</p>
  3726. .
  3727. Intraword strong emphasis with `**` is permitted:
  3728. .
  3729. foo**bar**
  3730. .
  3731. <p>foo<strong>bar</strong></p>
  3732. .
  3733. Rule 6:
  3734. .
  3735. __foo bar__
  3736. .
  3737. <p><strong>foo bar</strong></p>
  3738. .
  3739. This is not strong emphasis, because the opening delimiter is
  3740. followed by whitespace:
  3741. .
  3742. __ foo bar__
  3743. .
  3744. <p>__ foo bar__</p>
  3745. .
  3746. Intraword emphasis examples:
  3747. .
  3748. foo__bar__
  3749. .
  3750. <p>foo__bar__</p>
  3751. .
  3752. .
  3753. 5__6__78
  3754. .
  3755. <p>5__6__78</p>
  3756. .
  3757. .
  3758. пристаням__стремятся__
  3759. .
  3760. <p>пристаням<strong>стремятся</strong></p>
  3761. .
  3762. .
  3763. __foo, __bar__, baz__
  3764. .
  3765. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  3766. .
  3767. Rule 7:
  3768. This is not strong emphasis, because the closing delimiter is preceded
  3769. by whitespace:
  3770. .
  3771. **foo bar **
  3772. .
  3773. <p>**foo bar **</p>
  3774. .
  3775. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  3776. Rule 11.)
  3777. Intraword emphasis:
  3778. .
  3779. **foo**bar
  3780. .
  3781. <p><strong>foo</strong>bar</p>
  3782. .
  3783. Rule 8:
  3784. This is not strong emphasis, because the closing delimiter is
  3785. preceded by whitespace:
  3786. .
  3787. __foo bar __
  3788. .
  3789. <p>__foo bar __</p>
  3790. .
  3791. Intraword strong emphasis examples:
  3792. .
  3793. __foo__bar
  3794. .
  3795. <p>__foo__bar</p>
  3796. .
  3797. .
  3798. __пристаням__стремятся
  3799. .
  3800. <p><strong>пристаням</strong>стремятся</p>
  3801. .
  3802. .
  3803. __foo__bar__baz__
  3804. .
  3805. <p><strong>foo__bar__baz</strong></p>
  3806. .
  3807. Rule 9:
  3808. Any nonempty sequence of inline elements can be the contents of an
  3809. emphasized span.
  3810. .
  3811. *foo [bar](/url)*
  3812. .
  3813. <p><em>foo <a href="/url">bar</a></em></p>
  3814. .
  3815. .
  3816. *foo
  3817. bar*
  3818. .
  3819. <p><em>foo
  3820. bar</em></p>
  3821. .
  3822. In particular, emphasis and strong emphasis can be nested
  3823. inside emphasis:
  3824. .
  3825. _foo __bar__ baz_
  3826. .
  3827. <p><em>foo <strong>bar</strong> baz</em></p>
  3828. .
  3829. .
  3830. _foo _bar_ baz_
  3831. .
  3832. <p><em>foo <em>bar</em> baz</em></p>
  3833. .
  3834. .
  3835. __foo_ bar_
  3836. .
  3837. <p><em><em>foo</em> bar</em></p>
  3838. .
  3839. .
  3840. *foo *bar**
  3841. .
  3842. <p><em>foo <em>bar</em></em></p>
  3843. .
  3844. .
  3845. *foo **bar** baz*
  3846. .
  3847. <p><em>foo <strong>bar</strong> baz</em></p>
  3848. .
  3849. But note:
  3850. .
  3851. *foo**bar**baz*
  3852. .
  3853. <p><em>foo</em><em>bar</em><em>baz</em></p>
  3854. .
  3855. The difference is that in the preceding case,
  3856. the internal delimiters [can close emphasis](#can-close-emphasis),
  3857. while in the cases with spaces, they cannot.
  3858. .
  3859. ***foo** bar*
  3860. .
  3861. <p><em><strong>foo</strong> bar</em></p>
  3862. .
  3863. .
  3864. *foo **bar***
  3865. .
  3866. <p><em>foo <strong>bar</strong></em></p>
  3867. .
  3868. Note, however, that in the following case we get no strong
  3869. emphasis, because the opening delimiter is closed by the first
  3870. `*` before `bar`:
  3871. .
  3872. *foo**bar***
  3873. .
  3874. <p><em>foo</em><em>bar</em>**</p>
  3875. .
  3876. Indefinite levels of nesting are possible:
  3877. .
  3878. *foo **bar *baz* bim** bop*
  3879. .
  3880. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  3881. .
  3882. .
  3883. *foo [*bar*](/url)*
  3884. .
  3885. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  3886. .
  3887. There can be no empty emphasis or strong emphasis:
  3888. .
  3889. ** is not an empty emphasis
  3890. .
  3891. <p>** is not an empty emphasis</p>
  3892. .
  3893. .
  3894. **** is not an empty strong emphasis
  3895. .
  3896. <p>**** is not an empty strong emphasis</p>
  3897. .
  3898. Rule 10:
  3899. Any nonempty sequence of inline elements can be the contents of an
  3900. strongly emphasized span.
  3901. .
  3902. **foo [bar](/url)**
  3903. .
  3904. <p><strong>foo <a href="/url">bar</a></strong></p>
  3905. .
  3906. .
  3907. **foo
  3908. bar**
  3909. .
  3910. <p><strong>foo
  3911. bar</strong></p>
  3912. .
  3913. In particular, emphasis and strong emphasis can be nested
  3914. inside strong emphasis:
  3915. .
  3916. __foo _bar_ baz__
  3917. .
  3918. <p><strong>foo <em>bar</em> baz</strong></p>
  3919. .
  3920. .
  3921. __foo __bar__ baz__
  3922. .
  3923. <p><strong>foo <strong>bar</strong> baz</strong></p>
  3924. .
  3925. .
  3926. ____foo__ bar__
  3927. .
  3928. <p><strong><strong>foo</strong> bar</strong></p>
  3929. .
  3930. .
  3931. **foo **bar****
  3932. .
  3933. <p><strong>foo <strong>bar</strong></strong></p>
  3934. .
  3935. .
  3936. **foo *bar* baz**
  3937. .
  3938. <p><strong>foo <em>bar</em> baz</strong></p>
  3939. .
  3940. But note:
  3941. .
  3942. **foo*bar*baz**
  3943. .
  3944. <p><em><em>foo</em>bar</em>baz**</p>
  3945. .
  3946. The difference is that in the preceding case,
  3947. the internal delimiters [can close emphasis](#can-close-emphasis),
  3948. while in the cases with spaces, they cannot.
  3949. .
  3950. ***foo* bar**
  3951. .
  3952. <p><strong><em>foo</em> bar</strong></p>
  3953. .
  3954. .
  3955. **foo *bar***
  3956. .
  3957. <p><strong>foo <em>bar</em></strong></p>
  3958. .
  3959. Indefinite levels of nesting are possible:
  3960. .
  3961. **foo *bar **baz**
  3962. bim* bop**
  3963. .
  3964. <p><strong>foo <em>bar <strong>baz</strong>
  3965. bim</em> bop</strong></p>
  3966. .
  3967. .
  3968. **foo [*bar*](/url)**
  3969. .
  3970. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  3971. .
  3972. There can be no empty emphasis or strong emphasis:
  3973. .
  3974. __ is not an empty emphasis
  3975. .
  3976. <p>__ is not an empty emphasis</p>
  3977. .
  3978. .
  3979. ____ is not an empty strong emphasis
  3980. .
  3981. <p>____ is not an empty strong emphasis</p>
  3982. .
  3983. Rule 11:
  3984. .
  3985. foo ***
  3986. .
  3987. <p>foo ***</p>
  3988. .
  3989. .
  3990. foo *\**
  3991. .
  3992. <p>foo <em>*</em></p>
  3993. .
  3994. .
  3995. foo *_*
  3996. .
  3997. <p>foo <em>_</em></p>
  3998. .
  3999. .
  4000. foo *****
  4001. .
  4002. <p>foo *****</p>
  4003. .
  4004. .
  4005. foo **\***
  4006. .
  4007. <p>foo <strong>*</strong></p>
  4008. .
  4009. .
  4010. foo **_**
  4011. .
  4012. <p>foo <strong>_</strong></p>
  4013. .
  4014. Note that when delimiters do not match evenly, Rule 11 determines
  4015. that the excess literal `*` characters will appear outside of the
  4016. emphasis, rather than inside it:
  4017. .
  4018. **foo*
  4019. .
  4020. <p>*<em>foo</em></p>
  4021. .
  4022. .
  4023. *foo**
  4024. .
  4025. <p><em>foo</em>*</p>
  4026. .
  4027. .
  4028. ***foo**
  4029. .
  4030. <p>*<strong>foo</strong></p>
  4031. .
  4032. .
  4033. ****foo*
  4034. .
  4035. <p>***<em>foo</em></p>
  4036. .
  4037. .
  4038. **foo***
  4039. .
  4040. <p><strong>foo</strong>*</p>
  4041. .
  4042. .
  4043. *foo****
  4044. .
  4045. <p><em>foo</em>***</p>
  4046. .
  4047. Rule 12:
  4048. .
  4049. foo ___
  4050. .
  4051. <p>foo ___</p>
  4052. .
  4053. .
  4054. foo _\__
  4055. .
  4056. <p>foo <em>_</em></p>
  4057. .
  4058. .
  4059. foo _*_
  4060. .
  4061. <p>foo <em>*</em></p>
  4062. .
  4063. .
  4064. foo _____
  4065. .
  4066. <p>foo _____</p>
  4067. .
  4068. .
  4069. foo __\___
  4070. .
  4071. <p>foo <strong>_</strong></p>
  4072. .
  4073. .
  4074. foo __*__
  4075. .
  4076. <p>foo <strong>*</strong></p>
  4077. .
  4078. .
  4079. __foo_
  4080. .
  4081. <p>_<em>foo</em></p>
  4082. .
  4083. Note that when delimiters do not match evenly, Rule 12 determines
  4084. that the excess literal `_` characters will appear outside of the
  4085. emphasis, rather than inside it:
  4086. .
  4087. _foo__
  4088. .
  4089. <p><em>foo</em>_</p>
  4090. .
  4091. .
  4092. ___foo__
  4093. .
  4094. <p>_<strong>foo</strong></p>
  4095. .
  4096. .
  4097. ____foo_
  4098. .
  4099. <p>___<em>foo</em></p>
  4100. .
  4101. .
  4102. __foo___
  4103. .
  4104. <p><strong>foo</strong>_</p>
  4105. .
  4106. .
  4107. _foo____
  4108. .
  4109. <p><em>foo</em>___</p>
  4110. .
  4111. Rule 13 implies that if you want emphasis nested directly inside
  4112. emphasis, you must use different delimiters:
  4113. .
  4114. **foo**
  4115. .
  4116. <p><strong>foo</strong></p>
  4117. .
  4118. .
  4119. *_foo_*
  4120. .
  4121. <p><em><em>foo</em></em></p>
  4122. .
  4123. .
  4124. __foo__
  4125. .
  4126. <p><strong>foo</strong></p>
  4127. .
  4128. .
  4129. _*foo*_
  4130. .
  4131. <p><em><em>foo</em></em></p>
  4132. .
  4133. However, strong emphasis within strong emphasisis possible without
  4134. switching delimiters:
  4135. .
  4136. ****foo****
  4137. .
  4138. <p><strong><strong>foo</strong></strong></p>
  4139. .
  4140. .
  4141. ____foo____
  4142. .
  4143. <p><strong><strong>foo</strong></strong></p>
  4144. .
  4145. Rule 13 can be applied to arbitrarily long sequences of
  4146. delimiters:
  4147. .
  4148. ******foo******
  4149. .
  4150. <p><strong><strong><strong>foo</strong></strong></strong></p>
  4151. .
  4152. Rule 14:
  4153. .
  4154. ***foo***
  4155. .
  4156. <p><strong><em>foo</em></strong></p>
  4157. .
  4158. .
  4159. _____foo_____
  4160. .
  4161. <p><strong><strong><em>foo</em></strong></strong></p>
  4162. .
  4163. Rule 15:
  4164. .
  4165. *foo _bar* baz_
  4166. .
  4167. <p><em>foo _bar</em> baz_</p>
  4168. .
  4169. .
  4170. **foo*bar**
  4171. .
  4172. <p><em><em>foo</em>bar</em>*</p>
  4173. .
  4174. Rule 16:
  4175. .
  4176. **foo **bar baz**
  4177. .
  4178. <p>**foo <strong>bar baz</strong></p>
  4179. .
  4180. .
  4181. *foo *bar baz*
  4182. .
  4183. <p>*foo <em>bar baz</em></p>
  4184. .
  4185. Rule 17:
  4186. .
  4187. *[bar*](/url)
  4188. .
  4189. <p>*<a href="/url">bar*</a></p>
  4190. .
  4191. .
  4192. _foo [bar_](/url)
  4193. .
  4194. <p>_foo <a href="/url">bar_</a></p>
  4195. .
  4196. .
  4197. *<img src="foo" title="*"/>
  4198. .
  4199. <p>*<img src="foo" title="*"/></p>
  4200. .
  4201. .
  4202. **<a href="**">
  4203. .
  4204. <p>**<a href="**"></p>
  4205. .
  4206. .
  4207. __<a href="__">
  4208. .
  4209. <p>__<a href="__"></p>
  4210. .
  4211. .
  4212. *a `*`*
  4213. .
  4214. <p><em>a <code>*</code></em></p>
  4215. .
  4216. .
  4217. _a `_`_
  4218. .
  4219. <p><em>a <code>_</code></em></p>
  4220. .
  4221. .
  4222. **a<http://foo.bar?q=**>
  4223. .
  4224. <p>**a<a href="http://foo.bar?q=**">http://foo.bar?q=**</a></p>
  4225. .
  4226. .
  4227. __a<http://foo.bar?q=__>
  4228. .
  4229. <p>__a<a href="http://foo.bar?q=__">http://foo.bar?q=__</a></p>
  4230. .
  4231. ## Links
  4232. A link contains [link text](#link-label) (the visible text),
  4233. a [destination](#destination) (the URI that is the link destination),
  4234. and optionally a [link title](#link-title). There are two basic kinds
  4235. of links in Markdown. In [inline links](#inline-links) the destination
  4236. and title are given immediately after the link text. In [reference
  4237. links](#reference-links) the destination and title are defined elsewhere
  4238. in the document.
  4239. A [link text](@link-text) consists of a sequence of zero or more
  4240. inline elements enclosed by square brackets (`[` and `]`). The
  4241. following rules apply:
  4242. - Links may not contain other links, at any level of nesting.
  4243. - Brackets are allowed in the [link text](#link-text) only if (a) they
  4244. are backslash-escaped or (b) they appear as a matched pair of brackets,
  4245. with an open bracket `[`, a sequence of zero or more inlines, and
  4246. a close bracket `]`.
  4247. - Backtick [code spans](#code-span), [autolinks](#autolink), and
  4248. raw [HTML tags](#html-tag) bind more tightly
  4249. than the brackets in link text. Thus, for example,
  4250. `` [foo`]` `` could not be a link text, since the second `]`
  4251. is part of a code span.
  4252. - The brackets in link text bind more tightly than markers for
  4253. [emphasis and strong emphasis](#emphasis-and-strong-emphasis).
  4254. Thus, for example, `*[foo*](url)` is a link.
  4255. A [link destination](@link-destination) consists of either
  4256. - a sequence of zero or more characters between an opening `<` and a
  4257. closing `>` that contains no line breaks or unescaped `<` or `>`
  4258. characters, or
  4259. - a nonempty sequence of characters that does not include
  4260. ASCII space or control characters, and includes parentheses
  4261. only if (a) they are backslash-escaped or (b) they are part of
  4262. a balanced pair of unescaped parentheses that is not itself
  4263. inside a balanced pair of unescaped paretheses.
  4264. A [link title](@link-title) consists of either
  4265. - a sequence of zero or more characters between straight double-quote
  4266. characters (`"`), including a `"` character only if it is
  4267. backslash-escaped, or
  4268. - a sequence of zero or more characters between straight single-quote
  4269. characters (`'`), including a `'` character only if it is
  4270. backslash-escaped, or
  4271. - a sequence of zero or more characters between matching parentheses
  4272. (`(...)`), including a `)` character only if it is backslash-escaped.
  4273. An [inline link](@inline-link)
  4274. consists of a [link text](#link-text) followed immediately
  4275. by a left parenthesis `(`, optional whitespace,
  4276. an optional [link destination](#link-destination),
  4277. an optional [link title](#link-title) separated from the link
  4278. destination by whitespace, optional whitespace, and a right
  4279. parenthesis `)`. The link's text consists of the inlines contained
  4280. in the [link text](#link-text) (excluding the enclosing square brackets).
  4281. The link's URI consists of the link destination, excluding enclosing
  4282. `<...>` if present, with backslash-escapes in effect as described
  4283. above. The link's title consists of the link title, excluding its
  4284. enclosing delimiters, with backslash-escapes in effect as described
  4285. above.
  4286. Here is a simple inline link:
  4287. .
  4288. [link](/uri "title")
  4289. .
  4290. <p><a href="/uri" title="title">link</a></p>
  4291. .
  4292. The title may be omitted:
  4293. .
  4294. [link](/uri)
  4295. .
  4296. <p><a href="/uri">link</a></p>
  4297. .
  4298. Both the title and the destination may be omitted:
  4299. .
  4300. [link]()
  4301. .
  4302. <p><a href="">link</a></p>
  4303. .
  4304. .
  4305. [link](<>)
  4306. .
  4307. <p><a href="">link</a></p>
  4308. .
  4309. If the destination contains spaces, it must be enclosed in pointy
  4310. braces:
  4311. .
  4312. [link](/my uri)
  4313. .
  4314. <p>[link](/my uri)</p>
  4315. .
  4316. .
  4317. [link](</my uri>)
  4318. .
  4319. <p><a href="/my%20uri">link</a></p>
  4320. .
  4321. The destination cannot contain line breaks, even with pointy braces:
  4322. .
  4323. [link](foo
  4324. bar)
  4325. .
  4326. <p>[link](foo
  4327. bar)</p>
  4328. .
  4329. One level of balanced parentheses is allowed without escaping:
  4330. .
  4331. [link]((foo)and(bar))
  4332. .
  4333. <p><a href="(foo)and(bar)">link</a></p>
  4334. .
  4335. However, if you have parentheses within parentheses, you need to escape
  4336. or use the `<...>` form:
  4337. .
  4338. [link](foo(and(bar)))
  4339. .
  4340. <p>[link](foo(and(bar)))</p>
  4341. .
  4342. .
  4343. [link](foo(and\(bar\)))
  4344. .
  4345. <p><a href="foo(and(bar))">link</a></p>
  4346. .
  4347. .
  4348. [link](<foo(and(bar))>)
  4349. .
  4350. <p><a href="foo(and(bar))">link</a></p>
  4351. .
  4352. Parentheses and other symbols can also be escaped, as usual
  4353. in Markdown:
  4354. .
  4355. [link](foo\)\:)
  4356. .
  4357. <p><a href="foo):">link</a></p>
  4358. .
  4359. URL-escaping should be left alone inside the destination, as all
  4360. URL-escaped characters are also valid URL characters. HTML entities in
  4361. the destination will be parsed into their UTF-8 codepoints, as usual, and
  4362. optionally URL-escaped when written as HTML.
  4363. .
  4364. [link](foo%20b&auml;)
  4365. .
  4366. <p><a href="foo%20b%C3%A4">link</a></p>
  4367. .
  4368. Note that, because titles can often be parsed as destinations,
  4369. if you try to omit the destination and keep the title, you'll
  4370. get unexpected results:
  4371. .
  4372. [link]("title")
  4373. .
  4374. <p><a href="%22title%22">link</a></p>
  4375. .
  4376. Titles may be in single quotes, double quotes, or parentheses:
  4377. .
  4378. [link](/url "title")
  4379. [link](/url 'title')
  4380. [link](/url (title))
  4381. .
  4382. <p><a href="/url" title="title">link</a>
  4383. <a href="/url" title="title">link</a>
  4384. <a href="/url" title="title">link</a></p>
  4385. .
  4386. Backslash escapes and entities may be used in titles:
  4387. .
  4388. [link](/url "title \"&quot;")
  4389. .
  4390. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  4391. .
  4392. Nested balanced quotes are not allowed without escaping:
  4393. .
  4394. [link](/url "title "and" title")
  4395. .
  4396. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  4397. .
  4398. But it is easy to work around this by using a different quote type:
  4399. .
  4400. [link](/url 'title "and" title')
  4401. .
  4402. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  4403. .
  4404. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  4405. title, and its test suite included a test demonstrating this.
  4406. But it is hard to see a good rationale for the extra complexity this
  4407. brings, since there are already many ways---backslash escaping,
  4408. entities, or using a different quote type for the enclosing title---to
  4409. write titles containing double quotes. `Markdown.pl`'s handling of
  4410. titles has a number of other strange features. For example, it allows
  4411. single-quoted titles in inline links, but not reference links. And, in
  4412. reference links but not inline links, it allows a title to begin with
  4413. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  4414. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  4415. a simple, rational rule that works the same way in inline links and
  4416. link reference definitions.)
  4417. Whitespace is allowed around the destination and title:
  4418. .
  4419. [link]( /uri
  4420. "title" )
  4421. .
  4422. <p><a href="/uri" title="title">link</a></p>
  4423. .
  4424. But it is not allowed between the link text and the
  4425. following parenthesis:
  4426. .
  4427. [link] (/uri)
  4428. .
  4429. <p>[link] (/uri)</p>
  4430. .
  4431. The link text may contain balanced brackets, but not unbalanced ones,
  4432. unless they are escaped:
  4433. .
  4434. [link [foo [bar]]](/uri)
  4435. .
  4436. <p><a href="/uri">link [foo [bar]]</a></p>
  4437. .
  4438. .
  4439. [link] bar](/uri)
  4440. .
  4441. <p>[link] bar](/uri)</p>
  4442. .
  4443. .
  4444. [link [bar](/uri)
  4445. .
  4446. <p>[link <a href="/uri">bar</a></p>
  4447. .
  4448. .
  4449. [link \[bar](/uri)
  4450. .
  4451. <p><a href="/uri">link [bar</a></p>
  4452. .
  4453. The link text may contain inline content:
  4454. .
  4455. [link *foo **bar** `#`*](/uri)
  4456. .
  4457. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4458. .
  4459. .
  4460. [![moon](moon.jpg)](/uri)
  4461. .
  4462. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4463. .
  4464. However, links may not contain other links, at any level of nesting.
  4465. .
  4466. [foo [bar](/uri)](/uri)
  4467. .
  4468. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  4469. .
  4470. .
  4471. [foo *[bar [baz](/uri)](/uri)*](/uri)
  4472. .
  4473. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  4474. .
  4475. These cases illustrate the precedence of link text grouping over
  4476. emphasis grouping:
  4477. .
  4478. *[foo*](/uri)
  4479. .
  4480. <p>*<a href="/uri">foo*</a></p>
  4481. .
  4482. .
  4483. [foo *bar](baz*)
  4484. .
  4485. <p><a href="baz*">foo *bar</a></p>
  4486. .
  4487. These cases illustrate the precedence of HTML tags, code spans,
  4488. and autolinks over link grouping:
  4489. .
  4490. [foo <bar attr="](baz)">
  4491. .
  4492. <p>[foo <bar attr="](baz)"></p>
  4493. .
  4494. .
  4495. [foo`](/uri)`
  4496. .
  4497. <p>[foo<code>](/uri)</code></p>
  4498. .
  4499. .
  4500. [foo<http://example.com?search=](uri)>
  4501. .
  4502. <p>[foo<a href="http://example.com?search=%5D(uri)">http://example.com?search=](uri)</a></p>
  4503. .
  4504. There are three kinds of [reference links](@reference-link):
  4505. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  4506. and [shortcut](#shortcut-reference-link).
  4507. A [full reference link](@full-reference-link)
  4508. consists of a [link text](#link-text), optional whitespace, and
  4509. a [link label](#link-label) that [matches](#matches) a
  4510. [link reference definition](#link-reference-definition) elsewhere in the
  4511. document.
  4512. A [link label](@link-label) begins with a left bracket (`[`) and ends
  4513. with the first right bracket (`]`) that is not backslash-escaped.
  4514. Unescaped square bracket characters are not allowed in
  4515. [link labels](#link-label). A link label can have at most 999
  4516. characters inside the square brackets.
  4517. One label [matches](@matches)
  4518. another just in case their normalized forms are equal. To normalize a
  4519. label, perform the *unicode case fold* and collapse consecutive internal
  4520. whitespace to a single space. If there are multiple matching reference
  4521. link definitions, the one that comes first in the document is used. (It
  4522. is desirable in such cases to emit a warning.)
  4523. The contents of the first link label are parsed as inlines, which are
  4524. used as the link's text. The link's URI and title are provided by the
  4525. matching [link reference definition](#link-reference-definition).
  4526. Here is a simple example:
  4527. .
  4528. [foo][bar]
  4529. [bar]: /url "title"
  4530. .
  4531. <p><a href="/url" title="title">foo</a></p>
  4532. .
  4533. The rules for the [link text](#link-text) are the same as with
  4534. [inline links](#inline-link). Thus:
  4535. The link text may contain balanced brackets, but not unbalanced ones,
  4536. unless they are escaped:
  4537. .
  4538. [link [foo [bar]]][ref]
  4539. [ref]: /uri
  4540. .
  4541. <p><a href="/uri">link [foo [bar]]</a></p>
  4542. .
  4543. .
  4544. [link \[bar][ref]
  4545. [ref]: /uri
  4546. .
  4547. <p><a href="/uri">link [bar</a></p>
  4548. .
  4549. The link text may contain inline content:
  4550. .
  4551. [link *foo **bar** `#`*][ref]
  4552. [ref]: /uri
  4553. .
  4554. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4555. .
  4556. .
  4557. [![moon](moon.jpg)][ref]
  4558. [ref]: /uri
  4559. .
  4560. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4561. .
  4562. However, links may not contain other links, at any level of nesting.
  4563. .
  4564. [foo [bar](/uri)][ref]
  4565. [ref]: /uri
  4566. .
  4567. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  4568. .
  4569. .
  4570. [foo *bar [baz][ref]*][ref]
  4571. [ref]: /uri
  4572. .
  4573. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  4574. .
  4575. (In the examples above, we have two [shortcut reference
  4576. links](#shortcut-reference-link) instead of one [full reference
  4577. link](#full-reference-link).)
  4578. The following cases illustrate the precedence of link text grouping over
  4579. emphasis grouping:
  4580. .
  4581. *[foo*][ref]
  4582. [ref]: /uri
  4583. .
  4584. <p>*<a href="/uri">foo*</a></p>
  4585. .
  4586. .
  4587. [foo *bar][ref]
  4588. [ref]: /uri
  4589. .
  4590. <p><a href="/uri">foo *bar</a></p>
  4591. .
  4592. These cases illustrate the precedence of HTML tags, code spans,
  4593. and autolinks over link grouping:
  4594. .
  4595. [foo <bar attr="][ref]">
  4596. [ref]: /uri
  4597. .
  4598. <p>[foo <bar attr="][ref]"></p>
  4599. .
  4600. .
  4601. [foo`][ref]`
  4602. [ref]: /uri
  4603. .
  4604. <p>[foo<code>][ref]</code></p>
  4605. .
  4606. .
  4607. [foo<http://example.com?search=][ref]>
  4608. [ref]: /uri
  4609. .
  4610. <p>[foo<a href="http://example.com?search=%5D%5Bref%5D">http://example.com?search=][ref]</a></p>
  4611. .
  4612. Matching is case-insensitive:
  4613. .
  4614. [foo][BaR]
  4615. [bar]: /url "title"
  4616. .
  4617. <p><a href="/url" title="title">foo</a></p>
  4618. .
  4619. Unicode case fold is used:
  4620. .
  4621. [Толпой][Толпой] is a Russian word.
  4622. [ТОЛПОЙ]: /url
  4623. .
  4624. <p><a href="/url">Толпой</a> is a Russian word.</p>
  4625. .
  4626. Consecutive internal whitespace is treated as one space for
  4627. purposes of determining matching:
  4628. .
  4629. [Foo
  4630. bar]: /url
  4631. [Baz][Foo bar]
  4632. .
  4633. <p><a href="/url">Baz</a></p>
  4634. .
  4635. There can be whitespace between the [link text](#link-text) and the
  4636. [link label](#link-label):
  4637. .
  4638. [foo] [bar]
  4639. [bar]: /url "title"
  4640. .
  4641. <p><a href="/url" title="title">foo</a></p>
  4642. .
  4643. .
  4644. [foo]
  4645. [bar]
  4646. [bar]: /url "title"
  4647. .
  4648. <p><a href="/url" title="title">foo</a></p>
  4649. .
  4650. When there are multiple matching [link reference
  4651. definitions](#link-reference-definition), the first is used:
  4652. .
  4653. [foo]: /url1
  4654. [foo]: /url2
  4655. [bar][foo]
  4656. .
  4657. <p><a href="/url1">bar</a></p>
  4658. .
  4659. Note that matching is performed on normalized strings, not parsed
  4660. inline content. So the following does not match, even though the
  4661. labels define equivalent inline content:
  4662. .
  4663. [bar][foo\!]
  4664. [foo!]: /url
  4665. .
  4666. <p>[bar][foo!]</p>
  4667. .
  4668. [Link labels](#link-label) cannot contain brackets, unless they are
  4669. backslash-escaped:
  4670. .
  4671. [foo][ref[]
  4672. [ref[]: /uri
  4673. .
  4674. <p>[foo][ref[]</p>
  4675. <p>[ref[]: /uri</p>
  4676. .
  4677. .
  4678. [foo][ref[bar]]
  4679. [ref[bar]]: /uri
  4680. .
  4681. <p>[foo][ref[bar]]</p>
  4682. <p>[ref[bar]]: /uri</p>
  4683. .
  4684. .
  4685. [[[foo]]]
  4686. [[[foo]]]: /url
  4687. .
  4688. <p>[[[foo]]]</p>
  4689. <p>[[[foo]]]: /url</p>
  4690. .
  4691. .
  4692. [foo][ref\[]
  4693. [ref\[]: /uri
  4694. .
  4695. <p><a href="/uri">foo</a></p>
  4696. .
  4697. A [collapsed reference link](@collapsed-reference-link)
  4698. consists of a [link
  4699. label](#link-label) that [matches](#matches) a [link reference
  4700. definition](#link-reference-definition) elsewhere in the
  4701. document, optional whitespace, and the string `[]`. The contents of the
  4702. first link label are parsed as inlines, which are used as the link's
  4703. text. The link's URI and title are provided by the matching reference
  4704. link definition. Thus, `[foo][]` is equivalent to `[foo][foo]`.
  4705. .
  4706. [foo][]
  4707. [foo]: /url "title"
  4708. .
  4709. <p><a href="/url" title="title">foo</a></p>
  4710. .
  4711. .
  4712. [*foo* bar][]
  4713. [*foo* bar]: /url "title"
  4714. .
  4715. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4716. .
  4717. The link labels are case-insensitive:
  4718. .
  4719. [Foo][]
  4720. [foo]: /url "title"
  4721. .
  4722. <p><a href="/url" title="title">Foo</a></p>
  4723. .
  4724. As with full reference links, whitespace is allowed
  4725. between the two sets of brackets:
  4726. .
  4727. [foo]
  4728. []
  4729. [foo]: /url "title"
  4730. .
  4731. <p><a href="/url" title="title">foo</a></p>
  4732. .
  4733. A [shortcut reference link](@shortcut-reference-link)
  4734. consists of a [link
  4735. label](#link-label) that [matches](#matches) a [link reference
  4736. definition](#link-reference-definition) elsewhere in the
  4737. document and is not followed by `[]` or a link label.
  4738. The contents of the first link label are parsed as inlines,
  4739. which are used as the link's text. the link's URI and title
  4740. are provided by the matching link reference definition.
  4741. Thus, `[foo]` is equivalent to `[foo][]`.
  4742. .
  4743. [foo]
  4744. [foo]: /url "title"
  4745. .
  4746. <p><a href="/url" title="title">foo</a></p>
  4747. .
  4748. .
  4749. [*foo* bar]
  4750. [*foo* bar]: /url "title"
  4751. .
  4752. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4753. .
  4754. .
  4755. [[*foo* bar]]
  4756. [*foo* bar]: /url "title"
  4757. .
  4758. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  4759. .
  4760. The link labels are case-insensitive:
  4761. .
  4762. [Foo]
  4763. [foo]: /url "title"
  4764. .
  4765. <p><a href="/url" title="title">Foo</a></p>
  4766. .
  4767. A space after the link text should be preserved:
  4768. .
  4769. [foo] bar
  4770. [foo]: /url
  4771. .
  4772. <p><a href="/url">foo</a> bar</p>
  4773. .
  4774. If you just want bracketed text, you can backslash-escape the
  4775. opening bracket to avoid links:
  4776. .
  4777. \[foo]
  4778. [foo]: /url "title"
  4779. .
  4780. <p>[foo]</p>
  4781. .
  4782. Note that this is a link, because a link label ends with the first
  4783. following closing bracket:
  4784. .
  4785. [foo*]: /url
  4786. *[foo*]
  4787. .
  4788. <p>*<a href="/url">foo*</a></p>
  4789. .
  4790. This is a link too, for the same reason:
  4791. .
  4792. [foo`]: /url
  4793. [foo`]`
  4794. .
  4795. <p>[foo<code>]</code></p>
  4796. .
  4797. Full references take precedence over shortcut references:
  4798. .
  4799. [foo][bar]
  4800. [foo]: /url1
  4801. [bar]: /url2
  4802. .
  4803. <p><a href="/url2">foo</a></p>
  4804. .
  4805. In the following case `[bar][baz]` is parsed as a reference,
  4806. `[foo]` as normal text:
  4807. .
  4808. [foo][bar][baz]
  4809. [baz]: /url
  4810. .
  4811. <p>[foo]<a href="/url">bar</a></p>
  4812. .
  4813. Here, though, `[foo][bar]` is parsed as a reference, since
  4814. `[bar]` is defined:
  4815. .
  4816. [foo][bar][baz]
  4817. [baz]: /url1
  4818. [bar]: /url2
  4819. .
  4820. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  4821. .
  4822. Here `[foo]` is not parsed as a shortcut reference, because it
  4823. is followed by a link label (even though `[bar]` is not defined):
  4824. .
  4825. [foo][bar][baz]
  4826. [baz]: /url1
  4827. [foo]: /url2
  4828. .
  4829. <p>[foo]<a href="/url1">bar</a></p>
  4830. .
  4831. ## Images
  4832. Syntax for images is like the syntax for links, with one
  4833. difference. Instead of [link text](#link-text), we have an [image
  4834. description](@image-description). The rules for this are the
  4835. same as for [link text](#link-text), except that (a) an
  4836. image description starts with `![` rather than `[`, and
  4837. (b) an image description may contain links.
  4838. An image description has inline elements
  4839. as its contents. When an image is rendered to HTML,
  4840. this is standardly used as the image's `alt` attribute.
  4841. .
  4842. ![foo](/url "title")
  4843. .
  4844. <p><img src="/url" alt="foo" title="title" /></p>
  4845. .
  4846. .
  4847. ![foo *bar*]
  4848. [foo *bar*]: train.jpg "train & tracks"
  4849. .
  4850. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4851. .
  4852. .
  4853. ![foo ![bar](/url)](/url2)
  4854. .
  4855. <p><img src="/url2" alt="foo bar" /></p>
  4856. .
  4857. .
  4858. ![foo [bar](/url)](/url2)
  4859. .
  4860. <p><img src="/url2" alt="foo bar" /></p>
  4861. .
  4862. Though this spec is concerned with parsing, not rendering, it is
  4863. recommended that in rendering to HTML, only the plain string content
  4864. of the [image description](#image-description) be used. Note that in
  4865. the above example, the alt attribute's value is `foo bar`, not `foo
  4866. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  4867. content is rendered, without formatting.
  4868. .
  4869. ![foo *bar*][]
  4870. [foo *bar*]: train.jpg "train & tracks"
  4871. .
  4872. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4873. .
  4874. .
  4875. ![foo *bar*][foobar]
  4876. [FOOBAR]: train.jpg "train & tracks"
  4877. .
  4878. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4879. .
  4880. .
  4881. ![foo](train.jpg)
  4882. .
  4883. <p><img src="train.jpg" alt="foo" /></p>
  4884. .
  4885. .
  4886. My ![foo bar](/path/to/train.jpg "title" )
  4887. .
  4888. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  4889. .
  4890. .
  4891. ![foo](<url>)
  4892. .
  4893. <p><img src="url" alt="foo" /></p>
  4894. .
  4895. .
  4896. ![](/url)
  4897. .
  4898. <p><img src="/url" alt="" /></p>
  4899. .
  4900. Reference-style:
  4901. .
  4902. ![foo] [bar]
  4903. [bar]: /url
  4904. .
  4905. <p><img src="/url" alt="foo" /></p>
  4906. .
  4907. .
  4908. ![foo] [bar]
  4909. [BAR]: /url
  4910. .
  4911. <p><img src="/url" alt="foo" /></p>
  4912. .
  4913. Collapsed:
  4914. .
  4915. ![foo][]
  4916. [foo]: /url "title"
  4917. .
  4918. <p><img src="/url" alt="foo" title="title" /></p>
  4919. .
  4920. .
  4921. ![*foo* bar][]
  4922. [*foo* bar]: /url "title"
  4923. .
  4924. <p><img src="/url" alt="foo bar" title="title" /></p>
  4925. .
  4926. The labels are case-insensitive:
  4927. .
  4928. ![Foo][]
  4929. [foo]: /url "title"
  4930. .
  4931. <p><img src="/url" alt="Foo" title="title" /></p>
  4932. .
  4933. As with full reference links, whitespace is allowed
  4934. between the two sets of brackets:
  4935. .
  4936. ![foo]
  4937. []
  4938. [foo]: /url "title"
  4939. .
  4940. <p><img src="/url" alt="foo" title="title" /></p>
  4941. .
  4942. Shortcut:
  4943. .
  4944. ![foo]
  4945. [foo]: /url "title"
  4946. .
  4947. <p><img src="/url" alt="foo" title="title" /></p>
  4948. .
  4949. .
  4950. ![*foo* bar]
  4951. [*foo* bar]: /url "title"
  4952. .
  4953. <p><img src="/url" alt="foo bar" title="title" /></p>
  4954. .
  4955. Note that link labels cannot contain unescaped brackets:
  4956. .
  4957. ![[foo]]
  4958. [[foo]]: /url "title"
  4959. .
  4960. <p>![[foo]]</p>
  4961. <p>[[foo]]: /url &quot;title&quot;</p>
  4962. .
  4963. The link labels are case-insensitive:
  4964. .
  4965. ![Foo]
  4966. [foo]: /url "title"
  4967. .
  4968. <p><img src="/url" alt="Foo" title="title" /></p>
  4969. .
  4970. If you just want bracketed text, you can backslash-escape the
  4971. opening `!` and `[`:
  4972. .
  4973. \!\[foo]
  4974. [foo]: /url "title"
  4975. .
  4976. <p>![foo]</p>
  4977. .
  4978. If you want a link after a literal `!`, backslash-escape the
  4979. `!`:
  4980. .
  4981. \![foo]
  4982. [foo]: /url "title"
  4983. .
  4984. <p>!<a href="/url" title="title">foo</a></p>
  4985. .
  4986. ## Autolinks
  4987. [Autolinks](@autolink) are absolute URIs and email addresses inside `<` and `>`.
  4988. They are parsed as links, with the URL or email address as the link
  4989. label.
  4990. A [URI autolink](@uri-autolink)
  4991. consists of `<`, followed by an [absolute
  4992. URI](#absolute-uri) not containing `<`, followed by `>`. It is parsed
  4993. as a link to the URI, with the URI as the link's label.
  4994. An [absolute URI](@absolute-uri),
  4995. for these purposes, consists of a [scheme](#scheme) followed by a colon (`:`)
  4996. followed by zero or more characters other than ASCII whitespace and
  4997. control characters, `<`, and `>`. If the URI includes these characters,
  4998. you must use percent-encoding (e.g. `%20` for a space).
  4999. The following [schemes](@scheme)
  5000. are recognized (case-insensitive):
  5001. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  5002. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  5003. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  5004. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  5005. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  5006. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  5007. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  5008. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5009. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5010. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5011. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5012. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5013. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5014. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5015. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5016. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5017. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5018. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5019. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5020. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5021. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5022. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5023. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5024. Here are some valid autolinks:
  5025. .
  5026. <http://foo.bar.baz>
  5027. .
  5028. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5029. .
  5030. .
  5031. <http://foo.bar.baz?q=hello&id=22&boolean>
  5032. .
  5033. <p><a href="http://foo.bar.baz?q=hello&amp;id=22&amp;boolean">http://foo.bar.baz?q=hello&amp;id=22&amp;boolean</a></p>
  5034. .
  5035. .
  5036. <irc://foo.bar:2233/baz>
  5037. .
  5038. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5039. .
  5040. Uppercase is also fine:
  5041. .
  5042. <MAILTO:FOO@BAR.BAZ>
  5043. .
  5044. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5045. .
  5046. Spaces are not allowed in autolinks:
  5047. .
  5048. <http://foo.bar/baz bim>
  5049. .
  5050. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5051. .
  5052. An [email autolink](@email-autolink)
  5053. consists of `<`, followed by an [email address](#email-address),
  5054. followed by `>`. The link's label is the email address,
  5055. and the URL is `mailto:` followed by the email address.
  5056. An [email address](@email-address),
  5057. for these purposes, is anything that matches
  5058. the [non-normative regex from the HTML5
  5059. spec](http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#e-mail-state-%28type=email%29):
  5060. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5061. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5062. Examples of email autolinks:
  5063. .
  5064. <foo@bar.example.com>
  5065. .
  5066. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5067. .
  5068. .
  5069. <foo+special@Bar.baz-bar0.com>
  5070. .
  5071. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5072. .
  5073. These are not autolinks:
  5074. .
  5075. <>
  5076. .
  5077. <p>&lt;&gt;</p>
  5078. .
  5079. .
  5080. <heck://bing.bong>
  5081. .
  5082. <p>&lt;heck://bing.bong&gt;</p>
  5083. .
  5084. .
  5085. < http://foo.bar >
  5086. .
  5087. <p>&lt; http://foo.bar &gt;</p>
  5088. .
  5089. .
  5090. <foo.bar.baz>
  5091. .
  5092. <p>&lt;foo.bar.baz&gt;</p>
  5093. .
  5094. .
  5095. <localhost:5001/foo>
  5096. .
  5097. <p>&lt;localhost:5001/foo&gt;</p>
  5098. .
  5099. .
  5100. http://example.com
  5101. .
  5102. <p>http://example.com</p>
  5103. .
  5104. .
  5105. foo@bar.example.com
  5106. .
  5107. <p>foo@bar.example.com</p>
  5108. .
  5109. ## Raw HTML
  5110. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5111. raw HTML tag and will be rendered in HTML without escaping.
  5112. Tag and attribute names are not limited to current HTML tags,
  5113. so custom tags (and even, say, DocBook tags) may be used.
  5114. Here is the grammar for tags:
  5115. A [tag name](@tag-name) consists of an ASCII letter
  5116. followed by zero or more ASCII letters or digits.
  5117. An [attribute](@attribute) consists of whitespace,
  5118. an [attribute name](#attribute-name), and an optional
  5119. [attribute value specification](#attribute-value-specification).
  5120. An [attribute name](@attribute-name)
  5121. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5122. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5123. specification restricted to ASCII. HTML5 is laxer.)
  5124. An [attribute value specification](@attribute-value-specification)
  5125. consists of optional whitespace,
  5126. a `=` character, optional whitespace, and an [attribute
  5127. value](#attribute-value).
  5128. An [attribute value](@attribute-value)
  5129. consists of an [unquoted attribute value](#unquoted-attribute-value),
  5130. a [single-quoted attribute value](#single-quoted-attribute-value),
  5131. or a [double-quoted attribute value](#double-quoted-attribute-value).
  5132. An [unquoted attribute value](@unquoted-attribute-value)
  5133. is a nonempty string of characters not
  5134. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5135. A [single-quoted attribute value](@single-quoted-attribute-value)
  5136. consists of `'`, zero or more
  5137. characters not including `'`, and a final `'`.
  5138. A [double-quoted attribute value](@double-quoted-attribute-value)
  5139. consists of `"`, zero or more
  5140. characters not including `"`, and a final `"`.
  5141. An [open tag](@open-tag) consists of a `<` character,
  5142. a [tag name](#tag-name), zero or more [attributes](#attribute),
  5143. optional whitespace, an optional `/` character, and a `>` character.
  5144. A [closing tag](@closing-tag) consists of the
  5145. string `</`, a [tag name](#tag-name), optional whitespace, and the
  5146. character `>`.
  5147. An [HTML comment](@html-comment) consists of the
  5148. string `<!--`, a string of characters not including the string `--`, and
  5149. the string `-->`.
  5150. A [processing instruction](@processing-instruction)
  5151. consists of the string `<?`, a string
  5152. of characters not including the string `?>`, and the string
  5153. `?>`.
  5154. A [declaration](@declaration) consists of the
  5155. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5156. whitespace, a string of characters not including the character `>`, and
  5157. the character `>`.
  5158. A [CDATA section](@cdata-section) consists of
  5159. the string `<![CDATA[`, a string of characters not including the string
  5160. `]]>`, and the string `]]>`.
  5161. An [HTML tag](@html-tag) consists of an [open
  5162. tag](#open-tag), a [closing tag](#closing-tag), an [HTML
  5163. comment](#html-comment), a [processing
  5164. instruction](#processing-instruction), an [element type
  5165. declaration](#element-type-declaration), or a [CDATA
  5166. section](#cdata-section).
  5167. Here are some simple open tags:
  5168. .
  5169. <a><bab><c2c>
  5170. .
  5171. <p><a><bab><c2c></p>
  5172. .
  5173. Empty elements:
  5174. .
  5175. <a/><b2/>
  5176. .
  5177. <p><a/><b2/></p>
  5178. .
  5179. Whitespace is allowed:
  5180. .
  5181. <a /><b2
  5182. data="foo" >
  5183. .
  5184. <p><a /><b2
  5185. data="foo" ></p>
  5186. .
  5187. With attributes:
  5188. .
  5189. <a foo="bar" bam = 'baz <em>"</em>'
  5190. _boolean zoop:33=zoop:33 />
  5191. .
  5192. <p><a foo="bar" bam = 'baz <em>"</em>'
  5193. _boolean zoop:33=zoop:33 /></p>
  5194. .
  5195. Illegal tag names, not parsed as HTML:
  5196. .
  5197. <33> <__>
  5198. .
  5199. <p>&lt;33&gt; &lt;__&gt;</p>
  5200. .
  5201. Illegal attribute names:
  5202. .
  5203. <a h*#ref="hi">
  5204. .
  5205. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5206. .
  5207. Illegal attribute values:
  5208. .
  5209. <a href="hi'> <a href=hi'>
  5210. .
  5211. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5212. .
  5213. Illegal whitespace:
  5214. .
  5215. < a><
  5216. foo><bar/ >
  5217. .
  5218. <p>&lt; a&gt;&lt;
  5219. foo&gt;&lt;bar/ &gt;</p>
  5220. .
  5221. Missing whitespace:
  5222. .
  5223. <a href='bar'title=title>
  5224. .
  5225. <p>&lt;a href='bar'title=title&gt;</p>
  5226. .
  5227. Closing tags:
  5228. .
  5229. </a>
  5230. </foo >
  5231. .
  5232. <p></a>
  5233. </foo ></p>
  5234. .
  5235. Illegal attributes in closing tag:
  5236. .
  5237. </a href="foo">
  5238. .
  5239. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5240. .
  5241. Comments:
  5242. .
  5243. foo <!-- this is a
  5244. comment - with hyphen -->
  5245. .
  5246. <p>foo <!-- this is a
  5247. comment - with hyphen --></p>
  5248. .
  5249. .
  5250. foo <!-- not a comment -- two hyphens -->
  5251. .
  5252. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5253. .
  5254. Processing instructions:
  5255. .
  5256. foo <?php echo $a; ?>
  5257. .
  5258. <p>foo <?php echo $a; ?></p>
  5259. .
  5260. Declarations:
  5261. .
  5262. foo <!ELEMENT br EMPTY>
  5263. .
  5264. <p>foo <!ELEMENT br EMPTY></p>
  5265. .
  5266. CDATA sections:
  5267. .
  5268. foo <![CDATA[>&<]]>
  5269. .
  5270. <p>foo <![CDATA[>&<]]></p>
  5271. .
  5272. Entities are preserved in HTML attributes:
  5273. .
  5274. <a href="&ouml;">
  5275. .
  5276. <p><a href="&ouml;"></p>
  5277. .
  5278. Backslash escapes do not work in HTML attributes:
  5279. .
  5280. <a href="\*">
  5281. .
  5282. <p><a href="\*"></p>
  5283. .
  5284. .
  5285. <a href="\"">
  5286. .
  5287. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5288. .
  5289. ## Hard line breaks
  5290. A line break (not in a code span or HTML tag) that is preceded
  5291. by two or more spaces and does not occur at the end of a block
  5292. is parsed as a [hard line break](@hard-line-break) (rendered
  5293. in HTML as a `<br />` tag):
  5294. .
  5295. foo
  5296. baz
  5297. .
  5298. <p>foo<br />
  5299. baz</p>
  5300. .
  5301. For a more visible alternative, a backslash before the newline may be
  5302. used instead of two spaces:
  5303. .
  5304. foo\
  5305. baz
  5306. .
  5307. <p>foo<br />
  5308. baz</p>
  5309. .
  5310. More than two spaces can be used:
  5311. .
  5312. foo
  5313. baz
  5314. .
  5315. <p>foo<br />
  5316. baz</p>
  5317. .
  5318. Leading spaces at the beginning of the next line are ignored:
  5319. .
  5320. foo
  5321. bar
  5322. .
  5323. <p>foo<br />
  5324. bar</p>
  5325. .
  5326. .
  5327. foo\
  5328. bar
  5329. .
  5330. <p>foo<br />
  5331. bar</p>
  5332. .
  5333. Line breaks can occur inside emphasis, links, and other constructs
  5334. that allow inline content:
  5335. .
  5336. *foo
  5337. bar*
  5338. .
  5339. <p><em>foo<br />
  5340. bar</em></p>
  5341. .
  5342. .
  5343. *foo\
  5344. bar*
  5345. .
  5346. <p><em>foo<br />
  5347. bar</em></p>
  5348. .
  5349. Line breaks do not occur inside code spans
  5350. .
  5351. `code
  5352. span`
  5353. .
  5354. <p><code>code span</code></p>
  5355. .
  5356. .
  5357. `code\
  5358. span`
  5359. .
  5360. <p><code>code\ span</code></p>
  5361. .
  5362. or HTML tags:
  5363. .
  5364. <a href="foo
  5365. bar">
  5366. .
  5367. <p><a href="foo
  5368. bar"></p>
  5369. .
  5370. .
  5371. <a href="foo\
  5372. bar">
  5373. .
  5374. <p><a href="foo\
  5375. bar"></p>
  5376. .
  5377. Hard line breaks are for separating inline content within a block.
  5378. Neither syntax for hard line breaks works at the end of a paragraph or
  5379. other block element:
  5380. .
  5381. foo\
  5382. .
  5383. <p>foo\</p>
  5384. .
  5385. .
  5386. foo
  5387. .
  5388. <p>foo</p>
  5389. .
  5390. .
  5391. ### foo\
  5392. .
  5393. <h3>foo\</h3>
  5394. .
  5395. .
  5396. ### foo
  5397. .
  5398. <h3>foo</h3>
  5399. .
  5400. ## Soft line breaks
  5401. A regular line break (not in a code span or HTML tag) that is not
  5402. preceded by two or more spaces is parsed as a softbreak. (A
  5403. softbreak may be rendered in HTML either as a newline or as a space.
  5404. The result will be the same in browsers. In the examples here, a
  5405. newline will be used.)
  5406. .
  5407. foo
  5408. baz
  5409. .
  5410. <p>foo
  5411. baz</p>
  5412. .
  5413. Spaces at the end of the line and beginning of the next line are
  5414. removed:
  5415. .
  5416. foo
  5417. baz
  5418. .
  5419. <p>foo
  5420. baz</p>
  5421. .
  5422. A conforming parser may render a soft line break in HTML either as a
  5423. line break or as a space.
  5424. A renderer may also provide an option to render soft line breaks
  5425. as hard line breaks.
  5426. ## Textual content
  5427. Any characters not given an interpretation by the above rules will
  5428. be parsed as plain textual content.
  5429. .
  5430. hello $.;'there
  5431. .
  5432. <p>hello $.;'there</p>
  5433. .
  5434. .
  5435. Foo χρῆν
  5436. .
  5437. <p>Foo χρῆν</p>
  5438. .
  5439. Internal spaces are preserved verbatim:
  5440. .
  5441. Multiple spaces
  5442. .
  5443. <p>Multiple spaces</p>
  5444. .
  5445. <!-- END TESTS -->
  5446. # Appendix A: A parsing strategy {-}
  5447. ## Overview {-}
  5448. Parsing has two phases:
  5449. 1. In the first phase, lines of input are consumed and the block
  5450. structure of the document---its division into paragraphs, block quotes,
  5451. list items, and so on---is constructed. Text is assigned to these
  5452. blocks but not parsed. Link reference definitions are parsed and a
  5453. map of links is constructed.
  5454. 2. In the second phase, the raw text contents of paragraphs and headers
  5455. are parsed into sequences of Markdown inline elements (strings,
  5456. code spans, links, emphasis, and so on), using the map of link
  5457. references constructed in phase 1.
  5458. ## The document tree {-}
  5459. At each point in processing, the document is represented as a tree of
  5460. **blocks**. The root of the tree is a `document` block. The `document`
  5461. may have any number of other blocks as **children**. These children
  5462. may, in turn, have other blocks as children. The last child of a block
  5463. is normally considered **open**, meaning that subsequent lines of input
  5464. can alter its contents. (Blocks that are not open are **closed**.)
  5465. Here, for example, is a possible document tree, with the open blocks
  5466. marked by arrows:
  5467. ``` tree
  5468. -> document
  5469. -> block_quote
  5470. paragraph
  5471. "Lorem ipsum dolor\nsit amet."
  5472. -> list (type=bullet tight=true bullet_char=-)
  5473. list_item
  5474. paragraph
  5475. "Qui *quodsi iracundia*"
  5476. -> list_item
  5477. -> paragraph
  5478. "aliquando id"
  5479. ```
  5480. ## How source lines alter the document tree {-}
  5481. Each line that is processed has an effect on this tree. The line is
  5482. analyzed and, depending on its contents, the document may be altered
  5483. in one or more of the following ways:
  5484. 1. One or more open blocks may be closed.
  5485. 2. One or more new blocks may be created as children of the
  5486. last open block.
  5487. 3. Text may be added to the last (deepest) open block remaining
  5488. on the tree.
  5489. Once a line has been incorporated into the tree in this way,
  5490. it can be discarded, so input can be read in a stream.
  5491. We can see how this works by considering how the tree above is
  5492. generated by four lines of Markdown:
  5493. ``` markdown
  5494. > Lorem ipsum dolor
  5495. sit amet.
  5496. > - Qui *quodsi iracundia*
  5497. > - aliquando id
  5498. ```
  5499. At the outset, our document model is just
  5500. ``` tree
  5501. -> document
  5502. ```
  5503. The first line of our text,
  5504. ``` markdown
  5505. > Lorem ipsum dolor
  5506. ```
  5507. causes a `block_quote` block to be created as a child of our
  5508. open `document` block, and a `paragraph` block as a child of
  5509. the `block_quote`. Then the text is added to the last open
  5510. block, the `paragraph`:
  5511. ``` tree
  5512. -> document
  5513. -> block_quote
  5514. -> paragraph
  5515. "Lorem ipsum dolor"
  5516. ```
  5517. The next line,
  5518. ``` markdown
  5519. sit amet.
  5520. ```
  5521. is a "lazy continuation" of the open `paragraph`, so it gets added
  5522. to the paragraph's text:
  5523. ``` tree
  5524. -> document
  5525. -> block_quote
  5526. -> paragraph
  5527. "Lorem ipsum dolor\nsit amet."
  5528. ```
  5529. The third line,
  5530. ``` markdown
  5531. > - Qui *quodsi iracundia*
  5532. ```
  5533. causes the `paragraph` block to be closed, and a new `list` block
  5534. opened as a child of the `block_quote`. A `list_item` is also
  5535. added as a child of the `list`, and a `paragraph` as a child of
  5536. the `list_item`. The text is then added to the new `paragraph`:
  5537. ``` tree
  5538. -> document
  5539. -> block_quote
  5540. paragraph
  5541. "Lorem ipsum dolor\nsit amet."
  5542. -> list (type=bullet tight=true bullet_char=-)
  5543. -> list_item
  5544. -> paragraph
  5545. "Qui *quodsi iracundia*"
  5546. ```
  5547. The fourth line,
  5548. ``` markdown
  5549. > - aliquando id
  5550. ```
  5551. causes the `list_item` (and its child the `paragraph`) to be closed,
  5552. and a new `list_item` opened up as child of the `list`. A `paragraph`
  5553. is added as a child of the new `list_item`, to contain the text.
  5554. We thus obtain the final tree:
  5555. ``` tree
  5556. -> document
  5557. -> block_quote
  5558. paragraph
  5559. "Lorem ipsum dolor\nsit amet."
  5560. -> list (type=bullet tight=true bullet_char=-)
  5561. list_item
  5562. paragraph
  5563. "Qui *quodsi iracundia*"
  5564. -> list_item
  5565. -> paragraph
  5566. "aliquando id"
  5567. ```
  5568. ## From block structure to the final document {-}
  5569. Once all of the input has been parsed, all open blocks are closed.
  5570. We then "walk the tree," visiting every node, and parse raw
  5571. string contents of paragraphs and headers as inlines. At this
  5572. point we have seen all the link reference definitions, so we can
  5573. resolve reference links as we go.
  5574. ``` tree
  5575. document
  5576. block_quote
  5577. paragraph
  5578. str "Lorem ipsum dolor"
  5579. softbreak
  5580. str "sit amet."
  5581. list (type=bullet tight=true bullet_char=-)
  5582. list_item
  5583. paragraph
  5584. str "Qui "
  5585. emph
  5586. str "quodsi iracundia"
  5587. list_item
  5588. paragraph
  5589. str "aliquando id"
  5590. ```
  5591. Notice how the newline in the first paragraph has been parsed as
  5592. a `softbreak`, and the asterisks in the first list item have become
  5593. an `emph`.
  5594. The document can be rendered as HTML, or in any other format, given
  5595. an appropriate renderer.