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