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