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