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