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