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