aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 81ba573340120c64dc35901c4fd96d033a0dbf96 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.21-dev
  5. date:
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  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 section headers? (`Markdown.pl` does not
  103. allow this, but does allow blockquotes to include headers.)
  104. ``` markdown
  105. - # Heading
  106. ```
  107. 12. Can list items be empty?
  108. ``` markdown
  109. * a
  110. *
  111. * b
  112. ```
  113. 13. Can link references be defined inside block quotes or list items?
  114. ``` markdown
  115. > Blockquote [foo].
  116. >
  117. > [foo]: /url
  118. ```
  119. 14. If there are multiple definitions for the same reference, which takes
  120. precedence?
  121. ``` markdown
  122. [foo]: /url1
  123. [foo]: /url2
  124. [foo][]
  125. ```
  126. In the absence of a spec, early implementers consulted `Markdown.pl`
  127. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  128. gave manifestly bad results in many cases, so it was not a
  129. satisfactory replacement for a spec.
  130. Because there is no unambiguous spec, implementations have diverged
  131. considerably. As a result, users are often surprised to find that
  132. a document that renders one way on one system (say, a github wiki)
  133. renders differently on another (say, converting to docbook using
  134. pandoc). To make matters worse, because nothing in Markdown counts
  135. as a "syntax error," the divergence often isn't discovered right away.
  136. ## About this document
  137. This document attempts to specify Markdown syntax unambiguously.
  138. It contains many examples with side-by-side Markdown and
  139. HTML. These are intended to double as conformance tests. An
  140. accompanying script `spec_tests.py` can be used to run the tests
  141. against any Markdown program:
  142. python test/spec_tests.py --spec spec.txt --program PROGRAM
  143. Since this document describes how Markdown is to be parsed into
  144. an abstract syntax tree, it would have made sense to use an abstract
  145. representation of the syntax tree instead of HTML. But HTML is capable
  146. of representing the structural distinctions we need to make, and the
  147. choice of HTML for the tests makes it possible to run the tests against
  148. an implementation without writing an abstract syntax tree renderer.
  149. This document is generated from a text file, `spec.txt`, written
  150. in Markdown with a small extension for the side-by-side tests.
  151. The script `tools/makespec.py` can be used to convert `spec.txt` into
  152. HTML or CommonMark (which can then be converted into other formats).
  153. In the examples, the `→` character is used to represent tabs.
  154. # Preliminaries
  155. ## Characters and lines
  156. Any sequence of [character]s is a valid CommonMark
  157. document.
  158. A [character](@character) is a unicode code point.
  159. This spec does not specify an encoding; it thinks of lines as composed
  160. of characters rather than bytes. A conforming parser may be limited
  161. to a certain encoding.
  162. A [line](@line) is a sequence of zero or more [character]s
  163. following a [line ending] (or the beginning of the file) and
  164. followed by a [line ending] (or the end of the file).
  165. A [line ending](@line-ending) is a newline (`U+000A`), carriage return
  166. (`U+000D`), or carriage return + newline.
  167. A line containing no characters, or a line containing only spaces
  168. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@blank-line).
  169. The following definitions of character classes will be used in this spec:
  170. A [whitespace character](@whitespace-character) is a space
  171. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  172. form feed (`U+000C`), or carriage return (`U+000D`).
  173. [Whitespace](@whitespace) is a sequence of one or more [whitespace
  174. character]s.
  175. A [unicode whitespace character](@unicode-whitespace-character) is
  176. any code point in the unicode `Zs` class, or a tab (`U+0009`),
  177. carriage return (`U+000D`), newline (`U+000A`), or form feed
  178. (`U+000C`).
  179. [Unicode whitespace](@unicode-whitespace) is a sequence of one
  180. or more [unicode whitespace character]s.
  181. A [space](@space) is `U+0020`.
  182. A [non-whitespace character](@non-space-character) is any character
  183. that is not a [whitespace character].
  184. An [ASCII punctuation character](@ascii-punctuation-character)
  185. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  186. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  187. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  188. A [punctuation character](@punctuation-character) is an [ASCII
  189. punctuation character] or anything in
  190. the unicode classes `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  191. ## Tabs
  192. Tabs in lines are not expanded to [spaces][space]. However,
  193. in contexts where indentation is significant for the
  194. document's structure, tabs behave as if they were replaced
  195. by spaces with a tab stop of 4 characters.
  196. .
  197. →foo→baz→→bim
  198. .
  199. <pre><code>foo→baz→→bim
  200. </code></pre>
  201. .
  202. .
  203. →foo→baz→→bim
  204. .
  205. <pre><code>foo→baz→→bim
  206. </code></pre>
  207. .
  208. .
  209. a→a
  210. ὐ→a
  211. .
  212. <pre><code>a→a
  213. ὐ→a
  214. </code></pre>
  215. .
  216. .
  217. - foo
  218. →bar
  219. .
  220. <ul>
  221. <li>
  222. <p>foo</p>
  223. <p>bar</p>
  224. </li>
  225. </ul>
  226. .
  227. .
  228. >→foo→bar
  229. .
  230. <blockquote>
  231. <p>foo→bar</p>
  232. </blockquote>
  233. .
  234. ## Insecure characters
  235. For security reasons, the Unicode character `U+0000` must be replaced
  236. with the replacement character (`U+FFFD`).
  237. # Blocks and inlines
  238. We can think of a document as a sequence of
  239. [blocks](@block)---structural elements like paragraphs, block
  240. quotations, lists, headers, rules, and code blocks. Some blocks (like
  241. block quotes and list items) contain other blocks; others (like
  242. headers and paragraphs) contain [inline](@inline) content---text,
  243. links, emphasized text, images, code, and so on.
  244. ## Precedence
  245. Indicators of block structure always take precedence over indicators
  246. of inline structure. So, for example, the following is a list with
  247. two items, not a list with one item containing a code span:
  248. .
  249. - `one
  250. - two`
  251. .
  252. <ul>
  253. <li>`one</li>
  254. <li>two`</li>
  255. </ul>
  256. .
  257. This means that parsing can proceed in two steps: first, the block
  258. structure of the document can be discerned; second, text lines inside
  259. paragraphs, headers, and other block constructs can be parsed for inline
  260. structure. The second step requires information about link reference
  261. definitions that will be available only at the end of the first
  262. step. Note that the first step requires processing lines in sequence,
  263. but the second can be parallelized, since the inline parsing of
  264. one block element does not affect the inline parsing of any other.
  265. ## Container blocks and leaf blocks
  266. We can divide blocks into two types:
  267. [container block](@container-block)s,
  268. which can contain other blocks, and [leaf block](@leaf-block)s,
  269. which cannot.
  270. # Leaf blocks
  271. This section describes the different kinds of leaf block that make up a
  272. Markdown document.
  273. ## Horizontal rules
  274. A line consisting of 0-3 spaces of indentation, followed by a sequence
  275. of three or more matching `-`, `_`, or `*` characters, each followed
  276. optionally by any number of spaces, forms a
  277. [horizontal rule](@horizontal-rule).
  278. .
  279. ***
  280. ---
  281. ___
  282. .
  283. <hr />
  284. <hr />
  285. <hr />
  286. .
  287. Wrong characters:
  288. .
  289. +++
  290. .
  291. <p>+++</p>
  292. .
  293. .
  294. ===
  295. .
  296. <p>===</p>
  297. .
  298. Not enough characters:
  299. .
  300. --
  301. **
  302. __
  303. .
  304. <p>--
  305. **
  306. __</p>
  307. .
  308. One to three spaces indent are allowed:
  309. .
  310. ***
  311. ***
  312. ***
  313. .
  314. <hr />
  315. <hr />
  316. <hr />
  317. .
  318. Four spaces is too many:
  319. .
  320. ***
  321. .
  322. <pre><code>***
  323. </code></pre>
  324. .
  325. .
  326. Foo
  327. ***
  328. .
  329. <p>Foo
  330. ***</p>
  331. .
  332. More than three characters may be used:
  333. .
  334. _____________________________________
  335. .
  336. <hr />
  337. .
  338. Spaces are allowed between the characters:
  339. .
  340. - - -
  341. .
  342. <hr />
  343. .
  344. .
  345. ** * ** * ** * **
  346. .
  347. <hr />
  348. .
  349. .
  350. - - - -
  351. .
  352. <hr />
  353. .
  354. Spaces are allowed at the end:
  355. .
  356. - - - -
  357. .
  358. <hr />
  359. .
  360. However, no other characters may occur in the line:
  361. .
  362. _ _ _ _ a
  363. a------
  364. ---a---
  365. .
  366. <p>_ _ _ _ a</p>
  367. <p>a------</p>
  368. <p>---a---</p>
  369. .
  370. It is required that all of the [non-whitespace character]s be the same.
  371. So, this is not a horizontal rule:
  372. .
  373. *-*
  374. .
  375. <p><em>-</em></p>
  376. .
  377. Horizontal rules do not need blank lines before or after:
  378. .
  379. - foo
  380. ***
  381. - bar
  382. .
  383. <ul>
  384. <li>foo</li>
  385. </ul>
  386. <hr />
  387. <ul>
  388. <li>bar</li>
  389. </ul>
  390. .
  391. Horizontal rules can interrupt a paragraph:
  392. .
  393. Foo
  394. ***
  395. bar
  396. .
  397. <p>Foo</p>
  398. <hr />
  399. <p>bar</p>
  400. .
  401. If a line of dashes that meets the above conditions for being a
  402. horizontal rule could also be interpreted as the underline of a [setext
  403. header], the interpretation as a
  404. [setext header] takes precedence. Thus, for example,
  405. this is a setext header, not a paragraph followed by a horizontal rule:
  406. .
  407. Foo
  408. ---
  409. bar
  410. .
  411. <h2>Foo</h2>
  412. <p>bar</p>
  413. .
  414. When both a horizontal rule and a list item are possible
  415. interpretations of a line, the horizontal rule takes precedence:
  416. .
  417. * Foo
  418. * * *
  419. * Bar
  420. .
  421. <ul>
  422. <li>Foo</li>
  423. </ul>
  424. <hr />
  425. <ul>
  426. <li>Bar</li>
  427. </ul>
  428. .
  429. If you want a horizontal rule in a list item, use a different bullet:
  430. .
  431. - Foo
  432. - * * *
  433. .
  434. <ul>
  435. <li>Foo</li>
  436. <li>
  437. <hr />
  438. </li>
  439. </ul>
  440. .
  441. ## ATX headers
  442. An [ATX header](@atx-header)
  443. consists of a string of characters, parsed as inline content, between an
  444. opening sequence of 1--6 unescaped `#` characters and an optional
  445. closing sequence of any number of `#` characters. The opening sequence
  446. of `#` characters cannot be followed directly by a
  447. [non-whitespace character]. The optional closing sequence of `#`s must be
  448. preceded by a [space] and may be followed by spaces only. The opening
  449. `#` character may be indented 0-3 spaces. The raw contents of the
  450. header are stripped of leading and trailing spaces before being parsed
  451. as inline content. The header level is equal to the number of `#`
  452. characters in the opening sequence.
  453. Simple headers:
  454. .
  455. # foo
  456. ## foo
  457. ### foo
  458. #### foo
  459. ##### foo
  460. ###### foo
  461. .
  462. <h1>foo</h1>
  463. <h2>foo</h2>
  464. <h3>foo</h3>
  465. <h4>foo</h4>
  466. <h5>foo</h5>
  467. <h6>foo</h6>
  468. .
  469. More than six `#` characters is not a header:
  470. .
  471. ####### foo
  472. .
  473. <p>####### foo</p>
  474. .
  475. At least one space is required between the `#` characters and the
  476. header's contents, unless the header is empty. Note that many
  477. implementations currently do not require the space. However, the
  478. space was required by the
  479. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  480. and it helps prevent things like the following from being parsed as
  481. headers:
  482. .
  483. #5 bolt
  484. #foobar
  485. .
  486. <p>#5 bolt</p>
  487. <p>#foobar</p>
  488. .
  489. This is not a header, because the first `#` is escaped:
  490. .
  491. \## foo
  492. .
  493. <p>## foo</p>
  494. .
  495. Contents are parsed as inlines:
  496. .
  497. # foo *bar* \*baz\*
  498. .
  499. <h1>foo <em>bar</em> *baz*</h1>
  500. .
  501. Leading and trailing blanks are ignored in parsing inline content:
  502. .
  503. # foo
  504. .
  505. <h1>foo</h1>
  506. .
  507. One to three spaces indentation are allowed:
  508. .
  509. ### foo
  510. ## foo
  511. # foo
  512. .
  513. <h3>foo</h3>
  514. <h2>foo</h2>
  515. <h1>foo</h1>
  516. .
  517. Four spaces are too much:
  518. .
  519. # foo
  520. .
  521. <pre><code># foo
  522. </code></pre>
  523. .
  524. .
  525. foo
  526. # bar
  527. .
  528. <p>foo
  529. # bar</p>
  530. .
  531. A closing sequence of `#` characters is optional:
  532. .
  533. ## foo ##
  534. ### bar ###
  535. .
  536. <h2>foo</h2>
  537. <h3>bar</h3>
  538. .
  539. It need not be the same length as the opening sequence:
  540. .
  541. # foo ##################################
  542. ##### foo ##
  543. .
  544. <h1>foo</h1>
  545. <h5>foo</h5>
  546. .
  547. Spaces are allowed after the closing sequence:
  548. .
  549. ### foo ###
  550. .
  551. <h3>foo</h3>
  552. .
  553. A sequence of `#` characters with a
  554. [non-whitespace character] following it
  555. is not a closing sequence, but counts as part of the contents of the
  556. header:
  557. .
  558. ### foo ### b
  559. .
  560. <h3>foo ### b</h3>
  561. .
  562. The closing sequence must be preceded by a space:
  563. .
  564. # foo#
  565. .
  566. <h1>foo#</h1>
  567. .
  568. Backslash-escaped `#` characters do not count as part
  569. of the closing sequence:
  570. .
  571. ### foo \###
  572. ## foo #\##
  573. # foo \#
  574. .
  575. <h3>foo ###</h3>
  576. <h2>foo ###</h2>
  577. <h1>foo #</h1>
  578. .
  579. ATX headers need not be separated from surrounding content by blank
  580. lines, and they can interrupt paragraphs:
  581. .
  582. ****
  583. ## foo
  584. ****
  585. .
  586. <hr />
  587. <h2>foo</h2>
  588. <hr />
  589. .
  590. .
  591. Foo bar
  592. # baz
  593. Bar foo
  594. .
  595. <p>Foo bar</p>
  596. <h1>baz</h1>
  597. <p>Bar foo</p>
  598. .
  599. ATX headers can be empty:
  600. .
  601. ##
  602. #
  603. ### ###
  604. .
  605. <h2></h2>
  606. <h1></h1>
  607. <h3></h3>
  608. .
  609. ## Setext headers
  610. A [setext header](@setext-header)
  611. consists of a line of text, containing at least one [non-whitespace character],
  612. with no more than 3 spaces indentation, followed by a [setext header
  613. underline]. The line of text must be
  614. one that, were it not followed by the setext header underline,
  615. would be interpreted as part of a paragraph: it cannot be
  616. interpretable as a [code fence], [ATX header][ATX headers],
  617. [block quote][block quotes], [horizontal rule][horizontal rules],
  618. [list item][list items], or [HTML block][HTML blocks].
  619. A [setext header underline](@setext-header-underline) is a sequence of
  620. `=` characters or a sequence of `-` characters, with no more than 3
  621. spaces indentation and any number of trailing spaces. If a line
  622. containing a single `-` can be interpreted as an
  623. empty [list items], it should be interpreted this way
  624. and not as a [setext header underline].
  625. The header is a level 1 header if `=` characters are used in the
  626. [setext header underline], and a level 2
  627. header if `-` characters are used. The contents of the header are the
  628. result of parsing the first line as Markdown inline content.
  629. In general, a setext header need not be preceded or followed by a
  630. blank line. However, it cannot interrupt a paragraph, so when a
  631. setext header comes after a paragraph, a blank line is needed between
  632. them.
  633. Simple examples:
  634. .
  635. Foo *bar*
  636. =========
  637. Foo *bar*
  638. ---------
  639. .
  640. <h1>Foo <em>bar</em></h1>
  641. <h2>Foo <em>bar</em></h2>
  642. .
  643. The underlining can be any length:
  644. .
  645. Foo
  646. -------------------------
  647. Foo
  648. =
  649. .
  650. <h2>Foo</h2>
  651. <h1>Foo</h1>
  652. .
  653. The header content can be indented up to three spaces, and need
  654. not line up with the underlining:
  655. .
  656. Foo
  657. ---
  658. Foo
  659. -----
  660. Foo
  661. ===
  662. .
  663. <h2>Foo</h2>
  664. <h2>Foo</h2>
  665. <h1>Foo</h1>
  666. .
  667. Four spaces indent is too much:
  668. .
  669. Foo
  670. ---
  671. Foo
  672. ---
  673. .
  674. <pre><code>Foo
  675. ---
  676. Foo
  677. </code></pre>
  678. <hr />
  679. .
  680. The setext header underline can be indented up to three spaces, and
  681. may have trailing spaces:
  682. .
  683. Foo
  684. ----
  685. .
  686. <h2>Foo</h2>
  687. .
  688. Four spaces is too much:
  689. .
  690. Foo
  691. ---
  692. .
  693. <p>Foo
  694. ---</p>
  695. .
  696. The setext header underline cannot contain internal spaces:
  697. .
  698. Foo
  699. = =
  700. Foo
  701. --- -
  702. .
  703. <p>Foo
  704. = =</p>
  705. <p>Foo</p>
  706. <hr />
  707. .
  708. Trailing spaces in the content line do not cause a line break:
  709. .
  710. Foo
  711. -----
  712. .
  713. <h2>Foo</h2>
  714. .
  715. Nor does a backslash at the end:
  716. .
  717. Foo\
  718. ----
  719. .
  720. <h2>Foo\</h2>
  721. .
  722. Since indicators of block structure take precedence over
  723. indicators of inline structure, the following are setext headers:
  724. .
  725. `Foo
  726. ----
  727. `
  728. <a title="a lot
  729. ---
  730. of dashes"/>
  731. .
  732. <h2>`Foo</h2>
  733. <p>`</p>
  734. <h2>&lt;a title=&quot;a lot</h2>
  735. <p>of dashes&quot;/&gt;</p>
  736. .
  737. The setext header underline cannot be a [lazy continuation
  738. line] in a list item or block quote:
  739. .
  740. > Foo
  741. ---
  742. .
  743. <blockquote>
  744. <p>Foo</p>
  745. </blockquote>
  746. <hr />
  747. .
  748. .
  749. - Foo
  750. ---
  751. .
  752. <ul>
  753. <li>Foo</li>
  754. </ul>
  755. <hr />
  756. .
  757. A setext header cannot interrupt a paragraph:
  758. .
  759. Foo
  760. Bar
  761. ---
  762. Foo
  763. Bar
  764. ===
  765. .
  766. <p>Foo
  767. Bar</p>
  768. <hr />
  769. <p>Foo
  770. Bar
  771. ===</p>
  772. .
  773. But in general a blank line is not required before or after:
  774. .
  775. ---
  776. Foo
  777. ---
  778. Bar
  779. ---
  780. Baz
  781. .
  782. <hr />
  783. <h2>Foo</h2>
  784. <h2>Bar</h2>
  785. <p>Baz</p>
  786. .
  787. Setext headers cannot be empty:
  788. .
  789. ====
  790. .
  791. <p>====</p>
  792. .
  793. Setext header text lines must not be interpretable as block
  794. constructs other than paragraphs. So, the line of dashes
  795. in these examples gets interpreted as a horizontal rule:
  796. .
  797. ---
  798. ---
  799. .
  800. <hr />
  801. <hr />
  802. .
  803. .
  804. - foo
  805. -----
  806. .
  807. <ul>
  808. <li>foo</li>
  809. </ul>
  810. <hr />
  811. .
  812. .
  813. foo
  814. ---
  815. .
  816. <pre><code>foo
  817. </code></pre>
  818. <hr />
  819. .
  820. .
  821. > foo
  822. -----
  823. .
  824. <blockquote>
  825. <p>foo</p>
  826. </blockquote>
  827. <hr />
  828. .
  829. If you want a header with `> foo` as its literal text, you can
  830. use backslash escapes:
  831. .
  832. \> foo
  833. ------
  834. .
  835. <h2>&gt; foo</h2>
  836. .
  837. ## Indented code blocks
  838. An [indented code block](@indented-code-block) is composed of one or more
  839. [indented chunk]s separated by blank lines.
  840. An [indented chunk](@indented-chunk) is a sequence of non-blank lines,
  841. each indented four or more spaces. The contents of the code block are
  842. the literal contents of the lines, including trailing
  843. [line ending]s, minus four spaces of indentation.
  844. An indented code block has no [info string].
  845. An indented code block cannot interrupt a paragraph, so there must be
  846. a blank line between a paragraph and a following indented code block.
  847. (A blank line is not needed, however, between a code block and a following
  848. paragraph.)
  849. .
  850. a simple
  851. indented code block
  852. .
  853. <pre><code>a simple
  854. indented code block
  855. </code></pre>
  856. .
  857. If there is any ambiguity between an interpretation of indentation
  858. as a code block and as indicating that material belongs to a [list
  859. item][list items], the list item interpretation takes precedence:
  860. .
  861. - foo
  862. bar
  863. .
  864. <ul>
  865. <li>
  866. <p>foo</p>
  867. <p>bar</p>
  868. </li>
  869. </ul>
  870. .
  871. .
  872. 1. foo
  873. - bar
  874. .
  875. <ol>
  876. <li>
  877. <p>foo</p>
  878. <ul>
  879. <li>bar</li>
  880. </ul>
  881. </li>
  882. </ol>
  883. .
  884. The contents of a code block are literal text, and do not get parsed
  885. as Markdown:
  886. .
  887. <a/>
  888. *hi*
  889. - one
  890. .
  891. <pre><code>&lt;a/&gt;
  892. *hi*
  893. - one
  894. </code></pre>
  895. .
  896. Here we have three chunks separated by blank lines:
  897. .
  898. chunk1
  899. chunk2
  900. chunk3
  901. .
  902. <pre><code>chunk1
  903. chunk2
  904. chunk3
  905. </code></pre>
  906. .
  907. Any initial spaces beyond four will be included in the content, even
  908. in interior blank lines:
  909. .
  910. chunk1
  911. chunk2
  912. .
  913. <pre><code>chunk1
  914. chunk2
  915. </code></pre>
  916. .
  917. An indented code block cannot interrupt a paragraph. (This
  918. allows hanging indents and the like.)
  919. .
  920. Foo
  921. bar
  922. .
  923. <p>Foo
  924. bar</p>
  925. .
  926. However, any non-blank line with fewer than four leading spaces ends
  927. the code block immediately. So a paragraph may occur immediately
  928. after indented code:
  929. .
  930. foo
  931. bar
  932. .
  933. <pre><code>foo
  934. </code></pre>
  935. <p>bar</p>
  936. .
  937. And indented code can occur immediately before and after other kinds of
  938. blocks:
  939. .
  940. # Header
  941. foo
  942. Header
  943. ------
  944. foo
  945. ----
  946. .
  947. <h1>Header</h1>
  948. <pre><code>foo
  949. </code></pre>
  950. <h2>Header</h2>
  951. <pre><code>foo
  952. </code></pre>
  953. <hr />
  954. .
  955. The first line can be indented more than four spaces:
  956. .
  957. foo
  958. bar
  959. .
  960. <pre><code> foo
  961. bar
  962. </code></pre>
  963. .
  964. Blank lines preceding or following an indented code block
  965. are not included in it:
  966. .
  967. foo
  968. .
  969. <pre><code>foo
  970. </code></pre>
  971. .
  972. Trailing spaces are included in the code block's content:
  973. .
  974. foo
  975. .
  976. <pre><code>foo
  977. </code></pre>
  978. .
  979. ## Fenced code blocks
  980. A [code fence](@code-fence) is a sequence
  981. of at least three consecutive backtick characters (`` ` ``) or
  982. tildes (`~`). (Tildes and backticks cannot be mixed.)
  983. A [fenced code block](@fenced-code-block)
  984. begins with a code fence, indented no more than three spaces.
  985. The line with the opening code fence may optionally contain some text
  986. following the code fence; this is trimmed of leading and trailing
  987. spaces and called the [info string](@info-string).
  988. The [info string] may not contain any backtick
  989. characters. (The reason for this restriction is that otherwise
  990. some inline code would be incorrectly interpreted as the
  991. beginning of a fenced code block.)
  992. The content of the code block consists of all subsequent lines, until
  993. a closing [code fence] of the same type as the code block
  994. began with (backticks or tildes), and with at least as many backticks
  995. or tildes as the opening code fence. If the leading code fence is
  996. indented N spaces, then up to N spaces of indentation are removed from
  997. each line of the content (if present). (If a content line is not
  998. indented, it is preserved unchanged. If it is indented less than N
  999. spaces, all of the indentation is removed.)
  1000. The closing code fence may be indented up to three spaces, and may be
  1001. followed only by spaces, which are ignored. If the end of the
  1002. containing block (or document) is reached and no closing code fence
  1003. has been found, the code block contains all of the lines after the
  1004. opening code fence until the end of the containing block (or
  1005. document). (An alternative spec would require backtracking in the
  1006. event that a closing code fence is not found. But this makes parsing
  1007. much less efficient, and there seems to be no real down side to the
  1008. behavior described here.)
  1009. A fenced code block may interrupt a paragraph, and does not require
  1010. a blank line either before or after.
  1011. The content of a code fence is treated as literal text, not parsed
  1012. as inlines. The first word of the [info string] is typically used to
  1013. specify the language of the code sample, and rendered in the `class`
  1014. attribute of the `code` tag. However, this spec does not mandate any
  1015. particular treatment of the [info string].
  1016. Here is a simple example with backticks:
  1017. .
  1018. ```
  1019. <
  1020. >
  1021. ```
  1022. .
  1023. <pre><code>&lt;
  1024. &gt;
  1025. </code></pre>
  1026. .
  1027. With tildes:
  1028. .
  1029. ~~~
  1030. <
  1031. >
  1032. ~~~
  1033. .
  1034. <pre><code>&lt;
  1035. &gt;
  1036. </code></pre>
  1037. .
  1038. The closing code fence must use the same character as the opening
  1039. fence:
  1040. .
  1041. ```
  1042. aaa
  1043. ~~~
  1044. ```
  1045. .
  1046. <pre><code>aaa
  1047. ~~~
  1048. </code></pre>
  1049. .
  1050. .
  1051. ~~~
  1052. aaa
  1053. ```
  1054. ~~~
  1055. .
  1056. <pre><code>aaa
  1057. ```
  1058. </code></pre>
  1059. .
  1060. The closing code fence must be at least as long as the opening fence:
  1061. .
  1062. ````
  1063. aaa
  1064. ```
  1065. ``````
  1066. .
  1067. <pre><code>aaa
  1068. ```
  1069. </code></pre>
  1070. .
  1071. .
  1072. ~~~~
  1073. aaa
  1074. ~~~
  1075. ~~~~
  1076. .
  1077. <pre><code>aaa
  1078. ~~~
  1079. </code></pre>
  1080. .
  1081. Unclosed code blocks are closed by the end of the document:
  1082. .
  1083. ```
  1084. .
  1085. <pre><code></code></pre>
  1086. .
  1087. .
  1088. `````
  1089. ```
  1090. aaa
  1091. .
  1092. <pre><code>
  1093. ```
  1094. aaa
  1095. </code></pre>
  1096. .
  1097. A code block can have all empty lines as its content:
  1098. .
  1099. ```
  1100. ```
  1101. .
  1102. <pre><code>
  1103. </code></pre>
  1104. .
  1105. A code block can be empty:
  1106. .
  1107. ```
  1108. ```
  1109. .
  1110. <pre><code></code></pre>
  1111. .
  1112. Fences can be indented. If the opening fence is indented,
  1113. content lines will have equivalent opening indentation removed,
  1114. if present:
  1115. .
  1116. ```
  1117. aaa
  1118. aaa
  1119. ```
  1120. .
  1121. <pre><code>aaa
  1122. aaa
  1123. </code></pre>
  1124. .
  1125. .
  1126. ```
  1127. aaa
  1128. aaa
  1129. aaa
  1130. ```
  1131. .
  1132. <pre><code>aaa
  1133. aaa
  1134. aaa
  1135. </code></pre>
  1136. .
  1137. .
  1138. ```
  1139. aaa
  1140. aaa
  1141. aaa
  1142. ```
  1143. .
  1144. <pre><code>aaa
  1145. aaa
  1146. aaa
  1147. </code></pre>
  1148. .
  1149. Four spaces indentation produces an indented code block:
  1150. .
  1151. ```
  1152. aaa
  1153. ```
  1154. .
  1155. <pre><code>```
  1156. aaa
  1157. ```
  1158. </code></pre>
  1159. .
  1160. Closing fences may be indented by 0-3 spaces, and their indentation
  1161. need not match that of the opening fence:
  1162. .
  1163. ```
  1164. aaa
  1165. ```
  1166. .
  1167. <pre><code>aaa
  1168. </code></pre>
  1169. .
  1170. .
  1171. ```
  1172. aaa
  1173. ```
  1174. .
  1175. <pre><code>aaa
  1176. </code></pre>
  1177. .
  1178. This is not a closing fence, because it is indented 4 spaces:
  1179. .
  1180. ```
  1181. aaa
  1182. ```
  1183. .
  1184. <pre><code>aaa
  1185. ```
  1186. </code></pre>
  1187. .
  1188. Code fences (opening and closing) cannot contain internal spaces:
  1189. .
  1190. ``` ```
  1191. aaa
  1192. .
  1193. <p><code></code>
  1194. aaa</p>
  1195. .
  1196. .
  1197. ~~~~~~
  1198. aaa
  1199. ~~~ ~~
  1200. .
  1201. <pre><code>aaa
  1202. ~~~ ~~
  1203. </code></pre>
  1204. .
  1205. Fenced code blocks can interrupt paragraphs, and can be followed
  1206. directly by paragraphs, without a blank line between:
  1207. .
  1208. foo
  1209. ```
  1210. bar
  1211. ```
  1212. baz
  1213. .
  1214. <p>foo</p>
  1215. <pre><code>bar
  1216. </code></pre>
  1217. <p>baz</p>
  1218. .
  1219. Other blocks can also occur before and after fenced code blocks
  1220. without an intervening blank line:
  1221. .
  1222. foo
  1223. ---
  1224. ~~~
  1225. bar
  1226. ~~~
  1227. # baz
  1228. .
  1229. <h2>foo</h2>
  1230. <pre><code>bar
  1231. </code></pre>
  1232. <h1>baz</h1>
  1233. .
  1234. An [info string] can be provided after the opening code fence.
  1235. Opening and closing spaces will be stripped, and the first word, prefixed
  1236. with `language-`, is used as the value for the `class` attribute of the
  1237. `code` element within the enclosing `pre` element.
  1238. .
  1239. ```ruby
  1240. def foo(x)
  1241. return 3
  1242. end
  1243. ```
  1244. .
  1245. <pre><code class="language-ruby">def foo(x)
  1246. return 3
  1247. end
  1248. </code></pre>
  1249. .
  1250. .
  1251. ~~~~ ruby startline=3 $%@#$
  1252. def foo(x)
  1253. return 3
  1254. end
  1255. ~~~~~~~
  1256. .
  1257. <pre><code class="language-ruby">def foo(x)
  1258. return 3
  1259. end
  1260. </code></pre>
  1261. .
  1262. .
  1263. ````;
  1264. ````
  1265. .
  1266. <pre><code class="language-;"></code></pre>
  1267. .
  1268. [Info string]s for backtick code blocks cannot contain backticks:
  1269. .
  1270. ``` aa ```
  1271. foo
  1272. .
  1273. <p><code>aa</code>
  1274. foo</p>
  1275. .
  1276. Closing code fences cannot have [info string]s:
  1277. .
  1278. ```
  1279. ``` aaa
  1280. ```
  1281. .
  1282. <pre><code>``` aaa
  1283. </code></pre>
  1284. .
  1285. ## HTML blocks
  1286. An [HTML block tag](@html-block-tag) is
  1287. an [open tag] or [closing tag] whose tag
  1288. name is one of the following (case-insensitive):
  1289. `article`, `header`, `aside`, `hgroup`, `blockquote`, `hr`, `iframe`,
  1290. `body`, `li`, `map`, `button`, `object`, `canvas`, `ol`, `caption`,
  1291. `output`, `col`, `p`, `colgroup`, `pre`, `dd`, `progress`, `div`,
  1292. `section`, `dl`, `table`, `td`, `dt`, `tbody`, `embed`, `textarea`,
  1293. `fieldset`, `tfoot`, `figcaption`, `th`, `figure`, `thead`, `footer`,
  1294. `tr`, `form`, `ul`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `video`,
  1295. `script`, `style`.
  1296. An [HTML block](@html-block) begins with an
  1297. [HTML block tag], [HTML comment], [processing instruction],
  1298. [declaration], or [CDATA section].
  1299. It ends when a [blank line] or the end of the
  1300. input is encountered. The initial line may be indented up to three
  1301. spaces, and subsequent lines may have any indentation. The contents
  1302. of the HTML block are interpreted as raw HTML, and will not be escaped
  1303. in HTML output.
  1304. Some simple examples:
  1305. .
  1306. <table>
  1307. <tr>
  1308. <td>
  1309. hi
  1310. </td>
  1311. </tr>
  1312. </table>
  1313. okay.
  1314. .
  1315. <table>
  1316. <tr>
  1317. <td>
  1318. hi
  1319. </td>
  1320. </tr>
  1321. </table>
  1322. <p>okay.</p>
  1323. .
  1324. .
  1325. <div>
  1326. *hello*
  1327. <foo><a>
  1328. .
  1329. <div>
  1330. *hello*
  1331. <foo><a>
  1332. .
  1333. Here we have two HTML blocks with a Markdown paragraph between them:
  1334. .
  1335. <DIV CLASS="foo">
  1336. *Markdown*
  1337. </DIV>
  1338. .
  1339. <DIV CLASS="foo">
  1340. <p><em>Markdown</em></p>
  1341. </DIV>
  1342. .
  1343. In the following example, what looks like a Markdown code block
  1344. is actually part of the HTML block, which continues until a blank
  1345. line or the end of the document is reached:
  1346. .
  1347. <div></div>
  1348. ``` c
  1349. int x = 33;
  1350. ```
  1351. .
  1352. <div></div>
  1353. ``` c
  1354. int x = 33;
  1355. ```
  1356. .
  1357. A comment:
  1358. .
  1359. <!-- Foo
  1360. bar
  1361. baz -->
  1362. .
  1363. <!-- Foo
  1364. bar
  1365. baz -->
  1366. .
  1367. A processing instruction:
  1368. .
  1369. <?php
  1370. echo '>';
  1371. ?>
  1372. .
  1373. <?php
  1374. echo '>';
  1375. ?>
  1376. .
  1377. CDATA:
  1378. .
  1379. <![CDATA[
  1380. function matchwo(a,b)
  1381. {
  1382. if (a < b && a < 0) then
  1383. {
  1384. return 1;
  1385. }
  1386. else
  1387. {
  1388. return 0;
  1389. }
  1390. }
  1391. ]]>
  1392. .
  1393. <![CDATA[
  1394. function matchwo(a,b)
  1395. {
  1396. if (a < b && a < 0) then
  1397. {
  1398. return 1;
  1399. }
  1400. else
  1401. {
  1402. return 0;
  1403. }
  1404. }
  1405. ]]>
  1406. .
  1407. The opening tag can be indented 1-3 spaces, but not 4:
  1408. .
  1409. <!-- foo -->
  1410. <!-- foo -->
  1411. .
  1412. <!-- foo -->
  1413. <pre><code>&lt;!-- foo --&gt;
  1414. </code></pre>
  1415. .
  1416. An HTML block can interrupt a paragraph, and need not be preceded
  1417. by a blank line.
  1418. .
  1419. Foo
  1420. <div>
  1421. bar
  1422. </div>
  1423. .
  1424. <p>Foo</p>
  1425. <div>
  1426. bar
  1427. </div>
  1428. .
  1429. However, a following blank line is always needed, except at the end of
  1430. a document:
  1431. .
  1432. <div>
  1433. bar
  1434. </div>
  1435. *foo*
  1436. .
  1437. <div>
  1438. bar
  1439. </div>
  1440. *foo*
  1441. .
  1442. An incomplete HTML block tag may also start an HTML block:
  1443. .
  1444. <div class
  1445. foo
  1446. .
  1447. <div class
  1448. foo
  1449. .
  1450. This rule differs from John Gruber's original Markdown syntax
  1451. specification, which says:
  1452. > The only restrictions are that block-level HTML elements —
  1453. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  1454. > surrounding content by blank lines, and the start and end tags of the
  1455. > block should not be indented with tabs or spaces.
  1456. In some ways Gruber's rule is more restrictive than the one given
  1457. here:
  1458. - It requires that an HTML block be preceded by a blank line.
  1459. - It does not allow the start tag to be indented.
  1460. - It requires a matching end tag, which it also does not allow to
  1461. be indented.
  1462. Indeed, most Markdown implementations, including some of Gruber's
  1463. own perl implementations, do not impose these restrictions.
  1464. There is one respect, however, in which Gruber's rule is more liberal
  1465. than the one given here, since it allows blank lines to occur inside
  1466. an HTML block. There are two reasons for disallowing them here.
  1467. First, it removes the need to parse balanced tags, which is
  1468. expensive and can require backtracking from the end of the document
  1469. if no matching end tag is found. Second, it provides a very simple
  1470. and flexible way of including Markdown content inside HTML tags:
  1471. simply separate the Markdown from the HTML using blank lines:
  1472. .
  1473. <div>
  1474. *Emphasized* text.
  1475. </div>
  1476. .
  1477. <div>
  1478. <p><em>Emphasized</em> text.</p>
  1479. </div>
  1480. .
  1481. Compare:
  1482. .
  1483. <div>
  1484. *Emphasized* text.
  1485. </div>
  1486. .
  1487. <div>
  1488. *Emphasized* text.
  1489. </div>
  1490. .
  1491. Some Markdown implementations have adopted a convention of
  1492. interpreting content inside tags as text if the open tag has
  1493. the attribute `markdown=1`. The rule given above seems a simpler and
  1494. more elegant way of achieving the same expressive power, which is also
  1495. much simpler to parse.
  1496. The main potential drawback is that one can no longer paste HTML
  1497. blocks into Markdown documents with 100% reliability. However,
  1498. *in most cases* this will work fine, because the blank lines in
  1499. HTML are usually followed by HTML block tags. For example:
  1500. .
  1501. <table>
  1502. <tr>
  1503. <td>
  1504. Hi
  1505. </td>
  1506. </tr>
  1507. </table>
  1508. .
  1509. <table>
  1510. <tr>
  1511. <td>
  1512. Hi
  1513. </td>
  1514. </tr>
  1515. </table>
  1516. .
  1517. Moreover, blank lines are usually not necessary and can be
  1518. deleted. The exception is inside `<pre>` tags; here, one can
  1519. replace the blank lines with `&#10;` entities.
  1520. So there is no important loss of expressive power with the new rule.
  1521. ## Link reference definitions
  1522. A [link reference definition](@link-reference-definition)
  1523. consists of a [link label], indented up to three spaces, followed
  1524. by a colon (`:`), optional [whitespace] (including up to one
  1525. [line ending]), a [link destination],
  1526. optional [whitespace] (including up to one
  1527. [line ending]), and an optional [link
  1528. title], which if it is present must be separated
  1529. from the [link destination] by [whitespace].
  1530. No further [non-whitespace character]s may occur on the line.
  1531. A [link reference definition]
  1532. does not correspond to a structural element of a document. Instead, it
  1533. defines a label which can be used in [reference link]s
  1534. and reference-style [images] elsewhere in the document. [Link
  1535. reference definitions] can come either before or after the links that use
  1536. them.
  1537. .
  1538. [foo]: /url "title"
  1539. [foo]
  1540. .
  1541. <p><a href="/url" title="title">foo</a></p>
  1542. .
  1543. .
  1544. [foo]:
  1545. /url
  1546. 'the title'
  1547. [foo]
  1548. .
  1549. <p><a href="/url" title="the title">foo</a></p>
  1550. .
  1551. .
  1552. [Foo*bar\]]:my_(url) 'title (with parens)'
  1553. [Foo*bar\]]
  1554. .
  1555. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  1556. .
  1557. .
  1558. [Foo bar]:
  1559. <my url>
  1560. 'title'
  1561. [Foo bar]
  1562. .
  1563. <p><a href="my%20url" title="title">Foo bar</a></p>
  1564. .
  1565. The title may extend over multiple lines:
  1566. .
  1567. [foo]: /url '
  1568. title
  1569. line1
  1570. line2
  1571. '
  1572. [foo]
  1573. .
  1574. <p><a href="/url" title="
  1575. title
  1576. line1
  1577. line2
  1578. ">foo</a></p>
  1579. .
  1580. However, it may not contain a [blank line]:
  1581. .
  1582. [foo]: /url 'title
  1583. with blank line'
  1584. [foo]
  1585. .
  1586. <p>[foo]: /url 'title</p>
  1587. <p>with blank line'</p>
  1588. <p>[foo]</p>
  1589. .
  1590. The title may be omitted:
  1591. .
  1592. [foo]:
  1593. /url
  1594. [foo]
  1595. .
  1596. <p><a href="/url">foo</a></p>
  1597. .
  1598. The link destination may not be omitted:
  1599. .
  1600. [foo]:
  1601. [foo]
  1602. .
  1603. <p>[foo]:</p>
  1604. <p>[foo]</p>
  1605. .
  1606. Both title and destination can contain backslash escapes
  1607. and literal backslashes:
  1608. .
  1609. [foo]: /url\bar\*baz "foo\"bar\baz"
  1610. [foo]
  1611. .
  1612. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  1613. .
  1614. A link can come before its corresponding definition:
  1615. .
  1616. [foo]
  1617. [foo]: url
  1618. .
  1619. <p><a href="url">foo</a></p>
  1620. .
  1621. If there are several matching definitions, the first one takes
  1622. precedence:
  1623. .
  1624. [foo]
  1625. [foo]: first
  1626. [foo]: second
  1627. .
  1628. <p><a href="first">foo</a></p>
  1629. .
  1630. As noted in the section on [Links], matching of labels is
  1631. case-insensitive (see [matches]).
  1632. .
  1633. [FOO]: /url
  1634. [Foo]
  1635. .
  1636. <p><a href="/url">Foo</a></p>
  1637. .
  1638. .
  1639. [ΑΓΩ]: /φου
  1640. [αγω]
  1641. .
  1642. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  1643. .
  1644. Here is a link reference definition with no corresponding link.
  1645. It contributes nothing to the document.
  1646. .
  1647. [foo]: /url
  1648. .
  1649. .
  1650. Here is another one:
  1651. .
  1652. [
  1653. foo
  1654. ]: /url
  1655. bar
  1656. .
  1657. <p>bar</p>
  1658. .
  1659. This is not a link reference definition, because there are
  1660. [non-whitespace character]s after the title:
  1661. .
  1662. [foo]: /url "title" ok
  1663. .
  1664. <p>[foo]: /url &quot;title&quot; ok</p>
  1665. .
  1666. This is a link reference definition, but it has no title:
  1667. .
  1668. [foo]: /url
  1669. "title" ok
  1670. .
  1671. <p>&quot;title&quot; ok</p>
  1672. .
  1673. This is not a link reference definition, because it is indented
  1674. four spaces:
  1675. .
  1676. [foo]: /url "title"
  1677. [foo]
  1678. .
  1679. <pre><code>[foo]: /url &quot;title&quot;
  1680. </code></pre>
  1681. <p>[foo]</p>
  1682. .
  1683. This is not a link reference definition, because it occurs inside
  1684. a code block:
  1685. .
  1686. ```
  1687. [foo]: /url
  1688. ```
  1689. [foo]
  1690. .
  1691. <pre><code>[foo]: /url
  1692. </code></pre>
  1693. <p>[foo]</p>
  1694. .
  1695. A [link reference definition] cannot interrupt a paragraph.
  1696. .
  1697. Foo
  1698. [bar]: /baz
  1699. [bar]
  1700. .
  1701. <p>Foo
  1702. [bar]: /baz</p>
  1703. <p>[bar]</p>
  1704. .
  1705. However, it can directly follow other block elements, such as headers
  1706. and horizontal rules, and it need not be followed by a blank line.
  1707. .
  1708. # [Foo]
  1709. [foo]: /url
  1710. > bar
  1711. .
  1712. <h1><a href="/url">Foo</a></h1>
  1713. <blockquote>
  1714. <p>bar</p>
  1715. </blockquote>
  1716. .
  1717. Several [link reference definition]s
  1718. can occur one after another, without intervening blank lines.
  1719. .
  1720. [foo]: /foo-url "foo"
  1721. [bar]: /bar-url
  1722. "bar"
  1723. [baz]: /baz-url
  1724. [foo],
  1725. [bar],
  1726. [baz]
  1727. .
  1728. <p><a href="/foo-url" title="foo">foo</a>,
  1729. <a href="/bar-url" title="bar">bar</a>,
  1730. <a href="/baz-url">baz</a></p>
  1731. .
  1732. [Link reference definition]s can occur
  1733. inside block containers, like lists and block quotations. They
  1734. affect the entire document, not just the container in which they
  1735. are defined:
  1736. .
  1737. [foo]
  1738. > [foo]: /url
  1739. .
  1740. <p><a href="/url">foo</a></p>
  1741. <blockquote>
  1742. </blockquote>
  1743. .
  1744. ## Paragraphs
  1745. A sequence of non-blank lines that cannot be interpreted as other
  1746. kinds of blocks forms a [paragraph](@paragraph).
  1747. The contents of the paragraph are the result of parsing the
  1748. paragraph's raw content as inlines. The paragraph's raw content
  1749. is formed by concatenating the lines and removing initial and final
  1750. [whitespace].
  1751. A simple example with two paragraphs:
  1752. .
  1753. aaa
  1754. bbb
  1755. .
  1756. <p>aaa</p>
  1757. <p>bbb</p>
  1758. .
  1759. Paragraphs can contain multiple lines, but no blank lines:
  1760. .
  1761. aaa
  1762. bbb
  1763. ccc
  1764. ddd
  1765. .
  1766. <p>aaa
  1767. bbb</p>
  1768. <p>ccc
  1769. ddd</p>
  1770. .
  1771. Multiple blank lines between paragraph have no effect:
  1772. .
  1773. aaa
  1774. bbb
  1775. .
  1776. <p>aaa</p>
  1777. <p>bbb</p>
  1778. .
  1779. Leading spaces are skipped:
  1780. .
  1781. aaa
  1782. bbb
  1783. .
  1784. <p>aaa
  1785. bbb</p>
  1786. .
  1787. Lines after the first may be indented any amount, since indented
  1788. code blocks cannot interrupt paragraphs.
  1789. .
  1790. aaa
  1791. bbb
  1792. ccc
  1793. .
  1794. <p>aaa
  1795. bbb
  1796. ccc</p>
  1797. .
  1798. However, the first line may be indented at most three spaces,
  1799. or an indented code block will be triggered:
  1800. .
  1801. aaa
  1802. bbb
  1803. .
  1804. <p>aaa
  1805. bbb</p>
  1806. .
  1807. .
  1808. aaa
  1809. bbb
  1810. .
  1811. <pre><code>aaa
  1812. </code></pre>
  1813. <p>bbb</p>
  1814. .
  1815. Final spaces are stripped before inline parsing, so a paragraph
  1816. that ends with two or more spaces will not end with a [hard line
  1817. break]:
  1818. .
  1819. aaa
  1820. bbb
  1821. .
  1822. <p>aaa<br />
  1823. bbb</p>
  1824. .
  1825. ## Blank lines
  1826. [Blank line]s between block-level elements are ignored,
  1827. except for the role they play in determining whether a [list]
  1828. is [tight] or [loose].
  1829. Blank lines at the beginning and end of the document are also ignored.
  1830. .
  1831. aaa
  1832. # aaa
  1833. .
  1834. <p>aaa</p>
  1835. <h1>aaa</h1>
  1836. .
  1837. # Container blocks
  1838. A [container block] is a block that has other
  1839. blocks as its contents. There are two basic kinds of container blocks:
  1840. [block quotes] and [list items].
  1841. [Lists] are meta-containers for [list items].
  1842. We define the syntax for container blocks recursively. The general
  1843. form of the definition is:
  1844. > If X is a sequence of blocks, then the result of
  1845. > transforming X in such-and-such a way is a container of type Y
  1846. > with these blocks as its content.
  1847. So, we explain what counts as a block quote or list item by explaining
  1848. how these can be *generated* from their contents. This should suffice
  1849. to define the syntax, although it does not give a recipe for *parsing*
  1850. these constructions. (A recipe is provided below in the section entitled
  1851. [A parsing strategy](#appendix-a-a-parsing-strategy).)
  1852. ## Block quotes
  1853. A [block quote marker](@block-quote-marker)
  1854. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  1855. with a following space, or (b) a single character `>` not followed by a space.
  1856. The following rules define [block quotes]:
  1857. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  1858. of blocks *Bs*, then the result of prepending a [block quote
  1859. marker] to the beginning of each line in *Ls*
  1860. is a [block quote](#block-quotes) containing *Bs*.
  1861. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  1862. quote](#block-quotes) with contents *Bs*, then the result of deleting
  1863. the initial [block quote marker] from one or
  1864. more lines in which the next [non-whitespace character] after the [block
  1865. quote marker] is [paragraph continuation
  1866. text] is a block quote with *Bs* as its content.
  1867. [Paragraph continuation text](@paragraph-continuation-text) is text
  1868. that will be parsed as part of the content of a paragraph, but does
  1869. not occur at the beginning of the paragraph.
  1870. 3. **Consecutiveness.** A document cannot contain two [block
  1871. quotes] in a row unless there is a [blank line] between them.
  1872. Nothing else counts as a [block quote](#block-quotes).
  1873. Here is a simple example:
  1874. .
  1875. > # Foo
  1876. > bar
  1877. > baz
  1878. .
  1879. <blockquote>
  1880. <h1>Foo</h1>
  1881. <p>bar
  1882. baz</p>
  1883. </blockquote>
  1884. .
  1885. The spaces after the `>` characters can be omitted:
  1886. .
  1887. ># Foo
  1888. >bar
  1889. > baz
  1890. .
  1891. <blockquote>
  1892. <h1>Foo</h1>
  1893. <p>bar
  1894. baz</p>
  1895. </blockquote>
  1896. .
  1897. The `>` characters can be indented 1-3 spaces:
  1898. .
  1899. > # Foo
  1900. > bar
  1901. > baz
  1902. .
  1903. <blockquote>
  1904. <h1>Foo</h1>
  1905. <p>bar
  1906. baz</p>
  1907. </blockquote>
  1908. .
  1909. Four spaces gives us a code block:
  1910. .
  1911. > # Foo
  1912. > bar
  1913. > baz
  1914. .
  1915. <pre><code>&gt; # Foo
  1916. &gt; bar
  1917. &gt; baz
  1918. </code></pre>
  1919. .
  1920. The Laziness clause allows us to omit the `>` before a
  1921. paragraph continuation line:
  1922. .
  1923. > # Foo
  1924. > bar
  1925. baz
  1926. .
  1927. <blockquote>
  1928. <h1>Foo</h1>
  1929. <p>bar
  1930. baz</p>
  1931. </blockquote>
  1932. .
  1933. A block quote can contain some lazy and some non-lazy
  1934. continuation lines:
  1935. .
  1936. > bar
  1937. baz
  1938. > foo
  1939. .
  1940. <blockquote>
  1941. <p>bar
  1942. baz
  1943. foo</p>
  1944. </blockquote>
  1945. .
  1946. Laziness only applies to lines that would have been continuations of
  1947. paragraphs had they been prepended with `>`. For example, the
  1948. `>` cannot be omitted in the second line of
  1949. ``` markdown
  1950. > foo
  1951. > ---
  1952. ```
  1953. without changing the meaning:
  1954. .
  1955. > foo
  1956. ---
  1957. .
  1958. <blockquote>
  1959. <p>foo</p>
  1960. </blockquote>
  1961. <hr />
  1962. .
  1963. Similarly, if we omit the `>` in the second line of
  1964. ``` markdown
  1965. > - foo
  1966. > - bar
  1967. ```
  1968. then the block quote ends after the first line:
  1969. .
  1970. > - foo
  1971. - bar
  1972. .
  1973. <blockquote>
  1974. <ul>
  1975. <li>foo</li>
  1976. </ul>
  1977. </blockquote>
  1978. <ul>
  1979. <li>bar</li>
  1980. </ul>
  1981. .
  1982. For the same reason, we can't omit the `>` in front of
  1983. subsequent lines of an indented or fenced code block:
  1984. .
  1985. > foo
  1986. bar
  1987. .
  1988. <blockquote>
  1989. <pre><code>foo
  1990. </code></pre>
  1991. </blockquote>
  1992. <pre><code>bar
  1993. </code></pre>
  1994. .
  1995. .
  1996. > ```
  1997. foo
  1998. ```
  1999. .
  2000. <blockquote>
  2001. <pre><code></code></pre>
  2002. </blockquote>
  2003. <p>foo</p>
  2004. <pre><code></code></pre>
  2005. .
  2006. A block quote can be empty:
  2007. .
  2008. >
  2009. .
  2010. <blockquote>
  2011. </blockquote>
  2012. .
  2013. .
  2014. >
  2015. >
  2016. >
  2017. .
  2018. <blockquote>
  2019. </blockquote>
  2020. .
  2021. A block quote can have initial or final blank lines:
  2022. .
  2023. >
  2024. > foo
  2025. >
  2026. .
  2027. <blockquote>
  2028. <p>foo</p>
  2029. </blockquote>
  2030. .
  2031. A blank line always separates block quotes:
  2032. .
  2033. > foo
  2034. > bar
  2035. .
  2036. <blockquote>
  2037. <p>foo</p>
  2038. </blockquote>
  2039. <blockquote>
  2040. <p>bar</p>
  2041. </blockquote>
  2042. .
  2043. (Most current Markdown implementations, including John Gruber's
  2044. original `Markdown.pl`, will parse this example as a single block quote
  2045. with two paragraphs. But it seems better to allow the author to decide
  2046. whether two block quotes or one are wanted.)
  2047. Consecutiveness means that if we put these block quotes together,
  2048. we get a single block quote:
  2049. .
  2050. > foo
  2051. > bar
  2052. .
  2053. <blockquote>
  2054. <p>foo
  2055. bar</p>
  2056. </blockquote>
  2057. .
  2058. To get a block quote with two paragraphs, use:
  2059. .
  2060. > foo
  2061. >
  2062. > bar
  2063. .
  2064. <blockquote>
  2065. <p>foo</p>
  2066. <p>bar</p>
  2067. </blockquote>
  2068. .
  2069. Block quotes can interrupt paragraphs:
  2070. .
  2071. foo
  2072. > bar
  2073. .
  2074. <p>foo</p>
  2075. <blockquote>
  2076. <p>bar</p>
  2077. </blockquote>
  2078. .
  2079. In general, blank lines are not needed before or after block
  2080. quotes:
  2081. .
  2082. > aaa
  2083. ***
  2084. > bbb
  2085. .
  2086. <blockquote>
  2087. <p>aaa</p>
  2088. </blockquote>
  2089. <hr />
  2090. <blockquote>
  2091. <p>bbb</p>
  2092. </blockquote>
  2093. .
  2094. However, because of laziness, a blank line is needed between
  2095. a block quote and a following paragraph:
  2096. .
  2097. > bar
  2098. baz
  2099. .
  2100. <blockquote>
  2101. <p>bar
  2102. baz</p>
  2103. </blockquote>
  2104. .
  2105. .
  2106. > bar
  2107. baz
  2108. .
  2109. <blockquote>
  2110. <p>bar</p>
  2111. </blockquote>
  2112. <p>baz</p>
  2113. .
  2114. .
  2115. > bar
  2116. >
  2117. baz
  2118. .
  2119. <blockquote>
  2120. <p>bar</p>
  2121. </blockquote>
  2122. <p>baz</p>
  2123. .
  2124. It is a consequence of the Laziness rule that any number
  2125. of initial `>`s may be omitted on a continuation line of a
  2126. nested block quote:
  2127. .
  2128. > > > foo
  2129. bar
  2130. .
  2131. <blockquote>
  2132. <blockquote>
  2133. <blockquote>
  2134. <p>foo
  2135. bar</p>
  2136. </blockquote>
  2137. </blockquote>
  2138. </blockquote>
  2139. .
  2140. .
  2141. >>> foo
  2142. > bar
  2143. >>baz
  2144. .
  2145. <blockquote>
  2146. <blockquote>
  2147. <blockquote>
  2148. <p>foo
  2149. bar
  2150. baz</p>
  2151. </blockquote>
  2152. </blockquote>
  2153. </blockquote>
  2154. .
  2155. When including an indented code block in a block quote,
  2156. remember that the [block quote marker] includes
  2157. both the `>` and a following space. So *five spaces* are needed after
  2158. the `>`:
  2159. .
  2160. > code
  2161. > not code
  2162. .
  2163. <blockquote>
  2164. <pre><code>code
  2165. </code></pre>
  2166. </blockquote>
  2167. <blockquote>
  2168. <p>not code</p>
  2169. </blockquote>
  2170. .
  2171. ## List items
  2172. A [list marker](@list-marker) is a
  2173. [bullet list marker] or an [ordered list marker].
  2174. A [bullet list marker](@bullet-list-marker)
  2175. is a `-`, `+`, or `*` character.
  2176. An [ordered list marker](@ordered-list-marker)
  2177. is a sequence of one of more digits (`0-9`), followed by either a
  2178. `.` character or a `)` character.
  2179. The following rules define [list items]:
  2180. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2181. blocks *Bs* starting with a [non-whitespace character] and not separated
  2182. from each other by more than one blank line, and *M* is a list
  2183. marker of width *W* followed by 0 < *N* < 5 spaces, then the result
  2184. of prepending *M* and the following spaces to the first line of
  2185. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2186. list item with *Bs* as its contents. The type of the list item
  2187. (bullet or ordered) is determined by the type of its list marker.
  2188. If the list item is ordered, then it is also assigned a start
  2189. number, based on the ordered list marker.
  2190. For example, let *Ls* be the lines
  2191. .
  2192. A paragraph
  2193. with two lines.
  2194. indented code
  2195. > A block quote.
  2196. .
  2197. <p>A paragraph
  2198. with two lines.</p>
  2199. <pre><code>indented code
  2200. </code></pre>
  2201. <blockquote>
  2202. <p>A block quote.</p>
  2203. </blockquote>
  2204. .
  2205. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2206. that the following is an ordered list item with start number 1,
  2207. and the same contents as *Ls*:
  2208. .
  2209. 1. A paragraph
  2210. with two lines.
  2211. indented code
  2212. > A block quote.
  2213. .
  2214. <ol>
  2215. <li>
  2216. <p>A paragraph
  2217. with two lines.</p>
  2218. <pre><code>indented code
  2219. </code></pre>
  2220. <blockquote>
  2221. <p>A block quote.</p>
  2222. </blockquote>
  2223. </li>
  2224. </ol>
  2225. .
  2226. The most important thing to notice is that the position of
  2227. the text after the list marker determines how much indentation
  2228. is needed in subsequent blocks in the list item. If the list
  2229. marker takes up two spaces, and there are three spaces between
  2230. the list marker and the next [non-whitespace character], then blocks
  2231. must be indented five spaces in order to fall under the list
  2232. item.
  2233. Here are some examples showing how far content must be indented to be
  2234. put under the list item:
  2235. .
  2236. - one
  2237. two
  2238. .
  2239. <ul>
  2240. <li>one</li>
  2241. </ul>
  2242. <p>two</p>
  2243. .
  2244. .
  2245. - one
  2246. two
  2247. .
  2248. <ul>
  2249. <li>
  2250. <p>one</p>
  2251. <p>two</p>
  2252. </li>
  2253. </ul>
  2254. .
  2255. .
  2256. - one
  2257. two
  2258. .
  2259. <ul>
  2260. <li>one</li>
  2261. </ul>
  2262. <pre><code> two
  2263. </code></pre>
  2264. .
  2265. .
  2266. - one
  2267. two
  2268. .
  2269. <ul>
  2270. <li>
  2271. <p>one</p>
  2272. <p>two</p>
  2273. </li>
  2274. </ul>
  2275. .
  2276. It is tempting to think of this in terms of columns: the continuation
  2277. blocks must be indented at least to the column of the first
  2278. [non-whitespace character] after the list marker. However, that is not quite right.
  2279. The spaces after the list marker determine how much relative indentation
  2280. is needed. Which column this indentation reaches will depend on
  2281. how the list item is embedded in other constructions, as shown by
  2282. this example:
  2283. .
  2284. > > 1. one
  2285. >>
  2286. >> two
  2287. .
  2288. <blockquote>
  2289. <blockquote>
  2290. <ol>
  2291. <li>
  2292. <p>one</p>
  2293. <p>two</p>
  2294. </li>
  2295. </ol>
  2296. </blockquote>
  2297. </blockquote>
  2298. .
  2299. Here `two` occurs in the same column as the list marker `1.`,
  2300. but is actually contained in the list item, because there is
  2301. sufficient indentation after the last containing blockquote marker.
  2302. The converse is also possible. In the following example, the word `two`
  2303. occurs far to the right of the initial text of the list item, `one`, but
  2304. it is not considered part of the list item, because it is not indented
  2305. far enough past the blockquote marker:
  2306. .
  2307. >>- one
  2308. >>
  2309. > > two
  2310. .
  2311. <blockquote>
  2312. <blockquote>
  2313. <ul>
  2314. <li>one</li>
  2315. </ul>
  2316. <p>two</p>
  2317. </blockquote>
  2318. </blockquote>
  2319. .
  2320. Note that at least one space is needed between the list marker and
  2321. any following content, so these are not list items:
  2322. .
  2323. -one
  2324. 2.two
  2325. .
  2326. <p>-one</p>
  2327. <p>2.two</p>
  2328. .
  2329. A list item may not contain blocks that are separated by more than
  2330. one blank line. Thus, two blank lines will end a list, unless the
  2331. two blanks are contained in a [fenced code block].
  2332. .
  2333. - foo
  2334. bar
  2335. - foo
  2336. bar
  2337. - ```
  2338. foo
  2339. bar
  2340. ```
  2341. - baz
  2342. + ```
  2343. foo
  2344. bar
  2345. ```
  2346. .
  2347. <ul>
  2348. <li>
  2349. <p>foo</p>
  2350. <p>bar</p>
  2351. </li>
  2352. <li>
  2353. <p>foo</p>
  2354. </li>
  2355. </ul>
  2356. <p>bar</p>
  2357. <ul>
  2358. <li>
  2359. <pre><code>foo
  2360. bar
  2361. </code></pre>
  2362. </li>
  2363. <li>
  2364. <p>baz</p>
  2365. <ul>
  2366. <li>
  2367. <pre><code>foo
  2368. bar
  2369. </code></pre>
  2370. </li>
  2371. </ul>
  2372. </li>
  2373. </ul>
  2374. .
  2375. A list item may contain any kind of block:
  2376. .
  2377. 1. foo
  2378. ```
  2379. bar
  2380. ```
  2381. baz
  2382. > bam
  2383. .
  2384. <ol>
  2385. <li>
  2386. <p>foo</p>
  2387. <pre><code>bar
  2388. </code></pre>
  2389. <p>baz</p>
  2390. <blockquote>
  2391. <p>bam</p>
  2392. </blockquote>
  2393. </li>
  2394. </ol>
  2395. .
  2396. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  2397. constitute a sequence of blocks *Bs* starting with an indented code
  2398. block and not separated from each other by more than one blank line,
  2399. and *M* is a list marker of width *W* followed by
  2400. one space, then the result of prepending *M* and the following
  2401. space to the first line of *Ls*, and indenting subsequent lines of
  2402. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  2403. If a line is empty, then it need not be indented. The type of the
  2404. list item (bullet or ordered) is determined by the type of its list
  2405. marker. If the list item is ordered, then it is also assigned a
  2406. start number, based on the ordered list marker.
  2407. An indented code block will have to be indented four spaces beyond
  2408. the edge of the region where text will be included in the list item.
  2409. In the following case that is 6 spaces:
  2410. .
  2411. - foo
  2412. bar
  2413. .
  2414. <ul>
  2415. <li>
  2416. <p>foo</p>
  2417. <pre><code>bar
  2418. </code></pre>
  2419. </li>
  2420. </ul>
  2421. .
  2422. And in this case it is 11 spaces:
  2423. .
  2424. 10. foo
  2425. bar
  2426. .
  2427. <ol start="10">
  2428. <li>
  2429. <p>foo</p>
  2430. <pre><code>bar
  2431. </code></pre>
  2432. </li>
  2433. </ol>
  2434. .
  2435. If the *first* block in the list item is an indented code block,
  2436. then by rule #2, the contents must be indented *one* space after the
  2437. list marker:
  2438. .
  2439. indented code
  2440. paragraph
  2441. more code
  2442. .
  2443. <pre><code>indented code
  2444. </code></pre>
  2445. <p>paragraph</p>
  2446. <pre><code>more code
  2447. </code></pre>
  2448. .
  2449. .
  2450. 1. indented code
  2451. paragraph
  2452. more code
  2453. .
  2454. <ol>
  2455. <li>
  2456. <pre><code>indented code
  2457. </code></pre>
  2458. <p>paragraph</p>
  2459. <pre><code>more code
  2460. </code></pre>
  2461. </li>
  2462. </ol>
  2463. .
  2464. Note that an additional space indent is interpreted as space
  2465. inside the code block:
  2466. .
  2467. 1. indented code
  2468. paragraph
  2469. more code
  2470. .
  2471. <ol>
  2472. <li>
  2473. <pre><code> indented code
  2474. </code></pre>
  2475. <p>paragraph</p>
  2476. <pre><code>more code
  2477. </code></pre>
  2478. </li>
  2479. </ol>
  2480. .
  2481. Note that rules #1 and #2 only apply to two cases: (a) cases
  2482. in which the lines to be included in a list item begin with a
  2483. [non-whitespace character], and (b) cases in which
  2484. they begin with an indented code
  2485. block. In a case like the following, where the first block begins with
  2486. a three-space indent, the rules do not allow us to form a list item by
  2487. indenting the whole thing and prepending a list marker:
  2488. .
  2489. foo
  2490. bar
  2491. .
  2492. <p>foo</p>
  2493. <p>bar</p>
  2494. .
  2495. .
  2496. - foo
  2497. bar
  2498. .
  2499. <ul>
  2500. <li>foo</li>
  2501. </ul>
  2502. <p>bar</p>
  2503. .
  2504. This is not a significant restriction, because when a block begins
  2505. with 1-3 spaces indent, the indentation can always be removed without
  2506. a change in interpretation, allowing rule #1 to be applied. So, in
  2507. the above case:
  2508. .
  2509. - foo
  2510. bar
  2511. .
  2512. <ul>
  2513. <li>
  2514. <p>foo</p>
  2515. <p>bar</p>
  2516. </li>
  2517. </ul>
  2518. .
  2519. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  2520. starting with a single [blank line] constitute a (possibly empty)
  2521. sequence of blocks *Bs*, not separated from each other by more than
  2522. one blank line, and *M* is a list marker of width *W*,
  2523. then the result of prepending *M* to the first line of *Ls*, and
  2524. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  2525. item with *Bs* as its contents.
  2526. If a line is empty, then it need not be indented. The type of the
  2527. list item (bullet or ordered) is determined by the type of its list
  2528. marker. If the list item is ordered, then it is also assigned a
  2529. start number, based on the ordered list marker.
  2530. Here are some list items that start with a blank line but are not empty:
  2531. .
  2532. -
  2533. foo
  2534. -
  2535. ```
  2536. bar
  2537. ```
  2538. -
  2539. baz
  2540. .
  2541. <ul>
  2542. <li>foo</li>
  2543. <li>
  2544. <pre><code>bar
  2545. </code></pre>
  2546. </li>
  2547. <li>
  2548. <pre><code>baz
  2549. </code></pre>
  2550. </li>
  2551. </ul>
  2552. .
  2553. Here is an empty bullet list item:
  2554. .
  2555. - foo
  2556. -
  2557. - bar
  2558. .
  2559. <ul>
  2560. <li>foo</li>
  2561. <li></li>
  2562. <li>bar</li>
  2563. </ul>
  2564. .
  2565. It does not matter whether there are spaces following the [list marker]:
  2566. .
  2567. - foo
  2568. -
  2569. - bar
  2570. .
  2571. <ul>
  2572. <li>foo</li>
  2573. <li></li>
  2574. <li>bar</li>
  2575. </ul>
  2576. .
  2577. Here is an empty ordered list item:
  2578. .
  2579. 1. foo
  2580. 2.
  2581. 3. bar
  2582. .
  2583. <ol>
  2584. <li>foo</li>
  2585. <li></li>
  2586. <li>bar</li>
  2587. </ol>
  2588. .
  2589. A list may start or end with an empty list item:
  2590. .
  2591. *
  2592. .
  2593. <ul>
  2594. <li></li>
  2595. </ul>
  2596. .
  2597. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  2598. according to rule #1, #2, or #3, then the result of indenting each line
  2599. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  2600. list item with the same contents and attributes. If a line is
  2601. empty, then it need not be indented.
  2602. Indented one space:
  2603. .
  2604. 1. A paragraph
  2605. with two lines.
  2606. indented code
  2607. > A block quote.
  2608. .
  2609. <ol>
  2610. <li>
  2611. <p>A paragraph
  2612. with two lines.</p>
  2613. <pre><code>indented code
  2614. </code></pre>
  2615. <blockquote>
  2616. <p>A block quote.</p>
  2617. </blockquote>
  2618. </li>
  2619. </ol>
  2620. .
  2621. Indented two spaces:
  2622. .
  2623. 1. A paragraph
  2624. with two lines.
  2625. indented code
  2626. > A block quote.
  2627. .
  2628. <ol>
  2629. <li>
  2630. <p>A paragraph
  2631. with two lines.</p>
  2632. <pre><code>indented code
  2633. </code></pre>
  2634. <blockquote>
  2635. <p>A block quote.</p>
  2636. </blockquote>
  2637. </li>
  2638. </ol>
  2639. .
  2640. Indented three spaces:
  2641. .
  2642. 1. A paragraph
  2643. with two lines.
  2644. indented code
  2645. > A block quote.
  2646. .
  2647. <ol>
  2648. <li>
  2649. <p>A paragraph
  2650. with two lines.</p>
  2651. <pre><code>indented code
  2652. </code></pre>
  2653. <blockquote>
  2654. <p>A block quote.</p>
  2655. </blockquote>
  2656. </li>
  2657. </ol>
  2658. .
  2659. Four spaces indent gives a code block:
  2660. .
  2661. 1. A paragraph
  2662. with two lines.
  2663. indented code
  2664. > A block quote.
  2665. .
  2666. <pre><code>1. A paragraph
  2667. with two lines.
  2668. indented code
  2669. &gt; A block quote.
  2670. </code></pre>
  2671. .
  2672. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  2673. item](#list-items) with contents *Bs*, then the result of deleting
  2674. some or all of the indentation from one or more lines in which the
  2675. next [non-whitespace character] after the indentation is
  2676. [paragraph continuation text] is a
  2677. list item with the same contents and attributes. The unindented
  2678. lines are called
  2679. [lazy continuation line](@lazy-continuation-line)s.
  2680. Here is an example with [lazy continuation line]s:
  2681. .
  2682. 1. A paragraph
  2683. with two lines.
  2684. indented code
  2685. > A block quote.
  2686. .
  2687. <ol>
  2688. <li>
  2689. <p>A paragraph
  2690. with two lines.</p>
  2691. <pre><code>indented code
  2692. </code></pre>
  2693. <blockquote>
  2694. <p>A block quote.</p>
  2695. </blockquote>
  2696. </li>
  2697. </ol>
  2698. .
  2699. Indentation can be partially deleted:
  2700. .
  2701. 1. A paragraph
  2702. with two lines.
  2703. .
  2704. <ol>
  2705. <li>A paragraph
  2706. with two lines.</li>
  2707. </ol>
  2708. .
  2709. These examples show how laziness can work in nested structures:
  2710. .
  2711. > 1. > Blockquote
  2712. continued here.
  2713. .
  2714. <blockquote>
  2715. <ol>
  2716. <li>
  2717. <blockquote>
  2718. <p>Blockquote
  2719. continued here.</p>
  2720. </blockquote>
  2721. </li>
  2722. </ol>
  2723. </blockquote>
  2724. .
  2725. .
  2726. > 1. > Blockquote
  2727. > continued here.
  2728. .
  2729. <blockquote>
  2730. <ol>
  2731. <li>
  2732. <blockquote>
  2733. <p>Blockquote
  2734. continued here.</p>
  2735. </blockquote>
  2736. </li>
  2737. </ol>
  2738. </blockquote>
  2739. .
  2740. 6. **That's all.** Nothing that is not counted as a list item by rules
  2741. #1--5 counts as a [list item](#list-items).
  2742. The rules for sublists follow from the general rules above. A sublist
  2743. must be indented the same number of spaces a paragraph would need to be
  2744. in order to be included in the list item.
  2745. So, in this case we need two spaces indent:
  2746. .
  2747. - foo
  2748. - bar
  2749. - baz
  2750. .
  2751. <ul>
  2752. <li>foo
  2753. <ul>
  2754. <li>bar
  2755. <ul>
  2756. <li>baz</li>
  2757. </ul>
  2758. </li>
  2759. </ul>
  2760. </li>
  2761. </ul>
  2762. .
  2763. One is not enough:
  2764. .
  2765. - foo
  2766. - bar
  2767. - baz
  2768. .
  2769. <ul>
  2770. <li>foo</li>
  2771. <li>bar</li>
  2772. <li>baz</li>
  2773. </ul>
  2774. .
  2775. Here we need four, because the list marker is wider:
  2776. .
  2777. 10) foo
  2778. - bar
  2779. .
  2780. <ol start="10">
  2781. <li>foo
  2782. <ul>
  2783. <li>bar</li>
  2784. </ul>
  2785. </li>
  2786. </ol>
  2787. .
  2788. Three is not enough:
  2789. .
  2790. 10) foo
  2791. - bar
  2792. .
  2793. <ol start="10">
  2794. <li>foo</li>
  2795. </ol>
  2796. <ul>
  2797. <li>bar</li>
  2798. </ul>
  2799. .
  2800. A list may be the first block in a list item:
  2801. .
  2802. - - foo
  2803. .
  2804. <ul>
  2805. <li>
  2806. <ul>
  2807. <li>foo</li>
  2808. </ul>
  2809. </li>
  2810. </ul>
  2811. .
  2812. .
  2813. 1. - 2. foo
  2814. .
  2815. <ol>
  2816. <li>
  2817. <ul>
  2818. <li>
  2819. <ol start="2">
  2820. <li>foo</li>
  2821. </ol>
  2822. </li>
  2823. </ul>
  2824. </li>
  2825. </ol>
  2826. .
  2827. A list item can contain a header:
  2828. .
  2829. - # Foo
  2830. - Bar
  2831. ---
  2832. baz
  2833. .
  2834. <ul>
  2835. <li>
  2836. <h1>Foo</h1>
  2837. </li>
  2838. <li>
  2839. <h2>Bar</h2>
  2840. baz</li>
  2841. </ul>
  2842. .
  2843. ### Motivation
  2844. John Gruber's Markdown spec says the following about list items:
  2845. 1. "List markers typically start at the left margin, but may be indented
  2846. by up to three spaces. List markers must be followed by one or more
  2847. spaces or a tab."
  2848. 2. "To make lists look nice, you can wrap items with hanging indents....
  2849. But if you don't want to, you don't have to."
  2850. 3. "List items may consist of multiple paragraphs. Each subsequent
  2851. paragraph in a list item must be indented by either 4 spaces or one
  2852. tab."
  2853. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  2854. but here again, Markdown will allow you to be lazy."
  2855. 5. "To put a blockquote within a list item, the blockquote's `>`
  2856. delimiters need to be indented."
  2857. 6. "To put a code block within a list item, the code block needs to be
  2858. indented twice — 8 spaces or two tabs."
  2859. These rules specify that a paragraph under a list item must be indented
  2860. four spaces (presumably, from the left margin, rather than the start of
  2861. the list marker, but this is not said), and that code under a list item
  2862. must be indented eight spaces instead of the usual four. They also say
  2863. that a block quote must be indented, but not by how much; however, the
  2864. example given has four spaces indentation. Although nothing is said
  2865. about other kinds of block-level content, it is certainly reasonable to
  2866. infer that *all* block elements under a list item, including other
  2867. lists, must be indented four spaces. This principle has been called the
  2868. *four-space rule*.
  2869. The four-space rule is clear and principled, and if the reference
  2870. implementation `Markdown.pl` had followed it, it probably would have
  2871. become the standard. However, `Markdown.pl` allowed paragraphs and
  2872. sublists to start with only two spaces indentation, at least on the
  2873. outer level. Worse, its behavior was inconsistent: a sublist of an
  2874. outer-level list needed two spaces indentation, but a sublist of this
  2875. sublist needed three spaces. It is not surprising, then, that different
  2876. implementations of Markdown have developed very different rules for
  2877. determining what comes under a list item. (Pandoc and python-Markdown,
  2878. for example, stuck with Gruber's syntax description and the four-space
  2879. rule, while discount, redcarpet, marked, PHP Markdown, and others
  2880. followed `Markdown.pl`'s behavior more closely.)
  2881. Unfortunately, given the divergences between implementations, there
  2882. is no way to give a spec for list items that will be guaranteed not
  2883. to break any existing documents. However, the spec given here should
  2884. correctly handle lists formatted with either the four-space rule or
  2885. the more forgiving `Markdown.pl` behavior, provided they are laid out
  2886. in a way that is natural for a human to read.
  2887. The strategy here is to let the width and indentation of the list marker
  2888. determine the indentation necessary for blocks to fall under the list
  2889. item, rather than having a fixed and arbitrary number. The writer can
  2890. think of the body of the list item as a unit which gets indented to the
  2891. right enough to fit the list marker (and any indentation on the list
  2892. marker). (The laziness rule, #5, then allows continuation lines to be
  2893. unindented if needed.)
  2894. This rule is superior, we claim, to any rule requiring a fixed level of
  2895. indentation from the margin. The four-space rule is clear but
  2896. unnatural. It is quite unintuitive that
  2897. ``` markdown
  2898. - foo
  2899. bar
  2900. - baz
  2901. ```
  2902. should be parsed as two lists with an intervening paragraph,
  2903. ``` html
  2904. <ul>
  2905. <li>foo</li>
  2906. </ul>
  2907. <p>bar</p>
  2908. <ul>
  2909. <li>baz</li>
  2910. </ul>
  2911. ```
  2912. as the four-space rule demands, rather than a single list,
  2913. ``` html
  2914. <ul>
  2915. <li>
  2916. <p>foo</p>
  2917. <p>bar</p>
  2918. <ul>
  2919. <li>baz</li>
  2920. </ul>
  2921. </li>
  2922. </ul>
  2923. ```
  2924. The choice of four spaces is arbitrary. It can be learned, but it is
  2925. not likely to be guessed, and it trips up beginners regularly.
  2926. Would it help to adopt a two-space rule? The problem is that such
  2927. a rule, together with the rule allowing 1--3 spaces indentation of the
  2928. initial list marker, allows text that is indented *less than* the
  2929. original list marker to be included in the list item. For example,
  2930. `Markdown.pl` parses
  2931. ``` markdown
  2932. - one
  2933. two
  2934. ```
  2935. as a single list item, with `two` a continuation paragraph:
  2936. ``` html
  2937. <ul>
  2938. <li>
  2939. <p>one</p>
  2940. <p>two</p>
  2941. </li>
  2942. </ul>
  2943. ```
  2944. and similarly
  2945. ``` markdown
  2946. > - one
  2947. >
  2948. > two
  2949. ```
  2950. as
  2951. ``` html
  2952. <blockquote>
  2953. <ul>
  2954. <li>
  2955. <p>one</p>
  2956. <p>two</p>
  2957. </li>
  2958. </ul>
  2959. </blockquote>
  2960. ```
  2961. This is extremely unintuitive.
  2962. Rather than requiring a fixed indent from the margin, we could require
  2963. a fixed indent (say, two spaces, or even one space) from the list marker (which
  2964. may itself be indented). This proposal would remove the last anomaly
  2965. discussed. Unlike the spec presented above, it would count the following
  2966. as a list item with a subparagraph, even though the paragraph `bar`
  2967. is not indented as far as the first paragraph `foo`:
  2968. ``` markdown
  2969. 10. foo
  2970. bar
  2971. ```
  2972. Arguably this text does read like a list item with `bar` as a subparagraph,
  2973. which may count in favor of the proposal. However, on this proposal indented
  2974. code would have to be indented six spaces after the list marker. And this
  2975. would break a lot of existing Markdown, which has the pattern:
  2976. ``` markdown
  2977. 1. foo
  2978. indented code
  2979. ```
  2980. where the code is indented eight spaces. The spec above, by contrast, will
  2981. parse this text as expected, since the code block's indentation is measured
  2982. from the beginning of `foo`.
  2983. The one case that needs special treatment is a list item that *starts*
  2984. with indented code. How much indentation is required in that case, since
  2985. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  2986. that in such cases, we require one space indentation from the list marker
  2987. (and then the normal four spaces for the indented code). This will match the
  2988. four-space rule in cases where the list marker plus its initial indentation
  2989. takes four spaces (a common case), but diverge in other cases.
  2990. ## Lists
  2991. A [list](@list) is a sequence of one or more
  2992. list items [of the same type]. The list items
  2993. may be separated by single [blank lines], but two
  2994. blank lines end all containing lists.
  2995. Two list items are [of the same type](@of-the-same-type)
  2996. if they begin with a [list marker] of the same type.
  2997. Two list markers are of the
  2998. same type if (a) they are bullet list markers using the same character
  2999. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3000. delimiter (either `.` or `)`).
  3001. A list is an [ordered list](@ordered-list)
  3002. if its constituent list items begin with
  3003. [ordered list marker]s, and a
  3004. [bullet list](@bullet-list) if its constituent list
  3005. items begin with [bullet list marker]s.
  3006. The [start number](@start-number)
  3007. of an [ordered list] is determined by the list number of
  3008. its initial list item. The numbers of subsequent list items are
  3009. disregarded.
  3010. A list is [loose](@loose) if any of its constituent
  3011. list items are separated by blank lines, or if any of its constituent
  3012. list items directly contain two block-level elements with a blank line
  3013. between them. Otherwise a list is [tight](@tight).
  3014. (The difference in HTML output is that paragraphs in a loose list are
  3015. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3016. Changing the bullet or ordered list delimiter starts a new list:
  3017. .
  3018. - foo
  3019. - bar
  3020. + baz
  3021. .
  3022. <ul>
  3023. <li>foo</li>
  3024. <li>bar</li>
  3025. </ul>
  3026. <ul>
  3027. <li>baz</li>
  3028. </ul>
  3029. .
  3030. .
  3031. 1. foo
  3032. 2. bar
  3033. 3) baz
  3034. .
  3035. <ol>
  3036. <li>foo</li>
  3037. <li>bar</li>
  3038. </ol>
  3039. <ol start="3">
  3040. <li>baz</li>
  3041. </ol>
  3042. .
  3043. In CommonMark, a list can interrupt a paragraph. That is,
  3044. no blank line is needed to separate a paragraph from a following
  3045. list:
  3046. .
  3047. Foo
  3048. - bar
  3049. - baz
  3050. .
  3051. <p>Foo</p>
  3052. <ul>
  3053. <li>bar</li>
  3054. <li>baz</li>
  3055. </ul>
  3056. .
  3057. `Markdown.pl` does not allow this, through fear of triggering a list
  3058. via a numeral in a hard-wrapped line:
  3059. .
  3060. The number of windows in my house is
  3061. 14. The number of doors is 6.
  3062. .
  3063. <p>The number of windows in my house is</p>
  3064. <ol start="14">
  3065. <li>The number of doors is 6.</li>
  3066. </ol>
  3067. .
  3068. Oddly, `Markdown.pl` *does* allow a blockquote to interrupt a paragraph,
  3069. even though the same considerations might apply. We think that the two
  3070. cases should be treated the same. Here are two reasons for allowing
  3071. lists to interrupt paragraphs:
  3072. First, it is natural and not uncommon for people to start lists without
  3073. blank lines:
  3074. I need to buy
  3075. - new shoes
  3076. - a coat
  3077. - a plane ticket
  3078. Second, we are attracted to a
  3079. > [principle of uniformity](@principle-of-uniformity):
  3080. > if a chunk of text has a certain
  3081. > meaning, it will continue to have the same meaning when put into a
  3082. > container block (such as a list item or blockquote).
  3083. (Indeed, the spec for [list items] and [block quotes] presupposes
  3084. this principle.) This principle implies that if
  3085. * I need to buy
  3086. - new shoes
  3087. - a coat
  3088. - a plane ticket
  3089. is a list item containing a paragraph followed by a nested sublist,
  3090. as all Markdown implementations agree it is (though the paragraph
  3091. may be rendered without `<p>` tags, since the list is "tight"),
  3092. then
  3093. I need to buy
  3094. - new shoes
  3095. - a coat
  3096. - a plane ticket
  3097. by itself should be a paragraph followed by a nested sublist.
  3098. Our adherence to the [principle of uniformity]
  3099. thus inclines us to think that there are two coherent packages:
  3100. 1. Require blank lines before *all* lists and blockquotes,
  3101. including lists that occur as sublists inside other list items.
  3102. 2. Require blank lines in none of these places.
  3103. [reStructuredText](http://docutils.sourceforge.net/rst.html) takes
  3104. the first approach, for which there is much to be said. But the second
  3105. seems more consistent with established practice with Markdown.
  3106. There can be blank lines between items, but two blank lines end
  3107. a list:
  3108. .
  3109. - foo
  3110. - bar
  3111. - baz
  3112. .
  3113. <ul>
  3114. <li>
  3115. <p>foo</p>
  3116. </li>
  3117. <li>
  3118. <p>bar</p>
  3119. </li>
  3120. </ul>
  3121. <ul>
  3122. <li>baz</li>
  3123. </ul>
  3124. .
  3125. As illustrated above in the section on [list items],
  3126. two blank lines between blocks *within* a list item will also end a
  3127. list:
  3128. .
  3129. - foo
  3130. bar
  3131. - baz
  3132. .
  3133. <ul>
  3134. <li>foo</li>
  3135. </ul>
  3136. <p>bar</p>
  3137. <ul>
  3138. <li>baz</li>
  3139. </ul>
  3140. .
  3141. Indeed, two blank lines will end *all* containing lists:
  3142. .
  3143. - foo
  3144. - bar
  3145. - baz
  3146. bim
  3147. .
  3148. <ul>
  3149. <li>foo
  3150. <ul>
  3151. <li>bar
  3152. <ul>
  3153. <li>baz</li>
  3154. </ul>
  3155. </li>
  3156. </ul>
  3157. </li>
  3158. </ul>
  3159. <pre><code> bim
  3160. </code></pre>
  3161. .
  3162. Thus, two blank lines can be used to separate consecutive lists of
  3163. the same type, or to separate a list from an indented code block
  3164. that would otherwise be parsed as a subparagraph of the final list
  3165. item:
  3166. .
  3167. - foo
  3168. - bar
  3169. - baz
  3170. - bim
  3171. .
  3172. <ul>
  3173. <li>foo</li>
  3174. <li>bar</li>
  3175. </ul>
  3176. <ul>
  3177. <li>baz</li>
  3178. <li>bim</li>
  3179. </ul>
  3180. .
  3181. .
  3182. - foo
  3183. notcode
  3184. - foo
  3185. code
  3186. .
  3187. <ul>
  3188. <li>
  3189. <p>foo</p>
  3190. <p>notcode</p>
  3191. </li>
  3192. <li>
  3193. <p>foo</p>
  3194. </li>
  3195. </ul>
  3196. <pre><code>code
  3197. </code></pre>
  3198. .
  3199. List items need not be indented to the same level. The following
  3200. list items will be treated as items at the same list level,
  3201. since none is indented enough to belong to the previous list
  3202. item:
  3203. .
  3204. - a
  3205. - b
  3206. - c
  3207. - d
  3208. - e
  3209. - f
  3210. - g
  3211. - h
  3212. - i
  3213. .
  3214. <ul>
  3215. <li>a</li>
  3216. <li>b</li>
  3217. <li>c</li>
  3218. <li>d</li>
  3219. <li>e</li>
  3220. <li>f</li>
  3221. <li>g</li>
  3222. <li>h</li>
  3223. <li>i</li>
  3224. </ul>
  3225. .
  3226. .
  3227. 1. a
  3228. 2. b
  3229. 3. c
  3230. .
  3231. <ol>
  3232. <li>
  3233. <p>a</p>
  3234. </li>
  3235. <li>
  3236. <p>b</p>
  3237. </li>
  3238. <li>
  3239. <p>c</p>
  3240. </li>
  3241. </ol>
  3242. .
  3243. This is a loose list, because there is a blank line between
  3244. two of the list items:
  3245. .
  3246. - a
  3247. - b
  3248. - c
  3249. .
  3250. <ul>
  3251. <li>
  3252. <p>a</p>
  3253. </li>
  3254. <li>
  3255. <p>b</p>
  3256. </li>
  3257. <li>
  3258. <p>c</p>
  3259. </li>
  3260. </ul>
  3261. .
  3262. So is this, with a empty second item:
  3263. .
  3264. * a
  3265. *
  3266. * c
  3267. .
  3268. <ul>
  3269. <li>
  3270. <p>a</p>
  3271. </li>
  3272. <li></li>
  3273. <li>
  3274. <p>c</p>
  3275. </li>
  3276. </ul>
  3277. .
  3278. These are loose lists, even though there is no space between the items,
  3279. because one of the items directly contains two block-level elements
  3280. with a blank line between them:
  3281. .
  3282. - a
  3283. - b
  3284. c
  3285. - d
  3286. .
  3287. <ul>
  3288. <li>
  3289. <p>a</p>
  3290. </li>
  3291. <li>
  3292. <p>b</p>
  3293. <p>c</p>
  3294. </li>
  3295. <li>
  3296. <p>d</p>
  3297. </li>
  3298. </ul>
  3299. .
  3300. .
  3301. - a
  3302. - b
  3303. [ref]: /url
  3304. - d
  3305. .
  3306. <ul>
  3307. <li>
  3308. <p>a</p>
  3309. </li>
  3310. <li>
  3311. <p>b</p>
  3312. </li>
  3313. <li>
  3314. <p>d</p>
  3315. </li>
  3316. </ul>
  3317. .
  3318. This is a tight list, because the blank lines are in a code block:
  3319. .
  3320. - a
  3321. - ```
  3322. b
  3323. ```
  3324. - c
  3325. .
  3326. <ul>
  3327. <li>a</li>
  3328. <li>
  3329. <pre><code>b
  3330. </code></pre>
  3331. </li>
  3332. <li>c</li>
  3333. </ul>
  3334. .
  3335. This is a tight list, because the blank line is between two
  3336. paragraphs of a sublist. So the sublist is loose while
  3337. the outer list is tight:
  3338. .
  3339. - a
  3340. - b
  3341. c
  3342. - d
  3343. .
  3344. <ul>
  3345. <li>a
  3346. <ul>
  3347. <li>
  3348. <p>b</p>
  3349. <p>c</p>
  3350. </li>
  3351. </ul>
  3352. </li>
  3353. <li>d</li>
  3354. </ul>
  3355. .
  3356. This is a tight list, because the blank line is inside the
  3357. block quote:
  3358. .
  3359. * a
  3360. > b
  3361. >
  3362. * c
  3363. .
  3364. <ul>
  3365. <li>a
  3366. <blockquote>
  3367. <p>b</p>
  3368. </blockquote>
  3369. </li>
  3370. <li>c</li>
  3371. </ul>
  3372. .
  3373. This list is tight, because the consecutive block elements
  3374. are not separated by blank lines:
  3375. .
  3376. - a
  3377. > b
  3378. ```
  3379. c
  3380. ```
  3381. - d
  3382. .
  3383. <ul>
  3384. <li>a
  3385. <blockquote>
  3386. <p>b</p>
  3387. </blockquote>
  3388. <pre><code>c
  3389. </code></pre>
  3390. </li>
  3391. <li>d</li>
  3392. </ul>
  3393. .
  3394. A single-paragraph list is tight:
  3395. .
  3396. - a
  3397. .
  3398. <ul>
  3399. <li>a</li>
  3400. </ul>
  3401. .
  3402. .
  3403. - a
  3404. - b
  3405. .
  3406. <ul>
  3407. <li>a
  3408. <ul>
  3409. <li>b</li>
  3410. </ul>
  3411. </li>
  3412. </ul>
  3413. .
  3414. This list is loose, because of the blank line between the
  3415. two block elements in the list item:
  3416. .
  3417. 1. ```
  3418. foo
  3419. ```
  3420. bar
  3421. .
  3422. <ol>
  3423. <li>
  3424. <pre><code>foo
  3425. </code></pre>
  3426. <p>bar</p>
  3427. </li>
  3428. </ol>
  3429. .
  3430. Here the outer list is loose, the inner list tight:
  3431. .
  3432. * foo
  3433. * bar
  3434. baz
  3435. .
  3436. <ul>
  3437. <li>
  3438. <p>foo</p>
  3439. <ul>
  3440. <li>bar</li>
  3441. </ul>
  3442. <p>baz</p>
  3443. </li>
  3444. </ul>
  3445. .
  3446. .
  3447. - a
  3448. - b
  3449. - c
  3450. - d
  3451. - e
  3452. - f
  3453. .
  3454. <ul>
  3455. <li>
  3456. <p>a</p>
  3457. <ul>
  3458. <li>b</li>
  3459. <li>c</li>
  3460. </ul>
  3461. </li>
  3462. <li>
  3463. <p>d</p>
  3464. <ul>
  3465. <li>e</li>
  3466. <li>f</li>
  3467. </ul>
  3468. </li>
  3469. </ul>
  3470. .
  3471. # Inlines
  3472. Inlines are parsed sequentially from the beginning of the character
  3473. stream to the end (left to right, in left-to-right languages).
  3474. Thus, for example, in
  3475. .
  3476. `hi`lo`
  3477. .
  3478. <p><code>hi</code>lo`</p>
  3479. .
  3480. `hi` is parsed as code, leaving the backtick at the end as a literal
  3481. backtick.
  3482. ## Backslash escapes
  3483. Any ASCII punctuation character may be backslash-escaped:
  3484. .
  3485. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  3486. .
  3487. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  3488. .
  3489. Backslashes before other characters are treated as literal
  3490. backslashes:
  3491. .
  3492. \→\A\a\ \3\φ\«
  3493. .
  3494. <p>\→\A\a\ \3\φ\«</p>
  3495. .
  3496. Escaped characters are treated as regular characters and do
  3497. not have their usual Markdown meanings:
  3498. .
  3499. \*not emphasized*
  3500. \<br/> not a tag
  3501. \[not a link](/foo)
  3502. \`not code`
  3503. 1\. not a list
  3504. \* not a list
  3505. \# not a header
  3506. \[foo]: /url "not a reference"
  3507. .
  3508. <p>*not emphasized*
  3509. &lt;br/&gt; not a tag
  3510. [not a link](/foo)
  3511. `not code`
  3512. 1. not a list
  3513. * not a list
  3514. # not a header
  3515. [foo]: /url &quot;not a reference&quot;</p>
  3516. .
  3517. If a backslash is itself escaped, the following character is not:
  3518. .
  3519. \\*emphasis*
  3520. .
  3521. <p>\<em>emphasis</em></p>
  3522. .
  3523. A backslash at the end of the line is a [hard line break]:
  3524. .
  3525. foo\
  3526. bar
  3527. .
  3528. <p>foo<br />
  3529. bar</p>
  3530. .
  3531. Backslash escapes do not work in code blocks, code spans, autolinks, or
  3532. raw HTML:
  3533. .
  3534. `` \[\` ``
  3535. .
  3536. <p><code>\[\`</code></p>
  3537. .
  3538. .
  3539. \[\]
  3540. .
  3541. <pre><code>\[\]
  3542. </code></pre>
  3543. .
  3544. .
  3545. ~~~
  3546. \[\]
  3547. ~~~
  3548. .
  3549. <pre><code>\[\]
  3550. </code></pre>
  3551. .
  3552. .
  3553. <http://example.com?find=\*>
  3554. .
  3555. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  3556. .
  3557. .
  3558. <a href="/bar\/)">
  3559. .
  3560. <p><a href="/bar\/)"></p>
  3561. .
  3562. But they work in all other contexts, including URLs and link titles,
  3563. link references, and [info string]s in [fenced code block]s:
  3564. .
  3565. [foo](/bar\* "ti\*tle")
  3566. .
  3567. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3568. .
  3569. .
  3570. [foo]
  3571. [foo]: /bar\* "ti\*tle"
  3572. .
  3573. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3574. .
  3575. .
  3576. ``` foo\+bar
  3577. foo
  3578. ```
  3579. .
  3580. <pre><code class="language-foo+bar">foo
  3581. </code></pre>
  3582. .
  3583. ## Entities
  3584. With the goal of making this standard as HTML-agnostic as possible, all
  3585. valid HTML entities (except in code blocks and code spans)
  3586. are recognized as such and converted into unicode characters before
  3587. they are stored in the AST. This means that renderers to formats other
  3588. than HTML need not be HTML-entity aware. HTML renderers may either escape
  3589. unicode characters as entities or leave them as they are. (However,
  3590. `"`, `&`, `<`, and `>` must always be rendered as entities.)
  3591. [Named entities](@name-entities) consist of `&` + any
  3592. of the valid HTML5 entity names + `;`. The
  3593. [following document](https://html.spec.whatwg.org/multipage/entities.json)
  3594. is used as an authoritative source of the valid entity names and their
  3595. corresponding codepoints.
  3596. .
  3597. &nbsp; &amp; &copy; &AElig; &Dcaron;
  3598. &frac34; &HilbertSpace; &DifferentialD;
  3599. &ClockwiseContourIntegral; &ngE;
  3600. .
  3601. <p>  &amp; © Æ Ď
  3602. ¾ ℋ ⅆ
  3603. ∲ ≧̸</p>
  3604. .
  3605. [Decimal entities](@decimal-entities)
  3606. consist of `&#` + a string of 1--8 arabic digits + `;`. Again, these
  3607. entities need to be recognised and transformed into their corresponding
  3608. unicode codepoints. Invalid unicode codepoints will be replaced by
  3609. the "unknown codepoint" character (`U+FFFD`). For security reasons,
  3610. the codepoint `U+0000` will also be replaced by `U+FFFD`.
  3611. .
  3612. &#35; &#1234; &#992; &#98765432; &#0;
  3613. .
  3614. <p># Ӓ Ϡ � �</p>
  3615. .
  3616. [Hexadecimal entities](@hexadecimal-entities)
  3617. consist of `&#` + either `X` or `x` + a string of 1-8 hexadecimal
  3618. digits + `;`. They will also be parsed and turned into the corresponding
  3619. unicode codepoints in the AST.
  3620. .
  3621. &#X22; &#XD06; &#xcab;
  3622. .
  3623. <p>&quot; ആ ಫ</p>
  3624. .
  3625. Here are some nonentities:
  3626. .
  3627. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3628. .
  3629. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3630. .
  3631. Although HTML5 does accept some entities without a trailing semicolon
  3632. (such as `&copy`), these are not recognized as entities here, because it
  3633. makes the grammar too ambiguous:
  3634. .
  3635. &copy
  3636. .
  3637. <p>&amp;copy</p>
  3638. .
  3639. Strings that are not on the list of HTML5 named entities are not
  3640. recognized as entities either:
  3641. .
  3642. &MadeUpEntity;
  3643. .
  3644. <p>&amp;MadeUpEntity;</p>
  3645. .
  3646. Entities are recognized in any context besides code spans or
  3647. code blocks, including raw HTML, URLs, [link title]s, and
  3648. [fenced code block] [info string]s:
  3649. .
  3650. <a href="&ouml;&ouml;.html">
  3651. .
  3652. <p><a href="&ouml;&ouml;.html"></p>
  3653. .
  3654. .
  3655. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3656. .
  3657. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3658. .
  3659. .
  3660. [foo]
  3661. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3662. .
  3663. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3664. .
  3665. .
  3666. ``` f&ouml;&ouml;
  3667. foo
  3668. ```
  3669. .
  3670. <pre><code class="language-föö">foo
  3671. </code></pre>
  3672. .
  3673. Entities are treated as literal text in code spans and code blocks:
  3674. .
  3675. `f&ouml;&ouml;`
  3676. .
  3677. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3678. .
  3679. .
  3680. f&ouml;f&ouml;
  3681. .
  3682. <pre><code>f&amp;ouml;f&amp;ouml;
  3683. </code></pre>
  3684. .
  3685. ## Code spans
  3686. A [backtick string](@backtick-string)
  3687. is a string of one or more backtick characters (`` ` ``) that is neither
  3688. preceded nor followed by a backtick.
  3689. A [code span](@code-span) begins with a backtick string and ends with
  3690. a backtick string of equal length. The contents of the code span are
  3691. the characters between the two backtick strings, with leading and
  3692. trailing spaces and [line ending]s removed, and
  3693. [whitespace] collapsed to single spaces.
  3694. This is a simple code span:
  3695. .
  3696. `foo`
  3697. .
  3698. <p><code>foo</code></p>
  3699. .
  3700. Here two backticks are used, because the code contains a backtick.
  3701. This example also illustrates stripping of leading and trailing spaces:
  3702. .
  3703. `` foo ` bar ``
  3704. .
  3705. <p><code>foo ` bar</code></p>
  3706. .
  3707. This example shows the motivation for stripping leading and trailing
  3708. spaces:
  3709. .
  3710. ` `` `
  3711. .
  3712. <p><code>``</code></p>
  3713. .
  3714. [Line ending]s are treated like spaces:
  3715. .
  3716. ``
  3717. foo
  3718. ``
  3719. .
  3720. <p><code>foo</code></p>
  3721. .
  3722. Interior spaces and [line ending]s are collapsed into
  3723. single spaces, just as they would be by a browser:
  3724. .
  3725. `foo bar
  3726. baz`
  3727. .
  3728. <p><code>foo bar baz</code></p>
  3729. .
  3730. Q: Why not just leave the spaces, since browsers will collapse them
  3731. anyway? A: Because we might be targeting a non-HTML format, and we
  3732. shouldn't rely on HTML-specific rendering assumptions.
  3733. (Existing implementations differ in their treatment of internal
  3734. spaces and [line ending]s. Some, including `Markdown.pl` and
  3735. `showdown`, convert an internal [line ending] into a
  3736. `<br />` tag. But this makes things difficult for those who like to
  3737. hard-wrap their paragraphs, since a line break in the midst of a code
  3738. span will cause an unintended line break in the output. Others just
  3739. leave internal spaces as they are, which is fine if only HTML is being
  3740. targeted.)
  3741. .
  3742. `foo `` bar`
  3743. .
  3744. <p><code>foo `` bar</code></p>
  3745. .
  3746. Note that backslash escapes do not work in code spans. All backslashes
  3747. are treated literally:
  3748. .
  3749. `foo\`bar`
  3750. .
  3751. <p><code>foo\</code>bar`</p>
  3752. .
  3753. Backslash escapes are never needed, because one can always choose a
  3754. string of *n* backtick characters as delimiters, where the code does
  3755. not contain any strings of exactly *n* backtick characters.
  3756. Code span backticks have higher precedence than any other inline
  3757. constructs except HTML tags and autolinks. Thus, for example, this is
  3758. not parsed as emphasized text, since the second `*` is part of a code
  3759. span:
  3760. .
  3761. *foo`*`
  3762. .
  3763. <p>*foo<code>*</code></p>
  3764. .
  3765. And this is not parsed as a link:
  3766. .
  3767. [not a `link](/foo`)
  3768. .
  3769. <p>[not a <code>link](/foo</code>)</p>
  3770. .
  3771. Code spans, HTML tags, and autolinks have the same precedence.
  3772. Thus, this is code:
  3773. .
  3774. `<a href="`">`
  3775. .
  3776. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  3777. .
  3778. But this is an HTML tag:
  3779. .
  3780. <a href="`">`
  3781. .
  3782. <p><a href="`">`</p>
  3783. .
  3784. And this is code:
  3785. .
  3786. `<http://foo.bar.`baz>`
  3787. .
  3788. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  3789. .
  3790. But this is an autolink:
  3791. .
  3792. <http://foo.bar.`baz>`
  3793. .
  3794. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3795. .
  3796. When a backtick string is not closed by a matching backtick string,
  3797. we just have literal backticks:
  3798. .
  3799. ```foo``
  3800. .
  3801. <p>```foo``</p>
  3802. .
  3803. .
  3804. `foo
  3805. .
  3806. <p>`foo</p>
  3807. .
  3808. ## Emphasis and strong emphasis
  3809. John Gruber's original [Markdown syntax
  3810. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3811. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3812. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3813. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3814. > tag.
  3815. This is enough for most users, but these rules leave much undecided,
  3816. especially when it comes to nested emphasis. The original
  3817. `Markdown.pl` test suite makes it clear that triple `***` and
  3818. `___` delimiters can be used for strong emphasis, and most
  3819. implementations have also allowed the following patterns:
  3820. ``` markdown
  3821. ***strong emph***
  3822. ***strong** in emph*
  3823. ***emph* in strong**
  3824. **in strong *emph***
  3825. *in emph **strong***
  3826. ```
  3827. The following patterns are less widely supported, but the intent
  3828. is clear and they are useful (especially in contexts like bibliography
  3829. entries):
  3830. ``` markdown
  3831. *emph *with emph* in it*
  3832. **strong **with strong** in it**
  3833. ```
  3834. Many implementations have also restricted intraword emphasis to
  3835. the `*` forms, to avoid unwanted emphasis in words containing
  3836. internal underscores. (It is best practice to put these in code
  3837. spans, but users often do not.)
  3838. ``` markdown
  3839. internal emphasis: foo*bar*baz
  3840. no emphasis: foo_bar_baz
  3841. ```
  3842. The rules given below capture all of these patterns, while allowing
  3843. for efficient parsing strategies that do not backtrack.
  3844. First, some definitions. A [delimiter run](@delimiter-run) is either
  3845. a sequence of one or more `*` characters that is not preceded or
  3846. followed by a `*` character, or a sequence of one or more `_`
  3847. characters that is not preceded or followed by a `_` character.
  3848. A [left-flanking delimiter run](@left-flanking-delimiter-run) is
  3849. a [delimiter run] that is (a) not followed by [unicode whitespace],
  3850. and (b) either not followed by a [punctuation character], or
  3851. preceded by [unicode whitespace] or a [punctuation character].
  3852. For purposes of this definition, the beginning and the end of
  3853. the line count as unicode whitespace.
  3854. A [right-flanking delimiter run](@right-flanking-delimiter-run) is
  3855. a [delimiter run] that is (a) not preceded by [unicode whitespace],
  3856. and (b) either not preceded by a [punctuation character], or
  3857. followed by [unicode whitespace] or a [punctuation character].
  3858. For purposes of this definition, the beginning and the end of
  3859. the line count as unicode whitespace.
  3860. Here are some examples of delimiter runs.
  3861. - left-flanking but not right-flanking:
  3862. ```
  3863. ***abc
  3864. _abc
  3865. **"abc"
  3866. _"abc"
  3867. ```
  3868. - right-flanking but not left-flanking:
  3869. ```
  3870. abc***
  3871. abc_
  3872. "abc"**
  3873. "abc"_
  3874. ```
  3875. - Both left and right-flanking:
  3876. ```
  3877. abc***def
  3878. "abc"_"def"
  3879. ```
  3880. - Neither left nor right-flanking:
  3881. ```
  3882. abc *** def
  3883. a _ b
  3884. ```
  3885. (The idea of distinguishing left-flanking and right-flanking
  3886. delimiter runs based on the character before and the character
  3887. after comes from Roopesh Chander's
  3888. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  3889. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  3890. run," and its rules for distinguishing left- and right-flanking runs
  3891. are a bit more complex than the ones given here.)
  3892. The following rules define emphasis and strong emphasis:
  3893. 1. A single `*` character [can open emphasis](@can-open-emphasis)
  3894. iff (if and only if) it is part of a [left-flanking delimiter run].
  3895. 2. A single `_` character [can open emphasis] iff
  3896. it is part of a [left-flanking delimiter run]
  3897. and either (a) not part of a [right-flanking delimiter run]
  3898. or (b) part of a [right-flanking delimeter run]
  3899. preceded by punctuation.
  3900. 3. A single `*` character [can close emphasis](@can-close-emphasis)
  3901. iff it is part of a [right-flanking delimiter run].
  3902. 4. A single `_` character [can close emphasis] iff
  3903. it is part of a [right-flanking delimiter run]
  3904. and either (a) not part of a [left-flanking delimiter run]
  3905. or (b) part of a [left-flanking delimeter run]
  3906. followed by punctuation.
  3907. 5. A double `**` [can open strong emphasis](@can-open-strong-emphasis)
  3908. iff it is part of a [left-flanking delimiter run].
  3909. 6. A double `__` [can open strong emphasis] iff
  3910. it is part of a [left-flanking delimiter run]
  3911. and either (a) not part of a [right-flanking delimiter run]
  3912. or (b) part of a [right-flanking delimeter run]
  3913. preceded by punctuation.
  3914. 7. A double `**` [can close strong emphasis](@can-close-strong-emphasis)
  3915. iff it is part of a [right-flanking delimiter run].
  3916. 8. A double `__` [can close strong emphasis]
  3917. it is part of a [right-flanking delimiter run]
  3918. and either (a) not part of a [left-flanking delimiter run]
  3919. or (b) part of a [left-flanking delimeter run]
  3920. followed by punctuation.
  3921. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  3922. with a delimiter that [can close emphasis], and that uses the same
  3923. character (`_` or `*`) as the opening delimiter. There must
  3924. be a nonempty sequence of inlines between the open delimiter
  3925. and the closing delimiter; these form the contents of the emphasis
  3926. inline.
  3927. 10. Strong emphasis begins with a delimiter that
  3928. [can open strong emphasis] and ends with a delimiter that
  3929. [can close strong emphasis], and that uses the same character
  3930. (`_` or `*`) as the opening delimiter.
  3931. There must be a nonempty sequence of inlines between the open
  3932. delimiter and the closing delimiter; these form the contents of
  3933. the strong emphasis inline.
  3934. 11. A literal `*` character cannot occur at the beginning or end of
  3935. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  3936. is backslash-escaped.
  3937. 12. A literal `_` character cannot occur at the beginning or end of
  3938. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3939. is backslash-escaped.
  3940. Where rules 1--12 above are compatible with multiple parsings,
  3941. the following principles resolve ambiguity:
  3942. 13. The number of nestings should be minimized. Thus, for example,
  3943. an interpretation `<strong>...</strong>` is always preferred to
  3944. `<em><em>...</em></em>`.
  3945. 14. An interpretation `<strong><em>...</em></strong>` is always
  3946. preferred to `<em><strong>..</strong></em>`.
  3947. 15. When two potential emphasis or strong emphasis spans overlap,
  3948. so that the second begins before the first ends and ends after
  3949. the first ends, the first takes precedence. Thus, for example,
  3950. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3951. than `*foo <em>bar* baz</em>`. For the same reason,
  3952. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3953. rather than `<strong>foo*bar</strong>`.
  3954. 16. When there are two potential emphasis or strong emphasis spans
  3955. with the same closing delimiter, the shorter one (the one that
  3956. opens later) takes precedence. Thus, for example,
  3957. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3958. rather than `<strong>foo **bar baz</strong>`.
  3959. 17. Inline code spans, links, images, and HTML tags group more tightly
  3960. than emphasis. So, when there is a choice between an interpretation
  3961. that contains one of these elements and one that does not, the
  3962. former always wins. Thus, for example, `*[foo*](bar)` is
  3963. parsed as `*<a href="bar">foo*</a>` rather than as
  3964. `<em>[foo</em>](bar)`.
  3965. These rules can be illustrated through a series of examples.
  3966. Rule 1:
  3967. .
  3968. *foo bar*
  3969. .
  3970. <p><em>foo bar</em></p>
  3971. .
  3972. This is not emphasis, because the opening `*` is followed by
  3973. whitespace, and hence not part of a [left-flanking delimiter run]:
  3974. .
  3975. a * foo bar*
  3976. .
  3977. <p>a * foo bar*</p>
  3978. .
  3979. This is not emphasis, because the opening `*` is preceded
  3980. by an alphanumeric and followed by punctuation, and hence
  3981. not part of a [left-flanking delimiter run]:
  3982. .
  3983. a*"foo"*
  3984. .
  3985. <p>a*&quot;foo&quot;*</p>
  3986. .
  3987. Unicode nonbreaking spaces count as whitespace, too:
  3988. .
  3989. * a *
  3990. .
  3991. <p>* a *</p>
  3992. .
  3993. Intraword emphasis with `*` is permitted:
  3994. .
  3995. foo*bar*
  3996. .
  3997. <p>foo<em>bar</em></p>
  3998. .
  3999. .
  4000. 5*6*78
  4001. .
  4002. <p>5<em>6</em>78</p>
  4003. .
  4004. Rule 2:
  4005. .
  4006. _foo bar_
  4007. .
  4008. <p><em>foo bar</em></p>
  4009. .
  4010. This is not emphasis, because the opening `_` is followed by
  4011. whitespace:
  4012. .
  4013. _ foo bar_
  4014. .
  4015. <p>_ foo bar_</p>
  4016. .
  4017. This is not emphasis, because the opening `_` is preceded
  4018. by an alphanumeric and followed by punctuation:
  4019. .
  4020. a_"foo"_
  4021. .
  4022. <p>a_&quot;foo&quot;_</p>
  4023. .
  4024. Emphasis with `_` is not allowed inside words:
  4025. .
  4026. foo_bar_
  4027. .
  4028. <p>foo_bar_</p>
  4029. .
  4030. .
  4031. 5_6_78
  4032. .
  4033. <p>5_6_78</p>
  4034. .
  4035. .
  4036. пристаням_стремятся_
  4037. .
  4038. <p>пристаням_стремятся_</p>
  4039. .
  4040. Here `_` does not generate emphasis, because the first delimiter run
  4041. is right-flanking and the second left-flanking:
  4042. .
  4043. aa_"bb"_cc
  4044. .
  4045. <p>aa_&quot;bb&quot;_cc</p>
  4046. .
  4047. This is emphasis, even though the opening delimiter is
  4048. both left- and right-flanking, because it is preceded by
  4049. punctuation:
  4050. .
  4051. foo-_(bar)_
  4052. .
  4053. <p>foo-<em>(bar)</em></p>
  4054. .
  4055. Rule 3:
  4056. This is not emphasis, because the closing delimiter does
  4057. not match the opening delimiter:
  4058. .
  4059. _foo*
  4060. .
  4061. <p>_foo*</p>
  4062. .
  4063. This is not emphasis, because the closing `*` is preceded by
  4064. whitespace:
  4065. .
  4066. *foo bar *
  4067. .
  4068. <p>*foo bar *</p>
  4069. .
  4070. A newline also counts as whitespace:
  4071. .
  4072. *foo bar
  4073. *
  4074. .
  4075. <p>*foo bar</p>
  4076. <ul>
  4077. <li></li>
  4078. </ul>
  4079. .
  4080. This is not emphasis, because the second `*` is
  4081. preceded by punctuation and followed by an alphanumeric
  4082. (hence it is not part of a [right-flanking delimiter run]:
  4083. .
  4084. *(*foo)
  4085. .
  4086. <p>*(*foo)</p>
  4087. .
  4088. The point of this restriction is more easily appreciated
  4089. with this example:
  4090. .
  4091. *(*foo*)*
  4092. .
  4093. <p><em>(<em>foo</em>)</em></p>
  4094. .
  4095. Intraword emphasis with `*` is allowed:
  4096. .
  4097. *foo*bar
  4098. .
  4099. <p><em>foo</em>bar</p>
  4100. .
  4101. Rule 4:
  4102. This is not emphasis, because the closing `_` is preceded by
  4103. whitespace:
  4104. .
  4105. _foo bar _
  4106. .
  4107. <p>_foo bar _</p>
  4108. .
  4109. This is not emphasis, because the second `_` is
  4110. preceded by punctuation and followed by an alphanumeric:
  4111. .
  4112. _(_foo)
  4113. .
  4114. <p>_(_foo)</p>
  4115. .
  4116. This is emphasis within emphasis:
  4117. .
  4118. _(_foo_)_
  4119. .
  4120. <p><em>(<em>foo</em>)</em></p>
  4121. .
  4122. Intraword emphasis is disallowed for `_`:
  4123. .
  4124. _foo_bar
  4125. .
  4126. <p>_foo_bar</p>
  4127. .
  4128. .
  4129. _пристаням_стремятся
  4130. .
  4131. <p>_пристаням_стремятся</p>
  4132. .
  4133. .
  4134. _foo_bar_baz_
  4135. .
  4136. <p><em>foo_bar_baz</em></p>
  4137. .
  4138. This is emphasis, even though the closing delimiter is
  4139. both left- and right-flanking, because it is followed by
  4140. punctuation:
  4141. .
  4142. _(bar)_.
  4143. .
  4144. <p><em>(bar)</em>.</p>
  4145. .
  4146. Rule 5:
  4147. .
  4148. **foo bar**
  4149. .
  4150. <p><strong>foo bar</strong></p>
  4151. .
  4152. This is not strong emphasis, because the opening delimiter is
  4153. followed by whitespace:
  4154. .
  4155. ** foo bar**
  4156. .
  4157. <p>** foo bar**</p>
  4158. .
  4159. This is not strong emphasis, because the opening `**` is preceded
  4160. by an alphanumeric and followed by punctuation, and hence
  4161. not part of a [left-flanking delimiter run]:
  4162. .
  4163. a**"foo"**
  4164. .
  4165. <p>a**&quot;foo&quot;**</p>
  4166. .
  4167. Intraword strong emphasis with `**` is permitted:
  4168. .
  4169. foo**bar**
  4170. .
  4171. <p>foo<strong>bar</strong></p>
  4172. .
  4173. Rule 6:
  4174. .
  4175. __foo bar__
  4176. .
  4177. <p><strong>foo bar</strong></p>
  4178. .
  4179. This is not strong emphasis, because the opening delimiter is
  4180. followed by whitespace:
  4181. .
  4182. __ foo bar__
  4183. .
  4184. <p>__ foo bar__</p>
  4185. .
  4186. A newline counts as whitespace:
  4187. .
  4188. __
  4189. foo bar__
  4190. .
  4191. <p>__
  4192. foo bar__</p>
  4193. .
  4194. This is not strong emphasis, because the opening `__` is preceded
  4195. by an alphanumeric and followed by punctuation:
  4196. .
  4197. a__"foo"__
  4198. .
  4199. <p>a__&quot;foo&quot;__</p>
  4200. .
  4201. Intraword strong emphasis is forbidden with `__`:
  4202. .
  4203. foo__bar__
  4204. .
  4205. <p>foo__bar__</p>
  4206. .
  4207. .
  4208. 5__6__78
  4209. .
  4210. <p>5__6__78</p>
  4211. .
  4212. .
  4213. пристаням__стремятся__
  4214. .
  4215. <p>пристаням__стремятся__</p>
  4216. .
  4217. .
  4218. __foo, __bar__, baz__
  4219. .
  4220. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4221. .
  4222. This is strong emphasis, even though the opening delimiter is
  4223. both left- and right-flanking, because it is preceded by
  4224. punctuation:
  4225. .
  4226. foo-__(bar)__
  4227. .
  4228. <p>foo-<strong>(bar)</strong></p>
  4229. .
  4230. Rule 7:
  4231. This is not strong emphasis, because the closing delimiter is preceded
  4232. by whitespace:
  4233. .
  4234. **foo bar **
  4235. .
  4236. <p>**foo bar **</p>
  4237. .
  4238. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4239. Rule 11.)
  4240. This is not strong emphasis, because the second `**` is
  4241. preceded by punctuation and followed by an alphanumeric:
  4242. .
  4243. **(**foo)
  4244. .
  4245. <p>**(**foo)</p>
  4246. .
  4247. The point of this restriction is more easily appreciated
  4248. with these examples:
  4249. .
  4250. *(**foo**)*
  4251. .
  4252. <p><em>(<strong>foo</strong>)</em></p>
  4253. .
  4254. .
  4255. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4256. *Asclepias physocarpa*)**
  4257. .
  4258. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4259. <em>Asclepias physocarpa</em>)</strong></p>
  4260. .
  4261. .
  4262. **foo "*bar*" foo**
  4263. .
  4264. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4265. .
  4266. Intraword emphasis:
  4267. .
  4268. **foo**bar
  4269. .
  4270. <p><strong>foo</strong>bar</p>
  4271. .
  4272. Rule 8:
  4273. This is not strong emphasis, because the closing delimiter is
  4274. preceded by whitespace:
  4275. .
  4276. __foo bar __
  4277. .
  4278. <p>__foo bar __</p>
  4279. .
  4280. This is not strong emphasis, because the second `__` is
  4281. preceded by punctuation and followed by an alphanumeric:
  4282. .
  4283. __(__foo)
  4284. .
  4285. <p>__(__foo)</p>
  4286. .
  4287. The point of this restriction is more easily appreciated
  4288. with this example:
  4289. .
  4290. _(__foo__)_
  4291. .
  4292. <p><em>(<strong>foo</strong>)</em></p>
  4293. .
  4294. Intraword strong emphasis is forbidden with `__`:
  4295. .
  4296. __foo__bar
  4297. .
  4298. <p>__foo__bar</p>
  4299. .
  4300. .
  4301. __пристаням__стремятся
  4302. .
  4303. <p>__пристаням__стремятся</p>
  4304. .
  4305. .
  4306. __foo__bar__baz__
  4307. .
  4308. <p><strong>foo__bar__baz</strong></p>
  4309. .
  4310. This is strong emphasis, even though the closing delimiter is
  4311. both left- and right-flanking, because it is followed by
  4312. punctuation:
  4313. .
  4314. __(bar)__.
  4315. .
  4316. <p><strong>(bar)</strong>.</p>
  4317. .
  4318. Rule 9:
  4319. Any nonempty sequence of inline elements can be the contents of an
  4320. emphasized span.
  4321. .
  4322. *foo [bar](/url)*
  4323. .
  4324. <p><em>foo <a href="/url">bar</a></em></p>
  4325. .
  4326. .
  4327. *foo
  4328. bar*
  4329. .
  4330. <p><em>foo
  4331. bar</em></p>
  4332. .
  4333. In particular, emphasis and strong emphasis can be nested
  4334. inside emphasis:
  4335. .
  4336. _foo __bar__ baz_
  4337. .
  4338. <p><em>foo <strong>bar</strong> baz</em></p>
  4339. .
  4340. .
  4341. _foo _bar_ baz_
  4342. .
  4343. <p><em>foo <em>bar</em> baz</em></p>
  4344. .
  4345. .
  4346. __foo_ bar_
  4347. .
  4348. <p><em><em>foo</em> bar</em></p>
  4349. .
  4350. .
  4351. *foo *bar**
  4352. .
  4353. <p><em>foo <em>bar</em></em></p>
  4354. .
  4355. .
  4356. *foo **bar** baz*
  4357. .
  4358. <p><em>foo <strong>bar</strong> baz</em></p>
  4359. .
  4360. But note:
  4361. .
  4362. *foo**bar**baz*
  4363. .
  4364. <p><em>foo</em><em>bar</em><em>baz</em></p>
  4365. .
  4366. The difference is that in the preceding case, the internal delimiters
  4367. [can close emphasis], while in the cases with spaces, they cannot.
  4368. .
  4369. ***foo** bar*
  4370. .
  4371. <p><em><strong>foo</strong> bar</em></p>
  4372. .
  4373. .
  4374. *foo **bar***
  4375. .
  4376. <p><em>foo <strong>bar</strong></em></p>
  4377. .
  4378. Note, however, that in the following case we get no strong
  4379. emphasis, because the opening delimiter is closed by the first
  4380. `*` before `bar`:
  4381. .
  4382. *foo**bar***
  4383. .
  4384. <p><em>foo</em><em>bar</em>**</p>
  4385. .
  4386. Indefinite levels of nesting are possible:
  4387. .
  4388. *foo **bar *baz* bim** bop*
  4389. .
  4390. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  4391. .
  4392. .
  4393. *foo [*bar*](/url)*
  4394. .
  4395. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  4396. .
  4397. There can be no empty emphasis or strong emphasis:
  4398. .
  4399. ** is not an empty emphasis
  4400. .
  4401. <p>** is not an empty emphasis</p>
  4402. .
  4403. .
  4404. **** is not an empty strong emphasis
  4405. .
  4406. <p>**** is not an empty strong emphasis</p>
  4407. .
  4408. Rule 10:
  4409. Any nonempty sequence of inline elements can be the contents of an
  4410. strongly emphasized span.
  4411. .
  4412. **foo [bar](/url)**
  4413. .
  4414. <p><strong>foo <a href="/url">bar</a></strong></p>
  4415. .
  4416. .
  4417. **foo
  4418. bar**
  4419. .
  4420. <p><strong>foo
  4421. bar</strong></p>
  4422. .
  4423. In particular, emphasis and strong emphasis can be nested
  4424. inside strong emphasis:
  4425. .
  4426. __foo _bar_ baz__
  4427. .
  4428. <p><strong>foo <em>bar</em> baz</strong></p>
  4429. .
  4430. .
  4431. __foo __bar__ baz__
  4432. .
  4433. <p><strong>foo <strong>bar</strong> baz</strong></p>
  4434. .
  4435. .
  4436. ____foo__ bar__
  4437. .
  4438. <p><strong><strong>foo</strong> bar</strong></p>
  4439. .
  4440. .
  4441. **foo **bar****
  4442. .
  4443. <p><strong>foo <strong>bar</strong></strong></p>
  4444. .
  4445. .
  4446. **foo *bar* baz**
  4447. .
  4448. <p><strong>foo <em>bar</em> baz</strong></p>
  4449. .
  4450. But note:
  4451. .
  4452. **foo*bar*baz**
  4453. .
  4454. <p><em><em>foo</em>bar</em>baz**</p>
  4455. .
  4456. The difference is that in the preceding case, the internal delimiters
  4457. [can close emphasis], while in the cases with spaces, they cannot.
  4458. .
  4459. ***foo* bar**
  4460. .
  4461. <p><strong><em>foo</em> bar</strong></p>
  4462. .
  4463. .
  4464. **foo *bar***
  4465. .
  4466. <p><strong>foo <em>bar</em></strong></p>
  4467. .
  4468. Indefinite levels of nesting are possible:
  4469. .
  4470. **foo *bar **baz**
  4471. bim* bop**
  4472. .
  4473. <p><strong>foo <em>bar <strong>baz</strong>
  4474. bim</em> bop</strong></p>
  4475. .
  4476. .
  4477. **foo [*bar*](/url)**
  4478. .
  4479. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  4480. .
  4481. There can be no empty emphasis or strong emphasis:
  4482. .
  4483. __ is not an empty emphasis
  4484. .
  4485. <p>__ is not an empty emphasis</p>
  4486. .
  4487. .
  4488. ____ is not an empty strong emphasis
  4489. .
  4490. <p>____ is not an empty strong emphasis</p>
  4491. .
  4492. Rule 11:
  4493. .
  4494. foo ***
  4495. .
  4496. <p>foo ***</p>
  4497. .
  4498. .
  4499. foo *\**
  4500. .
  4501. <p>foo <em>*</em></p>
  4502. .
  4503. .
  4504. foo *_*
  4505. .
  4506. <p>foo <em>_</em></p>
  4507. .
  4508. .
  4509. foo *****
  4510. .
  4511. <p>foo *****</p>
  4512. .
  4513. .
  4514. foo **\***
  4515. .
  4516. <p>foo <strong>*</strong></p>
  4517. .
  4518. .
  4519. foo **_**
  4520. .
  4521. <p>foo <strong>_</strong></p>
  4522. .
  4523. Note that when delimiters do not match evenly, Rule 11 determines
  4524. that the excess literal `*` characters will appear outside of the
  4525. emphasis, rather than inside it:
  4526. .
  4527. **foo*
  4528. .
  4529. <p>*<em>foo</em></p>
  4530. .
  4531. .
  4532. *foo**
  4533. .
  4534. <p><em>foo</em>*</p>
  4535. .
  4536. .
  4537. ***foo**
  4538. .
  4539. <p>*<strong>foo</strong></p>
  4540. .
  4541. .
  4542. ****foo*
  4543. .
  4544. <p>***<em>foo</em></p>
  4545. .
  4546. .
  4547. **foo***
  4548. .
  4549. <p><strong>foo</strong>*</p>
  4550. .
  4551. .
  4552. *foo****
  4553. .
  4554. <p><em>foo</em>***</p>
  4555. .
  4556. Rule 12:
  4557. .
  4558. foo ___
  4559. .
  4560. <p>foo ___</p>
  4561. .
  4562. .
  4563. foo _\__
  4564. .
  4565. <p>foo <em>_</em></p>
  4566. .
  4567. .
  4568. foo _*_
  4569. .
  4570. <p>foo <em>*</em></p>
  4571. .
  4572. .
  4573. foo _____
  4574. .
  4575. <p>foo _____</p>
  4576. .
  4577. .
  4578. foo __\___
  4579. .
  4580. <p>foo <strong>_</strong></p>
  4581. .
  4582. .
  4583. foo __*__
  4584. .
  4585. <p>foo <strong>*</strong></p>
  4586. .
  4587. .
  4588. __foo_
  4589. .
  4590. <p>_<em>foo</em></p>
  4591. .
  4592. Note that when delimiters do not match evenly, Rule 12 determines
  4593. that the excess literal `_` characters will appear outside of the
  4594. emphasis, rather than inside it:
  4595. .
  4596. _foo__
  4597. .
  4598. <p><em>foo</em>_</p>
  4599. .
  4600. .
  4601. ___foo__
  4602. .
  4603. <p>_<strong>foo</strong></p>
  4604. .
  4605. .
  4606. ____foo_
  4607. .
  4608. <p>___<em>foo</em></p>
  4609. .
  4610. .
  4611. __foo___
  4612. .
  4613. <p><strong>foo</strong>_</p>
  4614. .
  4615. .
  4616. _foo____
  4617. .
  4618. <p><em>foo</em>___</p>
  4619. .
  4620. Rule 13 implies that if you want emphasis nested directly inside
  4621. emphasis, you must use different delimiters:
  4622. .
  4623. **foo**
  4624. .
  4625. <p><strong>foo</strong></p>
  4626. .
  4627. .
  4628. *_foo_*
  4629. .
  4630. <p><em><em>foo</em></em></p>
  4631. .
  4632. .
  4633. __foo__
  4634. .
  4635. <p><strong>foo</strong></p>
  4636. .
  4637. .
  4638. _*foo*_
  4639. .
  4640. <p><em><em>foo</em></em></p>
  4641. .
  4642. However, strong emphasis within strong emphasis is possible without
  4643. switching delimiters:
  4644. .
  4645. ****foo****
  4646. .
  4647. <p><strong><strong>foo</strong></strong></p>
  4648. .
  4649. .
  4650. ____foo____
  4651. .
  4652. <p><strong><strong>foo</strong></strong></p>
  4653. .
  4654. Rule 13 can be applied to arbitrarily long sequences of
  4655. delimiters:
  4656. .
  4657. ******foo******
  4658. .
  4659. <p><strong><strong><strong>foo</strong></strong></strong></p>
  4660. .
  4661. Rule 14:
  4662. .
  4663. ***foo***
  4664. .
  4665. <p><strong><em>foo</em></strong></p>
  4666. .
  4667. .
  4668. _____foo_____
  4669. .
  4670. <p><strong><strong><em>foo</em></strong></strong></p>
  4671. .
  4672. Rule 15:
  4673. .
  4674. *foo _bar* baz_
  4675. .
  4676. <p><em>foo _bar</em> baz_</p>
  4677. .
  4678. .
  4679. **foo*bar**
  4680. .
  4681. <p><em><em>foo</em>bar</em>*</p>
  4682. .
  4683. .
  4684. *foo __bar *baz bim__ bam*
  4685. .
  4686. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  4687. .
  4688. Rule 16:
  4689. .
  4690. **foo **bar baz**
  4691. .
  4692. <p>**foo <strong>bar baz</strong></p>
  4693. .
  4694. .
  4695. *foo *bar baz*
  4696. .
  4697. <p>*foo <em>bar baz</em></p>
  4698. .
  4699. Rule 17:
  4700. .
  4701. *[bar*](/url)
  4702. .
  4703. <p>*<a href="/url">bar*</a></p>
  4704. .
  4705. .
  4706. _foo [bar_](/url)
  4707. .
  4708. <p>_foo <a href="/url">bar_</a></p>
  4709. .
  4710. .
  4711. *<img src="foo" title="*"/>
  4712. .
  4713. <p>*<img src="foo" title="*"/></p>
  4714. .
  4715. .
  4716. **<a href="**">
  4717. .
  4718. <p>**<a href="**"></p>
  4719. .
  4720. .
  4721. __<a href="__">
  4722. .
  4723. <p>__<a href="__"></p>
  4724. .
  4725. .
  4726. *a `*`*
  4727. .
  4728. <p><em>a <code>*</code></em></p>
  4729. .
  4730. .
  4731. _a `_`_
  4732. .
  4733. <p><em>a <code>_</code></em></p>
  4734. .
  4735. .
  4736. **a<http://foo.bar/?q=**>
  4737. .
  4738. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  4739. .
  4740. .
  4741. __a<http://foo.bar/?q=__>
  4742. .
  4743. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  4744. .
  4745. ## Links
  4746. A link contains [link text] (the visible text), a [link destination]
  4747. (the URI that is the link destination), and optionally a [link title].
  4748. There are two basic kinds of links in Markdown. In [inline link]s the
  4749. destination and title are given immediately after the link text. In
  4750. [reference link]s the destination and title are defined elsewhere in
  4751. the document.
  4752. A [link text](@link-text) consists of a sequence of zero or more
  4753. inline elements enclosed by square brackets (`[` and `]`). The
  4754. following rules apply:
  4755. - Links may not contain other links, at any level of nesting. If
  4756. multiple otherwise valid link definitions appear nested inside each
  4757. other, the inner-most definition is used.
  4758. - Brackets are allowed in the [link text] only if (a) they
  4759. are backslash-escaped or (b) they appear as a matched pair of brackets,
  4760. with an open bracket `[`, a sequence of zero or more inlines, and
  4761. a close bracket `]`.
  4762. - Backtick [code span]s, [autolink]s, and raw [HTML tag]s bind more tightly
  4763. than the brackets in link text. Thus, for example,
  4764. `` [foo`]` `` could not be a link text, since the second `]`
  4765. is part of a code span.
  4766. - The brackets in link text bind more tightly than markers for
  4767. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  4768. A [link destination](@link-destination) consists of either
  4769. - a sequence of zero or more characters between an opening `<` and a
  4770. closing `>` that contains no line breaks or unescaped `<` or `>`
  4771. characters, or
  4772. - a nonempty sequence of characters that does not include
  4773. ASCII space or control characters, and includes parentheses
  4774. only if (a) they are backslash-escaped or (b) they are part of
  4775. a balanced pair of unescaped parentheses that is not itself
  4776. inside a balanced pair of unescaped parentheses.
  4777. A [link title](@link-title) consists of either
  4778. - a sequence of zero or more characters between straight double-quote
  4779. characters (`"`), including a `"` character only if it is
  4780. backslash-escaped, or
  4781. - a sequence of zero or more characters between straight single-quote
  4782. characters (`'`), including a `'` character only if it is
  4783. backslash-escaped, or
  4784. - a sequence of zero or more characters between matching parentheses
  4785. (`(...)`), including a `)` character only if it is backslash-escaped.
  4786. Although [link title]s may span multiple lines, they may not contain
  4787. a [blank line].
  4788. An [inline link](@inline-link) consists of a [link text] followed immediately
  4789. by a left parenthesis `(`, optional [whitespace], an optional
  4790. [link destination], an optional [link title] separated from the link
  4791. destination by [whitespace], optional [whitespace], and a right
  4792. parenthesis `)`. The link's text consists of the inlines contained
  4793. in the [link text] (excluding the enclosing square brackets).
  4794. The link's URI consists of the link destination, excluding enclosing
  4795. `<...>` if present, with backslash-escapes in effect as described
  4796. above. The link's title consists of the link title, excluding its
  4797. enclosing delimiters, with backslash-escapes in effect as described
  4798. above.
  4799. Here is a simple inline link:
  4800. .
  4801. [link](/uri "title")
  4802. .
  4803. <p><a href="/uri" title="title">link</a></p>
  4804. .
  4805. The title may be omitted:
  4806. .
  4807. [link](/uri)
  4808. .
  4809. <p><a href="/uri">link</a></p>
  4810. .
  4811. Both the title and the destination may be omitted:
  4812. .
  4813. [link]()
  4814. .
  4815. <p><a href="">link</a></p>
  4816. .
  4817. .
  4818. [link](<>)
  4819. .
  4820. <p><a href="">link</a></p>
  4821. .
  4822. If the destination contains spaces, it must be enclosed in pointy
  4823. braces:
  4824. .
  4825. [link](/my uri)
  4826. .
  4827. <p>[link](/my uri)</p>
  4828. .
  4829. .
  4830. [link](</my uri>)
  4831. .
  4832. <p><a href="/my%20uri">link</a></p>
  4833. .
  4834. The destination cannot contain line breaks, even with pointy braces:
  4835. .
  4836. [link](foo
  4837. bar)
  4838. .
  4839. <p>[link](foo
  4840. bar)</p>
  4841. .
  4842. .
  4843. [link](<foo
  4844. bar>)
  4845. .
  4846. <p>[link](<foo
  4847. bar>)</p>
  4848. .
  4849. One level of balanced parentheses is allowed without escaping:
  4850. .
  4851. [link]((foo)and(bar))
  4852. .
  4853. <p><a href="(foo)and(bar)">link</a></p>
  4854. .
  4855. However, if you have parentheses within parentheses, you need to escape
  4856. or use the `<...>` form:
  4857. .
  4858. [link](foo(and(bar)))
  4859. .
  4860. <p>[link](foo(and(bar)))</p>
  4861. .
  4862. .
  4863. [link](foo(and\(bar\)))
  4864. .
  4865. <p><a href="foo(and(bar))">link</a></p>
  4866. .
  4867. .
  4868. [link](<foo(and(bar))>)
  4869. .
  4870. <p><a href="foo(and(bar))">link</a></p>
  4871. .
  4872. Parentheses and other symbols can also be escaped, as usual
  4873. in Markdown:
  4874. .
  4875. [link](foo\)\:)
  4876. .
  4877. <p><a href="foo):">link</a></p>
  4878. .
  4879. A link can contain fragment identifiers and queries:
  4880. .
  4881. [link](#fragment)
  4882. [link](http://example.com#fragment)
  4883. [link](http://example.com?foo=bar&baz#fragment)
  4884. .
  4885. <p><a href="#fragment">link</a></p>
  4886. <p><a href="http://example.com#fragment">link</a></p>
  4887. <p><a href="http://example.com?foo=bar&baz#fragment">link</a></p>
  4888. .
  4889. Note that a backslash before a non-escapable character is
  4890. just a backslash:
  4891. .
  4892. [link](foo\bar)
  4893. .
  4894. <p><a href="foo%5Cbar">link</a></p>
  4895. .
  4896. URL-escaping should be left alone inside the destination, as all
  4897. URL-escaped characters are also valid URL characters. HTML entities in
  4898. the destination will be parsed into the corresponding unicode
  4899. codepoints, as usual, and optionally URL-escaped when written as HTML.
  4900. .
  4901. [link](foo%20b&auml;)
  4902. .
  4903. <p><a href="foo%20b%C3%A4">link</a></p>
  4904. .
  4905. Note that, because titles can often be parsed as destinations,
  4906. if you try to omit the destination and keep the title, you'll
  4907. get unexpected results:
  4908. .
  4909. [link]("title")
  4910. .
  4911. <p><a href="%22title%22">link</a></p>
  4912. .
  4913. Titles may be in single quotes, double quotes, or parentheses:
  4914. .
  4915. [link](/url "title")
  4916. [link](/url 'title')
  4917. [link](/url (title))
  4918. .
  4919. <p><a href="/url" title="title">link</a>
  4920. <a href="/url" title="title">link</a>
  4921. <a href="/url" title="title">link</a></p>
  4922. .
  4923. Backslash escapes and entities may be used in titles:
  4924. .
  4925. [link](/url "title \"&quot;")
  4926. .
  4927. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  4928. .
  4929. Nested balanced quotes are not allowed without escaping:
  4930. .
  4931. [link](/url "title "and" title")
  4932. .
  4933. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  4934. .
  4935. But it is easy to work around this by using a different quote type:
  4936. .
  4937. [link](/url 'title "and" title')
  4938. .
  4939. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  4940. .
  4941. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  4942. title, and its test suite included a test demonstrating this.
  4943. But it is hard to see a good rationale for the extra complexity this
  4944. brings, since there are already many ways---backslash escaping,
  4945. entities, or using a different quote type for the enclosing title---to
  4946. write titles containing double quotes. `Markdown.pl`'s handling of
  4947. titles has a number of other strange features. For example, it allows
  4948. single-quoted titles in inline links, but not reference links. And, in
  4949. reference links but not inline links, it allows a title to begin with
  4950. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  4951. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  4952. a simple, rational rule that works the same way in inline links and
  4953. link reference definitions.)
  4954. [Whitespace] is allowed around the destination and title:
  4955. .
  4956. [link]( /uri
  4957. "title" )
  4958. .
  4959. <p><a href="/uri" title="title">link</a></p>
  4960. .
  4961. But it is not allowed between the link text and the
  4962. following parenthesis:
  4963. .
  4964. [link] (/uri)
  4965. .
  4966. <p>[link] (/uri)</p>
  4967. .
  4968. The link text may contain balanced brackets, but not unbalanced ones,
  4969. unless they are escaped:
  4970. .
  4971. [link [foo [bar]]](/uri)
  4972. .
  4973. <p><a href="/uri">link [foo [bar]]</a></p>
  4974. .
  4975. .
  4976. [link] bar](/uri)
  4977. .
  4978. <p>[link] bar](/uri)</p>
  4979. .
  4980. .
  4981. [link [bar](/uri)
  4982. .
  4983. <p>[link <a href="/uri">bar</a></p>
  4984. .
  4985. .
  4986. [link \[bar](/uri)
  4987. .
  4988. <p><a href="/uri">link [bar</a></p>
  4989. .
  4990. The link text may contain inline content:
  4991. .
  4992. [link *foo **bar** `#`*](/uri)
  4993. .
  4994. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4995. .
  4996. .
  4997. [![moon](moon.jpg)](/uri)
  4998. .
  4999. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5000. .
  5001. However, links may not contain other links, at any level of nesting.
  5002. .
  5003. [foo [bar](/uri)](/uri)
  5004. .
  5005. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5006. .
  5007. .
  5008. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5009. .
  5010. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5011. .
  5012. .
  5013. ![[[foo](uri1)](uri2)](uri3)
  5014. .
  5015. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5016. .
  5017. These cases illustrate the precedence of link text grouping over
  5018. emphasis grouping:
  5019. .
  5020. *[foo*](/uri)
  5021. .
  5022. <p>*<a href="/uri">foo*</a></p>
  5023. .
  5024. .
  5025. [foo *bar](baz*)
  5026. .
  5027. <p><a href="baz*">foo *bar</a></p>
  5028. .
  5029. Note that brackets that *aren't* part of links do not take
  5030. precedence:
  5031. .
  5032. *foo [bar* baz]
  5033. .
  5034. <p><em>foo [bar</em> baz]</p>
  5035. .
  5036. These cases illustrate the precedence of HTML tags, code spans,
  5037. and autolinks over link grouping:
  5038. .
  5039. [foo <bar attr="](baz)">
  5040. .
  5041. <p>[foo <bar attr="](baz)"></p>
  5042. .
  5043. .
  5044. [foo`](/uri)`
  5045. .
  5046. <p>[foo<code>](/uri)</code></p>
  5047. .
  5048. .
  5049. [foo<http://example.com/?search=](uri)>
  5050. .
  5051. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5052. .
  5053. There are three kinds of [reference link](@reference-link)s:
  5054. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5055. and [shortcut](#shortcut-reference-link).
  5056. A [full reference link](@full-reference-link)
  5057. consists of a [link text], optional [whitespace], and a [link label]
  5058. that [matches] a [link reference definition] elsewhere in the document.
  5059. A [link label](@link-label) begins with a left bracket (`[`) and ends
  5060. with the first right bracket (`]`) that is not backslash-escaped.
  5061. Between these brackets there must be at least one [non-whitespace character].
  5062. Unescaped square bracket characters are not allowed in
  5063. [link label]s. A link label can have at most 999
  5064. characters inside the square brackets.
  5065. One label [matches](@matches)
  5066. another just in case their normalized forms are equal. To normalize a
  5067. label, perform the *unicode case fold* and collapse consecutive internal
  5068. [whitespace] to a single space. If there are multiple
  5069. matching reference link definitions, the one that comes first in the
  5070. document is used. (It is desirable in such cases to emit a warning.)
  5071. The contents of the first link label are parsed as inlines, which are
  5072. used as the link's text. The link's URI and title are provided by the
  5073. matching [link reference definition].
  5074. Here is a simple example:
  5075. .
  5076. [foo][bar]
  5077. [bar]: /url "title"
  5078. .
  5079. <p><a href="/url" title="title">foo</a></p>
  5080. .
  5081. The rules for the [link text] are the same as with
  5082. [inline link]s. Thus:
  5083. The link text may contain balanced brackets, but not unbalanced ones,
  5084. unless they are escaped:
  5085. .
  5086. [link [foo [bar]]][ref]
  5087. [ref]: /uri
  5088. .
  5089. <p><a href="/uri">link [foo [bar]]</a></p>
  5090. .
  5091. .
  5092. [link \[bar][ref]
  5093. [ref]: /uri
  5094. .
  5095. <p><a href="/uri">link [bar</a></p>
  5096. .
  5097. The link text may contain inline content:
  5098. .
  5099. [link *foo **bar** `#`*][ref]
  5100. [ref]: /uri
  5101. .
  5102. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5103. .
  5104. .
  5105. [![moon](moon.jpg)][ref]
  5106. [ref]: /uri
  5107. .
  5108. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5109. .
  5110. However, links may not contain other links, at any level of nesting.
  5111. .
  5112. [foo [bar](/uri)][ref]
  5113. [ref]: /uri
  5114. .
  5115. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5116. .
  5117. .
  5118. [foo *bar [baz][ref]*][ref]
  5119. [ref]: /uri
  5120. .
  5121. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5122. .
  5123. (In the examples above, we have two [shortcut reference link]s
  5124. instead of one [full reference link].)
  5125. The following cases illustrate the precedence of link text grouping over
  5126. emphasis grouping:
  5127. .
  5128. *[foo*][ref]
  5129. [ref]: /uri
  5130. .
  5131. <p>*<a href="/uri">foo*</a></p>
  5132. .
  5133. .
  5134. [foo *bar][ref]
  5135. [ref]: /uri
  5136. .
  5137. <p><a href="/uri">foo *bar</a></p>
  5138. .
  5139. These cases illustrate the precedence of HTML tags, code spans,
  5140. and autolinks over link grouping:
  5141. .
  5142. [foo <bar attr="][ref]">
  5143. [ref]: /uri
  5144. .
  5145. <p>[foo <bar attr="][ref]"></p>
  5146. .
  5147. .
  5148. [foo`][ref]`
  5149. [ref]: /uri
  5150. .
  5151. <p>[foo<code>][ref]</code></p>
  5152. .
  5153. .
  5154. [foo<http://example.com/?search=][ref]>
  5155. [ref]: /uri
  5156. .
  5157. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5158. .
  5159. Matching is case-insensitive:
  5160. .
  5161. [foo][BaR]
  5162. [bar]: /url "title"
  5163. .
  5164. <p><a href="/url" title="title">foo</a></p>
  5165. .
  5166. Unicode case fold is used:
  5167. .
  5168. [Толпой][Толпой] is a Russian word.
  5169. [ТОЛПОЙ]: /url
  5170. .
  5171. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5172. .
  5173. Consecutive internal [whitespace] is treated as one space for
  5174. purposes of determining matching:
  5175. .
  5176. [Foo
  5177. bar]: /url
  5178. [Baz][Foo bar]
  5179. .
  5180. <p><a href="/url">Baz</a></p>
  5181. .
  5182. There can be [whitespace] between the [link text] and the [link label]:
  5183. .
  5184. [foo] [bar]
  5185. [bar]: /url "title"
  5186. .
  5187. <p><a href="/url" title="title">foo</a></p>
  5188. .
  5189. .
  5190. [foo]
  5191. [bar]
  5192. [bar]: /url "title"
  5193. .
  5194. <p><a href="/url" title="title">foo</a></p>
  5195. .
  5196. When there are multiple matching [link reference definition]s,
  5197. the first is used:
  5198. .
  5199. [foo]: /url1
  5200. [foo]: /url2
  5201. [bar][foo]
  5202. .
  5203. <p><a href="/url1">bar</a></p>
  5204. .
  5205. Note that matching is performed on normalized strings, not parsed
  5206. inline content. So the following does not match, even though the
  5207. labels define equivalent inline content:
  5208. .
  5209. [bar][foo\!]
  5210. [foo!]: /url
  5211. .
  5212. <p>[bar][foo!]</p>
  5213. .
  5214. [Link label]s cannot contain brackets, unless they are
  5215. backslash-escaped:
  5216. .
  5217. [foo][ref[]
  5218. [ref[]: /uri
  5219. .
  5220. <p>[foo][ref[]</p>
  5221. <p>[ref[]: /uri</p>
  5222. .
  5223. .
  5224. [foo][ref[bar]]
  5225. [ref[bar]]: /uri
  5226. .
  5227. <p>[foo][ref[bar]]</p>
  5228. <p>[ref[bar]]: /uri</p>
  5229. .
  5230. .
  5231. [[[foo]]]
  5232. [[[foo]]]: /url
  5233. .
  5234. <p>[[[foo]]]</p>
  5235. <p>[[[foo]]]: /url</p>
  5236. .
  5237. .
  5238. [foo][ref\[]
  5239. [ref\[]: /uri
  5240. .
  5241. <p><a href="/uri">foo</a></p>
  5242. .
  5243. A [link label] must contain at least one [non-whitespace character]:
  5244. .
  5245. []
  5246. []: /uri
  5247. .
  5248. <p>[]</p>
  5249. <p>[]: /uri</p>
  5250. .
  5251. .
  5252. [
  5253. ]
  5254. [
  5255. ]: /uri
  5256. .
  5257. <p>[
  5258. ]</p>
  5259. <p>[
  5260. ]: /uri</p>
  5261. .
  5262. A [collapsed reference link](@collapsed-reference-link)
  5263. consists of a [link label] that [matches] a
  5264. [link reference definition] elsewhere in the
  5265. document, optional [whitespace], and the string `[]`.
  5266. The contents of the first link label are parsed as inlines,
  5267. which are used as the link's text. The link's URI and title are
  5268. provided by the matching reference link definition. Thus,
  5269. `[foo][]` is equivalent to `[foo][foo]`.
  5270. .
  5271. [foo][]
  5272. [foo]: /url "title"
  5273. .
  5274. <p><a href="/url" title="title">foo</a></p>
  5275. .
  5276. .
  5277. [*foo* bar][]
  5278. [*foo* bar]: /url "title"
  5279. .
  5280. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5281. .
  5282. The link labels are case-insensitive:
  5283. .
  5284. [Foo][]
  5285. [foo]: /url "title"
  5286. .
  5287. <p><a href="/url" title="title">Foo</a></p>
  5288. .
  5289. As with full reference links, [whitespace] is allowed
  5290. between the two sets of brackets:
  5291. .
  5292. [foo]
  5293. []
  5294. [foo]: /url "title"
  5295. .
  5296. <p><a href="/url" title="title">foo</a></p>
  5297. .
  5298. A [shortcut reference link](@shortcut-reference-link)
  5299. consists of a [link label] that [matches] a
  5300. [link reference definition] elsewhere in the
  5301. document and is not followed by `[]` or a link label.
  5302. The contents of the first link label are parsed as inlines,
  5303. which are used as the link's text. the link's URI and title
  5304. are provided by the matching link reference definition.
  5305. Thus, `[foo]` is equivalent to `[foo][]`.
  5306. .
  5307. [foo]
  5308. [foo]: /url "title"
  5309. .
  5310. <p><a href="/url" title="title">foo</a></p>
  5311. .
  5312. .
  5313. [*foo* bar]
  5314. [*foo* bar]: /url "title"
  5315. .
  5316. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5317. .
  5318. .
  5319. [[*foo* bar]]
  5320. [*foo* bar]: /url "title"
  5321. .
  5322. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  5323. .
  5324. .
  5325. [[bar [foo]
  5326. [foo]: /url
  5327. .
  5328. <p>[[bar <a href="/url">foo</a></p>
  5329. .
  5330. The link labels are case-insensitive:
  5331. .
  5332. [Foo]
  5333. [foo]: /url "title"
  5334. .
  5335. <p><a href="/url" title="title">Foo</a></p>
  5336. .
  5337. A space after the link text should be preserved:
  5338. .
  5339. [foo] bar
  5340. [foo]: /url
  5341. .
  5342. <p><a href="/url">foo</a> bar</p>
  5343. .
  5344. If you just want bracketed text, you can backslash-escape the
  5345. opening bracket to avoid links:
  5346. .
  5347. \[foo]
  5348. [foo]: /url "title"
  5349. .
  5350. <p>[foo]</p>
  5351. .
  5352. Note that this is a link, because a link label ends with the first
  5353. following closing bracket:
  5354. .
  5355. [foo*]: /url
  5356. *[foo*]
  5357. .
  5358. <p>*<a href="/url">foo*</a></p>
  5359. .
  5360. Full references take precedence over shortcut references:
  5361. .
  5362. [foo][bar]
  5363. [foo]: /url1
  5364. [bar]: /url2
  5365. .
  5366. <p><a href="/url2">foo</a></p>
  5367. .
  5368. In the following case `[bar][baz]` is parsed as a reference,
  5369. `[foo]` as normal text:
  5370. .
  5371. [foo][bar][baz]
  5372. [baz]: /url
  5373. .
  5374. <p>[foo]<a href="/url">bar</a></p>
  5375. .
  5376. Here, though, `[foo][bar]` is parsed as a reference, since
  5377. `[bar]` is defined:
  5378. .
  5379. [foo][bar][baz]
  5380. [baz]: /url1
  5381. [bar]: /url2
  5382. .
  5383. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  5384. .
  5385. Here `[foo]` is not parsed as a shortcut reference, because it
  5386. is followed by a link label (even though `[bar]` is not defined):
  5387. .
  5388. [foo][bar][baz]
  5389. [baz]: /url1
  5390. [foo]: /url2
  5391. .
  5392. <p>[foo]<a href="/url1">bar</a></p>
  5393. .
  5394. ## Images
  5395. Syntax for images is like the syntax for links, with one
  5396. difference. Instead of [link text], we have an
  5397. [image description](@image-description). The rules for this are the
  5398. same as for [link text], except that (a) an
  5399. image description starts with `![` rather than `[`, and
  5400. (b) an image description may contain links.
  5401. An image description has inline elements
  5402. as its contents. When an image is rendered to HTML,
  5403. this is standardly used as the image's `alt` attribute.
  5404. .
  5405. ![foo](/url "title")
  5406. .
  5407. <p><img src="/url" alt="foo" title="title" /></p>
  5408. .
  5409. .
  5410. ![foo *bar*]
  5411. [foo *bar*]: train.jpg "train & tracks"
  5412. .
  5413. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5414. .
  5415. .
  5416. ![foo ![bar](/url)](/url2)
  5417. .
  5418. <p><img src="/url2" alt="foo bar" /></p>
  5419. .
  5420. .
  5421. ![foo [bar](/url)](/url2)
  5422. .
  5423. <p><img src="/url2" alt="foo bar" /></p>
  5424. .
  5425. Though this spec is concerned with parsing, not rendering, it is
  5426. recommended that in rendering to HTML, only the plain string content
  5427. of the [image description] be used. Note that in
  5428. the above example, the alt attribute's value is `foo bar`, not `foo
  5429. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  5430. content is rendered, without formatting.
  5431. .
  5432. ![foo *bar*][]
  5433. [foo *bar*]: train.jpg "train & tracks"
  5434. .
  5435. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5436. .
  5437. .
  5438. ![foo *bar*][foobar]
  5439. [FOOBAR]: train.jpg "train & tracks"
  5440. .
  5441. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5442. .
  5443. .
  5444. ![foo](train.jpg)
  5445. .
  5446. <p><img src="train.jpg" alt="foo" /></p>
  5447. .
  5448. .
  5449. My ![foo bar](/path/to/train.jpg "title" )
  5450. .
  5451. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  5452. .
  5453. .
  5454. ![foo](<url>)
  5455. .
  5456. <p><img src="url" alt="foo" /></p>
  5457. .
  5458. .
  5459. ![](/url)
  5460. .
  5461. <p><img src="/url" alt="" /></p>
  5462. .
  5463. Reference-style:
  5464. .
  5465. ![foo] [bar]
  5466. [bar]: /url
  5467. .
  5468. <p><img src="/url" alt="foo" /></p>
  5469. .
  5470. .
  5471. ![foo] [bar]
  5472. [BAR]: /url
  5473. .
  5474. <p><img src="/url" alt="foo" /></p>
  5475. .
  5476. Collapsed:
  5477. .
  5478. ![foo][]
  5479. [foo]: /url "title"
  5480. .
  5481. <p><img src="/url" alt="foo" title="title" /></p>
  5482. .
  5483. .
  5484. ![*foo* bar][]
  5485. [*foo* bar]: /url "title"
  5486. .
  5487. <p><img src="/url" alt="foo bar" title="title" /></p>
  5488. .
  5489. The labels are case-insensitive:
  5490. .
  5491. ![Foo][]
  5492. [foo]: /url "title"
  5493. .
  5494. <p><img src="/url" alt="Foo" title="title" /></p>
  5495. .
  5496. As with full reference links, [whitespace] is allowed
  5497. between the two sets of brackets:
  5498. .
  5499. ![foo]
  5500. []
  5501. [foo]: /url "title"
  5502. .
  5503. <p><img src="/url" alt="foo" title="title" /></p>
  5504. .
  5505. Shortcut:
  5506. .
  5507. ![foo]
  5508. [foo]: /url "title"
  5509. .
  5510. <p><img src="/url" alt="foo" title="title" /></p>
  5511. .
  5512. .
  5513. ![*foo* bar]
  5514. [*foo* bar]: /url "title"
  5515. .
  5516. <p><img src="/url" alt="foo bar" title="title" /></p>
  5517. .
  5518. Note that link labels cannot contain unescaped brackets:
  5519. .
  5520. ![[foo]]
  5521. [[foo]]: /url "title"
  5522. .
  5523. <p>![[foo]]</p>
  5524. <p>[[foo]]: /url &quot;title&quot;</p>
  5525. .
  5526. The link labels are case-insensitive:
  5527. .
  5528. ![Foo]
  5529. [foo]: /url "title"
  5530. .
  5531. <p><img src="/url" alt="Foo" title="title" /></p>
  5532. .
  5533. If you just want bracketed text, you can backslash-escape the
  5534. opening `!` and `[`:
  5535. .
  5536. \!\[foo]
  5537. [foo]: /url "title"
  5538. .
  5539. <p>![foo]</p>
  5540. .
  5541. If you want a link after a literal `!`, backslash-escape the
  5542. `!`:
  5543. .
  5544. \![foo]
  5545. [foo]: /url "title"
  5546. .
  5547. <p>!<a href="/url" title="title">foo</a></p>
  5548. .
  5549. ## Autolinks
  5550. [Autolink](@autolink)s are absolute URIs and email addresses inside
  5551. `<` and `>`. They are parsed as links, with the URL or email address
  5552. as the link label.
  5553. A [URI autolink](@uri-autolink) consists of `<`, followed by an
  5554. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  5555. a link to the URI, with the URI as the link's label.
  5556. An [absolute URI](@absolute-uri),
  5557. for these purposes, consists of a [scheme] followed by a colon (`:`)
  5558. followed by zero or more characters other than ASCII
  5559. [whitespace] and control characters, `<`, and `>`. If
  5560. the URI includes these characters, you must use percent-encoding
  5561. (e.g. `%20` for a space).
  5562. The following [schemes](@scheme)
  5563. are recognized (case-insensitive):
  5564. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  5565. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  5566. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  5567. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  5568. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  5569. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  5570. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  5571. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5572. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5573. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5574. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5575. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5576. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5577. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5578. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5579. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5580. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5581. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5582. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5583. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5584. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5585. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5586. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5587. Here are some valid autolinks:
  5588. .
  5589. <http://foo.bar.baz>
  5590. .
  5591. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5592. .
  5593. .
  5594. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  5595. .
  5596. <p><a href="http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean">http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean</a></p>
  5597. .
  5598. .
  5599. <irc://foo.bar:2233/baz>
  5600. .
  5601. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5602. .
  5603. Uppercase is also fine:
  5604. .
  5605. <MAILTO:FOO@BAR.BAZ>
  5606. .
  5607. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5608. .
  5609. Spaces are not allowed in autolinks:
  5610. .
  5611. <http://foo.bar/baz bim>
  5612. .
  5613. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5614. .
  5615. Backslash-escapes do not work inside autolinks:
  5616. .
  5617. <http://example.com/\[\>
  5618. .
  5619. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  5620. .
  5621. An [email autolink](@email-autolink)
  5622. consists of `<`, followed by an [email address],
  5623. followed by `>`. The link's label is the email address,
  5624. and the URL is `mailto:` followed by the email address.
  5625. An [email address](@email-address),
  5626. for these purposes, is anything that matches
  5627. the [non-normative regex from the HTML5
  5628. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  5629. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5630. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5631. Examples of email autolinks:
  5632. .
  5633. <foo@bar.example.com>
  5634. .
  5635. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5636. .
  5637. .
  5638. <foo+special@Bar.baz-bar0.com>
  5639. .
  5640. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5641. .
  5642. Backslash-escapes do not work inside email autolinks:
  5643. .
  5644. <foo\+@bar.example.com>
  5645. .
  5646. <p>&lt;foo+@bar.example.com&gt;</p>
  5647. .
  5648. These are not autolinks:
  5649. .
  5650. <>
  5651. .
  5652. <p>&lt;&gt;</p>
  5653. .
  5654. .
  5655. <heck://bing.bong>
  5656. .
  5657. <p>&lt;heck://bing.bong&gt;</p>
  5658. .
  5659. .
  5660. < http://foo.bar >
  5661. .
  5662. <p>&lt; http://foo.bar &gt;</p>
  5663. .
  5664. .
  5665. <foo.bar.baz>
  5666. .
  5667. <p>&lt;foo.bar.baz&gt;</p>
  5668. .
  5669. .
  5670. <localhost:5001/foo>
  5671. .
  5672. <p>&lt;localhost:5001/foo&gt;</p>
  5673. .
  5674. .
  5675. http://example.com
  5676. .
  5677. <p>http://example.com</p>
  5678. .
  5679. .
  5680. foo@bar.example.com
  5681. .
  5682. <p>foo@bar.example.com</p>
  5683. .
  5684. ## Raw HTML
  5685. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5686. raw HTML tag and will be rendered in HTML without escaping.
  5687. Tag and attribute names are not limited to current HTML tags,
  5688. so custom tags (and even, say, DocBook tags) may be used.
  5689. Here is the grammar for tags:
  5690. A [tag name](@tag-name) consists of an ASCII letter
  5691. followed by zero or more ASCII letters or digits.
  5692. An [attribute](@attribute) consists of [whitespace],
  5693. an [attribute name], and an optional
  5694. [attribute value specification].
  5695. An [attribute name](@attribute-name)
  5696. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5697. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5698. specification restricted to ASCII. HTML5 is laxer.)
  5699. An [attribute value specification](@attribute-value-specification)
  5700. consists of optional [whitespace],
  5701. a `=` character, optional [whitespace], and an [attribute
  5702. value].
  5703. An [attribute value](@attribute-value)
  5704. consists of an [unquoted attribute value],
  5705. a [single-quoted attribute value], or a [double-quoted attribute value].
  5706. An [unquoted attribute value](@unquoted-attribute-value)
  5707. is a nonempty string of characters not
  5708. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5709. A [single-quoted attribute value](@single-quoted-attribute-value)
  5710. consists of `'`, zero or more
  5711. characters not including `'`, and a final `'`.
  5712. A [double-quoted attribute value](@double-quoted-attribute-value)
  5713. consists of `"`, zero or more
  5714. characters not including `"`, and a final `"`.
  5715. An [open tag](@open-tag) consists of a `<` character, a [tag name],
  5716. zero or more [attributes](@attribute], optional [whitespace], an optional `/`
  5717. character, and a `>` character.
  5718. A [closing tag](@closing-tag) consists of the string `</`, a
  5719. [tag name], optional [whitespace], and the character `>`.
  5720. An [HTML comment](@html-comment) consists of `<!--` + *text* + `-->`,
  5721. where *text* does not start with `>` or `->`, does not end with `-`,
  5722. and does not contain `--`. (See the
  5723. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  5724. A [processing instruction](@processing-instruction)
  5725. consists of the string `<?`, a string
  5726. of characters not including the string `?>`, and the string
  5727. `?>`.
  5728. A [declaration](@declaration) consists of the
  5729. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5730. [whitespace], a string of characters not including the
  5731. character `>`, and the character `>`.
  5732. A [CDATA section](@cdata-section) consists of
  5733. the string `<![CDATA[`, a string of characters not including the string
  5734. `]]>`, and the string `]]>`.
  5735. An [HTML tag](@html-tag) consists of an [open tag], a [closing tag],
  5736. an [HTML comment], a [processing instruction], a [declaration],
  5737. or a [CDATA section].
  5738. Here are some simple open tags:
  5739. .
  5740. <a><bab><c2c>
  5741. .
  5742. <p><a><bab><c2c></p>
  5743. .
  5744. Empty elements:
  5745. .
  5746. <a/><b2/>
  5747. .
  5748. <p><a/><b2/></p>
  5749. .
  5750. [Whitespace] is allowed:
  5751. .
  5752. <a /><b2
  5753. data="foo" >
  5754. .
  5755. <p><a /><b2
  5756. data="foo" ></p>
  5757. .
  5758. With attributes:
  5759. .
  5760. <a foo="bar" bam = 'baz <em>"</em>'
  5761. _boolean zoop:33=zoop:33 />
  5762. .
  5763. <p><a foo="bar" bam = 'baz <em>"</em>'
  5764. _boolean zoop:33=zoop:33 /></p>
  5765. .
  5766. Illegal tag names, not parsed as HTML:
  5767. .
  5768. <33> <__>
  5769. .
  5770. <p>&lt;33&gt; &lt;__&gt;</p>
  5771. .
  5772. Illegal attribute names:
  5773. .
  5774. <a h*#ref="hi">
  5775. .
  5776. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5777. .
  5778. Illegal attribute values:
  5779. .
  5780. <a href="hi'> <a href=hi'>
  5781. .
  5782. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5783. .
  5784. Illegal [whitespace]:
  5785. .
  5786. < a><
  5787. foo><bar/ >
  5788. .
  5789. <p>&lt; a&gt;&lt;
  5790. foo&gt;&lt;bar/ &gt;</p>
  5791. .
  5792. Missing [whitespace]:
  5793. .
  5794. <a href='bar'title=title>
  5795. .
  5796. <p>&lt;a href='bar'title=title&gt;</p>
  5797. .
  5798. Closing tags:
  5799. .
  5800. </a>
  5801. </foo >
  5802. .
  5803. <p></a>
  5804. </foo ></p>
  5805. .
  5806. Illegal attributes in closing tag:
  5807. .
  5808. </a href="foo">
  5809. .
  5810. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5811. .
  5812. Comments:
  5813. .
  5814. foo <!-- this is a
  5815. comment - with hyphen -->
  5816. .
  5817. <p>foo <!-- this is a
  5818. comment - with hyphen --></p>
  5819. .
  5820. .
  5821. foo <!-- not a comment -- two hyphens -->
  5822. .
  5823. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5824. .
  5825. Not comments:
  5826. .
  5827. foo <!--> foo -->
  5828. foo <!-- foo--->
  5829. .
  5830. <p>foo &lt;!--&gt; foo --&gt;</p>
  5831. <p>foo &lt;!-- foo---&gt;</p>
  5832. .
  5833. Processing instructions:
  5834. .
  5835. foo <?php echo $a; ?>
  5836. .
  5837. <p>foo <?php echo $a; ?></p>
  5838. .
  5839. Declarations:
  5840. .
  5841. foo <!ELEMENT br EMPTY>
  5842. .
  5843. <p>foo <!ELEMENT br EMPTY></p>
  5844. .
  5845. CDATA sections:
  5846. .
  5847. foo <![CDATA[>&<]]>
  5848. .
  5849. <p>foo <![CDATA[>&<]]></p>
  5850. .
  5851. Entities are preserved in HTML attributes:
  5852. .
  5853. <a href="&ouml;">
  5854. .
  5855. <p><a href="&ouml;"></p>
  5856. .
  5857. Backslash escapes do not work in HTML attributes:
  5858. .
  5859. <a href="\*">
  5860. .
  5861. <p><a href="\*"></p>
  5862. .
  5863. .
  5864. <a href="\"">
  5865. .
  5866. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5867. .
  5868. ## Hard line breaks
  5869. A line break (not in a code span or HTML tag) that is preceded
  5870. by two or more spaces and does not occur at the end of a block
  5871. is parsed as a [hard line break](@hard-line-break) (rendered
  5872. in HTML as a `<br />` tag):
  5873. .
  5874. foo
  5875. baz
  5876. .
  5877. <p>foo<br />
  5878. baz</p>
  5879. .
  5880. For a more visible alternative, a backslash before the
  5881. [line ending] may be used instead of two spaces:
  5882. .
  5883. foo\
  5884. baz
  5885. .
  5886. <p>foo<br />
  5887. baz</p>
  5888. .
  5889. More than two spaces can be used:
  5890. .
  5891. foo
  5892. baz
  5893. .
  5894. <p>foo<br />
  5895. baz</p>
  5896. .
  5897. Leading spaces at the beginning of the next line are ignored:
  5898. .
  5899. foo
  5900. bar
  5901. .
  5902. <p>foo<br />
  5903. bar</p>
  5904. .
  5905. .
  5906. foo\
  5907. bar
  5908. .
  5909. <p>foo<br />
  5910. bar</p>
  5911. .
  5912. Line breaks can occur inside emphasis, links, and other constructs
  5913. that allow inline content:
  5914. .
  5915. *foo
  5916. bar*
  5917. .
  5918. <p><em>foo<br />
  5919. bar</em></p>
  5920. .
  5921. .
  5922. *foo\
  5923. bar*
  5924. .
  5925. <p><em>foo<br />
  5926. bar</em></p>
  5927. .
  5928. Line breaks do not occur inside code spans
  5929. .
  5930. `code
  5931. span`
  5932. .
  5933. <p><code>code span</code></p>
  5934. .
  5935. .
  5936. `code\
  5937. span`
  5938. .
  5939. <p><code>code\ span</code></p>
  5940. .
  5941. or HTML tags:
  5942. .
  5943. <a href="foo
  5944. bar">
  5945. .
  5946. <p><a href="foo
  5947. bar"></p>
  5948. .
  5949. .
  5950. <a href="foo\
  5951. bar">
  5952. .
  5953. <p><a href="foo\
  5954. bar"></p>
  5955. .
  5956. Hard line breaks are for separating inline content within a block.
  5957. Neither syntax for hard line breaks works at the end of a paragraph or
  5958. other block element:
  5959. .
  5960. foo\
  5961. .
  5962. <p>foo\</p>
  5963. .
  5964. .
  5965. foo
  5966. .
  5967. <p>foo</p>
  5968. .
  5969. .
  5970. ### foo\
  5971. .
  5972. <h3>foo\</h3>
  5973. .
  5974. .
  5975. ### foo
  5976. .
  5977. <h3>foo</h3>
  5978. .
  5979. ## Soft line breaks
  5980. A regular line break (not in a code span or HTML tag) that is not
  5981. preceded by two or more spaces or a backslash is parsed as a
  5982. softbreak. (A softbreak may be rendered in HTML either as a
  5983. [line ending] or as a space. The result will be the same in
  5984. browsers. In the examples here, a [line ending] will be used.)
  5985. .
  5986. foo
  5987. baz
  5988. .
  5989. <p>foo
  5990. baz</p>
  5991. .
  5992. Spaces at the end of the line and beginning of the next line are
  5993. removed:
  5994. .
  5995. foo
  5996. baz
  5997. .
  5998. <p>foo
  5999. baz</p>
  6000. .
  6001. A conforming parser may render a soft line break in HTML either as a
  6002. line break or as a space.
  6003. A renderer may also provide an option to render soft line breaks
  6004. as hard line breaks.
  6005. ## Textual content
  6006. Any characters not given an interpretation by the above rules will
  6007. be parsed as plain textual content.
  6008. .
  6009. hello $.;'there
  6010. .
  6011. <p>hello $.;'there</p>
  6012. .
  6013. .
  6014. Foo χρῆν
  6015. .
  6016. <p>Foo χρῆν</p>
  6017. .
  6018. Internal spaces are preserved verbatim:
  6019. .
  6020. Multiple spaces
  6021. .
  6022. <p>Multiple spaces</p>
  6023. .
  6024. <!-- END TESTS -->
  6025. # Appendix A: A parsing strategy {-}
  6026. ## Overview {-}
  6027. Parsing has two phases:
  6028. 1. In the first phase, lines of input are consumed and the block
  6029. structure of the document---its division into paragraphs, block quotes,
  6030. list items, and so on---is constructed. Text is assigned to these
  6031. blocks but not parsed. Link reference definitions are parsed and a
  6032. map of links is constructed.
  6033. 2. In the second phase, the raw text contents of paragraphs and headers
  6034. are parsed into sequences of Markdown inline elements (strings,
  6035. code spans, links, emphasis, and so on), using the map of link
  6036. references constructed in phase 1.
  6037. ## The document tree {-}
  6038. At each point in processing, the document is represented as a tree of
  6039. **blocks**. The root of the tree is a `document` block. The `document`
  6040. may have any number of other blocks as **children**. These children
  6041. may, in turn, have other blocks as children. The last child of a block
  6042. is normally considered **open**, meaning that subsequent lines of input
  6043. can alter its contents. (Blocks that are not open are **closed**.)
  6044. Here, for example, is a possible document tree, with the open blocks
  6045. marked by arrows:
  6046. ``` tree
  6047. -> document
  6048. -> block_quote
  6049. paragraph
  6050. "Lorem ipsum dolor\nsit amet."
  6051. -> list (type=bullet tight=true bullet_char=-)
  6052. list_item
  6053. paragraph
  6054. "Qui *quodsi iracundia*"
  6055. -> list_item
  6056. -> paragraph
  6057. "aliquando id"
  6058. ```
  6059. ## How source lines alter the document tree {-}
  6060. Each line that is processed has an effect on this tree. The line is
  6061. analyzed and, depending on its contents, the document may be altered
  6062. in one or more of the following ways:
  6063. 1. One or more open blocks may be closed.
  6064. 2. One or more new blocks may be created as children of the
  6065. last open block.
  6066. 3. Text may be added to the last (deepest) open block remaining
  6067. on the tree.
  6068. Once a line has been incorporated into the tree in this way,
  6069. it can be discarded, so input can be read in a stream.
  6070. We can see how this works by considering how the tree above is
  6071. generated by four lines of Markdown:
  6072. ``` markdown
  6073. > Lorem ipsum dolor
  6074. sit amet.
  6075. > - Qui *quodsi iracundia*
  6076. > - aliquando id
  6077. ```
  6078. At the outset, our document model is just
  6079. ``` tree
  6080. -> document
  6081. ```
  6082. The first line of our text,
  6083. ``` markdown
  6084. > Lorem ipsum dolor
  6085. ```
  6086. causes a `block_quote` block to be created as a child of our
  6087. open `document` block, and a `paragraph` block as a child of
  6088. the `block_quote`. Then the text is added to the last open
  6089. block, the `paragraph`:
  6090. ``` tree
  6091. -> document
  6092. -> block_quote
  6093. -> paragraph
  6094. "Lorem ipsum dolor"
  6095. ```
  6096. The next line,
  6097. ``` markdown
  6098. sit amet.
  6099. ```
  6100. is a "lazy continuation" of the open `paragraph`, so it gets added
  6101. to the paragraph's text:
  6102. ``` tree
  6103. -> document
  6104. -> block_quote
  6105. -> paragraph
  6106. "Lorem ipsum dolor\nsit amet."
  6107. ```
  6108. The third line,
  6109. ``` markdown
  6110. > - Qui *quodsi iracundia*
  6111. ```
  6112. causes the `paragraph` block to be closed, and a new `list` block
  6113. opened as a child of the `block_quote`. A `list_item` is also
  6114. added as a child of the `list`, and a `paragraph` as a child of
  6115. the `list_item`. The text is then added to the new `paragraph`:
  6116. ``` tree
  6117. -> document
  6118. -> block_quote
  6119. paragraph
  6120. "Lorem ipsum dolor\nsit amet."
  6121. -> list (type=bullet tight=true bullet_char=-)
  6122. -> list_item
  6123. -> paragraph
  6124. "Qui *quodsi iracundia*"
  6125. ```
  6126. The fourth line,
  6127. ``` markdown
  6128. > - aliquando id
  6129. ```
  6130. causes the `list_item` (and its child the `paragraph`) to be closed,
  6131. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6132. is added as a child of the new `list_item`, to contain the text.
  6133. We thus obtain the final tree:
  6134. ``` tree
  6135. -> document
  6136. -> block_quote
  6137. paragraph
  6138. "Lorem ipsum dolor\nsit amet."
  6139. -> list (type=bullet tight=true bullet_char=-)
  6140. list_item
  6141. paragraph
  6142. "Qui *quodsi iracundia*"
  6143. -> list_item
  6144. -> paragraph
  6145. "aliquando id"
  6146. ```
  6147. ## From block structure to the final document {-}
  6148. Once all of the input has been parsed, all open blocks are closed.
  6149. We then "walk the tree," visiting every node, and parse raw
  6150. string contents of paragraphs and headers as inlines. At this
  6151. point we have seen all the link reference definitions, so we can
  6152. resolve reference links as we go.
  6153. ``` tree
  6154. document
  6155. block_quote
  6156. paragraph
  6157. str "Lorem ipsum dolor"
  6158. softbreak
  6159. str "sit amet."
  6160. list (type=bullet tight=true bullet_char=-)
  6161. list_item
  6162. paragraph
  6163. str "Qui "
  6164. emph
  6165. str "quodsi iracundia"
  6166. list_item
  6167. paragraph
  6168. str "aliquando id"
  6169. ```
  6170. Notice how the [line ending] in the first paragraph has
  6171. been parsed as a `softbreak`, and the asterisks in the first list item
  6172. have become an `emph`.
  6173. The document can be rendered as HTML, or in any other format, given
  6174. an appropriate renderer.