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