aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 0693ba491460c8fcb3b4b02961d3f8867a53394b (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><hr />
  374. </li>
  375. </ul>
  376. .
  377. ## ATX headers
  378. An [ATX header](@atx-header)
  379. consists of a string of characters, parsed as inline content, between an
  380. opening sequence of 1--6 unescaped `#` characters and an optional
  381. closing sequence of any number of `#` characters. The opening sequence
  382. of `#` characters cannot be followed directly by a nonspace character.
  383. The optional closing sequence of `#`s must be preceded by a space and may be
  384. followed by spaces only. The opening `#` character may be indented 0-3
  385. spaces. The raw contents of the header are stripped of leading and
  386. trailing spaces before being parsed as inline content. The header level
  387. is equal to the number of `#` characters in the opening sequence.
  388. Simple headers:
  389. .
  390. # foo
  391. ## foo
  392. ### foo
  393. #### foo
  394. ##### foo
  395. ###### foo
  396. .
  397. <h1>foo</h1>
  398. <h2>foo</h2>
  399. <h3>foo</h3>
  400. <h4>foo</h4>
  401. <h5>foo</h5>
  402. <h6>foo</h6>
  403. .
  404. More than six `#` characters is not a header:
  405. .
  406. ####### foo
  407. .
  408. <p>####### foo</p>
  409. .
  410. A space is required between the `#` characters and the header's
  411. contents. Note that many implementations currently do not require
  412. the space. However, the space was required by the [original ATX
  413. implementation](http://www.aaronsw.com/2002/atx/atx.py), and it helps
  414. prevent things like the following from being parsed as headers:
  415. .
  416. #5 bolt
  417. .
  418. <p>#5 bolt</p>
  419. .
  420. This is not a header, because the first `#` is escaped:
  421. .
  422. \## foo
  423. .
  424. <p>## foo</p>
  425. .
  426. Contents are parsed as inlines:
  427. .
  428. # foo *bar* \*baz\*
  429. .
  430. <h1>foo <em>bar</em> *baz*</h1>
  431. .
  432. Leading and trailing blanks are ignored in parsing inline content:
  433. .
  434. # foo
  435. .
  436. <h1>foo</h1>
  437. .
  438. One to three spaces indentation are allowed:
  439. .
  440. ### foo
  441. ## foo
  442. # foo
  443. .
  444. <h3>foo</h3>
  445. <h2>foo</h2>
  446. <h1>foo</h1>
  447. .
  448. Four spaces are too much:
  449. .
  450. # foo
  451. .
  452. <pre><code># foo
  453. </code></pre>
  454. .
  455. .
  456. foo
  457. # bar
  458. .
  459. <p>foo
  460. # bar</p>
  461. .
  462. A closing sequence of `#` characters is optional:
  463. .
  464. ## foo ##
  465. ### bar ###
  466. .
  467. <h2>foo</h2>
  468. <h3>bar</h3>
  469. .
  470. It need not be the same length as the opening sequence:
  471. .
  472. # foo ##################################
  473. ##### foo ##
  474. .
  475. <h1>foo</h1>
  476. <h5>foo</h5>
  477. .
  478. Spaces are allowed after the closing sequence:
  479. .
  480. ### foo ###
  481. .
  482. <h3>foo</h3>
  483. .
  484. A sequence of `#` characters with a nonspace character following it
  485. is not a closing sequence, but counts as part of the contents of the
  486. header:
  487. .
  488. ### foo ### b
  489. .
  490. <h3>foo ### b</h3>
  491. .
  492. The closing sequence must be preceded by a space:
  493. .
  494. # foo#
  495. .
  496. <h1>foo#</h1>
  497. .
  498. Backslash-escaped `#` characters do not count as part
  499. of the closing sequence:
  500. .
  501. ### foo \###
  502. ## foo #\##
  503. # foo \#
  504. .
  505. <h3>foo ###</h3>
  506. <h2>foo ###</h2>
  507. <h1>foo #</h1>
  508. .
  509. ATX headers need not be separated from surrounding content by blank
  510. lines, and they can interrupt paragraphs:
  511. .
  512. ****
  513. ## foo
  514. ****
  515. .
  516. <hr />
  517. <h2>foo</h2>
  518. <hr />
  519. .
  520. .
  521. Foo bar
  522. # baz
  523. Bar foo
  524. .
  525. <p>Foo bar</p>
  526. <h1>baz</h1>
  527. <p>Bar foo</p>
  528. .
  529. ATX headers can be empty:
  530. .
  531. ##
  532. #
  533. ### ###
  534. .
  535. <h2></h2>
  536. <h1></h1>
  537. <h3></h3>
  538. .
  539. ## Setext headers
  540. A [setext header](@setext-header)
  541. consists of a line of text, containing at least one nonspace character,
  542. with no more than 3 spaces indentation, followed by a [setext header
  543. underline](#setext-header-underline). The line of text must be
  544. one that, were it not followed by the setext header underline,
  545. would be interpreted as part of a paragraph: it cannot be a code
  546. block, header, blockquote, horizontal rule, or list. A [setext header
  547. underline](@setext-header-underline)
  548. is a sequence of `=` characters or a sequence of `-` characters, with no
  549. more than 3 spaces indentation and any number of trailing
  550. spaces. The header is a level 1 header if `=` characters are used, and
  551. a level 2 header if `-` characters are used. The contents of the header
  552. are the result of parsing the first line as Markdown inline content.
  553. In general, a setext header need not be preceded or followed by a
  554. blank line. However, it cannot interrupt a paragraph, so when a
  555. setext header comes after a paragraph, a blank line is needed between
  556. them.
  557. Simple examples:
  558. .
  559. Foo *bar*
  560. =========
  561. Foo *bar*
  562. ---------
  563. .
  564. <h1>Foo <em>bar</em></h1>
  565. <h2>Foo <em>bar</em></h2>
  566. .
  567. The underlining can be any length:
  568. .
  569. Foo
  570. -------------------------
  571. Foo
  572. =
  573. .
  574. <h2>Foo</h2>
  575. <h1>Foo</h1>
  576. .
  577. The header content can be indented up to three spaces, and need
  578. not line up with the underlining:
  579. .
  580. Foo
  581. ---
  582. Foo
  583. -----
  584. Foo
  585. ===
  586. .
  587. <h2>Foo</h2>
  588. <h2>Foo</h2>
  589. <h1>Foo</h1>
  590. .
  591. Four spaces indent is too much:
  592. .
  593. Foo
  594. ---
  595. Foo
  596. ---
  597. .
  598. <pre><code>Foo
  599. ---
  600. Foo
  601. </code></pre>
  602. <hr />
  603. .
  604. The setext header underline can be indented up to three spaces, and
  605. may have trailing spaces:
  606. .
  607. Foo
  608. ----
  609. .
  610. <h2>Foo</h2>
  611. .
  612. Four spaces is too much:
  613. .
  614. Foo
  615. ---
  616. .
  617. <p>Foo
  618. ---</p>
  619. .
  620. The setext header underline cannot contain internal spaces:
  621. .
  622. Foo
  623. = =
  624. Foo
  625. --- -
  626. .
  627. <p>Foo
  628. = =</p>
  629. <p>Foo</p>
  630. <hr />
  631. .
  632. Trailing spaces in the content line do not cause a line break:
  633. .
  634. Foo
  635. -----
  636. .
  637. <h2>Foo</h2>
  638. .
  639. Nor does a backslash at the end:
  640. .
  641. Foo\
  642. ----
  643. .
  644. <h2>Foo\</h2>
  645. .
  646. Since indicators of block structure take precedence over
  647. indicators of inline structure, the following are setext headers:
  648. .
  649. `Foo
  650. ----
  651. `
  652. <a title="a lot
  653. ---
  654. of dashes"/>
  655. .
  656. <h2>`Foo</h2>
  657. <p>`</p>
  658. <h2>&lt;a title=&quot;a lot</h2>
  659. <p>of dashes&quot;/&gt;</p>
  660. .
  661. The setext header underline cannot be a [lazy continuation
  662. line](#lazy-continuation-line) in a list item or block quote:
  663. .
  664. > Foo
  665. ---
  666. .
  667. <blockquote>
  668. <p>Foo</p>
  669. </blockquote>
  670. <hr />
  671. .
  672. .
  673. - Foo
  674. ---
  675. .
  676. <ul>
  677. <li>Foo</li>
  678. </ul>
  679. <hr />
  680. .
  681. A setext header cannot interrupt a paragraph:
  682. .
  683. Foo
  684. Bar
  685. ---
  686. Foo
  687. Bar
  688. ===
  689. .
  690. <p>Foo
  691. Bar</p>
  692. <hr />
  693. <p>Foo
  694. Bar
  695. ===</p>
  696. .
  697. But in general a blank line is not required before or after:
  698. .
  699. ---
  700. Foo
  701. ---
  702. Bar
  703. ---
  704. Baz
  705. .
  706. <hr />
  707. <h2>Foo</h2>
  708. <h2>Bar</h2>
  709. <p>Baz</p>
  710. .
  711. Setext headers cannot be empty:
  712. .
  713. ====
  714. .
  715. <p>====</p>
  716. .
  717. Setext header text lines must not be interpretable as block
  718. constructs other than paragraphs. So, the line of dashes
  719. in these examples gets interpreted as a horizontal rule:
  720. .
  721. ---
  722. ---
  723. .
  724. <hr />
  725. <hr />
  726. .
  727. .
  728. - foo
  729. -----
  730. .
  731. <ul>
  732. <li>foo</li>
  733. </ul>
  734. <hr />
  735. .
  736. .
  737. foo
  738. ---
  739. .
  740. <pre><code>foo
  741. </code></pre>
  742. <hr />
  743. .
  744. .
  745. > foo
  746. -----
  747. .
  748. <blockquote>
  749. <p>foo</p>
  750. </blockquote>
  751. <hr />
  752. .
  753. If you want a header with `> foo` as its literal text, you can
  754. use backslash escapes:
  755. .
  756. \> foo
  757. ------
  758. .
  759. <h2>&gt; foo</h2>
  760. .
  761. ## Indented code blocks
  762. An [indented code block](@indented-code-block)
  763. is composed of one or more
  764. [indented chunks](#indented-chunk) separated by blank lines.
  765. An [indented chunk](@indented-chunk)
  766. is a sequence of non-blank lines, each indented four or more
  767. spaces. An indented code block cannot interrupt a paragraph, so
  768. if it occurs before or after a paragraph, there must be an
  769. intervening blank line. The contents of the code block are
  770. the literal contents of the lines, including trailing newlines,
  771. minus four spaces of indentation. An indented code block has no
  772. attributes.
  773. .
  774. a simple
  775. indented code block
  776. .
  777. <pre><code>a simple
  778. indented code block
  779. </code></pre>
  780. .
  781. The contents are literal text, and do not get parsed as Markdown:
  782. .
  783. <a/>
  784. *hi*
  785. - one
  786. .
  787. <pre><code>&lt;a/&gt;
  788. *hi*
  789. - one
  790. </code></pre>
  791. .
  792. Here we have three chunks separated by blank lines:
  793. .
  794. chunk1
  795. chunk2
  796. chunk3
  797. .
  798. <pre><code>chunk1
  799. chunk2
  800. chunk3
  801. </code></pre>
  802. .
  803. Any initial spaces beyond four will be included in the content, even
  804. in interior blank lines:
  805. .
  806. chunk1
  807. chunk2
  808. .
  809. <pre><code>chunk1
  810. chunk2
  811. </code></pre>
  812. .
  813. An indented code block cannot interrupt a paragraph. (This
  814. allows hanging indents and the like.)
  815. .
  816. Foo
  817. bar
  818. .
  819. <p>Foo
  820. bar</p>
  821. .
  822. However, any non-blank line with fewer than four leading spaces ends
  823. the code block immediately. So a paragraph may occur immediately
  824. after indented code:
  825. .
  826. foo
  827. bar
  828. .
  829. <pre><code>foo
  830. </code></pre>
  831. <p>bar</p>
  832. .
  833. And indented code can occur immediately before and after other kinds of
  834. blocks:
  835. .
  836. # Header
  837. foo
  838. Header
  839. ------
  840. foo
  841. ----
  842. .
  843. <h1>Header</h1>
  844. <pre><code>foo
  845. </code></pre>
  846. <h2>Header</h2>
  847. <pre><code>foo
  848. </code></pre>
  849. <hr />
  850. .
  851. The first line can be indented more than four spaces:
  852. .
  853. foo
  854. bar
  855. .
  856. <pre><code> foo
  857. bar
  858. </code></pre>
  859. .
  860. Blank lines preceding or following an indented code block
  861. are not included in it:
  862. .
  863. foo
  864. .
  865. <pre><code>foo
  866. </code></pre>
  867. .
  868. Trailing spaces are included in the code block's content:
  869. .
  870. foo
  871. .
  872. <pre><code>foo
  873. </code></pre>
  874. .
  875. ## Fenced code blocks
  876. A [code fence](@code-fence) is a sequence
  877. of at least three consecutive backtick characters (`` ` ``) or
  878. tildes (`~`). (Tildes and backticks cannot be mixed.)
  879. A [fenced code block](@fenced-code-block)
  880. begins with a code fence, indented no more than three spaces.
  881. The line with the opening code fence may optionally contain some text
  882. following the code fence; this is trimmed of leading and trailing
  883. spaces and called the [info string](@info-string).
  884. The info string may not contain any backtick
  885. characters. (The reason for this restriction is that otherwise
  886. some inline code would be incorrectly interpreted as the
  887. beginning of a fenced code block.)
  888. The content of the code block consists of all subsequent lines, until
  889. a closing [code fence](#code-fence) of the same type as the code block
  890. began with (backticks or tildes), and with at least as many backticks
  891. or tildes as the opening code fence. If the leading code fence is
  892. indented N spaces, then up to N spaces of indentation are removed from
  893. each line of the content (if present). (If a content line is not
  894. indented, it is preserved unchanged. If it is indented less than N
  895. spaces, all of the indentation is removed.)
  896. The closing code fence may be indented up to three spaces, and may be
  897. followed only by spaces, which are ignored. If the end of the
  898. containing block (or document) is reached and no closing code fence
  899. has been found, the code block contains all of the lines after the
  900. opening code fence until the end of the containing block (or
  901. document). (An alternative spec would require backtracking in the
  902. event that a closing code fence is not found. But this makes parsing
  903. much less efficient, and there seems to be no real down side to the
  904. behavior described here.)
  905. A fenced code block may interrupt a paragraph, and does not require
  906. a blank line either before or after.
  907. The content of a code fence is treated as literal text, not parsed
  908. as inlines. The first word of the info string is typically used to
  909. specify the language of the code sample, and rendered in the `class`
  910. attribute of the `code` tag. However, this spec does not mandate any
  911. particular treatment of the info string.
  912. Here is a simple example with backticks:
  913. .
  914. ```
  915. <
  916. >
  917. ```
  918. .
  919. <pre><code>&lt;
  920. &gt;
  921. </code></pre>
  922. .
  923. With tildes:
  924. .
  925. ~~~
  926. <
  927. >
  928. ~~~
  929. .
  930. <pre><code>&lt;
  931. &gt;
  932. </code></pre>
  933. .
  934. The closing code fence must use the same character as the opening
  935. fence:
  936. .
  937. ```
  938. aaa
  939. ~~~
  940. ```
  941. .
  942. <pre><code>aaa
  943. ~~~
  944. </code></pre>
  945. .
  946. .
  947. ~~~
  948. aaa
  949. ```
  950. ~~~
  951. .
  952. <pre><code>aaa
  953. ```
  954. </code></pre>
  955. .
  956. The closing code fence must be at least as long as the opening fence:
  957. .
  958. ````
  959. aaa
  960. ```
  961. ``````
  962. .
  963. <pre><code>aaa
  964. ```
  965. </code></pre>
  966. .
  967. .
  968. ~~~~
  969. aaa
  970. ~~~
  971. ~~~~
  972. .
  973. <pre><code>aaa
  974. ~~~
  975. </code></pre>
  976. .
  977. Unclosed code blocks are closed by the end of the document:
  978. .
  979. ```
  980. .
  981. <pre><code></code></pre>
  982. .
  983. .
  984. `````
  985. ```
  986. aaa
  987. .
  988. <pre><code>
  989. ```
  990. aaa
  991. </code></pre>
  992. .
  993. A code block can have all empty lines as its content:
  994. .
  995. ```
  996. ```
  997. .
  998. <pre><code>
  999. </code></pre>
  1000. .
  1001. A code block can be empty:
  1002. .
  1003. ```
  1004. ```
  1005. .
  1006. <pre><code></code></pre>
  1007. .
  1008. Fences can be indented. If the opening fence is indented,
  1009. content lines will have equivalent opening indentation removed,
  1010. if present:
  1011. .
  1012. ```
  1013. aaa
  1014. aaa
  1015. ```
  1016. .
  1017. <pre><code>aaa
  1018. aaa
  1019. </code></pre>
  1020. .
  1021. .
  1022. ```
  1023. aaa
  1024. aaa
  1025. aaa
  1026. ```
  1027. .
  1028. <pre><code>aaa
  1029. aaa
  1030. aaa
  1031. </code></pre>
  1032. .
  1033. .
  1034. ```
  1035. aaa
  1036. aaa
  1037. aaa
  1038. ```
  1039. .
  1040. <pre><code>aaa
  1041. aaa
  1042. aaa
  1043. </code></pre>
  1044. .
  1045. Four spaces indentation produces an indented code block:
  1046. .
  1047. ```
  1048. aaa
  1049. ```
  1050. .
  1051. <pre><code>```
  1052. aaa
  1053. ```
  1054. </code></pre>
  1055. .
  1056. Closing fences may be indented by 0-3 spaces, and their indentation
  1057. need not match that of the opening fence:
  1058. .
  1059. ```
  1060. aaa
  1061. ```
  1062. .
  1063. <pre><code>aaa
  1064. </code></pre>
  1065. .
  1066. .
  1067. ```
  1068. aaa
  1069. ```
  1070. .
  1071. <pre><code>aaa
  1072. </code></pre>
  1073. .
  1074. This is not a closing fence, because it is indented 4 spaces:
  1075. .
  1076. ```
  1077. aaa
  1078. ```
  1079. .
  1080. <pre><code>aaa
  1081. ```
  1082. </code></pre>
  1083. .
  1084. Code fences (opening and closing) cannot contain internal spaces:
  1085. .
  1086. ``` ```
  1087. aaa
  1088. .
  1089. <p><code></code>
  1090. aaa</p>
  1091. .
  1092. .
  1093. ~~~~~~
  1094. aaa
  1095. ~~~ ~~
  1096. .
  1097. <pre><code>aaa
  1098. ~~~ ~~
  1099. </code></pre>
  1100. .
  1101. Fenced code blocks can interrupt paragraphs, and can be followed
  1102. directly by paragraphs, without a blank line between:
  1103. .
  1104. foo
  1105. ```
  1106. bar
  1107. ```
  1108. baz
  1109. .
  1110. <p>foo</p>
  1111. <pre><code>bar
  1112. </code></pre>
  1113. <p>baz</p>
  1114. .
  1115. Other blocks can also occur before and after fenced code blocks
  1116. without an intervening blank line:
  1117. .
  1118. foo
  1119. ---
  1120. ~~~
  1121. bar
  1122. ~~~
  1123. # baz
  1124. .
  1125. <h2>foo</h2>
  1126. <pre><code>bar
  1127. </code></pre>
  1128. <h1>baz</h1>
  1129. .
  1130. An [info string](#info-string) can be provided after the opening code fence.
  1131. Opening and closing spaces will be stripped, and the first word, prefixed
  1132. with `language-`, is used as the value for the `class` attribute of the
  1133. `code` element within the enclosing `pre` element.
  1134. .
  1135. ```ruby
  1136. def foo(x)
  1137. return 3
  1138. end
  1139. ```
  1140. .
  1141. <pre><code class="language-ruby">def foo(x)
  1142. return 3
  1143. end
  1144. </code></pre>
  1145. .
  1146. .
  1147. ~~~~ ruby startline=3 $%@#$
  1148. def foo(x)
  1149. return 3
  1150. end
  1151. ~~~~~~~
  1152. .
  1153. <pre><code class="language-ruby">def foo(x)
  1154. return 3
  1155. end
  1156. </code></pre>
  1157. .
  1158. .
  1159. ````;
  1160. ````
  1161. .
  1162. <pre><code class="language-;"></code></pre>
  1163. .
  1164. Info strings for backtick code blocks cannot contain backticks:
  1165. .
  1166. ``` aa ```
  1167. foo
  1168. .
  1169. <p><code>aa</code>
  1170. foo</p>
  1171. .
  1172. Closing code fences cannot have info strings:
  1173. .
  1174. ```
  1175. ``` aaa
  1176. ```
  1177. .
  1178. <pre><code>``` aaa
  1179. </code></pre>
  1180. .
  1181. ## HTML blocks
  1182. An [HTML block tag](@html-block-tag) is
  1183. an [open tag](#open-tag) or [closing tag](#closing-tag) whose tag
  1184. name is one of the following (case-insensitive):
  1185. `article`, `header`, `aside`, `hgroup`, `blockquote`, `hr`, `iframe`,
  1186. `body`, `li`, `map`, `button`, `object`, `canvas`, `ol`, `caption`,
  1187. `output`, `col`, `p`, `colgroup`, `pre`, `dd`, `progress`, `div`,
  1188. `section`, `dl`, `table`, `td`, `dt`, `tbody`, `embed`, `textarea`,
  1189. `fieldset`, `tfoot`, `figcaption`, `th`, `figure`, `thead`, `footer`,
  1190. `tr`, `form`, `ul`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `video`,
  1191. `script`, `style`.
  1192. An [HTML block](@html-block) begins with an
  1193. [HTML block tag](#html-block-tag), [HTML comment](#html-comment),
  1194. [processing instruction](#processing-instruction),
  1195. [declaration](#declaration), or [CDATA section](#cdata-section).
  1196. It ends when a [blank line](#blank-line) or the end of the
  1197. input is encountered. The initial line may be indented up to three
  1198. spaces, and subsequent lines may have any indentation. The contents
  1199. of the HTML block are interpreted as raw HTML, and will not be escaped
  1200. in HTML output.
  1201. Some simple examples:
  1202. .
  1203. <table>
  1204. <tr>
  1205. <td>
  1206. hi
  1207. </td>
  1208. </tr>
  1209. </table>
  1210. okay.
  1211. .
  1212. <table>
  1213. <tr>
  1214. <td>
  1215. hi
  1216. </td>
  1217. </tr>
  1218. </table>
  1219. <p>okay.</p>
  1220. .
  1221. .
  1222. <div>
  1223. *hello*
  1224. <foo><a>
  1225. .
  1226. <div>
  1227. *hello*
  1228. <foo><a>
  1229. .
  1230. Here we have two HTML blocks with a Markdown paragraph between them:
  1231. .
  1232. <DIV CLASS="foo">
  1233. *Markdown*
  1234. </DIV>
  1235. .
  1236. <DIV CLASS="foo">
  1237. <p><em>Markdown</em></p>
  1238. </DIV>
  1239. .
  1240. In the following example, what looks like a Markdown code block
  1241. is actually part of the HTML block, which continues until a blank
  1242. line or the end of the document is reached:
  1243. .
  1244. <div></div>
  1245. ``` c
  1246. int x = 33;
  1247. ```
  1248. .
  1249. <div></div>
  1250. ``` c
  1251. int x = 33;
  1252. ```
  1253. .
  1254. A comment:
  1255. .
  1256. <!-- Foo
  1257. bar
  1258. baz -->
  1259. .
  1260. <!-- Foo
  1261. bar
  1262. baz -->
  1263. .
  1264. A processing instruction:
  1265. .
  1266. <?php
  1267. echo '>';
  1268. ?>
  1269. .
  1270. <?php
  1271. echo '>';
  1272. ?>
  1273. .
  1274. CDATA:
  1275. .
  1276. <![CDATA[
  1277. function matchwo(a,b)
  1278. {
  1279. if (a < b && a < 0) then
  1280. {
  1281. return 1;
  1282. }
  1283. else
  1284. {
  1285. return 0;
  1286. }
  1287. }
  1288. ]]>
  1289. .
  1290. <![CDATA[
  1291. function matchwo(a,b)
  1292. {
  1293. if (a < b && a < 0) then
  1294. {
  1295. return 1;
  1296. }
  1297. else
  1298. {
  1299. return 0;
  1300. }
  1301. }
  1302. ]]>
  1303. .
  1304. The opening tag can be indented 1-3 spaces, but not 4:
  1305. .
  1306. <!-- foo -->
  1307. <!-- foo -->
  1308. .
  1309. <!-- foo -->
  1310. <pre><code>&lt;!-- foo --&gt;
  1311. </code></pre>
  1312. .
  1313. An HTML block can interrupt a paragraph, and need not be preceded
  1314. by a blank line.
  1315. .
  1316. Foo
  1317. <div>
  1318. bar
  1319. </div>
  1320. .
  1321. <p>Foo</p>
  1322. <div>
  1323. bar
  1324. </div>
  1325. .
  1326. However, a following blank line is always needed, except at the end of
  1327. a document:
  1328. .
  1329. <div>
  1330. bar
  1331. </div>
  1332. *foo*
  1333. .
  1334. <div>
  1335. bar
  1336. </div>
  1337. *foo*
  1338. .
  1339. An incomplete HTML block tag may also start an HTML block:
  1340. .
  1341. <div class
  1342. foo
  1343. .
  1344. <div class
  1345. foo
  1346. .
  1347. This rule differs from John Gruber's original Markdown syntax
  1348. specification, which says:
  1349. > The only restrictions are that block-level HTML elements —
  1350. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  1351. > surrounding content by blank lines, and the start and end tags of the
  1352. > block should not be indented with tabs or spaces.
  1353. In some ways Gruber's rule is more restrictive than the one given
  1354. here:
  1355. - It requires that an HTML block be preceded by a blank line.
  1356. - It does not allow the start tag to be indented.
  1357. - It requires a matching end tag, which it also does not allow to
  1358. be indented.
  1359. Indeed, most Markdown implementations, including some of Gruber's
  1360. own perl implementations, do not impose these restrictions.
  1361. There is one respect, however, in which Gruber's rule is more liberal
  1362. than the one given here, since it allows blank lines to occur inside
  1363. an HTML block. There are two reasons for disallowing them here.
  1364. First, it removes the need to parse balanced tags, which is
  1365. expensive and can require backtracking from the end of the document
  1366. if no matching end tag is found. Second, it provides a very simple
  1367. and flexible way of including Markdown content inside HTML tags:
  1368. simply separate the Markdown from the HTML using blank lines:
  1369. .
  1370. <div>
  1371. *Emphasized* text.
  1372. </div>
  1373. .
  1374. <div>
  1375. <p><em>Emphasized</em> text.</p>
  1376. </div>
  1377. .
  1378. Compare:
  1379. .
  1380. <div>
  1381. *Emphasized* text.
  1382. </div>
  1383. .
  1384. <div>
  1385. *Emphasized* text.
  1386. </div>
  1387. .
  1388. Some Markdown implementations have adopted a convention of
  1389. interpreting content inside tags as text if the open tag has
  1390. the attribute `markdown=1`. The rule given above seems a simpler and
  1391. more elegant way of achieving the same expressive power, which is also
  1392. much simpler to parse.
  1393. The main potential drawback is that one can no longer paste HTML
  1394. blocks into Markdown documents with 100% reliability. However,
  1395. *in most cases* this will work fine, because the blank lines in
  1396. HTML are usually followed by HTML block tags. For example:
  1397. .
  1398. <table>
  1399. <tr>
  1400. <td>
  1401. Hi
  1402. </td>
  1403. </tr>
  1404. </table>
  1405. .
  1406. <table>
  1407. <tr>
  1408. <td>
  1409. Hi
  1410. </td>
  1411. </tr>
  1412. </table>
  1413. .
  1414. Moreover, blank lines are usually not necessary and can be
  1415. deleted. The exception is inside `<pre>` tags; here, one can
  1416. replace the blank lines with `&#10;` entities.
  1417. So there is no important loss of expressive power with the new rule.
  1418. ## Link reference definitions
  1419. A [link reference definition](@link-reference-definition)
  1420. consists of a [link
  1421. label](#link-label), indented up to three spaces, followed
  1422. by a colon (`:`), optional blank space (including up to one
  1423. newline), a [link destination](#link-destination), optional
  1424. blank space (including up to one newline), and an optional [link
  1425. title](#link-title), which if it is present must be separated
  1426. from the [link destination](#link-destination) by whitespace.
  1427. No further non-space characters may occur on the line.
  1428. A [link reference-definition](#link-reference-definition)
  1429. does not correspond to a structural element of a document. Instead, it
  1430. defines a label which can be used in [reference links](#reference-link)
  1431. and reference-style [images](#image) elsewhere in the document. [Link
  1432. reference definitions] can come either before or after the links that use
  1433. them.
  1434. .
  1435. [foo]: /url "title"
  1436. [foo]
  1437. .
  1438. <p><a href="/url" title="title">foo</a></p>
  1439. .
  1440. .
  1441. [foo]:
  1442. /url
  1443. 'the title'
  1444. [foo]
  1445. .
  1446. <p><a href="/url" title="the title">foo</a></p>
  1447. .
  1448. .
  1449. [Foo*bar\]]:my_(url) 'title (with parens)'
  1450. [Foo*bar\]]
  1451. .
  1452. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  1453. .
  1454. .
  1455. [Foo bar]:
  1456. <my url>
  1457. 'title'
  1458. [Foo bar]
  1459. .
  1460. <p><a href="my%20url" title="title">Foo bar</a></p>
  1461. .
  1462. The title may be omitted:
  1463. .
  1464. [foo]:
  1465. /url
  1466. [foo]
  1467. .
  1468. <p><a href="/url">foo</a></p>
  1469. .
  1470. The link destination may not be omitted:
  1471. .
  1472. [foo]:
  1473. [foo]
  1474. .
  1475. <p>[foo]:</p>
  1476. <p>[foo]</p>
  1477. .
  1478. A link can come before its corresponding definition:
  1479. .
  1480. [foo]
  1481. [foo]: url
  1482. .
  1483. <p><a href="url">foo</a></p>
  1484. .
  1485. If there are several matching definitions, the first one takes
  1486. precedence:
  1487. .
  1488. [foo]
  1489. [foo]: first
  1490. [foo]: second
  1491. .
  1492. <p><a href="first">foo</a></p>
  1493. .
  1494. As noted in the section on [Links], matching of labels is
  1495. case-insensitive (see [matches](#matches)).
  1496. .
  1497. [FOO]: /url
  1498. [Foo]
  1499. .
  1500. <p><a href="/url">Foo</a></p>
  1501. .
  1502. .
  1503. [ΑΓΩ]: /φου
  1504. [αγω]
  1505. .
  1506. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  1507. .
  1508. Here is a link reference definition with no corresponding link.
  1509. It contributes nothing to the document.
  1510. .
  1511. [foo]: /url
  1512. .
  1513. .
  1514. This is not a link reference definition, because there are
  1515. non-space characters after the title:
  1516. .
  1517. [foo]: /url "title" ok
  1518. .
  1519. <p>[foo]: /url &quot;title&quot; ok</p>
  1520. .
  1521. This is not a link reference definition, because it is indented
  1522. four spaces:
  1523. .
  1524. [foo]: /url "title"
  1525. [foo]
  1526. .
  1527. <pre><code>[foo]: /url &quot;title&quot;
  1528. </code></pre>
  1529. <p>[foo]</p>
  1530. .
  1531. This is not a link reference definition, because it occurs inside
  1532. a code block:
  1533. .
  1534. ```
  1535. [foo]: /url
  1536. ```
  1537. [foo]
  1538. .
  1539. <pre><code>[foo]: /url
  1540. </code></pre>
  1541. <p>[foo]</p>
  1542. .
  1543. A [link reference definition](#link-reference-definition) cannot
  1544. interrupt a paragraph.
  1545. .
  1546. Foo
  1547. [bar]: /baz
  1548. [bar]
  1549. .
  1550. <p>Foo
  1551. [bar]: /baz</p>
  1552. <p>[bar]</p>
  1553. .
  1554. However, it can directly follow other block elements, such as headers
  1555. and horizontal rules, and it need not be followed by a blank line.
  1556. .
  1557. # [Foo]
  1558. [foo]: /url
  1559. > bar
  1560. .
  1561. <h1><a href="/url">Foo</a></h1>
  1562. <blockquote>
  1563. <p>bar</p>
  1564. </blockquote>
  1565. .
  1566. Several [link references](#link-reference) can occur one after another,
  1567. without intervening blank lines.
  1568. .
  1569. [foo]: /foo-url "foo"
  1570. [bar]: /bar-url
  1571. "bar"
  1572. [baz]: /baz-url
  1573. [foo],
  1574. [bar],
  1575. [baz]
  1576. .
  1577. <p><a href="/foo-url" title="foo">foo</a>,
  1578. <a href="/bar-url" title="bar">bar</a>,
  1579. <a href="/baz-url">baz</a></p>
  1580. .
  1581. [Link reference definitions](#link-reference-definition) can occur
  1582. inside block containers, like lists and block quotations. They
  1583. affect the entire document, not just the container in which they
  1584. are defined:
  1585. .
  1586. [foo]
  1587. > [foo]: /url
  1588. .
  1589. <p><a href="/url">foo</a></p>
  1590. <blockquote>
  1591. </blockquote>
  1592. .
  1593. ## Paragraphs
  1594. A sequence of non-blank lines that cannot be interpreted as other
  1595. kinds of blocks forms a [paragraph](@paragraph).
  1596. The contents of the paragraph are the result of parsing the
  1597. paragraph's raw content as inlines. The paragraph's raw content
  1598. is formed by concatenating the lines and removing initial and final
  1599. spaces.
  1600. A simple example with two paragraphs:
  1601. .
  1602. aaa
  1603. bbb
  1604. .
  1605. <p>aaa</p>
  1606. <p>bbb</p>
  1607. .
  1608. Paragraphs can contain multiple lines, but no blank lines:
  1609. .
  1610. aaa
  1611. bbb
  1612. ccc
  1613. ddd
  1614. .
  1615. <p>aaa
  1616. bbb</p>
  1617. <p>ccc
  1618. ddd</p>
  1619. .
  1620. Multiple blank lines between paragraph have no effect:
  1621. .
  1622. aaa
  1623. bbb
  1624. .
  1625. <p>aaa</p>
  1626. <p>bbb</p>
  1627. .
  1628. Leading spaces are skipped:
  1629. .
  1630. aaa
  1631. bbb
  1632. .
  1633. <p>aaa
  1634. bbb</p>
  1635. .
  1636. Lines after the first may be indented any amount, since indented
  1637. code blocks cannot interrupt paragraphs.
  1638. .
  1639. aaa
  1640. bbb
  1641. ccc
  1642. .
  1643. <p>aaa
  1644. bbb
  1645. ccc</p>
  1646. .
  1647. However, the first line may be indented at most three spaces,
  1648. or an indented code block will be triggered:
  1649. .
  1650. aaa
  1651. bbb
  1652. .
  1653. <p>aaa
  1654. bbb</p>
  1655. .
  1656. .
  1657. aaa
  1658. bbb
  1659. .
  1660. <pre><code>aaa
  1661. </code></pre>
  1662. <p>bbb</p>
  1663. .
  1664. Final spaces are stripped before inline parsing, so a paragraph
  1665. that ends with two or more spaces will not end with a [hard line
  1666. break](#hard-line-break):
  1667. .
  1668. aaa
  1669. bbb
  1670. .
  1671. <p>aaa<br />
  1672. bbb</p>
  1673. .
  1674. ## Blank lines
  1675. [Blank lines](#blank-line) between block-level elements are ignored,
  1676. except for the role they play in determining whether a [list](#list)
  1677. is [tight](#tight) or [loose](#loose).
  1678. Blank lines at the beginning and end of the document are also ignored.
  1679. .
  1680. aaa
  1681. # aaa
  1682. .
  1683. <p>aaa</p>
  1684. <h1>aaa</h1>
  1685. .
  1686. # Container blocks
  1687. A [container block](#container-block) is a block that has other
  1688. blocks as its contents. There are two basic kinds of container blocks:
  1689. [block quotes](#block-quote) and [list items](#list-item).
  1690. [Lists](#list) are meta-containers for [list items](#list-item).
  1691. We define the syntax for container blocks recursively. The general
  1692. form of the definition is:
  1693. > If X is a sequence of blocks, then the result of
  1694. > transforming X in such-and-such a way is a container of type Y
  1695. > with these blocks as its content.
  1696. So, we explain what counts as a block quote or list item by explaining
  1697. how these can be *generated* from their contents. This should suffice
  1698. to define the syntax, although it does not give a recipe for *parsing*
  1699. these constructions. (A recipe is provided below in the section entitled
  1700. [A parsing strategy](#appendix-a-a-parsing-strategy).)
  1701. ## Block quotes
  1702. A [block quote marker](@block-quote-marker)
  1703. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  1704. with a following space, or (b) a single character `>` not followed by a space.
  1705. The following rules define [block quotes](@block-quote):
  1706. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  1707. of blocks *Bs*, then the result of prepending a [block quote
  1708. marker](#block-quote-marker) to the beginning of each line in *Ls*
  1709. is a [block quote](#block-quote) containing *Bs*.
  1710. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  1711. quote](#block-quote) with contents *Bs*, then the result of deleting
  1712. the initial [block quote marker](#block-quote-marker) from one or
  1713. more lines in which the next non-space character after the [block
  1714. quote marker](#block-quote-marker) is [paragraph continuation
  1715. text](#paragraph-continuation-text) is a block quote with *Bs* as
  1716. its content.
  1717. [Paragraph continuation text](@paragraph-continuation-text) is text
  1718. that will be parsed as part of the content of a paragraph, but does
  1719. not occur at the beginning of the paragraph.
  1720. 3. **Consecutiveness.** A document cannot contain two [block
  1721. quotes](#block-quote) in a row unless there is a [blank
  1722. line](#blank-line) between them.
  1723. Nothing else counts as a [block quote](#block-quote).
  1724. Here is a simple example:
  1725. .
  1726. > # Foo
  1727. > bar
  1728. > baz
  1729. .
  1730. <blockquote>
  1731. <h1>Foo</h1>
  1732. <p>bar
  1733. baz</p>
  1734. </blockquote>
  1735. .
  1736. The spaces after the `>` characters can be omitted:
  1737. .
  1738. ># Foo
  1739. >bar
  1740. > baz
  1741. .
  1742. <blockquote>
  1743. <h1>Foo</h1>
  1744. <p>bar
  1745. baz</p>
  1746. </blockquote>
  1747. .
  1748. The `>` characters can be indented 1-3 spaces:
  1749. .
  1750. > # Foo
  1751. > bar
  1752. > baz
  1753. .
  1754. <blockquote>
  1755. <h1>Foo</h1>
  1756. <p>bar
  1757. baz</p>
  1758. </blockquote>
  1759. .
  1760. Four spaces gives us a code block:
  1761. .
  1762. > # Foo
  1763. > bar
  1764. > baz
  1765. .
  1766. <pre><code>&gt; # Foo
  1767. &gt; bar
  1768. &gt; baz
  1769. </code></pre>
  1770. .
  1771. The Laziness clause allows us to omit the `>` before a
  1772. paragraph continuation line:
  1773. .
  1774. > # Foo
  1775. > bar
  1776. baz
  1777. .
  1778. <blockquote>
  1779. <h1>Foo</h1>
  1780. <p>bar
  1781. baz</p>
  1782. </blockquote>
  1783. .
  1784. A block quote can contain some lazy and some non-lazy
  1785. continuation lines:
  1786. .
  1787. > bar
  1788. baz
  1789. > foo
  1790. .
  1791. <blockquote>
  1792. <p>bar
  1793. baz
  1794. foo</p>
  1795. </blockquote>
  1796. .
  1797. Laziness only applies to lines that are continuations of
  1798. paragraphs. Lines containing characters or indentation that indicate
  1799. block structure cannot be lazy.
  1800. .
  1801. > foo
  1802. ---
  1803. .
  1804. <blockquote>
  1805. <p>foo</p>
  1806. </blockquote>
  1807. <hr />
  1808. .
  1809. .
  1810. > - foo
  1811. - bar
  1812. .
  1813. <blockquote>
  1814. <ul>
  1815. <li>foo</li>
  1816. </ul>
  1817. </blockquote>
  1818. <ul>
  1819. <li>bar</li>
  1820. </ul>
  1821. .
  1822. .
  1823. > foo
  1824. bar
  1825. .
  1826. <blockquote>
  1827. <pre><code>foo
  1828. </code></pre>
  1829. </blockquote>
  1830. <pre><code>bar
  1831. </code></pre>
  1832. .
  1833. .
  1834. > ```
  1835. foo
  1836. ```
  1837. .
  1838. <blockquote>
  1839. <pre><code></code></pre>
  1840. </blockquote>
  1841. <p>foo</p>
  1842. <pre><code></code></pre>
  1843. .
  1844. A block quote can be empty:
  1845. .
  1846. >
  1847. .
  1848. <blockquote>
  1849. </blockquote>
  1850. .
  1851. .
  1852. >
  1853. >
  1854. >
  1855. .
  1856. <blockquote>
  1857. </blockquote>
  1858. .
  1859. A block quote can have initial or final blank lines:
  1860. .
  1861. >
  1862. > foo
  1863. >
  1864. .
  1865. <blockquote>
  1866. <p>foo</p>
  1867. </blockquote>
  1868. .
  1869. A blank line always separates block quotes:
  1870. .
  1871. > foo
  1872. > bar
  1873. .
  1874. <blockquote>
  1875. <p>foo</p>
  1876. </blockquote>
  1877. <blockquote>
  1878. <p>bar</p>
  1879. </blockquote>
  1880. .
  1881. (Most current Markdown implementations, including John Gruber's
  1882. original `Markdown.pl`, will parse this example as a single block quote
  1883. with two paragraphs. But it seems better to allow the author to decide
  1884. whether two block quotes or one are wanted.)
  1885. Consecutiveness means that if we put these block quotes together,
  1886. we get a single block quote:
  1887. .
  1888. > foo
  1889. > bar
  1890. .
  1891. <blockquote>
  1892. <p>foo
  1893. bar</p>
  1894. </blockquote>
  1895. .
  1896. To get a block quote with two paragraphs, use:
  1897. .
  1898. > foo
  1899. >
  1900. > bar
  1901. .
  1902. <blockquote>
  1903. <p>foo</p>
  1904. <p>bar</p>
  1905. </blockquote>
  1906. .
  1907. Block quotes can interrupt paragraphs:
  1908. .
  1909. foo
  1910. > bar
  1911. .
  1912. <p>foo</p>
  1913. <blockquote>
  1914. <p>bar</p>
  1915. </blockquote>
  1916. .
  1917. In general, blank lines are not needed before or after block
  1918. quotes:
  1919. .
  1920. > aaa
  1921. ***
  1922. > bbb
  1923. .
  1924. <blockquote>
  1925. <p>aaa</p>
  1926. </blockquote>
  1927. <hr />
  1928. <blockquote>
  1929. <p>bbb</p>
  1930. </blockquote>
  1931. .
  1932. However, because of laziness, a blank line is needed between
  1933. a block quote and a following paragraph:
  1934. .
  1935. > bar
  1936. baz
  1937. .
  1938. <blockquote>
  1939. <p>bar
  1940. baz</p>
  1941. </blockquote>
  1942. .
  1943. .
  1944. > bar
  1945. baz
  1946. .
  1947. <blockquote>
  1948. <p>bar</p>
  1949. </blockquote>
  1950. <p>baz</p>
  1951. .
  1952. .
  1953. > bar
  1954. >
  1955. baz
  1956. .
  1957. <blockquote>
  1958. <p>bar</p>
  1959. </blockquote>
  1960. <p>baz</p>
  1961. .
  1962. It is a consequence of the Laziness rule that any number
  1963. of initial `>`s may be omitted on a continuation line of a
  1964. nested block quote:
  1965. .
  1966. > > > foo
  1967. bar
  1968. .
  1969. <blockquote>
  1970. <blockquote>
  1971. <blockquote>
  1972. <p>foo
  1973. bar</p>
  1974. </blockquote>
  1975. </blockquote>
  1976. </blockquote>
  1977. .
  1978. .
  1979. >>> foo
  1980. > bar
  1981. >>baz
  1982. .
  1983. <blockquote>
  1984. <blockquote>
  1985. <blockquote>
  1986. <p>foo
  1987. bar
  1988. baz</p>
  1989. </blockquote>
  1990. </blockquote>
  1991. </blockquote>
  1992. .
  1993. When including an indented code block in a block quote,
  1994. remember that the [block quote marker](#block-quote-marker) includes
  1995. both the `>` and a following space. So *five spaces* are needed after
  1996. the `>`:
  1997. .
  1998. > code
  1999. > not code
  2000. .
  2001. <blockquote>
  2002. <pre><code>code
  2003. </code></pre>
  2004. </blockquote>
  2005. <blockquote>
  2006. <p>not code</p>
  2007. </blockquote>
  2008. .
  2009. ## List items
  2010. A [list marker](@list-marker) is a
  2011. [bullet list marker](#bullet-list-marker) or an [ordered list
  2012. marker](#ordered-list-marker).
  2013. A [bullet list marker](@bullet-list-marker)
  2014. is a `-`, `+`, or `*` character.
  2015. An [ordered list marker](@ordered-list-marker)
  2016. is a sequence of one of more digits (`0-9`), followed by either a
  2017. `.` character or a `)` character.
  2018. The following rules define [list items](@list-item):
  2019. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2020. blocks *Bs* starting with a non-space character and not separated
  2021. from each other by more than one blank line, and *M* is a list
  2022. marker *M* of width *W* followed by 0 < *N* < 5 spaces, then the result
  2023. of prepending *M* and the following spaces to the first line of
  2024. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2025. list item with *Bs* as its contents. The type of the list item
  2026. (bullet or ordered) is determined by the type of its list marker.
  2027. If the list item is ordered, then it is also assigned a start
  2028. number, based on the ordered list marker.
  2029. For example, let *Ls* be the lines
  2030. .
  2031. A paragraph
  2032. with two lines.
  2033. indented code
  2034. > A block quote.
  2035. .
  2036. <p>A paragraph
  2037. with two lines.</p>
  2038. <pre><code>indented code
  2039. </code></pre>
  2040. <blockquote>
  2041. <p>A block quote.</p>
  2042. </blockquote>
  2043. .
  2044. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2045. that the following is an ordered list item with start number 1,
  2046. and the same contents as *Ls*:
  2047. .
  2048. 1. A paragraph
  2049. with two lines.
  2050. indented code
  2051. > A block quote.
  2052. .
  2053. <ol>
  2054. <li>
  2055. <p>A paragraph
  2056. with two lines.</p>
  2057. <pre><code>indented code
  2058. </code></pre>
  2059. <blockquote>
  2060. <p>A block quote.</p>
  2061. </blockquote>
  2062. </li>
  2063. </ol>
  2064. .
  2065. The most important thing to notice is that the position of
  2066. the text after the list marker determines how much indentation
  2067. is needed in subsequent blocks in the list item. If the list
  2068. marker takes up two spaces, and there are three spaces between
  2069. the list marker and the next nonspace character, then blocks
  2070. must be indented five spaces in order to fall under the list
  2071. item.
  2072. Here are some examples showing how far content must be indented to be
  2073. put under the list item:
  2074. .
  2075. - one
  2076. two
  2077. .
  2078. <ul>
  2079. <li>one</li>
  2080. </ul>
  2081. <p>two</p>
  2082. .
  2083. .
  2084. - one
  2085. two
  2086. .
  2087. <ul>
  2088. <li>
  2089. <p>one</p>
  2090. <p>two</p>
  2091. </li>
  2092. </ul>
  2093. .
  2094. .
  2095. - one
  2096. two
  2097. .
  2098. <ul>
  2099. <li>one</li>
  2100. </ul>
  2101. <pre><code> two
  2102. </code></pre>
  2103. .
  2104. .
  2105. - one
  2106. two
  2107. .
  2108. <ul>
  2109. <li>
  2110. <p>one</p>
  2111. <p>two</p>
  2112. </li>
  2113. </ul>
  2114. .
  2115. It is tempting to think of this in terms of columns: the continuation
  2116. blocks must be indented at least to the column of the first nonspace
  2117. character after the list marker. However, that is not quite right.
  2118. The spaces after the list marker determine how much relative indentation
  2119. is needed. Which column this indentation reaches will depend on
  2120. how the list item is embedded in other constructions, as shown by
  2121. this example:
  2122. .
  2123. > > 1. one
  2124. >>
  2125. >> two
  2126. .
  2127. <blockquote>
  2128. <blockquote>
  2129. <ol>
  2130. <li>
  2131. <p>one</p>
  2132. <p>two</p>
  2133. </li>
  2134. </ol>
  2135. </blockquote>
  2136. </blockquote>
  2137. .
  2138. Here `two` occurs in the same column as the list marker `1.`,
  2139. but is actually contained in the list item, because there is
  2140. sufficent indentation after the last containing blockquote marker.
  2141. The converse is also possible. In the following example, the word `two`
  2142. occurs far to the right of the initial text of the list item, `one`, but
  2143. it is not considered part of the list item, because it is not indented
  2144. far enough past the blockquote marker:
  2145. .
  2146. >>- one
  2147. >>
  2148. > > two
  2149. .
  2150. <blockquote>
  2151. <blockquote>
  2152. <ul>
  2153. <li>one</li>
  2154. </ul>
  2155. <p>two</p>
  2156. </blockquote>
  2157. </blockquote>
  2158. .
  2159. A list item may not contain blocks that are separated by more than
  2160. one blank line. Thus, two blank lines will end a list, unless the
  2161. two blanks are contained in a [fenced code block](#fenced-code-block).
  2162. .
  2163. - foo
  2164. bar
  2165. - foo
  2166. bar
  2167. - ```
  2168. foo
  2169. bar
  2170. ```
  2171. .
  2172. <ul>
  2173. <li>
  2174. <p>foo</p>
  2175. <p>bar</p>
  2176. </li>
  2177. <li>
  2178. <p>foo</p>
  2179. </li>
  2180. </ul>
  2181. <p>bar</p>
  2182. <ul>
  2183. <li>
  2184. <pre><code>foo
  2185. bar
  2186. </code></pre>
  2187. </li>
  2188. </ul>
  2189. .
  2190. A list item may contain any kind of block:
  2191. .
  2192. 1. foo
  2193. ```
  2194. bar
  2195. ```
  2196. baz
  2197. > bam
  2198. .
  2199. <ol>
  2200. <li>
  2201. <p>foo</p>
  2202. <pre><code>bar
  2203. </code></pre>
  2204. <p>baz</p>
  2205. <blockquote>
  2206. <p>bam</p>
  2207. </blockquote>
  2208. </li>
  2209. </ol>
  2210. .
  2211. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  2212. constitute a sequence of blocks *Bs* starting with an indented code
  2213. block and not separated from each other by more than one blank line,
  2214. and *M* is a list marker *M* of width *W* followed by
  2215. one space, then the result of prepending *M* and the following
  2216. space to the first line of *Ls*, and indenting subsequent lines of
  2217. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  2218. If a line is empty, then it need not be indented. The type of the
  2219. list item (bullet or ordered) is determined by the type of its list
  2220. marker. If the list item is ordered, then it is also assigned a
  2221. start number, based on the ordered list marker.
  2222. An indented code block will have to be indented four spaces beyond
  2223. the edge of the region where text will be included in the list item.
  2224. In the following case that is 6 spaces:
  2225. .
  2226. - foo
  2227. bar
  2228. .
  2229. <ul>
  2230. <li>
  2231. <p>foo</p>
  2232. <pre><code>bar
  2233. </code></pre>
  2234. </li>
  2235. </ul>
  2236. .
  2237. And in this case it is 11 spaces:
  2238. .
  2239. 10. foo
  2240. bar
  2241. .
  2242. <ol start="10">
  2243. <li>
  2244. <p>foo</p>
  2245. <pre><code>bar
  2246. </code></pre>
  2247. </li>
  2248. </ol>
  2249. .
  2250. If the *first* block in the list item is an indented code block,
  2251. then by rule #2, the contents must be indented *one* space after the
  2252. list marker:
  2253. .
  2254. indented code
  2255. paragraph
  2256. more code
  2257. .
  2258. <pre><code>indented code
  2259. </code></pre>
  2260. <p>paragraph</p>
  2261. <pre><code>more code
  2262. </code></pre>
  2263. .
  2264. .
  2265. 1. indented code
  2266. paragraph
  2267. more code
  2268. .
  2269. <ol>
  2270. <li>
  2271. <pre><code>indented code
  2272. </code></pre>
  2273. <p>paragraph</p>
  2274. <pre><code>more code
  2275. </code></pre>
  2276. </li>
  2277. </ol>
  2278. .
  2279. Note that an additional space indent is interpreted as space
  2280. inside the code block:
  2281. .
  2282. 1. indented code
  2283. paragraph
  2284. more code
  2285. .
  2286. <ol>
  2287. <li>
  2288. <pre><code> indented code
  2289. </code></pre>
  2290. <p>paragraph</p>
  2291. <pre><code>more code
  2292. </code></pre>
  2293. </li>
  2294. </ol>
  2295. .
  2296. Note that rules #1 and #2 only apply to two cases: (a) cases
  2297. in which the lines to be included in a list item begin with a nonspace
  2298. character, and (b) cases in which they begin with an indented code
  2299. block. In a case like the following, where the first block begins with
  2300. a three-space indent, the rules do not allow us to form a list item by
  2301. indenting the whole thing and prepending a list marker:
  2302. .
  2303. foo
  2304. bar
  2305. .
  2306. <p>foo</p>
  2307. <p>bar</p>
  2308. .
  2309. .
  2310. - foo
  2311. bar
  2312. .
  2313. <ul>
  2314. <li>foo</li>
  2315. </ul>
  2316. <p>bar</p>
  2317. .
  2318. This is not a significant restriction, because when a block begins
  2319. with 1-3 spaces indent, the indentation can always be removed without
  2320. a change in interpretation, allowing rule #1 to be applied. So, in
  2321. the above case:
  2322. .
  2323. - foo
  2324. bar
  2325. .
  2326. <ul>
  2327. <li>
  2328. <p>foo</p>
  2329. <p>bar</p>
  2330. </li>
  2331. </ul>
  2332. .
  2333. 3. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  2334. according to rule #1 or #2, then the result of indenting each line
  2335. of *L* by 1-3 spaces (the same for each line) also constitutes a
  2336. list item with the same contents and attributes. If a line is
  2337. empty, then it need not be indented.
  2338. Indented one space:
  2339. .
  2340. 1. A paragraph
  2341. with two lines.
  2342. indented code
  2343. > A block quote.
  2344. .
  2345. <ol>
  2346. <li>
  2347. <p>A paragraph
  2348. with two lines.</p>
  2349. <pre><code>indented code
  2350. </code></pre>
  2351. <blockquote>
  2352. <p>A block quote.</p>
  2353. </blockquote>
  2354. </li>
  2355. </ol>
  2356. .
  2357. Indented two spaces:
  2358. .
  2359. 1. A paragraph
  2360. with two lines.
  2361. indented code
  2362. > A block quote.
  2363. .
  2364. <ol>
  2365. <li>
  2366. <p>A paragraph
  2367. with two lines.</p>
  2368. <pre><code>indented code
  2369. </code></pre>
  2370. <blockquote>
  2371. <p>A block quote.</p>
  2372. </blockquote>
  2373. </li>
  2374. </ol>
  2375. .
  2376. Indented three spaces:
  2377. .
  2378. 1. A paragraph
  2379. with two lines.
  2380. indented code
  2381. > A block quote.
  2382. .
  2383. <ol>
  2384. <li>
  2385. <p>A paragraph
  2386. with two lines.</p>
  2387. <pre><code>indented code
  2388. </code></pre>
  2389. <blockquote>
  2390. <p>A block quote.</p>
  2391. </blockquote>
  2392. </li>
  2393. </ol>
  2394. .
  2395. Four spaces indent gives a code block:
  2396. .
  2397. 1. A paragraph
  2398. with two lines.
  2399. indented code
  2400. > A block quote.
  2401. .
  2402. <pre><code>1. A paragraph
  2403. with two lines.
  2404. indented code
  2405. &gt; A block quote.
  2406. </code></pre>
  2407. .
  2408. 4. **Laziness.** If a string of lines *Ls* constitute a [list
  2409. item](#list-item) with contents *Bs*, then the result of deleting
  2410. some or all of the indentation from one or more lines in which the
  2411. next non-space character after the indentation is
  2412. [paragraph continuation text](#paragraph-continuation-text) is a
  2413. list item with the same contents and attributes. The unindented
  2414. lines are called
  2415. [lazy continuation lines](@lazy-continuation-line).
  2416. Here is an example with [lazy continuation
  2417. lines](#lazy-continuation-line):
  2418. .
  2419. 1. A paragraph
  2420. with two lines.
  2421. indented code
  2422. > A block quote.
  2423. .
  2424. <ol>
  2425. <li>
  2426. <p>A paragraph
  2427. with two lines.</p>
  2428. <pre><code>indented code
  2429. </code></pre>
  2430. <blockquote>
  2431. <p>A block quote.</p>
  2432. </blockquote>
  2433. </li>
  2434. </ol>
  2435. .
  2436. Indentation can be partially deleted:
  2437. .
  2438. 1. A paragraph
  2439. with two lines.
  2440. .
  2441. <ol>
  2442. <li>A paragraph
  2443. with two lines.</li>
  2444. </ol>
  2445. .
  2446. These examples show how laziness can work in nested structures:
  2447. .
  2448. > 1. > Blockquote
  2449. continued here.
  2450. .
  2451. <blockquote>
  2452. <ol>
  2453. <li>
  2454. <blockquote>
  2455. <p>Blockquote
  2456. continued here.</p>
  2457. </blockquote>
  2458. </li>
  2459. </ol>
  2460. </blockquote>
  2461. .
  2462. .
  2463. > 1. > Blockquote
  2464. > continued here.
  2465. .
  2466. <blockquote>
  2467. <ol>
  2468. <li>
  2469. <blockquote>
  2470. <p>Blockquote
  2471. continued here.</p>
  2472. </blockquote>
  2473. </li>
  2474. </ol>
  2475. </blockquote>
  2476. .
  2477. 5. **That's all.** Nothing that is not counted as a list item by rules
  2478. #1--4 counts as a [list item](#list-item).
  2479. The rules for sublists follow from the general rules above. A sublist
  2480. must be indented the same number of spaces a paragraph would need to be
  2481. in order to be included in the list item.
  2482. So, in this case we need two spaces indent:
  2483. .
  2484. - foo
  2485. - bar
  2486. - baz
  2487. .
  2488. <ul>
  2489. <li>foo
  2490. <ul>
  2491. <li>bar
  2492. <ul>
  2493. <li>baz</li>
  2494. </ul>
  2495. </li>
  2496. </ul>
  2497. </li>
  2498. </ul>
  2499. .
  2500. One is not enough:
  2501. .
  2502. - foo
  2503. - bar
  2504. - baz
  2505. .
  2506. <ul>
  2507. <li>foo</li>
  2508. <li>bar</li>
  2509. <li>baz</li>
  2510. </ul>
  2511. .
  2512. Here we need four, because the list marker is wider:
  2513. .
  2514. 10) foo
  2515. - bar
  2516. .
  2517. <ol start="10">
  2518. <li>foo
  2519. <ul>
  2520. <li>bar</li>
  2521. </ul>
  2522. </li>
  2523. </ol>
  2524. .
  2525. Three is not enough:
  2526. .
  2527. 10) foo
  2528. - bar
  2529. .
  2530. <ol start="10">
  2531. <li>foo</li>
  2532. </ol>
  2533. <ul>
  2534. <li>bar</li>
  2535. </ul>
  2536. .
  2537. A list may be the first block in a list item:
  2538. .
  2539. - - foo
  2540. .
  2541. <ul>
  2542. <li>
  2543. <ul>
  2544. <li>foo</li>
  2545. </ul>
  2546. </li>
  2547. </ul>
  2548. .
  2549. .
  2550. 1. - 2. foo
  2551. .
  2552. <ol>
  2553. <li>
  2554. <ul>
  2555. <li>
  2556. <ol start="2">
  2557. <li>foo</li>
  2558. </ol>
  2559. </li>
  2560. </ul>
  2561. </li>
  2562. </ol>
  2563. .
  2564. A list item may be empty:
  2565. .
  2566. - foo
  2567. -
  2568. - bar
  2569. .
  2570. <ul>
  2571. <li>foo</li>
  2572. <li></li>
  2573. <li>bar</li>
  2574. </ul>
  2575. .
  2576. .
  2577. -
  2578. .
  2579. <ul>
  2580. <li></li>
  2581. </ul>
  2582. .
  2583. A list item can contain a header:
  2584. .
  2585. - # Foo
  2586. - Bar
  2587. ---
  2588. baz
  2589. .
  2590. <ul>
  2591. <li>
  2592. <h1>Foo</h1>
  2593. </li>
  2594. <li>
  2595. <h2>Bar</h2>
  2596. <p>baz</p>
  2597. </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. If you just want bracketed text, you can backslash-escape the
  4766. opening bracket to avoid links:
  4767. .
  4768. \[foo]
  4769. [foo]: /url "title"
  4770. .
  4771. <p>[foo]</p>
  4772. .
  4773. Note that this is a link, because a link label ends with the first
  4774. following closing bracket:
  4775. .
  4776. [foo*]: /url
  4777. *[foo*]
  4778. .
  4779. <p>*<a href="/url">foo*</a></p>
  4780. .
  4781. This is a link too, for the same reason:
  4782. .
  4783. [foo`]: /url
  4784. [foo`]`
  4785. .
  4786. <p>[foo<code>]</code></p>
  4787. .
  4788. Full references take precedence over shortcut references:
  4789. .
  4790. [foo][bar]
  4791. [foo]: /url1
  4792. [bar]: /url2
  4793. .
  4794. <p><a href="/url2">foo</a></p>
  4795. .
  4796. In the following case `[bar][baz]` is parsed as a reference,
  4797. `[foo]` as normal text:
  4798. .
  4799. [foo][bar][baz]
  4800. [baz]: /url
  4801. .
  4802. <p>[foo]<a href="/url">bar</a></p>
  4803. .
  4804. Here, though, `[foo][bar]` is parsed as a reference, since
  4805. `[bar]` is defined:
  4806. .
  4807. [foo][bar][baz]
  4808. [baz]: /url1
  4809. [bar]: /url2
  4810. .
  4811. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  4812. .
  4813. Here `[foo]` is not parsed as a shortcut reference, because it
  4814. is followed by a link label (even though `[bar]` is not defined):
  4815. .
  4816. [foo][bar][baz]
  4817. [baz]: /url1
  4818. [foo]: /url2
  4819. .
  4820. <p>[foo]<a href="/url1">bar</a></p>
  4821. .
  4822. ## Images
  4823. Syntax for images is like the syntax for links, with one
  4824. difference. Instead of [link text](#link-text), we have an [image
  4825. description](@image-description). The rules for this are the
  4826. same as for [link text](#link-text), except that (a) an
  4827. image description starts with `![` rather than `[`, and
  4828. (b) an image description may contain links.
  4829. An image description has inline elements
  4830. as its contents. When an image is rendered to HTML,
  4831. this is standardly used as the image's `alt` attribute.
  4832. .
  4833. ![foo](/url "title")
  4834. .
  4835. <p><img src="/url" alt="foo" title="title" /></p>
  4836. .
  4837. .
  4838. ![foo *bar*]
  4839. [foo *bar*]: train.jpg "train & tracks"
  4840. .
  4841. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4842. .
  4843. .
  4844. ![foo ![bar](/url)](/url2)
  4845. .
  4846. <p><img src="/url2" alt="foo bar" /></p>
  4847. .
  4848. .
  4849. ![foo [bar](/url)](/url2)
  4850. .
  4851. <p><img src="/url2" alt="foo bar" /></p>
  4852. .
  4853. Though this spec is concerned with parsing, not rendering, it is
  4854. recommended that in rendering to HTML, only the plain string content
  4855. of the [image description](#image-description) be used. Note that in
  4856. the above example, the alt attribute's value is `foo bar`, not `foo
  4857. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  4858. content is rendered, without formatting.
  4859. .
  4860. ![foo *bar*][]
  4861. [foo *bar*]: train.jpg "train & tracks"
  4862. .
  4863. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4864. .
  4865. .
  4866. ![foo *bar*][foobar]
  4867. [FOOBAR]: train.jpg "train & tracks"
  4868. .
  4869. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  4870. .
  4871. .
  4872. ![foo](train.jpg)
  4873. .
  4874. <p><img src="train.jpg" alt="foo" /></p>
  4875. .
  4876. .
  4877. My ![foo bar](/path/to/train.jpg "title" )
  4878. .
  4879. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  4880. .
  4881. .
  4882. ![foo](<url>)
  4883. .
  4884. <p><img src="url" alt="foo" /></p>
  4885. .
  4886. .
  4887. ![](/url)
  4888. .
  4889. <p><img src="/url" alt="" /></p>
  4890. .
  4891. Reference-style:
  4892. .
  4893. ![foo] [bar]
  4894. [bar]: /url
  4895. .
  4896. <p><img src="/url" alt="foo" /></p>
  4897. .
  4898. .
  4899. ![foo] [bar]
  4900. [BAR]: /url
  4901. .
  4902. <p><img src="/url" alt="foo" /></p>
  4903. .
  4904. Collapsed:
  4905. .
  4906. ![foo][]
  4907. [foo]: /url "title"
  4908. .
  4909. <p><img src="/url" alt="foo" title="title" /></p>
  4910. .
  4911. .
  4912. ![*foo* bar][]
  4913. [*foo* bar]: /url "title"
  4914. .
  4915. <p><img src="/url" alt="foo bar" title="title" /></p>
  4916. .
  4917. The labels are case-insensitive:
  4918. .
  4919. ![Foo][]
  4920. [foo]: /url "title"
  4921. .
  4922. <p><img src="/url" alt="Foo" title="title" /></p>
  4923. .
  4924. As with full reference links, whitespace is allowed
  4925. between the two sets of brackets:
  4926. .
  4927. ![foo]
  4928. []
  4929. [foo]: /url "title"
  4930. .
  4931. <p><img src="/url" alt="foo" title="title" /></p>
  4932. .
  4933. Shortcut:
  4934. .
  4935. ![foo]
  4936. [foo]: /url "title"
  4937. .
  4938. <p><img src="/url" alt="foo" title="title" /></p>
  4939. .
  4940. .
  4941. ![*foo* bar]
  4942. [*foo* bar]: /url "title"
  4943. .
  4944. <p><img src="/url" alt="foo bar" title="title" /></p>
  4945. .
  4946. Note that link labels cannot contain unescaped brackets:
  4947. .
  4948. ![[foo]]
  4949. [[foo]]: /url "title"
  4950. .
  4951. <p>![[foo]]</p>
  4952. <p>[[foo]]: /url &quot;title&quot;</p>
  4953. .
  4954. The link labels are case-insensitive:
  4955. .
  4956. ![Foo]
  4957. [foo]: /url "title"
  4958. .
  4959. <p><img src="/url" alt="Foo" title="title" /></p>
  4960. .
  4961. If you just want bracketed text, you can backslash-escape the
  4962. opening `!` and `[`:
  4963. .
  4964. \!\[foo]
  4965. [foo]: /url "title"
  4966. .
  4967. <p>![foo]</p>
  4968. .
  4969. If you want a link after a literal `!`, backslash-escape the
  4970. `!`:
  4971. .
  4972. \![foo]
  4973. [foo]: /url "title"
  4974. .
  4975. <p>!<a href="/url" title="title">foo</a></p>
  4976. .
  4977. ## Autolinks
  4978. [Autolinks](@autolink) are absolute URIs and email addresses inside `<` and `>`.
  4979. They are parsed as links, with the URL or email address as the link
  4980. label.
  4981. A [URI autolink](@uri-autolink)
  4982. consists of `<`, followed by an [absolute
  4983. URI](#absolute-uri) not containing `<`, followed by `>`. It is parsed
  4984. as a link to the URI, with the URI as the link's label.
  4985. An [absolute URI](@absolute-uri),
  4986. for these purposes, consists of a [scheme](#scheme) followed by a colon (`:`)
  4987. followed by zero or more characters other than ASCII whitespace and
  4988. control characters, `<`, and `>`. If the URI includes these characters,
  4989. you must use percent-encoding (e.g. `%20` for a space).
  4990. The following [schemes](@scheme)
  4991. are recognized (case-insensitive):
  4992. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  4993. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  4994. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  4995. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  4996. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  4997. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  4998. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  4999. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5000. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5001. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5002. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5003. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5004. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5005. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5006. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5007. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5008. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5009. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5010. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5011. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5012. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5013. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5014. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5015. Here are some valid autolinks:
  5016. .
  5017. <http://foo.bar.baz>
  5018. .
  5019. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5020. .
  5021. .
  5022. <http://foo.bar.baz?q=hello&id=22&boolean>
  5023. .
  5024. <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>
  5025. .
  5026. .
  5027. <irc://foo.bar:2233/baz>
  5028. .
  5029. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5030. .
  5031. Uppercase is also fine:
  5032. .
  5033. <MAILTO:FOO@BAR.BAZ>
  5034. .
  5035. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5036. .
  5037. Spaces are not allowed in autolinks:
  5038. .
  5039. <http://foo.bar/baz bim>
  5040. .
  5041. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5042. .
  5043. An [email autolink](@email-autolink)
  5044. consists of `<`, followed by an [email address](#email-address),
  5045. followed by `>`. The link's label is the email address,
  5046. and the URL is `mailto:` followed by the email address.
  5047. An [email address](@email-address),
  5048. for these purposes, is anything that matches
  5049. the [non-normative regex from the HTML5
  5050. spec](http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#e-mail-state-%28type=email%29):
  5051. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5052. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5053. Examples of email autolinks:
  5054. .
  5055. <foo@bar.example.com>
  5056. .
  5057. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5058. .
  5059. .
  5060. <foo+special@Bar.baz-bar0.com>
  5061. .
  5062. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5063. .
  5064. These are not autolinks:
  5065. .
  5066. <>
  5067. .
  5068. <p>&lt;&gt;</p>
  5069. .
  5070. .
  5071. <heck://bing.bong>
  5072. .
  5073. <p>&lt;heck://bing.bong&gt;</p>
  5074. .
  5075. .
  5076. < http://foo.bar >
  5077. .
  5078. <p>&lt; http://foo.bar &gt;</p>
  5079. .
  5080. .
  5081. <foo.bar.baz>
  5082. .
  5083. <p>&lt;foo.bar.baz&gt;</p>
  5084. .
  5085. .
  5086. <localhost:5001/foo>
  5087. .
  5088. <p>&lt;localhost:5001/foo&gt;</p>
  5089. .
  5090. .
  5091. http://example.com
  5092. .
  5093. <p>http://example.com</p>
  5094. .
  5095. .
  5096. foo@bar.example.com
  5097. .
  5098. <p>foo@bar.example.com</p>
  5099. .
  5100. ## Raw HTML
  5101. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5102. raw HTML tag and will be rendered in HTML without escaping.
  5103. Tag and attribute names are not limited to current HTML tags,
  5104. so custom tags (and even, say, DocBook tags) may be used.
  5105. Here is the grammar for tags:
  5106. A [tag name](@tag-name) consists of an ASCII letter
  5107. followed by zero or more ASCII letters or digits.
  5108. An [attribute](@attribute) consists of whitespace,
  5109. an [attribute name](#attribute-name), and an optional
  5110. [attribute value specification](#attribute-value-specification).
  5111. An [attribute name](@attribute-name)
  5112. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5113. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5114. specification restricted to ASCII. HTML5 is laxer.)
  5115. An [attribute value specification](@attribute-value-specification)
  5116. consists of optional whitespace,
  5117. a `=` character, optional whitespace, and an [attribute
  5118. value](#attribute-value).
  5119. An [attribute value](@attribute-value)
  5120. consists of an [unquoted attribute value](#unquoted-attribute-value),
  5121. a [single-quoted attribute value](#single-quoted-attribute-value),
  5122. or a [double-quoted attribute value](#double-quoted-attribute-value).
  5123. An [unquoted attribute value](@unquoted-attribute-value)
  5124. is a nonempty string of characters not
  5125. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5126. A [single-quoted attribute value](@single-quoted-attribute-value)
  5127. consists of `'`, zero or more
  5128. characters not including `'`, and a final `'`.
  5129. A [double-quoted attribute value](@double-quoted-attribute-value)
  5130. consists of `"`, zero or more
  5131. characters not including `"`, and a final `"`.
  5132. An [open tag](@open-tag) consists of a `<` character,
  5133. a [tag name](#tag-name), zero or more [attributes](#attribute),
  5134. optional whitespace, an optional `/` character, and a `>` character.
  5135. A [closing tag](@closing-tag) consists of the
  5136. string `</`, a [tag name](#tag-name), optional whitespace, and the
  5137. character `>`.
  5138. An [HTML comment](@html-comment) consists of the
  5139. string `<!--`, a string of characters not including the string `--`, and
  5140. the string `-->`.
  5141. A [processing instruction](@processing-instruction)
  5142. consists of the string `<?`, a string
  5143. of characters not including the string `?>`, and the string
  5144. `?>`.
  5145. A [declaration](@declaration) consists of the
  5146. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5147. whitespace, a string of characters not including the character `>`, and
  5148. the character `>`.
  5149. A [CDATA section](@cdata-section) consists of
  5150. the string `<![CDATA[`, a string of characters not including the string
  5151. `]]>`, and the string `]]>`.
  5152. An [HTML tag](@html-tag) consists of an [open
  5153. tag](#open-tag), a [closing tag](#closing-tag), an [HTML
  5154. comment](#html-comment), a [processing
  5155. instruction](#processing-instruction), an [element type
  5156. declaration](#element-type-declaration), or a [CDATA
  5157. section](#cdata-section).
  5158. Here are some simple open tags:
  5159. .
  5160. <a><bab><c2c>
  5161. .
  5162. <p><a><bab><c2c></p>
  5163. .
  5164. Empty elements:
  5165. .
  5166. <a/><b2/>
  5167. .
  5168. <p><a/><b2/></p>
  5169. .
  5170. Whitespace is allowed:
  5171. .
  5172. <a /><b2
  5173. data="foo" >
  5174. .
  5175. <p><a /><b2
  5176. data="foo" ></p>
  5177. .
  5178. With attributes:
  5179. .
  5180. <a foo="bar" bam = 'baz <em>"</em>'
  5181. _boolean zoop:33=zoop:33 />
  5182. .
  5183. <p><a foo="bar" bam = 'baz <em>"</em>'
  5184. _boolean zoop:33=zoop:33 /></p>
  5185. .
  5186. Illegal tag names, not parsed as HTML:
  5187. .
  5188. <33> <__>
  5189. .
  5190. <p>&lt;33&gt; &lt;__&gt;</p>
  5191. .
  5192. Illegal attribute names:
  5193. .
  5194. <a h*#ref="hi">
  5195. .
  5196. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5197. .
  5198. Illegal attribute values:
  5199. .
  5200. <a href="hi'> <a href=hi'>
  5201. .
  5202. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5203. .
  5204. Illegal whitespace:
  5205. .
  5206. < a><
  5207. foo><bar/ >
  5208. .
  5209. <p>&lt; a&gt;&lt;
  5210. foo&gt;&lt;bar/ &gt;</p>
  5211. .
  5212. Missing whitespace:
  5213. .
  5214. <a href='bar'title=title>
  5215. .
  5216. <p>&lt;a href='bar'title=title&gt;</p>
  5217. .
  5218. Closing tags:
  5219. .
  5220. </a>
  5221. </foo >
  5222. .
  5223. <p></a>
  5224. </foo ></p>
  5225. .
  5226. Illegal attributes in closing tag:
  5227. .
  5228. </a href="foo">
  5229. .
  5230. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5231. .
  5232. Comments:
  5233. .
  5234. foo <!-- this is a
  5235. comment - with hyphen -->
  5236. .
  5237. <p>foo <!-- this is a
  5238. comment - with hyphen --></p>
  5239. .
  5240. .
  5241. foo <!-- not a comment -- two hyphens -->
  5242. .
  5243. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5244. .
  5245. Processing instructions:
  5246. .
  5247. foo <?php echo $a; ?>
  5248. .
  5249. <p>foo <?php echo $a; ?></p>
  5250. .
  5251. Declarations:
  5252. .
  5253. foo <!ELEMENT br EMPTY>
  5254. .
  5255. <p>foo <!ELEMENT br EMPTY></p>
  5256. .
  5257. CDATA sections:
  5258. .
  5259. foo <![CDATA[>&<]]>
  5260. .
  5261. <p>foo <![CDATA[>&<]]></p>
  5262. .
  5263. Entities are preserved in HTML attributes:
  5264. .
  5265. <a href="&ouml;">
  5266. .
  5267. <p><a href="&ouml;"></p>
  5268. .
  5269. Backslash escapes do not work in HTML attributes:
  5270. .
  5271. <a href="\*">
  5272. .
  5273. <p><a href="\*"></p>
  5274. .
  5275. .
  5276. <a href="\"">
  5277. .
  5278. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5279. .
  5280. ## Hard line breaks
  5281. A line break (not in a code span or HTML tag) that is preceded
  5282. by two or more spaces and does not occur at the end of a block
  5283. is parsed as a [hard line break](@hard-line-break) (rendered
  5284. in HTML as a `<br />` tag):
  5285. .
  5286. foo
  5287. baz
  5288. .
  5289. <p>foo<br />
  5290. baz</p>
  5291. .
  5292. For a more visible alternative, a backslash before the newline may be
  5293. used instead of two spaces:
  5294. .
  5295. foo\
  5296. baz
  5297. .
  5298. <p>foo<br />
  5299. baz</p>
  5300. .
  5301. More than two spaces can be used:
  5302. .
  5303. foo
  5304. baz
  5305. .
  5306. <p>foo<br />
  5307. baz</p>
  5308. .
  5309. Leading spaces at the beginning of the next line are ignored:
  5310. .
  5311. foo
  5312. bar
  5313. .
  5314. <p>foo<br />
  5315. bar</p>
  5316. .
  5317. .
  5318. foo\
  5319. bar
  5320. .
  5321. <p>foo<br />
  5322. bar</p>
  5323. .
  5324. Line breaks can occur inside emphasis, links, and other constructs
  5325. that allow inline content:
  5326. .
  5327. *foo
  5328. bar*
  5329. .
  5330. <p><em>foo<br />
  5331. bar</em></p>
  5332. .
  5333. .
  5334. *foo\
  5335. bar*
  5336. .
  5337. <p><em>foo<br />
  5338. bar</em></p>
  5339. .
  5340. Line breaks do not occur inside code spans
  5341. .
  5342. `code
  5343. span`
  5344. .
  5345. <p><code>code span</code></p>
  5346. .
  5347. .
  5348. `code\
  5349. span`
  5350. .
  5351. <p><code>code\ span</code></p>
  5352. .
  5353. or HTML tags:
  5354. .
  5355. <a href="foo
  5356. bar">
  5357. .
  5358. <p><a href="foo
  5359. bar"></p>
  5360. .
  5361. .
  5362. <a href="foo\
  5363. bar">
  5364. .
  5365. <p><a href="foo\
  5366. bar"></p>
  5367. .
  5368. Hard line breaks are for separating inline content within a block.
  5369. Neither syntax for hard line breaks works at the end of a paragraph or
  5370. other block element:
  5371. .
  5372. foo\
  5373. .
  5374. <p>foo\</p>
  5375. .
  5376. .
  5377. foo
  5378. .
  5379. <p>foo</p>
  5380. .
  5381. .
  5382. ### foo\
  5383. .
  5384. <h3>foo\</h3>
  5385. .
  5386. .
  5387. ### foo
  5388. .
  5389. <h3>foo</h3>
  5390. .
  5391. ## Soft line breaks
  5392. A regular line break (not in a code span or HTML tag) that is not
  5393. preceded by two or more spaces is parsed as a softbreak. (A
  5394. softbreak may be rendered in HTML either as a newline or as a space.
  5395. The result will be the same in browsers. In the examples here, a
  5396. newline will be used.)
  5397. .
  5398. foo
  5399. baz
  5400. .
  5401. <p>foo
  5402. baz</p>
  5403. .
  5404. Spaces at the end of the line and beginning of the next line are
  5405. removed:
  5406. .
  5407. foo
  5408. baz
  5409. .
  5410. <p>foo
  5411. baz</p>
  5412. .
  5413. A conforming parser may render a soft line break in HTML either as a
  5414. line break or as a space.
  5415. A renderer may also provide an option to render soft line breaks
  5416. as hard line breaks.
  5417. ## Strings
  5418. Any characters not given an interpretation by the above rules will
  5419. be parsed as string content.
  5420. .
  5421. hello $.;'there
  5422. .
  5423. <p>hello $.;'there</p>
  5424. .
  5425. .
  5426. Foo χρῆν
  5427. .
  5428. <p>Foo χρῆν</p>
  5429. .
  5430. Internal spaces are preserved verbatim:
  5431. .
  5432. Multiple spaces
  5433. .
  5434. <p>Multiple spaces</p>
  5435. .
  5436. <!-- END TESTS -->
  5437. # Appendix A: A parsing strategy {-}
  5438. ## Overview {-}
  5439. Parsing has two phases:
  5440. 1. In the first phase, lines of input are consumed and the block
  5441. structure of the document---its division into paragraphs, block quotes,
  5442. list items, and so on---is constructed. Text is assigned to these
  5443. blocks but not parsed. Link reference definitions are parsed and a
  5444. map of links is constructed.
  5445. 2. In the second phase, the raw text contents of paragraphs and headers
  5446. are parsed into sequences of Markdown inline elements (strings,
  5447. code spans, links, emphasis, and so on), using the map of link
  5448. references constructed in phase 1.
  5449. ## The document tree {-}
  5450. At each point in processing, the document is represented as a tree of
  5451. **blocks**. The root of the tree is a `document` block. The `document`
  5452. may have any number of other blocks as **children**. These children
  5453. may, in turn, have other blocks as children. The last child of a block
  5454. is normally considered **open**, meaning that subsequent lines of input
  5455. can alter its contents. (Blocks that are not open are **closed**.)
  5456. Here, for example, is a possible document tree, with the open blocks
  5457. marked by arrows:
  5458. ``` tree
  5459. -> document
  5460. -> block_quote
  5461. paragraph
  5462. "Lorem ipsum dolor\nsit amet."
  5463. -> list (type=bullet tight=true bullet_char=-)
  5464. list_item
  5465. paragraph
  5466. "Qui *quodsi iracundia*"
  5467. -> list_item
  5468. -> paragraph
  5469. "aliquando id"
  5470. ```
  5471. ## How source lines alter the document tree {-}
  5472. Each line that is processed has an effect on this tree. The line is
  5473. analyzed and, depending on its contents, the document may be altered
  5474. in one or more of the following ways:
  5475. 1. One or more open blocks may be closed.
  5476. 2. One or more new blocks may be created as children of the
  5477. last open block.
  5478. 3. Text may be added to the last (deepest) open block remaining
  5479. on the tree.
  5480. Once a line has been incorporated into the tree in this way,
  5481. it can be discarded, so input can be read in a stream.
  5482. We can see how this works by considering how the tree above is
  5483. generated by four lines of Markdown:
  5484. ``` markdown
  5485. > Lorem ipsum dolor
  5486. sit amet.
  5487. > - Qui *quodsi iracundia*
  5488. > - aliquando id
  5489. ```
  5490. At the outset, our document model is just
  5491. ``` tree
  5492. -> document
  5493. ```
  5494. The first line of our text,
  5495. ``` markdown
  5496. > Lorem ipsum dolor
  5497. ```
  5498. causes a `block_quote` block to be created as a child of our
  5499. open `document` block, and a `paragraph` block as a child of
  5500. the `block_quote`. Then the text is added to the last open
  5501. block, the `paragraph`:
  5502. ``` tree
  5503. -> document
  5504. -> block_quote
  5505. -> paragraph
  5506. "Lorem ipsum dolor"
  5507. ```
  5508. The next line,
  5509. ``` markdown
  5510. sit amet.
  5511. ```
  5512. is a "lazy continuation" of the open `paragraph`, so it gets added
  5513. to the paragraph's text:
  5514. ``` tree
  5515. -> document
  5516. -> block_quote
  5517. -> paragraph
  5518. "Lorem ipsum dolor\nsit amet."
  5519. ```
  5520. The third line,
  5521. ``` markdown
  5522. > - Qui *quodsi iracundia*
  5523. ```
  5524. causes the `paragraph` block to be closed, and a new `list` block
  5525. opened as a child of the `block_quote`. A `list_item` is also
  5526. added as a child of the `list`, and a `paragraph` as a child of
  5527. the `list_item`. The text is then added to the new `paragraph`:
  5528. ``` tree
  5529. -> document
  5530. -> block_quote
  5531. paragraph
  5532. "Lorem ipsum dolor\nsit amet."
  5533. -> list (type=bullet tight=true bullet_char=-)
  5534. -> list_item
  5535. -> paragraph
  5536. "Qui *quodsi iracundia*"
  5537. ```
  5538. The fourth line,
  5539. ``` markdown
  5540. > - aliquando id
  5541. ```
  5542. causes the `list_item` (and its child the `paragraph`) to be closed,
  5543. and a new `list_item` opened up as child of the `list`. A `paragraph`
  5544. is added as a child of the new `list_item`, to contain the text.
  5545. We thus obtain the final tree:
  5546. ``` tree
  5547. -> document
  5548. -> block_quote
  5549. paragraph
  5550. "Lorem ipsum dolor\nsit amet."
  5551. -> list (type=bullet tight=true bullet_char=-)
  5552. list_item
  5553. paragraph
  5554. "Qui *quodsi iracundia*"
  5555. -> list_item
  5556. -> paragraph
  5557. "aliquando id"
  5558. ```
  5559. ## From block structure to the final document {-}
  5560. Once all of the input has been parsed, all open blocks are closed.
  5561. We then "walk the tree," visiting every node, and parse raw
  5562. string contents of paragraphs and headers as inlines. At this
  5563. point we have seen all the link reference definitions, so we can
  5564. resolve reference links as we go.
  5565. ``` tree
  5566. document
  5567. block_quote
  5568. paragraph
  5569. str "Lorem ipsum dolor"
  5570. softbreak
  5571. str "sit amet."
  5572. list (type=bullet tight=true bullet_char=-)
  5573. list_item
  5574. paragraph
  5575. str "Qui "
  5576. emph
  5577. str "quodsi iracundia"
  5578. list_item
  5579. paragraph
  5580. str "aliquando id"
  5581. ```
  5582. Notice how the newline in the first paragraph has been parsed as
  5583. a `softbreak`, and the asterisks in the first list item have become
  5584. an `emph`.
  5585. The document can be rendered as HTML, or in any other format, given
  5586. an appropriate renderer.