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