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