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