aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 94920de6eafb13d17efdf44a2966b859d1bfc86e (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. reference definitions] can come 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, unless the
  2051. two blanks are contained in a [fenced code block](#fenced-code-block).
  2052. .
  2053. - foo
  2054. bar
  2055. - foo
  2056. bar
  2057. - ```
  2058. foo
  2059. bar
  2060. ```
  2061. .
  2062. <ul>
  2063. <li><p>foo</p>
  2064. <p>bar</p></li>
  2065. <li><p>foo</p></li>
  2066. </ul>
  2067. <p>bar</p>
  2068. <ul>
  2069. <li><pre><code>foo
  2070. bar
  2071. </code></pre></li>
  2072. </ul>
  2073. .
  2074. A list item may contain any kind of block:
  2075. .
  2076. 1. foo
  2077. ```
  2078. bar
  2079. ```
  2080. baz
  2081. > bam
  2082. .
  2083. <ol>
  2084. <li><p>foo</p>
  2085. <pre><code>bar
  2086. </code></pre>
  2087. <p>baz</p>
  2088. <blockquote>
  2089. <p>bam</p>
  2090. </blockquote></li>
  2091. </ol>
  2092. .
  2093. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  2094. constitute a sequence of blocks *Bs* starting with an indented code
  2095. block and not separated from each other by more than one blank line,
  2096. and *M* is a list marker *M* of width *W* followed by
  2097. one space, then the result of prepending *M* and the following
  2098. space to the first line of *Ls*, and indenting subsequent lines of
  2099. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  2100. If a line is empty, then it need not be indented. The type of the
  2101. list item (bullet or ordered) is determined by the type of its list
  2102. marker. If the list item is ordered, then it is also assigned a
  2103. start number, based on the ordered list marker.
  2104. An indented code block will have to be indented four spaces beyond
  2105. the edge of the region where text will be included in the list item.
  2106. In the following case that is 6 spaces:
  2107. .
  2108. - foo
  2109. bar
  2110. .
  2111. <ul>
  2112. <li><p>foo</p>
  2113. <pre><code>bar
  2114. </code></pre></li>
  2115. </ul>
  2116. .
  2117. And in this case it is 11 spaces:
  2118. .
  2119. 10. foo
  2120. bar
  2121. .
  2122. <ol start="10">
  2123. <li><p>foo</p>
  2124. <pre><code>bar
  2125. </code></pre></li>
  2126. </ol>
  2127. .
  2128. If the *first* block in the list item is an indented code block,
  2129. then by rule #2, the contents must be indented *one* space after the
  2130. list marker:
  2131. .
  2132. indented code
  2133. paragraph
  2134. more code
  2135. .
  2136. <pre><code>indented code
  2137. </code></pre>
  2138. <p>paragraph</p>
  2139. <pre><code>more code
  2140. </code></pre>
  2141. .
  2142. .
  2143. 1. indented code
  2144. paragraph
  2145. more code
  2146. .
  2147. <ol>
  2148. <li><pre><code>indented code
  2149. </code></pre>
  2150. <p>paragraph</p>
  2151. <pre><code>more code
  2152. </code></pre></li>
  2153. </ol>
  2154. .
  2155. Note that an additional space indent is interpreted as space
  2156. inside the code block:
  2157. .
  2158. 1. indented code
  2159. paragraph
  2160. more code
  2161. .
  2162. <ol>
  2163. <li><pre><code> indented code
  2164. </code></pre>
  2165. <p>paragraph</p>
  2166. <pre><code>more code
  2167. </code></pre></li>
  2168. </ol>
  2169. .
  2170. Note that rules #1 and #2 only apply to two cases: (a) cases
  2171. in which the lines to be included in a list item begin with a nonspace
  2172. character, and (b) cases in which they begin with an indented code
  2173. block. In a case like the following, where the first block begins with
  2174. a three-space indent, the rules do not allow us to form a list item by
  2175. indenting the whole thing and prepending a list marker:
  2176. .
  2177. foo
  2178. bar
  2179. .
  2180. <p>foo</p>
  2181. <p>bar</p>
  2182. .
  2183. .
  2184. - foo
  2185. bar
  2186. .
  2187. <ul>
  2188. <li>foo</li>
  2189. </ul>
  2190. <p>bar</p>
  2191. .
  2192. This is not a significant restriction, because when a block begins
  2193. with 1-3 spaces indent, the indentation can always be removed without
  2194. a change in interpretation, allowing rule #1 to be applied. So, in
  2195. the above case:
  2196. .
  2197. - foo
  2198. bar
  2199. .
  2200. <ul>
  2201. <li><p>foo</p>
  2202. <p>bar</p></li>
  2203. </ul>
  2204. .
  2205. 3. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  2206. according to rule #1 or #2, then the result of indenting each line
  2207. of *L* by 1-3 spaces (the same for each line) also constitutes a
  2208. list item with the same contents and attributes. If a line is
  2209. empty, then it need not be indented.
  2210. Indented one space:
  2211. .
  2212. 1. A paragraph
  2213. with two lines.
  2214. indented code
  2215. > A block quote.
  2216. .
  2217. <ol>
  2218. <li><p>A paragraph
  2219. with two lines.</p>
  2220. <pre><code>indented code
  2221. </code></pre>
  2222. <blockquote>
  2223. <p>A block quote.</p>
  2224. </blockquote></li>
  2225. </ol>
  2226. .
  2227. Indented two spaces:
  2228. .
  2229. 1. A paragraph
  2230. with two lines.
  2231. indented code
  2232. > A block quote.
  2233. .
  2234. <ol>
  2235. <li><p>A paragraph
  2236. with two lines.</p>
  2237. <pre><code>indented code
  2238. </code></pre>
  2239. <blockquote>
  2240. <p>A block quote.</p>
  2241. </blockquote></li>
  2242. </ol>
  2243. .
  2244. Indented three spaces:
  2245. .
  2246. 1. A paragraph
  2247. with two lines.
  2248. indented code
  2249. > A block quote.
  2250. .
  2251. <ol>
  2252. <li><p>A paragraph
  2253. with two lines.</p>
  2254. <pre><code>indented code
  2255. </code></pre>
  2256. <blockquote>
  2257. <p>A block quote.</p>
  2258. </blockquote></li>
  2259. </ol>
  2260. .
  2261. Four spaces indent gives a code block:
  2262. .
  2263. 1. A paragraph
  2264. with two lines.
  2265. indented code
  2266. > A block quote.
  2267. .
  2268. <pre><code>1. A paragraph
  2269. with two lines.
  2270. indented code
  2271. &gt; A block quote.
  2272. </code></pre>
  2273. .
  2274. 4. **Laziness.** If a string of lines *Ls* constitute a [list
  2275. item](#list-item) with contents *Bs*, then the result of deleting
  2276. some or all of the indentation from one or more lines in which the
  2277. next non-space character after the [list marker](#list--marker) is
  2278. [paragraph continuation text](#paragraph-continuation-text) is a
  2279. list item with the same contents and attributes.
  2280. Here is an example with lazy continuation lines:
  2281. .
  2282. 1. A paragraph
  2283. with two lines.
  2284. indented code
  2285. > A block quote.
  2286. .
  2287. <ol>
  2288. <li><p>A paragraph
  2289. with two lines.</p>
  2290. <pre><code>indented code
  2291. </code></pre>
  2292. <blockquote>
  2293. <p>A block quote.</p>
  2294. </blockquote></li>
  2295. </ol>
  2296. .
  2297. Indentation can be partially deleted:
  2298. .
  2299. 1. A paragraph
  2300. with two lines.
  2301. .
  2302. <ol>
  2303. <li>A paragraph
  2304. with two lines.</li>
  2305. </ol>
  2306. .
  2307. These examples show how laziness can work in nested structures:
  2308. .
  2309. > 1. > Blockquote
  2310. continued here.
  2311. .
  2312. <blockquote>
  2313. <ol>
  2314. <li><blockquote>
  2315. <p>Blockquote
  2316. continued here.</p>
  2317. </blockquote></li>
  2318. </ol>
  2319. </blockquote>
  2320. .
  2321. .
  2322. > 1. > Blockquote
  2323. > continued here.
  2324. .
  2325. <blockquote>
  2326. <ol>
  2327. <li><blockquote>
  2328. <p>Blockquote
  2329. continued here.</p>
  2330. </blockquote></li>
  2331. </ol>
  2332. </blockquote>
  2333. .
  2334. 5. **That's all.** Nothing that is not counted as a list item by rules
  2335. #1--4 counts as a [list item](#block-quote).
  2336. The rules for sublists follow from the general rules above. A sublist
  2337. must be indented the same number of spaces a paragraph would need to be
  2338. in order to be included in the list item.
  2339. So, in this case we need two spaces indent:
  2340. .
  2341. - foo
  2342. - bar
  2343. - baz
  2344. .
  2345. <ul>
  2346. <li>foo
  2347. <ul>
  2348. <li>bar
  2349. <ul>
  2350. <li>baz</li>
  2351. </ul></li>
  2352. </ul></li>
  2353. </ul>
  2354. .
  2355. One is not enough:
  2356. .
  2357. - foo
  2358. - bar
  2359. - baz
  2360. .
  2361. <ul>
  2362. <li>foo</li>
  2363. <li>bar</li>
  2364. <li>baz</li>
  2365. </ul>
  2366. .
  2367. Here we need four, because the list marker is wider:
  2368. .
  2369. 10) foo
  2370. - bar
  2371. .
  2372. <ol start="10">
  2373. <li>foo
  2374. <ul>
  2375. <li>bar</li>
  2376. </ul></li>
  2377. </ol>
  2378. .
  2379. Three is not enough:
  2380. .
  2381. 10) foo
  2382. - bar
  2383. .
  2384. <ol start="10">
  2385. <li>foo</li>
  2386. </ol>
  2387. <ul>
  2388. <li>bar</li>
  2389. </ul>
  2390. .
  2391. A list may be the first block in a list item:
  2392. .
  2393. - - foo
  2394. .
  2395. <ul>
  2396. <li><ul>
  2397. <li>foo</li>
  2398. </ul></li>
  2399. </ul>
  2400. .
  2401. .
  2402. 1. - 2. foo
  2403. .
  2404. <ol>
  2405. <li><ul>
  2406. <li><ol start="2">
  2407. <li>foo</li>
  2408. </ol></li>
  2409. </ul></li>
  2410. </ol>
  2411. .
  2412. A list item may be empty:
  2413. .
  2414. - foo
  2415. -
  2416. - bar
  2417. .
  2418. <ul>
  2419. <li>foo</li>
  2420. <li></li>
  2421. <li>bar</li>
  2422. </ul>
  2423. .
  2424. .
  2425. -
  2426. .
  2427. <ul>
  2428. <li></li>
  2429. </ul>
  2430. .
  2431. ### Motivation
  2432. John Gruber's markdown spec says the following about list items:
  2433. 1. "List markers typically start at the left margin, but may be indented
  2434. by up to three spaces. List markers must be followed by one or more
  2435. spaces or a tab."
  2436. 2. "To make lists look nice, you can wrap items with hanging indents....
  2437. But if you don't want to, you don't have to."
  2438. 3. "List items may consist of multiple paragraphs. Each subsequent
  2439. paragraph in a list item must be indented by either 4 spaces or one
  2440. tab."
  2441. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  2442. but here again, Markdown will allow you to be lazy."
  2443. 5. "To put a blockquote within a list item, the blockquote's `>`
  2444. delimiters need to be indented."
  2445. 6. "To put a code block within a list item, the code block needs to be
  2446. indented twice — 8 spaces or two tabs."
  2447. These rules specify that a paragraph under a list item must be indented
  2448. four spaces (presumably, from the left margin, rather than the start of
  2449. the list marker, but this is not said), and that code under a list item
  2450. must be indented eight spaces instead of the usual four. They also say
  2451. that a block quote must be indented, but not by how much; however, the
  2452. example given has four spaces indentation. Although nothing is said
  2453. about other kinds of block-level content, it is certainly reasonable to
  2454. infer that *all* block elements under a list item, including other
  2455. lists, must be indented four spaces. This principle has been called the
  2456. *four-space rule*.
  2457. The four-space rule is clear and principled, and if the reference
  2458. implementation `Markdown.pl` had followed it, it probably would have
  2459. become the standard. However, `Markdown.pl` allowed paragraphs and
  2460. sublists to start with only two spaces indentation, at least on the
  2461. outer level. Worse, its behavior was inconsistent: a sublist of an
  2462. outer-level list needed two spaces indentation, but a sublist of this
  2463. sublist needed three spaces. It is not surprising, then, that different
  2464. implementations of markdown have developed very different rules for
  2465. determining what comes under a list item. (Pandoc and python-markdown,
  2466. for example, stuck with Gruber's syntax description and the four-space
  2467. rule, while discount, redcarpet, marked, PHP markdown, and others
  2468. followed `Markdown.pl`'s behavior more closely.)
  2469. Unfortunately, given the divergences between implementations, there
  2470. is no way to give a spec for list items that will be guaranteed not
  2471. to break any existing documents. However, the spec given here should
  2472. correctly handle lists formatted with either the four-space rule or
  2473. the more forgiving `Markdown.pl` behavior, provided they are laid out
  2474. in a way that is natural for a human to read.
  2475. The strategy here is to let the width and indentation of the list marker
  2476. determine the indentation necessary for blocks to fall under the list
  2477. item, rather than having a fixed and arbitrary number. The writer can
  2478. think of the body of the list item as a unit which gets indented to the
  2479. right enough to fit the list marker (and any indentation on the list
  2480. marker). (The laziness rule, #4, then allows continuation lines to be
  2481. unindented if needed.)
  2482. This rule is superior, we claim, to any rule requiring a fixed level of
  2483. indentation from the margin. The four-space rule is clear but
  2484. unnatural. It is quite unintuitive that
  2485. ``` markdown
  2486. - foo
  2487. bar
  2488. - baz
  2489. ```
  2490. should be parsed as two lists with an intervening paragraph,
  2491. ``` html
  2492. <ul>
  2493. <li>foo</li>
  2494. </ul>
  2495. <p>bar</p>
  2496. <ul>
  2497. <li>baz</li>
  2498. </ul>
  2499. ```
  2500. as the four-space rule demands, rather than a single list,
  2501. ``` html
  2502. <ul>
  2503. <li><p>foo<p>
  2504. <p>bar></p></li>
  2505. <li><p>baz</p><li>
  2506. </ul>
  2507. ```
  2508. The choice of four spaces is arbitrary. It can be learned, but it is
  2509. not likely to be guessed, and it trips up beginners regularly.
  2510. Would it help to adopt a two-space rule? The problem is that such
  2511. a rule, together with the rule allowing 1--3 spaces indentation of the
  2512. initial list marker, allows text that is indented *less than* the
  2513. original list marker to be included in the list item. For example,
  2514. `Markdown.pl` parses
  2515. ``` markdown
  2516. - one
  2517. two
  2518. ```
  2519. as a single list item, with `two` a continuation paragraph:
  2520. ``` html
  2521. <ul>
  2522. <li><p>one</p>
  2523. <p>two</p></li>
  2524. </ul>
  2525. ```
  2526. and similarly
  2527. ``` markdown
  2528. > - one
  2529. >
  2530. > two
  2531. ```
  2532. as
  2533. ``` html
  2534. <blockquote>
  2535. <ul>
  2536. <li><p>one</p>
  2537. <p>two</p></li>
  2538. </ul>
  2539. </blockquote>
  2540. ```
  2541. This is extremely unintuitive.
  2542. Rather than requiring a fixed indent from the margin, we could require
  2543. a fixed indent (say, two spaces, or even one space) from the list marker (which
  2544. may itself be indented). This proposal would remove the last anomaly
  2545. discussed. Unlike the spec presented above, it would count the following
  2546. as a list item with a subparagraph, even though the paragraph `bar`
  2547. is not indented as far as the first paragraph `foo`:
  2548. ``` markdown
  2549. 10. foo
  2550. bar
  2551. ```
  2552. Arguably this text does read like a list item with `bar` as a subparagraph,
  2553. which may count in favor of the proposal. However, on this proposal indented
  2554. code would have to be indented six spaces after the list marker. And this
  2555. would break a lot of existing markdown, which has the pattern:
  2556. ``` markdown
  2557. 1. foo
  2558. indented code
  2559. ```
  2560. where the code is indented eight spaces. The spec above, by contrast, will
  2561. parse this text as expected, since the code block's indentation is measured
  2562. from the beginning of `foo`.
  2563. The one case that needs special treatment is a list item that *starts*
  2564. with indented code. How much indentation is required in that case, since
  2565. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  2566. that in such cases, we require one space indentation from the list marker
  2567. (and then the normal four spaces for the indented code). This will match the
  2568. four-space rule in cases where the list marker plus its initial indentation
  2569. takes four spaces (a common case), but diverge in other cases.
  2570. ## Lists
  2571. A [list](#list) <a id="list"/> is a sequence of one or more
  2572. list items [of the same type](#of-the-same-type). The list items
  2573. may be separated by single [blank lines](#blank-line), but two
  2574. blank lines end all containing lists.
  2575. Two list items are [of the same type](#of-the-same-type)
  2576. <a id="of-the-same-type"/> if they begin with a [list
  2577. marker](#list-marker) of the same type. Two list markers are of the
  2578. same type if (a) they are bullet list markers using the same character
  2579. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  2580. delimiter (either `.` or `)`).
  2581. A list is an [ordered list](#ordered-list) <a id="ordered-list"/>
  2582. if its constituent list items begin with
  2583. [ordered list markers](#ordered-list-marker), and a [bullet
  2584. list](#bullet-list) <a id="bullet-list"/> if its constituent list
  2585. items begin with [bullet list markers](#bullet-list-marker).
  2586. The [start number](#start-number) <a id="start-number"/>
  2587. of an [ordered list](#ordered-list) is determined by the list number of
  2588. its initial list item. The numbers of subsequent list items are
  2589. disregarded.
  2590. A list is [loose](#loose) if it any of its constituent list items are
  2591. separated by blank lines, or if any of its constituent list items
  2592. directly contain two block-level elements with a blank line between
  2593. them. Otherwise a list is [tight](#tight). (The difference in HTML output
  2594. is that paragraphs in a loose with are wrapped in `<p>` tags, while
  2595. paragraphs in a tight list are not.)
  2596. Changing the bullet or ordered list delimiter starts a new list:
  2597. .
  2598. - foo
  2599. - bar
  2600. + baz
  2601. .
  2602. <ul>
  2603. <li>foo</li>
  2604. <li>bar</li>
  2605. </ul>
  2606. <ul>
  2607. <li>baz</li>
  2608. </ul>
  2609. .
  2610. .
  2611. 1. foo
  2612. 2. bar
  2613. 3) baz
  2614. .
  2615. <ol>
  2616. <li>foo</li>
  2617. <li>bar</li>
  2618. </ol>
  2619. <ol start="3">
  2620. <li>baz</li>
  2621. </ol>
  2622. .
  2623. There can be blank lines between items, but two blank lines end
  2624. a list:
  2625. .
  2626. - foo
  2627. - bar
  2628. - baz
  2629. .
  2630. <ul>
  2631. <li><p>foo</p></li>
  2632. <li><p>bar</p></li>
  2633. </ul>
  2634. <ul>
  2635. <li>baz</li>
  2636. </ul>
  2637. .
  2638. As illustrated above in the section on [list items](#list-item),
  2639. two blank lines between blocks *within* a list item will also end a
  2640. list:
  2641. .
  2642. - foo
  2643. bar
  2644. - baz
  2645. .
  2646. <ul>
  2647. <li>foo</li>
  2648. </ul>
  2649. <p>bar</p>
  2650. <ul>
  2651. <li>baz</li>
  2652. </ul>
  2653. .
  2654. Indeed, two blank lines will end *all* containing lists:
  2655. .
  2656. - foo
  2657. - bar
  2658. - baz
  2659. bim
  2660. .
  2661. <ul>
  2662. <li>foo
  2663. <ul>
  2664. <li>bar
  2665. <ul>
  2666. <li>baz</li>
  2667. </ul></li>
  2668. </ul></li>
  2669. </ul>
  2670. <pre><code> bim
  2671. </code></pre>
  2672. .
  2673. Thus, two blank lines can be used to separate consecutive lists of
  2674. the same type, or to separate a list from an indented code block
  2675. that would otherwise be parsed as a subparagraph of the final list
  2676. item:
  2677. .
  2678. - foo
  2679. - bar
  2680. - baz
  2681. - bim
  2682. .
  2683. <ul>
  2684. <li>foo</li>
  2685. <li>bar</li>
  2686. </ul>
  2687. <ul>
  2688. <li>baz</li>
  2689. <li>bim</li>
  2690. </ul>
  2691. .
  2692. .
  2693. - foo
  2694. notcode
  2695. - foo
  2696. code
  2697. .
  2698. <ul>
  2699. <li><p>foo</p>
  2700. <p>notcode</p></li>
  2701. <li><p>foo</p></li>
  2702. </ul>
  2703. <pre><code>code
  2704. </code></pre>
  2705. .
  2706. List items need not be indented to the same level. The following
  2707. list items will be treated as items at the same list level,
  2708. since none is indented enough to belong to the previous list
  2709. item:
  2710. .
  2711. - a
  2712. - b
  2713. - c
  2714. - d
  2715. - e
  2716. - f
  2717. - g
  2718. .
  2719. <ul>
  2720. <li>a</li>
  2721. <li>b</li>
  2722. <li>c</li>
  2723. <li>d</li>
  2724. <li>e</li>
  2725. <li>f</li>
  2726. <li>g</li>
  2727. </ul>
  2728. .
  2729. This is a loose list, because there is a blank line between
  2730. two of the list items:
  2731. .
  2732. - a
  2733. - b
  2734. - c
  2735. .
  2736. <ul>
  2737. <li><p>a</p></li>
  2738. <li><p>b</p></li>
  2739. <li><p>c</p></li>
  2740. </ul>
  2741. .
  2742. So is this, with a empty second item:
  2743. .
  2744. * a
  2745. *
  2746. * c
  2747. .
  2748. <ul>
  2749. <li><p>a</p></li>
  2750. <li></li>
  2751. <li><p>c</p></li>
  2752. </ul>
  2753. .
  2754. These are loose lists, even though there is no space between the items,
  2755. because one of the items directly contains two block-level elements
  2756. with a blank line between them:
  2757. .
  2758. - a
  2759. - b
  2760. c
  2761. - d
  2762. .
  2763. <ul>
  2764. <li><p>a</p></li>
  2765. <li><p>b</p>
  2766. <p>c</p></li>
  2767. <li><p>d</p></li>
  2768. </ul>
  2769. .
  2770. .
  2771. - a
  2772. - b
  2773. [ref]: /url
  2774. - d
  2775. .
  2776. <ul>
  2777. <li><p>a</p></li>
  2778. <li><p>b</p></li>
  2779. <li><p>d</p></li>
  2780. </ul>
  2781. .
  2782. This is a tight list, because the blank lines are in a code block:
  2783. .
  2784. - a
  2785. - ```
  2786. b
  2787. ```
  2788. - c
  2789. .
  2790. <ul>
  2791. <li>a</li>
  2792. <li><pre><code>b
  2793. </code></pre></li>
  2794. <li>c</li>
  2795. </ul>
  2796. .
  2797. This is a tight list, because the blank line is between two
  2798. paragraphs of a sublist. So the inner list is loose while
  2799. the other list is tight:
  2800. .
  2801. - a
  2802. - b
  2803. c
  2804. - d
  2805. .
  2806. <ul>
  2807. <li>a
  2808. <ul>
  2809. <li><p>b</p>
  2810. <p>c</p></li>
  2811. </ul></li>
  2812. <li>d</li>
  2813. </ul>
  2814. .
  2815. This is a tight list, because the blank line is inside the
  2816. block quote:
  2817. .
  2818. * a
  2819. > b
  2820. >
  2821. * c
  2822. .
  2823. <ul>
  2824. <li>a
  2825. <blockquote>
  2826. <p>b</p>
  2827. </blockquote></li>
  2828. <li>c</li>
  2829. </ul>
  2830. .
  2831. This list is tight, because the consecutive block elements
  2832. are not separated by blank lines:
  2833. .
  2834. - a
  2835. > b
  2836. ```
  2837. c
  2838. ```
  2839. - d
  2840. .
  2841. <ul>
  2842. <li>a
  2843. <blockquote>
  2844. <p>b</p>
  2845. </blockquote>
  2846. <pre><code>c
  2847. </code></pre></li>
  2848. <li>d</li>
  2849. </ul>
  2850. .
  2851. A single-paragraph list is tight:
  2852. .
  2853. - a
  2854. .
  2855. <ul>
  2856. <li>a</li>
  2857. </ul>
  2858. .
  2859. .
  2860. - a
  2861. - b
  2862. .
  2863. <ul>
  2864. <li>a
  2865. <ul>
  2866. <li>b</li>
  2867. </ul></li>
  2868. </ul>
  2869. .
  2870. Here the outer list is loose, the inner list tight:
  2871. .
  2872. * foo
  2873. * bar
  2874. baz
  2875. .
  2876. <ul>
  2877. <li><p>foo</p>
  2878. <ul>
  2879. <li>bar</li>
  2880. </ul>
  2881. <p>baz</p></li>
  2882. </ul>
  2883. .
  2884. .
  2885. - a
  2886. - b
  2887. - c
  2888. - d
  2889. - e
  2890. - f
  2891. .
  2892. <ul>
  2893. <li><p>a</p>
  2894. <ul>
  2895. <li>b</li>
  2896. <li>c</li>
  2897. </ul></li>
  2898. <li><p>d</p>
  2899. <ul>
  2900. <li>e</li>
  2901. <li>f</li>
  2902. </ul></li>
  2903. </ul>
  2904. .
  2905. # Inlines
  2906. Inlines are parsed sequentially from the beginning of the character
  2907. stream to the end (left to right, in left-to-right languages).
  2908. Thus, for example, in
  2909. .
  2910. `hi`lo`
  2911. .
  2912. <p><code>hi</code>lo`</p>
  2913. .
  2914. `hi` is parsed as code, leaving the backtick at the end as a literal
  2915. backtick.
  2916. ## Backslash escapes
  2917. Any ASCII punctuation character may be backslash-escaped:
  2918. .
  2919. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  2920. .
  2921. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  2922. .
  2923. Backslashes before other characters are treated as literal
  2924. backslashes:
  2925. .
  2926. \→\A\a\ \3\φ\«
  2927. .
  2928. <p>\ \A\a\ \3\φ\«</p>
  2929. .
  2930. Escaped characters are treated as regular characters and do
  2931. not have their usual markdown meanings:
  2932. .
  2933. \*not emphasized*
  2934. \<br/> not a tag
  2935. \[not a link](/foo)
  2936. \`not code`
  2937. 1\. not a list
  2938. \* not a list
  2939. \# not a header
  2940. \[foo]: /url "not a reference"
  2941. .
  2942. <p>*not emphasized*
  2943. &lt;br/&gt; not a tag
  2944. [not a link](/foo)
  2945. `not code`
  2946. 1. not a list
  2947. * not a list
  2948. # not a header
  2949. [foo]: /url &quot;not a reference&quot;</p>
  2950. .
  2951. If a backslash is itself escaped, the following character is not:
  2952. .
  2953. \\*emphasis*
  2954. .
  2955. <p>\<em>emphasis</em></p>
  2956. .
  2957. A backslash at the end of the line is a hard line break:
  2958. .
  2959. foo\
  2960. bar
  2961. .
  2962. <p>foo<br />
  2963. bar</p>
  2964. .
  2965. Backslash escapes do not work in code blocks, code spans, autolinks, or
  2966. raw HTML:
  2967. .
  2968. `` \[\` ``
  2969. .
  2970. <p><code>\[\`</code></p>
  2971. .
  2972. .
  2973. \[\]
  2974. .
  2975. <pre><code>\[\]
  2976. </code></pre>
  2977. .
  2978. .
  2979. ~~~
  2980. \[\]
  2981. ~~~
  2982. .
  2983. <pre><code>\[\]
  2984. </code></pre>
  2985. .
  2986. .
  2987. <http://google.com?find=\*>
  2988. .
  2989. <p><a href="http://google.com?find=\*">http://google.com?find=\*</a></p>
  2990. .
  2991. .
  2992. <a href="/bar\/)">
  2993. .
  2994. <p><a href="/bar\/)"></p>
  2995. .
  2996. But they work in all other contexts, including URLs and link titles,
  2997. link references, and info strings in [fenced code
  2998. blocks](#fenced-code-block):
  2999. .
  3000. [foo](/bar\* "ti\*tle")
  3001. .
  3002. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3003. .
  3004. .
  3005. [foo]
  3006. [foo]: /bar\* "ti\*tle"
  3007. .
  3008. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3009. .
  3010. .
  3011. ``` foo\+bar
  3012. foo
  3013. ```
  3014. .
  3015. <pre class="foo+bar"><code>foo
  3016. </code></pre>
  3017. .
  3018. ## Entities
  3019. Entities are parsed as entities, not as literal text, in all contexts
  3020. except code spans and code blocks. Three kinds of entities are recognized.
  3021. [Named entities](#name-entities) <a id="named-entities"/> consist of `&`
  3022. + a string of 2-32 alphanumerics beginning with a letter + `;`.
  3023. .
  3024. &nbsp; &amp; &copy; &AElig; &Dcaron; &frac34; &HilbertSpace; &DifferentialD; &ClockwiseContourIntegral;
  3025. .
  3026. <p>&nbsp; &amp; &copy; &AElig; &Dcaron; &frac34; &HilbertSpace; &DifferentialD; &ClockwiseContourIntegral;</p>
  3027. .
  3028. [Decimal entities](#decimal-entities) <a id="decimal-entities"/>
  3029. consist of `&` + a string of 1--8 arabic digits + `;`.
  3030. .
  3031. &#1; &#35; &#1234; &#992; &#98765432;
  3032. .
  3033. <p>&#1; &#35; &#1234; &#992; &#98765432;</p>
  3034. .
  3035. [Hexadecimal entities](#hexadecimal-entities) <a id="hexadecimal-entities"/>
  3036. consist of `&` + either `X` or `x` + a string of 1-8 hexadecimal digits
  3037. + `;`.
  3038. .
  3039. &#x1; &#X22; &#XD06; &#xcab;
  3040. .
  3041. <p>&#x1; &#X22; &#XD06; &#xcab;</p>
  3042. .
  3043. Here are some nonentities:
  3044. .
  3045. &nbsp &x; &#; &#x; &#123456789; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3046. .
  3047. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;#123456789; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3048. .
  3049. Although HTML5 does accept some entities without a trailing semicolon
  3050. (such as `&copy`), these are not recognized as entities here:
  3051. .
  3052. &copy
  3053. .
  3054. <p>&amp;copy</p>
  3055. .
  3056. On the other hand, many strings that are not on the list of HTML5
  3057. named entities are recognized as entities here:
  3058. .
  3059. &MadeUpEntity;
  3060. .
  3061. <p>&MadeUpEntity;</p>
  3062. .
  3063. Entities are recognized in any any context besides code spans or
  3064. code blocks, including raw HTML, URLs, [link titles](#link-title), and
  3065. [fenced code block](#fenced-code-block) info strings:
  3066. .
  3067. <a href="&ouml;&ouml;.html">
  3068. .
  3069. <p><a href="&ouml;&ouml;.html"></p>
  3070. .
  3071. .
  3072. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3073. .
  3074. <p><a href="/f&ouml;&ouml;" title="f&ouml;&ouml;">foo</a></p>
  3075. .
  3076. .
  3077. [foo]
  3078. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3079. .
  3080. <p><a href="/f&ouml;&ouml;" title="f&ouml;&ouml;">foo</a></p>
  3081. .
  3082. .
  3083. ``` f&ouml;&ouml;
  3084. foo
  3085. ```
  3086. .
  3087. <pre class="f&ouml;&ouml;"><code>foo
  3088. </code></pre>
  3089. .
  3090. Entities are treated as literal text in code spans and code blocks:
  3091. .
  3092. `f&ouml;&ouml;`
  3093. .
  3094. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3095. .
  3096. .
  3097. f&ouml;f&ouml;
  3098. .
  3099. <pre><code>f&amp;ouml;f&amp;ouml;
  3100. </code></pre>
  3101. .
  3102. ## Code span
  3103. A [backtick string](#backtick-string) <a id="backtick-string"/>
  3104. is a string of one or more backtick characters (`` ` ``) that is neither
  3105. preceded nor followed by a backtick.
  3106. A code span begins with a backtick string and ends with a backtick
  3107. string of equal length. The contents of the code span are the
  3108. characters between the two backtick strings, with leading and trailing
  3109. spaces and newlines removed, and consecutive spaces and newlines
  3110. collapsed to single spaces.
  3111. This is a simple code span:
  3112. .
  3113. `foo`
  3114. .
  3115. <p><code>foo</code></p>
  3116. .
  3117. Here two backticks are used, because the code contains a backtick.
  3118. This example also illustrates stripping of leading and trailing spaces:
  3119. .
  3120. `` foo ` bar ``
  3121. .
  3122. <p><code>foo ` bar</code></p>
  3123. .
  3124. This example shows the motivation for stripping leading and trailing
  3125. spaces:
  3126. .
  3127. ` `` `
  3128. .
  3129. <p><code>``</code></p>
  3130. .
  3131. Newlines are treated like spaces:
  3132. .
  3133. ``
  3134. foo
  3135. ``
  3136. .
  3137. <p><code>foo</code></p>
  3138. .
  3139. Interior spaces and newlines are collapsed into single spaces, just
  3140. as they would be by a browser:
  3141. .
  3142. `foo bar
  3143. baz`
  3144. .
  3145. <p><code>foo bar baz</code></p>
  3146. .
  3147. Q: Why not just leave the spaces, since browsers will collapse them
  3148. anyway? A: Because we might be targeting a non-HTML format, and we
  3149. shouldn't rely on HTML-specific rendering assumptions.
  3150. (Existing implementations differ in their treatment of internal
  3151. spaces and newlines. Some, including `Markdown.pl` and
  3152. `showdown`, convert an internal newline into a `<br />` tag.
  3153. But this makes things difficult for those who like to hard-wrap
  3154. their paragraphs, since a line break in the midst of a code
  3155. span will cause an unintended line break in the output. Others
  3156. just leave internal spaces as they are, which is fine if only
  3157. HTML is being targeted.)
  3158. .
  3159. `foo `` bar`
  3160. .
  3161. <p><code>foo `` bar</code></p>
  3162. .
  3163. Note that backslash escapes do not work in code spans. All backslashes
  3164. are treated literally:
  3165. .
  3166. `foo\`bar`
  3167. .
  3168. <p><code>foo\</code>bar`</p>
  3169. .
  3170. Backslash escapes are never needed, because one can always choose a
  3171. string of *n* backtick characters as delimiters, where the code does
  3172. not contain any strings of exactly *n* backtick characters.
  3173. Code span backticks have higher precedence than any other inline
  3174. constructs except HTML tags and autolinks. Thus, for example, this is
  3175. not parsed as emphasized text, since the second `*` is part of a code
  3176. span:
  3177. .
  3178. *foo`*`
  3179. .
  3180. <p>*foo<code>*</code></p>
  3181. .
  3182. And this is not parsed as a link:
  3183. .
  3184. [not a `link](/foo`)
  3185. .
  3186. <p>[not a <code>link](/foo</code>)</p>
  3187. .
  3188. But this is a link:
  3189. .
  3190. <http://foo.bar.`baz>`
  3191. .
  3192. <p><a href="http://foo.bar.`baz">http://foo.bar.`baz</a>`</p>
  3193. .
  3194. And this is an HTML tag:
  3195. .
  3196. <a href="`">`
  3197. .
  3198. <p><a href="`">`</p>
  3199. .
  3200. When a backtick string is not closed by a matching backtick string,
  3201. we just have literal backticks:
  3202. .
  3203. ```foo``
  3204. .
  3205. <p>```foo``</p>
  3206. .
  3207. .
  3208. `foo
  3209. .
  3210. <p>`foo</p>
  3211. .
  3212. ## Emphasis and strong emphasis
  3213. John Gruber's original [markdown syntax
  3214. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3215. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3216. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3217. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3218. > tag.
  3219. This is enough for most users, but these rules leave much undecided,
  3220. especially when it comes to nested emphasis. The original
  3221. `Markdown.pl` test suite makes it clear that triple `***` and
  3222. `___` delimiters can be used for strong emphasis, and most
  3223. implementations have also allowed the following patterns:
  3224. ``` markdown
  3225. ***strong emph***
  3226. ***strong** in emph*
  3227. ***emph* in strong**
  3228. **in strong *emph***
  3229. *in emph **strong***
  3230. ```
  3231. The following patterns are less widely supported, but the intent
  3232. is clear and they are useful (especially in contexts like bibliography
  3233. entries):
  3234. ``` markdown
  3235. *emph *with emph* in it*
  3236. **strong **with strong** in it**
  3237. ```
  3238. Many implementations have also restricted intraword emphasis to
  3239. the `*` forms, to avoid unwanted emphasis in words containing
  3240. internal underscores. (It is best practice to put these in code
  3241. spans, but users often do not.)
  3242. ``` markdown
  3243. internal emphasis: foo*bar*baz
  3244. no emphasis: foo_bar_baz
  3245. ```
  3246. The following rules capture all of these patterns, while allowing
  3247. for efficient parsing strategies that do not backtrack:
  3248. 1. A single `*` character [can open emphasis](#can-open-emphasis)
  3249. <a id="can-open-emphasis"/> iff
  3250. (a) it is not part of a sequence of four or more unescaped `*`s,
  3251. (b) it is not followed by whitespace, and
  3252. (c) either it is not followed by a `*` character or it is
  3253. followed immediately by strong emphasis.
  3254. 2. A single `_` character [can open emphasis](#can-open-emphasis) iff
  3255. (a) it is not part of a sequence of four or more unescaped `_`s,
  3256. (b) it is not followed by whitespace,
  3257. (c) is is not preceded by an ASCII alphanumeric character, and
  3258. (d) either it is not followed by a `_` character or it is
  3259. followed immediately by strong emphasis.
  3260. 3. A single `*` character [can close emphasis](#can-close-emphasis)
  3261. <a id="can-close-emphasis"/> iff
  3262. (a) it is not part of a sequence of four or more unescaped `*`s, and
  3263. (b) it is not preceded by whitespace.
  3264. 4. A single `_` character [can close emphasis](#can-close-emphasis) iff
  3265. (a) it is not part of a sequence of four or more unescaped `_`s,
  3266. (b) it is not preceded by whitespace, and
  3267. (c) it is not followed by an ASCII alphanumeric character.
  3268. 5. A double `**` [can open strong emphasis](#can-open-strong-emphasis)
  3269. <a id="can-open-strong-emphasis" /> iff
  3270. (a) it is not part of a sequence of four or more unescaped `*`s,
  3271. (b) it is not followed by whitespace, and
  3272. (c) either it is not followed by a `*` character or it is
  3273. followed immediately by emphasis.
  3274. 6. A double `__` [can open strong emphasis](#can-open-strong-emphasis)
  3275. iff
  3276. (a) it is not part of a sequence of four or more unescaped `_`s,
  3277. (b) it is not followed by whitespace, and
  3278. (c) it is not preceded by an ASCII alphanumeric character, and
  3279. (d) either it is not followed by a `_` character or it is
  3280. followed immediately by emphasis.
  3281. 7. A double `**` [can close strong emphasis](#can-close-strong-emphasis)
  3282. <a id="can-close-strong-emphasis" /> iff
  3283. (a) it is not part of a sequence of four or more unescaped `*`s, and
  3284. (b) it is not preceded by whitespace.
  3285. 8. A double `__` [can close strong emphasis](#can-close-strong-emphasis)
  3286. iff
  3287. (a) it is not part of a sequence of four or more unescaped `_`s,
  3288. (b) it is not preceded by whitespace, and
  3289. (c) it is not followed by an ASCII alphanumeric character.
  3290. 9. Emphasis begins with a delimiter that [can open
  3291. emphasis](#can-open-emphasis) and includes inlines parsed
  3292. sequentially until a delimiter that [can close
  3293. emphasis](#can-close-emphasis), and that uses the same
  3294. character (`_` or `*`) as the opening delimiter, is reached.
  3295. 10. Strong emphasis begins with a delimiter that [can open strong
  3296. emphasis](#can-open-strong-emphasis) and includes inlines parsed
  3297. sequentially until a delimiter that [can close strong
  3298. emphasis](#can-close-strong-emphasis), and that uses the
  3299. same character (`_` or `*`) as the opening delimiter, is reached.
  3300. These rules can be illustrated through a series of examples.
  3301. Simple emphasis:
  3302. .
  3303. *foo bar*
  3304. .
  3305. <p><em>foo bar</em></p>
  3306. .
  3307. .
  3308. _foo bar_
  3309. .
  3310. <p><em>foo bar</em></p>
  3311. .
  3312. Simple strong emphasis:
  3313. .
  3314. **foo bar**
  3315. .
  3316. <p><strong>foo bar</strong></p>
  3317. .
  3318. .
  3319. __foo bar__
  3320. .
  3321. <p><strong>foo bar</strong></p>
  3322. .
  3323. Emphasis can continue over line breaks:
  3324. .
  3325. *foo
  3326. bar*
  3327. .
  3328. <p><em>foo
  3329. bar</em></p>
  3330. .
  3331. .
  3332. _foo
  3333. bar_
  3334. .
  3335. <p><em>foo
  3336. bar</em></p>
  3337. .
  3338. .
  3339. **foo
  3340. bar**
  3341. .
  3342. <p><strong>foo
  3343. bar</strong></p>
  3344. .
  3345. .
  3346. __foo
  3347. bar__
  3348. .
  3349. <p><strong>foo
  3350. bar</strong></p>
  3351. .
  3352. Emphasis can contain other inline constructs:
  3353. .
  3354. *foo [bar](/url)*
  3355. .
  3356. <p><em>foo <a href="/url">bar</a></em></p>
  3357. .
  3358. .
  3359. _foo [bar](/url)_
  3360. .
  3361. <p><em>foo <a href="/url">bar</a></em></p>
  3362. .
  3363. .
  3364. **foo [bar](/url)**
  3365. .
  3366. <p><strong>foo <a href="/url">bar</a></strong></p>
  3367. .
  3368. .
  3369. __foo [bar](/url)__
  3370. .
  3371. <p><strong>foo <a href="/url">bar</a></strong></p>
  3372. .
  3373. Symbols contained in other inline constructs will not
  3374. close emphasis:
  3375. .
  3376. *foo [bar*](/url)
  3377. .
  3378. <p>*foo <a href="/url">bar*</a></p>
  3379. .
  3380. .
  3381. _foo [bar_](/url)
  3382. .
  3383. <p>_foo <a href="/url">bar_</a></p>
  3384. .
  3385. .
  3386. **<a href="**">
  3387. .
  3388. <p>**<a href="**"></p>
  3389. .
  3390. .
  3391. __<a href="__">
  3392. .
  3393. <p>__<a href="__"></p>
  3394. .
  3395. .
  3396. *a `*`*
  3397. .
  3398. <p><em>a <code>*</code></em></p>
  3399. .
  3400. .
  3401. _a `_`_
  3402. .
  3403. <p><em>a <code>_</code></em></p>
  3404. .
  3405. .
  3406. **a<http://foo.bar?q=**>
  3407. .
  3408. <p>**a<a href="http://foo.bar?q=**">http://foo.bar?q=**</a></p>
  3409. .
  3410. .
  3411. __a<http://foo.bar?q=__>
  3412. .
  3413. <p>__a<a href="http://foo.bar?q=__">http://foo.bar?q=__</a></p>
  3414. .
  3415. This is not emphasis, because the opening delimiter is
  3416. followed by white space:
  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. .
  3428. and ** foo bar**
  3429. .
  3430. <p>and ** foo bar**</p>
  3431. .
  3432. .
  3433. __ foo bar__
  3434. .
  3435. <p>__ foo bar__</p>
  3436. .
  3437. This is not emphasis, because the closing delimiter is
  3438. preceded by white space:
  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. .
  3450. and **foo bar **
  3451. .
  3452. <p>and **foo bar **</p>
  3453. .
  3454. .
  3455. and __foo bar __
  3456. .
  3457. <p>and __foo bar __</p>
  3458. .
  3459. The rules imply that a sequence of four or more unescaped `*` or
  3460. `_` characters will always be parsed as a literal string:
  3461. .
  3462. ****hi****
  3463. .
  3464. <p>****hi****</p>
  3465. .
  3466. .
  3467. _____hi_____
  3468. .
  3469. <p>_____hi_____</p>
  3470. .
  3471. .
  3472. Sign here: _________
  3473. .
  3474. <p>Sign here: _________</p>
  3475. .
  3476. The rules also imply that there can be no empty emphasis or strong
  3477. emphasis:
  3478. .
  3479. ** is not an empty emphasis
  3480. .
  3481. <p>** is not an empty emphasis</p>
  3482. .
  3483. .
  3484. **** is not an empty strong emphasis
  3485. .
  3486. <p>**** is not an empty strong emphasis</p>
  3487. .
  3488. To include `*` or `_` in emphasized sections, use backslash escapes
  3489. or code spans:
  3490. .
  3491. *here is a \**
  3492. .
  3493. <p><em>here is a *</em></p>
  3494. .
  3495. .
  3496. __this is a double underscore (`__`)__
  3497. .
  3498. <p><strong>this is a double underscore (<code>__</code>)</strong></p>
  3499. .
  3500. `*` delimiters allow intra-word emphasis; `_` delimiters do not:
  3501. .
  3502. foo*bar*baz
  3503. .
  3504. <p>foo<em>bar</em>baz</p>
  3505. .
  3506. .
  3507. foo_bar_baz
  3508. .
  3509. <p>foo_bar_baz</p>
  3510. .
  3511. .
  3512. foo__bar__baz
  3513. .
  3514. <p>foo__bar__baz</p>
  3515. .
  3516. .
  3517. _foo_bar_baz_
  3518. .
  3519. <p><em>foo_bar_baz</em></p>
  3520. .
  3521. .
  3522. 11*15*32
  3523. .
  3524. <p>11<em>15</em>32</p>
  3525. .
  3526. .
  3527. 11_15_32
  3528. .
  3529. <p>11_15_32</p>
  3530. .
  3531. Internal underscores will be ignored in underscore-delimited
  3532. emphasis:
  3533. .
  3534. _foo_bar_baz_
  3535. .
  3536. <p><em>foo_bar_baz</em></p>
  3537. .
  3538. .
  3539. __foo__bar__baz__
  3540. .
  3541. <p><strong>foo__bar__baz</strong></p>
  3542. .
  3543. The rules are sufficient for the following nesting patterns:
  3544. .
  3545. ***foo bar***
  3546. .
  3547. <p><strong><em>foo bar</em></strong></p>
  3548. .
  3549. .
  3550. ___foo bar___
  3551. .
  3552. <p><strong><em>foo bar</em></strong></p>
  3553. .
  3554. .
  3555. ***foo** bar*
  3556. .
  3557. <p><em><strong>foo</strong> bar</em></p>
  3558. .
  3559. .
  3560. ___foo__ bar_
  3561. .
  3562. <p><em><strong>foo</strong> bar</em></p>
  3563. .
  3564. .
  3565. ***foo* bar**
  3566. .
  3567. <p><strong><em>foo</em> bar</strong></p>
  3568. .
  3569. .
  3570. ___foo_ bar__
  3571. .
  3572. <p><strong><em>foo</em> bar</strong></p>
  3573. .
  3574. .
  3575. *foo **bar***
  3576. .
  3577. <p><em>foo <strong>bar</strong></em></p>
  3578. .
  3579. .
  3580. _foo __bar___
  3581. .
  3582. <p><em>foo <strong>bar</strong></em></p>
  3583. .
  3584. .
  3585. **foo *bar***
  3586. .
  3587. <p><strong>foo <em>bar</em></strong></p>
  3588. .
  3589. .
  3590. __foo _bar___
  3591. .
  3592. <p><strong>foo <em>bar</em></strong></p>
  3593. .
  3594. .
  3595. *foo **bar***
  3596. .
  3597. <p><em>foo <strong>bar</strong></em></p>
  3598. .
  3599. .
  3600. _foo __bar___
  3601. .
  3602. <p><em>foo <strong>bar</strong></em></p>
  3603. .
  3604. .
  3605. *foo *bar* baz*
  3606. .
  3607. <p><em>foo <em>bar</em> baz</em></p>
  3608. .
  3609. .
  3610. _foo _bar_ baz_
  3611. .
  3612. <p><em>foo <em>bar</em> baz</em></p>
  3613. .
  3614. .
  3615. **foo **bar** baz**
  3616. .
  3617. <p><strong>foo <strong>bar</strong> baz</strong></p>
  3618. .
  3619. .
  3620. __foo __bar__ baz__
  3621. .
  3622. <p><strong>foo <strong>bar</strong> baz</strong></p>
  3623. .
  3624. .
  3625. *foo **bar** baz*
  3626. .
  3627. <p><em>foo <strong>bar</strong> baz</em></p>
  3628. .
  3629. .
  3630. _foo __bar__ baz_
  3631. .
  3632. <p><em>foo <strong>bar</strong> baz</em></p>
  3633. .
  3634. .
  3635. **foo *bar* baz**
  3636. .
  3637. <p><strong>foo <em>bar</em> baz</strong></p>
  3638. .
  3639. .
  3640. __foo _bar_ baz__
  3641. .
  3642. <p><strong>foo <em>bar</em> baz</strong></p>
  3643. .
  3644. Note that you cannot nest emphasis directly inside emphasis
  3645. using the same delimeter, or strong emphasis directly inside
  3646. strong emphasis:
  3647. .
  3648. **foo**
  3649. .
  3650. <p><strong>foo</strong></p>
  3651. .
  3652. .
  3653. ****foo****
  3654. .
  3655. <p>****foo****</p>
  3656. .
  3657. For these nestings, you need to switch delimiters:
  3658. .
  3659. *_foo_*
  3660. .
  3661. <p><em><em>foo</em></em></p>
  3662. .
  3663. .
  3664. **__foo__**
  3665. .
  3666. <p><strong><strong>foo</strong></strong></p>
  3667. .
  3668. Note that a `*` followed by a `*` can close emphasis, and
  3669. a `**` followed by a `*` can close strong emphasis (and
  3670. similarly for `_` and `__`):
  3671. .
  3672. *foo**
  3673. .
  3674. <p><em>foo</em>*</p>
  3675. .
  3676. .
  3677. *foo *bar**
  3678. .
  3679. <p><em>foo <em>bar</em></em></p>
  3680. .
  3681. .
  3682. **foo***
  3683. .
  3684. <p><strong>foo</strong>*</p>
  3685. .
  3686. The following contains no strong emphasis, because the opening
  3687. delimiter is closed by the first `*` before `bar`:
  3688. .
  3689. *foo**bar***
  3690. .
  3691. <p><em>foo</em><em>bar</em>**</p>
  3692. .
  3693. However, a string of four or more `****` can never close emphasis:
  3694. .
  3695. *foo****
  3696. .
  3697. <p>*foo****</p>
  3698. .
  3699. Note that there are some asymmetries here:
  3700. .
  3701. *foo**
  3702. **foo*
  3703. .
  3704. <p><em>foo</em>*</p>
  3705. <p>**foo*</p>
  3706. .
  3707. .
  3708. *foo *bar**
  3709. **foo* bar*
  3710. .
  3711. <p><em>foo <em>bar</em></em></p>
  3712. <p>**foo* bar*</p>
  3713. .
  3714. More cases with mismatched delimiters:
  3715. .
  3716. **foo* bar*
  3717. .
  3718. <p>**foo* bar*</p>
  3719. .
  3720. .
  3721. *bar***
  3722. .
  3723. <p><em>bar</em>**</p>
  3724. .
  3725. .
  3726. ***foo*
  3727. .
  3728. <p>***foo*</p>
  3729. .
  3730. .
  3731. **bar***
  3732. .
  3733. <p><strong>bar</strong>*</p>
  3734. .
  3735. .
  3736. ***foo**
  3737. .
  3738. <p>***foo**</p>
  3739. .
  3740. .
  3741. ***foo *bar*
  3742. .
  3743. <p>***foo <em>bar</em></p>
  3744. .
  3745. ## Links
  3746. A link contains a [link label](#link-label) (the visible text),
  3747. a [destination](#destination) (the URI that is the link destination),
  3748. and optionally a [link title](#link-title). There are two basic kinds
  3749. of links in markdown. In [inline links](#inline-links) the destination
  3750. and title are given immediately after the lable. In [reference
  3751. links](#reference-links) the destination and title are defined elsewhere
  3752. in the document.
  3753. A [link label](#link-label) <a id="link-label"/> consists of
  3754. - an opening `[`, followed by
  3755. - zero or more backtick code spans, autolinks, HTML tags, link labels,
  3756. backslash-escaped ASCII punctuation characters, or non-`]` characters,
  3757. followed by
  3758. - a closing `]`.
  3759. These rules are motivated by the following intuitive ideas:
  3760. - A link label is a container for inline elements.
  3761. - The square brackets bind more tightly than emphasis markers,
  3762. but less tightly than `<>` or `` ` ``.
  3763. - Link labels may contain material in matching square brackets.
  3764. A [link destination](#link-destination) <a id="link-destination"/>
  3765. consists of either
  3766. - a sequence of zero or more characters between an opening `<` and a
  3767. closing `>` that contains no line breaks or unescaped `<` or `>`
  3768. characters, or
  3769. - a nonempty sequence of characters that does not include
  3770. ASCII space or control characters, and includes parentheses
  3771. only if (a) they are backslash-escaped or (b) they are part of
  3772. a balanced pair of unescaped parentheses that is not itself
  3773. inside a balanced pair of unescaped paretheses.
  3774. A [link title](#link-title) <a id="link-title"/> consists of either
  3775. - a sequence of zero or more characters between straight double-quote
  3776. characters (`"`), including a `"` character only if it is
  3777. backslash-escaped, or
  3778. - a sequence of zero or more characters between straight single-quote
  3779. characters (`'`), including a `'` character only if it is
  3780. backslash-escaped, or
  3781. - a sequence of zero or more characters between matching parentheses
  3782. (`(...)`), including a `)` character only if it is backslash-escaped.
  3783. An [inline link](#inline-link) <a id="inline-link"/>
  3784. consists of a [link label](#link-label) followed immediately
  3785. by a left parenthesis `(`, optional whitespace,
  3786. an optional [link destination](#link-destination),
  3787. an optional [link title](#link-title) separated from the link
  3788. destination by whitespace, optional whitespace, and a right
  3789. parenthesis `)`. The link's text consists of the label (excluding
  3790. the enclosing square brackets) parsed as inlines. The link's
  3791. URI consists of the link destination, excluding enclosing `<...>` if
  3792. present, with backslash-escapes in effect as described above. The
  3793. link's title consists of the link title, excluding its enclosing
  3794. delimiters, with backslash-escapes in effect as described above.
  3795. Here is a simple inline link:
  3796. .
  3797. [link](/uri "title")
  3798. .
  3799. <p><a href="/uri" title="title">link</a></p>
  3800. .
  3801. The title may be omitted:
  3802. .
  3803. [link](/uri)
  3804. .
  3805. <p><a href="/uri">link</a></p>
  3806. .
  3807. Both the title and the destination may be omitted:
  3808. .
  3809. [link]()
  3810. .
  3811. <p><a href="">link</a></p>
  3812. .
  3813. .
  3814. [link](<>)
  3815. .
  3816. <p><a href="">link</a></p>
  3817. .
  3818. If the destination contains spaces, it must be enclosed in pointy
  3819. braces:
  3820. .
  3821. [link](/my uri)
  3822. .
  3823. <p>[link](/my uri)</p>
  3824. .
  3825. .
  3826. [link](</my uri>)
  3827. .
  3828. <p><a href="/my uri">link</a></p>
  3829. .
  3830. The destination cannot contain line breaks, even with pointy braces:
  3831. .
  3832. [link](foo
  3833. bar)
  3834. .
  3835. <p>[link](foo
  3836. bar)</p>
  3837. .
  3838. One level of balanced parentheses is allowed without escaping:
  3839. .
  3840. [link]((foo)and(bar))
  3841. .
  3842. <p><a href="(foo)and(bar)">link</a></p>
  3843. .
  3844. However, if you have parentheses within parentheses, you need to escape
  3845. or use the `<...>` form:
  3846. .
  3847. [link](foo(and(bar)))
  3848. .
  3849. <p>[link](foo(and(bar)))</p>
  3850. .
  3851. .
  3852. [link](foo(and\(bar\)))
  3853. .
  3854. <p><a href="foo(and(bar))">link</a></p>
  3855. .
  3856. .
  3857. [link](<foo(and(bar))>)
  3858. .
  3859. <p><a href="foo(and(bar))">link</a></p>
  3860. .
  3861. Parentheses and other symbols can also be escaped, as usual
  3862. in markdown:
  3863. .
  3864. [link](foo\)\:)
  3865. .
  3866. <p><a href="foo):">link</a></p>
  3867. .
  3868. URL-escaping and entities should be left alone inside the destination:
  3869. .
  3870. [link](foo%20b&auml;)
  3871. .
  3872. <p><a href="foo%20b&auml;">link</a></p>
  3873. .
  3874. Note that, because titles can often be parsed as destinations,
  3875. if you try to omit the destination and keep the title, you'll
  3876. get unexpected results:
  3877. .
  3878. [link]("title")
  3879. .
  3880. <p><a href="&quot;title&quot;">link</a></p>
  3881. .
  3882. Titles may be in single quotes, double quotes, or parentheses:
  3883. .
  3884. [link](/url "title")
  3885. [link](/url 'title')
  3886. [link](/url (title))
  3887. .
  3888. <p><a href="/url" title="title">link</a>
  3889. <a href="/url" title="title">link</a>
  3890. <a href="/url" title="title">link</a></p>
  3891. .
  3892. Backslash escapes and entities may be used in titles:
  3893. .
  3894. [link](/url "title \"&quot;")
  3895. .
  3896. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  3897. .
  3898. Nested balanced quotes are not allowed without escaping:
  3899. .
  3900. [link](/url "title "and" title")
  3901. .
  3902. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  3903. .
  3904. But it is easy to work around this by using a different quote type:
  3905. .
  3906. [link](/url 'title "and" title')
  3907. .
  3908. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  3909. .
  3910. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  3911. title, and its test suite included a test demonstrating this.
  3912. But it is hard to see a good rationale for the extra complexity this
  3913. brings, since there are already many ways---backslash escaping,
  3914. entities, or using a different quote type for the enclosing title---to
  3915. write titles containing double quotes. `Markdown.pl`'s handling of
  3916. titles has a number of other strange features. For example, it allows
  3917. single-quoted titles in inline links, but not reference links. And, in
  3918. reference links but not inline links, it allows a title to begin with
  3919. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  3920. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  3921. a simple, rational rule that works the same way in inline links and
  3922. link reference definitions.)
  3923. Whitespace is allowed around the destination and title:
  3924. .
  3925. [link]( /uri
  3926. "title" )
  3927. .
  3928. <p><a href="/uri" title="title">link</a></p>
  3929. .
  3930. But it is not allowed between the link label and the
  3931. following parenthesis:
  3932. .
  3933. [link] (/uri)
  3934. .
  3935. <p>[link] (/uri)</p>
  3936. .
  3937. Note that this is not a link, because the closing `]` occurs in
  3938. an HTML tag:
  3939. .
  3940. [foo <bar attr="](baz)">
  3941. .
  3942. <p>[foo <bar attr="](baz)"></p>
  3943. .
  3944. There are three kinds of [reference links](#reference-link):
  3945. <a id="reference-link"/>
  3946. A [full reference link](#full-reference-link) <a id="full-reference-link"/>
  3947. consists of a [link label](#link-label), optional whitespace, and
  3948. another [link label](#link-label) that [matches](#matches) a
  3949. [link reference definition](#link-reference-definition) elsewhere in the
  3950. document.
  3951. One label [matches](#matches) <a id="matches"/>
  3952. another just in case their normalized forms are equal. To normalize a
  3953. label, perform the *unicode case fold* and collapse consecutive internal
  3954. whitespace to a single space. If there are multiple matching reference
  3955. link definitions, the one that comes first in the document is used. (It
  3956. is desirable in such cases to emit a warning.)
  3957. The contents of the first link label are parsed as inlines, which are
  3958. used as the link's text. The link's URI and title are provided by the
  3959. matching [link reference definition](#link-reference-definition).
  3960. Here is a simple example:
  3961. .
  3962. [foo][bar]
  3963. [bar]: /url "title"
  3964. .
  3965. <p><a href="/url" title="title">foo</a></p>
  3966. .
  3967. The first label can contain inline content:
  3968. .
  3969. [*foo\!*][bar]
  3970. [bar]: /url "title"
  3971. .
  3972. <p><a href="/url" title="title"><em>foo!</em></a></p>
  3973. .
  3974. Matching is case-insensitive:
  3975. .
  3976. [foo][BaR]
  3977. [bar]: /url "title"
  3978. .
  3979. <p><a href="/url" title="title">foo</a></p>
  3980. .
  3981. Unicode case fold is used:
  3982. .
  3983. [Толпой][Толпой] is a Russian word.
  3984. [ТОЛПОЙ]: /url
  3985. .
  3986. <p><a href="/url">Толпой</a> is a Russian word.</p>
  3987. .
  3988. Consecutive internal whitespace is treated as one space for
  3989. purposes of determining matching:
  3990. .
  3991. [Foo
  3992. bar]: /url
  3993. [Baz][Foo bar]
  3994. .
  3995. <p><a href="/url">Baz</a></p>
  3996. .
  3997. There can be whitespace between the two labels:
  3998. .
  3999. [foo] [bar]
  4000. [bar]: /url "title"
  4001. .
  4002. <p><a href="/url" title="title">foo</a></p>
  4003. .
  4004. .
  4005. [foo]
  4006. [bar]
  4007. [bar]: /url "title"
  4008. .
  4009. <p><a href="/url" title="title">foo</a></p>
  4010. .
  4011. When there are multiple matching [link reference
  4012. definitions](#link-reference-definition), the first is used:
  4013. .
  4014. [foo]: /url1
  4015. [foo]: /url2
  4016. [bar][foo]
  4017. .
  4018. <p><a href="/url1">bar</a></p>
  4019. .
  4020. Note that matching is performed on normalized strings, not parsed
  4021. inline content. So the following does not match, even though the
  4022. labels define equivalent inline content:
  4023. .
  4024. [bar][foo\!]
  4025. [foo!]: /url
  4026. .
  4027. <p>[bar][foo!]</p>
  4028. .
  4029. A [collapsed reference link](#collapsed-reference-link)
  4030. <a id="collapsed-reference-link"/> consists of a [link
  4031. label](#link-label) that [matches](#matches) a [link reference
  4032. definition](#link-reference-definition) elsewhere in the
  4033. document, optional whitespace, and the string `[]`. The contents of the
  4034. first link label are parsed as inlines, which are used as the link's
  4035. text. The link's URI and title are provided by the matching reference
  4036. link definition. Thus, `[foo][]` is equivalent to `[foo][foo]`.
  4037. .
  4038. [foo][]
  4039. [foo]: /url "title"
  4040. .
  4041. <p><a href="/url" title="title">foo</a></p>
  4042. .
  4043. .
  4044. [*foo* bar][]
  4045. [*foo* bar]: /url "title"
  4046. .
  4047. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4048. .
  4049. The link labels are case-insensitive:
  4050. .
  4051. [Foo][]
  4052. [foo]: /url "title"
  4053. .
  4054. <p><a href="/url" title="title">Foo</a></p>
  4055. .
  4056. As with full reference links, whitespace is allowed
  4057. between the two sets of brackets:
  4058. .
  4059. [foo]
  4060. []
  4061. [foo]: /url "title"
  4062. .
  4063. <p><a href="/url" title="title">foo</a></p>
  4064. .
  4065. A [shortcut reference link](#shortcut-reference-link)
  4066. <a id="shortcut-reference-link"/> consists of a [link
  4067. label](#link-label) that [matches](#matches) a [link reference
  4068. definition](#link-reference-definition) elsewhere in the
  4069. document and is not followed by `[]` or a link label.
  4070. The contents of the first link label are parsed as inlines,
  4071. which are used as the link's text. the link's URI and title
  4072. are provided by the matching link reference definition.
  4073. Thus, `[foo]` is equivalent to `[foo][]`.
  4074. .
  4075. [foo]
  4076. [foo]: /url "title"
  4077. .
  4078. <p><a href="/url" title="title">foo</a></p>
  4079. .
  4080. .
  4081. [*foo* bar]
  4082. [*foo* bar]: /url "title"
  4083. .
  4084. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  4085. .
  4086. .
  4087. [[*foo* bar]]
  4088. [*foo* bar]: /url "title"
  4089. .
  4090. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  4091. .
  4092. The link labels are case-insensitive:
  4093. .
  4094. [Foo]
  4095. [foo]: /url "title"
  4096. .
  4097. <p><a href="/url" title="title">Foo</a></p>
  4098. .
  4099. If you just want bracketed text, you can backslash-escape the
  4100. opening bracket to avoid links:
  4101. .
  4102. \[foo]
  4103. [foo]: /url "title"
  4104. .
  4105. <p>[foo]</p>
  4106. .
  4107. Note that this is a link, because link labels bind more tightly
  4108. than emphasis:
  4109. .
  4110. [foo*]: /url
  4111. *[foo*]
  4112. .
  4113. <p>*<a href="/url">foo*</a></p>
  4114. .
  4115. However, this is not, because link labels bind tight less
  4116. tightly than code backticks:
  4117. .
  4118. [foo`]: /url
  4119. [foo`]`
  4120. .
  4121. <p>[foo<code>]</code></p>
  4122. .
  4123. Link labels can contain matched square brackets:
  4124. .
  4125. [[[foo]]]
  4126. [[[foo]]]: /url
  4127. .
  4128. <p><a href="/url">[[foo]]</a></p>
  4129. .
  4130. .
  4131. [[[foo]]]
  4132. [[[foo]]]: /url1
  4133. [foo]: /url2
  4134. .
  4135. <p><a href="/url1">[[foo]]</a></p>
  4136. .
  4137. For non-matching brackets, use backslash escapes:
  4138. .
  4139. [\[foo]
  4140. [\[foo]: /url
  4141. .
  4142. <p><a href="/url">[foo</a></p>
  4143. .
  4144. Full references take precedence over shortcut references:
  4145. .
  4146. [foo][bar]
  4147. [foo]: /url1
  4148. [bar]: /url2
  4149. .
  4150. <p><a href="/url2">foo</a></p>
  4151. .
  4152. In the following case `[bar][baz]` is parsed as a reference,
  4153. `[foo]` as normal text:
  4154. .
  4155. [foo][bar][baz]
  4156. [baz]: /url
  4157. .
  4158. <p>[foo]<a href="/url">bar</a></p>
  4159. .
  4160. Here, though, `[foo][bar]` is parsed as a reference, since
  4161. `[bar]` is defined:
  4162. .
  4163. [foo][bar][baz]
  4164. [baz]: /url1
  4165. [bar]: /url2
  4166. .
  4167. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  4168. .
  4169. Here `[foo]` is not parsed as a shortcut reference, because it
  4170. is followed by a link label (even though `[bar]` is not defined):
  4171. .
  4172. [foo][bar][baz]
  4173. [baz]: /url1
  4174. [foo]: /url2
  4175. .
  4176. <p>[foo]<a href="/url1">bar</a></p>
  4177. .
  4178. ## Images
  4179. An (unescaped) exclamation mark (`!`) followed by a reference or
  4180. inline link will be parsed as an image. The link label will be
  4181. used as the image's alt text, and the link title, if any, will
  4182. be used as the image's title.
  4183. .
  4184. ![foo](/url "title")
  4185. .
  4186. <p><img src="/url" alt="foo" title="title" /></p>
  4187. .
  4188. .
  4189. ![foo *bar*]
  4190. [foo *bar*]: train.jpg "train & tracks"
  4191. .
  4192. <p><img src="train.jpg" alt="foo &lt;em&gt;bar&lt;/em&gt;" title="train &amp; tracks" /></p>
  4193. .
  4194. .
  4195. ![foo *bar*][]
  4196. [foo *bar*]: train.jpg "train & tracks"
  4197. .
  4198. <p><img src="train.jpg" alt="foo &lt;em&gt;bar&lt;/em&gt;" title="train &amp; tracks" /></p>
  4199. .
  4200. .
  4201. ![foo *bar*][foobar]
  4202. [FOOBAR]: train.jpg "train & tracks"
  4203. .
  4204. <p><img src="train.jpg" alt="foo &lt;em&gt;bar&lt;/em&gt;" title="train &amp; tracks" /></p>
  4205. .
  4206. .
  4207. ![foo](train.jpg)
  4208. .
  4209. <p><img src="train.jpg" alt="foo" /></p>
  4210. .
  4211. .
  4212. My ![foo bar](/path/to/train.jpg "title" )
  4213. .
  4214. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  4215. .
  4216. .
  4217. ![foo](<url>)
  4218. .
  4219. <p><img src="url" alt="foo" /></p>
  4220. .
  4221. .
  4222. ![](/url)
  4223. .
  4224. <p><img src="/url" alt="" /></p>
  4225. .
  4226. Reference-style:
  4227. .
  4228. ![foo] [bar]
  4229. [bar]: /url
  4230. .
  4231. <p><img src="/url" alt="foo" /></p>
  4232. .
  4233. .
  4234. ![foo] [bar]
  4235. [BAR]: /url
  4236. .
  4237. <p><img src="/url" alt="foo" /></p>
  4238. .
  4239. Collapsed:
  4240. .
  4241. ![foo][]
  4242. [foo]: /url "title"
  4243. .
  4244. <p><img src="/url" alt="foo" title="title" /></p>
  4245. .
  4246. .
  4247. ![*foo* bar][]
  4248. [*foo* bar]: /url "title"
  4249. .
  4250. <p><img src="/url" alt="&lt;em&gt;foo&lt;/em&gt; bar" title="title" /></p>
  4251. .
  4252. The labels are case-insensitive:
  4253. .
  4254. ![Foo][]
  4255. [foo]: /url "title"
  4256. .
  4257. <p><img src="/url" alt="Foo" title="title" /></p>
  4258. .
  4259. As with full reference links, whitespace is allowed
  4260. between the two sets of brackets:
  4261. .
  4262. ![foo]
  4263. []
  4264. [foo]: /url "title"
  4265. .
  4266. <p><img src="/url" alt="foo" title="title" /></p>
  4267. .
  4268. Shortcut:
  4269. .
  4270. ![foo]
  4271. [foo]: /url "title"
  4272. .
  4273. <p><img src="/url" alt="foo" title="title" /></p>
  4274. .
  4275. .
  4276. ![*foo* bar]
  4277. [*foo* bar]: /url "title"
  4278. .
  4279. <p><img src="/url" alt="&lt;em&gt;foo&lt;/em&gt; bar" title="title" /></p>
  4280. .
  4281. .
  4282. ![[foo]]
  4283. [[foo]]: /url "title"
  4284. .
  4285. <p><img src="/url" alt="[foo]" title="title" /></p>
  4286. .
  4287. The link labels are case-insensitive:
  4288. .
  4289. ![Foo]
  4290. [foo]: /url "title"
  4291. .
  4292. <p><img src="/url" alt="Foo" title="title" /></p>
  4293. .
  4294. If you just want bracketed text, you can backslash-escape the
  4295. opening `!` and `[`:
  4296. .
  4297. \!\[foo]
  4298. [foo]: /url "title"
  4299. .
  4300. <p>![foo]</p>
  4301. .
  4302. If you want a link after a literal `!`, backslash-escape the
  4303. `!`:
  4304. .
  4305. \![foo]
  4306. [foo]: /url "title"
  4307. .
  4308. <p>!<a href="/url" title="title">foo</a></p>
  4309. .
  4310. ## Autolinks
  4311. Autolinks are absolute URIs and email addresses inside `<` and `>`.
  4312. They are parsed as links, with the URL or email address as the link
  4313. label.
  4314. A [URI autolink](#uri-autolink) <a id="uri-autolink"/>
  4315. consists of `<`, followed by an [absolute
  4316. URI](#absolute-uri) not containing `<`, followed by `>`. It is parsed
  4317. as a link to the URI, with the URI as the link's label.
  4318. An [absolute URI](#absolute-uri), <a id="absolute-uri"/>
  4319. for these purposes, consists of a [scheme](#scheme) followed by a colon (`:`)
  4320. followed by zero or more characters other than ASCII whitespace and
  4321. control characters, `<`, and `>`. If the URI includes these characters,
  4322. you must use percent-encoding (e.g. `%20` for a space).
  4323. The following [schemes](#scheme) <a id="scheme"/>
  4324. are recognized (case-insensitive):
  4325. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  4326. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  4327. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  4328. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  4329. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  4330. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  4331. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  4332. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  4333. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  4334. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  4335. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  4336. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  4337. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  4338. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  4339. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  4340. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  4341. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  4342. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  4343. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  4344. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  4345. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  4346. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  4347. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  4348. Here are some valid autolinks:
  4349. .
  4350. <http://foo.bar.baz>
  4351. .
  4352. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  4353. .
  4354. .
  4355. <http://foo.bar.baz?q=hello&id=22&boolean>
  4356. .
  4357. <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>
  4358. .
  4359. .
  4360. <irc://foo.bar:2233/baz>
  4361. .
  4362. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  4363. .
  4364. Uppercase is also fine:
  4365. .
  4366. <MAILTO:FOO@BAR.BAZ>
  4367. .
  4368. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  4369. .
  4370. Spaces are not allowed in autolinks:
  4371. .
  4372. <http://foo.bar/baz bim>
  4373. .
  4374. <p>&lt;http://foo.bar/baz bim&gt;</p>
  4375. .
  4376. An [email autolink](#email-autolink) <a id="email-autolink"/>
  4377. consists of `<`, followed by an [email address](#email-address),
  4378. followed by `>`. The link's label is the email address,
  4379. and the URL is `mailto:` followed by the email address.
  4380. An [email address](#email-address), <a id="email-address"/>
  4381. for these purposes, is anything that matches
  4382. the [non-normative regex from the HTML5
  4383. spec](http://www.whatwg.org/specs/web-apps/current-work/multipage/forms.html#e-mail-state-%28type=email%29):
  4384. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  4385. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  4386. Examples of email autolinks:
  4387. .
  4388. <foo@bar.baz.com>
  4389. .
  4390. <p><a href="mailto:foo@bar.baz.com">foo@bar.baz.com</a></p>
  4391. .
  4392. .
  4393. <foo+special@Bar.baz-bar0.com>
  4394. .
  4395. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  4396. .
  4397. These are not autolinks:
  4398. .
  4399. <>
  4400. .
  4401. <p>&lt;&gt;</p>
  4402. .
  4403. .
  4404. <heck://bing.bong>
  4405. .
  4406. <p>&lt;heck://bing.bong&gt;</p>
  4407. .
  4408. .
  4409. < http://foo.bar >
  4410. .
  4411. <p>&lt; http://foo.bar &gt;</p>
  4412. .
  4413. .
  4414. <foo.bar.baz>
  4415. .
  4416. <p>&lt;foo.bar.baz&gt;</p>
  4417. .
  4418. .
  4419. <localhost:5001/foo>
  4420. .
  4421. <p>&lt;localhost:5001/foo&gt;</p>
  4422. .
  4423. .
  4424. http://google.com
  4425. .
  4426. <p>http://google.com</p>
  4427. .
  4428. .
  4429. foo@bar.baz.com
  4430. .
  4431. <p>foo@bar.baz.com</p>
  4432. .
  4433. ## Raw HTML
  4434. Text between `<` and `>` that looks like an HTML tag is parsed as a
  4435. raw HTML tag and will be rendered in HTML without escaping.
  4436. Tag and attribute names are not limited to current HTML tags,
  4437. so custom tags (and even, say, DocBook tags) may be used.
  4438. Here is the grammar for tags:
  4439. A [tag name](#tag-name) <a id="tag-name"/> consists of an ASCII letter
  4440. followed by zero or more ASCII letters or digits.
  4441. An [attribute](#attribute) <a id="attribute"/> consists of whitespace,
  4442. an **attribute name**, and an optional **attribute value
  4443. specification**.
  4444. An [attribute name](#attribute-name) <a id="attribute-name"/>
  4445. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  4446. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  4447. specification restricted to ASCII. HTML5 is laxer.)
  4448. An [attribute value specification](#attribute-value-specification)
  4449. <a id="attribute-value-specification"/> consists of optional whitespace,
  4450. a `=` character, optional whitespace, and an [attribute
  4451. value](#attribute-value).
  4452. An [attribute value](#attribute-value) <a id="attribute-value"/>
  4453. consists of an [unquoted attribute value](#unquoted-attribute-value),
  4454. a [single-quoted attribute value](#single-quoted-attribute-value),
  4455. or a [double-quoted attribute value](#double-quoted-attribute-value).
  4456. An [unquoted attribute value](#unquoted-attribute-value)
  4457. <a id="unquoted-attribute-value"/> is a nonempty string of characters not
  4458. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  4459. A [single-quoted attribute value](#single-quoted-attribute-value)
  4460. <a id="single-quoted-attribute-value"/> consists of `'`, zero or more
  4461. characters not including `'`, and a final `'`.
  4462. A [double-quoted attribute value](#double-quoted-attribute-value)
  4463. <a id="double-quoted-attribute-value"/> consists of `"`, zero or more
  4464. characters not including `"`, and a final `"`.
  4465. An [open tag](#open-tag) <a id="open-tag"/> consists of a `<` character,
  4466. a [tag name](#tag-name), zero or more [attributes](#attribute),
  4467. optional whitespace, an optional `/` character, and a `>` character.
  4468. A [closing tag](#closing-tag) <a id="closing-tag"/> consists of the
  4469. string `</`, a [tag name](#tag-name), optional whitespace, and the
  4470. character `>`.
  4471. An [HTML comment](#html-comment) <a id="html-comment"/> consists of the
  4472. string `<!--`, a string of characters not including the string `--`, and
  4473. the string `-->`.
  4474. A [processing instruction](#processing-instruction)
  4475. <a id="processing-instruction"/> consists of the string `<?`, a string
  4476. of characters not including the string `?>`, and the string
  4477. `?>`.
  4478. A [declaration](#declaration) <a id="declaration"/> consists of the
  4479. string `<!`, a name consisting of one or more uppercase ASCII letters,
  4480. whitespace, a string of characters not including the character `>`, and
  4481. the character `>`.
  4482. A [CDATA section](#cdata-section) <a id="cdata-section"/> consists of
  4483. the string `<![CDATA[`, a string of characters not including the string
  4484. `]]>`, and the string `]]>`.
  4485. An [HTML tag](#html-tag) <a id="html-tag"/> consists of an [open
  4486. tag](#open-tag), a [closing tag](#closing-tag), an [HTML
  4487. comment](#html-comment), a [processing
  4488. instruction](#processing-instruction), an [element type
  4489. declaration](#element-type-declaration), or a [CDATA
  4490. section](#cdata-section).
  4491. Here are some simple open tags:
  4492. .
  4493. <a><bab><c2c>
  4494. .
  4495. <p><a><bab><c2c></p>
  4496. .
  4497. Empty elements:
  4498. .
  4499. <a/><b2/>
  4500. .
  4501. <p><a/><b2/></p>
  4502. .
  4503. Whitespace is allowed:
  4504. .
  4505. <a /><b2
  4506. data="foo" >
  4507. .
  4508. <p><a /><b2
  4509. data="foo" ></p>
  4510. .
  4511. With attributes:
  4512. .
  4513. <a foo="bar" bam = 'baz <em>"</em>'
  4514. _boolean zoop:33=zoop:33 />
  4515. .
  4516. <p><a foo="bar" bam = 'baz <em>"</em>'
  4517. _boolean zoop:33=zoop:33 /></p>
  4518. .
  4519. Illegal tag names, not parsed as HTML:
  4520. .
  4521. <33> <__>
  4522. .
  4523. <p>&lt;33&gt; &lt;__&gt;</p>
  4524. .
  4525. Illegal attribute names:
  4526. .
  4527. <a h*#ref="hi">
  4528. .
  4529. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  4530. .
  4531. Illegal attribute values:
  4532. .
  4533. <a href="hi'> <a href=hi'>
  4534. .
  4535. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  4536. .
  4537. Illegal whitespace:
  4538. .
  4539. < a><
  4540. foo><bar/ >
  4541. .
  4542. <p>&lt; a&gt;&lt;
  4543. foo&gt;&lt;bar/ &gt;</p>
  4544. .
  4545. Missing whitespace:
  4546. .
  4547. <a href='bar'title=title>
  4548. .
  4549. <p>&lt;a href='bar'title=title&gt;</p>
  4550. .
  4551. Closing tags:
  4552. .
  4553. </a>
  4554. </foo >
  4555. .
  4556. <p></a>
  4557. </foo ></p>
  4558. .
  4559. Illegal attributes in closing tag:
  4560. .
  4561. </a href="foo">
  4562. .
  4563. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  4564. .
  4565. Comments:
  4566. .
  4567. foo <!-- this is a
  4568. comment - with hyphen -->
  4569. .
  4570. <p>foo <!-- this is a
  4571. comment - with hyphen --></p>
  4572. .
  4573. .
  4574. foo <!-- not a comment -- two hyphens -->
  4575. .
  4576. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  4577. .
  4578. Processing instructions:
  4579. .
  4580. foo <?php echo $a; ?>
  4581. .
  4582. <p>foo <?php echo $a; ?></p>
  4583. .
  4584. Declarations:
  4585. .
  4586. foo <!ELEMENT br EMPTY>
  4587. .
  4588. <p>foo <!ELEMENT br EMPTY></p>
  4589. .
  4590. CDATA sections:
  4591. .
  4592. foo <![CDATA[>&<]]>
  4593. .
  4594. <p>foo <![CDATA[>&<]]></p>
  4595. .
  4596. Entities are preserved in HTML attributes:
  4597. .
  4598. <a href="&ouml;">
  4599. .
  4600. <p><a href="&ouml;"></p>
  4601. .
  4602. Backslash escapes do not work in HTML attributes:
  4603. .
  4604. <a href="\*">
  4605. .
  4606. <p><a href="\*"></p>
  4607. .
  4608. .
  4609. <a href="\"">
  4610. .
  4611. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  4612. .
  4613. ## Hard line breaks
  4614. A line break (not in a code span or HTML tag) that is preceded
  4615. by two or more spaces is parsed as a linebreak (rendered
  4616. in HTML as a `<br />` tag):
  4617. .
  4618. foo
  4619. baz
  4620. .
  4621. <p>foo<br />
  4622. baz</p>
  4623. .
  4624. For a more visible alternative, a backslash before the newline may be
  4625. used instead of two spaces:
  4626. .
  4627. foo\
  4628. baz
  4629. .
  4630. <p>foo<br />
  4631. baz</p>
  4632. .
  4633. More than two spaces can be used:
  4634. .
  4635. foo
  4636. baz
  4637. .
  4638. <p>foo<br />
  4639. baz</p>
  4640. .
  4641. Leading spaces at the beginning of the next line are ignored:
  4642. .
  4643. foo
  4644. bar
  4645. .
  4646. <p>foo<br />
  4647. bar</p>
  4648. .
  4649. .
  4650. foo\
  4651. bar
  4652. .
  4653. <p>foo<br />
  4654. bar</p>
  4655. .
  4656. Line breaks can occur inside emphasis, links, and other constructs
  4657. that allow inline content:
  4658. .
  4659. *foo
  4660. bar*
  4661. .
  4662. <p><em>foo<br />
  4663. bar</em></p>
  4664. .
  4665. .
  4666. *foo\
  4667. bar*
  4668. .
  4669. <p><em>foo<br />
  4670. bar</em></p>
  4671. .
  4672. Line breaks do not occur inside code spans
  4673. .
  4674. `code
  4675. span`
  4676. .
  4677. <p><code>code span</code></p>
  4678. .
  4679. .
  4680. `code\
  4681. span`
  4682. .
  4683. <p><code>code\ span</code></p>
  4684. .
  4685. or HTML tags:
  4686. .
  4687. <a href="foo
  4688. bar">
  4689. .
  4690. <p><a href="foo
  4691. bar"></p>
  4692. .
  4693. .
  4694. <a href="foo\
  4695. bar">
  4696. .
  4697. <p><a href="foo\
  4698. bar"></p>
  4699. .
  4700. ## Soft line breaks
  4701. A regular line break (not in a code span or HTML tag) that is not
  4702. preceded by two or more spaces is parsed as a softbreak. (A
  4703. softbreak may be rendered in HTML either as a newline or as a space.
  4704. The result will be the same in browsers. In the examples here, a
  4705. newline will be used.)
  4706. .
  4707. foo
  4708. baz
  4709. .
  4710. <p>foo
  4711. baz</p>
  4712. .
  4713. Spaces at the end of the line and beginning of the next line are
  4714. removed:
  4715. .
  4716. foo
  4717. baz
  4718. .
  4719. <p>foo
  4720. baz</p>
  4721. .
  4722. A conforming parser may render a soft line break in HTML either as a
  4723. line break or as a space.
  4724. A renderer may also provide an option to render soft line breaks
  4725. as hard line breaks.
  4726. ## Strings
  4727. Any characters not given an interpretation by the above rules will
  4728. be parsed as string content.
  4729. .
  4730. hello $.;'there
  4731. .
  4732. <p>hello $.;'there</p>
  4733. .
  4734. .
  4735. Foo χρῆν
  4736. .
  4737. <p>Foo χρῆν</p>
  4738. .
  4739. Internal spaces are preserved verbatim:
  4740. .
  4741. Multiple spaces
  4742. .
  4743. <p>Multiple spaces</p>
  4744. .
  4745. <!-- END TESTS -->
  4746. # Appendix A: A parsing strategy {-}
  4747. ## Overview {-}
  4748. Parsing has two phases:
  4749. 1. In the first phase, lines of input are consumed and the block
  4750. structure of the document---its division into paragraphs, block quotes,
  4751. list items, and so on---is constructed. Text is assigned to these
  4752. blocks but not parsed. Link reference definitions are parsed and a
  4753. map of links is constructed.
  4754. 2. In the second phase, the raw text contents of paragraphs and headers
  4755. are parsed into sequences of markdown inline elements (strings,
  4756. code spans, links, emphasis, and so on), using the map of link
  4757. references constructed in phase 1.
  4758. ## The document tree {-}
  4759. At each point in processing, the document is represented as a tree of
  4760. **blocks**. The root of the tree is a `document` block. The `document`
  4761. may have any number of other blocks as **children**. These children
  4762. may, in turn, have other blocks a children. The last child of a block
  4763. is normally considered **open**, meaning that subsequent lines of input
  4764. can alter its contents. (Blocks that are not open are **closed**.)
  4765. Here, for example, is a possible document tree, with the open blocks
  4766. marked by arrows:
  4767. ``` tree
  4768. -> document
  4769. -> block_quote
  4770. paragraph
  4771. "Lorem ipsum dolor\nsit amet."
  4772. -> list (type=bullet tight=true bullet_char=-)
  4773. list_item
  4774. paragraph
  4775. "Qui *quodsi iracundia*"
  4776. -> list_item
  4777. -> paragraph
  4778. "aliquando id"
  4779. ```
  4780. ## How source lines alter the document tree {-}
  4781. Each line that is processed has an effect on this tree. The line is
  4782. analyzed and, depending on its contents, the document may be altered
  4783. in one or more of the following ways:
  4784. 1. One or more open blocks may be closed.
  4785. 2. One or more new blocks may be created as children of the
  4786. last open block.
  4787. 3. Text may be added to the last (deepest) open block remaining
  4788. on the tree.
  4789. Once a line has been incorporated into the tree in this way,
  4790. it can be discarded, so input can be read in a stream.
  4791. We can see how this works by considering how the tree above is
  4792. generated by four lines of markdown:
  4793. ``` markdown
  4794. > Lorem ipsum dolor
  4795. sit amet.
  4796. > - Qui *quodsi iracundia*
  4797. > - aliquando id
  4798. ```
  4799. At the outset, our document model is just
  4800. ``` tree
  4801. -> document
  4802. ```
  4803. The first line of our text,
  4804. ``` markdown
  4805. > Lorem ipsum dolor
  4806. ```
  4807. causes a `block_quote` block to be created as a child of our
  4808. open `document` block, and a `paragraph` block as a child of
  4809. the `block_quote`. Then the text is added to the last open
  4810. block, the `paragraph`:
  4811. ``` tree
  4812. -> document
  4813. -> block_quote
  4814. -> paragraph
  4815. "Lorem ipsum dolor"
  4816. ```
  4817. The next line,
  4818. ``` markdown
  4819. sit amet.
  4820. ```
  4821. is a "lazy continuation" of the open `paragraph`, so it gets added
  4822. to the paragraph's text:
  4823. ``` tree
  4824. -> document
  4825. -> block_quote
  4826. -> paragraph
  4827. "Lorem ipsum dolor\nsit amet."
  4828. ```
  4829. The third line,
  4830. ``` markdown
  4831. > - Qui *quodsi iracundia*
  4832. ```
  4833. causes the `paragraph` block to be closed, and a new `list` block
  4834. opened as a child of the `block_quote`. A `list_item` is also
  4835. added as a child of the `list`, and a `paragraph` as a chid of
  4836. the `list_item`. The text is then added to the `paragraph`:
  4837. ``` tree
  4838. -> document
  4839. -> block_quote
  4840. paragraph
  4841. "Lorem ipsum dolor\nsit amet."
  4842. -> list (type=bullet tight=true bullet_char=-)
  4843. -> list_item
  4844. -> paragraph
  4845. "Qui *quodsi iracundia*"
  4846. ```
  4847. The fourth line,
  4848. ``` markdown
  4849. > - aliquando id
  4850. ```
  4851. causes the `list_item` (and its child the `paragraph`) to be closed,
  4852. and a new `list_item` opened up as child of the `list`. A `paragraph`
  4853. is added as a child of the new `list_item`, to contain the text.
  4854. We thus obtain the final tree:
  4855. ``` tree
  4856. -> document
  4857. -> block_quote
  4858. paragraph
  4859. "Lorem ipsum dolor\nsit amet."
  4860. -> list (type=bullet tight=true bullet_char=-)
  4861. list_item
  4862. paragraph
  4863. "Qui *quodsi iracundia*"
  4864. -> list_item
  4865. -> paragraph
  4866. "aliquando id"
  4867. ```
  4868. ## From block structure to the final document {-}
  4869. Once all of the input has been parsed, all open blocks are closed.
  4870. We then "walk the tree," visiting every node, and parse raw
  4871. string contents of paragraphs and headers as inlines. At this
  4872. point we have seen all the link reference definitions, so we can
  4873. resolve reference links as we go.
  4874. ``` tree
  4875. document
  4876. block_quote
  4877. paragraph
  4878. str "Lorem ipsum dolor"
  4879. softbreak
  4880. str "sit amet."
  4881. list (type=bullet tight=true bullet_char=-)
  4882. list_item
  4883. paragraph
  4884. str "Qui "
  4885. emph
  4886. str "quodsi iracundia"
  4887. list_item
  4888. paragraph
  4889. str "aliquando id"
  4890. ```
  4891. Notice how the newline in the first paragraph has been parsed as
  4892. a `softbreak`, and the asterisks in the first list item have become
  4893. an `emph`.
  4894. The document can be rendered as HTML, or in any other format, given
  4895. an appropriate renderer.