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