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