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