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