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