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