aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 0a62b803920812b38caf765013d934385880bdc4 (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. `footer`, `tr`, `form`, `ul`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`,
  1095. `video`, `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 appending 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://google.com?find=\*>
  2997. .
  2998. <p><a href="http://google.com?find=%5C*">http://google.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 HTML valid HTML Entities in any
  3029. context are recognized as such and converted into their actual values (i.e. the UTF8 characters representing
  3030. the entity itself) before they are stored in the AST.
  3031. This allows implementations that target HTML output to trivially escape the entities when generating HTML,
  3032. and simplifies the job of implementations targetting other languages, as these will only need to handle the
  3033. UTF8 chars and need not be HTML-entity aware.
  3034. [Named entities](#name-entities) <a id="named-entities"></a> consist of `&`
  3035. + any of the valid HTML5 entity names + `;`. The [following document](http://www.whatwg.org/specs/web-apps/current-work/multipage/entities.json)
  3036. is used as an authoritative source of the valid entity names and their corresponding codepoints.
  3037. Conforming implementations that target Markdown don't need to generate entities for all the valid
  3038. named entities that exist, with the exception of `"` (`&quot;`), `&` (`&amp;`), `<` (`&lt;`) and `>` (`&gt;`),
  3039. which always need to be written as entities for security reasons.
  3040. .
  3041. &nbsp; &amp; &copy; &AElig; &Dcaron; &frac34; &HilbertSpace; &DifferentialD; &ClockwiseContourIntegral;
  3042. .
  3043. <p>  &amp; © Æ Ď ¾ ℋ ⅆ ∲</p>
  3044. .
  3045. [Decimal entities](#decimal-entities) <a id="decimal-entities"></a>
  3046. consist of `&#` + a string of 1--8 arabic digits + `;`. Again, these entities need to be recognised
  3047. and tranformed into their corresponding UTF8 codepoints. Invalid Unicode codepoints will be written
  3048. as the "unknown codepoint" character (`0xFFFD`)
  3049. .
  3050. &#35; &#1234; &#992; &#98765432;
  3051. .
  3052. <p># Ӓ Ϡ �</p>
  3053. .
  3054. [Hexadecimal entities](#hexadecimal-entities) <a id="hexadecimal-entities"></a>
  3055. consist of `&#` + either `X` or `x` + a string of 1-8 hexadecimal digits
  3056. + `;`. They will also be parsed and turned into their corresponding UTF8 values in the AST.
  3057. .
  3058. &#X22; &#XD06; &#xcab;
  3059. .
  3060. <p>&quot; ആ ಫ</p>
  3061. .
  3062. Here are some nonentities:
  3063. .
  3064. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3065. .
  3066. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3067. .
  3068. Although HTML5 does accept some entities without a trailing semicolon
  3069. (such as `&copy`), these are not recognized as entities here, because it makes the grammar too ambiguous:
  3070. .
  3071. &copy
  3072. .
  3073. <p>&amp;copy</p>
  3074. .
  3075. Strings that are not on the list of HTML5 named entities are not recognized as entities either:
  3076. .
  3077. &MadeUpEntity;
  3078. .
  3079. <p>&amp;MadeUpEntity;</p>
  3080. .
  3081. Entities are recognized in any context besides code spans or
  3082. code blocks, including raw HTML, URLs, [link titles](#link-title), and
  3083. [fenced code block](#fenced-code-block) info strings:
  3084. .
  3085. <a href="&ouml;&ouml;.html">
  3086. .
  3087. <p><a href="&ouml;&ouml;.html"></p>
  3088. .
  3089. .
  3090. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3091. .
  3092. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3093. .
  3094. .
  3095. [foo]
  3096. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3097. .
  3098. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3099. .
  3100. .
  3101. ``` f&ouml;&ouml;
  3102. foo
  3103. ```
  3104. .
  3105. <pre><code class="language-föö">foo
  3106. </code></pre>
  3107. .
  3108. Entities are treated as literal text in code spans and code blocks:
  3109. .
  3110. `f&ouml;&ouml;`
  3111. .
  3112. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3113. .
  3114. .
  3115. f&ouml;f&ouml;
  3116. .
  3117. <pre><code>f&amp;ouml;f&amp;ouml;
  3118. </code></pre>
  3119. .
  3120. ## Code span
  3121. A [backtick string](#backtick-string) <a id="backtick-string"></a>
  3122. is a string of one or more backtick characters (`` ` ``) that is neither
  3123. preceded nor followed by a backtick.
  3124. A code span begins with a backtick string and ends with a backtick
  3125. string of equal length. The contents of the code span are the
  3126. characters between the two backtick strings, with leading and trailing
  3127. spaces and newlines removed, and consecutive spaces and newlines
  3128. collapsed to single spaces.
  3129. This is a simple code span:
  3130. .
  3131. `foo`
  3132. .
  3133. <p><code>foo</code></p>
  3134. .
  3135. Here two backticks are used, because the code contains a backtick.
  3136. This example also illustrates stripping of leading and trailing spaces:
  3137. .
  3138. `` foo ` bar ``
  3139. .
  3140. <p><code>foo ` bar</code></p>
  3141. .
  3142. This example shows the motivation for stripping leading and trailing
  3143. spaces:
  3144. .
  3145. ` `` `
  3146. .
  3147. <p><code>``</code></p>
  3148. .
  3149. Newlines are treated like spaces:
  3150. .
  3151. ``
  3152. foo
  3153. ``
  3154. .
  3155. <p><code>foo</code></p>
  3156. .
  3157. Interior spaces and newlines are collapsed into single spaces, just
  3158. as they would be by a browser:
  3159. .
  3160. `foo bar
  3161. baz`
  3162. .
  3163. <p><code>foo bar baz</code></p>
  3164. .
  3165. Q: Why not just leave the spaces, since browsers will collapse them
  3166. anyway? A: Because we might be targeting a non-HTML format, and we
  3167. shouldn't rely on HTML-specific rendering assumptions.
  3168. (Existing implementations differ in their treatment of internal
  3169. spaces and newlines. Some, including `Markdown.pl` and
  3170. `showdown`, convert an internal newline into a `<br />` tag.
  3171. But this makes things difficult for those who like to hard-wrap
  3172. their paragraphs, since a line break in the midst of a code
  3173. span will cause an unintended line break in the output. Others
  3174. just leave internal spaces as they are, which is fine if only
  3175. HTML is being targeted.)
  3176. .
  3177. `foo `` bar`
  3178. .
  3179. <p><code>foo `` bar</code></p>
  3180. .
  3181. Note that backslash escapes do not work in code spans. All backslashes
  3182. are treated literally:
  3183. .
  3184. `foo\`bar`
  3185. .
  3186. <p><code>foo\</code>bar`</p>
  3187. .
  3188. Backslash escapes are never needed, because one can always choose a
  3189. string of *n* backtick characters as delimiters, where the code does
  3190. not contain any strings of exactly *n* backtick characters.
  3191. Code span backticks have higher precedence than any other inline
  3192. constructs except HTML tags and autolinks. Thus, for example, this is
  3193. not parsed as emphasized text, since the second `*` is part of a code
  3194. span:
  3195. .
  3196. *foo`*`
  3197. .
  3198. <p>*foo<code>*</code></p>
  3199. .
  3200. And this is not parsed as a link:
  3201. .
  3202. [not a `link](/foo`)
  3203. .
  3204. <p>[not a <code>link](/foo</code>)</p>
  3205. .
  3206. But this is a link:
  3207. .
  3208. <http://foo.bar.`baz>`
  3209. .
  3210. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3211. .
  3212. And this is an HTML tag:
  3213. .
  3214. <a href="`">`
  3215. .
  3216. <p><a href="`">`</p>
  3217. .
  3218. When a backtick string is not closed by a matching backtick string,
  3219. we just have literal backticks:
  3220. .
  3221. ```foo``
  3222. .
  3223. <p>```foo``</p>
  3224. .
  3225. .
  3226. `foo
  3227. .
  3228. <p>`foo</p>
  3229. .
  3230. ## Emphasis and strong emphasis
  3231. John Gruber's original [Markdown syntax
  3232. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3233. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3234. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3235. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3236. > tag.
  3237. This is enough for most users, but these rules leave much undecided,
  3238. especially when it comes to nested emphasis. The original
  3239. `Markdown.pl` test suite makes it clear that triple `***` and
  3240. `___` delimiters can be used for strong emphasis, and most
  3241. implementations have also allowed the following patterns:
  3242. ``` markdown
  3243. ***strong emph***
  3244. ***strong** in emph*
  3245. ***emph* in strong**
  3246. **in strong *emph***
  3247. *in emph **strong***
  3248. ```
  3249. The following patterns are less widely supported, but the intent
  3250. is clear and they are useful (especially in contexts like bibliography
  3251. entries):
  3252. ``` markdown
  3253. *emph *with emph* in it*
  3254. **strong **with strong** in it**
  3255. ```
  3256. Many implementations have also restricted intraword emphasis to
  3257. the `*` forms, to avoid unwanted emphasis in words containing
  3258. internal underscores. (It is best practice to put these in code
  3259. spans, but users often do not.)
  3260. ``` markdown
  3261. internal emphasis: foo*bar*baz
  3262. no emphasis: foo_bar_baz
  3263. ```
  3264. The following rules capture all of these patterns, while allowing
  3265. for efficient parsing strategies that do not backtrack:
  3266. 1. A single `*` character [can open emphasis](#can-open-emphasis)
  3267. <a id="can-open-emphasis"></a> iff
  3268. (a) it is not part of a sequence of four or more unescaped `*`s,
  3269. (b) it is not followed by whitespace, and
  3270. (c) either it is not followed by a `*` character or it is
  3271. followed immediately by emphasis or strong emphasis.
  3272. 2. A single `_` character [can open emphasis](#can-open-emphasis) iff
  3273. (a) it is not part of a sequence of four or more unescaped `_`s,
  3274. (b) it is not followed by whitespace,
  3275. (c) it is not preceded by an ASCII alphanumeric character, and
  3276. (d) either it is not followed by a `_` character or it is
  3277. followed immediately by emphasis or strong emphasis.
  3278. 3. A single `*` character [can close emphasis](#can-close-emphasis)
  3279. <a id="can-close-emphasis"></a> iff
  3280. (a) it is not part of a sequence of four or more unescaped `*`s, and
  3281. (b) it is not preceded by whitespace.
  3282. 4. A single `_` character [can close emphasis](#can-close-emphasis) iff
  3283. (a) it is not part of a sequence of four or more unescaped `_`s,
  3284. (b) it is not preceded by whitespace, and
  3285. (c) it is not followed by an ASCII alphanumeric character.
  3286. 5. A double `**` [can open strong emphasis](#can-open-strong-emphasis)
  3287. <a id="can-open-strong-emphasis" ></a> iff
  3288. (a) it is not part of a sequence of four or more unescaped `*`s,
  3289. (b) it is not followed by whitespace, and
  3290. (c) either it is not followed by a `*` character or it is
  3291. followed immediately by emphasis.
  3292. 6. A double `__` [can open strong emphasis](#can-open-strong-emphasis)
  3293. iff
  3294. (a) it is not part of a sequence of four or more unescaped `_`s,
  3295. (b) it is not followed by whitespace, and
  3296. (c) it is not preceded by an ASCII alphanumeric character, and
  3297. (d) either it is not followed by a `_` character or it is
  3298. followed immediately by emphasis.
  3299. 7. A double `**` [can close strong emphasis](#can-close-strong-emphasis)
  3300. <a id="can-close-strong-emphasis" ></a> iff
  3301. (a) it is not part of a sequence of four or more unescaped `*`s, and
  3302. (b) it is not preceded by whitespace.
  3303. 8. A double `__` [can close strong emphasis](#can-close-strong-emphasis)
  3304. iff
  3305. (a) it is not part of a sequence of four or more unescaped `_`s,
  3306. (b) it is not preceded by whitespace, and
  3307. (c) it is not followed by an ASCII alphanumeric character.
  3308. 9. Emphasis begins with a delimiter that [can open
  3309. emphasis](#can-open-emphasis) and includes inlines parsed
  3310. sequentially until a delimiter that [can close
  3311. emphasis](#can-close-emphasis), and that uses the same
  3312. character (`_` or `*`) as the opening delimiter, is reached.
  3313. 10. Strong emphasis begins with a delimiter that [can open strong
  3314. emphasis](#can-open-strong-emphasis) and includes inlines parsed
  3315. sequentially until a delimiter that [can close strong
  3316. emphasis](#can-close-strong-emphasis), and that uses the
  3317. same character (`_` or `*`) as the opening delimiter, is reached.
  3318. 11. In case of ambiguity, strong emphasis takes precedence. Thus,
  3319. `**foo**` is `<strong>foo</strong>`, not `<em><em>foo</em></em>`,
  3320. and `***foo***` is `<strong><em>foo</em></strong>`, not
  3321. `<em><strong>foo</strong></em>` or `<em><em><em>foo</em></em></em>`.
  3322. These rules can be illustrated through a series of examples.
  3323. Simple emphasis:
  3324. .
  3325. *foo bar*
  3326. .
  3327. <p><em>foo bar</em></p>
  3328. .
  3329. .
  3330. _foo bar_
  3331. .
  3332. <p><em>foo bar</em></p>
  3333. .
  3334. Simple strong emphasis:
  3335. .
  3336. **foo bar**
  3337. .
  3338. <p><strong>foo bar</strong></p>
  3339. .
  3340. .
  3341. __foo bar__
  3342. .
  3343. <p><strong>foo bar</strong></p>
  3344. .
  3345. Emphasis can continue over line breaks:
  3346. .
  3347. *foo
  3348. bar*
  3349. .
  3350. <p><em>foo
  3351. bar</em></p>
  3352. .
  3353. .
  3354. _foo
  3355. bar_
  3356. .
  3357. <p><em>foo
  3358. bar</em></p>
  3359. .
  3360. .
  3361. **foo
  3362. bar**
  3363. .
  3364. <p><strong>foo
  3365. bar</strong></p>
  3366. .
  3367. .
  3368. __foo
  3369. bar__
  3370. .
  3371. <p><strong>foo
  3372. bar</strong></p>
  3373. .
  3374. Emphasis can contain other inline constructs:
  3375. .
  3376. *foo [bar](/url)*
  3377. .
  3378. <p><em>foo <a href="/url">bar</a></em></p>
  3379. .
  3380. .
  3381. _foo [bar](/url)_
  3382. .
  3383. <p><em>foo <a href="/url">bar</a></em></p>
  3384. .
  3385. .
  3386. **foo [bar](/url)**
  3387. .
  3388. <p><strong>foo <a href="/url">bar</a></strong></p>
  3389. .
  3390. .
  3391. __foo [bar](/url)__
  3392. .
  3393. <p><strong>foo <a href="/url">bar</a></strong></p>
  3394. .
  3395. Symbols contained in other inline constructs will not
  3396. close emphasis:
  3397. .
  3398. *foo [bar*](/url)
  3399. .
  3400. <p>*foo <a href="/url">bar*</a></p>
  3401. .
  3402. .
  3403. _foo [bar_](/url)
  3404. .
  3405. <p>_foo <a href="/url">bar_</a></p>
  3406. .
  3407. .
  3408. **<a href="**">
  3409. .
  3410. <p>**<a href="**"></p>
  3411. .
  3412. .
  3413. __<a href="__">
  3414. .
  3415. <p>__<a href="__"></p>
  3416. .
  3417. .
  3418. *a `*`*
  3419. .
  3420. <p><em>a <code>*</code></em></p>
  3421. .
  3422. .
  3423. _a `_`_
  3424. .
  3425. <p><em>a <code>_</code></em></p>
  3426. .
  3427. .
  3428. **a<http://foo.bar?q=**>
  3429. .
  3430. <p>**a<a href="http://foo.bar?q=**">http://foo.bar?q=**</a></p>
  3431. .
  3432. .
  3433. __a<http://foo.bar?q=__>
  3434. .
  3435. <p>__a<a href="http://foo.bar?q=__">http://foo.bar?q=__</a></p>
  3436. .
  3437. This is not emphasis, because the opening delimiter is
  3438. followed by white space:
  3439. .
  3440. and * foo bar*
  3441. .
  3442. <p>and * foo bar*</p>
  3443. .
  3444. .
  3445. _ foo bar_
  3446. .
  3447. <p>_ foo bar_</p>
  3448. .
  3449. .
  3450. and ** foo bar**
  3451. .
  3452. <p>and ** foo bar**</p>
  3453. .
  3454. .
  3455. __ foo bar__
  3456. .
  3457. <p>__ foo bar__</p>
  3458. .
  3459. This is not emphasis, because the closing delimiter is
  3460. preceded by white space:
  3461. .
  3462. and *foo bar *
  3463. .
  3464. <p>and *foo bar *</p>
  3465. .
  3466. .
  3467. and _foo bar _
  3468. .
  3469. <p>and _foo bar _</p>
  3470. .
  3471. .
  3472. and **foo bar **
  3473. .
  3474. <p>and **foo bar **</p>
  3475. .
  3476. .
  3477. and __foo bar __
  3478. .
  3479. <p>and __foo bar __</p>
  3480. .
  3481. The rules imply that a sequence of four or more unescaped `*` or
  3482. `_` characters will always be parsed as a literal string:
  3483. .
  3484. ****hi****
  3485. .
  3486. <p>****hi****</p>
  3487. .
  3488. .
  3489. _____hi_____
  3490. .
  3491. <p>_____hi_____</p>
  3492. .
  3493. .
  3494. Sign here: _________
  3495. .
  3496. <p>Sign here: _________</p>
  3497. .
  3498. The rules also imply that there can be no empty emphasis or strong
  3499. emphasis:
  3500. .
  3501. ** is not an empty emphasis
  3502. .
  3503. <p>** is not an empty emphasis</p>
  3504. .
  3505. .
  3506. **** is not an empty strong emphasis
  3507. .
  3508. <p>**** is not an empty strong emphasis</p>
  3509. .
  3510. To include `*` or `_` in emphasized sections, use backslash escapes
  3511. or code spans:
  3512. .
  3513. *here is a \**
  3514. .
  3515. <p><em>here is a *</em></p>
  3516. .
  3517. .
  3518. __this is a double underscore (`__`)__
  3519. .
  3520. <p><strong>this is a double underscore (<code>__</code>)</strong></p>
  3521. .
  3522. `*` delimiters allow intra-word emphasis; `_` delimiters do not:
  3523. .
  3524. foo*bar*baz
  3525. .
  3526. <p>foo<em>bar</em>baz</p>
  3527. .
  3528. .
  3529. foo_bar_baz
  3530. .
  3531. <p>foo_bar_baz</p>
  3532. .
  3533. .
  3534. foo__bar__baz
  3535. .
  3536. <p>foo__bar__baz</p>
  3537. .
  3538. .
  3539. _foo_bar_baz_
  3540. .
  3541. <p><em>foo_bar_baz</em></p>
  3542. .
  3543. .
  3544. 11*15*32
  3545. .
  3546. <p>11<em>15</em>32</p>
  3547. .
  3548. .
  3549. 11_15_32
  3550. .
  3551. <p>11_15_32</p>
  3552. .
  3553. Internal underscores will be ignored in underscore-delimited
  3554. emphasis:
  3555. .
  3556. _foo_bar_baz_
  3557. .
  3558. <p><em>foo_bar_baz</em></p>
  3559. .
  3560. .
  3561. __foo__bar__baz__
  3562. .
  3563. <p><strong>foo__bar__baz</strong></p>
  3564. .
  3565. The rules are sufficient for the following nesting patterns:
  3566. .
  3567. ***foo bar***
  3568. .
  3569. <p><strong><em>foo bar</em></strong></p>
  3570. .
  3571. .
  3572. ___foo bar___
  3573. .
  3574. <p><strong><em>foo bar</em></strong></p>
  3575. .
  3576. .
  3577. ***foo** bar*
  3578. .
  3579. <p><em><strong>foo</strong> bar</em></p>
  3580. .
  3581. .
  3582. ___foo__ bar_
  3583. .
  3584. <p><em><strong>foo</strong> bar</em></p>
  3585. .
  3586. .
  3587. ***foo* bar**
  3588. .
  3589. <p><strong><em>foo</em> bar</strong></p>
  3590. .
  3591. .
  3592. ___foo_ bar__
  3593. .
  3594. <p><strong><em>foo</em> bar</strong></p>
  3595. .
  3596. .
  3597. *foo **bar***
  3598. .
  3599. <p><em>foo <strong>bar</strong></em></p>
  3600. .
  3601. .
  3602. _foo __bar___
  3603. .
  3604. <p><em>foo <strong>bar</strong></em></p>
  3605. .
  3606. .
  3607. **foo *bar***
  3608. .
  3609. <p><strong>foo <em>bar</em></strong></p>
  3610. .
  3611. .
  3612. __foo _bar___
  3613. .
  3614. <p><strong>foo <em>bar</em></strong></p>
  3615. .
  3616. .
  3617. *foo **bar***
  3618. .
  3619. <p><em>foo <strong>bar</strong></em></p>
  3620. .
  3621. .
  3622. _foo __bar___
  3623. .
  3624. <p><em>foo <strong>bar</strong></em></p>
  3625. .
  3626. .
  3627. *foo *bar* baz*
  3628. .
  3629. <p><em>foo <em>bar</em> baz</em></p>
  3630. .
  3631. .
  3632. _foo _bar_ baz_
  3633. .
  3634. <p><em>foo <em>bar</em> baz</em></p>
  3635. .
  3636. .
  3637. **foo **bar** baz**
  3638. .
  3639. <p><strong>foo <strong>bar</strong> baz</strong></p>
  3640. .
  3641. .
  3642. __foo __bar__ baz__
  3643. .
  3644. <p><strong>foo <strong>bar</strong> baz</strong></p>
  3645. .
  3646. .
  3647. *foo **bar** baz*
  3648. .
  3649. <p><em>foo <strong>bar</strong> baz</em></p>
  3650. .
  3651. .
  3652. _foo __bar__ baz_
  3653. .
  3654. <p><em>foo <strong>bar</strong> baz</em></p>
  3655. .
  3656. .
  3657. **foo *bar* baz**
  3658. .
  3659. <p><strong>foo <em>bar</em> baz</strong></p>
  3660. .
  3661. .
  3662. __foo _bar_ baz__
  3663. .
  3664. <p><strong>foo <em>bar</em> baz</strong></p>
  3665. .
  3666. Note that you cannot nest emphasis directly inside emphasis
  3667. using the same delimeter, or strong emphasis directly inside
  3668. strong emphasis:
  3669. .
  3670. **foo**
  3671. .
  3672. <p><strong>foo</strong></p>
  3673. .
  3674. .
  3675. ****foo****
  3676. .
  3677. <p>****foo****</p>
  3678. .
  3679. For these nestings, you need to switch delimiters:
  3680. .
  3681. *_foo_*
  3682. .
  3683. <p><em><em>foo</em></em></p>
  3684. .
  3685. .
  3686. **__foo__**
  3687. .
  3688. <p><strong><strong>foo</strong></strong></p>
  3689. .
  3690. Note that a `*` followed by a `*` can close emphasis, and
  3691. a `**` followed by a `*` can close strong emphasis (and
  3692. similarly for `_` and `__`):
  3693. .
  3694. *foo**
  3695. .
  3696. <p><em>foo</em>*</p>
  3697. .
  3698. .
  3699. *foo *bar**
  3700. .
  3701. <p><em>foo <em>bar</em></em></p>
  3702. .
  3703. .
  3704. **foo***
  3705. .
  3706. <p><strong>foo</strong>*</p>
  3707. .
  3708. .
  3709. ***foo* bar***
  3710. .
  3711. <p><strong><em>foo</em> bar</strong>*</p>
  3712. .
  3713. .
  3714. ***foo** bar***
  3715. .
  3716. <p><em><strong>foo</strong> bar</em>**</p>
  3717. .
  3718. The following contains no strong emphasis, because the opening
  3719. delimiter is closed by the first `*` before `bar`:
  3720. .
  3721. *foo**bar***
  3722. .
  3723. <p><em>foo</em><em>bar</em>**</p>
  3724. .
  3725. However, a string of four or more `****` can never close emphasis:
  3726. .
  3727. *foo****
  3728. .
  3729. <p>*foo****</p>
  3730. .
  3731. Note that there are some asymmetries here:
  3732. .
  3733. *foo**
  3734. **foo*
  3735. .
  3736. <p><em>foo</em>*</p>
  3737. <p>**foo*</p>
  3738. .
  3739. .
  3740. *foo *bar**
  3741. **foo* bar*
  3742. .
  3743. <p><em>foo <em>bar</em></em></p>
  3744. <p><em><em>foo</em> bar</em></p>
  3745. .
  3746. More cases with mismatched delimiters:
  3747. .
  3748. *bar***
  3749. .
  3750. <p><em>bar</em>**</p>
  3751. .
  3752. .
  3753. ***foo*
  3754. .
  3755. <p>***foo*</p>
  3756. .
  3757. .
  3758. **bar***
  3759. .
  3760. <p><strong>bar</strong>*</p>
  3761. .
  3762. .
  3763. ***foo**
  3764. .
  3765. <p>***foo**</p>
  3766. .
  3767. .
  3768. ***foo *bar*
  3769. .
  3770. <p>***foo <em>bar</em></p>
  3771. .
  3772. ## Links
  3773. A link contains a [link label](#link-label) (the visible text),
  3774. a [destination](#destination) (the URI that is the link destination),
  3775. and optionally a [link title](#link-title). There are two basic kinds
  3776. of links in Markdown. In [inline links](#inline-links) the destination
  3777. and title are given immediately after the label. In [reference
  3778. links](#reference-links) the destination and title are defined elsewhere
  3779. in the document.
  3780. A [link label](#link-label) <a id="link-label"></a> consists of
  3781. - an opening `[`, followed by
  3782. - zero or more backtick code spans, autolinks, HTML tags, link labels,
  3783. backslash-escaped ASCII punctuation characters, or non-`]` characters,
  3784. followed by
  3785. - a closing `]`.
  3786. These rules are motivated by the following intuitive ideas:
  3787. - A link label is a container for inline elements.
  3788. - The square brackets bind more tightly than emphasis markers,
  3789. but less tightly than `<>` or `` ` ``.
  3790. - Link labels may contain material in matching square brackets.
  3791. A [link destination](#link-destination) <a id="link-destination"></a>
  3792. consists of either
  3793. - a sequence of zero or more characters between an opening `<` and a
  3794. closing `>` that contains no line breaks or unescaped `<` or `>`
  3795. characters, or
  3796. - a nonempty sequence of characters that does not include
  3797. ASCII space or control characters, and includes parentheses
  3798. only if (a) they are backslash-escaped or (b) they are part of
  3799. a balanced pair of unescaped parentheses that is not itself
  3800. inside a balanced pair of unescaped paretheses.
  3801. A [link title](#link-title) <a id="link-title"></a> consists of either
  3802. - a sequence of zero or more characters between straight double-quote
  3803. characters (`"`), including a `"` character only if it is
  3804. backslash-escaped, or
  3805. - a sequence of zero or more characters between straight single-quote
  3806. characters (`'`), including a `'` character only if it is
  3807. backslash-escaped, or
  3808. - a sequence of zero or more characters between matching parentheses
  3809. (`(...)`), including a `)` character only if it is backslash-escaped.
  3810. An [inline link](#inline-link) <a id="inline-link"></a>
  3811. consists of a [link label](#link-label) followed immediately
  3812. by a left parenthesis `(`, optional whitespace,
  3813. an optional [link destination](#link-destination),
  3814. an optional [link title](#link-title) separated from the link
  3815. destination by whitespace, optional whitespace, and a right
  3816. parenthesis `)`. The link's text consists of the label (excluding
  3817. the enclosing square brackets) parsed as inlines. The link's
  3818. URI consists of the link destination, excluding enclosing `<...>` if
  3819. present, with backslash-escapes in effect as described above. The
  3820. link's title consists of the link title, excluding its enclosing
  3821. delimiters, with backslash-escapes in effect as described above.
  3822. Here is a simple inline link:
  3823. .
  3824. [link](/uri "title")
  3825. .
  3826. <p><a href="/uri" title="title">link</a></p>
  3827. .
  3828. The title may be omitted:
  3829. .
  3830. [link](/uri)
  3831. .
  3832. <p><a href="/uri">link</a></p>
  3833. .
  3834. Both the title and the destination may be omitted:
  3835. .
  3836. [link]()
  3837. .
  3838. <p><a href="">link</a></p>
  3839. .
  3840. .
  3841. [link](<>)
  3842. .
  3843. <p><a href="">link</a></p>
  3844. .
  3845. If the destination contains spaces, it must be enclosed in pointy
  3846. braces:
  3847. .
  3848. [link](/my uri)
  3849. .
  3850. <p>[link](/my uri)</p>
  3851. .
  3852. .
  3853. [link](</my uri>)
  3854. .
  3855. <p><a href="/my%20uri">link</a></p>
  3856. .
  3857. The destination cannot contain line breaks, even with pointy braces:
  3858. .
  3859. [link](foo
  3860. bar)
  3861. .
  3862. <p>[link](foo
  3863. bar)</p>
  3864. .
  3865. One level of balanced parentheses is allowed without escaping:
  3866. .
  3867. [link]((foo)and(bar))
  3868. .
  3869. <p><a href="(foo)and(bar)">link</a></p>
  3870. .
  3871. However, if you have parentheses within parentheses, you need to escape
  3872. or use the `<...>` form:
  3873. .
  3874. [link](foo(and(bar)))
  3875. .
  3876. <p>[link](foo(and(bar)))</p>
  3877. .
  3878. .
  3879. [link](foo(and\(bar\)))
  3880. .
  3881. <p><a href="foo(and(bar))">link</a></p>
  3882. .
  3883. .
  3884. [link](<foo(and(bar))>)
  3885. .
  3886. <p><a href="foo(and(bar))">link</a></p>
  3887. .
  3888. Parentheses and other symbols can also be escaped, as usual
  3889. in Markdown:
  3890. .
  3891. [link](foo\)\:)
  3892. .
  3893. <p><a href="foo):">link</a></p>
  3894. .
  3895. URL-escaping and should be left alone inside the destination, as all URL-escaped characters
  3896. are also valid URL characters. HTML entities in the destination will be parsed into their UTF8
  3897. codepoints, as usual, and optionally URL-escaped when written as HTML.
  3898. .
  3899. [link](foo%20b&auml;)
  3900. .
  3901. <p><a href="foo%20b%C3%A4">link</a></p>
  3902. .
  3903. Note that, because titles can often be parsed as destinations,
  3904. if you try to omit the destination and keep the title, you'll
  3905. get unexpected results:
  3906. .
  3907. [link]("title")
  3908. .
  3909. <p><a href="%22title%22">link</a></p>
  3910. .
  3911. Titles may be in single quotes, double quotes, or parentheses:
  3912. .
  3913. [link](/url "title")
  3914. [link](/url 'title')
  3915. [link](/url (title))
  3916. .
  3917. <p><a href="/url" title="title">link</a>
  3918. <a href="/url" title="title">link</a>
  3919. <a href="/url" title="title">link</a></p>
  3920. .
  3921. Backslash escapes and entities may be used in titles:
  3922. .
  3923. [link](/url "title \"&quot;")
  3924. .
  3925. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  3926. .
  3927. Nested balanced quotes are not allowed without escaping:
  3928. .
  3929. [link](/url "title "and" title")
  3930. .
  3931. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  3932. .
  3933. But it is easy to work around this by using a different quote type:
  3934. .
  3935. [link](/url 'title "and" title')
  3936. .
  3937. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  3938. .
  3939. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  3940. title, and its test suite included a test demonstrating this.
  3941. But it is hard to see a good rationale for the extra complexity this
  3942. brings, since there are already many ways---backslash escaping,
  3943. entities, or using a different quote type for the enclosing title---to
  3944. write titles containing double quotes. `Markdown.pl`'s handling of
  3945. titles has a number of other strange features. For example, it allows
  3946. single-quoted titles in inline links, but not reference links. And, in
  3947. reference links but not inline links, it allows a title to begin with
  3948. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  3949. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  3950. a simple, rational rule that works the same way in inline links and
  3951. link reference definitions.)
  3952. Whitespace is allowed around the destination and title:
  3953. .
  3954. [link]( /uri
  3955. "title" )
  3956. .
  3957. <p><a href="/uri" title="title">link</a></p>
  3958. .
  3959. But it is not allowed between the link label and the
  3960. following parenthesis:
  3961. .
  3962. [link] (/uri)
  3963. .
  3964. <p>[link] (/uri)</p>
  3965. .
  3966. Note that this is not a link, because the closing `]` occurs in
  3967. an HTML tag:
  3968. .
  3969. [foo <bar attr="](baz)">
  3970. .
  3971. <p>[foo <bar attr="](baz)"></p>
  3972. .
  3973. There are three kinds of [reference links](#reference-link):
  3974. <a id="reference-link"></a>
  3975. A [full reference link](#full-reference-link) <a id="full-reference-link"></a>
  3976. consists of a [link label](#link-label), optional whitespace, and
  3977. another [link label](#link-label) that [matches](#matches) a
  3978. [link reference definition](#link-reference-definition) elsewhere in the
  3979. document.
  3980. One label [matches](#matches) <a id="matches"></a>
  3981. another just in case their normalized forms are equal. To normalize a
  3982. label, perform the *unicode case fold* and collapse consecutive internal
  3983. whitespace to a single space. If there are multiple matching reference
  3984. link definitions, the one that comes first in the document is used. (It
  3985. is desirable in such cases to emit a warning.)
  3986. The contents of the first link label are parsed as inlines, which are
  3987. used as the link's text. The link's URI and title are provided by the
  3988. matching [link reference definition](#link-reference-definition).
  3989. Here is a simple example:
  3990. .
  3991. [foo][bar]
  3992. [bar]: /url "title"
  3993. .
  3994. <p><a href="/url" title="title">foo</a></p>
  3995. .
  3996. The first label can contain inline content:
  3997. .
  3998. [*foo\!*][bar]
  3999. [bar]: /url "title"
  4000. .
  4001. <p><a href="/url" title="title"><em>foo!</em></a></p>
  4002. .
  4003. Matching is case-insensitive:
  4004. .
  4005. [foo][BaR]
  4006. [bar]: /url "title"
  4007. .
  4008. <p><a href="/url" title="title">foo</a></p>
  4009. .
  4010. Unicode case fold is used:
  4011. .
  4012. [Толпой][Толпой] is a Russian word.
  4013. [ТОЛПОЙ]: /url
  4014. .
  4015. <p><a href="/url">Толпой</a> is a Russian word.</p>
  4016. .
  4017. Consecutive internal whitespace is treated as one space for
  4018. purposes of determining matching:
  4019. .
  4020. [Foo
  4021. bar]: /url
  4022. [Baz][Foo bar]
  4023. .
  4024. <p><a href="/url">Baz</a></p>
  4025. .
  4026. There can be whitespace between the two labels:
  4027. .
  4028. [foo] [bar]
  4029. [bar]: /url "title"
  4030. .
  4031. <p><a href="/url" title="title">foo</a></p>
  4032. .
  4033. .
  4034. [foo]
  4035. [bar]
  4036. [bar]: /url "title"
  4037. .
  4038. <p><a href="/url" title="title">foo</a></p>
  4039. .
  4040. When there are multiple matching [link reference
  4041. definitions](#link-reference-definition), the first is used:
  4042. .
  4043. [foo]: /url1
  4044. [foo]: /url2
  4045. [bar][foo]
  4046. .
  4047. <p><a href="/url1">bar</a></p>
  4048. .
  4049. Note that matching is performed on normalized strings, not parsed
  4050. inline content. So the following does not match, even though the
  4051. labels define equivalent inline content:
  4052. .
  4053. [bar][foo\!]
  4054. [foo!]: /url
  4055. .
  4056. <p>[bar][foo!]</p>
  4057. .
  4058. A [collapsed reference link](#collapsed-reference-link)
  4059. <a id="collapsed-reference-link"></a> consists of a [link
  4060. label](#link-label) that [matches](#matches) a [link reference
  4061. definition](#link-reference-definition) elsewhere in the
  4062. document, optional whitespace, and the string `[]`. The contents of the
  4063. first link label are parsed as inlines, which are used as the link's
  4064. text. The link's URI and title are provided by the matching reference
  4065. link definition. Thus, `[foo][]` is equivalent to `[foo][foo]`.
  4066. .
  4067. [foo][]
  4068. [foo]: /url "title"
  4069. .
  4070. <p><a href="/url" title="title">foo</a></p>
  4071. .
  4072. .
  4073. [*foo* bar][]
  4074. [*foo* bar]: /url "title"
  4075. .
  4076. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4077. .
  4078. The link labels are case-insensitive:
  4079. .
  4080. [Foo][]
  4081. [foo]: /url "title"
  4082. .
  4083. <p><a href="/url" title="title">Foo</a></p>
  4084. .
  4085. As with full reference links, whitespace is allowed
  4086. between the two sets of brackets:
  4087. .
  4088. [foo]
  4089. []
  4090. [foo]: /url "title"
  4091. .
  4092. <p><a href="/url" title="title">foo</a></p>
  4093. .
  4094. A [shortcut reference link](#shortcut-reference-link)
  4095. <a id="shortcut-reference-link"></a> consists of a [link
  4096. label](#link-label) that [matches](#matches) a [link reference
  4097. definition](#link-reference-definition) elsewhere in the
  4098. document and is not followed by `[]` or a link label.
  4099. The contents of the first link label are parsed as inlines,
  4100. which are used as the link's text. the link's URI and title
  4101. are provided by the matching link reference definition.
  4102. Thus, `[foo]` is equivalent to `[foo][]`.
  4103. .
  4104. [foo]
  4105. [foo]: /url "title"
  4106. .
  4107. <p><a href="/url" title="title">foo</a></p>
  4108. .
  4109. .
  4110. [*foo* bar]
  4111. [*foo* bar]: /url "title"
  4112. .
  4113. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4114. .
  4115. .
  4116. [[*foo* bar]]
  4117. [*foo* bar]: /url "title"
  4118. .
  4119. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  4120. .
  4121. The link labels are case-insensitive:
  4122. .
  4123. [Foo]
  4124. [foo]: /url "title"
  4125. .
  4126. <p><a href="/url" title="title">Foo</a></p>
  4127. .
  4128. If you just want bracketed text, you can backslash-escape the
  4129. opening bracket to avoid links:
  4130. .
  4131. \[foo]
  4132. [foo]: /url "title"
  4133. .
  4134. <p>[foo]</p>
  4135. .
  4136. Note that this is a link, because link labels bind more tightly
  4137. than emphasis:
  4138. .
  4139. [foo*]: /url
  4140. *[foo*]
  4141. .
  4142. <p>*<a href="/url">foo*</a></p>
  4143. .
  4144. However, this is not, because link labels bind less
  4145. tightly than code backticks:
  4146. .
  4147. [foo`]: /url
  4148. [foo`]`
  4149. .
  4150. <p>[foo<code>]</code></p>
  4151. .
  4152. Link labels can contain matched square brackets:
  4153. .
  4154. [[[foo]]]
  4155. [[[foo]]]: /url
  4156. .
  4157. <p><a href="/url">[[foo]]</a></p>
  4158. .
  4159. .
  4160. [[[foo]]]
  4161. [[[foo]]]: /url1
  4162. [foo]: /url2
  4163. .
  4164. <p><a href="/url1">[[foo]]</a></p>
  4165. .
  4166. For non-matching brackets, use backslash escapes:
  4167. .
  4168. [\[foo]
  4169. [\[foo]: /url
  4170. .
  4171. <p><a href="/url">[foo</a></p>
  4172. .
  4173. Full references take precedence over shortcut references:
  4174. .
  4175. [foo][bar]
  4176. [foo]: /url1
  4177. [bar]: /url2
  4178. .
  4179. <p><a href="/url2">foo</a></p>
  4180. .
  4181. In the following case `[bar][baz]` is parsed as a reference,
  4182. `[foo]` as normal text:
  4183. .
  4184. [foo][bar][baz]
  4185. [baz]: /url
  4186. .
  4187. <p>[foo]<a href="/url">bar</a></p>
  4188. .
  4189. Here, though, `[foo][bar]` is parsed as a reference, since
  4190. `[bar]` is defined:
  4191. .
  4192. [foo][bar][baz]
  4193. [baz]: /url1
  4194. [bar]: /url2
  4195. .
  4196. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  4197. .
  4198. Here `[foo]` is not parsed as a shortcut reference, because it
  4199. is followed by a link label (even though `[bar]` is not defined):
  4200. .
  4201. [foo][bar][baz]
  4202. [baz]: /url1
  4203. [foo]: /url2
  4204. .
  4205. <p>[foo]<a href="/url1">bar</a></p>
  4206. .
  4207. ## Images
  4208. An (unescaped) exclamation mark (`!`) followed by a reference or
  4209. inline link will be parsed as an image. The link label will be
  4210. used as the image's alt text, and the link title, if any, will
  4211. be used as the image's title.
  4212. .
  4213. ![foo](/url "title")
  4214. .
  4215. <p><img src="/url" alt="foo" title="title" /></p>
  4216. .
  4217. .
  4218. ![foo *bar*]
  4219. [foo *bar*]: train.jpg "train & tracks"
  4220. .
  4221. <p><img src="train.jpg" alt="foo &lt;em&gt;bar&lt;/em&gt;" title="train &amp; tracks" /></p>
  4222. .
  4223. .
  4224. ![foo *bar*][]
  4225. [foo *bar*]: train.jpg "train & tracks"
  4226. .
  4227. <p><img src="train.jpg" alt="foo &lt;em&gt;bar&lt;/em&gt;" title="train &amp; tracks" /></p>
  4228. .
  4229. .
  4230. ![foo *bar*][foobar]
  4231. [FOOBAR]: train.jpg "train & tracks"
  4232. .
  4233. <p><img src="train.jpg" alt="foo &lt;em&gt;bar&lt;/em&gt;" title="train &amp; tracks" /></p>
  4234. .
  4235. .
  4236. ![foo](train.jpg)
  4237. .
  4238. <p><img src="train.jpg" alt="foo" /></p>
  4239. .
  4240. .
  4241. My ![foo bar](/path/to/train.jpg "title" )
  4242. .
  4243. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  4244. .
  4245. .
  4246. ![foo](<url>)
  4247. .
  4248. <p><img src="url" alt="foo" /></p>
  4249. .
  4250. .
  4251. ![](/url)
  4252. .
  4253. <p><img src="/url" alt="" /></p>
  4254. .
  4255. Reference-style:
  4256. .
  4257. ![foo] [bar]
  4258. [bar]: /url
  4259. .
  4260. <p><img src="/url" alt="foo" /></p>
  4261. .
  4262. .
  4263. ![foo] [bar]
  4264. [BAR]: /url
  4265. .
  4266. <p><img src="/url" alt="foo" /></p>
  4267. .
  4268. Collapsed:
  4269. .
  4270. ![foo][]
  4271. [foo]: /url "title"
  4272. .
  4273. <p><img src="/url" alt="foo" title="title" /></p>
  4274. .
  4275. .
  4276. ![*foo* bar][]
  4277. [*foo* bar]: /url "title"
  4278. .
  4279. <p><img src="/url" alt="&lt;em&gt;foo&lt;/em&gt; bar" title="title" /></p>
  4280. .
  4281. The labels are case-insensitive:
  4282. .
  4283. ![Foo][]
  4284. [foo]: /url "title"
  4285. .
  4286. <p><img src="/url" alt="Foo" title="title" /></p>
  4287. .
  4288. As with full reference links, whitespace is allowed
  4289. between the two sets of brackets:
  4290. .
  4291. ![foo]
  4292. []
  4293. [foo]: /url "title"
  4294. .
  4295. <p><img src="/url" alt="foo" title="title" /></p>
  4296. .
  4297. Shortcut:
  4298. .
  4299. ![foo]
  4300. [foo]: /url "title"
  4301. .
  4302. <p><img src="/url" alt="foo" title="title" /></p>
  4303. .
  4304. .
  4305. ![*foo* bar]
  4306. [*foo* bar]: /url "title"
  4307. .
  4308. <p><img src="/url" alt="&lt;em&gt;foo&lt;/em&gt; bar" title="title" /></p>
  4309. .
  4310. .
  4311. ![[foo]]
  4312. [[foo]]: /url "title"
  4313. .
  4314. <p><img src="/url" alt="[foo]" title="title" /></p>
  4315. .
  4316. The link labels are case-insensitive:
  4317. .
  4318. ![Foo]
  4319. [foo]: /url "title"
  4320. .
  4321. <p><img src="/url" alt="Foo" title="title" /></p>
  4322. .
  4323. If you just want bracketed text, you can backslash-escape the
  4324. opening `!` and `[`:
  4325. .
  4326. \!\[foo]
  4327. [foo]: /url "title"
  4328. .
  4329. <p>![foo]</p>
  4330. .
  4331. If you want a link after a literal `!`, backslash-escape the
  4332. `!`:
  4333. .
  4334. \![foo]
  4335. [foo]: /url "title"
  4336. .
  4337. <p>!<a href="/url" title="title">foo</a></p>
  4338. .
  4339. ## Autolinks
  4340. Autolinks are absolute URIs and email addresses inside `<` and `>`.
  4341. They are parsed as links, with the URL or email address as the link
  4342. label.
  4343. A [URI autolink](#uri-autolink) <a id="uri-autolink"></a>
  4344. consists of `<`, followed by an [absolute
  4345. URI](#absolute-uri) not containing `<`, followed by `>`. It is parsed
  4346. as a link to the URI, with the URI as the link's label.
  4347. An [absolute URI](#absolute-uri), <a id="absolute-uri"></a>
  4348. for these purposes, consists of a [scheme](#scheme) followed by a colon (`:`)
  4349. followed by zero or more characters other than ASCII whitespace and
  4350. control characters, `<`, and `>`. If the URI includes these characters,
  4351. you must use percent-encoding (e.g. `%20` for a space).
  4352. The following [schemes](#scheme) <a id="scheme"></a>
  4353. are recognized (case-insensitive):
  4354. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  4355. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  4356. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  4357. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  4358. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  4359. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  4360. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  4361. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  4362. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  4363. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  4364. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  4365. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  4366. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  4367. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  4368. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  4369. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  4370. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  4371. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  4372. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  4373. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  4374. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  4375. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  4376. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  4377. Here are some valid autolinks:
  4378. .
  4379. <http://foo.bar.baz>
  4380. .
  4381. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  4382. .
  4383. .
  4384. <http://foo.bar.baz?q=hello&id=22&boolean>
  4385. .
  4386. <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>
  4387. .
  4388. .
  4389. <irc://foo.bar:2233/baz>
  4390. .
  4391. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  4392. .
  4393. Uppercase is also fine:
  4394. .
  4395. <MAILTO:FOO@BAR.BAZ>
  4396. .
  4397. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  4398. .
  4399. Spaces are not allowed in autolinks:
  4400. .
  4401. <http://foo.bar/baz bim>
  4402. .
  4403. <p>&lt;http://foo.bar/baz bim&gt;</p>
  4404. .
  4405. An [email autolink](#email-autolink) <a id="email-autolink"></a>
  4406. consists of `<`, followed by an [email address](#email-address),
  4407. followed by `>`. The link's label is the email address,
  4408. and the URL is `mailto:` followed by the email address.
  4409. An [email address](#email-address), <a id="email-address"></a>
  4410. for these purposes, is anything that matches
  4411. the [non-normative regex from the HTML5
  4412. spec](http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#e-mail-state-%28type=email%29):
  4413. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  4414. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  4415. Examples of email autolinks:
  4416. .
  4417. <foo@bar.baz.com>
  4418. .
  4419. <p><a href="mailto:foo@bar.baz.com">foo@bar.baz.com</a></p>
  4420. .
  4421. .
  4422. <foo+special@Bar.baz-bar0.com>
  4423. .
  4424. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  4425. .
  4426. These are not autolinks:
  4427. .
  4428. <>
  4429. .
  4430. <p>&lt;&gt;</p>
  4431. .
  4432. .
  4433. <heck://bing.bong>
  4434. .
  4435. <p>&lt;heck://bing.bong&gt;</p>
  4436. .
  4437. .
  4438. < http://foo.bar >
  4439. .
  4440. <p>&lt; http://foo.bar &gt;</p>
  4441. .
  4442. .
  4443. <foo.bar.baz>
  4444. .
  4445. <p>&lt;foo.bar.baz&gt;</p>
  4446. .
  4447. .
  4448. <localhost:5001/foo>
  4449. .
  4450. <p>&lt;localhost:5001/foo&gt;</p>
  4451. .
  4452. .
  4453. http://google.com
  4454. .
  4455. <p>http://google.com</p>
  4456. .
  4457. .
  4458. foo@bar.baz.com
  4459. .
  4460. <p>foo@bar.baz.com</p>
  4461. .
  4462. ## Raw HTML
  4463. Text between `<` and `>` that looks like an HTML tag is parsed as a
  4464. raw HTML tag and will be rendered in HTML without escaping.
  4465. Tag and attribute names are not limited to current HTML tags,
  4466. so custom tags (and even, say, DocBook tags) may be used.
  4467. Here is the grammar for tags:
  4468. A [tag name](#tag-name) <a id="tag-name"></a> consists of an ASCII letter
  4469. followed by zero or more ASCII letters or digits.
  4470. An [attribute](#attribute) <a id="attribute"></a> consists of whitespace,
  4471. an **attribute name**, and an optional **attribute value
  4472. specification**.
  4473. An [attribute name](#attribute-name) <a id="attribute-name"></a>
  4474. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  4475. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  4476. specification restricted to ASCII. HTML5 is laxer.)
  4477. An [attribute value specification](#attribute-value-specification)
  4478. <a id="attribute-value-specification"></a> consists of optional whitespace,
  4479. a `=` character, optional whitespace, and an [attribute
  4480. value](#attribute-value).
  4481. An [attribute value](#attribute-value) <a id="attribute-value"></a>
  4482. consists of an [unquoted attribute value](#unquoted-attribute-value),
  4483. a [single-quoted attribute value](#single-quoted-attribute-value),
  4484. or a [double-quoted attribute value](#double-quoted-attribute-value).
  4485. An [unquoted attribute value](#unquoted-attribute-value)
  4486. <a id="unquoted-attribute-value"></a> is a nonempty string of characters not
  4487. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  4488. A [single-quoted attribute value](#single-quoted-attribute-value)
  4489. <a id="single-quoted-attribute-value"></a> consists of `'`, zero or more
  4490. characters not including `'`, and a final `'`.
  4491. A [double-quoted attribute value](#double-quoted-attribute-value)
  4492. <a id="double-quoted-attribute-value"></a> consists of `"`, zero or more
  4493. characters not including `"`, and a final `"`.
  4494. An [open tag](#open-tag) <a id="open-tag"></a> consists of a `<` character,
  4495. a [tag name](#tag-name), zero or more [attributes](#attribute),
  4496. optional whitespace, an optional `/` character, and a `>` character.
  4497. A [closing tag](#closing-tag) <a id="closing-tag"></a> consists of the
  4498. string `</`, a [tag name](#tag-name), optional whitespace, and the
  4499. character `>`.
  4500. An [HTML comment](#html-comment) <a id="html-comment"></a> consists of the
  4501. string `<!--`, a string of characters not including the string `--`, and
  4502. the string `-->`.
  4503. A [processing instruction](#processing-instruction)
  4504. <a id="processing-instruction"></a> consists of the string `<?`, a string
  4505. of characters not including the string `?>`, and the string
  4506. `?>`.
  4507. A [declaration](#declaration) <a id="declaration"></a> consists of the
  4508. string `<!`, a name consisting of one or more uppercase ASCII letters,
  4509. whitespace, a string of characters not including the character `>`, and
  4510. the character `>`.
  4511. A [CDATA section](#cdata-section) <a id="cdata-section"></a> consists of
  4512. the string `<![CDATA[`, a string of characters not including the string
  4513. `]]>`, and the string `]]>`.
  4514. An [HTML tag](#html-tag) <a id="html-tag"></a> consists of an [open
  4515. tag](#open-tag), a [closing tag](#closing-tag), an [HTML
  4516. comment](#html-comment), a [processing
  4517. instruction](#processing-instruction), an [element type
  4518. declaration](#element-type-declaration), or a [CDATA
  4519. section](#cdata-section).
  4520. Here are some simple open tags:
  4521. .
  4522. <a><bab><c2c>
  4523. .
  4524. <p><a><bab><c2c></p>
  4525. .
  4526. Empty elements:
  4527. .
  4528. <a/><b2/>
  4529. .
  4530. <p><a/><b2/></p>
  4531. .
  4532. Whitespace is allowed:
  4533. .
  4534. <a /><b2
  4535. data="foo" >
  4536. .
  4537. <p><a /><b2
  4538. data="foo" ></p>
  4539. .
  4540. With attributes:
  4541. .
  4542. <a foo="bar" bam = 'baz <em>"</em>'
  4543. _boolean zoop:33=zoop:33 />
  4544. .
  4545. <p><a foo="bar" bam = 'baz <em>"</em>'
  4546. _boolean zoop:33=zoop:33 /></p>
  4547. .
  4548. Illegal tag names, not parsed as HTML:
  4549. .
  4550. <33> <__>
  4551. .
  4552. <p>&lt;33&gt; &lt;__&gt;</p>
  4553. .
  4554. Illegal attribute names:
  4555. .
  4556. <a h*#ref="hi">
  4557. .
  4558. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  4559. .
  4560. Illegal attribute values:
  4561. .
  4562. <a href="hi'> <a href=hi'>
  4563. .
  4564. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  4565. .
  4566. Illegal whitespace:
  4567. .
  4568. < a><
  4569. foo><bar/ >
  4570. .
  4571. <p>&lt; a&gt;&lt;
  4572. foo&gt;&lt;bar/ &gt;</p>
  4573. .
  4574. Missing whitespace:
  4575. .
  4576. <a href='bar'title=title>
  4577. .
  4578. <p>&lt;a href='bar'title=title&gt;</p>
  4579. .
  4580. Closing tags:
  4581. .
  4582. </a>
  4583. </foo >
  4584. .
  4585. <p></a>
  4586. </foo ></p>
  4587. .
  4588. Illegal attributes in closing tag:
  4589. .
  4590. </a href="foo">
  4591. .
  4592. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  4593. .
  4594. Comments:
  4595. .
  4596. foo <!-- this is a
  4597. comment - with hyphen -->
  4598. .
  4599. <p>foo <!-- this is a
  4600. comment - with hyphen --></p>
  4601. .
  4602. .
  4603. foo <!-- not a comment -- two hyphens -->
  4604. .
  4605. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  4606. .
  4607. Processing instructions:
  4608. .
  4609. foo <?php echo $a; ?>
  4610. .
  4611. <p>foo <?php echo $a; ?></p>
  4612. .
  4613. Declarations:
  4614. .
  4615. foo <!ELEMENT br EMPTY>
  4616. .
  4617. <p>foo <!ELEMENT br EMPTY></p>
  4618. .
  4619. CDATA sections:
  4620. .
  4621. foo <![CDATA[>&<]]>
  4622. .
  4623. <p>foo <![CDATA[>&<]]></p>
  4624. .
  4625. Entities are preserved in HTML attributes:
  4626. .
  4627. <a href="&ouml;">
  4628. .
  4629. <p><a href="&ouml;"></p>
  4630. .
  4631. Backslash escapes do not work in HTML attributes:
  4632. .
  4633. <a href="\*">
  4634. .
  4635. <p><a href="\*"></p>
  4636. .
  4637. .
  4638. <a href="\"">
  4639. .
  4640. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  4641. .
  4642. ## Hard line breaks
  4643. A line break (not in a code span or HTML tag) that is preceded
  4644. by two or more spaces is parsed as a linebreak (rendered
  4645. in HTML as a `<br />` tag):
  4646. .
  4647. foo
  4648. baz
  4649. .
  4650. <p>foo<br />
  4651. baz</p>
  4652. .
  4653. For a more visible alternative, a backslash before the newline may be
  4654. used instead of two spaces:
  4655. .
  4656. foo\
  4657. baz
  4658. .
  4659. <p>foo<br />
  4660. baz</p>
  4661. .
  4662. More than two spaces can be used:
  4663. .
  4664. foo
  4665. baz
  4666. .
  4667. <p>foo<br />
  4668. baz</p>
  4669. .
  4670. Leading spaces at the beginning of the next line are ignored:
  4671. .
  4672. foo
  4673. bar
  4674. .
  4675. <p>foo<br />
  4676. bar</p>
  4677. .
  4678. .
  4679. foo\
  4680. bar
  4681. .
  4682. <p>foo<br />
  4683. bar</p>
  4684. .
  4685. Line breaks can occur inside emphasis, links, and other constructs
  4686. that allow inline content:
  4687. .
  4688. *foo
  4689. bar*
  4690. .
  4691. <p><em>foo<br />
  4692. bar</em></p>
  4693. .
  4694. .
  4695. *foo\
  4696. bar*
  4697. .
  4698. <p><em>foo<br />
  4699. bar</em></p>
  4700. .
  4701. Line breaks do not occur inside code spans
  4702. .
  4703. `code
  4704. span`
  4705. .
  4706. <p><code>code span</code></p>
  4707. .
  4708. .
  4709. `code\
  4710. span`
  4711. .
  4712. <p><code>code\ span</code></p>
  4713. .
  4714. or HTML tags:
  4715. .
  4716. <a href="foo
  4717. bar">
  4718. .
  4719. <p><a href="foo
  4720. bar"></p>
  4721. .
  4722. .
  4723. <a href="foo\
  4724. bar">
  4725. .
  4726. <p><a href="foo\
  4727. bar"></p>
  4728. .
  4729. ## Soft line breaks
  4730. A regular line break (not in a code span or HTML tag) that is not
  4731. preceded by two or more spaces is parsed as a softbreak. (A
  4732. softbreak may be rendered in HTML either as a newline or as a space.
  4733. The result will be the same in browsers. In the examples here, a
  4734. newline will be used.)
  4735. .
  4736. foo
  4737. baz
  4738. .
  4739. <p>foo
  4740. baz</p>
  4741. .
  4742. Spaces at the end of the line and beginning of the next line are
  4743. removed:
  4744. .
  4745. foo
  4746. baz
  4747. .
  4748. <p>foo
  4749. baz</p>
  4750. .
  4751. A conforming parser may render a soft line break in HTML either as a
  4752. line break or as a space.
  4753. A renderer may also provide an option to render soft line breaks
  4754. as hard line breaks.
  4755. ## Strings
  4756. Any characters not given an interpretation by the above rules will
  4757. be parsed as string content.
  4758. .
  4759. hello $.;'there
  4760. .
  4761. <p>hello $.;'there</p>
  4762. .
  4763. .
  4764. Foo χρῆν
  4765. .
  4766. <p>Foo χρῆν</p>
  4767. .
  4768. Internal spaces are preserved verbatim:
  4769. .
  4770. Multiple spaces
  4771. .
  4772. <p>Multiple spaces</p>
  4773. .
  4774. <!-- END TESTS -->
  4775. # Appendix A: A parsing strategy {-}
  4776. ## Overview {-}
  4777. Parsing has two phases:
  4778. 1. In the first phase, lines of input are consumed and the block
  4779. structure of the document---its division into paragraphs, block quotes,
  4780. list items, and so on---is constructed. Text is assigned to these
  4781. blocks but not parsed. Link reference definitions are parsed and a
  4782. map of links is constructed.
  4783. 2. In the second phase, the raw text contents of paragraphs and headers
  4784. are parsed into sequences of Markdown inline elements (strings,
  4785. code spans, links, emphasis, and so on), using the map of link
  4786. references constructed in phase 1.
  4787. ## The document tree {-}
  4788. At each point in processing, the document is represented as a tree of
  4789. **blocks**. The root of the tree is a `document` block. The `document`
  4790. may have any number of other blocks as **children**. These children
  4791. may, in turn, have other blocks as children. The last child of a block
  4792. is normally considered **open**, meaning that subsequent lines of input
  4793. can alter its contents. (Blocks that are not open are **closed**.)
  4794. Here, for example, is a possible document tree, with the open blocks
  4795. marked by arrows:
  4796. ``` tree
  4797. -> document
  4798. -> block_quote
  4799. paragraph
  4800. "Lorem ipsum dolor\nsit amet."
  4801. -> list (type=bullet tight=true bullet_char=-)
  4802. list_item
  4803. paragraph
  4804. "Qui *quodsi iracundia*"
  4805. -> list_item
  4806. -> paragraph
  4807. "aliquando id"
  4808. ```
  4809. ## How source lines alter the document tree {-}
  4810. Each line that is processed has an effect on this tree. The line is
  4811. analyzed and, depending on its contents, the document may be altered
  4812. in one or more of the following ways:
  4813. 1. One or more open blocks may be closed.
  4814. 2. One or more new blocks may be created as children of the
  4815. last open block.
  4816. 3. Text may be added to the last (deepest) open block remaining
  4817. on the tree.
  4818. Once a line has been incorporated into the tree in this way,
  4819. it can be discarded, so input can be read in a stream.
  4820. We can see how this works by considering how the tree above is
  4821. generated by four lines of Markdown:
  4822. ``` markdown
  4823. > Lorem ipsum dolor
  4824. sit amet.
  4825. > - Qui *quodsi iracundia*
  4826. > - aliquando id
  4827. ```
  4828. At the outset, our document model is just
  4829. ``` tree
  4830. -> document
  4831. ```
  4832. The first line of our text,
  4833. ``` markdown
  4834. > Lorem ipsum dolor
  4835. ```
  4836. causes a `block_quote` block to be created as a child of our
  4837. open `document` block, and a `paragraph` block as a child of
  4838. the `block_quote`. Then the text is added to the last open
  4839. block, the `paragraph`:
  4840. ``` tree
  4841. -> document
  4842. -> block_quote
  4843. -> paragraph
  4844. "Lorem ipsum dolor"
  4845. ```
  4846. The next line,
  4847. ``` markdown
  4848. sit amet.
  4849. ```
  4850. is a "lazy continuation" of the open `paragraph`, so it gets added
  4851. to the paragraph's text:
  4852. ``` tree
  4853. -> document
  4854. -> block_quote
  4855. -> paragraph
  4856. "Lorem ipsum dolor\nsit amet."
  4857. ```
  4858. The third line,
  4859. ``` markdown
  4860. > - Qui *quodsi iracundia*
  4861. ```
  4862. causes the `paragraph` block to be closed, and a new `list` block
  4863. opened as a child of the `block_quote`. A `list_item` is also
  4864. added as a child of the `list`, and a `paragraph` as a child of
  4865. the `list_item`. The text is then added to the new `paragraph`:
  4866. ``` tree
  4867. -> document
  4868. -> block_quote
  4869. paragraph
  4870. "Lorem ipsum dolor\nsit amet."
  4871. -> list (type=bullet tight=true bullet_char=-)
  4872. -> list_item
  4873. -> paragraph
  4874. "Qui *quodsi iracundia*"
  4875. ```
  4876. The fourth line,
  4877. ``` markdown
  4878. > - aliquando id
  4879. ```
  4880. causes the `list_item` (and its child the `paragraph`) to be closed,
  4881. and a new `list_item` opened up as child of the `list`. A `paragraph`
  4882. is added as a child of the new `list_item`, to contain the text.
  4883. We thus obtain the final tree:
  4884. ``` tree
  4885. -> document
  4886. -> block_quote
  4887. paragraph
  4888. "Lorem ipsum dolor\nsit amet."
  4889. -> list (type=bullet tight=true bullet_char=-)
  4890. list_item
  4891. paragraph
  4892. "Qui *quodsi iracundia*"
  4893. -> list_item
  4894. -> paragraph
  4895. "aliquando id"
  4896. ```
  4897. ## From block structure to the final document {-}
  4898. Once all of the input has been parsed, all open blocks are closed.
  4899. We then "walk the tree," visiting every node, and parse raw
  4900. string contents of paragraphs and headers as inlines. At this
  4901. point we have seen all the link reference definitions, so we can
  4902. resolve reference links as we go.
  4903. ``` tree
  4904. document
  4905. block_quote
  4906. paragraph
  4907. str "Lorem ipsum dolor"
  4908. softbreak
  4909. str "sit amet."
  4910. list (type=bullet tight=true bullet_char=-)
  4911. list_item
  4912. paragraph
  4913. str "Qui "
  4914. emph
  4915. str "quodsi iracundia"
  4916. list_item
  4917. paragraph
  4918. str "aliquando id"
  4919. ```
  4920. Notice how the newline in the first paragraph has been parsed as
  4921. a `softbreak`, and the asterisks in the first list item have become
  4922. an `emph`.
  4923. The document can be rendered as HTML, or in any other format, given
  4924. an appropriate renderer.