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