aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 6d90d07f0e8bb9c12f1413e5673929cfdc44e1a8 (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. baz</li>
  2598. </ul>
  2599. .
  2600. ### Motivation
  2601. John Gruber's Markdown spec says the following about list items:
  2602. 1. "List markers typically start at the left margin, but may be indented
  2603. by up to three spaces. List markers must be followed by one or more
  2604. spaces or a tab."
  2605. 2. "To make lists look nice, you can wrap items with hanging indents....
  2606. But if you don't want to, you don't have to."
  2607. 3. "List items may consist of multiple paragraphs. Each subsequent
  2608. paragraph in a list item must be indented by either 4 spaces or one
  2609. tab."
  2610. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  2611. but here again, Markdown will allow you to be lazy."
  2612. 5. "To put a blockquote within a list item, the blockquote's `>`
  2613. delimiters need to be indented."
  2614. 6. "To put a code block within a list item, the code block needs to be
  2615. indented twice — 8 spaces or two tabs."
  2616. These rules specify that a paragraph under a list item must be indented
  2617. four spaces (presumably, from the left margin, rather than the start of
  2618. the list marker, but this is not said), and that code under a list item
  2619. must be indented eight spaces instead of the usual four. They also say
  2620. that a block quote must be indented, but not by how much; however, the
  2621. example given has four spaces indentation. Although nothing is said
  2622. about other kinds of block-level content, it is certainly reasonable to
  2623. infer that *all* block elements under a list item, including other
  2624. lists, must be indented four spaces. This principle has been called the
  2625. *four-space rule*.
  2626. The four-space rule is clear and principled, and if the reference
  2627. implementation `Markdown.pl` had followed it, it probably would have
  2628. become the standard. However, `Markdown.pl` allowed paragraphs and
  2629. sublists to start with only two spaces indentation, at least on the
  2630. outer level. Worse, its behavior was inconsistent: a sublist of an
  2631. outer-level list needed two spaces indentation, but a sublist of this
  2632. sublist needed three spaces. It is not surprising, then, that different
  2633. implementations of Markdown have developed very different rules for
  2634. determining what comes under a list item. (Pandoc and python-Markdown,
  2635. for example, stuck with Gruber's syntax description and the four-space
  2636. rule, while discount, redcarpet, marked, PHP Markdown, and others
  2637. followed `Markdown.pl`'s behavior more closely.)
  2638. Unfortunately, given the divergences between implementations, there
  2639. is no way to give a spec for list items that will be guaranteed not
  2640. to break any existing documents. However, the spec given here should
  2641. correctly handle lists formatted with either the four-space rule or
  2642. the more forgiving `Markdown.pl` behavior, provided they are laid out
  2643. in a way that is natural for a human to read.
  2644. The strategy here is to let the width and indentation of the list marker
  2645. determine the indentation necessary for blocks to fall under the list
  2646. item, rather than having a fixed and arbitrary number. The writer can
  2647. think of the body of the list item as a unit which gets indented to the
  2648. right enough to fit the list marker (and any indentation on the list
  2649. marker). (The laziness rule, #4, then allows continuation lines to be
  2650. unindented if needed.)
  2651. This rule is superior, we claim, to any rule requiring a fixed level of
  2652. indentation from the margin. The four-space rule is clear but
  2653. unnatural. It is quite unintuitive that
  2654. ``` markdown
  2655. - foo
  2656. bar
  2657. - baz
  2658. ```
  2659. should be parsed as two lists with an intervening paragraph,
  2660. ``` html
  2661. <ul>
  2662. <li>foo</li>
  2663. </ul>
  2664. <p>bar</p>
  2665. <ul>
  2666. <li>baz</li>
  2667. </ul>
  2668. ```
  2669. as the four-space rule demands, rather than a single list,
  2670. ``` html
  2671. <ul>
  2672. <li>
  2673. <p>foo</p>
  2674. <p>bar</p>
  2675. <ul>
  2676. <li>baz</li>
  2677. </ul>
  2678. </li>
  2679. </ul>
  2680. ```
  2681. The choice of four spaces is arbitrary. It can be learned, but it is
  2682. not likely to be guessed, and it trips up beginners regularly.
  2683. Would it help to adopt a two-space rule? The problem is that such
  2684. a rule, together with the rule allowing 1--3 spaces indentation of the
  2685. initial list marker, allows text that is indented *less than* the
  2686. original list marker to be included in the list item. For example,
  2687. `Markdown.pl` parses
  2688. ``` markdown
  2689. - one
  2690. two
  2691. ```
  2692. as a single list item, with `two` a continuation paragraph:
  2693. ``` html
  2694. <ul>
  2695. <li>
  2696. <p>one</p>
  2697. <p>two</p>
  2698. </li>
  2699. </ul>
  2700. ```
  2701. and similarly
  2702. ``` markdown
  2703. > - one
  2704. >
  2705. > two
  2706. ```
  2707. as
  2708. ``` html
  2709. <blockquote>
  2710. <ul>
  2711. <li>
  2712. <p>one</p>
  2713. <p>two</p>
  2714. </li>
  2715. </ul>
  2716. </blockquote>
  2717. ```
  2718. This is extremely unintuitive.
  2719. Rather than requiring a fixed indent from the margin, we could require
  2720. a fixed indent (say, two spaces, or even one space) from the list marker (which
  2721. may itself be indented). This proposal would remove the last anomaly
  2722. discussed. Unlike the spec presented above, it would count the following
  2723. as a list item with a subparagraph, even though the paragraph `bar`
  2724. is not indented as far as the first paragraph `foo`:
  2725. ``` markdown
  2726. 10. foo
  2727. bar
  2728. ```
  2729. Arguably this text does read like a list item with `bar` as a subparagraph,
  2730. which may count in favor of the proposal. However, on this proposal indented
  2731. code would have to be indented six spaces after the list marker. And this
  2732. would break a lot of existing Markdown, which has the pattern:
  2733. ``` markdown
  2734. 1. foo
  2735. indented code
  2736. ```
  2737. where the code is indented eight spaces. The spec above, by contrast, will
  2738. parse this text as expected, since the code block's indentation is measured
  2739. from the beginning of `foo`.
  2740. The one case that needs special treatment is a list item that *starts*
  2741. with indented code. How much indentation is required in that case, since
  2742. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  2743. that in such cases, we require one space indentation from the list marker
  2744. (and then the normal four spaces for the indented code). This will match the
  2745. four-space rule in cases where the list marker plus its initial indentation
  2746. takes four spaces (a common case), but diverge in other cases.
  2747. ## Lists
  2748. A [list](@list) is a sequence of one or more
  2749. list items [of the same type](#of-the-same-type). The list items
  2750. may be separated by single [blank lines](#blank-line), but two
  2751. blank lines end all containing lists.
  2752. Two list items are [of the same type](@of-the-same-type)
  2753. if they begin with a [list
  2754. marker](#list-marker) of the same type. Two list markers are of the
  2755. same type if (a) they are bullet list markers using the same character
  2756. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  2757. delimiter (either `.` or `)`).
  2758. A list is an [ordered list](@ordered-list)
  2759. if its constituent list items begin with
  2760. [ordered list markers](#ordered-list-marker), and a [bullet
  2761. list](@bullet-list) if its constituent list
  2762. items begin with [bullet list markers](#bullet-list-marker).
  2763. The [start number](@start-number)
  2764. of an [ordered list](#ordered-list) is determined by the list number of
  2765. its initial list item. The numbers of subsequent list items are
  2766. disregarded.
  2767. A list is [loose](@loose) if it any of its constituent
  2768. list items are separated by blank lines, or if any of its constituent
  2769. list items directly contain two block-level elements with a blank line
  2770. between them. Otherwise a list is [tight](@tight).
  2771. (The difference in HTML output is that paragraphs in a loose list are
  2772. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  2773. Changing the bullet or ordered list delimiter starts a new list:
  2774. .
  2775. - foo
  2776. - bar
  2777. + baz
  2778. .
  2779. <ul>
  2780. <li>foo</li>
  2781. <li>bar</li>
  2782. </ul>
  2783. <ul>
  2784. <li>baz</li>
  2785. </ul>
  2786. .
  2787. .
  2788. 1. foo
  2789. 2. bar
  2790. 3) baz
  2791. .
  2792. <ol>
  2793. <li>foo</li>
  2794. <li>bar</li>
  2795. </ol>
  2796. <ol start="3">
  2797. <li>baz</li>
  2798. </ol>
  2799. .
  2800. In CommonMark, a list can interrupt a paragraph. That is,
  2801. no blank line is needed to separate a paragraph from a following
  2802. list:
  2803. .
  2804. Foo
  2805. - bar
  2806. - baz
  2807. .
  2808. <p>Foo</p>
  2809. <ul>
  2810. <li>bar</li>
  2811. <li>baz</li>
  2812. </ul>
  2813. .
  2814. `Markdown.pl` does not allow this, through fear of triggering a list
  2815. via a numeral in a hard-wrapped line:
  2816. .
  2817. The number of windows in my house is
  2818. 14. The number of doors is 6.
  2819. .
  2820. <p>The number of windows in my house is</p>
  2821. <ol start="14">
  2822. <li>The number of doors is 6.</li>
  2823. </ol>
  2824. .
  2825. Oddly, `Markdown.pl` *does* allow a blockquote to interrupt a paragraph,
  2826. even though the same considerations might apply. We think that the two
  2827. cases should be treated the same. Here are two reasons for allowing
  2828. lists to interrupt paragraphs:
  2829. First, it is natural and not uncommon for people to start lists without
  2830. blank lines:
  2831. I need to buy
  2832. - new shoes
  2833. - a coat
  2834. - a plane ticket
  2835. Second, we are attracted to a
  2836. > [principle of uniformity](@principle-of-uniformity):
  2837. > if a span of text has a certain
  2838. > meaning, it will continue to have the same meaning when put into a list
  2839. > item.
  2840. (Indeed, the spec for [list items](#list-item) presupposes this.)
  2841. This principle implies that if
  2842. * I need to buy
  2843. - new shoes
  2844. - a coat
  2845. - a plane ticket
  2846. is a list item containing a paragraph followed by a nested sublist,
  2847. as all Markdown implementations agree it is (though the paragraph
  2848. may be rendered without `<p>` tags, since the list is "tight"),
  2849. then
  2850. I need to buy
  2851. - new shoes
  2852. - a coat
  2853. - a plane ticket
  2854. by itself should be a paragraph followed by a nested sublist.
  2855. Our adherence to the [principle of uniformity](#principle-of-uniformity)
  2856. thus inclines us to think that there are two coherent packages:
  2857. 1. Require blank lines before *all* lists and blockquotes,
  2858. including lists that occur as sublists inside other list items.
  2859. 2. Require blank lines in none of these places.
  2860. [reStructuredText](http://docutils.sourceforge.net/rst.html) takes
  2861. the first approach, for which there is much to be said. But the second
  2862. seems more consistent with established practice with Markdown.
  2863. There can be blank lines between items, but two blank lines end
  2864. a list:
  2865. .
  2866. - foo
  2867. - bar
  2868. - baz
  2869. .
  2870. <ul>
  2871. <li>
  2872. <p>foo</p>
  2873. </li>
  2874. <li>
  2875. <p>bar</p>
  2876. </li>
  2877. </ul>
  2878. <ul>
  2879. <li>baz</li>
  2880. </ul>
  2881. .
  2882. As illustrated above in the section on [list items](#list-item),
  2883. two blank lines between blocks *within* a list item will also end a
  2884. list:
  2885. .
  2886. - foo
  2887. bar
  2888. - baz
  2889. .
  2890. <ul>
  2891. <li>foo</li>
  2892. </ul>
  2893. <p>bar</p>
  2894. <ul>
  2895. <li>baz</li>
  2896. </ul>
  2897. .
  2898. Indeed, two blank lines will end *all* containing lists:
  2899. .
  2900. - foo
  2901. - bar
  2902. - baz
  2903. bim
  2904. .
  2905. <ul>
  2906. <li>foo
  2907. <ul>
  2908. <li>bar
  2909. <ul>
  2910. <li>baz</li>
  2911. </ul>
  2912. </li>
  2913. </ul>
  2914. </li>
  2915. </ul>
  2916. <pre><code> bim
  2917. </code></pre>
  2918. .
  2919. Thus, two blank lines can be used to separate consecutive lists of
  2920. the same type, or to separate a list from an indented code block
  2921. that would otherwise be parsed as a subparagraph of the final list
  2922. item:
  2923. .
  2924. - foo
  2925. - bar
  2926. - baz
  2927. - bim
  2928. .
  2929. <ul>
  2930. <li>foo</li>
  2931. <li>bar</li>
  2932. </ul>
  2933. <ul>
  2934. <li>baz</li>
  2935. <li>bim</li>
  2936. </ul>
  2937. .
  2938. .
  2939. - foo
  2940. notcode
  2941. - foo
  2942. code
  2943. .
  2944. <ul>
  2945. <li>
  2946. <p>foo</p>
  2947. <p>notcode</p>
  2948. </li>
  2949. <li>
  2950. <p>foo</p>
  2951. </li>
  2952. </ul>
  2953. <pre><code>code
  2954. </code></pre>
  2955. .
  2956. List items need not be indented to the same level. The following
  2957. list items will be treated as items at the same list level,
  2958. since none is indented enough to belong to the previous list
  2959. item:
  2960. .
  2961. - a
  2962. - b
  2963. - c
  2964. - d
  2965. - e
  2966. - f
  2967. - g
  2968. .
  2969. <ul>
  2970. <li>a</li>
  2971. <li>b</li>
  2972. <li>c</li>
  2973. <li>d</li>
  2974. <li>e</li>
  2975. <li>f</li>
  2976. <li>g</li>
  2977. </ul>
  2978. .
  2979. This is a loose list, because there is a blank line between
  2980. two of the list items:
  2981. .
  2982. - a
  2983. - b
  2984. - c
  2985. .
  2986. <ul>
  2987. <li>
  2988. <p>a</p>
  2989. </li>
  2990. <li>
  2991. <p>b</p>
  2992. </li>
  2993. <li>
  2994. <p>c</p>
  2995. </li>
  2996. </ul>
  2997. .
  2998. So is this, with a empty second item:
  2999. .
  3000. * a
  3001. *
  3002. * c
  3003. .
  3004. <ul>
  3005. <li>
  3006. <p>a</p>
  3007. </li>
  3008. <li></li>
  3009. <li>
  3010. <p>c</p>
  3011. </li>
  3012. </ul>
  3013. .
  3014. These are loose lists, even though there is no space between the items,
  3015. because one of the items directly contains two block-level elements
  3016. with a blank line between them:
  3017. .
  3018. - a
  3019. - b
  3020. c
  3021. - d
  3022. .
  3023. <ul>
  3024. <li>
  3025. <p>a</p>
  3026. </li>
  3027. <li>
  3028. <p>b</p>
  3029. <p>c</p>
  3030. </li>
  3031. <li>
  3032. <p>d</p>
  3033. </li>
  3034. </ul>
  3035. .
  3036. .
  3037. - a
  3038. - b
  3039. [ref]: /url
  3040. - d
  3041. .
  3042. <ul>
  3043. <li>
  3044. <p>a</p>
  3045. </li>
  3046. <li>
  3047. <p>b</p>
  3048. </li>
  3049. <li>
  3050. <p>d</p>
  3051. </li>
  3052. </ul>
  3053. .
  3054. This is a tight list, because the blank lines are in a code block:
  3055. .
  3056. - a
  3057. - ```
  3058. b
  3059. ```
  3060. - c
  3061. .
  3062. <ul>
  3063. <li>a</li>
  3064. <li>
  3065. <pre><code>b
  3066. </code></pre>
  3067. </li>
  3068. <li>c</li>
  3069. </ul>
  3070. .
  3071. This is a tight list, because the blank line is between two
  3072. paragraphs of a sublist. So the sublist is loose while
  3073. the outer list is tight:
  3074. .
  3075. - a
  3076. - b
  3077. c
  3078. - d
  3079. .
  3080. <ul>
  3081. <li>a
  3082. <ul>
  3083. <li>
  3084. <p>b</p>
  3085. <p>c</p>
  3086. </li>
  3087. </ul>
  3088. </li>
  3089. <li>d</li>
  3090. </ul>
  3091. .
  3092. This is a tight list, because the blank line is inside the
  3093. block quote:
  3094. .
  3095. * a
  3096. > b
  3097. >
  3098. * c
  3099. .
  3100. <ul>
  3101. <li>a
  3102. <blockquote>
  3103. <p>b</p>
  3104. </blockquote>
  3105. </li>
  3106. <li>c</li>
  3107. </ul>
  3108. .
  3109. This list is tight, because the consecutive block elements
  3110. are not separated by blank lines:
  3111. .
  3112. - a
  3113. > b
  3114. ```
  3115. c
  3116. ```
  3117. - d
  3118. .
  3119. <ul>
  3120. <li>a
  3121. <blockquote>
  3122. <p>b</p>
  3123. </blockquote>
  3124. <pre><code>c
  3125. </code></pre>
  3126. </li>
  3127. <li>d</li>
  3128. </ul>
  3129. .
  3130. A single-paragraph list is tight:
  3131. .
  3132. - a
  3133. .
  3134. <ul>
  3135. <li>a</li>
  3136. </ul>
  3137. .
  3138. .
  3139. - a
  3140. - b
  3141. .
  3142. <ul>
  3143. <li>a
  3144. <ul>
  3145. <li>b</li>
  3146. </ul>
  3147. </li>
  3148. </ul>
  3149. .
  3150. Here the outer list is loose, the inner list tight:
  3151. .
  3152. * foo
  3153. * bar
  3154. baz
  3155. .
  3156. <ul>
  3157. <li>
  3158. <p>foo</p>
  3159. <ul>
  3160. <li>bar</li>
  3161. </ul>
  3162. <p>baz</p>
  3163. </li>
  3164. </ul>
  3165. .
  3166. .
  3167. - a
  3168. - b
  3169. - c
  3170. - d
  3171. - e
  3172. - f
  3173. .
  3174. <ul>
  3175. <li>
  3176. <p>a</p>
  3177. <ul>
  3178. <li>b</li>
  3179. <li>c</li>
  3180. </ul>
  3181. </li>
  3182. <li>
  3183. <p>d</p>
  3184. <ul>
  3185. <li>e</li>
  3186. <li>f</li>
  3187. </ul>
  3188. </li>
  3189. </ul>
  3190. .
  3191. # Inlines
  3192. Inlines are parsed sequentially from the beginning of the character
  3193. stream to the end (left to right, in left-to-right languages).
  3194. Thus, for example, in
  3195. .
  3196. `hi`lo`
  3197. .
  3198. <p><code>hi</code>lo`</p>
  3199. .
  3200. `hi` is parsed as code, leaving the backtick at the end as a literal
  3201. backtick.
  3202. ## Backslash escapes
  3203. Any ASCII punctuation character may be backslash-escaped:
  3204. .
  3205. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  3206. .
  3207. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  3208. .
  3209. Backslashes before other characters are treated as literal
  3210. backslashes:
  3211. .
  3212. \→\A\a\ \3\φ\«
  3213. .
  3214. <p>\ \A\a\ \3\φ\«</p>
  3215. .
  3216. Escaped characters are treated as regular characters and do
  3217. not have their usual Markdown meanings:
  3218. .
  3219. \*not emphasized*
  3220. \<br/> not a tag
  3221. \[not a link](/foo)
  3222. \`not code`
  3223. 1\. not a list
  3224. \* not a list
  3225. \# not a header
  3226. \[foo]: /url "not a reference"
  3227. .
  3228. <p>*not emphasized*
  3229. &lt;br/&gt; not a tag
  3230. [not a link](/foo)
  3231. `not code`
  3232. 1. not a list
  3233. * not a list
  3234. # not a header
  3235. [foo]: /url &quot;not a reference&quot;</p>
  3236. .
  3237. If a backslash is itself escaped, the following character is not:
  3238. .
  3239. \\*emphasis*
  3240. .
  3241. <p>\<em>emphasis</em></p>
  3242. .
  3243. A backslash at the end of the line is a [hard line
  3244. break](#hard-line-break):
  3245. .
  3246. foo\
  3247. bar
  3248. .
  3249. <p>foo<br />
  3250. bar</p>
  3251. .
  3252. Backslash escapes do not work in code blocks, code spans, autolinks, or
  3253. raw HTML:
  3254. .
  3255. `` \[\` ``
  3256. .
  3257. <p><code>\[\`</code></p>
  3258. .
  3259. .
  3260. \[\]
  3261. .
  3262. <pre><code>\[\]
  3263. </code></pre>
  3264. .
  3265. .
  3266. ~~~
  3267. \[\]
  3268. ~~~
  3269. .
  3270. <pre><code>\[\]
  3271. </code></pre>
  3272. .
  3273. .
  3274. <http://example.com?find=\*>
  3275. .
  3276. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  3277. .
  3278. .
  3279. <a href="/bar\/)">
  3280. .
  3281. <p><a href="/bar\/)"></p>
  3282. .
  3283. But they work in all other contexts, including URLs and link titles,
  3284. link references, and info strings in [fenced code
  3285. blocks](#fenced-code-block):
  3286. .
  3287. [foo](/bar\* "ti\*tle")
  3288. .
  3289. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3290. .
  3291. .
  3292. [foo]
  3293. [foo]: /bar\* "ti\*tle"
  3294. .
  3295. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3296. .
  3297. .
  3298. ``` foo\+bar
  3299. foo
  3300. ```
  3301. .
  3302. <pre><code class="language-foo+bar">foo
  3303. </code></pre>
  3304. .
  3305. ## Entities
  3306. With the goal of making this standard as HTML-agnostic as possible, all
  3307. valid HTML entities in any context are recognized as such and
  3308. converted into unicode characters before they are stored in the AST.
  3309. This allows implementations that target HTML output to trivially escape
  3310. the entities when generating HTML, and simplifies the job of
  3311. implementations targetting other languages, as these will only need to
  3312. handle the unicode chars and need not be HTML-entity aware.
  3313. [Named entities](@name-entities) consist of `&`
  3314. + any of the valid HTML5 entity names + `;`. The
  3315. [following document](http://www.whatwg.org/specs/web-apps/current-work/multipage/entities.json)
  3316. is used as an authoritative source of the valid entity names and their
  3317. corresponding codepoints.
  3318. Conforming implementations that target HTML don't need to generate
  3319. entities for all the valid named entities that exist, with the exception
  3320. of `"` (`&quot;`), `&` (`&amp;`), `<` (`&lt;`) and `>` (`&gt;`), which
  3321. always need to be written as entities for security reasons.
  3322. .
  3323. &nbsp; &amp; &copy; &AElig; &Dcaron; &frac34; &HilbertSpace; &DifferentialD; &ClockwiseContourIntegral;
  3324. .
  3325. <p>  &amp; © Æ Ď ¾ ℋ ⅆ ∲</p>
  3326. .
  3327. [Decimal entities](@decimal-entities)
  3328. consist of `&#` + a string of 1--8 arabic digits + `;`. Again, these
  3329. entities need to be recognised and tranformed into their corresponding
  3330. UTF8 codepoints. Invalid Unicode codepoints will be written as the
  3331. "unknown codepoint" character (`0xFFFD`)
  3332. .
  3333. &#35; &#1234; &#992; &#98765432;
  3334. .
  3335. <p># Ӓ Ϡ �</p>
  3336. .
  3337. [Hexadecimal entities](@hexadecimal-entities)
  3338. consist of `&#` + either `X` or `x` + a string of 1-8 hexadecimal digits
  3339. + `;`. They will also be parsed and turned into their corresponding UTF8 values in the AST.
  3340. .
  3341. &#X22; &#XD06; &#xcab;
  3342. .
  3343. <p>&quot; ആ ಫ</p>
  3344. .
  3345. Here are some nonentities:
  3346. .
  3347. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3348. .
  3349. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3350. .
  3351. Although HTML5 does accept some entities without a trailing semicolon
  3352. (such as `&copy`), these are not recognized as entities here, because it
  3353. makes the grammar too ambiguous:
  3354. .
  3355. &copy
  3356. .
  3357. <p>&amp;copy</p>
  3358. .
  3359. Strings that are not on the list of HTML5 named entities are not
  3360. recognized as entities either:
  3361. .
  3362. &MadeUpEntity;
  3363. .
  3364. <p>&amp;MadeUpEntity;</p>
  3365. .
  3366. Entities are recognized in any context besides code spans or
  3367. code blocks, including raw HTML, URLs, [link titles](#link-title), and
  3368. [fenced code block](#fenced-code-block) info strings:
  3369. .
  3370. <a href="&ouml;&ouml;.html">
  3371. .
  3372. <p><a href="&ouml;&ouml;.html"></p>
  3373. .
  3374. .
  3375. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3376. .
  3377. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3378. .
  3379. .
  3380. [foo]
  3381. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3382. .
  3383. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3384. .
  3385. .
  3386. ``` f&ouml;&ouml;
  3387. foo
  3388. ```
  3389. .
  3390. <pre><code class="language-föö">foo
  3391. </code></pre>
  3392. .
  3393. Entities are treated as literal text in code spans and code blocks:
  3394. .
  3395. `f&ouml;&ouml;`
  3396. .
  3397. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3398. .
  3399. .
  3400. f&ouml;f&ouml;
  3401. .
  3402. <pre><code>f&amp;ouml;f&amp;ouml;
  3403. </code></pre>
  3404. .
  3405. ## Code span
  3406. A [backtick string](@backtick-string)
  3407. is a string of one or more backtick characters (`` ` ``) that is neither
  3408. preceded nor followed by a backtick.
  3409. A [code span](@code-span) begins with a backtick string and ends with a backtick
  3410. string of equal length. The contents of the code span are the
  3411. characters between the two backtick strings, with leading and trailing
  3412. spaces and newlines removed, and consecutive spaces and newlines
  3413. collapsed to single spaces.
  3414. This is a simple code span:
  3415. .
  3416. `foo`
  3417. .
  3418. <p><code>foo</code></p>
  3419. .
  3420. Here two backticks are used, because the code contains a backtick.
  3421. This example also illustrates stripping of leading and trailing spaces:
  3422. .
  3423. `` foo ` bar ``
  3424. .
  3425. <p><code>foo ` bar</code></p>
  3426. .
  3427. This example shows the motivation for stripping leading and trailing
  3428. spaces:
  3429. .
  3430. ` `` `
  3431. .
  3432. <p><code>``</code></p>
  3433. .
  3434. Newlines are treated like spaces:
  3435. .
  3436. ``
  3437. foo
  3438. ``
  3439. .
  3440. <p><code>foo</code></p>
  3441. .
  3442. Interior spaces and newlines are collapsed into single spaces, just
  3443. as they would be by a browser:
  3444. .
  3445. `foo bar
  3446. baz`
  3447. .
  3448. <p><code>foo bar baz</code></p>
  3449. .
  3450. Q: Why not just leave the spaces, since browsers will collapse them
  3451. anyway? A: Because we might be targeting a non-HTML format, and we
  3452. shouldn't rely on HTML-specific rendering assumptions.
  3453. (Existing implementations differ in their treatment of internal
  3454. spaces and newlines. Some, including `Markdown.pl` and
  3455. `showdown`, convert an internal newline into a `<br />` tag.
  3456. But this makes things difficult for those who like to hard-wrap
  3457. their paragraphs, since a line break in the midst of a code
  3458. span will cause an unintended line break in the output. Others
  3459. just leave internal spaces as they are, which is fine if only
  3460. HTML is being targeted.)
  3461. .
  3462. `foo `` bar`
  3463. .
  3464. <p><code>foo `` bar</code></p>
  3465. .
  3466. Note that backslash escapes do not work in code spans. All backslashes
  3467. are treated literally:
  3468. .
  3469. `foo\`bar`
  3470. .
  3471. <p><code>foo\</code>bar`</p>
  3472. .
  3473. Backslash escapes are never needed, because one can always choose a
  3474. string of *n* backtick characters as delimiters, where the code does
  3475. not contain any strings of exactly *n* backtick characters.
  3476. Code span backticks have higher precedence than any other inline
  3477. constructs except HTML tags and autolinks. Thus, for example, this is
  3478. not parsed as emphasized text, since the second `*` is part of a code
  3479. span:
  3480. .
  3481. *foo`*`
  3482. .
  3483. <p>*foo<code>*</code></p>
  3484. .
  3485. And this is not parsed as a link:
  3486. .
  3487. [not a `link](/foo`)
  3488. .
  3489. <p>[not a <code>link](/foo</code>)</p>
  3490. .
  3491. But this is a link:
  3492. .
  3493. <http://foo.bar.`baz>`
  3494. .
  3495. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3496. .
  3497. And this is an HTML tag:
  3498. .
  3499. <a href="`">`
  3500. .
  3501. <p><a href="`">`</p>
  3502. .
  3503. When a backtick string is not closed by a matching backtick string,
  3504. we just have literal backticks:
  3505. .
  3506. ```foo``
  3507. .
  3508. <p>```foo``</p>
  3509. .
  3510. .
  3511. `foo
  3512. .
  3513. <p>`foo</p>
  3514. .
  3515. ## Emphasis and strong emphasis
  3516. John Gruber's original [Markdown syntax
  3517. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3518. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3519. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3520. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3521. > tag.
  3522. This is enough for most users, but these rules leave much undecided,
  3523. especially when it comes to nested emphasis. The original
  3524. `Markdown.pl` test suite makes it clear that triple `***` and
  3525. `___` delimiters can be used for strong emphasis, and most
  3526. implementations have also allowed the following patterns:
  3527. ``` markdown
  3528. ***strong emph***
  3529. ***strong** in emph*
  3530. ***emph* in strong**
  3531. **in strong *emph***
  3532. *in emph **strong***
  3533. ```
  3534. The following patterns are less widely supported, but the intent
  3535. is clear and they are useful (especially in contexts like bibliography
  3536. entries):
  3537. ``` markdown
  3538. *emph *with emph* in it*
  3539. **strong **with strong** in it**
  3540. ```
  3541. Many implementations have also restricted intraword emphasis to
  3542. the `*` forms, to avoid unwanted emphasis in words containing
  3543. internal underscores. (It is best practice to put these in code
  3544. spans, but users often do not.)
  3545. ``` markdown
  3546. internal emphasis: foo*bar*baz
  3547. no emphasis: foo_bar_baz
  3548. ```
  3549. The following rules capture all of these patterns, while allowing
  3550. for efficient parsing strategies that do not backtrack:
  3551. 1. A single `*` character [can open emphasis](@can-open-emphasis)
  3552. iff it is not followed by
  3553. whitespace.
  3554. 2. A single `_` character [can open emphasis](#can-open-emphasis) iff
  3555. it is not followed by whitespace and it is not preceded by an
  3556. ASCII alphanumeric character.
  3557. 3. A single `*` character [can close emphasis](@can-close-emphasis)
  3558. iff it is not preceded by whitespace.
  3559. 4. A single `_` character [can close emphasis](#can-close-emphasis) iff
  3560. it is not preceded by whitespace and it is not followed by an
  3561. ASCII alphanumeric character.
  3562. 5. A double `**` [can open strong emphasis](@can-open-strong-emphasis)
  3563. iff it is not followed by
  3564. whitespace.
  3565. 6. A double `__` [can open strong emphasis](#can-open-strong-emphasis)
  3566. iff it is not followed by whitespace and it is not preceded by an
  3567. ASCII alphanumeric character.
  3568. 7. A double `**` [can close strong emphasis](@can-close-strong-emphasis)
  3569. iff it is not preceded by
  3570. whitespace.
  3571. 8. A double `__` [can close strong emphasis](#can-close-strong-emphasis)
  3572. iff it is not preceded by whitespace and it is not followed by an
  3573. ASCII alphanumeric character.
  3574. 9. Emphasis begins with a delimiter that [can open
  3575. emphasis](#can-open-emphasis) and ends with a delimiter that [can close
  3576. emphasis](#can-close-emphasis), and that uses the same
  3577. character (`_` or `*`) as the opening delimiter. There must
  3578. be a nonempty sequence of inlines between the open delimiter
  3579. and the closing delimiter; these form the contents of the emphasis
  3580. inline.
  3581. 10. Strong emphasis begins with a delimiter that [can open strong
  3582. emphasis](#can-open-strong-emphasis) and ends with a delimiter that
  3583. [can close strong emphasis](#can-close-strong-emphasis), and that
  3584. uses the same character (`_` or `*`) as the opening delimiter.
  3585. There must be a nonempty sequence of inlines between the open
  3586. delimiter and the closing delimiter; these form the contents of
  3587. the strong emphasis inline.
  3588. 11. A literal `*` character cannot occur at the beginning or end of
  3589. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  3590. is backslash-escaped.
  3591. 12. A literal `_` character cannot occur at the beginning or end of
  3592. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3593. is backslash-escaped.
  3594. Where rules 1--12 above are compatible with multiple parsings,
  3595. the following principles resolve ambiguity:
  3596. 13. The number of nestings should be minimized. Thus, for example,
  3597. an interpretation `<strong>...</strong>` is always preferred to
  3598. `<em><em>...</em></em>`.
  3599. 14. An interpretation `<strong><em>...</em></strong>` is always
  3600. preferred to `<em><strong>..</strong></em>`.
  3601. 15. When two potential emphasis or strong emphasis spans overlap,
  3602. so that the second begins before the first ends and ends after
  3603. the first ends, the first is preferred. Thus, for example,
  3604. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3605. than `*foo <em>bar* baz</em>`. For the same reason,
  3606. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3607. rather than `<strong>foo*bar</strong>`.
  3608. 16. When there are two potential emphasis or strong emphasis spans
  3609. with the same closing delimiter, the shorter one (the one that
  3610. opens later) is preferred. Thus, for example,
  3611. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3612. rather than `<strong>foo **bar baz</strong>`.
  3613. 17. Inline code spans, links, images, and HTML tags group more tightly
  3614. than emphasis. So, when there is a choice between an interpretation
  3615. that contains one of these elements and one that does not, the
  3616. former always wins. Thus, for example, `*[foo*](bar)` is
  3617. parsed as `*<a href="bar">foo*</a>` rather than as
  3618. `<em>[foo</em>](bar)`.
  3619. These rules can be illustrated through a series of examples.
  3620. Rule 1:
  3621. .
  3622. *foo bar*
  3623. .
  3624. <p><em>foo bar</em></p>
  3625. .
  3626. This is not emphasis, because the opening `*` is followed by
  3627. whitespace:
  3628. .
  3629. a * foo bar*
  3630. .
  3631. <p>a * foo bar*</p>
  3632. .
  3633. Intraword emphasis with `*` is permitted:
  3634. .
  3635. foo*bar*
  3636. .
  3637. <p>foo<em>bar</em></p>
  3638. .
  3639. .
  3640. 5*6*78
  3641. .
  3642. <p>5<em>6</em>78</p>
  3643. .
  3644. Rule 2:
  3645. .
  3646. _foo bar_
  3647. .
  3648. <p><em>foo bar</em></p>
  3649. .
  3650. This is not emphasis, because the opening `*` is followed by
  3651. whitespace:
  3652. .
  3653. _ foo bar_
  3654. .
  3655. <p>_ foo bar_</p>
  3656. .
  3657. Emphasis with `_` is not allowed inside ASCII words:
  3658. .
  3659. foo_bar_
  3660. .
  3661. <p>foo_bar_</p>
  3662. .
  3663. .
  3664. 5_6_78
  3665. .
  3666. <p>5_6_78</p>
  3667. .
  3668. But it is permitted inside non-ASCII words:
  3669. .
  3670. пристаням_стремятся_
  3671. .
  3672. <p>пристаням<em>стремятся</em></p>
  3673. .
  3674. Rule 3:
  3675. This is not emphasis, because the closing `*` is preceded by
  3676. whitespace:
  3677. .
  3678. *foo bar *
  3679. .
  3680. <p>*foo bar *</p>
  3681. .
  3682. Intraword emphasis with `*` is allowed:
  3683. .
  3684. *foo*bar
  3685. .
  3686. <p><em>foo</em>bar</p>
  3687. .
  3688. Rule 4:
  3689. This is not emphasis, because the closing `_` is preceded by
  3690. whitespace:
  3691. .
  3692. _foo bar _
  3693. .
  3694. <p>_foo bar _</p>
  3695. .
  3696. Intraword emphasis:
  3697. .
  3698. _foo_bar
  3699. .
  3700. <p>_foo_bar</p>
  3701. .
  3702. .
  3703. _пристаням_стремятся
  3704. .
  3705. <p><em>пристаням</em>стремятся</p>
  3706. .
  3707. .
  3708. _foo_bar_baz_
  3709. .
  3710. <p><em>foo_bar_baz</em></p>
  3711. .
  3712. Rule 5:
  3713. .
  3714. **foo bar**
  3715. .
  3716. <p><strong>foo bar</strong></p>
  3717. .
  3718. This is not strong emphasis, because the opening delimiter is
  3719. followed by whitespace:
  3720. .
  3721. ** foo bar**
  3722. .
  3723. <p>** foo bar**</p>
  3724. .
  3725. Intraword strong emphasis with `**` is permitted:
  3726. .
  3727. foo**bar**
  3728. .
  3729. <p>foo<strong>bar</strong></p>
  3730. .
  3731. Rule 6:
  3732. .
  3733. __foo bar__
  3734. .
  3735. <p><strong>foo bar</strong></p>
  3736. .
  3737. This is not strong emphasis, because the opening delimiter is
  3738. followed by whitespace:
  3739. .
  3740. __ foo bar__
  3741. .
  3742. <p>__ foo bar__</p>
  3743. .
  3744. Intraword emphasis examples:
  3745. .
  3746. foo__bar__
  3747. .
  3748. <p>foo__bar__</p>
  3749. .
  3750. .
  3751. 5__6__78
  3752. .
  3753. <p>5__6__78</p>
  3754. .
  3755. .
  3756. пристаням__стремятся__
  3757. .
  3758. <p>пристаням<strong>стремятся</strong></p>
  3759. .
  3760. .
  3761. __foo, __bar__, baz__
  3762. .
  3763. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  3764. .
  3765. Rule 7:
  3766. This is not strong emphasis, because the closing delimiter is preceded
  3767. by whitespace:
  3768. .
  3769. **foo bar **
  3770. .
  3771. <p>**foo bar **</p>
  3772. .
  3773. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  3774. Rule 11.)
  3775. Intraword emphasis:
  3776. .
  3777. **foo**bar
  3778. .
  3779. <p><strong>foo</strong>bar</p>
  3780. .
  3781. Rule 8:
  3782. This is not strong emphasis, because the closing delimiter is
  3783. preceded by whitespace:
  3784. .
  3785. __foo bar __
  3786. .
  3787. <p>__foo bar __</p>
  3788. .
  3789. Intraword strong emphasis examples:
  3790. .
  3791. __foo__bar
  3792. .
  3793. <p>__foo__bar</p>
  3794. .
  3795. .
  3796. __пристаням__стремятся
  3797. .
  3798. <p><strong>пристаням</strong>стремятся</p>
  3799. .
  3800. .
  3801. __foo__bar__baz__
  3802. .
  3803. <p><strong>foo__bar__baz</strong></p>
  3804. .
  3805. Rule 9:
  3806. Any nonempty sequence of inline elements can be the contents of an
  3807. emphasized span.
  3808. .
  3809. *foo [bar](/url)*
  3810. .
  3811. <p><em>foo <a href="/url">bar</a></em></p>
  3812. .
  3813. .
  3814. *foo
  3815. bar*
  3816. .
  3817. <p><em>foo
  3818. bar</em></p>
  3819. .
  3820. In particular, emphasis and strong emphasis can be nested
  3821. inside emphasis:
  3822. .
  3823. _foo __bar__ baz_
  3824. .
  3825. <p><em>foo <strong>bar</strong> baz</em></p>
  3826. .
  3827. .
  3828. _foo _bar_ baz_
  3829. .
  3830. <p><em>foo <em>bar</em> baz</em></p>
  3831. .
  3832. .
  3833. __foo_ bar_
  3834. .
  3835. <p><em><em>foo</em> bar</em></p>
  3836. .
  3837. .
  3838. *foo *bar**
  3839. .
  3840. <p><em>foo <em>bar</em></em></p>
  3841. .
  3842. .
  3843. *foo **bar** baz*
  3844. .
  3845. <p><em>foo <strong>bar</strong> baz</em></p>
  3846. .
  3847. But note:
  3848. .
  3849. *foo**bar**baz*
  3850. .
  3851. <p><em>foo</em><em>bar</em><em>baz</em></p>
  3852. .
  3853. The difference is that in the preceding case,
  3854. the internal delimiters [can close emphasis](#can-close-emphasis),
  3855. while in the cases with spaces, they cannot.
  3856. .
  3857. ***foo** bar*
  3858. .
  3859. <p><em><strong>foo</strong> bar</em></p>
  3860. .
  3861. .
  3862. *foo **bar***
  3863. .
  3864. <p><em>foo <strong>bar</strong></em></p>
  3865. .
  3866. Note, however, that in the following case we get no strong
  3867. emphasis, because the opening delimiter is closed by the first
  3868. `*` before `bar`:
  3869. .
  3870. *foo**bar***
  3871. .
  3872. <p><em>foo</em><em>bar</em>**</p>
  3873. .
  3874. Indefinite levels of nesting are possible:
  3875. .
  3876. *foo **bar *baz* bim** bop*
  3877. .
  3878. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  3879. .
  3880. .
  3881. *foo [*bar*](/url)*
  3882. .
  3883. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  3884. .
  3885. There can be no empty emphasis or strong emphasis:
  3886. .
  3887. ** is not an empty emphasis
  3888. .
  3889. <p>** is not an empty emphasis</p>
  3890. .
  3891. .
  3892. **** is not an empty strong emphasis
  3893. .
  3894. <p>**** is not an empty strong emphasis</p>
  3895. .
  3896. Rule 10:
  3897. Any nonempty sequence of inline elements can be the contents of an
  3898. strongly emphasized span.
  3899. .
  3900. **foo [bar](/url)**
  3901. .
  3902. <p><strong>foo <a href="/url">bar</a></strong></p>
  3903. .
  3904. .
  3905. **foo
  3906. bar**
  3907. .
  3908. <p><strong>foo
  3909. bar</strong></p>
  3910. .
  3911. In particular, emphasis and strong emphasis can be nested
  3912. inside strong emphasis:
  3913. .
  3914. __foo _bar_ baz__
  3915. .
  3916. <p><strong>foo <em>bar</em> baz</strong></p>
  3917. .
  3918. .
  3919. __foo __bar__ baz__
  3920. .
  3921. <p><strong>foo <strong>bar</strong> baz</strong></p>
  3922. .
  3923. .
  3924. ____foo__ bar__
  3925. .
  3926. <p><strong><strong>foo</strong> bar</strong></p>
  3927. .
  3928. .
  3929. **foo **bar****
  3930. .
  3931. <p><strong>foo <strong>bar</strong></strong></p>
  3932. .
  3933. .
  3934. **foo *bar* baz**
  3935. .
  3936. <p><strong>foo <em>bar</em> baz</strong></p>
  3937. .
  3938. But note:
  3939. .
  3940. **foo*bar*baz**
  3941. .
  3942. <p><em><em>foo</em>bar</em>baz**</p>
  3943. .
  3944. The difference is that in the preceding case,
  3945. the internal delimiters [can close emphasis](#can-close-emphasis),
  3946. while in the cases with spaces, they cannot.
  3947. .
  3948. ***foo* bar**
  3949. .
  3950. <p><strong><em>foo</em> bar</strong></p>
  3951. .
  3952. .
  3953. **foo *bar***
  3954. .
  3955. <p><strong>foo <em>bar</em></strong></p>
  3956. .
  3957. Indefinite levels of nesting are possible:
  3958. .
  3959. **foo *bar **baz**
  3960. bim* bop**
  3961. .
  3962. <p><strong>foo <em>bar <strong>baz</strong>
  3963. bim</em> bop</strong></p>
  3964. .
  3965. .
  3966. **foo [*bar*](/url)**
  3967. .
  3968. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  3969. .
  3970. There can be no empty emphasis or strong emphasis:
  3971. .
  3972. __ is not an empty emphasis
  3973. .
  3974. <p>__ is not an empty emphasis</p>
  3975. .
  3976. .
  3977. ____ is not an empty strong emphasis
  3978. .
  3979. <p>____ is not an empty strong emphasis</p>
  3980. .
  3981. Rule 11:
  3982. .
  3983. foo ***
  3984. .
  3985. <p>foo ***</p>
  3986. .
  3987. .
  3988. foo *\**
  3989. .
  3990. <p>foo <em>*</em></p>
  3991. .
  3992. .
  3993. foo *_*
  3994. .
  3995. <p>foo <em>_</em></p>
  3996. .
  3997. .
  3998. foo *****
  3999. .
  4000. <p>foo *****</p>
  4001. .
  4002. .
  4003. foo **\***
  4004. .
  4005. <p>foo <strong>*</strong></p>
  4006. .
  4007. .
  4008. foo **_**
  4009. .
  4010. <p>foo <strong>_</strong></p>
  4011. .
  4012. Note that when delimiters do not match evenly, Rule 11 determines
  4013. that the excess literal `*` characters will appear outside of the
  4014. emphasis, rather than inside it:
  4015. .
  4016. **foo*
  4017. .
  4018. <p>*<em>foo</em></p>
  4019. .
  4020. .
  4021. *foo**
  4022. .
  4023. <p><em>foo</em>*</p>
  4024. .
  4025. .
  4026. ***foo**
  4027. .
  4028. <p>*<strong>foo</strong></p>
  4029. .
  4030. .
  4031. ****foo*
  4032. .
  4033. <p>***<em>foo</em></p>
  4034. .
  4035. .
  4036. **foo***
  4037. .
  4038. <p><strong>foo</strong>*</p>
  4039. .
  4040. .
  4041. *foo****
  4042. .
  4043. <p><em>foo</em>***</p>
  4044. .
  4045. Rule 12:
  4046. .
  4047. foo ___
  4048. .
  4049. <p>foo ___</p>
  4050. .
  4051. .
  4052. foo _\__
  4053. .
  4054. <p>foo <em>_</em></p>
  4055. .
  4056. .
  4057. foo _*_
  4058. .
  4059. <p>foo <em>*</em></p>
  4060. .
  4061. .
  4062. foo _____
  4063. .
  4064. <p>foo _____</p>
  4065. .
  4066. .
  4067. foo __\___
  4068. .
  4069. <p>foo <strong>_</strong></p>
  4070. .
  4071. .
  4072. foo __*__
  4073. .
  4074. <p>foo <strong>*</strong></p>
  4075. .
  4076. .
  4077. __foo_
  4078. .
  4079. <p>_<em>foo</em></p>
  4080. .
  4081. Note that when delimiters do not match evenly, Rule 12 determines
  4082. that the excess literal `_` characters will appear outside of the
  4083. emphasis, rather than inside it:
  4084. .
  4085. _foo__
  4086. .
  4087. <p><em>foo</em>_</p>
  4088. .
  4089. .
  4090. ___foo__
  4091. .
  4092. <p>_<strong>foo</strong></p>
  4093. .
  4094. .
  4095. ____foo_
  4096. .
  4097. <p>___<em>foo</em></p>
  4098. .
  4099. .
  4100. __foo___
  4101. .
  4102. <p><strong>foo</strong>_</p>
  4103. .
  4104. .
  4105. _foo____
  4106. .
  4107. <p><em>foo</em>___</p>
  4108. .
  4109. Rule 13 implies that if you want emphasis nested directly inside
  4110. emphasis, you must use different delimiters:
  4111. .
  4112. **foo**
  4113. .
  4114. <p><strong>foo</strong></p>
  4115. .
  4116. .
  4117. *_foo_*
  4118. .
  4119. <p><em><em>foo</em></em></p>
  4120. .
  4121. .
  4122. __foo__
  4123. .
  4124. <p><strong>foo</strong></p>
  4125. .
  4126. .
  4127. _*foo*_
  4128. .
  4129. <p><em><em>foo</em></em></p>
  4130. .
  4131. However, strong emphasis within strong emphasisis possible without
  4132. switching delimiters:
  4133. .
  4134. ****foo****
  4135. .
  4136. <p><strong><strong>foo</strong></strong></p>
  4137. .
  4138. .
  4139. ____foo____
  4140. .
  4141. <p><strong><strong>foo</strong></strong></p>
  4142. .
  4143. Rule 13 can be applied to arbitrarily long sequences of
  4144. delimiters:
  4145. .
  4146. ******foo******
  4147. .
  4148. <p><strong><strong><strong>foo</strong></strong></strong></p>
  4149. .
  4150. Rule 14:
  4151. .
  4152. ***foo***
  4153. .
  4154. <p><strong><em>foo</em></strong></p>
  4155. .
  4156. .
  4157. _____foo_____
  4158. .
  4159. <p><strong><strong><em>foo</em></strong></strong></p>
  4160. .
  4161. Rule 15:
  4162. .
  4163. *foo _bar* baz_
  4164. .
  4165. <p><em>foo _bar</em> baz_</p>
  4166. .
  4167. .
  4168. **foo*bar**
  4169. .
  4170. <p><em><em>foo</em>bar</em>*</p>
  4171. .
  4172. Rule 16:
  4173. .
  4174. **foo **bar baz**
  4175. .
  4176. <p>**foo <strong>bar baz</strong></p>
  4177. .
  4178. .
  4179. *foo *bar baz*
  4180. .
  4181. <p>*foo <em>bar baz</em></p>
  4182. .
  4183. Rule 17:
  4184. .
  4185. *[bar*](/url)
  4186. .
  4187. <p>*<a href="/url">bar*</a></p>
  4188. .
  4189. .
  4190. _foo [bar_](/url)
  4191. .
  4192. <p>_foo <a href="/url">bar_</a></p>
  4193. .
  4194. .
  4195. *<img src="foo" title="*"/>
  4196. .
  4197. <p>*<img src="foo" title="*"/></p>
  4198. .
  4199. .
  4200. **<a href="**">
  4201. .
  4202. <p>**<a href="**"></p>
  4203. .
  4204. .
  4205. __<a href="__">
  4206. .
  4207. <p>__<a href="__"></p>
  4208. .
  4209. .
  4210. *a `*`*
  4211. .
  4212. <p><em>a <code>*</code></em></p>
  4213. .
  4214. .
  4215. _a `_`_
  4216. .
  4217. <p><em>a <code>_</code></em></p>
  4218. .
  4219. .
  4220. **a<http://foo.bar?q=**>
  4221. .
  4222. <p>**a<a href="http://foo.bar?q=**">http://foo.bar?q=**</a></p>
  4223. .
  4224. .
  4225. __a<http://foo.bar?q=__>
  4226. .
  4227. <p>__a<a href="http://foo.bar?q=__">http://foo.bar?q=__</a></p>
  4228. .
  4229. ## Links
  4230. A link contains [link text](#link-label) (the visible text),
  4231. a [destination](#destination) (the URI that is the link destination),
  4232. and optionally a [link title](#link-title). There are two basic kinds
  4233. of links in Markdown. In [inline links](#inline-links) the destination
  4234. and title are given immediately after the link text. In [reference
  4235. links](#reference-links) the destination and title are defined elsewhere
  4236. in the document.
  4237. A [link text](@link-text) consists of a sequence of zero or more
  4238. inline elements enclosed by square brackets (`[` and `]`). The
  4239. following rules apply:
  4240. - Links may not contain other links, at any level of nesting.
  4241. - Brackets are allowed in the [link text](#link-text) only if (a) they
  4242. are backslash-escaped or (b) they appear as a matched pair of brackets,
  4243. with an open bracket `[`, a sequence of zero or more inlines, and
  4244. a close bracket `]`.
  4245. - Backtick [code spans](#code-span), [autolinks](#autolink), and
  4246. raw [HTML tags](#html-tag) bind more tightly
  4247. than the brackets in link text. Thus, for example,
  4248. `` [foo`]` `` could not be a link text, since the second `]`
  4249. is part of a code span.
  4250. - The brackets in link text bind more tightly than markers for
  4251. [emphasis and strong emphasis](#emphasis-and-strong-emphasis).
  4252. Thus, for example, `*[foo*](url)` is a link.
  4253. A [link destination](@link-destination) consists of either
  4254. - a sequence of zero or more characters between an opening `<` and a
  4255. closing `>` that contains no line breaks or unescaped `<` or `>`
  4256. characters, or
  4257. - a nonempty sequence of characters that does not include
  4258. ASCII space or control characters, and includes parentheses
  4259. only if (a) they are backslash-escaped or (b) they are part of
  4260. a balanced pair of unescaped parentheses that is not itself
  4261. inside a balanced pair of unescaped paretheses.
  4262. A [link title](@link-title) consists of either
  4263. - a sequence of zero or more characters between straight double-quote
  4264. characters (`"`), including a `"` character only if it is
  4265. backslash-escaped, or
  4266. - a sequence of zero or more characters between straight single-quote
  4267. characters (`'`), including a `'` character only if it is
  4268. backslash-escaped, or
  4269. - a sequence of zero or more characters between matching parentheses
  4270. (`(...)`), including a `)` character only if it is backslash-escaped.
  4271. An [inline link](@inline-link)
  4272. consists of a [link text](#link-text) followed immediately
  4273. by a left parenthesis `(`, optional whitespace,
  4274. an optional [link destination](#link-destination),
  4275. an optional [link title](#link-title) separated from the link
  4276. destination by whitespace, optional whitespace, and a right
  4277. parenthesis `)`. The link's text consists of the inlines contained
  4278. in the [link text](#link-text) (excluding the enclosing square brackets).
  4279. The link's URI consists of the link destination, excluding enclosing
  4280. `<...>` if present, with backslash-escapes in effect as described
  4281. above. The link's title consists of the link title, excluding its
  4282. enclosing delimiters, with backslash-escapes in effect as described
  4283. above.
  4284. Here is a simple inline link:
  4285. .
  4286. [link](/uri "title")
  4287. .
  4288. <p><a href="/uri" title="title">link</a></p>
  4289. .
  4290. The title may be omitted:
  4291. .
  4292. [link](/uri)
  4293. .
  4294. <p><a href="/uri">link</a></p>
  4295. .
  4296. Both the title and the destination may be omitted:
  4297. .
  4298. [link]()
  4299. .
  4300. <p><a href="">link</a></p>
  4301. .
  4302. .
  4303. [link](<>)
  4304. .
  4305. <p><a href="">link</a></p>
  4306. .
  4307. If the destination contains spaces, it must be enclosed in pointy
  4308. braces:
  4309. .
  4310. [link](/my uri)
  4311. .
  4312. <p>[link](/my uri)</p>
  4313. .
  4314. .
  4315. [link](</my uri>)
  4316. .
  4317. <p><a href="/my%20uri">link</a></p>
  4318. .
  4319. The destination cannot contain line breaks, even with pointy braces:
  4320. .
  4321. [link](foo
  4322. bar)
  4323. .
  4324. <p>[link](foo
  4325. bar)</p>
  4326. .
  4327. One level of balanced parentheses is allowed without escaping:
  4328. .
  4329. [link]((foo)and(bar))
  4330. .
  4331. <p><a href="(foo)and(bar)">link</a></p>
  4332. .
  4333. However, if you have parentheses within parentheses, you need to escape
  4334. or use the `<...>` form:
  4335. .
  4336. [link](foo(and(bar)))
  4337. .
  4338. <p>[link](foo(and(bar)))</p>
  4339. .
  4340. .
  4341. [link](foo(and\(bar\)))
  4342. .
  4343. <p><a href="foo(and(bar))">link</a></p>
  4344. .
  4345. .
  4346. [link](<foo(and(bar))>)
  4347. .
  4348. <p><a href="foo(and(bar))">link</a></p>
  4349. .
  4350. Parentheses and other symbols can also be escaped, as usual
  4351. in Markdown:
  4352. .
  4353. [link](foo\)\:)
  4354. .
  4355. <p><a href="foo):">link</a></p>
  4356. .
  4357. URL-escaping should be left alone inside the destination, as all
  4358. URL-escaped characters are also valid URL characters. HTML entities in
  4359. the destination will be parsed into their UTF-8 codepoints, as usual, and
  4360. optionally URL-escaped when written as HTML.
  4361. .
  4362. [link](foo%20b&auml;)
  4363. .
  4364. <p><a href="foo%20b%C3%A4">link</a></p>
  4365. .
  4366. Note that, because titles can often be parsed as destinations,
  4367. if you try to omit the destination and keep the title, you'll
  4368. get unexpected results:
  4369. .
  4370. [link]("title")
  4371. .
  4372. <p><a href="%22title%22">link</a></p>
  4373. .
  4374. Titles may be in single quotes, double quotes, or parentheses:
  4375. .
  4376. [link](/url "title")
  4377. [link](/url 'title')
  4378. [link](/url (title))
  4379. .
  4380. <p><a href="/url" title="title">link</a>
  4381. <a href="/url" title="title">link</a>
  4382. <a href="/url" title="title">link</a></p>
  4383. .
  4384. Backslash escapes and entities may be used in titles:
  4385. .
  4386. [link](/url "title \"&quot;")
  4387. .
  4388. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  4389. .
  4390. Nested balanced quotes are not allowed without escaping:
  4391. .
  4392. [link](/url "title "and" title")
  4393. .
  4394. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  4395. .
  4396. But it is easy to work around this by using a different quote type:
  4397. .
  4398. [link](/url 'title "and" title')
  4399. .
  4400. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  4401. .
  4402. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  4403. title, and its test suite included a test demonstrating this.
  4404. But it is hard to see a good rationale for the extra complexity this
  4405. brings, since there are already many ways---backslash escaping,
  4406. entities, or using a different quote type for the enclosing title---to
  4407. write titles containing double quotes. `Markdown.pl`'s handling of
  4408. titles has a number of other strange features. For example, it allows
  4409. single-quoted titles in inline links, but not reference links. And, in
  4410. reference links but not inline links, it allows a title to begin with
  4411. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  4412. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  4413. a simple, rational rule that works the same way in inline links and
  4414. link reference definitions.)
  4415. Whitespace is allowed around the destination and title:
  4416. .
  4417. [link]( /uri
  4418. "title" )
  4419. .
  4420. <p><a href="/uri" title="title">link</a></p>
  4421. .
  4422. But it is not allowed between the link text and the
  4423. following parenthesis:
  4424. .
  4425. [link] (/uri)
  4426. .
  4427. <p>[link] (/uri)</p>
  4428. .
  4429. The link text may contain balanced brackets, but not unbalanced ones,
  4430. unless they are escaped:
  4431. .
  4432. [link [foo [bar]]](/uri)
  4433. .
  4434. <p><a href="/uri">link [foo [bar]]</a></p>
  4435. .
  4436. .
  4437. [link] bar](/uri)
  4438. .
  4439. <p>[link] bar](/uri)</p>
  4440. .
  4441. .
  4442. [link [bar](/uri)
  4443. .
  4444. <p>[link <a href="/uri">bar</a></p>
  4445. .
  4446. .
  4447. [link \[bar](/uri)
  4448. .
  4449. <p><a href="/uri">link [bar</a></p>
  4450. .
  4451. The link text may contain inline content:
  4452. .
  4453. [link *foo **bar** `#`*](/uri)
  4454. .
  4455. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4456. .
  4457. .
  4458. [![moon](moon.jpg)](/uri)
  4459. .
  4460. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4461. .
  4462. However, links may not contain other links, at any level of nesting.
  4463. .
  4464. [foo [bar](/uri)](/uri)
  4465. .
  4466. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  4467. .
  4468. .
  4469. [foo *[bar [baz](/uri)](/uri)*](/uri)
  4470. .
  4471. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  4472. .
  4473. These cases illustrate the precedence of link text grouping over
  4474. emphasis grouping:
  4475. .
  4476. *[foo*](/uri)
  4477. .
  4478. <p>*<a href="/uri">foo*</a></p>
  4479. .
  4480. .
  4481. [foo *bar](baz*)
  4482. .
  4483. <p><a href="baz*">foo *bar</a></p>
  4484. .
  4485. These cases illustrate the precedence of HTML tags, code spans,
  4486. and autolinks over link grouping:
  4487. .
  4488. [foo <bar attr="](baz)">
  4489. .
  4490. <p>[foo <bar attr="](baz)"></p>
  4491. .
  4492. .
  4493. [foo`](/uri)`
  4494. .
  4495. <p>[foo<code>](/uri)</code></p>
  4496. .
  4497. .
  4498. [foo<http://example.com?search=](uri)>
  4499. .
  4500. <p>[foo<a href="http://example.com?search=%5D(uri)">http://example.com?search=](uri)</a></p>
  4501. .
  4502. There are three kinds of [reference links](@reference-link):
  4503. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  4504. and [shortcut](#shortcut-reference-link).
  4505. A [full reference link](@full-reference-link)
  4506. consists of a [link text](#link-text), optional whitespace, and
  4507. a [link label](#link-label) that [matches](#matches) a
  4508. [link reference definition](#link-reference-definition) elsewhere in the
  4509. document.
  4510. A [link label](@link-label) begins with a left bracket (`[`) and ends
  4511. with the first right bracket (`]`) that is not backslash-escaped.
  4512. Unescaped square bracket characters are not allowed in
  4513. [link labels](#link-label). A link label can have at most 999
  4514. characters inside the square brackets.
  4515. One label [matches](@matches)
  4516. another just in case their normalized forms are equal. To normalize a
  4517. label, perform the *unicode case fold* and collapse consecutive internal
  4518. whitespace to a single space. If there are multiple matching reference
  4519. link definitions, the one that comes first in the document is used. (It
  4520. is desirable in such cases to emit a warning.)
  4521. The contents of the first link label are parsed as inlines, which are
  4522. used as the link's text. The link's URI and title are provided by the
  4523. matching [link reference definition](#link-reference-definition).
  4524. Here is a simple example:
  4525. .
  4526. [foo][bar]
  4527. [bar]: /url "title"
  4528. .
  4529. <p><a href="/url" title="title">foo</a></p>
  4530. .
  4531. The rules for the [link text](#link-text) are the same as with
  4532. [inline links](#inline-link). Thus:
  4533. The link text may contain balanced brackets, but not unbalanced ones,
  4534. unless they are escaped:
  4535. .
  4536. [link [foo [bar]]][ref]
  4537. [ref]: /uri
  4538. .
  4539. <p><a href="/uri">link [foo [bar]]</a></p>
  4540. .
  4541. .
  4542. [link \[bar][ref]
  4543. [ref]: /uri
  4544. .
  4545. <p><a href="/uri">link [bar</a></p>
  4546. .
  4547. The link text may contain inline content:
  4548. .
  4549. [link *foo **bar** `#`*][ref]
  4550. [ref]: /uri
  4551. .
  4552. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4553. .
  4554. .
  4555. [![moon](moon.jpg)][ref]
  4556. [ref]: /uri
  4557. .
  4558. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4559. .
  4560. However, links may not contain other links, at any level of nesting.
  4561. .
  4562. [foo [bar](/uri)][ref]
  4563. [ref]: /uri
  4564. .
  4565. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  4566. .
  4567. .
  4568. [foo *bar [baz][ref]*][ref]
  4569. [ref]: /uri
  4570. .
  4571. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  4572. .
  4573. (In the examples above, we have two [shortcut reference
  4574. links](#shortcut-reference-link) instead of one [full reference
  4575. link](#full-reference-link).)
  4576. The following cases illustrate the precedence of link text grouping over
  4577. emphasis grouping:
  4578. .
  4579. *[foo*][ref]
  4580. [ref]: /uri
  4581. .
  4582. <p>*<a href="/uri">foo*</a></p>
  4583. .
  4584. .
  4585. [foo *bar][ref]
  4586. [ref]: /uri
  4587. .
  4588. <p><a href="/uri">foo *bar</a></p>
  4589. .
  4590. These cases illustrate the precedence of HTML tags, code spans,
  4591. and autolinks over link grouping:
  4592. .
  4593. [foo <bar attr="][ref]">
  4594. [ref]: /uri
  4595. .
  4596. <p>[foo <bar attr="][ref]"></p>
  4597. .
  4598. .
  4599. [foo`][ref]`
  4600. [ref]: /uri
  4601. .
  4602. <p>[foo<code>][ref]</code></p>
  4603. .
  4604. .
  4605. [foo<http://example.com?search=][ref]>
  4606. [ref]: /uri
  4607. .
  4608. <p>[foo<a href="http://example.com?search=%5D%5Bref%5D">http://example.com?search=][ref]</a></p>
  4609. .
  4610. Matching is case-insensitive:
  4611. .
  4612. [foo][BaR]
  4613. [bar]: /url "title"
  4614. .
  4615. <p><a href="/url" title="title">foo</a></p>
  4616. .
  4617. Unicode case fold is used:
  4618. .
  4619. [Толпой][Толпой] is a Russian word.
  4620. [ТОЛПОЙ]: /url
  4621. .
  4622. <p><a href="/url">Толпой</a> is a Russian word.</p>
  4623. .
  4624. Consecutive internal whitespace is treated as one space for
  4625. purposes of determining matching:
  4626. .
  4627. [Foo
  4628. bar]: /url
  4629. [Baz][Foo bar]
  4630. .
  4631. <p><a href="/url">Baz</a></p>
  4632. .
  4633. There can be whitespace between the [link text](#link-text) and the
  4634. [link label](#link-label):
  4635. .
  4636. [foo] [bar]
  4637. [bar]: /url "title"
  4638. .
  4639. <p><a href="/url" title="title">foo</a></p>
  4640. .
  4641. .
  4642. [foo]
  4643. [bar]
  4644. [bar]: /url "title"
  4645. .
  4646. <p><a href="/url" title="title">foo</a></p>
  4647. .
  4648. When there are multiple matching [link reference
  4649. definitions](#link-reference-definition), the first is used:
  4650. .
  4651. [foo]: /url1
  4652. [foo]: /url2
  4653. [bar][foo]
  4654. .
  4655. <p><a href="/url1">bar</a></p>
  4656. .
  4657. Note that matching is performed on normalized strings, not parsed
  4658. inline content. So the following does not match, even though the
  4659. labels define equivalent inline content:
  4660. .
  4661. [bar][foo\!]
  4662. [foo!]: /url
  4663. .
  4664. <p>[bar][foo!]</p>
  4665. .
  4666. [Link labels](#link-label) cannot contain brackets, unless they are
  4667. backslash-escaped:
  4668. .
  4669. [foo][ref[]
  4670. [ref[]: /uri
  4671. .
  4672. <p>[foo][ref[]</p>
  4673. <p>[ref[]: /uri</p>
  4674. .
  4675. .
  4676. [foo][ref[bar]]
  4677. [ref[bar]]: /uri
  4678. .
  4679. <p>[foo][ref[bar]]</p>
  4680. <p>[ref[bar]]: /uri</p>
  4681. .
  4682. .
  4683. [[[foo]]]
  4684. [[[foo]]]: /url
  4685. .
  4686. <p>[[[foo]]]</p>
  4687. <p>[[[foo]]]: /url</p>
  4688. .
  4689. .
  4690. [foo][ref\[]
  4691. [ref\[]: /uri
  4692. .
  4693. <p><a href="/uri">foo</a></p>
  4694. .
  4695. A [collapsed reference link](@collapsed-reference-link)
  4696. consists of a [link
  4697. label](#link-label) that [matches](#matches) a [link reference
  4698. definition](#link-reference-definition) elsewhere in the
  4699. document, optional whitespace, and the string `[]`. The contents of the
  4700. first link label are parsed as inlines, which are used as the link's
  4701. text. The link's URI and title are provided by the matching reference
  4702. link definition. Thus, `[foo][]` is equivalent to `[foo][foo]`.
  4703. .
  4704. [foo][]
  4705. [foo]: /url "title"
  4706. .
  4707. <p><a href="/url" title="title">foo</a></p>
  4708. .
  4709. .
  4710. [*foo* bar][]
  4711. [*foo* bar]: /url "title"
  4712. .
  4713. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4714. .
  4715. The link labels are case-insensitive:
  4716. .
  4717. [Foo][]
  4718. [foo]: /url "title"
  4719. .
  4720. <p><a href="/url" title="title">Foo</a></p>
  4721. .
  4722. As with full reference links, whitespace is allowed
  4723. between the two sets of brackets:
  4724. .
  4725. [foo]
  4726. []
  4727. [foo]: /url "title"
  4728. .
  4729. <p><a href="/url" title="title">foo</a></p>
  4730. .
  4731. A [shortcut reference link](@shortcut-reference-link)
  4732. consists of a [link
  4733. label](#link-label) that [matches](#matches) a [link reference
  4734. definition](#link-reference-definition) elsewhere in the
  4735. document and is not followed by `[]` or a link label.
  4736. The contents of the first link label are parsed as inlines,
  4737. which are used as the link's text. the link's URI and title
  4738. are provided by the matching link reference definition.
  4739. Thus, `[foo]` is equivalent to `[foo][]`.
  4740. .
  4741. [foo]
  4742. [foo]: /url "title"
  4743. .
  4744. <p><a href="/url" title="title">foo</a></p>
  4745. .
  4746. .
  4747. [*foo* bar]
  4748. [*foo* bar]: /url "title"
  4749. .
  4750. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4751. .
  4752. .
  4753. [[*foo* bar]]
  4754. [*foo* bar]: /url "title"
  4755. .
  4756. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  4757. .
  4758. The link labels are case-insensitive:
  4759. .
  4760. [Foo]
  4761. [foo]: /url "title"
  4762. .
  4763. <p><a href="/url" title="title">Foo</a></p>
  4764. .
  4765. A space after the link text should be preserved:
  4766. .
  4767. [foo] bar
  4768. [foo]: /url
  4769. .
  4770. <p><a href="/url">foo</a> bar</p>
  4771. .
  4772. If you just want bracketed text, you can backslash-escape the
  4773. opening bracket to avoid links:
  4774. .
  4775. \[foo]
  4776. [foo]: /url "title"
  4777. .
  4778. <p>[foo]</p>
  4779. .
  4780. Note that this is a link, because a link label ends with the first
  4781. following closing bracket:
  4782. .
  4783. [foo*]: /url
  4784. *[foo*]
  4785. .
  4786. <p>*<a href="/url">foo*</a></p>
  4787. .
  4788. This is a link too, for the same reason:
  4789. .
  4790. [foo`]: /url
  4791. [foo`]`
  4792. .
  4793. <p>[foo<code>]</code></p>
  4794. .
  4795. Full references take precedence over shortcut references:
  4796. .
  4797. [foo][bar]
  4798. [foo]: /url1
  4799. [bar]: /url2
  4800. .
  4801. <p><a href="/url2">foo</a></p>
  4802. .
  4803. In the following case `[bar][baz]` is parsed as a reference,
  4804. `[foo]` as normal text:
  4805. .
  4806. [foo][bar][baz]
  4807. [baz]: /url
  4808. .
  4809. <p>[foo]<a href="/url">bar</a></p>
  4810. .
  4811. Here, though, `[foo][bar]` is parsed as a reference, since
  4812. `[bar]` is defined:
  4813. .
  4814. [foo][bar][baz]
  4815. [baz]: /url1
  4816. [bar]: /url2
  4817. .
  4818. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  4819. .
  4820. Here `[foo]` is not parsed as a shortcut reference, because it
  4821. is followed by a link label (even though `[bar]` is not defined):
  4822. .
  4823. [foo][bar][baz]
  4824. [baz]: /url1
  4825. [foo]: /url2
  4826. .
  4827. <p>[foo]<a href="/url1">bar</a></p>
  4828. .
  4829. ## Images
  4830. Syntax for images is like the syntax for links, with one
  4831. difference. Instead of [link text](#link-text), we have an [image
  4832. description](@image-description). The rules for this are the
  4833. same as for [link text](#link-text), except that (a) an
  4834. image description starts with `![` rather than `[`, and
  4835. (b) an image description may contain links.
  4836. An image description has inline elements
  4837. as its contents. When an image is rendered to HTML,
  4838. this is standardly used as the image's `alt` attribute.
  4839. .
  4840. ![foo](/url "title")
  4841. .
  4842. <p><img src="/url" alt="foo" title="title" /></p>
  4843. .
  4844. .
  4845. ![foo *bar*]
  4846. [foo *bar*]: train.jpg "train & tracks"
  4847. .
  4848. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4849. .
  4850. .
  4851. ![foo ![bar](/url)](/url2)
  4852. .
  4853. <p><img src="/url2" alt="foo bar" /></p>
  4854. .
  4855. .
  4856. ![foo [bar](/url)](/url2)
  4857. .
  4858. <p><img src="/url2" alt="foo bar" /></p>
  4859. .
  4860. Though this spec is concerned with parsing, not rendering, it is
  4861. recommended that in rendering to HTML, only the plain string content
  4862. of the [image description](#image-description) be used. Note that in
  4863. the above example, the alt attribute's value is `foo bar`, not `foo
  4864. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  4865. content is rendered, without formatting.
  4866. .
  4867. ![foo *bar*][]
  4868. [foo *bar*]: train.jpg "train & tracks"
  4869. .
  4870. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4871. .
  4872. .
  4873. ![foo *bar*][foobar]
  4874. [FOOBAR]: train.jpg "train & tracks"
  4875. .
  4876. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4877. .
  4878. .
  4879. ![foo](train.jpg)
  4880. .
  4881. <p><img src="train.jpg" alt="foo" /></p>
  4882. .
  4883. .
  4884. My ![foo bar](/path/to/train.jpg "title" )
  4885. .
  4886. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  4887. .
  4888. .
  4889. ![foo](<url>)
  4890. .
  4891. <p><img src="url" alt="foo" /></p>
  4892. .
  4893. .
  4894. ![](/url)
  4895. .
  4896. <p><img src="/url" alt="" /></p>
  4897. .
  4898. Reference-style:
  4899. .
  4900. ![foo] [bar]
  4901. [bar]: /url
  4902. .
  4903. <p><img src="/url" alt="foo" /></p>
  4904. .
  4905. .
  4906. ![foo] [bar]
  4907. [BAR]: /url
  4908. .
  4909. <p><img src="/url" alt="foo" /></p>
  4910. .
  4911. Collapsed:
  4912. .
  4913. ![foo][]
  4914. [foo]: /url "title"
  4915. .
  4916. <p><img src="/url" alt="foo" title="title" /></p>
  4917. .
  4918. .
  4919. ![*foo* bar][]
  4920. [*foo* bar]: /url "title"
  4921. .
  4922. <p><img src="/url" alt="foo bar" title="title" /></p>
  4923. .
  4924. The labels are case-insensitive:
  4925. .
  4926. ![Foo][]
  4927. [foo]: /url "title"
  4928. .
  4929. <p><img src="/url" alt="Foo" title="title" /></p>
  4930. .
  4931. As with full reference links, whitespace is allowed
  4932. between the two sets of brackets:
  4933. .
  4934. ![foo]
  4935. []
  4936. [foo]: /url "title"
  4937. .
  4938. <p><img src="/url" alt="foo" title="title" /></p>
  4939. .
  4940. Shortcut:
  4941. .
  4942. ![foo]
  4943. [foo]: /url "title"
  4944. .
  4945. <p><img src="/url" alt="foo" title="title" /></p>
  4946. .
  4947. .
  4948. ![*foo* bar]
  4949. [*foo* bar]: /url "title"
  4950. .
  4951. <p><img src="/url" alt="foo bar" title="title" /></p>
  4952. .
  4953. Note that link labels cannot contain unescaped brackets:
  4954. .
  4955. ![[foo]]
  4956. [[foo]]: /url "title"
  4957. .
  4958. <p>![[foo]]</p>
  4959. <p>[[foo]]: /url &quot;title&quot;</p>
  4960. .
  4961. The link labels are case-insensitive:
  4962. .
  4963. ![Foo]
  4964. [foo]: /url "title"
  4965. .
  4966. <p><img src="/url" alt="Foo" title="title" /></p>
  4967. .
  4968. If you just want bracketed text, you can backslash-escape the
  4969. opening `!` and `[`:
  4970. .
  4971. \!\[foo]
  4972. [foo]: /url "title"
  4973. .
  4974. <p>![foo]</p>
  4975. .
  4976. If you want a link after a literal `!`, backslash-escape the
  4977. `!`:
  4978. .
  4979. \![foo]
  4980. [foo]: /url "title"
  4981. .
  4982. <p>!<a href="/url" title="title">foo</a></p>
  4983. .
  4984. ## Autolinks
  4985. [Autolinks](@autolink) are absolute URIs and email addresses inside `<` and `>`.
  4986. They are parsed as links, with the URL or email address as the link
  4987. label.
  4988. A [URI autolink](@uri-autolink)
  4989. consists of `<`, followed by an [absolute
  4990. URI](#absolute-uri) not containing `<`, followed by `>`. It is parsed
  4991. as a link to the URI, with the URI as the link's label.
  4992. An [absolute URI](@absolute-uri),
  4993. for these purposes, consists of a [scheme](#scheme) followed by a colon (`:`)
  4994. followed by zero or more characters other than ASCII whitespace and
  4995. control characters, `<`, and `>`. If the URI includes these characters,
  4996. you must use percent-encoding (e.g. `%20` for a space).
  4997. The following [schemes](@scheme)
  4998. are recognized (case-insensitive):
  4999. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  5000. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  5001. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  5002. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  5003. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  5004. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  5005. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  5006. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5007. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5008. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5009. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5010. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5011. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5012. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5013. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5014. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5015. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5016. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5017. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5018. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5019. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5020. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5021. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5022. Here are some valid autolinks:
  5023. .
  5024. <http://foo.bar.baz>
  5025. .
  5026. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5027. .
  5028. .
  5029. <http://foo.bar.baz?q=hello&id=22&boolean>
  5030. .
  5031. <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>
  5032. .
  5033. .
  5034. <irc://foo.bar:2233/baz>
  5035. .
  5036. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5037. .
  5038. Uppercase is also fine:
  5039. .
  5040. <MAILTO:FOO@BAR.BAZ>
  5041. .
  5042. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5043. .
  5044. Spaces are not allowed in autolinks:
  5045. .
  5046. <http://foo.bar/baz bim>
  5047. .
  5048. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5049. .
  5050. An [email autolink](@email-autolink)
  5051. consists of `<`, followed by an [email address](#email-address),
  5052. followed by `>`. The link's label is the email address,
  5053. and the URL is `mailto:` followed by the email address.
  5054. An [email address](@email-address),
  5055. for these purposes, is anything that matches
  5056. the [non-normative regex from the HTML5
  5057. spec](http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#e-mail-state-%28type=email%29):
  5058. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5059. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5060. Examples of email autolinks:
  5061. .
  5062. <foo@bar.example.com>
  5063. .
  5064. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5065. .
  5066. .
  5067. <foo+special@Bar.baz-bar0.com>
  5068. .
  5069. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5070. .
  5071. These are not autolinks:
  5072. .
  5073. <>
  5074. .
  5075. <p>&lt;&gt;</p>
  5076. .
  5077. .
  5078. <heck://bing.bong>
  5079. .
  5080. <p>&lt;heck://bing.bong&gt;</p>
  5081. .
  5082. .
  5083. < http://foo.bar >
  5084. .
  5085. <p>&lt; http://foo.bar &gt;</p>
  5086. .
  5087. .
  5088. <foo.bar.baz>
  5089. .
  5090. <p>&lt;foo.bar.baz&gt;</p>
  5091. .
  5092. .
  5093. <localhost:5001/foo>
  5094. .
  5095. <p>&lt;localhost:5001/foo&gt;</p>
  5096. .
  5097. .
  5098. http://example.com
  5099. .
  5100. <p>http://example.com</p>
  5101. .
  5102. .
  5103. foo@bar.example.com
  5104. .
  5105. <p>foo@bar.example.com</p>
  5106. .
  5107. ## Raw HTML
  5108. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5109. raw HTML tag and will be rendered in HTML without escaping.
  5110. Tag and attribute names are not limited to current HTML tags,
  5111. so custom tags (and even, say, DocBook tags) may be used.
  5112. Here is the grammar for tags:
  5113. A [tag name](@tag-name) consists of an ASCII letter
  5114. followed by zero or more ASCII letters or digits.
  5115. An [attribute](@attribute) consists of whitespace,
  5116. an [attribute name](#attribute-name), and an optional
  5117. [attribute value specification](#attribute-value-specification).
  5118. An [attribute name](@attribute-name)
  5119. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5120. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5121. specification restricted to ASCII. HTML5 is laxer.)
  5122. An [attribute value specification](@attribute-value-specification)
  5123. consists of optional whitespace,
  5124. a `=` character, optional whitespace, and an [attribute
  5125. value](#attribute-value).
  5126. An [attribute value](@attribute-value)
  5127. consists of an [unquoted attribute value](#unquoted-attribute-value),
  5128. a [single-quoted attribute value](#single-quoted-attribute-value),
  5129. or a [double-quoted attribute value](#double-quoted-attribute-value).
  5130. An [unquoted attribute value](@unquoted-attribute-value)
  5131. is a nonempty string of characters not
  5132. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5133. A [single-quoted attribute value](@single-quoted-attribute-value)
  5134. consists of `'`, zero or more
  5135. characters not including `'`, and a final `'`.
  5136. A [double-quoted attribute value](@double-quoted-attribute-value)
  5137. consists of `"`, zero or more
  5138. characters not including `"`, and a final `"`.
  5139. An [open tag](@open-tag) consists of a `<` character,
  5140. a [tag name](#tag-name), zero or more [attributes](#attribute),
  5141. optional whitespace, an optional `/` character, and a `>` character.
  5142. A [closing tag](@closing-tag) consists of the
  5143. string `</`, a [tag name](#tag-name), optional whitespace, and the
  5144. character `>`.
  5145. An [HTML comment](@html-comment) consists of the
  5146. string `<!--`, a string of characters not including the string `--`, and
  5147. the string `-->`.
  5148. A [processing instruction](@processing-instruction)
  5149. consists of the string `<?`, a string
  5150. of characters not including the string `?>`, and the string
  5151. `?>`.
  5152. A [declaration](@declaration) consists of the
  5153. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5154. whitespace, a string of characters not including the character `>`, and
  5155. the character `>`.
  5156. A [CDATA section](@cdata-section) consists of
  5157. the string `<![CDATA[`, a string of characters not including the string
  5158. `]]>`, and the string `]]>`.
  5159. An [HTML tag](@html-tag) consists of an [open
  5160. tag](#open-tag), a [closing tag](#closing-tag), an [HTML
  5161. comment](#html-comment), a [processing
  5162. instruction](#processing-instruction), an [element type
  5163. declaration](#element-type-declaration), or a [CDATA
  5164. section](#cdata-section).
  5165. Here are some simple open tags:
  5166. .
  5167. <a><bab><c2c>
  5168. .
  5169. <p><a><bab><c2c></p>
  5170. .
  5171. Empty elements:
  5172. .
  5173. <a/><b2/>
  5174. .
  5175. <p><a/><b2/></p>
  5176. .
  5177. Whitespace is allowed:
  5178. .
  5179. <a /><b2
  5180. data="foo" >
  5181. .
  5182. <p><a /><b2
  5183. data="foo" ></p>
  5184. .
  5185. With attributes:
  5186. .
  5187. <a foo="bar" bam = 'baz <em>"</em>'
  5188. _boolean zoop:33=zoop:33 />
  5189. .
  5190. <p><a foo="bar" bam = 'baz <em>"</em>'
  5191. _boolean zoop:33=zoop:33 /></p>
  5192. .
  5193. Illegal tag names, not parsed as HTML:
  5194. .
  5195. <33> <__>
  5196. .
  5197. <p>&lt;33&gt; &lt;__&gt;</p>
  5198. .
  5199. Illegal attribute names:
  5200. .
  5201. <a h*#ref="hi">
  5202. .
  5203. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5204. .
  5205. Illegal attribute values:
  5206. .
  5207. <a href="hi'> <a href=hi'>
  5208. .
  5209. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5210. .
  5211. Illegal whitespace:
  5212. .
  5213. < a><
  5214. foo><bar/ >
  5215. .
  5216. <p>&lt; a&gt;&lt;
  5217. foo&gt;&lt;bar/ &gt;</p>
  5218. .
  5219. Missing whitespace:
  5220. .
  5221. <a href='bar'title=title>
  5222. .
  5223. <p>&lt;a href='bar'title=title&gt;</p>
  5224. .
  5225. Closing tags:
  5226. .
  5227. </a>
  5228. </foo >
  5229. .
  5230. <p></a>
  5231. </foo ></p>
  5232. .
  5233. Illegal attributes in closing tag:
  5234. .
  5235. </a href="foo">
  5236. .
  5237. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5238. .
  5239. Comments:
  5240. .
  5241. foo <!-- this is a
  5242. comment - with hyphen -->
  5243. .
  5244. <p>foo <!-- this is a
  5245. comment - with hyphen --></p>
  5246. .
  5247. .
  5248. foo <!-- not a comment -- two hyphens -->
  5249. .
  5250. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5251. .
  5252. Processing instructions:
  5253. .
  5254. foo <?php echo $a; ?>
  5255. .
  5256. <p>foo <?php echo $a; ?></p>
  5257. .
  5258. Declarations:
  5259. .
  5260. foo <!ELEMENT br EMPTY>
  5261. .
  5262. <p>foo <!ELEMENT br EMPTY></p>
  5263. .
  5264. CDATA sections:
  5265. .
  5266. foo <![CDATA[>&<]]>
  5267. .
  5268. <p>foo <![CDATA[>&<]]></p>
  5269. .
  5270. Entities are preserved in HTML attributes:
  5271. .
  5272. <a href="&ouml;">
  5273. .
  5274. <p><a href="&ouml;"></p>
  5275. .
  5276. Backslash escapes do not work in HTML attributes:
  5277. .
  5278. <a href="\*">
  5279. .
  5280. <p><a href="\*"></p>
  5281. .
  5282. .
  5283. <a href="\"">
  5284. .
  5285. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5286. .
  5287. ## Hard line breaks
  5288. A line break (not in a code span or HTML tag) that is preceded
  5289. by two or more spaces and does not occur at the end of a block
  5290. is parsed as a [hard line break](@hard-line-break) (rendered
  5291. in HTML as a `<br />` tag):
  5292. .
  5293. foo
  5294. baz
  5295. .
  5296. <p>foo<br />
  5297. baz</p>
  5298. .
  5299. For a more visible alternative, a backslash before the newline may be
  5300. used instead of two spaces:
  5301. .
  5302. foo\
  5303. baz
  5304. .
  5305. <p>foo<br />
  5306. baz</p>
  5307. .
  5308. More than two spaces can be used:
  5309. .
  5310. foo
  5311. baz
  5312. .
  5313. <p>foo<br />
  5314. baz</p>
  5315. .
  5316. Leading spaces at the beginning of the next line are ignored:
  5317. .
  5318. foo
  5319. bar
  5320. .
  5321. <p>foo<br />
  5322. bar</p>
  5323. .
  5324. .
  5325. foo\
  5326. bar
  5327. .
  5328. <p>foo<br />
  5329. bar</p>
  5330. .
  5331. Line breaks can occur inside emphasis, links, and other constructs
  5332. that allow inline content:
  5333. .
  5334. *foo
  5335. bar*
  5336. .
  5337. <p><em>foo<br />
  5338. bar</em></p>
  5339. .
  5340. .
  5341. *foo\
  5342. bar*
  5343. .
  5344. <p><em>foo<br />
  5345. bar</em></p>
  5346. .
  5347. Line breaks do not occur inside code spans
  5348. .
  5349. `code
  5350. span`
  5351. .
  5352. <p><code>code span</code></p>
  5353. .
  5354. .
  5355. `code\
  5356. span`
  5357. .
  5358. <p><code>code\ span</code></p>
  5359. .
  5360. or HTML tags:
  5361. .
  5362. <a href="foo
  5363. bar">
  5364. .
  5365. <p><a href="foo
  5366. bar"></p>
  5367. .
  5368. .
  5369. <a href="foo\
  5370. bar">
  5371. .
  5372. <p><a href="foo\
  5373. bar"></p>
  5374. .
  5375. Hard line breaks are for separating inline content within a block.
  5376. Neither syntax for hard line breaks works at the end of a paragraph or
  5377. other block element:
  5378. .
  5379. foo\
  5380. .
  5381. <p>foo\</p>
  5382. .
  5383. .
  5384. foo
  5385. .
  5386. <p>foo</p>
  5387. .
  5388. .
  5389. ### foo\
  5390. .
  5391. <h3>foo\</h3>
  5392. .
  5393. .
  5394. ### foo
  5395. .
  5396. <h3>foo</h3>
  5397. .
  5398. ## Soft line breaks
  5399. A regular line break (not in a code span or HTML tag) that is not
  5400. preceded by two or more spaces is parsed as a softbreak. (A
  5401. softbreak may be rendered in HTML either as a newline or as a space.
  5402. The result will be the same in browsers. In the examples here, a
  5403. newline will be used.)
  5404. .
  5405. foo
  5406. baz
  5407. .
  5408. <p>foo
  5409. baz</p>
  5410. .
  5411. Spaces at the end of the line and beginning of the next line are
  5412. removed:
  5413. .
  5414. foo
  5415. baz
  5416. .
  5417. <p>foo
  5418. baz</p>
  5419. .
  5420. A conforming parser may render a soft line break in HTML either as a
  5421. line break or as a space.
  5422. A renderer may also provide an option to render soft line breaks
  5423. as hard line breaks.
  5424. ## Strings
  5425. Any characters not given an interpretation by the above rules will
  5426. be parsed as string content.
  5427. .
  5428. hello $.;'there
  5429. .
  5430. <p>hello $.;'there</p>
  5431. .
  5432. .
  5433. Foo χρῆν
  5434. .
  5435. <p>Foo χρῆν</p>
  5436. .
  5437. Internal spaces are preserved verbatim:
  5438. .
  5439. Multiple spaces
  5440. .
  5441. <p>Multiple spaces</p>
  5442. .
  5443. <!-- END TESTS -->
  5444. # Appendix A: A parsing strategy {-}
  5445. ## Overview {-}
  5446. Parsing has two phases:
  5447. 1. In the first phase, lines of input are consumed and the block
  5448. structure of the document---its division into paragraphs, block quotes,
  5449. list items, and so on---is constructed. Text is assigned to these
  5450. blocks but not parsed. Link reference definitions are parsed and a
  5451. map of links is constructed.
  5452. 2. In the second phase, the raw text contents of paragraphs and headers
  5453. are parsed into sequences of Markdown inline elements (strings,
  5454. code spans, links, emphasis, and so on), using the map of link
  5455. references constructed in phase 1.
  5456. ## The document tree {-}
  5457. At each point in processing, the document is represented as a tree of
  5458. **blocks**. The root of the tree is a `document` block. The `document`
  5459. may have any number of other blocks as **children**. These children
  5460. may, in turn, have other blocks as children. The last child of a block
  5461. is normally considered **open**, meaning that subsequent lines of input
  5462. can alter its contents. (Blocks that are not open are **closed**.)
  5463. Here, for example, is a possible document tree, with the open blocks
  5464. marked by arrows:
  5465. ``` tree
  5466. -> document
  5467. -> block_quote
  5468. paragraph
  5469. "Lorem ipsum dolor\nsit amet."
  5470. -> list (type=bullet tight=true bullet_char=-)
  5471. list_item
  5472. paragraph
  5473. "Qui *quodsi iracundia*"
  5474. -> list_item
  5475. -> paragraph
  5476. "aliquando id"
  5477. ```
  5478. ## How source lines alter the document tree {-}
  5479. Each line that is processed has an effect on this tree. The line is
  5480. analyzed and, depending on its contents, the document may be altered
  5481. in one or more of the following ways:
  5482. 1. One or more open blocks may be closed.
  5483. 2. One or more new blocks may be created as children of the
  5484. last open block.
  5485. 3. Text may be added to the last (deepest) open block remaining
  5486. on the tree.
  5487. Once a line has been incorporated into the tree in this way,
  5488. it can be discarded, so input can be read in a stream.
  5489. We can see how this works by considering how the tree above is
  5490. generated by four lines of Markdown:
  5491. ``` markdown
  5492. > Lorem ipsum dolor
  5493. sit amet.
  5494. > - Qui *quodsi iracundia*
  5495. > - aliquando id
  5496. ```
  5497. At the outset, our document model is just
  5498. ``` tree
  5499. -> document
  5500. ```
  5501. The first line of our text,
  5502. ``` markdown
  5503. > Lorem ipsum dolor
  5504. ```
  5505. causes a `block_quote` block to be created as a child of our
  5506. open `document` block, and a `paragraph` block as a child of
  5507. the `block_quote`. Then the text is added to the last open
  5508. block, the `paragraph`:
  5509. ``` tree
  5510. -> document
  5511. -> block_quote
  5512. -> paragraph
  5513. "Lorem ipsum dolor"
  5514. ```
  5515. The next line,
  5516. ``` markdown
  5517. sit amet.
  5518. ```
  5519. is a "lazy continuation" of the open `paragraph`, so it gets added
  5520. to the paragraph's text:
  5521. ``` tree
  5522. -> document
  5523. -> block_quote
  5524. -> paragraph
  5525. "Lorem ipsum dolor\nsit amet."
  5526. ```
  5527. The third line,
  5528. ``` markdown
  5529. > - Qui *quodsi iracundia*
  5530. ```
  5531. causes the `paragraph` block to be closed, and a new `list` block
  5532. opened as a child of the `block_quote`. A `list_item` is also
  5533. added as a child of the `list`, and a `paragraph` as a child of
  5534. the `list_item`. The text is then added to the new `paragraph`:
  5535. ``` tree
  5536. -> document
  5537. -> block_quote
  5538. paragraph
  5539. "Lorem ipsum dolor\nsit amet."
  5540. -> list (type=bullet tight=true bullet_char=-)
  5541. -> list_item
  5542. -> paragraph
  5543. "Qui *quodsi iracundia*"
  5544. ```
  5545. The fourth line,
  5546. ``` markdown
  5547. > - aliquando id
  5548. ```
  5549. causes the `list_item` (and its child the `paragraph`) to be closed,
  5550. and a new `list_item` opened up as child of the `list`. A `paragraph`
  5551. is added as a child of the new `list_item`, to contain the text.
  5552. We thus obtain the final tree:
  5553. ``` tree
  5554. -> document
  5555. -> block_quote
  5556. paragraph
  5557. "Lorem ipsum dolor\nsit amet."
  5558. -> list (type=bullet tight=true bullet_char=-)
  5559. list_item
  5560. paragraph
  5561. "Qui *quodsi iracundia*"
  5562. -> list_item
  5563. -> paragraph
  5564. "aliquando id"
  5565. ```
  5566. ## From block structure to the final document {-}
  5567. Once all of the input has been parsed, all open blocks are closed.
  5568. We then "walk the tree," visiting every node, and parse raw
  5569. string contents of paragraphs and headers as inlines. At this
  5570. point we have seen all the link reference definitions, so we can
  5571. resolve reference links as we go.
  5572. ``` tree
  5573. document
  5574. block_quote
  5575. paragraph
  5576. str "Lorem ipsum dolor"
  5577. softbreak
  5578. str "sit amet."
  5579. list (type=bullet tight=true bullet_char=-)
  5580. list_item
  5581. paragraph
  5582. str "Qui "
  5583. emph
  5584. str "quodsi iracundia"
  5585. list_item
  5586. paragraph
  5587. str "aliquando id"
  5588. ```
  5589. Notice how the newline in the first paragraph has been parsed as
  5590. a `softbreak`, and the asterisks in the first list item have become
  5591. an `emph`.
  5592. The document can be rendered as HTML, or in any other format, given
  5593. an appropriate renderer.