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