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