aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 3217e6c827a7dd4c5f3447ce2c551b8f3cd2fcbc (plain)
  1. ---
  2. title: CommonMark Spec
  3. author:
  4. - John MacFarlane
  5. version: 0.13
  6. date: 2014-12-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 test/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) is composed of one or more
  766. [indented chunks](#indented-chunk) separated by blank lines.
  767. An [indented chunk](@indented-chunk) is a sequence of non-blank lines,
  768. each indented four or more spaces. The contents of the code block are
  769. the literal contents of the lines, including trailing newlines,
  770. minus four spaces of indentation. An indented code block has no
  771. attributes.
  772. An indented code block cannot interrupt a paragraph, so there must be
  773. a blank line between a paragraph and a following indented code block.
  774. (A blank line is not needed, however, between a code block and a following
  775. paragraph.)
  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 whitespace. (For these purposes,
  3555. any unicode space character counts as 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 whitespace.
  3572. 8. A double `__` [can close strong emphasis](#can-close-strong-emphasis)
  3573. iff it is not preceded by whitespace and it is not followed by an
  3574. ASCII alphanumeric character.
  3575. 9. Emphasis begins with a delimiter that [can open
  3576. emphasis](#can-open-emphasis) and ends with a delimiter that [can close
  3577. emphasis](#can-close-emphasis), and that uses the same
  3578. character (`_` or `*`) as the opening delimiter. There must
  3579. be a nonempty sequence of inlines between the open delimiter
  3580. and the closing delimiter; these form the contents of the emphasis
  3581. inline.
  3582. 10. Strong emphasis begins with a delimiter that [can open strong
  3583. emphasis](#can-open-strong-emphasis) and ends with a delimiter that
  3584. [can close strong emphasis](#can-close-strong-emphasis), and that
  3585. uses the same character (`_` or `*`) as the opening delimiter.
  3586. There must be a nonempty sequence of inlines between the open
  3587. delimiter and the closing delimiter; these form the contents of
  3588. the strong emphasis inline.
  3589. 11. A literal `*` character cannot occur at the beginning or end of
  3590. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  3591. is backslash-escaped.
  3592. 12. A literal `_` character cannot occur at the beginning or end of
  3593. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3594. is backslash-escaped.
  3595. Where rules 1--12 above are compatible with multiple parsings,
  3596. the following principles resolve ambiguity:
  3597. 13. The number of nestings should be minimized. Thus, for example,
  3598. an interpretation `<strong>...</strong>` is always preferred to
  3599. `<em><em>...</em></em>`.
  3600. 14. An interpretation `<strong><em>...</em></strong>` is always
  3601. preferred to `<em><strong>..</strong></em>`.
  3602. 15. When two potential emphasis or strong emphasis spans overlap,
  3603. so that the second begins before the first ends and ends after
  3604. the first ends, the first is preferred. Thus, for example,
  3605. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3606. than `*foo <em>bar* baz</em>`. For the same reason,
  3607. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3608. rather than `<strong>foo*bar</strong>`.
  3609. 16. When there are two potential emphasis or strong emphasis spans
  3610. with the same closing delimiter, the shorter one (the one that
  3611. opens later) is preferred. Thus, for example,
  3612. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3613. rather than `<strong>foo **bar baz</strong>`.
  3614. 17. Inline code spans, links, images, and HTML tags group more tightly
  3615. than emphasis. So, when there is a choice between an interpretation
  3616. that contains one of these elements and one that does not, the
  3617. former always wins. Thus, for example, `*[foo*](bar)` is
  3618. parsed as `*<a href="bar">foo*</a>` rather than as
  3619. `<em>[foo</em>](bar)`.
  3620. These rules can be illustrated through a series of examples.
  3621. Rule 1:
  3622. .
  3623. *foo bar*
  3624. .
  3625. <p><em>foo bar</em></p>
  3626. .
  3627. This is not emphasis, because the opening `*` is followed by
  3628. whitespace:
  3629. .
  3630. a * foo bar*
  3631. .
  3632. <p>a * foo bar*</p>
  3633. .
  3634. Unicode nonbreaking spaces count as whitespace, too:
  3635. .
  3636. * a *
  3637. .
  3638. <p>* a *</p>
  3639. .
  3640. Intraword emphasis with `*` is permitted:
  3641. .
  3642. foo*bar*
  3643. .
  3644. <p>foo<em>bar</em></p>
  3645. .
  3646. .
  3647. 5*6*78
  3648. .
  3649. <p>5<em>6</em>78</p>
  3650. .
  3651. Rule 2:
  3652. .
  3653. _foo bar_
  3654. .
  3655. <p><em>foo bar</em></p>
  3656. .
  3657. This is not emphasis, because the opening `*` is followed by
  3658. whitespace:
  3659. .
  3660. _ foo bar_
  3661. .
  3662. <p>_ foo bar_</p>
  3663. .
  3664. Emphasis with `_` is not allowed inside ASCII words:
  3665. .
  3666. foo_bar_
  3667. .
  3668. <p>foo_bar_</p>
  3669. .
  3670. .
  3671. 5_6_78
  3672. .
  3673. <p>5_6_78</p>
  3674. .
  3675. But it is permitted inside non-ASCII words:
  3676. .
  3677. пристаням_стремятся_
  3678. .
  3679. <p>пристаням<em>стремятся</em></p>
  3680. .
  3681. Rule 3:
  3682. This is not emphasis, because the closing `*` is preceded by
  3683. whitespace:
  3684. .
  3685. *foo bar *
  3686. .
  3687. <p>*foo bar *</p>
  3688. .
  3689. Intraword emphasis with `*` is allowed:
  3690. .
  3691. *foo*bar
  3692. .
  3693. <p><em>foo</em>bar</p>
  3694. .
  3695. Rule 4:
  3696. This is not emphasis, because the closing `_` is preceded by
  3697. whitespace:
  3698. .
  3699. _foo bar _
  3700. .
  3701. <p>_foo bar _</p>
  3702. .
  3703. Intraword emphasis:
  3704. .
  3705. _foo_bar
  3706. .
  3707. <p>_foo_bar</p>
  3708. .
  3709. .
  3710. _пристаням_стремятся
  3711. .
  3712. <p><em>пристаням</em>стремятся</p>
  3713. .
  3714. .
  3715. _foo_bar_baz_
  3716. .
  3717. <p><em>foo_bar_baz</em></p>
  3718. .
  3719. Rule 5:
  3720. .
  3721. **foo bar**
  3722. .
  3723. <p><strong>foo bar</strong></p>
  3724. .
  3725. This is not strong emphasis, because the opening delimiter is
  3726. followed by whitespace:
  3727. .
  3728. ** foo bar**
  3729. .
  3730. <p>** foo bar**</p>
  3731. .
  3732. Intraword strong emphasis with `**` is permitted:
  3733. .
  3734. foo**bar**
  3735. .
  3736. <p>foo<strong>bar</strong></p>
  3737. .
  3738. Rule 6:
  3739. .
  3740. __foo bar__
  3741. .
  3742. <p><strong>foo bar</strong></p>
  3743. .
  3744. This is not strong emphasis, because the opening delimiter is
  3745. followed by whitespace:
  3746. .
  3747. __ foo bar__
  3748. .
  3749. <p>__ foo bar__</p>
  3750. .
  3751. Intraword emphasis examples:
  3752. .
  3753. foo__bar__
  3754. .
  3755. <p>foo__bar__</p>
  3756. .
  3757. .
  3758. 5__6__78
  3759. .
  3760. <p>5__6__78</p>
  3761. .
  3762. .
  3763. пристаням__стремятся__
  3764. .
  3765. <p>пристаням<strong>стремятся</strong></p>
  3766. .
  3767. .
  3768. __foo, __bar__, baz__
  3769. .
  3770. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  3771. .
  3772. Rule 7:
  3773. This is not strong emphasis, because the closing delimiter is preceded
  3774. by whitespace:
  3775. .
  3776. **foo bar **
  3777. .
  3778. <p>**foo bar **</p>
  3779. .
  3780. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  3781. Rule 11.)
  3782. Intraword emphasis:
  3783. .
  3784. **foo**bar
  3785. .
  3786. <p><strong>foo</strong>bar</p>
  3787. .
  3788. Rule 8:
  3789. This is not strong emphasis, because the closing delimiter is
  3790. preceded by whitespace:
  3791. .
  3792. __foo bar __
  3793. .
  3794. <p>__foo bar __</p>
  3795. .
  3796. Intraword strong emphasis examples:
  3797. .
  3798. __foo__bar
  3799. .
  3800. <p>__foo__bar</p>
  3801. .
  3802. .
  3803. __пристаням__стремятся
  3804. .
  3805. <p><strong>пристаням</strong>стремятся</p>
  3806. .
  3807. .
  3808. __foo__bar__baz__
  3809. .
  3810. <p><strong>foo__bar__baz</strong></p>
  3811. .
  3812. Rule 9:
  3813. Any nonempty sequence of inline elements can be the contents of an
  3814. emphasized span.
  3815. .
  3816. *foo [bar](/url)*
  3817. .
  3818. <p><em>foo <a href="/url">bar</a></em></p>
  3819. .
  3820. .
  3821. *foo
  3822. bar*
  3823. .
  3824. <p><em>foo
  3825. bar</em></p>
  3826. .
  3827. In particular, emphasis and strong emphasis can be nested
  3828. inside emphasis:
  3829. .
  3830. _foo __bar__ baz_
  3831. .
  3832. <p><em>foo <strong>bar</strong> baz</em></p>
  3833. .
  3834. .
  3835. _foo _bar_ baz_
  3836. .
  3837. <p><em>foo <em>bar</em> baz</em></p>
  3838. .
  3839. .
  3840. __foo_ bar_
  3841. .
  3842. <p><em><em>foo</em> bar</em></p>
  3843. .
  3844. .
  3845. *foo *bar**
  3846. .
  3847. <p><em>foo <em>bar</em></em></p>
  3848. .
  3849. .
  3850. *foo **bar** baz*
  3851. .
  3852. <p><em>foo <strong>bar</strong> baz</em></p>
  3853. .
  3854. But note:
  3855. .
  3856. *foo**bar**baz*
  3857. .
  3858. <p><em>foo</em><em>bar</em><em>baz</em></p>
  3859. .
  3860. The difference is that in the preceding case,
  3861. the internal delimiters [can close emphasis](#can-close-emphasis),
  3862. while in the cases with spaces, they cannot.
  3863. .
  3864. ***foo** bar*
  3865. .
  3866. <p><em><strong>foo</strong> bar</em></p>
  3867. .
  3868. .
  3869. *foo **bar***
  3870. .
  3871. <p><em>foo <strong>bar</strong></em></p>
  3872. .
  3873. Note, however, that in the following case we get no strong
  3874. emphasis, because the opening delimiter is closed by the first
  3875. `*` before `bar`:
  3876. .
  3877. *foo**bar***
  3878. .
  3879. <p><em>foo</em><em>bar</em>**</p>
  3880. .
  3881. Indefinite levels of nesting are possible:
  3882. .
  3883. *foo **bar *baz* bim** bop*
  3884. .
  3885. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  3886. .
  3887. .
  3888. *foo [*bar*](/url)*
  3889. .
  3890. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  3891. .
  3892. There can be no empty emphasis or strong emphasis:
  3893. .
  3894. ** is not an empty emphasis
  3895. .
  3896. <p>** is not an empty emphasis</p>
  3897. .
  3898. .
  3899. **** is not an empty strong emphasis
  3900. .
  3901. <p>**** is not an empty strong emphasis</p>
  3902. .
  3903. Rule 10:
  3904. Any nonempty sequence of inline elements can be the contents of an
  3905. strongly emphasized span.
  3906. .
  3907. **foo [bar](/url)**
  3908. .
  3909. <p><strong>foo <a href="/url">bar</a></strong></p>
  3910. .
  3911. .
  3912. **foo
  3913. bar**
  3914. .
  3915. <p><strong>foo
  3916. bar</strong></p>
  3917. .
  3918. In particular, emphasis and strong emphasis can be nested
  3919. inside strong emphasis:
  3920. .
  3921. __foo _bar_ baz__
  3922. .
  3923. <p><strong>foo <em>bar</em> baz</strong></p>
  3924. .
  3925. .
  3926. __foo __bar__ baz__
  3927. .
  3928. <p><strong>foo <strong>bar</strong> baz</strong></p>
  3929. .
  3930. .
  3931. ____foo__ bar__
  3932. .
  3933. <p><strong><strong>foo</strong> bar</strong></p>
  3934. .
  3935. .
  3936. **foo **bar****
  3937. .
  3938. <p><strong>foo <strong>bar</strong></strong></p>
  3939. .
  3940. .
  3941. **foo *bar* baz**
  3942. .
  3943. <p><strong>foo <em>bar</em> baz</strong></p>
  3944. .
  3945. But note:
  3946. .
  3947. **foo*bar*baz**
  3948. .
  3949. <p><em><em>foo</em>bar</em>baz**</p>
  3950. .
  3951. The difference is that in the preceding case,
  3952. the internal delimiters [can close emphasis](#can-close-emphasis),
  3953. while in the cases with spaces, they cannot.
  3954. .
  3955. ***foo* bar**
  3956. .
  3957. <p><strong><em>foo</em> bar</strong></p>
  3958. .
  3959. .
  3960. **foo *bar***
  3961. .
  3962. <p><strong>foo <em>bar</em></strong></p>
  3963. .
  3964. Indefinite levels of nesting are possible:
  3965. .
  3966. **foo *bar **baz**
  3967. bim* bop**
  3968. .
  3969. <p><strong>foo <em>bar <strong>baz</strong>
  3970. bim</em> bop</strong></p>
  3971. .
  3972. .
  3973. **foo [*bar*](/url)**
  3974. .
  3975. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  3976. .
  3977. There can be no empty emphasis or strong emphasis:
  3978. .
  3979. __ is not an empty emphasis
  3980. .
  3981. <p>__ is not an empty emphasis</p>
  3982. .
  3983. .
  3984. ____ is not an empty strong emphasis
  3985. .
  3986. <p>____ is not an empty strong emphasis</p>
  3987. .
  3988. Rule 11:
  3989. .
  3990. foo ***
  3991. .
  3992. <p>foo ***</p>
  3993. .
  3994. .
  3995. foo *\**
  3996. .
  3997. <p>foo <em>*</em></p>
  3998. .
  3999. .
  4000. foo *_*
  4001. .
  4002. <p>foo <em>_</em></p>
  4003. .
  4004. .
  4005. foo *****
  4006. .
  4007. <p>foo *****</p>
  4008. .
  4009. .
  4010. foo **\***
  4011. .
  4012. <p>foo <strong>*</strong></p>
  4013. .
  4014. .
  4015. foo **_**
  4016. .
  4017. <p>foo <strong>_</strong></p>
  4018. .
  4019. Note that when delimiters do not match evenly, Rule 11 determines
  4020. that the excess literal `*` characters will appear outside of the
  4021. emphasis, rather than inside it:
  4022. .
  4023. **foo*
  4024. .
  4025. <p>*<em>foo</em></p>
  4026. .
  4027. .
  4028. *foo**
  4029. .
  4030. <p><em>foo</em>*</p>
  4031. .
  4032. .
  4033. ***foo**
  4034. .
  4035. <p>*<strong>foo</strong></p>
  4036. .
  4037. .
  4038. ****foo*
  4039. .
  4040. <p>***<em>foo</em></p>
  4041. .
  4042. .
  4043. **foo***
  4044. .
  4045. <p><strong>foo</strong>*</p>
  4046. .
  4047. .
  4048. *foo****
  4049. .
  4050. <p><em>foo</em>***</p>
  4051. .
  4052. Rule 12:
  4053. .
  4054. foo ___
  4055. .
  4056. <p>foo ___</p>
  4057. .
  4058. .
  4059. foo _\__
  4060. .
  4061. <p>foo <em>_</em></p>
  4062. .
  4063. .
  4064. foo _*_
  4065. .
  4066. <p>foo <em>*</em></p>
  4067. .
  4068. .
  4069. foo _____
  4070. .
  4071. <p>foo _____</p>
  4072. .
  4073. .
  4074. foo __\___
  4075. .
  4076. <p>foo <strong>_</strong></p>
  4077. .
  4078. .
  4079. foo __*__
  4080. .
  4081. <p>foo <strong>*</strong></p>
  4082. .
  4083. .
  4084. __foo_
  4085. .
  4086. <p>_<em>foo</em></p>
  4087. .
  4088. Note that when delimiters do not match evenly, Rule 12 determines
  4089. that the excess literal `_` characters will appear outside of the
  4090. emphasis, rather than inside it:
  4091. .
  4092. _foo__
  4093. .
  4094. <p><em>foo</em>_</p>
  4095. .
  4096. .
  4097. ___foo__
  4098. .
  4099. <p>_<strong>foo</strong></p>
  4100. .
  4101. .
  4102. ____foo_
  4103. .
  4104. <p>___<em>foo</em></p>
  4105. .
  4106. .
  4107. __foo___
  4108. .
  4109. <p><strong>foo</strong>_</p>
  4110. .
  4111. .
  4112. _foo____
  4113. .
  4114. <p><em>foo</em>___</p>
  4115. .
  4116. Rule 13 implies that if you want emphasis nested directly inside
  4117. emphasis, you must use different delimiters:
  4118. .
  4119. **foo**
  4120. .
  4121. <p><strong>foo</strong></p>
  4122. .
  4123. .
  4124. *_foo_*
  4125. .
  4126. <p><em><em>foo</em></em></p>
  4127. .
  4128. .
  4129. __foo__
  4130. .
  4131. <p><strong>foo</strong></p>
  4132. .
  4133. .
  4134. _*foo*_
  4135. .
  4136. <p><em><em>foo</em></em></p>
  4137. .
  4138. However, strong emphasis within strong emphasisis possible without
  4139. switching delimiters:
  4140. .
  4141. ****foo****
  4142. .
  4143. <p><strong><strong>foo</strong></strong></p>
  4144. .
  4145. .
  4146. ____foo____
  4147. .
  4148. <p><strong><strong>foo</strong></strong></p>
  4149. .
  4150. Rule 13 can be applied to arbitrarily long sequences of
  4151. delimiters:
  4152. .
  4153. ******foo******
  4154. .
  4155. <p><strong><strong><strong>foo</strong></strong></strong></p>
  4156. .
  4157. Rule 14:
  4158. .
  4159. ***foo***
  4160. .
  4161. <p><strong><em>foo</em></strong></p>
  4162. .
  4163. .
  4164. _____foo_____
  4165. .
  4166. <p><strong><strong><em>foo</em></strong></strong></p>
  4167. .
  4168. Rule 15:
  4169. .
  4170. *foo _bar* baz_
  4171. .
  4172. <p><em>foo _bar</em> baz_</p>
  4173. .
  4174. .
  4175. **foo*bar**
  4176. .
  4177. <p><em><em>foo</em>bar</em>*</p>
  4178. .
  4179. Rule 16:
  4180. .
  4181. **foo **bar baz**
  4182. .
  4183. <p>**foo <strong>bar baz</strong></p>
  4184. .
  4185. .
  4186. *foo *bar baz*
  4187. .
  4188. <p>*foo <em>bar baz</em></p>
  4189. .
  4190. Rule 17:
  4191. .
  4192. *[bar*](/url)
  4193. .
  4194. <p>*<a href="/url">bar*</a></p>
  4195. .
  4196. .
  4197. _foo [bar_](/url)
  4198. .
  4199. <p>_foo <a href="/url">bar_</a></p>
  4200. .
  4201. .
  4202. *<img src="foo" title="*"/>
  4203. .
  4204. <p>*<img src="foo" title="*"/></p>
  4205. .
  4206. .
  4207. **<a href="**">
  4208. .
  4209. <p>**<a href="**"></p>
  4210. .
  4211. .
  4212. __<a href="__">
  4213. .
  4214. <p>__<a href="__"></p>
  4215. .
  4216. .
  4217. *a `*`*
  4218. .
  4219. <p><em>a <code>*</code></em></p>
  4220. .
  4221. .
  4222. _a `_`_
  4223. .
  4224. <p><em>a <code>_</code></em></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. .
  4232. __a<http://foo.bar?q=__>
  4233. .
  4234. <p>__a<a href="http://foo.bar?q=__">http://foo.bar?q=__</a></p>
  4235. .
  4236. ## Links
  4237. A link contains [link text](#link-label) (the visible text),
  4238. a [destination](#destination) (the URI that is the link destination),
  4239. and optionally a [link title](#link-title). There are two basic kinds
  4240. of links in Markdown. In [inline links](#inline-links) the destination
  4241. and title are given immediately after the link text. In [reference
  4242. links](#reference-links) the destination and title are defined elsewhere
  4243. in the document.
  4244. A [link text](@link-text) consists of a sequence of zero or more
  4245. inline elements enclosed by square brackets (`[` and `]`). The
  4246. following rules apply:
  4247. - Links may not contain other links, at any level of nesting.
  4248. - Brackets are allowed in the [link text](#link-text) only if (a) they
  4249. are backslash-escaped or (b) they appear as a matched pair of brackets,
  4250. with an open bracket `[`, a sequence of zero or more inlines, and
  4251. a close bracket `]`.
  4252. - Backtick [code spans](#code-span), [autolinks](#autolink), and
  4253. raw [HTML tags](#html-tag) bind more tightly
  4254. than the brackets in link text. Thus, for example,
  4255. `` [foo`]` `` could not be a link text, since the second `]`
  4256. is part of a code span.
  4257. - The brackets in link text bind more tightly than markers for
  4258. [emphasis and strong emphasis](#emphasis-and-strong-emphasis).
  4259. Thus, for example, `*[foo*](url)` is a link.
  4260. A [link destination](@link-destination) consists of either
  4261. - a sequence of zero or more characters between an opening `<` and a
  4262. closing `>` that contains no line breaks or unescaped `<` or `>`
  4263. characters, or
  4264. - a nonempty sequence of characters that does not include
  4265. ASCII space or control characters, and includes parentheses
  4266. only if (a) they are backslash-escaped or (b) they are part of
  4267. a balanced pair of unescaped parentheses that is not itself
  4268. inside a balanced pair of unescaped paretheses.
  4269. A [link title](@link-title) consists of either
  4270. - a sequence of zero or more characters between straight double-quote
  4271. characters (`"`), including a `"` character only if it is
  4272. backslash-escaped, or
  4273. - a sequence of zero or more characters between straight single-quote
  4274. characters (`'`), including a `'` character only if it is
  4275. backslash-escaped, or
  4276. - a sequence of zero or more characters between matching parentheses
  4277. (`(...)`), including a `)` character only if it is backslash-escaped.
  4278. An [inline link](@inline-link)
  4279. consists of a [link text](#link-text) followed immediately
  4280. by a left parenthesis `(`, optional whitespace,
  4281. an optional [link destination](#link-destination),
  4282. an optional [link title](#link-title) separated from the link
  4283. destination by whitespace, optional whitespace, and a right
  4284. parenthesis `)`. The link's text consists of the inlines contained
  4285. in the [link text](#link-text) (excluding the enclosing square brackets).
  4286. The link's URI consists of the link destination, excluding enclosing
  4287. `<...>` if present, with backslash-escapes in effect as described
  4288. above. The link's title consists of the link title, excluding its
  4289. enclosing delimiters, with backslash-escapes in effect as described
  4290. above.
  4291. Here is a simple inline link:
  4292. .
  4293. [link](/uri "title")
  4294. .
  4295. <p><a href="/uri" title="title">link</a></p>
  4296. .
  4297. The title may be omitted:
  4298. .
  4299. [link](/uri)
  4300. .
  4301. <p><a href="/uri">link</a></p>
  4302. .
  4303. Both the title and the destination may be omitted:
  4304. .
  4305. [link]()
  4306. .
  4307. <p><a href="">link</a></p>
  4308. .
  4309. .
  4310. [link](<>)
  4311. .
  4312. <p><a href="">link</a></p>
  4313. .
  4314. If the destination contains spaces, it must be enclosed in pointy
  4315. braces:
  4316. .
  4317. [link](/my uri)
  4318. .
  4319. <p>[link](/my uri)</p>
  4320. .
  4321. .
  4322. [link](</my uri>)
  4323. .
  4324. <p><a href="/my%20uri">link</a></p>
  4325. .
  4326. The destination cannot contain line breaks, even with pointy braces:
  4327. .
  4328. [link](foo
  4329. bar)
  4330. .
  4331. <p>[link](foo
  4332. bar)</p>
  4333. .
  4334. One level of balanced parentheses is allowed without escaping:
  4335. .
  4336. [link]((foo)and(bar))
  4337. .
  4338. <p><a href="(foo)and(bar)">link</a></p>
  4339. .
  4340. However, if you have parentheses within parentheses, you need to escape
  4341. or use the `<...>` form:
  4342. .
  4343. [link](foo(and(bar)))
  4344. .
  4345. <p>[link](foo(and(bar)))</p>
  4346. .
  4347. .
  4348. [link](foo(and\(bar\)))
  4349. .
  4350. <p><a href="foo(and(bar))">link</a></p>
  4351. .
  4352. .
  4353. [link](<foo(and(bar))>)
  4354. .
  4355. <p><a href="foo(and(bar))">link</a></p>
  4356. .
  4357. Parentheses and other symbols can also be escaped, as usual
  4358. in Markdown:
  4359. .
  4360. [link](foo\)\:)
  4361. .
  4362. <p><a href="foo):">link</a></p>
  4363. .
  4364. URL-escaping should be left alone inside the destination, as all
  4365. URL-escaped characters are also valid URL characters. HTML entities in
  4366. the destination will be parsed into their UTF-8 codepoints, as usual, and
  4367. optionally URL-escaped when written as HTML.
  4368. .
  4369. [link](foo%20b&auml;)
  4370. .
  4371. <p><a href="foo%20b%C3%A4">link</a></p>
  4372. .
  4373. Note that, because titles can often be parsed as destinations,
  4374. if you try to omit the destination and keep the title, you'll
  4375. get unexpected results:
  4376. .
  4377. [link]("title")
  4378. .
  4379. <p><a href="%22title%22">link</a></p>
  4380. .
  4381. Titles may be in single quotes, double quotes, or parentheses:
  4382. .
  4383. [link](/url "title")
  4384. [link](/url 'title')
  4385. [link](/url (title))
  4386. .
  4387. <p><a href="/url" title="title">link</a>
  4388. <a href="/url" title="title">link</a>
  4389. <a href="/url" title="title">link</a></p>
  4390. .
  4391. Backslash escapes and entities may be used in titles:
  4392. .
  4393. [link](/url "title \"&quot;")
  4394. .
  4395. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  4396. .
  4397. Nested balanced quotes are not allowed without escaping:
  4398. .
  4399. [link](/url "title "and" title")
  4400. .
  4401. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  4402. .
  4403. But it is easy to work around this by using a different quote type:
  4404. .
  4405. [link](/url 'title "and" title')
  4406. .
  4407. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  4408. .
  4409. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  4410. title, and its test suite included a test demonstrating this.
  4411. But it is hard to see a good rationale for the extra complexity this
  4412. brings, since there are already many ways---backslash escaping,
  4413. entities, or using a different quote type for the enclosing title---to
  4414. write titles containing double quotes. `Markdown.pl`'s handling of
  4415. titles has a number of other strange features. For example, it allows
  4416. single-quoted titles in inline links, but not reference links. And, in
  4417. reference links but not inline links, it allows a title to begin with
  4418. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  4419. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  4420. a simple, rational rule that works the same way in inline links and
  4421. link reference definitions.)
  4422. Whitespace is allowed around the destination and title:
  4423. .
  4424. [link]( /uri
  4425. "title" )
  4426. .
  4427. <p><a href="/uri" title="title">link</a></p>
  4428. .
  4429. But it is not allowed between the link text and the
  4430. following parenthesis:
  4431. .
  4432. [link] (/uri)
  4433. .
  4434. <p>[link] (/uri)</p>
  4435. .
  4436. The link text may contain balanced brackets, but not unbalanced ones,
  4437. unless they are escaped:
  4438. .
  4439. [link [foo [bar]]](/uri)
  4440. .
  4441. <p><a href="/uri">link [foo [bar]]</a></p>
  4442. .
  4443. .
  4444. [link] bar](/uri)
  4445. .
  4446. <p>[link] bar](/uri)</p>
  4447. .
  4448. .
  4449. [link [bar](/uri)
  4450. .
  4451. <p>[link <a href="/uri">bar</a></p>
  4452. .
  4453. .
  4454. [link \[bar](/uri)
  4455. .
  4456. <p><a href="/uri">link [bar</a></p>
  4457. .
  4458. The link text may contain inline content:
  4459. .
  4460. [link *foo **bar** `#`*](/uri)
  4461. .
  4462. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4463. .
  4464. .
  4465. [![moon](moon.jpg)](/uri)
  4466. .
  4467. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4468. .
  4469. However, links may not contain other links, at any level of nesting.
  4470. .
  4471. [foo [bar](/uri)](/uri)
  4472. .
  4473. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  4474. .
  4475. .
  4476. [foo *[bar [baz](/uri)](/uri)*](/uri)
  4477. .
  4478. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  4479. .
  4480. These cases illustrate the precedence of link text grouping over
  4481. emphasis grouping:
  4482. .
  4483. *[foo*](/uri)
  4484. .
  4485. <p>*<a href="/uri">foo*</a></p>
  4486. .
  4487. .
  4488. [foo *bar](baz*)
  4489. .
  4490. <p><a href="baz*">foo *bar</a></p>
  4491. .
  4492. These cases illustrate the precedence of HTML tags, code spans,
  4493. and autolinks over link grouping:
  4494. .
  4495. [foo <bar attr="](baz)">
  4496. .
  4497. <p>[foo <bar attr="](baz)"></p>
  4498. .
  4499. .
  4500. [foo`](/uri)`
  4501. .
  4502. <p>[foo<code>](/uri)</code></p>
  4503. .
  4504. .
  4505. [foo<http://example.com?search=](uri)>
  4506. .
  4507. <p>[foo<a href="http://example.com?search=%5D(uri)">http://example.com?search=](uri)</a></p>
  4508. .
  4509. There are three kinds of [reference links](@reference-link):
  4510. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  4511. and [shortcut](#shortcut-reference-link).
  4512. A [full reference link](@full-reference-link)
  4513. consists of a [link text](#link-text), optional whitespace, and
  4514. a [link label](#link-label) that [matches](#matches) a
  4515. [link reference definition](#link-reference-definition) elsewhere in the
  4516. document.
  4517. A [link label](@link-label) begins with a left bracket (`[`) and ends
  4518. with the first right bracket (`]`) that is not backslash-escaped.
  4519. Unescaped square bracket characters are not allowed in
  4520. [link labels](#link-label). A link label can have at most 999
  4521. characters inside the square brackets.
  4522. One label [matches](@matches)
  4523. another just in case their normalized forms are equal. To normalize a
  4524. label, perform the *unicode case fold* and collapse consecutive internal
  4525. whitespace to a single space. If there are multiple matching reference
  4526. link definitions, the one that comes first in the document is used. (It
  4527. is desirable in such cases to emit a warning.)
  4528. The contents of the first link label are parsed as inlines, which are
  4529. used as the link's text. The link's URI and title are provided by the
  4530. matching [link reference definition](#link-reference-definition).
  4531. Here is a simple example:
  4532. .
  4533. [foo][bar]
  4534. [bar]: /url "title"
  4535. .
  4536. <p><a href="/url" title="title">foo</a></p>
  4537. .
  4538. The rules for the [link text](#link-text) are the same as with
  4539. [inline links](#inline-link). Thus:
  4540. The link text may contain balanced brackets, but not unbalanced ones,
  4541. unless they are escaped:
  4542. .
  4543. [link [foo [bar]]][ref]
  4544. [ref]: /uri
  4545. .
  4546. <p><a href="/uri">link [foo [bar]]</a></p>
  4547. .
  4548. .
  4549. [link \[bar][ref]
  4550. [ref]: /uri
  4551. .
  4552. <p><a href="/uri">link [bar</a></p>
  4553. .
  4554. The link text may contain inline content:
  4555. .
  4556. [link *foo **bar** `#`*][ref]
  4557. [ref]: /uri
  4558. .
  4559. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4560. .
  4561. .
  4562. [![moon](moon.jpg)][ref]
  4563. [ref]: /uri
  4564. .
  4565. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4566. .
  4567. However, links may not contain other links, at any level of nesting.
  4568. .
  4569. [foo [bar](/uri)][ref]
  4570. [ref]: /uri
  4571. .
  4572. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  4573. .
  4574. .
  4575. [foo *bar [baz][ref]*][ref]
  4576. [ref]: /uri
  4577. .
  4578. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  4579. .
  4580. (In the examples above, we have two [shortcut reference
  4581. links](#shortcut-reference-link) instead of one [full reference
  4582. link](#full-reference-link).)
  4583. The following cases illustrate the precedence of link text grouping over
  4584. emphasis grouping:
  4585. .
  4586. *[foo*][ref]
  4587. [ref]: /uri
  4588. .
  4589. <p>*<a href="/uri">foo*</a></p>
  4590. .
  4591. .
  4592. [foo *bar][ref]
  4593. [ref]: /uri
  4594. .
  4595. <p><a href="/uri">foo *bar</a></p>
  4596. .
  4597. These cases illustrate the precedence of HTML tags, code spans,
  4598. and autolinks over link grouping:
  4599. .
  4600. [foo <bar attr="][ref]">
  4601. [ref]: /uri
  4602. .
  4603. <p>[foo <bar attr="][ref]"></p>
  4604. .
  4605. .
  4606. [foo`][ref]`
  4607. [ref]: /uri
  4608. .
  4609. <p>[foo<code>][ref]</code></p>
  4610. .
  4611. .
  4612. [foo<http://example.com?search=][ref]>
  4613. [ref]: /uri
  4614. .
  4615. <p>[foo<a href="http://example.com?search=%5D%5Bref%5D">http://example.com?search=][ref]</a></p>
  4616. .
  4617. Matching is case-insensitive:
  4618. .
  4619. [foo][BaR]
  4620. [bar]: /url "title"
  4621. .
  4622. <p><a href="/url" title="title">foo</a></p>
  4623. .
  4624. Unicode case fold is used:
  4625. .
  4626. [Толпой][Толпой] is a Russian word.
  4627. [ТОЛПОЙ]: /url
  4628. .
  4629. <p><a href="/url">Толпой</a> is a Russian word.</p>
  4630. .
  4631. Consecutive internal whitespace is treated as one space for
  4632. purposes of determining matching:
  4633. .
  4634. [Foo
  4635. bar]: /url
  4636. [Baz][Foo bar]
  4637. .
  4638. <p><a href="/url">Baz</a></p>
  4639. .
  4640. There can be whitespace between the [link text](#link-text) and the
  4641. [link label](#link-label):
  4642. .
  4643. [foo] [bar]
  4644. [bar]: /url "title"
  4645. .
  4646. <p><a href="/url" title="title">foo</a></p>
  4647. .
  4648. .
  4649. [foo]
  4650. [bar]
  4651. [bar]: /url "title"
  4652. .
  4653. <p><a href="/url" title="title">foo</a></p>
  4654. .
  4655. When there are multiple matching [link reference
  4656. definitions](#link-reference-definition), the first is used:
  4657. .
  4658. [foo]: /url1
  4659. [foo]: /url2
  4660. [bar][foo]
  4661. .
  4662. <p><a href="/url1">bar</a></p>
  4663. .
  4664. Note that matching is performed on normalized strings, not parsed
  4665. inline content. So the following does not match, even though the
  4666. labels define equivalent inline content:
  4667. .
  4668. [bar][foo\!]
  4669. [foo!]: /url
  4670. .
  4671. <p>[bar][foo!]</p>
  4672. .
  4673. [Link labels](#link-label) cannot contain brackets, unless they are
  4674. backslash-escaped:
  4675. .
  4676. [foo][ref[]
  4677. [ref[]: /uri
  4678. .
  4679. <p>[foo][ref[]</p>
  4680. <p>[ref[]: /uri</p>
  4681. .
  4682. .
  4683. [foo][ref[bar]]
  4684. [ref[bar]]: /uri
  4685. .
  4686. <p>[foo][ref[bar]]</p>
  4687. <p>[ref[bar]]: /uri</p>
  4688. .
  4689. .
  4690. [[[foo]]]
  4691. [[[foo]]]: /url
  4692. .
  4693. <p>[[[foo]]]</p>
  4694. <p>[[[foo]]]: /url</p>
  4695. .
  4696. .
  4697. [foo][ref\[]
  4698. [ref\[]: /uri
  4699. .
  4700. <p><a href="/uri">foo</a></p>
  4701. .
  4702. A [collapsed reference link](@collapsed-reference-link)
  4703. consists of a [link
  4704. label](#link-label) that [matches](#matches) a [link reference
  4705. definition](#link-reference-definition) elsewhere in the
  4706. document, optional whitespace, and the string `[]`. The contents of the
  4707. first link label are parsed as inlines, which are used as the link's
  4708. text. The link's URI and title are provided by the matching reference
  4709. link definition. Thus, `[foo][]` is equivalent to `[foo][foo]`.
  4710. .
  4711. [foo][]
  4712. [foo]: /url "title"
  4713. .
  4714. <p><a href="/url" title="title">foo</a></p>
  4715. .
  4716. .
  4717. [*foo* bar][]
  4718. [*foo* bar]: /url "title"
  4719. .
  4720. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4721. .
  4722. The link labels are case-insensitive:
  4723. .
  4724. [Foo][]
  4725. [foo]: /url "title"
  4726. .
  4727. <p><a href="/url" title="title">Foo</a></p>
  4728. .
  4729. As with full reference links, whitespace is allowed
  4730. between the two sets of brackets:
  4731. .
  4732. [foo]
  4733. []
  4734. [foo]: /url "title"
  4735. .
  4736. <p><a href="/url" title="title">foo</a></p>
  4737. .
  4738. A [shortcut reference link](@shortcut-reference-link)
  4739. consists of a [link
  4740. label](#link-label) that [matches](#matches) a [link reference
  4741. definition](#link-reference-definition) elsewhere in the
  4742. document and is not followed by `[]` or a link label.
  4743. The contents of the first link label are parsed as inlines,
  4744. which are used as the link's text. the link's URI and title
  4745. are provided by the matching link reference definition.
  4746. Thus, `[foo]` is equivalent to `[foo][]`.
  4747. .
  4748. [foo]
  4749. [foo]: /url "title"
  4750. .
  4751. <p><a href="/url" title="title">foo</a></p>
  4752. .
  4753. .
  4754. [*foo* bar]
  4755. [*foo* bar]: /url "title"
  4756. .
  4757. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4758. .
  4759. .
  4760. [[*foo* bar]]
  4761. [*foo* bar]: /url "title"
  4762. .
  4763. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  4764. .
  4765. The link labels are case-insensitive:
  4766. .
  4767. [Foo]
  4768. [foo]: /url "title"
  4769. .
  4770. <p><a href="/url" title="title">Foo</a></p>
  4771. .
  4772. A space after the link text should be preserved:
  4773. .
  4774. [foo] bar
  4775. [foo]: /url
  4776. .
  4777. <p><a href="/url">foo</a> bar</p>
  4778. .
  4779. If you just want bracketed text, you can backslash-escape the
  4780. opening bracket to avoid links:
  4781. .
  4782. \[foo]
  4783. [foo]: /url "title"
  4784. .
  4785. <p>[foo]</p>
  4786. .
  4787. Note that this is a link, because a link label ends with the first
  4788. following closing bracket:
  4789. .
  4790. [foo*]: /url
  4791. *[foo*]
  4792. .
  4793. <p>*<a href="/url">foo*</a></p>
  4794. .
  4795. This is a link too, for the same reason:
  4796. .
  4797. [foo`]: /url
  4798. [foo`]`
  4799. .
  4800. <p>[foo<code>]</code></p>
  4801. .
  4802. Full references take precedence over shortcut references:
  4803. .
  4804. [foo][bar]
  4805. [foo]: /url1
  4806. [bar]: /url2
  4807. .
  4808. <p><a href="/url2">foo</a></p>
  4809. .
  4810. In the following case `[bar][baz]` is parsed as a reference,
  4811. `[foo]` as normal text:
  4812. .
  4813. [foo][bar][baz]
  4814. [baz]: /url
  4815. .
  4816. <p>[foo]<a href="/url">bar</a></p>
  4817. .
  4818. Here, though, `[foo][bar]` is parsed as a reference, since
  4819. `[bar]` is defined:
  4820. .
  4821. [foo][bar][baz]
  4822. [baz]: /url1
  4823. [bar]: /url2
  4824. .
  4825. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  4826. .
  4827. Here `[foo]` is not parsed as a shortcut reference, because it
  4828. is followed by a link label (even though `[bar]` is not defined):
  4829. .
  4830. [foo][bar][baz]
  4831. [baz]: /url1
  4832. [foo]: /url2
  4833. .
  4834. <p>[foo]<a href="/url1">bar</a></p>
  4835. .
  4836. ## Images
  4837. Syntax for images is like the syntax for links, with one
  4838. difference. Instead of [link text](#link-text), we have an [image
  4839. description](@image-description). The rules for this are the
  4840. same as for [link text](#link-text), except that (a) an
  4841. image description starts with `![` rather than `[`, and
  4842. (b) an image description may contain links.
  4843. An image description has inline elements
  4844. as its contents. When an image is rendered to HTML,
  4845. this is standardly used as the image's `alt` attribute.
  4846. .
  4847. ![foo](/url "title")
  4848. .
  4849. <p><img src="/url" alt="foo" title="title" /></p>
  4850. .
  4851. .
  4852. ![foo *bar*]
  4853. [foo *bar*]: train.jpg "train & tracks"
  4854. .
  4855. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4856. .
  4857. .
  4858. ![foo ![bar](/url)](/url2)
  4859. .
  4860. <p><img src="/url2" alt="foo bar" /></p>
  4861. .
  4862. .
  4863. ![foo [bar](/url)](/url2)
  4864. .
  4865. <p><img src="/url2" alt="foo bar" /></p>
  4866. .
  4867. Though this spec is concerned with parsing, not rendering, it is
  4868. recommended that in rendering to HTML, only the plain string content
  4869. of the [image description](#image-description) be used. Note that in
  4870. the above example, the alt attribute's value is `foo bar`, not `foo
  4871. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  4872. content is rendered, without formatting.
  4873. .
  4874. ![foo *bar*][]
  4875. [foo *bar*]: train.jpg "train & tracks"
  4876. .
  4877. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4878. .
  4879. .
  4880. ![foo *bar*][foobar]
  4881. [FOOBAR]: train.jpg "train & tracks"
  4882. .
  4883. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4884. .
  4885. .
  4886. ![foo](train.jpg)
  4887. .
  4888. <p><img src="train.jpg" alt="foo" /></p>
  4889. .
  4890. .
  4891. My ![foo bar](/path/to/train.jpg "title" )
  4892. .
  4893. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  4894. .
  4895. .
  4896. ![foo](<url>)
  4897. .
  4898. <p><img src="url" alt="foo" /></p>
  4899. .
  4900. .
  4901. ![](/url)
  4902. .
  4903. <p><img src="/url" alt="" /></p>
  4904. .
  4905. Reference-style:
  4906. .
  4907. ![foo] [bar]
  4908. [bar]: /url
  4909. .
  4910. <p><img src="/url" alt="foo" /></p>
  4911. .
  4912. .
  4913. ![foo] [bar]
  4914. [BAR]: /url
  4915. .
  4916. <p><img src="/url" alt="foo" /></p>
  4917. .
  4918. Collapsed:
  4919. .
  4920. ![foo][]
  4921. [foo]: /url "title"
  4922. .
  4923. <p><img src="/url" alt="foo" title="title" /></p>
  4924. .
  4925. .
  4926. ![*foo* bar][]
  4927. [*foo* bar]: /url "title"
  4928. .
  4929. <p><img src="/url" alt="foo bar" title="title" /></p>
  4930. .
  4931. The labels are case-insensitive:
  4932. .
  4933. ![Foo][]
  4934. [foo]: /url "title"
  4935. .
  4936. <p><img src="/url" alt="Foo" title="title" /></p>
  4937. .
  4938. As with full reference links, whitespace is allowed
  4939. between the two sets of brackets:
  4940. .
  4941. ![foo]
  4942. []
  4943. [foo]: /url "title"
  4944. .
  4945. <p><img src="/url" alt="foo" title="title" /></p>
  4946. .
  4947. Shortcut:
  4948. .
  4949. ![foo]
  4950. [foo]: /url "title"
  4951. .
  4952. <p><img src="/url" alt="foo" title="title" /></p>
  4953. .
  4954. .
  4955. ![*foo* bar]
  4956. [*foo* bar]: /url "title"
  4957. .
  4958. <p><img src="/url" alt="foo bar" title="title" /></p>
  4959. .
  4960. Note that link labels cannot contain unescaped brackets:
  4961. .
  4962. ![[foo]]
  4963. [[foo]]: /url "title"
  4964. .
  4965. <p>![[foo]]</p>
  4966. <p>[[foo]]: /url &quot;title&quot;</p>
  4967. .
  4968. The link labels are case-insensitive:
  4969. .
  4970. ![Foo]
  4971. [foo]: /url "title"
  4972. .
  4973. <p><img src="/url" alt="Foo" title="title" /></p>
  4974. .
  4975. If you just want bracketed text, you can backslash-escape the
  4976. opening `!` and `[`:
  4977. .
  4978. \!\[foo]
  4979. [foo]: /url "title"
  4980. .
  4981. <p>![foo]</p>
  4982. .
  4983. If you want a link after a literal `!`, backslash-escape the
  4984. `!`:
  4985. .
  4986. \![foo]
  4987. [foo]: /url "title"
  4988. .
  4989. <p>!<a href="/url" title="title">foo</a></p>
  4990. .
  4991. ## Autolinks
  4992. [Autolinks](@autolink) are absolute URIs and email addresses inside `<` and `>`.
  4993. They are parsed as links, with the URL or email address as the link
  4994. label.
  4995. A [URI autolink](@uri-autolink)
  4996. consists of `<`, followed by an [absolute
  4997. URI](#absolute-uri) not containing `<`, followed by `>`. It is parsed
  4998. as a link to the URI, with the URI as the link's label.
  4999. An [absolute URI](@absolute-uri),
  5000. for these purposes, consists of a [scheme](#scheme) followed by a colon (`:`)
  5001. followed by zero or more characters other than ASCII whitespace and
  5002. control characters, `<`, and `>`. If the URI includes these characters,
  5003. you must use percent-encoding (e.g. `%20` for a space).
  5004. The following [schemes](@scheme)
  5005. are recognized (case-insensitive):
  5006. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  5007. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  5008. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  5009. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  5010. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  5011. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  5012. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  5013. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5014. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5015. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5016. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5017. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5018. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5019. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5020. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5021. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5022. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5023. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5024. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5025. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5026. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5027. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5028. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5029. Here are some valid autolinks:
  5030. .
  5031. <http://foo.bar.baz>
  5032. .
  5033. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5034. .
  5035. .
  5036. <http://foo.bar.baz?q=hello&id=22&boolean>
  5037. .
  5038. <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>
  5039. .
  5040. .
  5041. <irc://foo.bar:2233/baz>
  5042. .
  5043. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5044. .
  5045. Uppercase is also fine:
  5046. .
  5047. <MAILTO:FOO@BAR.BAZ>
  5048. .
  5049. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5050. .
  5051. Spaces are not allowed in autolinks:
  5052. .
  5053. <http://foo.bar/baz bim>
  5054. .
  5055. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5056. .
  5057. An [email autolink](@email-autolink)
  5058. consists of `<`, followed by an [email address](#email-address),
  5059. followed by `>`. The link's label is the email address,
  5060. and the URL is `mailto:` followed by the email address.
  5061. An [email address](@email-address),
  5062. for these purposes, is anything that matches
  5063. the [non-normative regex from the HTML5
  5064. spec](http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#e-mail-state-%28type=email%29):
  5065. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5066. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5067. Examples of email autolinks:
  5068. .
  5069. <foo@bar.example.com>
  5070. .
  5071. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5072. .
  5073. .
  5074. <foo+special@Bar.baz-bar0.com>
  5075. .
  5076. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5077. .
  5078. These are not autolinks:
  5079. .
  5080. <>
  5081. .
  5082. <p>&lt;&gt;</p>
  5083. .
  5084. .
  5085. <heck://bing.bong>
  5086. .
  5087. <p>&lt;heck://bing.bong&gt;</p>
  5088. .
  5089. .
  5090. < http://foo.bar >
  5091. .
  5092. <p>&lt; http://foo.bar &gt;</p>
  5093. .
  5094. .
  5095. <foo.bar.baz>
  5096. .
  5097. <p>&lt;foo.bar.baz&gt;</p>
  5098. .
  5099. .
  5100. <localhost:5001/foo>
  5101. .
  5102. <p>&lt;localhost:5001/foo&gt;</p>
  5103. .
  5104. .
  5105. http://example.com
  5106. .
  5107. <p>http://example.com</p>
  5108. .
  5109. .
  5110. foo@bar.example.com
  5111. .
  5112. <p>foo@bar.example.com</p>
  5113. .
  5114. ## Raw HTML
  5115. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5116. raw HTML tag and will be rendered in HTML without escaping.
  5117. Tag and attribute names are not limited to current HTML tags,
  5118. so custom tags (and even, say, DocBook tags) may be used.
  5119. Here is the grammar for tags:
  5120. A [tag name](@tag-name) consists of an ASCII letter
  5121. followed by zero or more ASCII letters or digits.
  5122. An [attribute](@attribute) consists of whitespace,
  5123. an [attribute name](#attribute-name), and an optional
  5124. [attribute value specification](#attribute-value-specification).
  5125. An [attribute name](@attribute-name)
  5126. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5127. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5128. specification restricted to ASCII. HTML5 is laxer.)
  5129. An [attribute value specification](@attribute-value-specification)
  5130. consists of optional whitespace,
  5131. a `=` character, optional whitespace, and an [attribute
  5132. value](#attribute-value).
  5133. An [attribute value](@attribute-value)
  5134. consists of an [unquoted attribute value](#unquoted-attribute-value),
  5135. a [single-quoted attribute value](#single-quoted-attribute-value),
  5136. or a [double-quoted attribute value](#double-quoted-attribute-value).
  5137. An [unquoted attribute value](@unquoted-attribute-value)
  5138. is a nonempty string of characters not
  5139. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5140. A [single-quoted attribute value](@single-quoted-attribute-value)
  5141. consists of `'`, zero or more
  5142. characters not including `'`, and a final `'`.
  5143. A [double-quoted attribute value](@double-quoted-attribute-value)
  5144. consists of `"`, zero or more
  5145. characters not including `"`, and a final `"`.
  5146. An [open tag](@open-tag) consists of a `<` character,
  5147. a [tag name](#tag-name), zero or more [attributes](#attribute),
  5148. optional whitespace, an optional `/` character, and a `>` character.
  5149. A [closing tag](@closing-tag) consists of the
  5150. string `</`, a [tag name](#tag-name), optional whitespace, and the
  5151. character `>`.
  5152. An [HTML comment](@html-comment) consists of the
  5153. string `<!--`, a string of characters not including the string `--`, and
  5154. the string `-->`.
  5155. A [processing instruction](@processing-instruction)
  5156. consists of the string `<?`, a string
  5157. of characters not including the string `?>`, and the string
  5158. `?>`.
  5159. A [declaration](@declaration) consists of the
  5160. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5161. whitespace, a string of characters not including the character `>`, and
  5162. the character `>`.
  5163. A [CDATA section](@cdata-section) consists of
  5164. the string `<![CDATA[`, a string of characters not including the string
  5165. `]]>`, and the string `]]>`.
  5166. An [HTML tag](@html-tag) consists of an [open
  5167. tag](#open-tag), a [closing tag](#closing-tag), an [HTML
  5168. comment](#html-comment), a [processing
  5169. instruction](#processing-instruction), an [element type
  5170. declaration](#element-type-declaration), or a [CDATA
  5171. section](#cdata-section).
  5172. Here are some simple open tags:
  5173. .
  5174. <a><bab><c2c>
  5175. .
  5176. <p><a><bab><c2c></p>
  5177. .
  5178. Empty elements:
  5179. .
  5180. <a/><b2/>
  5181. .
  5182. <p><a/><b2/></p>
  5183. .
  5184. Whitespace is allowed:
  5185. .
  5186. <a /><b2
  5187. data="foo" >
  5188. .
  5189. <p><a /><b2
  5190. data="foo" ></p>
  5191. .
  5192. With attributes:
  5193. .
  5194. <a foo="bar" bam = 'baz <em>"</em>'
  5195. _boolean zoop:33=zoop:33 />
  5196. .
  5197. <p><a foo="bar" bam = 'baz <em>"</em>'
  5198. _boolean zoop:33=zoop:33 /></p>
  5199. .
  5200. Illegal tag names, not parsed as HTML:
  5201. .
  5202. <33> <__>
  5203. .
  5204. <p>&lt;33&gt; &lt;__&gt;</p>
  5205. .
  5206. Illegal attribute names:
  5207. .
  5208. <a h*#ref="hi">
  5209. .
  5210. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5211. .
  5212. Illegal attribute values:
  5213. .
  5214. <a href="hi'> <a href=hi'>
  5215. .
  5216. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5217. .
  5218. Illegal whitespace:
  5219. .
  5220. < a><
  5221. foo><bar/ >
  5222. .
  5223. <p>&lt; a&gt;&lt;
  5224. foo&gt;&lt;bar/ &gt;</p>
  5225. .
  5226. Missing whitespace:
  5227. .
  5228. <a href='bar'title=title>
  5229. .
  5230. <p>&lt;a href='bar'title=title&gt;</p>
  5231. .
  5232. Closing tags:
  5233. .
  5234. </a>
  5235. </foo >
  5236. .
  5237. <p></a>
  5238. </foo ></p>
  5239. .
  5240. Illegal attributes in closing tag:
  5241. .
  5242. </a href="foo">
  5243. .
  5244. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5245. .
  5246. Comments:
  5247. .
  5248. foo <!-- this is a
  5249. comment - with hyphen -->
  5250. .
  5251. <p>foo <!-- this is a
  5252. comment - with hyphen --></p>
  5253. .
  5254. .
  5255. foo <!-- not a comment -- two hyphens -->
  5256. .
  5257. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5258. .
  5259. Processing instructions:
  5260. .
  5261. foo <?php echo $a; ?>
  5262. .
  5263. <p>foo <?php echo $a; ?></p>
  5264. .
  5265. Declarations:
  5266. .
  5267. foo <!ELEMENT br EMPTY>
  5268. .
  5269. <p>foo <!ELEMENT br EMPTY></p>
  5270. .
  5271. CDATA sections:
  5272. .
  5273. foo <![CDATA[>&<]]>
  5274. .
  5275. <p>foo <![CDATA[>&<]]></p>
  5276. .
  5277. Entities are preserved in HTML attributes:
  5278. .
  5279. <a href="&ouml;">
  5280. .
  5281. <p><a href="&ouml;"></p>
  5282. .
  5283. Backslash escapes do not work in HTML attributes:
  5284. .
  5285. <a href="\*">
  5286. .
  5287. <p><a href="\*"></p>
  5288. .
  5289. .
  5290. <a href="\"">
  5291. .
  5292. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5293. .
  5294. ## Hard line breaks
  5295. A line break (not in a code span or HTML tag) that is preceded
  5296. by two or more spaces and does not occur at the end of a block
  5297. is parsed as a [hard line break](@hard-line-break) (rendered
  5298. in HTML as a `<br />` tag):
  5299. .
  5300. foo
  5301. baz
  5302. .
  5303. <p>foo<br />
  5304. baz</p>
  5305. .
  5306. For a more visible alternative, a backslash before the newline may be
  5307. used instead of two spaces:
  5308. .
  5309. foo\
  5310. baz
  5311. .
  5312. <p>foo<br />
  5313. baz</p>
  5314. .
  5315. More than two spaces can be used:
  5316. .
  5317. foo
  5318. baz
  5319. .
  5320. <p>foo<br />
  5321. baz</p>
  5322. .
  5323. Leading spaces at the beginning of the next line are ignored:
  5324. .
  5325. foo
  5326. bar
  5327. .
  5328. <p>foo<br />
  5329. bar</p>
  5330. .
  5331. .
  5332. foo\
  5333. bar
  5334. .
  5335. <p>foo<br />
  5336. bar</p>
  5337. .
  5338. Line breaks can occur inside emphasis, links, and other constructs
  5339. that allow inline content:
  5340. .
  5341. *foo
  5342. bar*
  5343. .
  5344. <p><em>foo<br />
  5345. bar</em></p>
  5346. .
  5347. .
  5348. *foo\
  5349. bar*
  5350. .
  5351. <p><em>foo<br />
  5352. bar</em></p>
  5353. .
  5354. Line breaks do not occur inside code spans
  5355. .
  5356. `code
  5357. span`
  5358. .
  5359. <p><code>code span</code></p>
  5360. .
  5361. .
  5362. `code\
  5363. span`
  5364. .
  5365. <p><code>code\ span</code></p>
  5366. .
  5367. or HTML tags:
  5368. .
  5369. <a href="foo
  5370. bar">
  5371. .
  5372. <p><a href="foo
  5373. bar"></p>
  5374. .
  5375. .
  5376. <a href="foo\
  5377. bar">
  5378. .
  5379. <p><a href="foo\
  5380. bar"></p>
  5381. .
  5382. Hard line breaks are for separating inline content within a block.
  5383. Neither syntax for hard line breaks works at the end of a paragraph or
  5384. other block element:
  5385. .
  5386. foo\
  5387. .
  5388. <p>foo\</p>
  5389. .
  5390. .
  5391. foo
  5392. .
  5393. <p>foo</p>
  5394. .
  5395. .
  5396. ### foo\
  5397. .
  5398. <h3>foo\</h3>
  5399. .
  5400. .
  5401. ### foo
  5402. .
  5403. <h3>foo</h3>
  5404. .
  5405. ## Soft line breaks
  5406. A regular line break (not in a code span or HTML tag) that is not
  5407. preceded by two or more spaces is parsed as a softbreak. (A
  5408. softbreak may be rendered in HTML either as a newline or as a space.
  5409. The result will be the same in browsers. In the examples here, a
  5410. newline will be used.)
  5411. .
  5412. foo
  5413. baz
  5414. .
  5415. <p>foo
  5416. baz</p>
  5417. .
  5418. Spaces at the end of the line and beginning of the next line are
  5419. removed:
  5420. .
  5421. foo
  5422. baz
  5423. .
  5424. <p>foo
  5425. baz</p>
  5426. .
  5427. A conforming parser may render a soft line break in HTML either as a
  5428. line break or as a space.
  5429. A renderer may also provide an option to render soft line breaks
  5430. as hard line breaks.
  5431. ## Textual content
  5432. Any characters not given an interpretation by the above rules will
  5433. be parsed as plain textual content.
  5434. .
  5435. hello $.;'there
  5436. .
  5437. <p>hello $.;'there</p>
  5438. .
  5439. .
  5440. Foo χρῆν
  5441. .
  5442. <p>Foo χρῆν</p>
  5443. .
  5444. Internal spaces are preserved verbatim:
  5445. .
  5446. Multiple spaces
  5447. .
  5448. <p>Multiple spaces</p>
  5449. .
  5450. <!-- END TESTS -->
  5451. # Appendix A: A parsing strategy {-}
  5452. ## Overview {-}
  5453. Parsing has two phases:
  5454. 1. In the first phase, lines of input are consumed and the block
  5455. structure of the document---its division into paragraphs, block quotes,
  5456. list items, and so on---is constructed. Text is assigned to these
  5457. blocks but not parsed. Link reference definitions are parsed and a
  5458. map of links is constructed.
  5459. 2. In the second phase, the raw text contents of paragraphs and headers
  5460. are parsed into sequences of Markdown inline elements (strings,
  5461. code spans, links, emphasis, and so on), using the map of link
  5462. references constructed in phase 1.
  5463. ## The document tree {-}
  5464. At each point in processing, the document is represented as a tree of
  5465. **blocks**. The root of the tree is a `document` block. The `document`
  5466. may have any number of other blocks as **children**. These children
  5467. may, in turn, have other blocks as children. The last child of a block
  5468. is normally considered **open**, meaning that subsequent lines of input
  5469. can alter its contents. (Blocks that are not open are **closed**.)
  5470. Here, for example, is a possible document tree, with the open blocks
  5471. marked by arrows:
  5472. ``` tree
  5473. -> document
  5474. -> block_quote
  5475. paragraph
  5476. "Lorem ipsum dolor\nsit amet."
  5477. -> list (type=bullet tight=true bullet_char=-)
  5478. list_item
  5479. paragraph
  5480. "Qui *quodsi iracundia*"
  5481. -> list_item
  5482. -> paragraph
  5483. "aliquando id"
  5484. ```
  5485. ## How source lines alter the document tree {-}
  5486. Each line that is processed has an effect on this tree. The line is
  5487. analyzed and, depending on its contents, the document may be altered
  5488. in one or more of the following ways:
  5489. 1. One or more open blocks may be closed.
  5490. 2. One or more new blocks may be created as children of the
  5491. last open block.
  5492. 3. Text may be added to the last (deepest) open block remaining
  5493. on the tree.
  5494. Once a line has been incorporated into the tree in this way,
  5495. it can be discarded, so input can be read in a stream.
  5496. We can see how this works by considering how the tree above is
  5497. generated by four lines of Markdown:
  5498. ``` markdown
  5499. > Lorem ipsum dolor
  5500. sit amet.
  5501. > - Qui *quodsi iracundia*
  5502. > - aliquando id
  5503. ```
  5504. At the outset, our document model is just
  5505. ``` tree
  5506. -> document
  5507. ```
  5508. The first line of our text,
  5509. ``` markdown
  5510. > Lorem ipsum dolor
  5511. ```
  5512. causes a `block_quote` block to be created as a child of our
  5513. open `document` block, and a `paragraph` block as a child of
  5514. the `block_quote`. Then the text is added to the last open
  5515. block, the `paragraph`:
  5516. ``` tree
  5517. -> document
  5518. -> block_quote
  5519. -> paragraph
  5520. "Lorem ipsum dolor"
  5521. ```
  5522. The next line,
  5523. ``` markdown
  5524. sit amet.
  5525. ```
  5526. is a "lazy continuation" of the open `paragraph`, so it gets added
  5527. to the paragraph's text:
  5528. ``` tree
  5529. -> document
  5530. -> block_quote
  5531. -> paragraph
  5532. "Lorem ipsum dolor\nsit amet."
  5533. ```
  5534. The third line,
  5535. ``` markdown
  5536. > - Qui *quodsi iracundia*
  5537. ```
  5538. causes the `paragraph` block to be closed, and a new `list` block
  5539. opened as a child of the `block_quote`. A `list_item` is also
  5540. added as a child of the `list`, and a `paragraph` as a child of
  5541. the `list_item`. The text is then added to the new `paragraph`:
  5542. ``` tree
  5543. -> document
  5544. -> block_quote
  5545. paragraph
  5546. "Lorem ipsum dolor\nsit amet."
  5547. -> list (type=bullet tight=true bullet_char=-)
  5548. -> list_item
  5549. -> paragraph
  5550. "Qui *quodsi iracundia*"
  5551. ```
  5552. The fourth line,
  5553. ``` markdown
  5554. > - aliquando id
  5555. ```
  5556. causes the `list_item` (and its child the `paragraph`) to be closed,
  5557. and a new `list_item` opened up as child of the `list`. A `paragraph`
  5558. is added as a child of the new `list_item`, to contain the text.
  5559. We thus obtain the final tree:
  5560. ``` tree
  5561. -> document
  5562. -> block_quote
  5563. paragraph
  5564. "Lorem ipsum dolor\nsit amet."
  5565. -> list (type=bullet tight=true bullet_char=-)
  5566. list_item
  5567. paragraph
  5568. "Qui *quodsi iracundia*"
  5569. -> list_item
  5570. -> paragraph
  5571. "aliquando id"
  5572. ```
  5573. ## From block structure to the final document {-}
  5574. Once all of the input has been parsed, all open blocks are closed.
  5575. We then "walk the tree," visiting every node, and parse raw
  5576. string contents of paragraphs and headers as inlines. At this
  5577. point we have seen all the link reference definitions, so we can
  5578. resolve reference links as we go.
  5579. ``` tree
  5580. document
  5581. block_quote
  5582. paragraph
  5583. str "Lorem ipsum dolor"
  5584. softbreak
  5585. str "sit amet."
  5586. list (type=bullet tight=true bullet_char=-)
  5587. list_item
  5588. paragraph
  5589. str "Qui "
  5590. emph
  5591. str "quodsi iracundia"
  5592. list_item
  5593. paragraph
  5594. str "aliquando id"
  5595. ```
  5596. Notice how the newline in the first paragraph has been parsed as
  5597. a `softbreak`, and the asterisks in the first list item have become
  5598. an `emph`.
  5599. The document can be rendered as HTML, or in any other format, given
  5600. an appropriate renderer.