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