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