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