aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: f6ea79399b7bc9fe3d0eea85b5aecaa48ee7b251 (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 a link reference definition, but it has no title:
  1640. .
  1641. [foo]: /url
  1642. "title" ok
  1643. .
  1644. <p>&quot;title&quot; ok</p>
  1645. .
  1646. This is not a link reference definition, because it is indented
  1647. four spaces:
  1648. .
  1649. [foo]: /url "title"
  1650. [foo]
  1651. .
  1652. <pre><code>[foo]: /url &quot;title&quot;
  1653. </code></pre>
  1654. <p>[foo]</p>
  1655. .
  1656. This is not a link reference definition, because it occurs inside
  1657. a code block:
  1658. .
  1659. ```
  1660. [foo]: /url
  1661. ```
  1662. [foo]
  1663. .
  1664. <pre><code>[foo]: /url
  1665. </code></pre>
  1666. <p>[foo]</p>
  1667. .
  1668. A [link reference definition] cannot interrupt a paragraph.
  1669. .
  1670. Foo
  1671. [bar]: /baz
  1672. [bar]
  1673. .
  1674. <p>Foo
  1675. [bar]: /baz</p>
  1676. <p>[bar]</p>
  1677. .
  1678. However, it can directly follow other block elements, such as headers
  1679. and horizontal rules, and it need not be followed by a blank line.
  1680. .
  1681. # [Foo]
  1682. [foo]: /url
  1683. > bar
  1684. .
  1685. <h1><a href="/url">Foo</a></h1>
  1686. <blockquote>
  1687. <p>bar</p>
  1688. </blockquote>
  1689. .
  1690. Several [link reference definition]s
  1691. can occur one after another, without intervening blank lines.
  1692. .
  1693. [foo]: /foo-url "foo"
  1694. [bar]: /bar-url
  1695. "bar"
  1696. [baz]: /baz-url
  1697. [foo],
  1698. [bar],
  1699. [baz]
  1700. .
  1701. <p><a href="/foo-url" title="foo">foo</a>,
  1702. <a href="/bar-url" title="bar">bar</a>,
  1703. <a href="/baz-url">baz</a></p>
  1704. .
  1705. [Link reference definition]s can occur
  1706. inside block containers, like lists and block quotations. They
  1707. affect the entire document, not just the container in which they
  1708. are defined:
  1709. .
  1710. [foo]
  1711. > [foo]: /url
  1712. .
  1713. <p><a href="/url">foo</a></p>
  1714. <blockquote>
  1715. </blockquote>
  1716. .
  1717. ## Paragraphs
  1718. A sequence of non-blank lines that cannot be interpreted as other
  1719. kinds of blocks forms a [paragraph](@paragraph).
  1720. The contents of the paragraph are the result of parsing the
  1721. paragraph's raw content as inlines. The paragraph's raw content
  1722. is formed by concatenating the lines and removing initial and final
  1723. [whitespace].
  1724. A simple example with two paragraphs:
  1725. .
  1726. aaa
  1727. bbb
  1728. .
  1729. <p>aaa</p>
  1730. <p>bbb</p>
  1731. .
  1732. Paragraphs can contain multiple lines, but no blank lines:
  1733. .
  1734. aaa
  1735. bbb
  1736. ccc
  1737. ddd
  1738. .
  1739. <p>aaa
  1740. bbb</p>
  1741. <p>ccc
  1742. ddd</p>
  1743. .
  1744. Multiple blank lines between paragraph have no effect:
  1745. .
  1746. aaa
  1747. bbb
  1748. .
  1749. <p>aaa</p>
  1750. <p>bbb</p>
  1751. .
  1752. Leading spaces are skipped:
  1753. .
  1754. aaa
  1755. bbb
  1756. .
  1757. <p>aaa
  1758. bbb</p>
  1759. .
  1760. Lines after the first may be indented any amount, since indented
  1761. code blocks cannot interrupt paragraphs.
  1762. .
  1763. aaa
  1764. bbb
  1765. ccc
  1766. .
  1767. <p>aaa
  1768. bbb
  1769. ccc</p>
  1770. .
  1771. However, the first line may be indented at most three spaces,
  1772. or an indented code block will be triggered:
  1773. .
  1774. aaa
  1775. bbb
  1776. .
  1777. <p>aaa
  1778. bbb</p>
  1779. .
  1780. .
  1781. aaa
  1782. bbb
  1783. .
  1784. <pre><code>aaa
  1785. </code></pre>
  1786. <p>bbb</p>
  1787. .
  1788. Final spaces are stripped before inline parsing, so a paragraph
  1789. that ends with two or more spaces will not end with a [hard line
  1790. break]:
  1791. .
  1792. aaa
  1793. bbb
  1794. .
  1795. <p>aaa<br />
  1796. bbb</p>
  1797. .
  1798. ## Blank lines
  1799. [Blank line]s between block-level elements are ignored,
  1800. except for the role they play in determining whether a [list]
  1801. is [tight] or [loose].
  1802. Blank lines at the beginning and end of the document are also ignored.
  1803. .
  1804. aaa
  1805. # aaa
  1806. .
  1807. <p>aaa</p>
  1808. <h1>aaa</h1>
  1809. .
  1810. # Container blocks
  1811. A [container block] is a block that has other
  1812. blocks as its contents. There are two basic kinds of container blocks:
  1813. [block quotes] and [list items].
  1814. [Lists] are meta-containers for [list items].
  1815. We define the syntax for container blocks recursively. The general
  1816. form of the definition is:
  1817. > If X is a sequence of blocks, then the result of
  1818. > transforming X in such-and-such a way is a container of type Y
  1819. > with these blocks as its content.
  1820. So, we explain what counts as a block quote or list item by explaining
  1821. how these can be *generated* from their contents. This should suffice
  1822. to define the syntax, although it does not give a recipe for *parsing*
  1823. these constructions. (A recipe is provided below in the section entitled
  1824. [A parsing strategy](#appendix-a-a-parsing-strategy).)
  1825. ## Block quotes
  1826. A [block quote marker](@block-quote-marker)
  1827. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  1828. with a following space, or (b) a single character `>` not followed by a space.
  1829. The following rules define [block quotes]:
  1830. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  1831. of blocks *Bs*, then the result of prepending a [block quote
  1832. marker] to the beginning of each line in *Ls*
  1833. is a [block quote](#block-quotes) containing *Bs*.
  1834. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  1835. quote](#block-quotes) with contents *Bs*, then the result of deleting
  1836. the initial [block quote marker] from one or
  1837. more lines in which the next [non-space character] after the [block
  1838. quote marker] is [paragraph continuation
  1839. text] is a block quote with *Bs* as its content.
  1840. [Paragraph continuation text](@paragraph-continuation-text) is text
  1841. that will be parsed as part of the content of a paragraph, but does
  1842. not occur at the beginning of the paragraph.
  1843. 3. **Consecutiveness.** A document cannot contain two [block
  1844. quotes] in a row unless there is a [blank line] between them.
  1845. Nothing else counts as a [block quote](#block-quotes).
  1846. Here is a simple example:
  1847. .
  1848. > # Foo
  1849. > bar
  1850. > baz
  1851. .
  1852. <blockquote>
  1853. <h1>Foo</h1>
  1854. <p>bar
  1855. baz</p>
  1856. </blockquote>
  1857. .
  1858. The spaces after the `>` characters can be omitted:
  1859. .
  1860. ># Foo
  1861. >bar
  1862. > baz
  1863. .
  1864. <blockquote>
  1865. <h1>Foo</h1>
  1866. <p>bar
  1867. baz</p>
  1868. </blockquote>
  1869. .
  1870. The `>` characters can be indented 1-3 spaces:
  1871. .
  1872. > # Foo
  1873. > bar
  1874. > baz
  1875. .
  1876. <blockquote>
  1877. <h1>Foo</h1>
  1878. <p>bar
  1879. baz</p>
  1880. </blockquote>
  1881. .
  1882. Four spaces gives us a code block:
  1883. .
  1884. > # Foo
  1885. > bar
  1886. > baz
  1887. .
  1888. <pre><code>&gt; # Foo
  1889. &gt; bar
  1890. &gt; baz
  1891. </code></pre>
  1892. .
  1893. The Laziness clause allows us to omit the `>` before a
  1894. paragraph continuation line:
  1895. .
  1896. > # Foo
  1897. > bar
  1898. baz
  1899. .
  1900. <blockquote>
  1901. <h1>Foo</h1>
  1902. <p>bar
  1903. baz</p>
  1904. </blockquote>
  1905. .
  1906. A block quote can contain some lazy and some non-lazy
  1907. continuation lines:
  1908. .
  1909. > bar
  1910. baz
  1911. > foo
  1912. .
  1913. <blockquote>
  1914. <p>bar
  1915. baz
  1916. foo</p>
  1917. </blockquote>
  1918. .
  1919. Laziness only applies to lines that would have been continuations of
  1920. paragraphs had they been prepended with `>`. For example, the
  1921. `>` cannot be omitted in the second line of
  1922. ``` markdown
  1923. > foo
  1924. > ---
  1925. ```
  1926. without changing the meaning:
  1927. .
  1928. > foo
  1929. ---
  1930. .
  1931. <blockquote>
  1932. <p>foo</p>
  1933. </blockquote>
  1934. <hr />
  1935. .
  1936. Similarly, if we omit the `>` in the second line of
  1937. ``` markdown
  1938. > - foo
  1939. > - bar
  1940. ```
  1941. then the block quote ends after the first line:
  1942. .
  1943. > - foo
  1944. - bar
  1945. .
  1946. <blockquote>
  1947. <ul>
  1948. <li>foo</li>
  1949. </ul>
  1950. </blockquote>
  1951. <ul>
  1952. <li>bar</li>
  1953. </ul>
  1954. .
  1955. For the same reason, we can't omit the `>` in front of
  1956. subsequent lines of an indented or fenced code block:
  1957. .
  1958. > foo
  1959. bar
  1960. .
  1961. <blockquote>
  1962. <pre><code>foo
  1963. </code></pre>
  1964. </blockquote>
  1965. <pre><code>bar
  1966. </code></pre>
  1967. .
  1968. .
  1969. > ```
  1970. foo
  1971. ```
  1972. .
  1973. <blockquote>
  1974. <pre><code></code></pre>
  1975. </blockquote>
  1976. <p>foo</p>
  1977. <pre><code></code></pre>
  1978. .
  1979. A block quote can be empty:
  1980. .
  1981. >
  1982. .
  1983. <blockquote>
  1984. </blockquote>
  1985. .
  1986. .
  1987. >
  1988. >
  1989. >
  1990. .
  1991. <blockquote>
  1992. </blockquote>
  1993. .
  1994. A block quote can have initial or final blank lines:
  1995. .
  1996. >
  1997. > foo
  1998. >
  1999. .
  2000. <blockquote>
  2001. <p>foo</p>
  2002. </blockquote>
  2003. .
  2004. A blank line always separates block quotes:
  2005. .
  2006. > foo
  2007. > bar
  2008. .
  2009. <blockquote>
  2010. <p>foo</p>
  2011. </blockquote>
  2012. <blockquote>
  2013. <p>bar</p>
  2014. </blockquote>
  2015. .
  2016. (Most current Markdown implementations, including John Gruber's
  2017. original `Markdown.pl`, will parse this example as a single block quote
  2018. with two paragraphs. But it seems better to allow the author to decide
  2019. whether two block quotes or one are wanted.)
  2020. Consecutiveness means that if we put these block quotes together,
  2021. we get a single block quote:
  2022. .
  2023. > foo
  2024. > bar
  2025. .
  2026. <blockquote>
  2027. <p>foo
  2028. bar</p>
  2029. </blockquote>
  2030. .
  2031. To get a block quote with two paragraphs, use:
  2032. .
  2033. > foo
  2034. >
  2035. > bar
  2036. .
  2037. <blockquote>
  2038. <p>foo</p>
  2039. <p>bar</p>
  2040. </blockquote>
  2041. .
  2042. Block quotes can interrupt paragraphs:
  2043. .
  2044. foo
  2045. > bar
  2046. .
  2047. <p>foo</p>
  2048. <blockquote>
  2049. <p>bar</p>
  2050. </blockquote>
  2051. .
  2052. In general, blank lines are not needed before or after block
  2053. quotes:
  2054. .
  2055. > aaa
  2056. ***
  2057. > bbb
  2058. .
  2059. <blockquote>
  2060. <p>aaa</p>
  2061. </blockquote>
  2062. <hr />
  2063. <blockquote>
  2064. <p>bbb</p>
  2065. </blockquote>
  2066. .
  2067. However, because of laziness, a blank line is needed between
  2068. a block quote and a following paragraph:
  2069. .
  2070. > bar
  2071. baz
  2072. .
  2073. <blockquote>
  2074. <p>bar
  2075. baz</p>
  2076. </blockquote>
  2077. .
  2078. .
  2079. > bar
  2080. baz
  2081. .
  2082. <blockquote>
  2083. <p>bar</p>
  2084. </blockquote>
  2085. <p>baz</p>
  2086. .
  2087. .
  2088. > bar
  2089. >
  2090. baz
  2091. .
  2092. <blockquote>
  2093. <p>bar</p>
  2094. </blockquote>
  2095. <p>baz</p>
  2096. .
  2097. It is a consequence of the Laziness rule that any number
  2098. of initial `>`s may be omitted on a continuation line of a
  2099. nested block quote:
  2100. .
  2101. > > > foo
  2102. bar
  2103. .
  2104. <blockquote>
  2105. <blockquote>
  2106. <blockquote>
  2107. <p>foo
  2108. bar</p>
  2109. </blockquote>
  2110. </blockquote>
  2111. </blockquote>
  2112. .
  2113. .
  2114. >>> foo
  2115. > bar
  2116. >>baz
  2117. .
  2118. <blockquote>
  2119. <blockquote>
  2120. <blockquote>
  2121. <p>foo
  2122. bar
  2123. baz</p>
  2124. </blockquote>
  2125. </blockquote>
  2126. </blockquote>
  2127. .
  2128. When including an indented code block in a block quote,
  2129. remember that the [block quote marker] includes
  2130. both the `>` and a following space. So *five spaces* are needed after
  2131. the `>`:
  2132. .
  2133. > code
  2134. > not code
  2135. .
  2136. <blockquote>
  2137. <pre><code>code
  2138. </code></pre>
  2139. </blockquote>
  2140. <blockquote>
  2141. <p>not code</p>
  2142. </blockquote>
  2143. .
  2144. ## List items
  2145. A [list marker](@list-marker) is a
  2146. [bullet list marker] or an [ordered list marker].
  2147. A [bullet list marker](@bullet-list-marker)
  2148. is a `-`, `+`, or `*` character.
  2149. An [ordered list marker](@ordered-list-marker)
  2150. is a sequence of one of more digits (`0-9`), followed by either a
  2151. `.` character or a `)` character.
  2152. The following rules define [list items]:
  2153. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2154. blocks *Bs* starting with a [non-space character] and not separated
  2155. from each other by more than one blank line, and *M* is a list
  2156. marker of width *W* followed by 0 < *N* < 5 spaces, then the result
  2157. of prepending *M* and the following spaces to the first line of
  2158. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2159. list item with *Bs* as its contents. The type of the list item
  2160. (bullet or ordered) is determined by the type of its list marker.
  2161. If the list item is ordered, then it is also assigned a start
  2162. number, based on the ordered list marker.
  2163. For example, let *Ls* be the lines
  2164. .
  2165. A paragraph
  2166. with two lines.
  2167. indented code
  2168. > A block quote.
  2169. .
  2170. <p>A paragraph
  2171. with two lines.</p>
  2172. <pre><code>indented code
  2173. </code></pre>
  2174. <blockquote>
  2175. <p>A block quote.</p>
  2176. </blockquote>
  2177. .
  2178. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2179. that the following is an ordered list item with start number 1,
  2180. and the same contents as *Ls*:
  2181. .
  2182. 1. A paragraph
  2183. with two lines.
  2184. indented code
  2185. > A block quote.
  2186. .
  2187. <ol>
  2188. <li>
  2189. <p>A paragraph
  2190. with two lines.</p>
  2191. <pre><code>indented code
  2192. </code></pre>
  2193. <blockquote>
  2194. <p>A block quote.</p>
  2195. </blockquote>
  2196. </li>
  2197. </ol>
  2198. .
  2199. The most important thing to notice is that the position of
  2200. the text after the list marker determines how much indentation
  2201. is needed in subsequent blocks in the list item. If the list
  2202. marker takes up two spaces, and there are three spaces between
  2203. the list marker and the next [non-space character], then blocks
  2204. must be indented five spaces in order to fall under the list
  2205. item.
  2206. Here are some examples showing how far content must be indented to be
  2207. put under the list item:
  2208. .
  2209. - one
  2210. two
  2211. .
  2212. <ul>
  2213. <li>one</li>
  2214. </ul>
  2215. <p>two</p>
  2216. .
  2217. .
  2218. - one
  2219. two
  2220. .
  2221. <ul>
  2222. <li>
  2223. <p>one</p>
  2224. <p>two</p>
  2225. </li>
  2226. </ul>
  2227. .
  2228. .
  2229. - one
  2230. two
  2231. .
  2232. <ul>
  2233. <li>one</li>
  2234. </ul>
  2235. <pre><code> two
  2236. </code></pre>
  2237. .
  2238. .
  2239. - one
  2240. two
  2241. .
  2242. <ul>
  2243. <li>
  2244. <p>one</p>
  2245. <p>two</p>
  2246. </li>
  2247. </ul>
  2248. .
  2249. It is tempting to think of this in terms of columns: the continuation
  2250. blocks must be indented at least to the column of the first
  2251. [non-space character] after the list marker. However, that is not quite right.
  2252. The spaces after the list marker determine how much relative indentation
  2253. is needed. Which column this indentation reaches will depend on
  2254. how the list item is embedded in other constructions, as shown by
  2255. this example:
  2256. .
  2257. > > 1. one
  2258. >>
  2259. >> two
  2260. .
  2261. <blockquote>
  2262. <blockquote>
  2263. <ol>
  2264. <li>
  2265. <p>one</p>
  2266. <p>two</p>
  2267. </li>
  2268. </ol>
  2269. </blockquote>
  2270. </blockquote>
  2271. .
  2272. Here `two` occurs in the same column as the list marker `1.`,
  2273. but is actually contained in the list item, because there is
  2274. sufficient indentation after the last containing blockquote marker.
  2275. The converse is also possible. In the following example, the word `two`
  2276. occurs far to the right of the initial text of the list item, `one`, but
  2277. it is not considered part of the list item, because it is not indented
  2278. far enough past the blockquote marker:
  2279. .
  2280. >>- one
  2281. >>
  2282. > > two
  2283. .
  2284. <blockquote>
  2285. <blockquote>
  2286. <ul>
  2287. <li>one</li>
  2288. </ul>
  2289. <p>two</p>
  2290. </blockquote>
  2291. </blockquote>
  2292. .
  2293. Note that at least one space is needed between the list marker and
  2294. any following content, so these are not list items:
  2295. .
  2296. -one
  2297. 2.two
  2298. .
  2299. <p>-one</p>
  2300. <p>2.two</p>
  2301. .
  2302. A list item may not contain blocks that are separated by more than
  2303. one blank line. Thus, two blank lines will end a list, unless the
  2304. two blanks are contained in a [fenced code block].
  2305. .
  2306. - foo
  2307. bar
  2308. - foo
  2309. bar
  2310. - ```
  2311. foo
  2312. bar
  2313. ```
  2314. - baz
  2315. + ```
  2316. foo
  2317. bar
  2318. ```
  2319. .
  2320. <ul>
  2321. <li>
  2322. <p>foo</p>
  2323. <p>bar</p>
  2324. </li>
  2325. <li>
  2326. <p>foo</p>
  2327. </li>
  2328. </ul>
  2329. <p>bar</p>
  2330. <ul>
  2331. <li>
  2332. <pre><code>foo
  2333. bar
  2334. </code></pre>
  2335. </li>
  2336. <li>
  2337. <p>baz</p>
  2338. <ul>
  2339. <li>
  2340. <pre><code>foo
  2341. bar
  2342. </code></pre>
  2343. </li>
  2344. </ul>
  2345. </li>
  2346. </ul>
  2347. .
  2348. A list item may contain any kind of block:
  2349. .
  2350. 1. foo
  2351. ```
  2352. bar
  2353. ```
  2354. baz
  2355. > bam
  2356. .
  2357. <ol>
  2358. <li>
  2359. <p>foo</p>
  2360. <pre><code>bar
  2361. </code></pre>
  2362. <p>baz</p>
  2363. <blockquote>
  2364. <p>bam</p>
  2365. </blockquote>
  2366. </li>
  2367. </ol>
  2368. .
  2369. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  2370. constitute a sequence of blocks *Bs* starting with an indented code
  2371. block and not separated from each other by more than one blank line,
  2372. and *M* is a list marker of width *W* followed by
  2373. one space, then the result of prepending *M* and the following
  2374. space to the first line of *Ls*, and indenting subsequent lines of
  2375. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  2376. If a line is empty, then it need not be indented. The type of the
  2377. list item (bullet or ordered) is determined by the type of its list
  2378. marker. If the list item is ordered, then it is also assigned a
  2379. start number, based on the ordered list marker.
  2380. An indented code block will have to be indented four spaces beyond
  2381. the edge of the region where text will be included in the list item.
  2382. In the following case that is 6 spaces:
  2383. .
  2384. - foo
  2385. bar
  2386. .
  2387. <ul>
  2388. <li>
  2389. <p>foo</p>
  2390. <pre><code>bar
  2391. </code></pre>
  2392. </li>
  2393. </ul>
  2394. .
  2395. And in this case it is 11 spaces:
  2396. .
  2397. 10. foo
  2398. bar
  2399. .
  2400. <ol start="10">
  2401. <li>
  2402. <p>foo</p>
  2403. <pre><code>bar
  2404. </code></pre>
  2405. </li>
  2406. </ol>
  2407. .
  2408. If the *first* block in the list item is an indented code block,
  2409. then by rule #2, the contents must be indented *one* space after the
  2410. list marker:
  2411. .
  2412. indented code
  2413. paragraph
  2414. more code
  2415. .
  2416. <pre><code>indented code
  2417. </code></pre>
  2418. <p>paragraph</p>
  2419. <pre><code>more code
  2420. </code></pre>
  2421. .
  2422. .
  2423. 1. indented code
  2424. paragraph
  2425. more code
  2426. .
  2427. <ol>
  2428. <li>
  2429. <pre><code>indented code
  2430. </code></pre>
  2431. <p>paragraph</p>
  2432. <pre><code>more code
  2433. </code></pre>
  2434. </li>
  2435. </ol>
  2436. .
  2437. Note that an additional space indent is interpreted as space
  2438. inside the code block:
  2439. .
  2440. 1. indented code
  2441. paragraph
  2442. more code
  2443. .
  2444. <ol>
  2445. <li>
  2446. <pre><code> indented code
  2447. </code></pre>
  2448. <p>paragraph</p>
  2449. <pre><code>more code
  2450. </code></pre>
  2451. </li>
  2452. </ol>
  2453. .
  2454. Note that rules #1 and #2 only apply to two cases: (a) cases
  2455. in which the lines to be included in a list item begin with a
  2456. [non-space character], and (b) cases in which
  2457. they begin with an indented code
  2458. block. In a case like the following, where the first block begins with
  2459. a three-space indent, the rules do not allow us to form a list item by
  2460. indenting the whole thing and prepending a list marker:
  2461. .
  2462. foo
  2463. bar
  2464. .
  2465. <p>foo</p>
  2466. <p>bar</p>
  2467. .
  2468. .
  2469. - foo
  2470. bar
  2471. .
  2472. <ul>
  2473. <li>foo</li>
  2474. </ul>
  2475. <p>bar</p>
  2476. .
  2477. This is not a significant restriction, because when a block begins
  2478. with 1-3 spaces indent, the indentation can always be removed without
  2479. a change in interpretation, allowing rule #1 to be applied. So, in
  2480. the above case:
  2481. .
  2482. - foo
  2483. bar
  2484. .
  2485. <ul>
  2486. <li>
  2487. <p>foo</p>
  2488. <p>bar</p>
  2489. </li>
  2490. </ul>
  2491. .
  2492. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  2493. starting with a single [blank line] constitute a (possibly empty)
  2494. sequence of blocks *Bs*, not separated from each other by more than
  2495. one blank line, and *M* is a list marker of width *W*,
  2496. then the result of prepending *M* to the first line of *Ls*, and
  2497. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  2498. item with *Bs* as its contents.
  2499. If a line is empty, then it need not be indented. The type of the
  2500. list item (bullet or ordered) is determined by the type of its list
  2501. marker. If the list item is ordered, then it is also assigned a
  2502. start number, based on the ordered list marker.
  2503. Here are some list items that start with a blank line but are not empty:
  2504. .
  2505. -
  2506. foo
  2507. -
  2508. ```
  2509. bar
  2510. ```
  2511. -
  2512. baz
  2513. .
  2514. <ul>
  2515. <li>foo</li>
  2516. <li>
  2517. <pre><code>bar
  2518. </code></pre>
  2519. </li>
  2520. <li>
  2521. <pre><code>baz
  2522. </code></pre>
  2523. </li>
  2524. </ul>
  2525. .
  2526. Here is an empty bullet list item:
  2527. .
  2528. - foo
  2529. -
  2530. - bar
  2531. .
  2532. <ul>
  2533. <li>foo</li>
  2534. <li></li>
  2535. <li>bar</li>
  2536. </ul>
  2537. .
  2538. It does not matter whether there are spaces following the [list marker]:
  2539. .
  2540. - foo
  2541. -
  2542. - bar
  2543. .
  2544. <ul>
  2545. <li>foo</li>
  2546. <li></li>
  2547. <li>bar</li>
  2548. </ul>
  2549. .
  2550. Here is an empty ordered list item:
  2551. .
  2552. 1. foo
  2553. 2.
  2554. 3. bar
  2555. .
  2556. <ol>
  2557. <li>foo</li>
  2558. <li></li>
  2559. <li>bar</li>
  2560. </ol>
  2561. .
  2562. A list may start or end with an empty list item:
  2563. .
  2564. *
  2565. .
  2566. <ul>
  2567. <li></li>
  2568. </ul>
  2569. .
  2570. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  2571. according to rule #1, #2, or #3, then the result of indenting each line
  2572. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  2573. list item with the same contents and attributes. If a line is
  2574. empty, then it need not be indented.
  2575. Indented one space:
  2576. .
  2577. 1. A paragraph
  2578. with two lines.
  2579. indented code
  2580. > A block quote.
  2581. .
  2582. <ol>
  2583. <li>
  2584. <p>A paragraph
  2585. with two lines.</p>
  2586. <pre><code>indented code
  2587. </code></pre>
  2588. <blockquote>
  2589. <p>A block quote.</p>
  2590. </blockquote>
  2591. </li>
  2592. </ol>
  2593. .
  2594. Indented two spaces:
  2595. .
  2596. 1. A paragraph
  2597. with two lines.
  2598. indented code
  2599. > A block quote.
  2600. .
  2601. <ol>
  2602. <li>
  2603. <p>A paragraph
  2604. with two lines.</p>
  2605. <pre><code>indented code
  2606. </code></pre>
  2607. <blockquote>
  2608. <p>A block quote.</p>
  2609. </blockquote>
  2610. </li>
  2611. </ol>
  2612. .
  2613. Indented three spaces:
  2614. .
  2615. 1. A paragraph
  2616. with two lines.
  2617. indented code
  2618. > A block quote.
  2619. .
  2620. <ol>
  2621. <li>
  2622. <p>A paragraph
  2623. with two lines.</p>
  2624. <pre><code>indented code
  2625. </code></pre>
  2626. <blockquote>
  2627. <p>A block quote.</p>
  2628. </blockquote>
  2629. </li>
  2630. </ol>
  2631. .
  2632. Four spaces indent gives a code block:
  2633. .
  2634. 1. A paragraph
  2635. with two lines.
  2636. indented code
  2637. > A block quote.
  2638. .
  2639. <pre><code>1. A paragraph
  2640. with two lines.
  2641. indented code
  2642. &gt; A block quote.
  2643. </code></pre>
  2644. .
  2645. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  2646. item](#list-items) with contents *Bs*, then the result of deleting
  2647. some or all of the indentation from one or more lines in which the
  2648. next [non-space character] after the indentation is
  2649. [paragraph continuation text] is a
  2650. list item with the same contents and attributes. The unindented
  2651. lines are called
  2652. [lazy continuation line](@lazy-continuation-line)s.
  2653. Here is an example with [lazy continuation line]s:
  2654. .
  2655. 1. A paragraph
  2656. with two lines.
  2657. indented code
  2658. > A block quote.
  2659. .
  2660. <ol>
  2661. <li>
  2662. <p>A paragraph
  2663. with two lines.</p>
  2664. <pre><code>indented code
  2665. </code></pre>
  2666. <blockquote>
  2667. <p>A block quote.</p>
  2668. </blockquote>
  2669. </li>
  2670. </ol>
  2671. .
  2672. Indentation can be partially deleted:
  2673. .
  2674. 1. A paragraph
  2675. with two lines.
  2676. .
  2677. <ol>
  2678. <li>A paragraph
  2679. with two lines.</li>
  2680. </ol>
  2681. .
  2682. These examples show how laziness can work in nested structures:
  2683. .
  2684. > 1. > Blockquote
  2685. continued here.
  2686. .
  2687. <blockquote>
  2688. <ol>
  2689. <li>
  2690. <blockquote>
  2691. <p>Blockquote
  2692. continued here.</p>
  2693. </blockquote>
  2694. </li>
  2695. </ol>
  2696. </blockquote>
  2697. .
  2698. .
  2699. > 1. > Blockquote
  2700. > continued here.
  2701. .
  2702. <blockquote>
  2703. <ol>
  2704. <li>
  2705. <blockquote>
  2706. <p>Blockquote
  2707. continued here.</p>
  2708. </blockquote>
  2709. </li>
  2710. </ol>
  2711. </blockquote>
  2712. .
  2713. 6. **That's all.** Nothing that is not counted as a list item by rules
  2714. #1--5 counts as a [list item](#list-items).
  2715. The rules for sublists follow from the general rules above. A sublist
  2716. must be indented the same number of spaces a paragraph would need to be
  2717. in order to be included in the list item.
  2718. So, in this case we need two spaces indent:
  2719. .
  2720. - foo
  2721. - bar
  2722. - baz
  2723. .
  2724. <ul>
  2725. <li>foo
  2726. <ul>
  2727. <li>bar
  2728. <ul>
  2729. <li>baz</li>
  2730. </ul>
  2731. </li>
  2732. </ul>
  2733. </li>
  2734. </ul>
  2735. .
  2736. One is not enough:
  2737. .
  2738. - foo
  2739. - bar
  2740. - baz
  2741. .
  2742. <ul>
  2743. <li>foo</li>
  2744. <li>bar</li>
  2745. <li>baz</li>
  2746. </ul>
  2747. .
  2748. Here we need four, because the list marker is wider:
  2749. .
  2750. 10) foo
  2751. - bar
  2752. .
  2753. <ol start="10">
  2754. <li>foo
  2755. <ul>
  2756. <li>bar</li>
  2757. </ul>
  2758. </li>
  2759. </ol>
  2760. .
  2761. Three is not enough:
  2762. .
  2763. 10) foo
  2764. - bar
  2765. .
  2766. <ol start="10">
  2767. <li>foo</li>
  2768. </ol>
  2769. <ul>
  2770. <li>bar</li>
  2771. </ul>
  2772. .
  2773. A list may be the first block in a list item:
  2774. .
  2775. - - foo
  2776. .
  2777. <ul>
  2778. <li>
  2779. <ul>
  2780. <li>foo</li>
  2781. </ul>
  2782. </li>
  2783. </ul>
  2784. .
  2785. .
  2786. 1. - 2. foo
  2787. .
  2788. <ol>
  2789. <li>
  2790. <ul>
  2791. <li>
  2792. <ol start="2">
  2793. <li>foo</li>
  2794. </ol>
  2795. </li>
  2796. </ul>
  2797. </li>
  2798. </ol>
  2799. .
  2800. A list item can contain a header:
  2801. .
  2802. - # Foo
  2803. - Bar
  2804. ---
  2805. baz
  2806. .
  2807. <ul>
  2808. <li>
  2809. <h1>Foo</h1>
  2810. </li>
  2811. <li>
  2812. <h2>Bar</h2>
  2813. baz</li>
  2814. </ul>
  2815. .
  2816. ### Motivation
  2817. John Gruber's Markdown spec says the following about list items:
  2818. 1. "List markers typically start at the left margin, but may be indented
  2819. by up to three spaces. List markers must be followed by one or more
  2820. spaces or a tab."
  2821. 2. "To make lists look nice, you can wrap items with hanging indents....
  2822. But if you don't want to, you don't have to."
  2823. 3. "List items may consist of multiple paragraphs. Each subsequent
  2824. paragraph in a list item must be indented by either 4 spaces or one
  2825. tab."
  2826. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  2827. but here again, Markdown will allow you to be lazy."
  2828. 5. "To put a blockquote within a list item, the blockquote's `>`
  2829. delimiters need to be indented."
  2830. 6. "To put a code block within a list item, the code block needs to be
  2831. indented twice — 8 spaces or two tabs."
  2832. These rules specify that a paragraph under a list item must be indented
  2833. four spaces (presumably, from the left margin, rather than the start of
  2834. the list marker, but this is not said), and that code under a list item
  2835. must be indented eight spaces instead of the usual four. They also say
  2836. that a block quote must be indented, but not by how much; however, the
  2837. example given has four spaces indentation. Although nothing is said
  2838. about other kinds of block-level content, it is certainly reasonable to
  2839. infer that *all* block elements under a list item, including other
  2840. lists, must be indented four spaces. This principle has been called the
  2841. *four-space rule*.
  2842. The four-space rule is clear and principled, and if the reference
  2843. implementation `Markdown.pl` had followed it, it probably would have
  2844. become the standard. However, `Markdown.pl` allowed paragraphs and
  2845. sublists to start with only two spaces indentation, at least on the
  2846. outer level. Worse, its behavior was inconsistent: a sublist of an
  2847. outer-level list needed two spaces indentation, but a sublist of this
  2848. sublist needed three spaces. It is not surprising, then, that different
  2849. implementations of Markdown have developed very different rules for
  2850. determining what comes under a list item. (Pandoc and python-Markdown,
  2851. for example, stuck with Gruber's syntax description and the four-space
  2852. rule, while discount, redcarpet, marked, PHP Markdown, and others
  2853. followed `Markdown.pl`'s behavior more closely.)
  2854. Unfortunately, given the divergences between implementations, there
  2855. is no way to give a spec for list items that will be guaranteed not
  2856. to break any existing documents. However, the spec given here should
  2857. correctly handle lists formatted with either the four-space rule or
  2858. the more forgiving `Markdown.pl` behavior, provided they are laid out
  2859. in a way that is natural for a human to read.
  2860. The strategy here is to let the width and indentation of the list marker
  2861. determine the indentation necessary for blocks to fall under the list
  2862. item, rather than having a fixed and arbitrary number. The writer can
  2863. think of the body of the list item as a unit which gets indented to the
  2864. right enough to fit the list marker (and any indentation on the list
  2865. marker). (The laziness rule, #5, then allows continuation lines to be
  2866. unindented if needed.)
  2867. This rule is superior, we claim, to any rule requiring a fixed level of
  2868. indentation from the margin. The four-space rule is clear but
  2869. unnatural. It is quite unintuitive that
  2870. ``` markdown
  2871. - foo
  2872. bar
  2873. - baz
  2874. ```
  2875. should be parsed as two lists with an intervening paragraph,
  2876. ``` html
  2877. <ul>
  2878. <li>foo</li>
  2879. </ul>
  2880. <p>bar</p>
  2881. <ul>
  2882. <li>baz</li>
  2883. </ul>
  2884. ```
  2885. as the four-space rule demands, rather than a single list,
  2886. ``` html
  2887. <ul>
  2888. <li>
  2889. <p>foo</p>
  2890. <p>bar</p>
  2891. <ul>
  2892. <li>baz</li>
  2893. </ul>
  2894. </li>
  2895. </ul>
  2896. ```
  2897. The choice of four spaces is arbitrary. It can be learned, but it is
  2898. not likely to be guessed, and it trips up beginners regularly.
  2899. Would it help to adopt a two-space rule? The problem is that such
  2900. a rule, together with the rule allowing 1--3 spaces indentation of the
  2901. initial list marker, allows text that is indented *less than* the
  2902. original list marker to be included in the list item. For example,
  2903. `Markdown.pl` parses
  2904. ``` markdown
  2905. - one
  2906. two
  2907. ```
  2908. as a single list item, with `two` a continuation paragraph:
  2909. ``` html
  2910. <ul>
  2911. <li>
  2912. <p>one</p>
  2913. <p>two</p>
  2914. </li>
  2915. </ul>
  2916. ```
  2917. and similarly
  2918. ``` markdown
  2919. > - one
  2920. >
  2921. > two
  2922. ```
  2923. as
  2924. ``` html
  2925. <blockquote>
  2926. <ul>
  2927. <li>
  2928. <p>one</p>
  2929. <p>two</p>
  2930. </li>
  2931. </ul>
  2932. </blockquote>
  2933. ```
  2934. This is extremely unintuitive.
  2935. Rather than requiring a fixed indent from the margin, we could require
  2936. a fixed indent (say, two spaces, or even one space) from the list marker (which
  2937. may itself be indented). This proposal would remove the last anomaly
  2938. discussed. Unlike the spec presented above, it would count the following
  2939. as a list item with a subparagraph, even though the paragraph `bar`
  2940. is not indented as far as the first paragraph `foo`:
  2941. ``` markdown
  2942. 10. foo
  2943. bar
  2944. ```
  2945. Arguably this text does read like a list item with `bar` as a subparagraph,
  2946. which may count in favor of the proposal. However, on this proposal indented
  2947. code would have to be indented six spaces after the list marker. And this
  2948. would break a lot of existing Markdown, which has the pattern:
  2949. ``` markdown
  2950. 1. foo
  2951. indented code
  2952. ```
  2953. where the code is indented eight spaces. The spec above, by contrast, will
  2954. parse this text as expected, since the code block's indentation is measured
  2955. from the beginning of `foo`.
  2956. The one case that needs special treatment is a list item that *starts*
  2957. with indented code. How much indentation is required in that case, since
  2958. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  2959. that in such cases, we require one space indentation from the list marker
  2960. (and then the normal four spaces for the indented code). This will match the
  2961. four-space rule in cases where the list marker plus its initial indentation
  2962. takes four spaces (a common case), but diverge in other cases.
  2963. ## Lists
  2964. A [list](@list) is a sequence of one or more
  2965. list items [of the same type]. The list items
  2966. may be separated by single [blank lines], but two
  2967. blank lines end all containing lists.
  2968. Two list items are [of the same type](@of-the-same-type)
  2969. if they begin with a [list marker] of the same type.
  2970. Two list markers are of the
  2971. same type if (a) they are bullet list markers using the same character
  2972. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  2973. delimiter (either `.` or `)`).
  2974. A list is an [ordered list](@ordered-list)
  2975. if its constituent list items begin with
  2976. [ordered list marker]s, and a
  2977. [bullet list](@bullet-list) if its constituent list
  2978. items begin with [bullet list marker]s.
  2979. The [start number](@start-number)
  2980. of an [ordered list] is determined by the list number of
  2981. its initial list item. The numbers of subsequent list items are
  2982. disregarded.
  2983. A list is [loose](@loose) if any of its constituent
  2984. list items are separated by blank lines, or if any of its constituent
  2985. list items directly contain two block-level elements with a blank line
  2986. between them. Otherwise a list is [tight](@tight).
  2987. (The difference in HTML output is that paragraphs in a loose list are
  2988. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  2989. Changing the bullet or ordered list delimiter starts a new list:
  2990. .
  2991. - foo
  2992. - bar
  2993. + baz
  2994. .
  2995. <ul>
  2996. <li>foo</li>
  2997. <li>bar</li>
  2998. </ul>
  2999. <ul>
  3000. <li>baz</li>
  3001. </ul>
  3002. .
  3003. .
  3004. 1. foo
  3005. 2. bar
  3006. 3) baz
  3007. .
  3008. <ol>
  3009. <li>foo</li>
  3010. <li>bar</li>
  3011. </ol>
  3012. <ol start="3">
  3013. <li>baz</li>
  3014. </ol>
  3015. .
  3016. In CommonMark, a list can interrupt a paragraph. That is,
  3017. no blank line is needed to separate a paragraph from a following
  3018. list:
  3019. .
  3020. Foo
  3021. - bar
  3022. - baz
  3023. .
  3024. <p>Foo</p>
  3025. <ul>
  3026. <li>bar</li>
  3027. <li>baz</li>
  3028. </ul>
  3029. .
  3030. `Markdown.pl` does not allow this, through fear of triggering a list
  3031. via a numeral in a hard-wrapped line:
  3032. .
  3033. The number of windows in my house is
  3034. 14. The number of doors is 6.
  3035. .
  3036. <p>The number of windows in my house is</p>
  3037. <ol start="14">
  3038. <li>The number of doors is 6.</li>
  3039. </ol>
  3040. .
  3041. Oddly, `Markdown.pl` *does* allow a blockquote to interrupt a paragraph,
  3042. even though the same considerations might apply. We think that the two
  3043. cases should be treated the same. Here are two reasons for allowing
  3044. lists to interrupt paragraphs:
  3045. First, it is natural and not uncommon for people to start lists without
  3046. blank lines:
  3047. I need to buy
  3048. - new shoes
  3049. - a coat
  3050. - a plane ticket
  3051. Second, we are attracted to a
  3052. > [principle of uniformity](@principle-of-uniformity):
  3053. > if a chunk of text has a certain
  3054. > meaning, it will continue to have the same meaning when put into a
  3055. > container block (such as a list item or blockquote).
  3056. (Indeed, the spec for [list items] and [block quotes] presupposes
  3057. this principle.) This principle implies that if
  3058. * I need to buy
  3059. - new shoes
  3060. - a coat
  3061. - a plane ticket
  3062. is a list item containing a paragraph followed by a nested sublist,
  3063. as all Markdown implementations agree it is (though the paragraph
  3064. may be rendered without `<p>` tags, since the list is "tight"),
  3065. then
  3066. I need to buy
  3067. - new shoes
  3068. - a coat
  3069. - a plane ticket
  3070. by itself should be a paragraph followed by a nested sublist.
  3071. Our adherence to the [principle of uniformity]
  3072. thus inclines us to think that there are two coherent packages:
  3073. 1. Require blank lines before *all* lists and blockquotes,
  3074. including lists that occur as sublists inside other list items.
  3075. 2. Require blank lines in none of these places.
  3076. [reStructuredText](http://docutils.sourceforge.net/rst.html) takes
  3077. the first approach, for which there is much to be said. But the second
  3078. seems more consistent with established practice with Markdown.
  3079. There can be blank lines between items, but two blank lines end
  3080. a list:
  3081. .
  3082. - foo
  3083. - bar
  3084. - baz
  3085. .
  3086. <ul>
  3087. <li>
  3088. <p>foo</p>
  3089. </li>
  3090. <li>
  3091. <p>bar</p>
  3092. </li>
  3093. </ul>
  3094. <ul>
  3095. <li>baz</li>
  3096. </ul>
  3097. .
  3098. As illustrated above in the section on [list items],
  3099. two blank lines between blocks *within* a list item will also end a
  3100. list:
  3101. .
  3102. - foo
  3103. bar
  3104. - baz
  3105. .
  3106. <ul>
  3107. <li>foo</li>
  3108. </ul>
  3109. <p>bar</p>
  3110. <ul>
  3111. <li>baz</li>
  3112. </ul>
  3113. .
  3114. Indeed, two blank lines will end *all* containing lists:
  3115. .
  3116. - foo
  3117. - bar
  3118. - baz
  3119. bim
  3120. .
  3121. <ul>
  3122. <li>foo
  3123. <ul>
  3124. <li>bar
  3125. <ul>
  3126. <li>baz</li>
  3127. </ul>
  3128. </li>
  3129. </ul>
  3130. </li>
  3131. </ul>
  3132. <pre><code> bim
  3133. </code></pre>
  3134. .
  3135. Thus, two blank lines can be used to separate consecutive lists of
  3136. the same type, or to separate a list from an indented code block
  3137. that would otherwise be parsed as a subparagraph of the final list
  3138. item:
  3139. .
  3140. - foo
  3141. - bar
  3142. - baz
  3143. - bim
  3144. .
  3145. <ul>
  3146. <li>foo</li>
  3147. <li>bar</li>
  3148. </ul>
  3149. <ul>
  3150. <li>baz</li>
  3151. <li>bim</li>
  3152. </ul>
  3153. .
  3154. .
  3155. - foo
  3156. notcode
  3157. - foo
  3158. code
  3159. .
  3160. <ul>
  3161. <li>
  3162. <p>foo</p>
  3163. <p>notcode</p>
  3164. </li>
  3165. <li>
  3166. <p>foo</p>
  3167. </li>
  3168. </ul>
  3169. <pre><code>code
  3170. </code></pre>
  3171. .
  3172. List items need not be indented to the same level. The following
  3173. list items will be treated as items at the same list level,
  3174. since none is indented enough to belong to the previous list
  3175. item:
  3176. .
  3177. - a
  3178. - b
  3179. - c
  3180. - d
  3181. - e
  3182. - f
  3183. - g
  3184. - h
  3185. - i
  3186. .
  3187. <ul>
  3188. <li>a</li>
  3189. <li>b</li>
  3190. <li>c</li>
  3191. <li>d</li>
  3192. <li>e</li>
  3193. <li>f</li>
  3194. <li>g</li>
  3195. <li>h</li>
  3196. <li>i</li>
  3197. </ul>
  3198. .
  3199. .
  3200. 1. a
  3201. 2. b
  3202. 3. c
  3203. .
  3204. <ol>
  3205. <li>
  3206. <p>a</p>
  3207. </li>
  3208. <li>
  3209. <p>b</p>
  3210. </li>
  3211. <li>
  3212. <p>c</p>
  3213. </li>
  3214. </ol>
  3215. .
  3216. This is a loose list, because there is a blank line between
  3217. two of the list items:
  3218. .
  3219. - a
  3220. - b
  3221. - c
  3222. .
  3223. <ul>
  3224. <li>
  3225. <p>a</p>
  3226. </li>
  3227. <li>
  3228. <p>b</p>
  3229. </li>
  3230. <li>
  3231. <p>c</p>
  3232. </li>
  3233. </ul>
  3234. .
  3235. So is this, with a empty second item:
  3236. .
  3237. * a
  3238. *
  3239. * c
  3240. .
  3241. <ul>
  3242. <li>
  3243. <p>a</p>
  3244. </li>
  3245. <li></li>
  3246. <li>
  3247. <p>c</p>
  3248. </li>
  3249. </ul>
  3250. .
  3251. These are loose lists, even though there is no space between the items,
  3252. because one of the items directly contains two block-level elements
  3253. with a blank line between them:
  3254. .
  3255. - a
  3256. - b
  3257. c
  3258. - d
  3259. .
  3260. <ul>
  3261. <li>
  3262. <p>a</p>
  3263. </li>
  3264. <li>
  3265. <p>b</p>
  3266. <p>c</p>
  3267. </li>
  3268. <li>
  3269. <p>d</p>
  3270. </li>
  3271. </ul>
  3272. .
  3273. .
  3274. - a
  3275. - b
  3276. [ref]: /url
  3277. - d
  3278. .
  3279. <ul>
  3280. <li>
  3281. <p>a</p>
  3282. </li>
  3283. <li>
  3284. <p>b</p>
  3285. </li>
  3286. <li>
  3287. <p>d</p>
  3288. </li>
  3289. </ul>
  3290. .
  3291. This is a tight list, because the blank lines are in a code block:
  3292. .
  3293. - a
  3294. - ```
  3295. b
  3296. ```
  3297. - c
  3298. .
  3299. <ul>
  3300. <li>a</li>
  3301. <li>
  3302. <pre><code>b
  3303. </code></pre>
  3304. </li>
  3305. <li>c</li>
  3306. </ul>
  3307. .
  3308. This is a tight list, because the blank line is between two
  3309. paragraphs of a sublist. So the sublist is loose while
  3310. the outer list is tight:
  3311. .
  3312. - a
  3313. - b
  3314. c
  3315. - d
  3316. .
  3317. <ul>
  3318. <li>a
  3319. <ul>
  3320. <li>
  3321. <p>b</p>
  3322. <p>c</p>
  3323. </li>
  3324. </ul>
  3325. </li>
  3326. <li>d</li>
  3327. </ul>
  3328. .
  3329. This is a tight list, because the blank line is inside the
  3330. block quote:
  3331. .
  3332. * a
  3333. > b
  3334. >
  3335. * c
  3336. .
  3337. <ul>
  3338. <li>a
  3339. <blockquote>
  3340. <p>b</p>
  3341. </blockquote>
  3342. </li>
  3343. <li>c</li>
  3344. </ul>
  3345. .
  3346. This list is tight, because the consecutive block elements
  3347. are not separated by blank lines:
  3348. .
  3349. - a
  3350. > b
  3351. ```
  3352. c
  3353. ```
  3354. - d
  3355. .
  3356. <ul>
  3357. <li>a
  3358. <blockquote>
  3359. <p>b</p>
  3360. </blockquote>
  3361. <pre><code>c
  3362. </code></pre>
  3363. </li>
  3364. <li>d</li>
  3365. </ul>
  3366. .
  3367. A single-paragraph list is tight:
  3368. .
  3369. - a
  3370. .
  3371. <ul>
  3372. <li>a</li>
  3373. </ul>
  3374. .
  3375. .
  3376. - a
  3377. - b
  3378. .
  3379. <ul>
  3380. <li>a
  3381. <ul>
  3382. <li>b</li>
  3383. </ul>
  3384. </li>
  3385. </ul>
  3386. .
  3387. This list is loose, because of the blank line between the
  3388. two block elements in the list item:
  3389. .
  3390. 1. ```
  3391. foo
  3392. ```
  3393. bar
  3394. .
  3395. <ol>
  3396. <li>
  3397. <pre><code>foo
  3398. </code></pre>
  3399. <p>bar</p>
  3400. </li>
  3401. </ol>
  3402. .
  3403. Here the outer list is loose, the inner list tight:
  3404. .
  3405. * foo
  3406. * bar
  3407. baz
  3408. .
  3409. <ul>
  3410. <li>
  3411. <p>foo</p>
  3412. <ul>
  3413. <li>bar</li>
  3414. </ul>
  3415. <p>baz</p>
  3416. </li>
  3417. </ul>
  3418. .
  3419. .
  3420. - a
  3421. - b
  3422. - c
  3423. - d
  3424. - e
  3425. - f
  3426. .
  3427. <ul>
  3428. <li>
  3429. <p>a</p>
  3430. <ul>
  3431. <li>b</li>
  3432. <li>c</li>
  3433. </ul>
  3434. </li>
  3435. <li>
  3436. <p>d</p>
  3437. <ul>
  3438. <li>e</li>
  3439. <li>f</li>
  3440. </ul>
  3441. </li>
  3442. </ul>
  3443. .
  3444. # Inlines
  3445. Inlines are parsed sequentially from the beginning of the character
  3446. stream to the end (left to right, in left-to-right languages).
  3447. Thus, for example, in
  3448. .
  3449. `hi`lo`
  3450. .
  3451. <p><code>hi</code>lo`</p>
  3452. .
  3453. `hi` is parsed as code, leaving the backtick at the end as a literal
  3454. backtick.
  3455. ## Backslash escapes
  3456. Any ASCII punctuation character may be backslash-escaped:
  3457. .
  3458. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  3459. .
  3460. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  3461. .
  3462. Backslashes before other characters are treated as literal
  3463. backslashes:
  3464. .
  3465. \→\A\a\ \3\φ\«
  3466. .
  3467. <p>\ \A\a\ \3\φ\«</p>
  3468. .
  3469. Escaped characters are treated as regular characters and do
  3470. not have their usual Markdown meanings:
  3471. .
  3472. \*not emphasized*
  3473. \<br/> not a tag
  3474. \[not a link](/foo)
  3475. \`not code`
  3476. 1\. not a list
  3477. \* not a list
  3478. \# not a header
  3479. \[foo]: /url "not a reference"
  3480. .
  3481. <p>*not emphasized*
  3482. &lt;br/&gt; not a tag
  3483. [not a link](/foo)
  3484. `not code`
  3485. 1. not a list
  3486. * not a list
  3487. # not a header
  3488. [foo]: /url &quot;not a reference&quot;</p>
  3489. .
  3490. If a backslash is itself escaped, the following character is not:
  3491. .
  3492. \\*emphasis*
  3493. .
  3494. <p>\<em>emphasis</em></p>
  3495. .
  3496. A backslash at the end of the line is a [hard line break]:
  3497. .
  3498. foo\
  3499. bar
  3500. .
  3501. <p>foo<br />
  3502. bar</p>
  3503. .
  3504. Backslash escapes do not work in code blocks, code spans, autolinks, or
  3505. raw HTML:
  3506. .
  3507. `` \[\` ``
  3508. .
  3509. <p><code>\[\`</code></p>
  3510. .
  3511. .
  3512. \[\]
  3513. .
  3514. <pre><code>\[\]
  3515. </code></pre>
  3516. .
  3517. .
  3518. ~~~
  3519. \[\]
  3520. ~~~
  3521. .
  3522. <pre><code>\[\]
  3523. </code></pre>
  3524. .
  3525. .
  3526. <http://example.com?find=\*>
  3527. .
  3528. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  3529. .
  3530. .
  3531. <a href="/bar\/)">
  3532. .
  3533. <p><a href="/bar\/)"></p>
  3534. .
  3535. But they work in all other contexts, including URLs and link titles,
  3536. link references, and [info string]s in [fenced code block]s:
  3537. .
  3538. [foo](/bar\* "ti\*tle")
  3539. .
  3540. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3541. .
  3542. .
  3543. [foo]
  3544. [foo]: /bar\* "ti\*tle"
  3545. .
  3546. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3547. .
  3548. .
  3549. ``` foo\+bar
  3550. foo
  3551. ```
  3552. .
  3553. <pre><code class="language-foo+bar">foo
  3554. </code></pre>
  3555. .
  3556. ## Entities
  3557. With the goal of making this standard as HTML-agnostic as possible, all
  3558. valid HTML entities (except in code blocks and code spans)
  3559. are recognized as such and converted into unicode characters before
  3560. they are stored in the AST. This means that renderers to formats other
  3561. than HTML need not be HTML-entity aware. HTML renderers may either escape
  3562. unicode characters as entities or leave them as they are. (However,
  3563. `"`, `&`, `<`, and `>` must always be rendered as entities.)
  3564. [Named entities](@name-entities) consist of `&`
  3565. + any of the valid HTML5 entity names + `;`. The
  3566. [following document](https://html.spec.whatwg.org/multipage/entities.json)
  3567. is used as an authoritative source of the valid entity names and their
  3568. corresponding codepoints.
  3569. .
  3570. &nbsp; &amp; &copy; &AElig; &Dcaron;
  3571. &frac34; &HilbertSpace; &DifferentialD;
  3572. &ClockwiseContourIntegral; &ngE;
  3573. .
  3574. <p>  &amp; © Æ Ď
  3575. ¾ ℋ ⅆ
  3576. ∲ ≧̸</p>
  3577. .
  3578. [Decimal entities](@decimal-entities)
  3579. consist of `&#` + a string of 1--8 arabic digits + `;`. Again, these
  3580. entities need to be recognised and transformed into their corresponding
  3581. unicode codepoints. Invalid unicode codepoints will be replaced by
  3582. the "unknown codepoint" character (`U+FFFD`). For security reasons,
  3583. the codepoint `U+0000` will also be replaced by `U+FFFD`.
  3584. .
  3585. &#35; &#1234; &#992; &#98765432; &#0;
  3586. .
  3587. <p># Ӓ Ϡ � �</p>
  3588. .
  3589. [Hexadecimal entities](@hexadecimal-entities)
  3590. consist of `&#` + either `X` or `x` + a string of 1-8 hexadecimal digits
  3591. + `;`. They will also be parsed and turned into the corresponding
  3592. unicode codepoints in the AST.
  3593. .
  3594. &#X22; &#XD06; &#xcab;
  3595. .
  3596. <p>&quot; ആ ಫ</p>
  3597. .
  3598. Here are some nonentities:
  3599. .
  3600. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3601. .
  3602. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3603. .
  3604. Although HTML5 does accept some entities without a trailing semicolon
  3605. (such as `&copy`), these are not recognized as entities here, because it
  3606. makes the grammar too ambiguous:
  3607. .
  3608. &copy
  3609. .
  3610. <p>&amp;copy</p>
  3611. .
  3612. Strings that are not on the list of HTML5 named entities are not
  3613. recognized as entities either:
  3614. .
  3615. &MadeUpEntity;
  3616. .
  3617. <p>&amp;MadeUpEntity;</p>
  3618. .
  3619. Entities are recognized in any context besides code spans or
  3620. code blocks, including raw HTML, URLs, [link title]s, and
  3621. [fenced code block] [info string]s:
  3622. .
  3623. <a href="&ouml;&ouml;.html">
  3624. .
  3625. <p><a href="&ouml;&ouml;.html"></p>
  3626. .
  3627. .
  3628. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3629. .
  3630. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3631. .
  3632. .
  3633. [foo]
  3634. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3635. .
  3636. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3637. .
  3638. .
  3639. ``` f&ouml;&ouml;
  3640. foo
  3641. ```
  3642. .
  3643. <pre><code class="language-föö">foo
  3644. </code></pre>
  3645. .
  3646. Entities are treated as literal text in code spans and code blocks:
  3647. .
  3648. `f&ouml;&ouml;`
  3649. .
  3650. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3651. .
  3652. .
  3653. f&ouml;f&ouml;
  3654. .
  3655. <pre><code>f&amp;ouml;f&amp;ouml;
  3656. </code></pre>
  3657. .
  3658. ## Code spans
  3659. A [backtick string](@backtick-string)
  3660. is a string of one or more backtick characters (`` ` ``) that is neither
  3661. preceded nor followed by a backtick.
  3662. A [code span](@code-span) begins with a backtick string and ends with
  3663. a backtick string of equal length. The contents of the code span are
  3664. the characters between the two backtick strings, with leading and
  3665. trailing spaces and [line ending]s removed, and
  3666. [whitespace] collapsed to single spaces.
  3667. This is a simple code span:
  3668. .
  3669. `foo`
  3670. .
  3671. <p><code>foo</code></p>
  3672. .
  3673. Here two backticks are used, because the code contains a backtick.
  3674. This example also illustrates stripping of leading and trailing spaces:
  3675. .
  3676. `` foo ` bar ``
  3677. .
  3678. <p><code>foo ` bar</code></p>
  3679. .
  3680. This example shows the motivation for stripping leading and trailing
  3681. spaces:
  3682. .
  3683. ` `` `
  3684. .
  3685. <p><code>``</code></p>
  3686. .
  3687. [Line ending]s are treated like spaces:
  3688. .
  3689. ``
  3690. foo
  3691. ``
  3692. .
  3693. <p><code>foo</code></p>
  3694. .
  3695. Interior spaces and [line ending]s are collapsed into
  3696. single spaces, just as they would be by a browser:
  3697. .
  3698. `foo bar
  3699. baz`
  3700. .
  3701. <p><code>foo bar baz</code></p>
  3702. .
  3703. Q: Why not just leave the spaces, since browsers will collapse them
  3704. anyway? A: Because we might be targeting a non-HTML format, and we
  3705. shouldn't rely on HTML-specific rendering assumptions.
  3706. (Existing implementations differ in their treatment of internal
  3707. spaces and [line ending]s. Some, including `Markdown.pl` and
  3708. `showdown`, convert an internal [line ending] into a
  3709. `<br />` tag. But this makes things difficult for those who like to
  3710. hard-wrap their paragraphs, since a line break in the midst of a code
  3711. span will cause an unintended line break in the output. Others just
  3712. leave internal spaces as they are, which is fine if only HTML is being
  3713. targeted.)
  3714. .
  3715. `foo `` bar`
  3716. .
  3717. <p><code>foo `` bar</code></p>
  3718. .
  3719. Note that backslash escapes do not work in code spans. All backslashes
  3720. are treated literally:
  3721. .
  3722. `foo\`bar`
  3723. .
  3724. <p><code>foo\</code>bar`</p>
  3725. .
  3726. Backslash escapes are never needed, because one can always choose a
  3727. string of *n* backtick characters as delimiters, where the code does
  3728. not contain any strings of exactly *n* backtick characters.
  3729. Code span backticks have higher precedence than any other inline
  3730. constructs except HTML tags and autolinks. Thus, for example, this is
  3731. not parsed as emphasized text, since the second `*` is part of a code
  3732. span:
  3733. .
  3734. *foo`*`
  3735. .
  3736. <p>*foo<code>*</code></p>
  3737. .
  3738. And this is not parsed as a link:
  3739. .
  3740. [not a `link](/foo`)
  3741. .
  3742. <p>[not a <code>link](/foo</code>)</p>
  3743. .
  3744. Code spans, HTML tags, and autolinks have the same precedence.
  3745. Thus, this is code:
  3746. .
  3747. `<a href="`">`
  3748. .
  3749. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  3750. .
  3751. But this is an HTML tag:
  3752. .
  3753. <a href="`">`
  3754. .
  3755. <p><a href="`">`</p>
  3756. .
  3757. And this is code:
  3758. .
  3759. `<http://foo.bar.`baz>`
  3760. .
  3761. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  3762. .
  3763. But this is an autolink:
  3764. .
  3765. <http://foo.bar.`baz>`
  3766. .
  3767. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3768. .
  3769. When a backtick string is not closed by a matching backtick string,
  3770. we just have literal backticks:
  3771. .
  3772. ```foo``
  3773. .
  3774. <p>```foo``</p>
  3775. .
  3776. .
  3777. `foo
  3778. .
  3779. <p>`foo</p>
  3780. .
  3781. ## Emphasis and strong emphasis
  3782. John Gruber's original [Markdown syntax
  3783. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3784. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3785. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3786. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3787. > tag.
  3788. This is enough for most users, but these rules leave much undecided,
  3789. especially when it comes to nested emphasis. The original
  3790. `Markdown.pl` test suite makes it clear that triple `***` and
  3791. `___` delimiters can be used for strong emphasis, and most
  3792. implementations have also allowed the following patterns:
  3793. ``` markdown
  3794. ***strong emph***
  3795. ***strong** in emph*
  3796. ***emph* in strong**
  3797. **in strong *emph***
  3798. *in emph **strong***
  3799. ```
  3800. The following patterns are less widely supported, but the intent
  3801. is clear and they are useful (especially in contexts like bibliography
  3802. entries):
  3803. ``` markdown
  3804. *emph *with emph* in it*
  3805. **strong **with strong** in it**
  3806. ```
  3807. Many implementations have also restricted intraword emphasis to
  3808. the `*` forms, to avoid unwanted emphasis in words containing
  3809. internal underscores. (It is best practice to put these in code
  3810. spans, but users often do not.)
  3811. ``` markdown
  3812. internal emphasis: foo*bar*baz
  3813. no emphasis: foo_bar_baz
  3814. ```
  3815. The rules given below capture all of these patterns, while allowing
  3816. for efficient parsing strategies that do not backtrack.
  3817. First, some definitions. A [delimiter run](@delimiter-run) is either
  3818. a sequence of one or more `*` characters that is not preceded or
  3819. followed by a `*` character, or a sequence of one or more `_`
  3820. characters that is not preceded or followed by a `_` character.
  3821. A [left-flanking delimiter run](@left-flanking-delimiter-run) is
  3822. a [delimiter run] that is (a) not followed by [unicode whitespace],
  3823. and (b) either not followed by a [punctuation character], or
  3824. preceded by [unicode whitespace] or a [punctuation character].
  3825. For purposes of this definition, the beginning and the end of
  3826. the line count as unicode whitespace.
  3827. A [right-flanking delimiter run](@right-flanking-delimiter-run) is
  3828. a [delimiter run] that is (a) not preceded by [unicode whitespace],
  3829. and (b) either not preceded by a [punctuation character], or
  3830. followed by [unicode whitespace] or a [punctuation character].
  3831. For purposes of this definition, the beginning and the end of
  3832. the line count as unicode whitespace.
  3833. Here are some examples of delimiter runs.
  3834. - left-flanking but not right-flanking:
  3835. ```
  3836. ***abc
  3837. _abc
  3838. **"abc"
  3839. _"abc"
  3840. ```
  3841. - right-flanking but not left-flanking:
  3842. ```
  3843. abc***
  3844. abc_
  3845. "abc"**
  3846. "abc"_
  3847. ```
  3848. - Both left and right-flanking:
  3849. ```
  3850. abc***def
  3851. "abc"_"def"
  3852. ```
  3853. - Neither left nor right-flanking:
  3854. ```
  3855. abc *** def
  3856. a _ b
  3857. ```
  3858. (The idea of distinguishing left-flanking and right-flanking
  3859. delimiter runs based on the character before and the character
  3860. after comes from Roopesh Chander's
  3861. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  3862. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  3863. run," and its rules for distinguishing left- and right-flanking runs
  3864. are a bit more complex than the ones given here.)
  3865. The following rules define emphasis and strong emphasis:
  3866. 1. A single `*` character [can open emphasis](@can-open-emphasis)
  3867. iff (if and only if) it is part of a [left-flanking delimiter run].
  3868. 2. A single `_` character [can open emphasis] iff
  3869. it is part of a [left-flanking delimiter run]
  3870. and either (a) not part of a [right-flanking delimiter run]
  3871. or (b) part of a [right-flanking delimeter run]
  3872. preceded by punctuation.
  3873. 3. A single `*` character [can close emphasis](@can-close-emphasis)
  3874. iff it is part of a [right-flanking delimiter run].
  3875. 4. A single `_` character [can close emphasis] iff
  3876. it is part of a [right-flanking delimiter run]
  3877. and either (a) not part of a [left-flanking delimiter run]
  3878. or (b) part of a [left-flanking delimeter run]
  3879. followed by punctuation.
  3880. 5. A double `**` [can open strong emphasis](@can-open-strong-emphasis)
  3881. iff it is part of a [left-flanking delimiter run].
  3882. 6. A double `__` [can open strong emphasis] iff
  3883. it is part of a [left-flanking delimiter run]
  3884. and either (a) not part of a [right-flanking delimiter run]
  3885. or (b) part of a [right-flanking delimeter run]
  3886. preceded by punctuation.
  3887. 7. A double `**` [can close strong emphasis](@can-close-strong-emphasis)
  3888. iff it is part of a [right-flanking delimiter run].
  3889. 8. A double `__` [can close strong emphasis]
  3890. it is part of a [right-flanking delimiter run]
  3891. and either (a) not part of a [left-flanking delimiter run]
  3892. or (b) part of a [left-flanking delimeter run]
  3893. followed by punctuation.
  3894. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  3895. with a delimiter that [can close emphasis], and that uses the same
  3896. character (`_` or `*`) as the opening delimiter. There must
  3897. be a nonempty sequence of inlines between the open delimiter
  3898. and the closing delimiter; these form the contents of the emphasis
  3899. inline.
  3900. 10. Strong emphasis begins with a delimiter that
  3901. [can open strong emphasis] and ends with a delimiter that
  3902. [can close strong emphasis], and that uses the same character
  3903. (`_` or `*`) as the opening delimiter.
  3904. There must be a nonempty sequence of inlines between the open
  3905. delimiter and the closing delimiter; these form the contents of
  3906. the strong emphasis inline.
  3907. 11. A literal `*` character cannot occur at the beginning or end of
  3908. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  3909. is backslash-escaped.
  3910. 12. A literal `_` character cannot occur at the beginning or end of
  3911. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3912. is backslash-escaped.
  3913. Where rules 1--12 above are compatible with multiple parsings,
  3914. the following principles resolve ambiguity:
  3915. 13. The number of nestings should be minimized. Thus, for example,
  3916. an interpretation `<strong>...</strong>` is always preferred to
  3917. `<em><em>...</em></em>`.
  3918. 14. An interpretation `<strong><em>...</em></strong>` is always
  3919. preferred to `<em><strong>..</strong></em>`.
  3920. 15. When two potential emphasis or strong emphasis spans overlap,
  3921. so that the second begins before the first ends and ends after
  3922. the first ends, the first takes precedence. Thus, for example,
  3923. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3924. than `*foo <em>bar* baz</em>`. For the same reason,
  3925. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3926. rather than `<strong>foo*bar</strong>`.
  3927. 16. When there are two potential emphasis or strong emphasis spans
  3928. with the same closing delimiter, the shorter one (the one that
  3929. opens later) takes precedence. Thus, for example,
  3930. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3931. rather than `<strong>foo **bar baz</strong>`.
  3932. 17. Inline code spans, links, images, and HTML tags group more tightly
  3933. than emphasis. So, when there is a choice between an interpretation
  3934. that contains one of these elements and one that does not, the
  3935. former always wins. Thus, for example, `*[foo*](bar)` is
  3936. parsed as `*<a href="bar">foo*</a>` rather than as
  3937. `<em>[foo</em>](bar)`.
  3938. These rules can be illustrated through a series of examples.
  3939. Rule 1:
  3940. .
  3941. *foo bar*
  3942. .
  3943. <p><em>foo bar</em></p>
  3944. .
  3945. This is not emphasis, because the opening `*` is followed by
  3946. whitespace, and hence not part of a [left-flanking delimiter run]:
  3947. .
  3948. a * foo bar*
  3949. .
  3950. <p>a * foo bar*</p>
  3951. .
  3952. This is not emphasis, because the opening `*` is preceded
  3953. by an alphanumeric and followed by punctuation, and hence
  3954. not part of a [left-flanking delimiter run]:
  3955. .
  3956. a*"foo"*
  3957. .
  3958. <p>a*&quot;foo&quot;*</p>
  3959. .
  3960. Unicode nonbreaking spaces count as whitespace, too:
  3961. .
  3962. * a *
  3963. .
  3964. <p>* a *</p>
  3965. .
  3966. Intraword emphasis with `*` is permitted:
  3967. .
  3968. foo*bar*
  3969. .
  3970. <p>foo<em>bar</em></p>
  3971. .
  3972. .
  3973. 5*6*78
  3974. .
  3975. <p>5<em>6</em>78</p>
  3976. .
  3977. Rule 2:
  3978. .
  3979. _foo bar_
  3980. .
  3981. <p><em>foo bar</em></p>
  3982. .
  3983. This is not emphasis, because the opening `_` is followed by
  3984. whitespace:
  3985. .
  3986. _ foo bar_
  3987. .
  3988. <p>_ foo bar_</p>
  3989. .
  3990. This is not emphasis, because the opening `_` is preceded
  3991. by an alphanumeric and followed by punctuation:
  3992. .
  3993. a_"foo"_
  3994. .
  3995. <p>a_&quot;foo&quot;_</p>
  3996. .
  3997. Emphasis with `_` is not allowed inside words:
  3998. .
  3999. foo_bar_
  4000. .
  4001. <p>foo_bar_</p>
  4002. .
  4003. .
  4004. 5_6_78
  4005. .
  4006. <p>5_6_78</p>
  4007. .
  4008. .
  4009. пристаням_стремятся_
  4010. .
  4011. <p>пристаням_стремятся_</p>
  4012. .
  4013. Here `_` does not generate emphasis, because the first delimiter run
  4014. is right-flanking and the second left-flanking:
  4015. .
  4016. aa_"bb"_cc
  4017. .
  4018. <p>aa_&quot;bb&quot;_cc</p>
  4019. .
  4020. This is emphasis, even though the opening delimiter is
  4021. both left- and right-flanking, because it is preceded by
  4022. punctuation:
  4023. .
  4024. foo-_(bar)_
  4025. .
  4026. <p>foo-<em>(bar)</em></p>
  4027. .
  4028. Rule 3:
  4029. This is not emphasis, because the closing delimiter does
  4030. not match the opening delimiter:
  4031. .
  4032. _foo*
  4033. .
  4034. <p>_foo*</p>
  4035. .
  4036. This is not emphasis, because the closing `*` is preceded by
  4037. whitespace:
  4038. .
  4039. *foo bar *
  4040. .
  4041. <p>*foo bar *</p>
  4042. .
  4043. A newline also counts as whitespace:
  4044. .
  4045. *foo bar
  4046. *
  4047. .
  4048. <p>*foo bar</p>
  4049. <ul>
  4050. <li></li>
  4051. </ul>
  4052. .
  4053. This is not emphasis, because the second `*` is
  4054. preceded by punctuation and followed by an alphanumeric
  4055. (hence it is not part of a [right-flanking delimiter run]:
  4056. .
  4057. *(*foo)
  4058. .
  4059. <p>*(*foo)</p>
  4060. .
  4061. The point of this restriction is more easily appreciated
  4062. with this example:
  4063. .
  4064. *(*foo*)*
  4065. .
  4066. <p><em>(<em>foo</em>)</em></p>
  4067. .
  4068. Intraword emphasis with `*` is allowed:
  4069. .
  4070. *foo*bar
  4071. .
  4072. <p><em>foo</em>bar</p>
  4073. .
  4074. Rule 4:
  4075. This is not emphasis, because the closing `_` is preceded by
  4076. whitespace:
  4077. .
  4078. _foo bar _
  4079. .
  4080. <p>_foo bar _</p>
  4081. .
  4082. This is not emphasis, because the second `_` is
  4083. preceded by punctuation and followed by an alphanumeric:
  4084. .
  4085. _(_foo)
  4086. .
  4087. <p>_(_foo)</p>
  4088. .
  4089. This is emphasis within emphasis:
  4090. .
  4091. _(_foo_)_
  4092. .
  4093. <p><em>(<em>foo</em>)</em></p>
  4094. .
  4095. Intraword emphasis is disallowed for `_`:
  4096. .
  4097. _foo_bar
  4098. .
  4099. <p>_foo_bar</p>
  4100. .
  4101. .
  4102. _пристаням_стремятся
  4103. .
  4104. <p>_пристаням_стремятся</p>
  4105. .
  4106. .
  4107. _foo_bar_baz_
  4108. .
  4109. <p><em>foo_bar_baz</em></p>
  4110. .
  4111. This is emphasis, even though the closing delimiter is
  4112. both left- and right-flanking, because it is followed by
  4113. punctuation:
  4114. .
  4115. _(bar)_.
  4116. .
  4117. <p><em>(bar)</em>.</p>
  4118. .
  4119. Rule 5:
  4120. .
  4121. **foo bar**
  4122. .
  4123. <p><strong>foo bar</strong></p>
  4124. .
  4125. This is not strong emphasis, because the opening delimiter is
  4126. followed by whitespace:
  4127. .
  4128. ** foo bar**
  4129. .
  4130. <p>** foo bar**</p>
  4131. .
  4132. This is not strong emphasis, because the opening `**` is preceded
  4133. by an alphanumeric and followed by punctuation, and hence
  4134. not part of a [left-flanking delimiter run]:
  4135. .
  4136. a**"foo"**
  4137. .
  4138. <p>a**&quot;foo&quot;**</p>
  4139. .
  4140. Intraword strong emphasis with `**` is permitted:
  4141. .
  4142. foo**bar**
  4143. .
  4144. <p>foo<strong>bar</strong></p>
  4145. .
  4146. Rule 6:
  4147. .
  4148. __foo bar__
  4149. .
  4150. <p><strong>foo bar</strong></p>
  4151. .
  4152. This is not strong emphasis, because the opening delimiter is
  4153. followed by whitespace:
  4154. .
  4155. __ foo bar__
  4156. .
  4157. <p>__ foo bar__</p>
  4158. .
  4159. A newline counts as whitespace:
  4160. .
  4161. __
  4162. foo bar__
  4163. .
  4164. <p>__
  4165. foo bar__</p>
  4166. .
  4167. This is not strong emphasis, because the opening `__` is preceded
  4168. by an alphanumeric and followed by punctuation:
  4169. .
  4170. a__"foo"__
  4171. .
  4172. <p>a__&quot;foo&quot;__</p>
  4173. .
  4174. Intraword strong emphasis is forbidden with `__`:
  4175. .
  4176. foo__bar__
  4177. .
  4178. <p>foo__bar__</p>
  4179. .
  4180. .
  4181. 5__6__78
  4182. .
  4183. <p>5__6__78</p>
  4184. .
  4185. .
  4186. пристаням__стремятся__
  4187. .
  4188. <p>пристаням__стремятся__</p>
  4189. .
  4190. .
  4191. __foo, __bar__, baz__
  4192. .
  4193. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4194. .
  4195. This is strong emphasis, even though the opening delimiter is
  4196. both left- and right-flanking, because it is preceded by
  4197. punctuation:
  4198. .
  4199. foo-__(bar)__
  4200. .
  4201. <p>foo-<strong>(bar)</strong></p>
  4202. .
  4203. Rule 7:
  4204. This is not strong emphasis, because the closing delimiter is preceded
  4205. by whitespace:
  4206. .
  4207. **foo bar **
  4208. .
  4209. <p>**foo bar **</p>
  4210. .
  4211. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4212. Rule 11.)
  4213. This is not strong emphasis, because the second `**` is
  4214. preceded by punctuation and followed by an alphanumeric:
  4215. .
  4216. **(**foo)
  4217. .
  4218. <p>**(**foo)</p>
  4219. .
  4220. The point of this restriction is more easily appreciated
  4221. with these examples:
  4222. .
  4223. *(**foo**)*
  4224. .
  4225. <p><em>(<strong>foo</strong>)</em></p>
  4226. .
  4227. .
  4228. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4229. *Asclepias physocarpa*)**
  4230. .
  4231. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4232. <em>Asclepias physocarpa</em>)</strong></p>
  4233. .
  4234. .
  4235. **foo "*bar*" foo**
  4236. .
  4237. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4238. .
  4239. Intraword emphasis:
  4240. .
  4241. **foo**bar
  4242. .
  4243. <p><strong>foo</strong>bar</p>
  4244. .
  4245. Rule 8:
  4246. This is not strong emphasis, because the closing delimiter is
  4247. preceded by whitespace:
  4248. .
  4249. __foo bar __
  4250. .
  4251. <p>__foo bar __</p>
  4252. .
  4253. This is not strong emphasis, because the second `__` is
  4254. preceded by punctuation and followed by an alphanumeric:
  4255. .
  4256. __(__foo)
  4257. .
  4258. <p>__(__foo)</p>
  4259. .
  4260. The point of this restriction is more easily appreciated
  4261. with this example:
  4262. .
  4263. _(__foo__)_
  4264. .
  4265. <p><em>(<strong>foo</strong>)</em></p>
  4266. .
  4267. Intraword strong emphasis is forbidden with `__`:
  4268. .
  4269. __foo__bar
  4270. .
  4271. <p>__foo__bar</p>
  4272. .
  4273. .
  4274. __пристаням__стремятся
  4275. .
  4276. <p>__пристаням__стремятся</p>
  4277. .
  4278. .
  4279. __foo__bar__baz__
  4280. .
  4281. <p><strong>foo__bar__baz</strong></p>
  4282. .
  4283. This is strong emphasis, even though the closing delimiter is
  4284. both left- and right-flanking, because it is followed by
  4285. punctuation:
  4286. .
  4287. __(bar)__.
  4288. .
  4289. <p><strong>(bar)</strong>.</p>
  4290. .
  4291. Rule 9:
  4292. Any nonempty sequence of inline elements can be the contents of an
  4293. emphasized span.
  4294. .
  4295. *foo [bar](/url)*
  4296. .
  4297. <p><em>foo <a href="/url">bar</a></em></p>
  4298. .
  4299. .
  4300. *foo
  4301. bar*
  4302. .
  4303. <p><em>foo
  4304. bar</em></p>
  4305. .
  4306. In particular, emphasis and strong emphasis can be nested
  4307. inside emphasis:
  4308. .
  4309. _foo __bar__ baz_
  4310. .
  4311. <p><em>foo <strong>bar</strong> baz</em></p>
  4312. .
  4313. .
  4314. _foo _bar_ baz_
  4315. .
  4316. <p><em>foo <em>bar</em> baz</em></p>
  4317. .
  4318. .
  4319. __foo_ bar_
  4320. .
  4321. <p><em><em>foo</em> bar</em></p>
  4322. .
  4323. .
  4324. *foo *bar**
  4325. .
  4326. <p><em>foo <em>bar</em></em></p>
  4327. .
  4328. .
  4329. *foo **bar** baz*
  4330. .
  4331. <p><em>foo <strong>bar</strong> baz</em></p>
  4332. .
  4333. But note:
  4334. .
  4335. *foo**bar**baz*
  4336. .
  4337. <p><em>foo</em><em>bar</em><em>baz</em></p>
  4338. .
  4339. The difference is that in the preceding case, the internal delimiters
  4340. [can close emphasis], while in the cases with spaces, they cannot.
  4341. .
  4342. ***foo** bar*
  4343. .
  4344. <p><em><strong>foo</strong> bar</em></p>
  4345. .
  4346. .
  4347. *foo **bar***
  4348. .
  4349. <p><em>foo <strong>bar</strong></em></p>
  4350. .
  4351. Note, however, that in the following case we get no strong
  4352. emphasis, because the opening delimiter is closed by the first
  4353. `*` before `bar`:
  4354. .
  4355. *foo**bar***
  4356. .
  4357. <p><em>foo</em><em>bar</em>**</p>
  4358. .
  4359. Indefinite levels of nesting are possible:
  4360. .
  4361. *foo **bar *baz* bim** bop*
  4362. .
  4363. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  4364. .
  4365. .
  4366. *foo [*bar*](/url)*
  4367. .
  4368. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  4369. .
  4370. There can be no empty emphasis or strong emphasis:
  4371. .
  4372. ** is not an empty emphasis
  4373. .
  4374. <p>** is not an empty emphasis</p>
  4375. .
  4376. .
  4377. **** is not an empty strong emphasis
  4378. .
  4379. <p>**** is not an empty strong emphasis</p>
  4380. .
  4381. Rule 10:
  4382. Any nonempty sequence of inline elements can be the contents of an
  4383. strongly emphasized span.
  4384. .
  4385. **foo [bar](/url)**
  4386. .
  4387. <p><strong>foo <a href="/url">bar</a></strong></p>
  4388. .
  4389. .
  4390. **foo
  4391. bar**
  4392. .
  4393. <p><strong>foo
  4394. bar</strong></p>
  4395. .
  4396. In particular, emphasis and strong emphasis can be nested
  4397. inside strong emphasis:
  4398. .
  4399. __foo _bar_ baz__
  4400. .
  4401. <p><strong>foo <em>bar</em> baz</strong></p>
  4402. .
  4403. .
  4404. __foo __bar__ baz__
  4405. .
  4406. <p><strong>foo <strong>bar</strong> baz</strong></p>
  4407. .
  4408. .
  4409. ____foo__ bar__
  4410. .
  4411. <p><strong><strong>foo</strong> bar</strong></p>
  4412. .
  4413. .
  4414. **foo **bar****
  4415. .
  4416. <p><strong>foo <strong>bar</strong></strong></p>
  4417. .
  4418. .
  4419. **foo *bar* baz**
  4420. .
  4421. <p><strong>foo <em>bar</em> baz</strong></p>
  4422. .
  4423. But note:
  4424. .
  4425. **foo*bar*baz**
  4426. .
  4427. <p><em><em>foo</em>bar</em>baz**</p>
  4428. .
  4429. The difference is that in the preceding case, the internal delimiters
  4430. [can close emphasis], while in the cases with spaces, they cannot.
  4431. .
  4432. ***foo* bar**
  4433. .
  4434. <p><strong><em>foo</em> bar</strong></p>
  4435. .
  4436. .
  4437. **foo *bar***
  4438. .
  4439. <p><strong>foo <em>bar</em></strong></p>
  4440. .
  4441. Indefinite levels of nesting are possible:
  4442. .
  4443. **foo *bar **baz**
  4444. bim* bop**
  4445. .
  4446. <p><strong>foo <em>bar <strong>baz</strong>
  4447. bim</em> bop</strong></p>
  4448. .
  4449. .
  4450. **foo [*bar*](/url)**
  4451. .
  4452. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  4453. .
  4454. There can be no empty emphasis or strong emphasis:
  4455. .
  4456. __ is not an empty emphasis
  4457. .
  4458. <p>__ is not an empty emphasis</p>
  4459. .
  4460. .
  4461. ____ is not an empty strong emphasis
  4462. .
  4463. <p>____ is not an empty strong emphasis</p>
  4464. .
  4465. Rule 11:
  4466. .
  4467. foo ***
  4468. .
  4469. <p>foo ***</p>
  4470. .
  4471. .
  4472. foo *\**
  4473. .
  4474. <p>foo <em>*</em></p>
  4475. .
  4476. .
  4477. foo *_*
  4478. .
  4479. <p>foo <em>_</em></p>
  4480. .
  4481. .
  4482. foo *****
  4483. .
  4484. <p>foo *****</p>
  4485. .
  4486. .
  4487. foo **\***
  4488. .
  4489. <p>foo <strong>*</strong></p>
  4490. .
  4491. .
  4492. foo **_**
  4493. .
  4494. <p>foo <strong>_</strong></p>
  4495. .
  4496. Note that when delimiters do not match evenly, Rule 11 determines
  4497. that the excess literal `*` characters will appear outside of the
  4498. emphasis, rather than inside it:
  4499. .
  4500. **foo*
  4501. .
  4502. <p>*<em>foo</em></p>
  4503. .
  4504. .
  4505. *foo**
  4506. .
  4507. <p><em>foo</em>*</p>
  4508. .
  4509. .
  4510. ***foo**
  4511. .
  4512. <p>*<strong>foo</strong></p>
  4513. .
  4514. .
  4515. ****foo*
  4516. .
  4517. <p>***<em>foo</em></p>
  4518. .
  4519. .
  4520. **foo***
  4521. .
  4522. <p><strong>foo</strong>*</p>
  4523. .
  4524. .
  4525. *foo****
  4526. .
  4527. <p><em>foo</em>***</p>
  4528. .
  4529. Rule 12:
  4530. .
  4531. foo ___
  4532. .
  4533. <p>foo ___</p>
  4534. .
  4535. .
  4536. foo _\__
  4537. .
  4538. <p>foo <em>_</em></p>
  4539. .
  4540. .
  4541. foo _*_
  4542. .
  4543. <p>foo <em>*</em></p>
  4544. .
  4545. .
  4546. foo _____
  4547. .
  4548. <p>foo _____</p>
  4549. .
  4550. .
  4551. foo __\___
  4552. .
  4553. <p>foo <strong>_</strong></p>
  4554. .
  4555. .
  4556. foo __*__
  4557. .
  4558. <p>foo <strong>*</strong></p>
  4559. .
  4560. .
  4561. __foo_
  4562. .
  4563. <p>_<em>foo</em></p>
  4564. .
  4565. Note that when delimiters do not match evenly, Rule 12 determines
  4566. that the excess literal `_` characters will appear outside of the
  4567. emphasis, rather than inside it:
  4568. .
  4569. _foo__
  4570. .
  4571. <p><em>foo</em>_</p>
  4572. .
  4573. .
  4574. ___foo__
  4575. .
  4576. <p>_<strong>foo</strong></p>
  4577. .
  4578. .
  4579. ____foo_
  4580. .
  4581. <p>___<em>foo</em></p>
  4582. .
  4583. .
  4584. __foo___
  4585. .
  4586. <p><strong>foo</strong>_</p>
  4587. .
  4588. .
  4589. _foo____
  4590. .
  4591. <p><em>foo</em>___</p>
  4592. .
  4593. Rule 13 implies that if you want emphasis nested directly inside
  4594. emphasis, you must use different delimiters:
  4595. .
  4596. **foo**
  4597. .
  4598. <p><strong>foo</strong></p>
  4599. .
  4600. .
  4601. *_foo_*
  4602. .
  4603. <p><em><em>foo</em></em></p>
  4604. .
  4605. .
  4606. __foo__
  4607. .
  4608. <p><strong>foo</strong></p>
  4609. .
  4610. .
  4611. _*foo*_
  4612. .
  4613. <p><em><em>foo</em></em></p>
  4614. .
  4615. However, strong emphasis within strong emphasis is possible without
  4616. switching delimiters:
  4617. .
  4618. ****foo****
  4619. .
  4620. <p><strong><strong>foo</strong></strong></p>
  4621. .
  4622. .
  4623. ____foo____
  4624. .
  4625. <p><strong><strong>foo</strong></strong></p>
  4626. .
  4627. Rule 13 can be applied to arbitrarily long sequences of
  4628. delimiters:
  4629. .
  4630. ******foo******
  4631. .
  4632. <p><strong><strong><strong>foo</strong></strong></strong></p>
  4633. .
  4634. Rule 14:
  4635. .
  4636. ***foo***
  4637. .
  4638. <p><strong><em>foo</em></strong></p>
  4639. .
  4640. .
  4641. _____foo_____
  4642. .
  4643. <p><strong><strong><em>foo</em></strong></strong></p>
  4644. .
  4645. Rule 15:
  4646. .
  4647. *foo _bar* baz_
  4648. .
  4649. <p><em>foo _bar</em> baz_</p>
  4650. .
  4651. .
  4652. **foo*bar**
  4653. .
  4654. <p><em><em>foo</em>bar</em>*</p>
  4655. .
  4656. .
  4657. *foo __bar *baz bim__ bam*
  4658. .
  4659. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  4660. .
  4661. Rule 16:
  4662. .
  4663. **foo **bar baz**
  4664. .
  4665. <p>**foo <strong>bar baz</strong></p>
  4666. .
  4667. .
  4668. *foo *bar baz*
  4669. .
  4670. <p>*foo <em>bar baz</em></p>
  4671. .
  4672. Rule 17:
  4673. .
  4674. *[bar*](/url)
  4675. .
  4676. <p>*<a href="/url">bar*</a></p>
  4677. .
  4678. .
  4679. _foo [bar_](/url)
  4680. .
  4681. <p>_foo <a href="/url">bar_</a></p>
  4682. .
  4683. .
  4684. *<img src="foo" title="*"/>
  4685. .
  4686. <p>*<img src="foo" title="*"/></p>
  4687. .
  4688. .
  4689. **<a href="**">
  4690. .
  4691. <p>**<a href="**"></p>
  4692. .
  4693. .
  4694. __<a href="__">
  4695. .
  4696. <p>__<a href="__"></p>
  4697. .
  4698. .
  4699. *a `*`*
  4700. .
  4701. <p><em>a <code>*</code></em></p>
  4702. .
  4703. .
  4704. _a `_`_
  4705. .
  4706. <p><em>a <code>_</code></em></p>
  4707. .
  4708. .
  4709. **a<http://foo.bar/?q=**>
  4710. .
  4711. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  4712. .
  4713. .
  4714. __a<http://foo.bar/?q=__>
  4715. .
  4716. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  4717. .
  4718. ## Links
  4719. A link contains [link text] (the visible text), a [link destination]
  4720. (the URI that is the link destination), and optionally a [link title].
  4721. There are two basic kinds of links in Markdown. In [inline link]s the
  4722. destination and title are given immediately after the link text. In
  4723. [reference link]s the destination and title are defined elsewhere in
  4724. the document.
  4725. A [link text](@link-text) consists of a sequence of zero or more
  4726. inline elements enclosed by square brackets (`[` and `]`). The
  4727. following rules apply:
  4728. - Links may not contain other links, at any level of nesting. If
  4729. multiple otherwise valid link definitions appear nested inside each
  4730. other, the inner-most definition is used.
  4731. - Brackets are allowed in the [link text] only if (a) they
  4732. are backslash-escaped or (b) they appear as a matched pair of brackets,
  4733. with an open bracket `[`, a sequence of zero or more inlines, and
  4734. a close bracket `]`.
  4735. - Backtick [code span]s, [autolink]s, and raw [HTML tag]s bind more tightly
  4736. than the brackets in link text. Thus, for example,
  4737. `` [foo`]` `` could not be a link text, since the second `]`
  4738. is part of a code span.
  4739. - The brackets in link text bind more tightly than markers for
  4740. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  4741. A [link destination](@link-destination) consists of either
  4742. - a sequence of zero or more characters between an opening `<` and a
  4743. closing `>` that contains no line breaks or unescaped `<` or `>`
  4744. characters, or
  4745. - a nonempty sequence of characters that does not include
  4746. ASCII space or control characters, and includes parentheses
  4747. only if (a) they are backslash-escaped or (b) they are part of
  4748. a balanced pair of unescaped parentheses that is not itself
  4749. inside a balanced pair of unescaped parentheses.
  4750. A [link title](@link-title) consists of either
  4751. - a sequence of zero or more characters between straight double-quote
  4752. characters (`"`), including a `"` character only if it is
  4753. backslash-escaped, or
  4754. - a sequence of zero or more characters between straight single-quote
  4755. characters (`'`), including a `'` character only if it is
  4756. backslash-escaped, or
  4757. - a sequence of zero or more characters between matching parentheses
  4758. (`(...)`), including a `)` character only if it is backslash-escaped.
  4759. Although [link title]s may span multiple lines, they may not contain
  4760. a [blank line].
  4761. An [inline link](@inline-link) consists of a [link text] followed immediately
  4762. by a left parenthesis `(`, optional [whitespace], an optional
  4763. [link destination], an optional [link title] separated from the link
  4764. destination by [whitespace], optional [whitespace], and a right
  4765. parenthesis `)`. The link's text consists of the inlines contained
  4766. in the [link text] (excluding the enclosing square brackets).
  4767. The link's URI consists of the link destination, excluding enclosing
  4768. `<...>` if present, with backslash-escapes in effect as described
  4769. above. The link's title consists of the link title, excluding its
  4770. enclosing delimiters, with backslash-escapes in effect as described
  4771. above.
  4772. Here is a simple inline link:
  4773. .
  4774. [link](/uri "title")
  4775. .
  4776. <p><a href="/uri" title="title">link</a></p>
  4777. .
  4778. The title may be omitted:
  4779. .
  4780. [link](/uri)
  4781. .
  4782. <p><a href="/uri">link</a></p>
  4783. .
  4784. Both the title and the destination may be omitted:
  4785. .
  4786. [link]()
  4787. .
  4788. <p><a href="">link</a></p>
  4789. .
  4790. .
  4791. [link](<>)
  4792. .
  4793. <p><a href="">link</a></p>
  4794. .
  4795. If the destination contains spaces, it must be enclosed in pointy
  4796. braces:
  4797. .
  4798. [link](/my uri)
  4799. .
  4800. <p>[link](/my uri)</p>
  4801. .
  4802. .
  4803. [link](</my uri>)
  4804. .
  4805. <p><a href="/my%20uri">link</a></p>
  4806. .
  4807. The destination cannot contain line breaks, even with pointy braces:
  4808. .
  4809. [link](foo
  4810. bar)
  4811. .
  4812. <p>[link](foo
  4813. bar)</p>
  4814. .
  4815. .
  4816. [link](<foo
  4817. bar>)
  4818. .
  4819. <p>[link](<foo
  4820. bar>)</p>
  4821. .
  4822. One level of balanced parentheses is allowed without escaping:
  4823. .
  4824. [link]((foo)and(bar))
  4825. .
  4826. <p><a href="(foo)and(bar)">link</a></p>
  4827. .
  4828. However, if you have parentheses within parentheses, you need to escape
  4829. or use the `<...>` form:
  4830. .
  4831. [link](foo(and(bar)))
  4832. .
  4833. <p>[link](foo(and(bar)))</p>
  4834. .
  4835. .
  4836. [link](foo(and\(bar\)))
  4837. .
  4838. <p><a href="foo(and(bar))">link</a></p>
  4839. .
  4840. .
  4841. [link](<foo(and(bar))>)
  4842. .
  4843. <p><a href="foo(and(bar))">link</a></p>
  4844. .
  4845. Parentheses and other symbols can also be escaped, as usual
  4846. in Markdown:
  4847. .
  4848. [link](foo\)\:)
  4849. .
  4850. <p><a href="foo):">link</a></p>
  4851. .
  4852. Note that a backslash before a non-escapable character is
  4853. just a backslash:
  4854. .
  4855. [link](foo\bar)
  4856. .
  4857. <p><a href="foo%5Cbar">link</a></p>
  4858. .
  4859. URL-escaping should be left alone inside the destination, as all
  4860. URL-escaped characters are also valid URL characters. HTML entities in
  4861. the destination will be parsed into the corresponding unicode
  4862. codepoints, as usual, and optionally URL-escaped when written as HTML.
  4863. .
  4864. [link](foo%20b&auml;)
  4865. .
  4866. <p><a href="foo%20b%C3%A4">link</a></p>
  4867. .
  4868. Note that, because titles can often be parsed as destinations,
  4869. if you try to omit the destination and keep the title, you'll
  4870. get unexpected results:
  4871. .
  4872. [link]("title")
  4873. .
  4874. <p><a href="%22title%22">link</a></p>
  4875. .
  4876. Titles may be in single quotes, double quotes, or parentheses:
  4877. .
  4878. [link](/url "title")
  4879. [link](/url 'title')
  4880. [link](/url (title))
  4881. .
  4882. <p><a href="/url" title="title">link</a>
  4883. <a href="/url" title="title">link</a>
  4884. <a href="/url" title="title">link</a></p>
  4885. .
  4886. Backslash escapes and entities may be used in titles:
  4887. .
  4888. [link](/url "title \"&quot;")
  4889. .
  4890. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  4891. .
  4892. Nested balanced quotes are not allowed without escaping:
  4893. .
  4894. [link](/url "title "and" title")
  4895. .
  4896. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  4897. .
  4898. But it is easy to work around this by using a different quote type:
  4899. .
  4900. [link](/url 'title "and" title')
  4901. .
  4902. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  4903. .
  4904. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  4905. title, and its test suite included a test demonstrating this.
  4906. But it is hard to see a good rationale for the extra complexity this
  4907. brings, since there are already many ways---backslash escaping,
  4908. entities, or using a different quote type for the enclosing title---to
  4909. write titles containing double quotes. `Markdown.pl`'s handling of
  4910. titles has a number of other strange features. For example, it allows
  4911. single-quoted titles in inline links, but not reference links. And, in
  4912. reference links but not inline links, it allows a title to begin with
  4913. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  4914. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  4915. a simple, rational rule that works the same way in inline links and
  4916. link reference definitions.)
  4917. [Whitespace] is allowed around the destination and title:
  4918. .
  4919. [link]( /uri
  4920. "title" )
  4921. .
  4922. <p><a href="/uri" title="title">link</a></p>
  4923. .
  4924. But it is not allowed between the link text and the
  4925. following parenthesis:
  4926. .
  4927. [link] (/uri)
  4928. .
  4929. <p>[link] (/uri)</p>
  4930. .
  4931. The link text may contain balanced brackets, but not unbalanced ones,
  4932. unless they are escaped:
  4933. .
  4934. [link [foo [bar]]](/uri)
  4935. .
  4936. <p><a href="/uri">link [foo [bar]]</a></p>
  4937. .
  4938. .
  4939. [link] bar](/uri)
  4940. .
  4941. <p>[link] bar](/uri)</p>
  4942. .
  4943. .
  4944. [link [bar](/uri)
  4945. .
  4946. <p>[link <a href="/uri">bar</a></p>
  4947. .
  4948. .
  4949. [link \[bar](/uri)
  4950. .
  4951. <p><a href="/uri">link [bar</a></p>
  4952. .
  4953. The link text may contain inline content:
  4954. .
  4955. [link *foo **bar** `#`*](/uri)
  4956. .
  4957. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4958. .
  4959. .
  4960. [![moon](moon.jpg)](/uri)
  4961. .
  4962. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4963. .
  4964. However, links may not contain other links, at any level of nesting.
  4965. .
  4966. [foo [bar](/uri)](/uri)
  4967. .
  4968. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  4969. .
  4970. .
  4971. [foo *[bar [baz](/uri)](/uri)*](/uri)
  4972. .
  4973. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  4974. .
  4975. .
  4976. ![[[foo](uri1)](uri2)](uri3)
  4977. .
  4978. <p><img src="uri3" alt="[foo](uri2)" /></p>
  4979. .
  4980. These cases illustrate the precedence of link text grouping over
  4981. emphasis grouping:
  4982. .
  4983. *[foo*](/uri)
  4984. .
  4985. <p>*<a href="/uri">foo*</a></p>
  4986. .
  4987. .
  4988. [foo *bar](baz*)
  4989. .
  4990. <p><a href="baz*">foo *bar</a></p>
  4991. .
  4992. Note that brackets that *aren't* part of links do not take
  4993. precedence:
  4994. .
  4995. *foo [bar* baz]
  4996. .
  4997. <p><em>foo [bar</em> baz]</p>
  4998. .
  4999. These cases illustrate the precedence of HTML tags, code spans,
  5000. and autolinks over link grouping:
  5001. .
  5002. [foo <bar attr="](baz)">
  5003. .
  5004. <p>[foo <bar attr="](baz)"></p>
  5005. .
  5006. .
  5007. [foo`](/uri)`
  5008. .
  5009. <p>[foo<code>](/uri)</code></p>
  5010. .
  5011. .
  5012. [foo<http://example.com/?search=](uri)>
  5013. .
  5014. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5015. .
  5016. There are three kinds of [reference link](@reference-link)s:
  5017. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5018. and [shortcut](#shortcut-reference-link).
  5019. A [full reference link](@full-reference-link)
  5020. consists of a [link text], optional [whitespace], and a [link label]
  5021. that [matches] a [link reference definition] elsewhere in the document.
  5022. A [link label](@link-label) begins with a left bracket (`[`) and ends
  5023. with the first right bracket (`]`) that is not backslash-escaped.
  5024. Between these brackets there must be at least one non-[whitespace character].
  5025. Unescaped square bracket characters are not allowed in
  5026. [link label]s. A link label can have at most 999
  5027. characters inside the square brackets.
  5028. One label [matches](@matches)
  5029. another just in case their normalized forms are equal. To normalize a
  5030. label, perform the *unicode case fold* and collapse consecutive internal
  5031. [whitespace] to a single space. If there are multiple
  5032. matching reference link definitions, the one that comes first in the
  5033. document is used. (It is desirable in such cases to emit a warning.)
  5034. The contents of the first link label are parsed as inlines, which are
  5035. used as the link's text. The link's URI and title are provided by the
  5036. matching [link reference definition].
  5037. Here is a simple example:
  5038. .
  5039. [foo][bar]
  5040. [bar]: /url "title"
  5041. .
  5042. <p><a href="/url" title="title">foo</a></p>
  5043. .
  5044. The rules for the [link text] are the same as with
  5045. [inline link]s. Thus:
  5046. The link text may contain balanced brackets, but not unbalanced ones,
  5047. unless they are escaped:
  5048. .
  5049. [link [foo [bar]]][ref]
  5050. [ref]: /uri
  5051. .
  5052. <p><a href="/uri">link [foo [bar]]</a></p>
  5053. .
  5054. .
  5055. [link \[bar][ref]
  5056. [ref]: /uri
  5057. .
  5058. <p><a href="/uri">link [bar</a></p>
  5059. .
  5060. The link text may contain inline content:
  5061. .
  5062. [link *foo **bar** `#`*][ref]
  5063. [ref]: /uri
  5064. .
  5065. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5066. .
  5067. .
  5068. [![moon](moon.jpg)][ref]
  5069. [ref]: /uri
  5070. .
  5071. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5072. .
  5073. However, links may not contain other links, at any level of nesting.
  5074. .
  5075. [foo [bar](/uri)][ref]
  5076. [ref]: /uri
  5077. .
  5078. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5079. .
  5080. .
  5081. [foo *bar [baz][ref]*][ref]
  5082. [ref]: /uri
  5083. .
  5084. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5085. .
  5086. (In the examples above, we have two [shortcut reference link]s
  5087. instead of one [full reference link].)
  5088. The following cases illustrate the precedence of link text grouping over
  5089. emphasis grouping:
  5090. .
  5091. *[foo*][ref]
  5092. [ref]: /uri
  5093. .
  5094. <p>*<a href="/uri">foo*</a></p>
  5095. .
  5096. .
  5097. [foo *bar][ref]
  5098. [ref]: /uri
  5099. .
  5100. <p><a href="/uri">foo *bar</a></p>
  5101. .
  5102. These cases illustrate the precedence of HTML tags, code spans,
  5103. and autolinks over link grouping:
  5104. .
  5105. [foo <bar attr="][ref]">
  5106. [ref]: /uri
  5107. .
  5108. <p>[foo <bar attr="][ref]"></p>
  5109. .
  5110. .
  5111. [foo`][ref]`
  5112. [ref]: /uri
  5113. .
  5114. <p>[foo<code>][ref]</code></p>
  5115. .
  5116. .
  5117. [foo<http://example.com/?search=][ref]>
  5118. [ref]: /uri
  5119. .
  5120. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5121. .
  5122. Matching is case-insensitive:
  5123. .
  5124. [foo][BaR]
  5125. [bar]: /url "title"
  5126. .
  5127. <p><a href="/url" title="title">foo</a></p>
  5128. .
  5129. Unicode case fold is used:
  5130. .
  5131. [Толпой][Толпой] is a Russian word.
  5132. [ТОЛПОЙ]: /url
  5133. .
  5134. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5135. .
  5136. Consecutive internal [whitespace] is treated as one space for
  5137. purposes of determining matching:
  5138. .
  5139. [Foo
  5140. bar]: /url
  5141. [Baz][Foo bar]
  5142. .
  5143. <p><a href="/url">Baz</a></p>
  5144. .
  5145. There can be [whitespace] between the [link text] and the [link label]:
  5146. .
  5147. [foo] [bar]
  5148. [bar]: /url "title"
  5149. .
  5150. <p><a href="/url" title="title">foo</a></p>
  5151. .
  5152. .
  5153. [foo]
  5154. [bar]
  5155. [bar]: /url "title"
  5156. .
  5157. <p><a href="/url" title="title">foo</a></p>
  5158. .
  5159. When there are multiple matching [link reference definition]s,
  5160. the first is used:
  5161. .
  5162. [foo]: /url1
  5163. [foo]: /url2
  5164. [bar][foo]
  5165. .
  5166. <p><a href="/url1">bar</a></p>
  5167. .
  5168. Note that matching is performed on normalized strings, not parsed
  5169. inline content. So the following does not match, even though the
  5170. labels define equivalent inline content:
  5171. .
  5172. [bar][foo\!]
  5173. [foo!]: /url
  5174. .
  5175. <p>[bar][foo!]</p>
  5176. .
  5177. [Link label]s cannot contain brackets, unless they are
  5178. backslash-escaped:
  5179. .
  5180. [foo][ref[]
  5181. [ref[]: /uri
  5182. .
  5183. <p>[foo][ref[]</p>
  5184. <p>[ref[]: /uri</p>
  5185. .
  5186. .
  5187. [foo][ref[bar]]
  5188. [ref[bar]]: /uri
  5189. .
  5190. <p>[foo][ref[bar]]</p>
  5191. <p>[ref[bar]]: /uri</p>
  5192. .
  5193. .
  5194. [[[foo]]]
  5195. [[[foo]]]: /url
  5196. .
  5197. <p>[[[foo]]]</p>
  5198. <p>[[[foo]]]: /url</p>
  5199. .
  5200. .
  5201. [foo][ref\[]
  5202. [ref\[]: /uri
  5203. .
  5204. <p><a href="/uri">foo</a></p>
  5205. .
  5206. A [link label] must contain at least one non-[whitespace character]:
  5207. .
  5208. []
  5209. []: /uri
  5210. .
  5211. <p>[]</p>
  5212. <p>[]: /uri</p>
  5213. .
  5214. .
  5215. [
  5216. ]
  5217. [
  5218. ]: /uri
  5219. .
  5220. <p>[
  5221. ]</p>
  5222. <p>[
  5223. ]: /uri</p>
  5224. .
  5225. A [collapsed reference link](@collapsed-reference-link)
  5226. consists of a [link label] that [matches] a
  5227. [link reference definition] elsewhere in the
  5228. document, optional [whitespace], and the string `[]`.
  5229. The contents of the first link label are parsed as inlines,
  5230. which are used as the link's text. The link's URI and title are
  5231. provided by the matching reference link definition. Thus,
  5232. `[foo][]` is equivalent to `[foo][foo]`.
  5233. .
  5234. [foo][]
  5235. [foo]: /url "title"
  5236. .
  5237. <p><a href="/url" title="title">foo</a></p>
  5238. .
  5239. .
  5240. [*foo* bar][]
  5241. [*foo* bar]: /url "title"
  5242. .
  5243. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5244. .
  5245. The link labels are case-insensitive:
  5246. .
  5247. [Foo][]
  5248. [foo]: /url "title"
  5249. .
  5250. <p><a href="/url" title="title">Foo</a></p>
  5251. .
  5252. As with full reference links, [whitespace] is allowed
  5253. between the two sets of brackets:
  5254. .
  5255. [foo]
  5256. []
  5257. [foo]: /url "title"
  5258. .
  5259. <p><a href="/url" title="title">foo</a></p>
  5260. .
  5261. A [shortcut reference link](@shortcut-reference-link)
  5262. consists of a [link label] that [matches] a
  5263. [link reference definition] elsewhere in the
  5264. document and is not followed by `[]` or a link label.
  5265. The contents of the first link label are parsed as inlines,
  5266. which are used as the link's text. the link's URI and title
  5267. are provided by the matching link reference definition.
  5268. Thus, `[foo]` is equivalent to `[foo][]`.
  5269. .
  5270. [foo]
  5271. [foo]: /url "title"
  5272. .
  5273. <p><a href="/url" title="title">foo</a></p>
  5274. .
  5275. .
  5276. [*foo* bar]
  5277. [*foo* bar]: /url "title"
  5278. .
  5279. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5280. .
  5281. .
  5282. [[*foo* bar]]
  5283. [*foo* bar]: /url "title"
  5284. .
  5285. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  5286. .
  5287. .
  5288. [[bar [foo]
  5289. [foo]: /url
  5290. .
  5291. <p>[[bar <a href="/url">foo</a></p>
  5292. .
  5293. The link labels are case-insensitive:
  5294. .
  5295. [Foo]
  5296. [foo]: /url "title"
  5297. .
  5298. <p><a href="/url" title="title">Foo</a></p>
  5299. .
  5300. A space after the link text should be preserved:
  5301. .
  5302. [foo] bar
  5303. [foo]: /url
  5304. .
  5305. <p><a href="/url">foo</a> bar</p>
  5306. .
  5307. If you just want bracketed text, you can backslash-escape the
  5308. opening bracket to avoid links:
  5309. .
  5310. \[foo]
  5311. [foo]: /url "title"
  5312. .
  5313. <p>[foo]</p>
  5314. .
  5315. Note that this is a link, because a link label ends with the first
  5316. following closing bracket:
  5317. .
  5318. [foo*]: /url
  5319. *[foo*]
  5320. .
  5321. <p>*<a href="/url">foo*</a></p>
  5322. .
  5323. Full references take precedence over shortcut references:
  5324. .
  5325. [foo][bar]
  5326. [foo]: /url1
  5327. [bar]: /url2
  5328. .
  5329. <p><a href="/url2">foo</a></p>
  5330. .
  5331. In the following case `[bar][baz]` is parsed as a reference,
  5332. `[foo]` as normal text:
  5333. .
  5334. [foo][bar][baz]
  5335. [baz]: /url
  5336. .
  5337. <p>[foo]<a href="/url">bar</a></p>
  5338. .
  5339. Here, though, `[foo][bar]` is parsed as a reference, since
  5340. `[bar]` is defined:
  5341. .
  5342. [foo][bar][baz]
  5343. [baz]: /url1
  5344. [bar]: /url2
  5345. .
  5346. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  5347. .
  5348. Here `[foo]` is not parsed as a shortcut reference, because it
  5349. is followed by a link label (even though `[bar]` is not defined):
  5350. .
  5351. [foo][bar][baz]
  5352. [baz]: /url1
  5353. [foo]: /url2
  5354. .
  5355. <p>[foo]<a href="/url1">bar</a></p>
  5356. .
  5357. ## Images
  5358. Syntax for images is like the syntax for links, with one
  5359. difference. Instead of [link text], we have an
  5360. [image description](@image-description). The rules for this are the
  5361. same as for [link text], except that (a) an
  5362. image description starts with `![` rather than `[`, and
  5363. (b) an image description may contain links.
  5364. An image description has inline elements
  5365. as its contents. When an image is rendered to HTML,
  5366. this is standardly used as the image's `alt` attribute.
  5367. .
  5368. ![foo](/url "title")
  5369. .
  5370. <p><img src="/url" alt="foo" title="title" /></p>
  5371. .
  5372. .
  5373. ![foo *bar*]
  5374. [foo *bar*]: train.jpg "train & tracks"
  5375. .
  5376. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5377. .
  5378. .
  5379. ![foo ![bar](/url)](/url2)
  5380. .
  5381. <p><img src="/url2" alt="foo bar" /></p>
  5382. .
  5383. .
  5384. ![foo [bar](/url)](/url2)
  5385. .
  5386. <p><img src="/url2" alt="foo bar" /></p>
  5387. .
  5388. Though this spec is concerned with parsing, not rendering, it is
  5389. recommended that in rendering to HTML, only the plain string content
  5390. of the [image description] be used. Note that in
  5391. the above example, the alt attribute's value is `foo bar`, not `foo
  5392. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  5393. content is rendered, without formatting.
  5394. .
  5395. ![foo *bar*][]
  5396. [foo *bar*]: train.jpg "train & tracks"
  5397. .
  5398. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5399. .
  5400. .
  5401. ![foo *bar*][foobar]
  5402. [FOOBAR]: train.jpg "train & tracks"
  5403. .
  5404. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5405. .
  5406. .
  5407. ![foo](train.jpg)
  5408. .
  5409. <p><img src="train.jpg" alt="foo" /></p>
  5410. .
  5411. .
  5412. My ![foo bar](/path/to/train.jpg "title" )
  5413. .
  5414. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  5415. .
  5416. .
  5417. ![foo](<url>)
  5418. .
  5419. <p><img src="url" alt="foo" /></p>
  5420. .
  5421. .
  5422. ![](/url)
  5423. .
  5424. <p><img src="/url" alt="" /></p>
  5425. .
  5426. Reference-style:
  5427. .
  5428. ![foo] [bar]
  5429. [bar]: /url
  5430. .
  5431. <p><img src="/url" alt="foo" /></p>
  5432. .
  5433. .
  5434. ![foo] [bar]
  5435. [BAR]: /url
  5436. .
  5437. <p><img src="/url" alt="foo" /></p>
  5438. .
  5439. Collapsed:
  5440. .
  5441. ![foo][]
  5442. [foo]: /url "title"
  5443. .
  5444. <p><img src="/url" alt="foo" title="title" /></p>
  5445. .
  5446. .
  5447. ![*foo* bar][]
  5448. [*foo* bar]: /url "title"
  5449. .
  5450. <p><img src="/url" alt="foo bar" title="title" /></p>
  5451. .
  5452. The labels are case-insensitive:
  5453. .
  5454. ![Foo][]
  5455. [foo]: /url "title"
  5456. .
  5457. <p><img src="/url" alt="Foo" title="title" /></p>
  5458. .
  5459. As with full reference links, [whitespace] is allowed
  5460. between the two sets of brackets:
  5461. .
  5462. ![foo]
  5463. []
  5464. [foo]: /url "title"
  5465. .
  5466. <p><img src="/url" alt="foo" title="title" /></p>
  5467. .
  5468. Shortcut:
  5469. .
  5470. ![foo]
  5471. [foo]: /url "title"
  5472. .
  5473. <p><img src="/url" alt="foo" title="title" /></p>
  5474. .
  5475. .
  5476. ![*foo* bar]
  5477. [*foo* bar]: /url "title"
  5478. .
  5479. <p><img src="/url" alt="foo bar" title="title" /></p>
  5480. .
  5481. Note that link labels cannot contain unescaped brackets:
  5482. .
  5483. ![[foo]]
  5484. [[foo]]: /url "title"
  5485. .
  5486. <p>![[foo]]</p>
  5487. <p>[[foo]]: /url &quot;title&quot;</p>
  5488. .
  5489. The link labels are case-insensitive:
  5490. .
  5491. ![Foo]
  5492. [foo]: /url "title"
  5493. .
  5494. <p><img src="/url" alt="Foo" title="title" /></p>
  5495. .
  5496. If you just want bracketed text, you can backslash-escape the
  5497. opening `!` and `[`:
  5498. .
  5499. \!\[foo]
  5500. [foo]: /url "title"
  5501. .
  5502. <p>![foo]</p>
  5503. .
  5504. If you want a link after a literal `!`, backslash-escape the
  5505. `!`:
  5506. .
  5507. \![foo]
  5508. [foo]: /url "title"
  5509. .
  5510. <p>!<a href="/url" title="title">foo</a></p>
  5511. .
  5512. ## Autolinks
  5513. [Autolink](@autolink)s are absolute URIs and email addresses inside
  5514. `<` and `>`. They are parsed as links, with the URL or email address
  5515. as the link label.
  5516. A [URI autolink](@uri-autolink) consists of `<`, followed by an
  5517. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  5518. a link to the URI, with the URI as the link's label.
  5519. An [absolute URI](@absolute-uri),
  5520. for these purposes, consists of a [scheme] followed by a colon (`:`)
  5521. followed by zero or more characters other than ASCII
  5522. [whitespace] and control characters, `<`, and `>`. If
  5523. the URI includes these characters, you must use percent-encoding
  5524. (e.g. `%20` for a space).
  5525. The following [schemes](@scheme)
  5526. are recognized (case-insensitive):
  5527. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  5528. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  5529. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  5530. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  5531. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  5532. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  5533. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  5534. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5535. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5536. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5537. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5538. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5539. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5540. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5541. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5542. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5543. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5544. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5545. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5546. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5547. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5548. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5549. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5550. Here are some valid autolinks:
  5551. .
  5552. <http://foo.bar.baz>
  5553. .
  5554. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5555. .
  5556. .
  5557. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  5558. .
  5559. <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>
  5560. .
  5561. .
  5562. <irc://foo.bar:2233/baz>
  5563. .
  5564. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5565. .
  5566. Uppercase is also fine:
  5567. .
  5568. <MAILTO:FOO@BAR.BAZ>
  5569. .
  5570. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5571. .
  5572. Spaces are not allowed in autolinks:
  5573. .
  5574. <http://foo.bar/baz bim>
  5575. .
  5576. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5577. .
  5578. Backslash-escapes do not work inside autolinks:
  5579. .
  5580. <http://example.com/\[\>
  5581. .
  5582. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  5583. .
  5584. An [email autolink](@email-autolink)
  5585. consists of `<`, followed by an [email address],
  5586. followed by `>`. The link's label is the email address,
  5587. and the URL is `mailto:` followed by the email address.
  5588. An [email address](@email-address),
  5589. for these purposes, is anything that matches
  5590. the [non-normative regex from the HTML5
  5591. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  5592. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5593. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5594. Examples of email autolinks:
  5595. .
  5596. <foo@bar.example.com>
  5597. .
  5598. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5599. .
  5600. .
  5601. <foo+special@Bar.baz-bar0.com>
  5602. .
  5603. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5604. .
  5605. Backslash-escapes do not work inside email autolinks:
  5606. .
  5607. <foo\+@bar.example.com>
  5608. .
  5609. <p>&lt;foo+@bar.example.com&gt;</p>
  5610. .
  5611. These are not autolinks:
  5612. .
  5613. <>
  5614. .
  5615. <p>&lt;&gt;</p>
  5616. .
  5617. .
  5618. <heck://bing.bong>
  5619. .
  5620. <p>&lt;heck://bing.bong&gt;</p>
  5621. .
  5622. .
  5623. < http://foo.bar >
  5624. .
  5625. <p>&lt; http://foo.bar &gt;</p>
  5626. .
  5627. .
  5628. <foo.bar.baz>
  5629. .
  5630. <p>&lt;foo.bar.baz&gt;</p>
  5631. .
  5632. .
  5633. <localhost:5001/foo>
  5634. .
  5635. <p>&lt;localhost:5001/foo&gt;</p>
  5636. .
  5637. .
  5638. http://example.com
  5639. .
  5640. <p>http://example.com</p>
  5641. .
  5642. .
  5643. foo@bar.example.com
  5644. .
  5645. <p>foo@bar.example.com</p>
  5646. .
  5647. ## Raw HTML
  5648. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5649. raw HTML tag and will be rendered in HTML without escaping.
  5650. Tag and attribute names are not limited to current HTML tags,
  5651. so custom tags (and even, say, DocBook tags) may be used.
  5652. Here is the grammar for tags:
  5653. A [tag name](@tag-name) consists of an ASCII letter
  5654. followed by zero or more ASCII letters or digits.
  5655. An [attribute](@attribute) consists of [whitespace],
  5656. an [attribute name], and an optional
  5657. [attribute value specification].
  5658. An [attribute name](@attribute-name)
  5659. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5660. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5661. specification restricted to ASCII. HTML5 is laxer.)
  5662. An [attribute value specification](@attribute-value-specification)
  5663. consists of optional [whitespace],
  5664. a `=` character, optional [whitespace], and an [attribute
  5665. value].
  5666. An [attribute value](@attribute-value)
  5667. consists of an [unquoted attribute value],
  5668. a [single-quoted attribute value], or a [double-quoted attribute value].
  5669. An [unquoted attribute value](@unquoted-attribute-value)
  5670. is a nonempty string of characters not
  5671. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5672. A [single-quoted attribute value](@single-quoted-attribute-value)
  5673. consists of `'`, zero or more
  5674. characters not including `'`, and a final `'`.
  5675. A [double-quoted attribute value](@double-quoted-attribute-value)
  5676. consists of `"`, zero or more
  5677. characters not including `"`, and a final `"`.
  5678. An [open tag](@open-tag) consists of a `<` character, a [tag name],
  5679. zero or more [attributes], optional [whitespace], an optional `/`
  5680. character, and a `>` character.
  5681. A [closing tag](@closing-tag) consists of the string `</`, a
  5682. [tag name], optional [whitespace], and the character `>`.
  5683. An [HTML comment](@html-comment) consists of `<!--` + *text* + `-->`,
  5684. where *text* does not start with `>` or `->`, does not end with `-`,
  5685. and does not contain `--`. (See the
  5686. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  5687. A [processing instruction](@processing-instruction)
  5688. consists of the string `<?`, a string
  5689. of characters not including the string `?>`, and the string
  5690. `?>`.
  5691. A [declaration](@declaration) consists of the
  5692. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5693. [whitespace], a string of characters not including the
  5694. character `>`, and the character `>`.
  5695. A [CDATA section](@cdata-section) consists of
  5696. the string `<![CDATA[`, a string of characters not including the string
  5697. `]]>`, and the string `]]>`.
  5698. An [HTML tag](@html-tag) consists of an [open tag], a [closing tag],
  5699. an [HTML comment], a [processing instruction], a [declaration],
  5700. or a [CDATA section].
  5701. Here are some simple open tags:
  5702. .
  5703. <a><bab><c2c>
  5704. .
  5705. <p><a><bab><c2c></p>
  5706. .
  5707. Empty elements:
  5708. .
  5709. <a/><b2/>
  5710. .
  5711. <p><a/><b2/></p>
  5712. .
  5713. [Whitespace] is allowed:
  5714. .
  5715. <a /><b2
  5716. data="foo" >
  5717. .
  5718. <p><a /><b2
  5719. data="foo" ></p>
  5720. .
  5721. With attributes:
  5722. .
  5723. <a foo="bar" bam = 'baz <em>"</em>'
  5724. _boolean zoop:33=zoop:33 />
  5725. .
  5726. <p><a foo="bar" bam = 'baz <em>"</em>'
  5727. _boolean zoop:33=zoop:33 /></p>
  5728. .
  5729. Illegal tag names, not parsed as HTML:
  5730. .
  5731. <33> <__>
  5732. .
  5733. <p>&lt;33&gt; &lt;__&gt;</p>
  5734. .
  5735. Illegal attribute names:
  5736. .
  5737. <a h*#ref="hi">
  5738. .
  5739. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5740. .
  5741. Illegal attribute values:
  5742. .
  5743. <a href="hi'> <a href=hi'>
  5744. .
  5745. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5746. .
  5747. Illegal [whitespace]:
  5748. .
  5749. < a><
  5750. foo><bar/ >
  5751. .
  5752. <p>&lt; a&gt;&lt;
  5753. foo&gt;&lt;bar/ &gt;</p>
  5754. .
  5755. Missing [whitespace]:
  5756. .
  5757. <a href='bar'title=title>
  5758. .
  5759. <p>&lt;a href='bar'title=title&gt;</p>
  5760. .
  5761. Closing tags:
  5762. .
  5763. </a>
  5764. </foo >
  5765. .
  5766. <p></a>
  5767. </foo ></p>
  5768. .
  5769. Illegal attributes in closing tag:
  5770. .
  5771. </a href="foo">
  5772. .
  5773. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5774. .
  5775. Comments:
  5776. .
  5777. foo <!-- this is a
  5778. comment - with hyphen -->
  5779. .
  5780. <p>foo <!-- this is a
  5781. comment - with hyphen --></p>
  5782. .
  5783. .
  5784. foo <!-- not a comment -- two hyphens -->
  5785. .
  5786. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5787. .
  5788. Not comments:
  5789. .
  5790. foo <!--> foo -->
  5791. foo <!-- foo--->
  5792. .
  5793. <p>foo &lt;!--&gt; foo --&gt;</p>
  5794. <p>foo &lt;!-- foo---&gt;</p>
  5795. .
  5796. Processing instructions:
  5797. .
  5798. foo <?php echo $a; ?>
  5799. .
  5800. <p>foo <?php echo $a; ?></p>
  5801. .
  5802. Declarations:
  5803. .
  5804. foo <!ELEMENT br EMPTY>
  5805. .
  5806. <p>foo <!ELEMENT br EMPTY></p>
  5807. .
  5808. CDATA sections:
  5809. .
  5810. foo <![CDATA[>&<]]>
  5811. .
  5812. <p>foo <![CDATA[>&<]]></p>
  5813. .
  5814. Entities are preserved in HTML attributes:
  5815. .
  5816. <a href="&ouml;">
  5817. .
  5818. <p><a href="&ouml;"></p>
  5819. .
  5820. Backslash escapes do not work in HTML attributes:
  5821. .
  5822. <a href="\*">
  5823. .
  5824. <p><a href="\*"></p>
  5825. .
  5826. .
  5827. <a href="\"">
  5828. .
  5829. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5830. .
  5831. ## Hard line breaks
  5832. A line break (not in a code span or HTML tag) that is preceded
  5833. by two or more spaces and does not occur at the end of a block
  5834. is parsed as a [hard line break](@hard-line-break) (rendered
  5835. in HTML as a `<br />` tag):
  5836. .
  5837. foo
  5838. baz
  5839. .
  5840. <p>foo<br />
  5841. baz</p>
  5842. .
  5843. For a more visible alternative, a backslash before the
  5844. [line ending] may be used instead of two spaces:
  5845. .
  5846. foo\
  5847. baz
  5848. .
  5849. <p>foo<br />
  5850. baz</p>
  5851. .
  5852. More than two spaces can be used:
  5853. .
  5854. foo
  5855. baz
  5856. .
  5857. <p>foo<br />
  5858. baz</p>
  5859. .
  5860. Leading spaces at the beginning of the next line are ignored:
  5861. .
  5862. foo
  5863. bar
  5864. .
  5865. <p>foo<br />
  5866. bar</p>
  5867. .
  5868. .
  5869. foo\
  5870. bar
  5871. .
  5872. <p>foo<br />
  5873. bar</p>
  5874. .
  5875. Line breaks can occur inside emphasis, links, and other constructs
  5876. that allow inline content:
  5877. .
  5878. *foo
  5879. bar*
  5880. .
  5881. <p><em>foo<br />
  5882. bar</em></p>
  5883. .
  5884. .
  5885. *foo\
  5886. bar*
  5887. .
  5888. <p><em>foo<br />
  5889. bar</em></p>
  5890. .
  5891. Line breaks do not occur inside code spans
  5892. .
  5893. `code
  5894. span`
  5895. .
  5896. <p><code>code span</code></p>
  5897. .
  5898. .
  5899. `code\
  5900. span`
  5901. .
  5902. <p><code>code\ span</code></p>
  5903. .
  5904. or HTML tags:
  5905. .
  5906. <a href="foo
  5907. bar">
  5908. .
  5909. <p><a href="foo
  5910. bar"></p>
  5911. .
  5912. .
  5913. <a href="foo\
  5914. bar">
  5915. .
  5916. <p><a href="foo\
  5917. bar"></p>
  5918. .
  5919. Hard line breaks are for separating inline content within a block.
  5920. Neither syntax for hard line breaks works at the end of a paragraph or
  5921. other block element:
  5922. .
  5923. foo\
  5924. .
  5925. <p>foo\</p>
  5926. .
  5927. .
  5928. foo
  5929. .
  5930. <p>foo</p>
  5931. .
  5932. .
  5933. ### foo\
  5934. .
  5935. <h3>foo\</h3>
  5936. .
  5937. .
  5938. ### foo
  5939. .
  5940. <h3>foo</h3>
  5941. .
  5942. ## Soft line breaks
  5943. A regular line break (not in a code span or HTML tag) that is not
  5944. preceded by two or more spaces or a backslash is parsed as a
  5945. softbreak. (A softbreak may be rendered in HTML either as a
  5946. [line ending] or as a space. The result will be the same in
  5947. browsers. In the examples here, a [line ending] will be used.)
  5948. .
  5949. foo
  5950. baz
  5951. .
  5952. <p>foo
  5953. baz</p>
  5954. .
  5955. Spaces at the end of the line and beginning of the next line are
  5956. removed:
  5957. .
  5958. foo
  5959. baz
  5960. .
  5961. <p>foo
  5962. baz</p>
  5963. .
  5964. A conforming parser may render a soft line break in HTML either as a
  5965. line break or as a space.
  5966. A renderer may also provide an option to render soft line breaks
  5967. as hard line breaks.
  5968. ## Textual content
  5969. Any characters not given an interpretation by the above rules will
  5970. be parsed as plain textual content.
  5971. .
  5972. hello $.;'there
  5973. .
  5974. <p>hello $.;'there</p>
  5975. .
  5976. .
  5977. Foo χρῆν
  5978. .
  5979. <p>Foo χρῆν</p>
  5980. .
  5981. Internal spaces are preserved verbatim:
  5982. .
  5983. Multiple spaces
  5984. .
  5985. <p>Multiple spaces</p>
  5986. .
  5987. <!-- END TESTS -->
  5988. # Appendix A: A parsing strategy {-}
  5989. ## Overview {-}
  5990. Parsing has two phases:
  5991. 1. In the first phase, lines of input are consumed and the block
  5992. structure of the document---its division into paragraphs, block quotes,
  5993. list items, and so on---is constructed. Text is assigned to these
  5994. blocks but not parsed. Link reference definitions are parsed and a
  5995. map of links is constructed.
  5996. 2. In the second phase, the raw text contents of paragraphs and headers
  5997. are parsed into sequences of Markdown inline elements (strings,
  5998. code spans, links, emphasis, and so on), using the map of link
  5999. references constructed in phase 1.
  6000. ## The document tree {-}
  6001. At each point in processing, the document is represented as a tree of
  6002. **blocks**. The root of the tree is a `document` block. The `document`
  6003. may have any number of other blocks as **children**. These children
  6004. may, in turn, have other blocks as children. The last child of a block
  6005. is normally considered **open**, meaning that subsequent lines of input
  6006. can alter its contents. (Blocks that are not open are **closed**.)
  6007. Here, for example, is a possible document tree, with the open blocks
  6008. marked by arrows:
  6009. ``` tree
  6010. -> document
  6011. -> block_quote
  6012. paragraph
  6013. "Lorem ipsum dolor\nsit amet."
  6014. -> list (type=bullet tight=true bullet_char=-)
  6015. list_item
  6016. paragraph
  6017. "Qui *quodsi iracundia*"
  6018. -> list_item
  6019. -> paragraph
  6020. "aliquando id"
  6021. ```
  6022. ## How source lines alter the document tree {-}
  6023. Each line that is processed has an effect on this tree. The line is
  6024. analyzed and, depending on its contents, the document may be altered
  6025. in one or more of the following ways:
  6026. 1. One or more open blocks may be closed.
  6027. 2. One or more new blocks may be created as children of the
  6028. last open block.
  6029. 3. Text may be added to the last (deepest) open block remaining
  6030. on the tree.
  6031. Once a line has been incorporated into the tree in this way,
  6032. it can be discarded, so input can be read in a stream.
  6033. We can see how this works by considering how the tree above is
  6034. generated by four lines of Markdown:
  6035. ``` markdown
  6036. > Lorem ipsum dolor
  6037. sit amet.
  6038. > - Qui *quodsi iracundia*
  6039. > - aliquando id
  6040. ```
  6041. At the outset, our document model is just
  6042. ``` tree
  6043. -> document
  6044. ```
  6045. The first line of our text,
  6046. ``` markdown
  6047. > Lorem ipsum dolor
  6048. ```
  6049. causes a `block_quote` block to be created as a child of our
  6050. open `document` block, and a `paragraph` block as a child of
  6051. the `block_quote`. Then the text is added to the last open
  6052. block, the `paragraph`:
  6053. ``` tree
  6054. -> document
  6055. -> block_quote
  6056. -> paragraph
  6057. "Lorem ipsum dolor"
  6058. ```
  6059. The next line,
  6060. ``` markdown
  6061. sit amet.
  6062. ```
  6063. is a "lazy continuation" of the open `paragraph`, so it gets added
  6064. to the paragraph's text:
  6065. ``` tree
  6066. -> document
  6067. -> block_quote
  6068. -> paragraph
  6069. "Lorem ipsum dolor\nsit amet."
  6070. ```
  6071. The third line,
  6072. ``` markdown
  6073. > - Qui *quodsi iracundia*
  6074. ```
  6075. causes the `paragraph` block to be closed, and a new `list` block
  6076. opened as a child of the `block_quote`. A `list_item` is also
  6077. added as a child of the `list`, and a `paragraph` as a child of
  6078. the `list_item`. The text is then added to the new `paragraph`:
  6079. ``` tree
  6080. -> document
  6081. -> block_quote
  6082. paragraph
  6083. "Lorem ipsum dolor\nsit amet."
  6084. -> list (type=bullet tight=true bullet_char=-)
  6085. -> list_item
  6086. -> paragraph
  6087. "Qui *quodsi iracundia*"
  6088. ```
  6089. The fourth line,
  6090. ``` markdown
  6091. > - aliquando id
  6092. ```
  6093. causes the `list_item` (and its child the `paragraph`) to be closed,
  6094. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6095. is added as a child of the new `list_item`, to contain the text.
  6096. We thus obtain the final tree:
  6097. ``` tree
  6098. -> document
  6099. -> block_quote
  6100. paragraph
  6101. "Lorem ipsum dolor\nsit amet."
  6102. -> list (type=bullet tight=true bullet_char=-)
  6103. list_item
  6104. paragraph
  6105. "Qui *quodsi iracundia*"
  6106. -> list_item
  6107. -> paragraph
  6108. "aliquando id"
  6109. ```
  6110. ## From block structure to the final document {-}
  6111. Once all of the input has been parsed, all open blocks are closed.
  6112. We then "walk the tree," visiting every node, and parse raw
  6113. string contents of paragraphs and headers as inlines. At this
  6114. point we have seen all the link reference definitions, so we can
  6115. resolve reference links as we go.
  6116. ``` tree
  6117. document
  6118. block_quote
  6119. paragraph
  6120. str "Lorem ipsum dolor"
  6121. softbreak
  6122. str "sit amet."
  6123. list (type=bullet tight=true bullet_char=-)
  6124. list_item
  6125. paragraph
  6126. str "Qui "
  6127. emph
  6128. str "quodsi iracundia"
  6129. list_item
  6130. paragraph
  6131. str "aliquando id"
  6132. ```
  6133. Notice how the [line ending] in the first paragraph has
  6134. been parsed as a `softbreak`, and the asterisks in the first list item
  6135. have become an `emph`.
  6136. The document can be rendered as HTML, or in any other format, given
  6137. an appropriate renderer.