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