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