aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 0f397113524a6391d96cca1063962adec3eba4aa (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.18
  5. date: 2015-03-03
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions used for indicating formatting in email and
  12. usenet posts. It was developed in 2004 by John Gruber, who wrote
  13. the first Markdown-to-HTML converter in perl, and it soon became
  14. widely used in websites. By 2014 there were dozens of
  15. implementations in many languages. Some of them extended basic
  16. Markdown syntax with conventions for footnotes, definition lists,
  17. tables, and other constructs, and some allowed output not just in
  18. HTML but in LaTeX and many other formats.
  19. ## Why is a spec needed?
  20. John Gruber's [canonical description of Markdown's
  21. syntax](http://daringfireball.net/projects/markdown/syntax)
  22. does not specify the syntax unambiguously. Here are some examples of
  23. questions it does not answer:
  24. 1. How much indentation is needed for a sublist? The spec says that
  25. continuation paragraphs need to be indented four spaces, but is
  26. not fully explicit about sublists. It is natural to think that
  27. they, too, must be indented four spaces, but `Markdown.pl` does
  28. not require that. This is hardly a "corner case," and divergences
  29. between implementations on this issue often lead to surprises for
  30. users in real documents. (See [this comment by John
  31. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  32. 2. Is a blank line needed before a block quote or header?
  33. Most implementations do not require the blank line. However,
  34. this can lead to unexpected results in hard-wrapped text, and
  35. also to ambiguities in parsing (note that some implementations
  36. put the header inside the blockquote, while others do not).
  37. (John Gruber has also spoken [in favor of requiring the blank
  38. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  39. 3. Is a blank line needed before an indented code block?
  40. (`Markdown.pl` requires it, but this is not mentioned in the
  41. documentation, and some implementations do not require it.)
  42. ``` markdown
  43. paragraph
  44. code?
  45. ```
  46. 4. What is the exact rule for determining when list items get
  47. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  48. "tight"? What should we do with a list like this?
  49. ``` markdown
  50. 1. one
  51. 2. two
  52. 3. three
  53. ```
  54. Or this?
  55. ``` markdown
  56. 1. one
  57. - a
  58. - b
  59. 2. two
  60. ```
  61. (There are some relevant comments by John Gruber
  62. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  63. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  64. ``` markdown
  65. 8. item 1
  66. 9. item 2
  67. 10. item 2a
  68. ```
  69. 6. Is this one list with a horizontal rule in its second item,
  70. or two lists separated by a horizontal rule?
  71. ``` markdown
  72. * a
  73. * * * * *
  74. * b
  75. ```
  76. 7. When list markers change from numbers to bullets, do we have
  77. two lists or one? (The Markdown syntax description suggests two,
  78. but the perl scripts and many other implementations produce one.)
  79. ``` markdown
  80. 1. fee
  81. 2. fie
  82. - foe
  83. - fum
  84. ```
  85. 8. What are the precedence rules for the markers of inline structure?
  86. For example, is the following a valid link, or does the code span
  87. take precedence ?
  88. ``` markdown
  89. [a backtick (`)](/url) and [another backtick (`)](/url).
  90. ```
  91. 9. What are the precedence rules for markers of emphasis and strong
  92. emphasis? For example, how should the following be parsed?
  93. ``` markdown
  94. *foo *bar* baz*
  95. ```
  96. 10. What are the precedence rules between block-level and inline-level
  97. structure? For example, how should the following be parsed?
  98. ``` markdown
  99. - `a long code span can contain a hyphen like this
  100. - and it can screw things up`
  101. ```
  102. 11. Can list items include section headers? (`Markdown.pl` does not
  103. allow this, but does allow blockquotes to include headers.)
  104. ``` markdown
  105. - # Heading
  106. ```
  107. 12. Can list items be empty?
  108. ``` markdown
  109. * a
  110. *
  111. * b
  112. ```
  113. 13. Can link references be defined inside block quotes or list items?
  114. ``` markdown
  115. > Blockquote [foo].
  116. >
  117. > [foo]: /url
  118. ```
  119. 14. If there are multiple definitions for the same reference, which takes
  120. precedence?
  121. ``` markdown
  122. [foo]: /url1
  123. [foo]: /url2
  124. [foo][]
  125. ```
  126. In the absence of a spec, early implementers consulted `Markdown.pl`
  127. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  128. gave manifestly bad results in many cases, so it was not a
  129. satisfactory replacement for a spec.
  130. Because there is no unambiguous spec, implementations have diverged
  131. considerably. As a result, users are often surprised to find that
  132. a document that renders one way on one system (say, a github wiki)
  133. renders differently on another (say, converting to docbook using
  134. pandoc). To make matters worse, because nothing in Markdown counts
  135. as a "syntax error," the divergence often isn't discovered right away.
  136. ## About this document
  137. This document attempts to specify Markdown syntax unambiguously.
  138. It contains many examples with side-by-side Markdown and
  139. HTML. These are intended to double as conformance tests. An
  140. accompanying script `spec_tests.py` can be used to run the tests
  141. against any Markdown program:
  142. python test/spec_tests.py --spec spec.txt --program PROGRAM
  143. Since this document describes how Markdown is to be parsed into
  144. an abstract syntax tree, it would have made sense to use an abstract
  145. representation of the syntax tree instead of HTML. But HTML is capable
  146. of representing the structural distinctions we need to make, and the
  147. choice of HTML for the tests makes it possible to run the tests against
  148. an implementation without writing an abstract syntax tree renderer.
  149. This document is generated from a text file, `spec.txt`, written
  150. in Markdown with a small extension for the side-by-side tests.
  151. The script `tools/makespec.py` can be used to convert `spec.txt` into
  152. HTML or CommonMark (which can then be converted into other formats).
  153. In the examples, the `→` character is used to represent tabs.
  154. # Preliminaries
  155. ## Characters and lines
  156. Any sequence of [character]s is a valid CommonMark
  157. document.
  158. A [character](@character) is a unicode code point.
  159. This spec does not specify an encoding; it thinks of lines as composed
  160. of characters rather than bytes. A conforming parser may be limited
  161. to a certain encoding.
  162. A [line](@line) is a sequence of zero or more [character]s
  163. followed by a [line ending] or by the end of file.
  164. A [line ending](@line-ending) is, depending on the platform, a
  165. newline (`U+000A`), carriage return (`U+000D`), or
  166. carriage return + newline.
  167. For security reasons, a conforming parser must strip or replace the
  168. Unicode character `U+0000`.
  169. A line containing no characters, or a line containing only spaces
  170. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@blank-line).
  171. The following definitions of character classes will be used in this spec:
  172. A [whitespace character](@whitespace-character) is a space
  173. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  174. form feed (`U+000C`), or carriage return (`U+000D`).
  175. [Whitespace](@whitespace) is a sequence of one or more [whitespace
  176. character]s.
  177. A [unicode whitespace character](@unicode-whitespace-character) is
  178. any code point in the unicode `Zs` class, or a tab (`U+0009`),
  179. carriage return (`U+000D`), newline (`U+000A`), or form feed
  180. (`U+000C`).
  181. [Unicode whitespace](@unicode-whitespace) is a sequence of one
  182. or more [unicode whitespace character]s.
  183. A [non-space character](@non-space-character) is anything but `U+0020`.
  184. An [ASCII punctuation character](@ascii-punctuation-character)
  185. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  186. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  187. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  188. A [punctuation character](@punctuation-character) is an [ASCII
  189. punctuation character] or anything in
  190. the unicode classes `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  191. ## Tab expansion
  192. Tabs in lines are expanded to spaces, with a tab stop of 4 characters:
  193. .
  194. →foo→baz→→bim
  195. .
  196. <pre><code>foo baz bim
  197. </code></pre>
  198. .
  199. .
  200. a→a
  201. ὐ→a
  202. .
  203. <pre><code>a a
  204. ὐ a
  205. </code></pre>
  206. .
  207. # Blocks and inlines
  208. We can think of a document as a sequence of
  209. [blocks](@block)---structural
  210. elements like paragraphs, block quotations,
  211. lists, headers, rules, and code blocks. Blocks can contain other
  212. blocks, or they can contain [inline](@inline) content:
  213. words, spaces, links, emphasized text, images, and inline code.
  214. ## Precedence
  215. Indicators of block structure always take precedence over indicators
  216. of inline structure. So, for example, the following is a list with
  217. two items, not a list with one item containing a code span:
  218. .
  219. - `one
  220. - two`
  221. .
  222. <ul>
  223. <li>`one</li>
  224. <li>two`</li>
  225. </ul>
  226. .
  227. This means that parsing can proceed in two steps: first, the block
  228. structure of the document can be discerned; second, text lines inside
  229. paragraphs, headers, and other block constructs can be parsed for inline
  230. structure. The second step requires information about link reference
  231. definitions that will be available only at the end of the first
  232. step. Note that the first step requires processing lines in sequence,
  233. but the second can be parallelized, since the inline parsing of
  234. one block element does not affect the inline parsing of any other.
  235. ## Container blocks and leaf blocks
  236. We can divide blocks into two types:
  237. [container block](@container-block)s,
  238. which can contain other blocks, and [leaf block](@leaf-block)s,
  239. which cannot.
  240. # Leaf blocks
  241. This section describes the different kinds of leaf block that make up a
  242. Markdown document.
  243. ## Horizontal rules
  244. A line consisting of 0-3 spaces of indentation, followed by a sequence
  245. of three or more matching `-`, `_`, or `*` characters, each followed
  246. optionally by any number of spaces, forms a
  247. [horizontal rule](@horizontal-rule).
  248. .
  249. ***
  250. ---
  251. ___
  252. .
  253. <hr />
  254. <hr />
  255. <hr />
  256. .
  257. Wrong characters:
  258. .
  259. +++
  260. .
  261. <p>+++</p>
  262. .
  263. .
  264. ===
  265. .
  266. <p>===</p>
  267. .
  268. Not enough characters:
  269. .
  270. --
  271. **
  272. __
  273. .
  274. <p>--
  275. **
  276. __</p>
  277. .
  278. One to three spaces indent are allowed:
  279. .
  280. ***
  281. ***
  282. ***
  283. .
  284. <hr />
  285. <hr />
  286. <hr />
  287. .
  288. Four spaces is too many:
  289. .
  290. ***
  291. .
  292. <pre><code>***
  293. </code></pre>
  294. .
  295. .
  296. Foo
  297. ***
  298. .
  299. <p>Foo
  300. ***</p>
  301. .
  302. More than three characters may be used:
  303. .
  304. _____________________________________
  305. .
  306. <hr />
  307. .
  308. Spaces are allowed between the characters:
  309. .
  310. - - -
  311. .
  312. <hr />
  313. .
  314. .
  315. ** * ** * ** * **
  316. .
  317. <hr />
  318. .
  319. .
  320. - - - -
  321. .
  322. <hr />
  323. .
  324. Spaces are allowed at the end:
  325. .
  326. - - - -
  327. .
  328. <hr />
  329. .
  330. However, no other characters may occur in the line:
  331. .
  332. _ _ _ _ a
  333. a------
  334. ---a---
  335. .
  336. <p>_ _ _ _ a</p>
  337. <p>a------</p>
  338. <p>---a---</p>
  339. .
  340. It is required that all of the [non-space character]s be the same.
  341. So, this is not a horizontal rule:
  342. .
  343. *-*
  344. .
  345. <p><em>-</em></p>
  346. .
  347. Horizontal rules do not need blank lines before or after:
  348. .
  349. - foo
  350. ***
  351. - bar
  352. .
  353. <ul>
  354. <li>foo</li>
  355. </ul>
  356. <hr />
  357. <ul>
  358. <li>bar</li>
  359. </ul>
  360. .
  361. Horizontal rules can interrupt a paragraph:
  362. .
  363. Foo
  364. ***
  365. bar
  366. .
  367. <p>Foo</p>
  368. <hr />
  369. <p>bar</p>
  370. .
  371. If a line of dashes that meets the above conditions for being a
  372. horizontal rule could also be interpreted as the underline of a [setext
  373. header], the interpretation as a
  374. [setext header] takes precedence. Thus, for example,
  375. this is a setext header, not a paragraph followed by a horizontal rule:
  376. .
  377. Foo
  378. ---
  379. bar
  380. .
  381. <h2>Foo</h2>
  382. <p>bar</p>
  383. .
  384. When both a horizontal rule and a list item are possible
  385. interpretations of a line, the horizontal rule takes precedence:
  386. .
  387. * Foo
  388. * * *
  389. * Bar
  390. .
  391. <ul>
  392. <li>Foo</li>
  393. </ul>
  394. <hr />
  395. <ul>
  396. <li>Bar</li>
  397. </ul>
  398. .
  399. If you want a horizontal rule in a list item, use a different bullet:
  400. .
  401. - Foo
  402. - * * *
  403. .
  404. <ul>
  405. <li>Foo</li>
  406. <li>
  407. <hr />
  408. </li>
  409. </ul>
  410. .
  411. ## ATX headers
  412. An [ATX header](@atx-header)
  413. consists of a string of characters, parsed as inline content, between an
  414. opening sequence of 1--6 unescaped `#` characters and an optional
  415. closing sequence of any number of `#` characters. The opening sequence
  416. of `#` characters cannot be followed directly by a
  417. [non-space character].
  418. The optional closing sequence of `#`s must be preceded by a space and may be
  419. followed by spaces only. The opening `#` character may be indented 0-3
  420. spaces. The raw contents of the header are stripped of leading and
  421. trailing spaces before being parsed as inline content. The header level
  422. is equal to the number of `#` characters in the opening sequence.
  423. Simple headers:
  424. .
  425. # foo
  426. ## foo
  427. ### foo
  428. #### foo
  429. ##### foo
  430. ###### foo
  431. .
  432. <h1>foo</h1>
  433. <h2>foo</h2>
  434. <h3>foo</h3>
  435. <h4>foo</h4>
  436. <h5>foo</h5>
  437. <h6>foo</h6>
  438. .
  439. More than six `#` characters is not a header:
  440. .
  441. ####### foo
  442. .
  443. <p>####### foo</p>
  444. .
  445. A space is required between the `#` characters and the header's
  446. contents. Note that many implementations currently do not require
  447. the space. However, the space was required by the [original ATX
  448. implementation](http://www.aaronsw.com/2002/atx/atx.py), and it helps
  449. prevent things like the following from being parsed as headers:
  450. .
  451. #5 bolt
  452. .
  453. <p>#5 bolt</p>
  454. .
  455. This is not a header, because the first `#` is escaped:
  456. .
  457. \## foo
  458. .
  459. <p>## foo</p>
  460. .
  461. Contents are parsed as inlines:
  462. .
  463. # foo *bar* \*baz\*
  464. .
  465. <h1>foo <em>bar</em> *baz*</h1>
  466. .
  467. Leading and trailing blanks are ignored in parsing inline content:
  468. .
  469. # foo
  470. .
  471. <h1>foo</h1>
  472. .
  473. One to three spaces indentation are allowed:
  474. .
  475. ### foo
  476. ## foo
  477. # foo
  478. .
  479. <h3>foo</h3>
  480. <h2>foo</h2>
  481. <h1>foo</h1>
  482. .
  483. Four spaces are too much:
  484. .
  485. # foo
  486. .
  487. <pre><code># foo
  488. </code></pre>
  489. .
  490. .
  491. foo
  492. # bar
  493. .
  494. <p>foo
  495. # bar</p>
  496. .
  497. A closing sequence of `#` characters is optional:
  498. .
  499. ## foo ##
  500. ### bar ###
  501. .
  502. <h2>foo</h2>
  503. <h3>bar</h3>
  504. .
  505. It need not be the same length as the opening sequence:
  506. .
  507. # foo ##################################
  508. ##### foo ##
  509. .
  510. <h1>foo</h1>
  511. <h5>foo</h5>
  512. .
  513. Spaces are allowed after the closing sequence:
  514. .
  515. ### foo ###
  516. .
  517. <h3>foo</h3>
  518. .
  519. A sequence of `#` characters with a
  520. [non-space character] following it
  521. is not a closing sequence, but counts as part of the contents of the
  522. header:
  523. .
  524. ### foo ### b
  525. .
  526. <h3>foo ### b</h3>
  527. .
  528. The closing sequence must be preceded by a space:
  529. .
  530. # foo#
  531. .
  532. <h1>foo#</h1>
  533. .
  534. Backslash-escaped `#` characters do not count as part
  535. of the closing sequence:
  536. .
  537. ### foo \###
  538. ## foo #\##
  539. # foo \#
  540. .
  541. <h3>foo ###</h3>
  542. <h2>foo ###</h2>
  543. <h1>foo #</h1>
  544. .
  545. ATX headers need not be separated from surrounding content by blank
  546. lines, and they can interrupt paragraphs:
  547. .
  548. ****
  549. ## foo
  550. ****
  551. .
  552. <hr />
  553. <h2>foo</h2>
  554. <hr />
  555. .
  556. .
  557. Foo bar
  558. # baz
  559. Bar foo
  560. .
  561. <p>Foo bar</p>
  562. <h1>baz</h1>
  563. <p>Bar foo</p>
  564. .
  565. ATX headers can be empty:
  566. .
  567. ##
  568. #
  569. ### ###
  570. .
  571. <h2></h2>
  572. <h1></h1>
  573. <h3></h3>
  574. .
  575. ## Setext headers
  576. A [setext header](@setext-header)
  577. consists of a line of text, containing at least one
  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 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 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 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 *Ls* 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 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. unicode 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 the corresponding
  3502. unicode codepoints in the AST.
  3503. .
  3504. &#X22; &#XD06; &#xcab;
  3505. .
  3506. <p>&quot; ആ ಫ</p>
  3507. .
  3508. Here are some nonentities:
  3509. .
  3510. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3511. .
  3512. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3513. .
  3514. Although HTML5 does accept some entities without a trailing semicolon
  3515. (such as `&copy`), these are not recognized as entities here, because it
  3516. makes the grammar too ambiguous:
  3517. .
  3518. &copy
  3519. .
  3520. <p>&amp;copy</p>
  3521. .
  3522. Strings that are not on the list of HTML5 named entities are not
  3523. recognized as entities either:
  3524. .
  3525. &MadeUpEntity;
  3526. .
  3527. <p>&amp;MadeUpEntity;</p>
  3528. .
  3529. Entities are recognized in any context besides code spans or
  3530. code blocks, including raw HTML, URLs, [link title]s, and
  3531. [fenced code block] [info string]s:
  3532. .
  3533. <a href="&ouml;&ouml;.html">
  3534. .
  3535. <p><a href="&ouml;&ouml;.html"></p>
  3536. .
  3537. .
  3538. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3539. .
  3540. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3541. .
  3542. .
  3543. [foo]
  3544. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3545. .
  3546. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3547. .
  3548. .
  3549. ``` f&ouml;&ouml;
  3550. foo
  3551. ```
  3552. .
  3553. <pre><code class="language-föö">foo
  3554. </code></pre>
  3555. .
  3556. Entities are treated as literal text in code spans and code blocks:
  3557. .
  3558. `f&ouml;&ouml;`
  3559. .
  3560. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3561. .
  3562. .
  3563. f&ouml;f&ouml;
  3564. .
  3565. <pre><code>f&amp;ouml;f&amp;ouml;
  3566. </code></pre>
  3567. .
  3568. ## Code spans
  3569. A [backtick string](@backtick-string)
  3570. is a string of one or more backtick characters (`` ` ``) that is neither
  3571. preceded nor followed by a backtick.
  3572. A [code span](@code-span) begins with a backtick string and ends with
  3573. a backtick string of equal length. The contents of the code span are
  3574. the characters between the two backtick strings, with leading and
  3575. trailing spaces and [line ending]s removed, and
  3576. [whitespace] collapsed to single spaces.
  3577. This is a simple code span:
  3578. .
  3579. `foo`
  3580. .
  3581. <p><code>foo</code></p>
  3582. .
  3583. Here two backticks are used, because the code contains a backtick.
  3584. This example also illustrates stripping of leading and trailing spaces:
  3585. .
  3586. `` foo ` bar ``
  3587. .
  3588. <p><code>foo ` bar</code></p>
  3589. .
  3590. This example shows the motivation for stripping leading and trailing
  3591. spaces:
  3592. .
  3593. ` `` `
  3594. .
  3595. <p><code>``</code></p>
  3596. .
  3597. [Line ending]s are treated like spaces:
  3598. .
  3599. ``
  3600. foo
  3601. ``
  3602. .
  3603. <p><code>foo</code></p>
  3604. .
  3605. Interior spaces and [line ending]s are collapsed into
  3606. single spaces, just as they would be by a browser:
  3607. .
  3608. `foo bar
  3609. baz`
  3610. .
  3611. <p><code>foo bar baz</code></p>
  3612. .
  3613. Q: Why not just leave the spaces, since browsers will collapse them
  3614. anyway? A: Because we might be targeting a non-HTML format, and we
  3615. shouldn't rely on HTML-specific rendering assumptions.
  3616. (Existing implementations differ in their treatment of internal
  3617. spaces and [line ending]s. Some, including `Markdown.pl` and
  3618. `showdown`, convert an internal [line ending] into a
  3619. `<br />` tag. But this makes things difficult for those who like to
  3620. hard-wrap their paragraphs, since a line break in the midst of a code
  3621. span will cause an unintended line break in the output. Others just
  3622. leave internal spaces as they are, which is fine if only HTML is being
  3623. targeted.)
  3624. .
  3625. `foo `` bar`
  3626. .
  3627. <p><code>foo `` bar</code></p>
  3628. .
  3629. Note that backslash escapes do not work in code spans. All backslashes
  3630. are treated literally:
  3631. .
  3632. `foo\`bar`
  3633. .
  3634. <p><code>foo\</code>bar`</p>
  3635. .
  3636. Backslash escapes are never needed, because one can always choose a
  3637. string of *n* backtick characters as delimiters, where the code does
  3638. not contain any strings of exactly *n* backtick characters.
  3639. Code span backticks have higher precedence than any other inline
  3640. constructs except HTML tags and autolinks. Thus, for example, this is
  3641. not parsed as emphasized text, since the second `*` is part of a code
  3642. span:
  3643. .
  3644. *foo`*`
  3645. .
  3646. <p>*foo<code>*</code></p>
  3647. .
  3648. And this is not parsed as a link:
  3649. .
  3650. [not a `link](/foo`)
  3651. .
  3652. <p>[not a <code>link](/foo</code>)</p>
  3653. .
  3654. Code spans, HTML tags, and autolinks have the same precedence.
  3655. Thus, this is code:
  3656. .
  3657. `<a href="`">`
  3658. .
  3659. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  3660. .
  3661. But this is an HTML tag:
  3662. .
  3663. <a href="`">`
  3664. .
  3665. <p><a href="`">`</p>
  3666. .
  3667. And this is code:
  3668. .
  3669. `<http://foo.bar.`baz>`
  3670. .
  3671. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  3672. .
  3673. But this is an autolink:
  3674. .
  3675. <http://foo.bar.`baz>`
  3676. .
  3677. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3678. .
  3679. When a backtick string is not closed by a matching backtick string,
  3680. we just have literal backticks:
  3681. .
  3682. ```foo``
  3683. .
  3684. <p>```foo``</p>
  3685. .
  3686. .
  3687. `foo
  3688. .
  3689. <p>`foo</p>
  3690. .
  3691. ## Emphasis and strong emphasis
  3692. John Gruber's original [Markdown syntax
  3693. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3694. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3695. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3696. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3697. > tag.
  3698. This is enough for most users, but these rules leave much undecided,
  3699. especially when it comes to nested emphasis. The original
  3700. `Markdown.pl` test suite makes it clear that triple `***` and
  3701. `___` delimiters can be used for strong emphasis, and most
  3702. implementations have also allowed the following patterns:
  3703. ``` markdown
  3704. ***strong emph***
  3705. ***strong** in emph*
  3706. ***emph* in strong**
  3707. **in strong *emph***
  3708. *in emph **strong***
  3709. ```
  3710. The following patterns are less widely supported, but the intent
  3711. is clear and they are useful (especially in contexts like bibliography
  3712. entries):
  3713. ``` markdown
  3714. *emph *with emph* in it*
  3715. **strong **with strong** in it**
  3716. ```
  3717. Many implementations have also restricted intraword emphasis to
  3718. the `*` forms, to avoid unwanted emphasis in words containing
  3719. internal underscores. (It is best practice to put these in code
  3720. spans, but users often do not.)
  3721. ``` markdown
  3722. internal emphasis: foo*bar*baz
  3723. no emphasis: foo_bar_baz
  3724. ```
  3725. The rules given below capture all of these patterns, while allowing
  3726. for efficient parsing strategies that do not backtrack.
  3727. First, some definitions. A [delimiter run](@delimiter-run) is either
  3728. a sequence of one or more `*` characters that is not preceded or
  3729. followed by a `*` character, or a sequence of one or more `_`
  3730. characters that is not preceded or followed by a `_` character.
  3731. A [left-flanking delimiter run](@left-flanking-delimiter-run) is
  3732. a [delimiter run] that is (a) not followed by [unicode whitespace],
  3733. and (b) either not followed by a [punctuation character], or
  3734. preceded by [unicode whitespace] or a [punctuation character] or
  3735. the beginning of a line.
  3736. A [right-flanking delimiter run](@right-flanking-delimiter-run) is
  3737. a [delimiter run] that is (a) not preceded by [unicode whitespace],
  3738. and (b) either not preceded by a [punctuation character], or
  3739. followed by [unicode whitespace] or a [punctuation character] or
  3740. the end of a line.
  3741. Here are some examples of delimiter runs.
  3742. - left-flanking but not right-flanking:
  3743. ```
  3744. ***abc
  3745. _abc
  3746. **"abc"
  3747. _"abc"
  3748. ```
  3749. - right-flanking but not left-flanking:
  3750. ```
  3751. abc***
  3752. abc_
  3753. "abc"**
  3754. _"abc"
  3755. ```
  3756. - Both left and right-flanking:
  3757. ```
  3758. abc***def
  3759. "abc"_"def"
  3760. ```
  3761. - Neither left nor right-flanking:
  3762. ```
  3763. abc *** def
  3764. a _ b
  3765. ```
  3766. (The idea of distinguishing left-flanking and right-flanking
  3767. delimiter runs based on the character before and the character
  3768. after comes from Roopesh Chander's
  3769. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  3770. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  3771. run," and its rules for distinguishing left- and right-flanking runs
  3772. are a bit more complex than the ones given here.)
  3773. The following rules define emphasis and strong emphasis:
  3774. 1. A single `*` character [can open emphasis](@can-open-emphasis)
  3775. iff (if and only if) it is part of a [left-flanking delimiter run].
  3776. 2. A single `_` character [can open emphasis] iff
  3777. it is part of a [left-flanking delimiter run]
  3778. and not part of a [right-flanking delimiter run].
  3779. 3. A single `*` character [can close emphasis](@can-close-emphasis)
  3780. iff it is part of a [right-flanking delimiter run].
  3781. 4. A single `_` character [can close emphasis]
  3782. iff it is part of a [right-flanking delimiter run]
  3783. and not part of a [left-flanking delimiter run].
  3784. 5. A double `**` [can open strong emphasis](@can-open-strong-emphasis)
  3785. iff it is part of a [left-flanking delimiter run].
  3786. 6. A double `__` [can open strong emphasis]
  3787. iff it is part of a [left-flanking delimiter run]
  3788. and not part of a [right-flanking delimiter run].
  3789. 7. A double `**` [can close strong emphasis](@can-close-strong-emphasis)
  3790. iff it is part of a [right-flanking delimiter run].
  3791. 8. A double `__` [can close strong emphasis]
  3792. iff it is part of a [right-flanking delimiter run]
  3793. and not part of a [left-flanking delimiter run].
  3794. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  3795. with a delimiter that [can close emphasis], and that uses the same
  3796. character (`_` or `*`) as the opening delimiter. There must
  3797. be a nonempty sequence of inlines between the open delimiter
  3798. and the closing delimiter; these form the contents of the emphasis
  3799. inline.
  3800. 10. Strong emphasis begins with a delimiter that
  3801. [can open strong emphasis] and ends with a delimiter that
  3802. [can close strong emphasis], and that uses the same character
  3803. (`_` or `*`) as the opening delimiter.
  3804. There must be a nonempty sequence of inlines between the open
  3805. delimiter and the closing delimiter; these form the contents of
  3806. the strong emphasis inline.
  3807. 11. 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. 12. A literal `_` character cannot occur at the beginning or end of
  3811. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3812. is backslash-escaped.
  3813. Where rules 1--12 above are compatible with multiple parsings,
  3814. the following principles resolve ambiguity:
  3815. 13. The number of nestings should be minimized. Thus, for example,
  3816. an interpretation `<strong>...</strong>` is always preferred to
  3817. `<em><em>...</em></em>`.
  3818. 14. An interpretation `<strong><em>...</em></strong>` is always
  3819. preferred to `<em><strong>..</strong></em>`.
  3820. 15. When two potential emphasis or strong emphasis spans overlap,
  3821. so that the second begins before the first ends and ends after
  3822. the first ends, the first takes precedence. Thus, for example,
  3823. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3824. than `*foo <em>bar* baz</em>`. For the same reason,
  3825. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3826. rather than `<strong>foo*bar</strong>`.
  3827. 16. When there are two potential emphasis or strong emphasis spans
  3828. with the same closing delimiter, the shorter one (the one that
  3829. opens later) takes precedence. Thus, for example,
  3830. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3831. rather than `<strong>foo **bar baz</strong>`.
  3832. 17. Inline code spans, links, images, and HTML tags group more tightly
  3833. than emphasis. So, when there is a choice between an interpretation
  3834. that contains one of these elements and one that does not, the
  3835. former always wins. Thus, for example, `*[foo*](bar)` is
  3836. parsed as `*<a href="bar">foo*</a>` rather than as
  3837. `<em>[foo</em>](bar)`.
  3838. These rules can be illustrated through a series of examples.
  3839. Rule 1:
  3840. .
  3841. *foo bar*
  3842. .
  3843. <p><em>foo bar</em></p>
  3844. .
  3845. This is not emphasis, because the opening `*` is followed by
  3846. whitespace, and hence not part of a [left-flanking delimiter run]:
  3847. .
  3848. a * foo bar*
  3849. .
  3850. <p>a * foo bar*</p>
  3851. .
  3852. This is not emphasis, because the opening `*` is preceded
  3853. by an alphanumeric and followed by punctuation, and hence
  3854. not part of a [left-flanking delimiter run]:
  3855. .
  3856. a*"foo"*
  3857. .
  3858. <p>a*&quot;foo&quot;*</p>
  3859. .
  3860. Unicode nonbreaking spaces count as whitespace, too:
  3861. .
  3862. * a *
  3863. .
  3864. <p>* a *</p>
  3865. .
  3866. Intraword emphasis with `*` is permitted:
  3867. .
  3868. foo*bar*
  3869. .
  3870. <p>foo<em>bar</em></p>
  3871. .
  3872. .
  3873. 5*6*78
  3874. .
  3875. <p>5<em>6</em>78</p>
  3876. .
  3877. Rule 2:
  3878. .
  3879. _foo bar_
  3880. .
  3881. <p><em>foo bar</em></p>
  3882. .
  3883. This is not emphasis, because the opening `_` is followed by
  3884. whitespace:
  3885. .
  3886. _ foo bar_
  3887. .
  3888. <p>_ foo bar_</p>
  3889. .
  3890. This is not emphasis, because the opening `_` is preceded
  3891. by an alphanumeric and followed by punctuation:
  3892. .
  3893. a_"foo"_
  3894. .
  3895. <p>a_&quot;foo&quot;_</p>
  3896. .
  3897. Emphasis with `_` is not allowed inside words:
  3898. .
  3899. foo_bar_
  3900. .
  3901. <p>foo_bar_</p>
  3902. .
  3903. .
  3904. 5_6_78
  3905. .
  3906. <p>5_6_78</p>
  3907. .
  3908. .
  3909. пристаням_стремятся_
  3910. .
  3911. <p>пристаням_стремятся_</p>
  3912. .
  3913. Here `_` does not generate emphasis, because the first delimiter run
  3914. is right-flanking and the second left-flanking:
  3915. .
  3916. aa_"bb"_cc
  3917. .
  3918. <p>aa_&quot;bb&quot;_cc</p>
  3919. .
  3920. Here there is no emphasis, because the delimiter runs are
  3921. both left- and right-flanking:
  3922. .
  3923. "aa"_"bb"_"cc"
  3924. .
  3925. <p>&quot;aa&quot;_&quot;bb&quot;_&quot;cc&quot;</p>
  3926. .
  3927. Rule 3:
  3928. This is not emphasis, because the closing delimiter does
  3929. not match the opening delimiter:
  3930. .
  3931. _foo*
  3932. .
  3933. <p>_foo*</p>
  3934. .
  3935. This is not emphasis, because the closing `*` is preceded by
  3936. whitespace:
  3937. .
  3938. *foo bar *
  3939. .
  3940. <p>*foo bar *</p>
  3941. .
  3942. A newline also counts as whitespace:
  3943. .
  3944. *foo bar
  3945. *
  3946. .
  3947. <p>*foo bar</p>
  3948. <ul>
  3949. <li></li>
  3950. </ul>
  3951. .
  3952. This is not emphasis, because the second `*` is
  3953. preceded by punctuation and followed by an alphanumeric
  3954. (hence it is not part of a [right-flanking delimiter run]:
  3955. .
  3956. *(*foo)
  3957. .
  3958. <p>*(*foo)</p>
  3959. .
  3960. The point of this restriction is more easily appreciated
  3961. with this example:
  3962. .
  3963. *(*foo*)*
  3964. .
  3965. <p><em>(<em>foo</em>)</em></p>
  3966. .
  3967. Intraword emphasis with `*` is allowed:
  3968. .
  3969. *foo*bar
  3970. .
  3971. <p><em>foo</em>bar</p>
  3972. .
  3973. Rule 4:
  3974. This is not emphasis, because the closing `_` is preceded by
  3975. whitespace:
  3976. .
  3977. _foo bar _
  3978. .
  3979. <p>_foo bar _</p>
  3980. .
  3981. This is not emphasis, because the second `_` is
  3982. preceded by punctuation and followed by an alphanumeric:
  3983. .
  3984. _(_foo)
  3985. .
  3986. <p>_(_foo)</p>
  3987. .
  3988. This is emphasis within emphasis:
  3989. .
  3990. _(_foo_)_
  3991. .
  3992. <p><em>(<em>foo</em>)</em></p>
  3993. .
  3994. Intraword emphasis is disallowed for `_`:
  3995. .
  3996. _foo_bar
  3997. .
  3998. <p>_foo_bar</p>
  3999. .
  4000. .
  4001. _пристаням_стремятся
  4002. .
  4003. <p>_пристаням_стремятся</p>
  4004. .
  4005. .
  4006. _foo_bar_baz_
  4007. .
  4008. <p><em>foo_bar_baz</em></p>
  4009. .
  4010. Rule 5:
  4011. .
  4012. **foo bar**
  4013. .
  4014. <p><strong>foo bar</strong></p>
  4015. .
  4016. This is not strong emphasis, because the opening delimiter is
  4017. followed by whitespace:
  4018. .
  4019. ** foo bar**
  4020. .
  4021. <p>** foo bar**</p>
  4022. .
  4023. This is not strong emphasis, because the opening `**` is preceded
  4024. by an alphanumeric and followed by punctuation, and hence
  4025. not part of a [left-flanking delimiter run]:
  4026. .
  4027. a**"foo"**
  4028. .
  4029. <p>a**&quot;foo&quot;**</p>
  4030. .
  4031. Intraword strong emphasis with `**` is permitted:
  4032. .
  4033. foo**bar**
  4034. .
  4035. <p>foo<strong>bar</strong></p>
  4036. .
  4037. Rule 6:
  4038. .
  4039. __foo bar__
  4040. .
  4041. <p><strong>foo bar</strong></p>
  4042. .
  4043. This is not strong emphasis, because the opening delimiter is
  4044. followed by whitespace:
  4045. .
  4046. __ foo bar__
  4047. .
  4048. <p>__ foo bar__</p>
  4049. .
  4050. A newline counts as whitespace:
  4051. .
  4052. __
  4053. foo bar__
  4054. .
  4055. <p>__
  4056. foo bar__</p>
  4057. .
  4058. This is not strong emphasis, because the opening `__` is preceded
  4059. by an alphanumeric and followed by punctuation:
  4060. .
  4061. a__"foo"__
  4062. .
  4063. <p>a__&quot;foo&quot;__</p>
  4064. .
  4065. Intraword strong emphasis is forbidden with `__`:
  4066. .
  4067. foo__bar__
  4068. .
  4069. <p>foo__bar__</p>
  4070. .
  4071. .
  4072. 5__6__78
  4073. .
  4074. <p>5__6__78</p>
  4075. .
  4076. .
  4077. пристаням__стремятся__
  4078. .
  4079. <p>пристаням__стремятся__</p>
  4080. .
  4081. .
  4082. __foo, __bar__, baz__
  4083. .
  4084. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4085. .
  4086. Rule 7:
  4087. This is not strong emphasis, because the closing delimiter is preceded
  4088. by whitespace:
  4089. .
  4090. **foo bar **
  4091. .
  4092. <p>**foo bar **</p>
  4093. .
  4094. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4095. Rule 11.)
  4096. This is not strong emphasis, because the second `**` is
  4097. preceded by punctuation and followed by an alphanumeric:
  4098. .
  4099. **(**foo)
  4100. .
  4101. <p>**(**foo)</p>
  4102. .
  4103. The point of this restriction is more easily appreciated
  4104. with these examples:
  4105. .
  4106. *(**foo**)*
  4107. .
  4108. <p><em>(<strong>foo</strong>)</em></p>
  4109. .
  4110. .
  4111. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4112. *Asclepias physocarpa*)**
  4113. .
  4114. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4115. <em>Asclepias physocarpa</em>)</strong></p>
  4116. .
  4117. .
  4118. **foo "*bar*" foo**
  4119. .
  4120. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4121. .
  4122. Intraword emphasis:
  4123. .
  4124. **foo**bar
  4125. .
  4126. <p><strong>foo</strong>bar</p>
  4127. .
  4128. Rule 8:
  4129. This is not strong emphasis, because the closing delimiter is
  4130. preceded by whitespace:
  4131. .
  4132. __foo bar __
  4133. .
  4134. <p>__foo bar __</p>
  4135. .
  4136. This is not strong emphasis, because the second `__` is
  4137. preceded by punctuation and followed by an alphanumeric:
  4138. .
  4139. __(__foo)
  4140. .
  4141. <p>__(__foo)</p>
  4142. .
  4143. The point of this restriction is more easily appreciated
  4144. with this example:
  4145. .
  4146. _(__foo__)_
  4147. .
  4148. <p><em>(<strong>foo</strong>)</em></p>
  4149. .
  4150. Intraword strong emphasis is forbidden with `__`:
  4151. .
  4152. __foo__bar
  4153. .
  4154. <p>__foo__bar</p>
  4155. .
  4156. .
  4157. __пристаням__стремятся
  4158. .
  4159. <p>__пристаням__стремятся</p>
  4160. .
  4161. .
  4162. __foo__bar__baz__
  4163. .
  4164. <p><strong>foo__bar__baz</strong></p>
  4165. .
  4166. Rule 9:
  4167. Any nonempty sequence of inline elements can be the contents of an
  4168. emphasized span.
  4169. .
  4170. *foo [bar](/url)*
  4171. .
  4172. <p><em>foo <a href="/url">bar</a></em></p>
  4173. .
  4174. .
  4175. *foo
  4176. bar*
  4177. .
  4178. <p><em>foo
  4179. bar</em></p>
  4180. .
  4181. In particular, emphasis and strong emphasis can be nested
  4182. inside emphasis:
  4183. .
  4184. _foo __bar__ baz_
  4185. .
  4186. <p><em>foo <strong>bar</strong> baz</em></p>
  4187. .
  4188. .
  4189. _foo _bar_ baz_
  4190. .
  4191. <p><em>foo <em>bar</em> baz</em></p>
  4192. .
  4193. .
  4194. __foo_ bar_
  4195. .
  4196. <p><em><em>foo</em> bar</em></p>
  4197. .
  4198. .
  4199. *foo *bar**
  4200. .
  4201. <p><em>foo <em>bar</em></em></p>
  4202. .
  4203. .
  4204. *foo **bar** baz*
  4205. .
  4206. <p><em>foo <strong>bar</strong> baz</em></p>
  4207. .
  4208. But note:
  4209. .
  4210. *foo**bar**baz*
  4211. .
  4212. <p><em>foo</em><em>bar</em><em>baz</em></p>
  4213. .
  4214. The difference is that in the preceding case, the internal delimiters
  4215. [can close emphasis], while in the cases with spaces, they cannot.
  4216. .
  4217. ***foo** bar*
  4218. .
  4219. <p><em><strong>foo</strong> bar</em></p>
  4220. .
  4221. .
  4222. *foo **bar***
  4223. .
  4224. <p><em>foo <strong>bar</strong></em></p>
  4225. .
  4226. Note, however, that in the following case we get no strong
  4227. emphasis, because the opening delimiter is closed by the first
  4228. `*` before `bar`:
  4229. .
  4230. *foo**bar***
  4231. .
  4232. <p><em>foo</em><em>bar</em>**</p>
  4233. .
  4234. Indefinite levels of nesting are possible:
  4235. .
  4236. *foo **bar *baz* bim** bop*
  4237. .
  4238. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  4239. .
  4240. .
  4241. *foo [*bar*](/url)*
  4242. .
  4243. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  4244. .
  4245. There can be no empty emphasis or strong emphasis:
  4246. .
  4247. ** is not an empty emphasis
  4248. .
  4249. <p>** is not an empty emphasis</p>
  4250. .
  4251. .
  4252. **** is not an empty strong emphasis
  4253. .
  4254. <p>**** is not an empty strong emphasis</p>
  4255. .
  4256. Rule 10:
  4257. Any nonempty sequence of inline elements can be the contents of an
  4258. strongly emphasized span.
  4259. .
  4260. **foo [bar](/url)**
  4261. .
  4262. <p><strong>foo <a href="/url">bar</a></strong></p>
  4263. .
  4264. .
  4265. **foo
  4266. bar**
  4267. .
  4268. <p><strong>foo
  4269. bar</strong></p>
  4270. .
  4271. In particular, emphasis and strong emphasis can be nested
  4272. inside strong emphasis:
  4273. .
  4274. __foo _bar_ baz__
  4275. .
  4276. <p><strong>foo <em>bar</em> baz</strong></p>
  4277. .
  4278. .
  4279. __foo __bar__ baz__
  4280. .
  4281. <p><strong>foo <strong>bar</strong> baz</strong></p>
  4282. .
  4283. .
  4284. ____foo__ bar__
  4285. .
  4286. <p><strong><strong>foo</strong> bar</strong></p>
  4287. .
  4288. .
  4289. **foo **bar****
  4290. .
  4291. <p><strong>foo <strong>bar</strong></strong></p>
  4292. .
  4293. .
  4294. **foo *bar* baz**
  4295. .
  4296. <p><strong>foo <em>bar</em> baz</strong></p>
  4297. .
  4298. But note:
  4299. .
  4300. **foo*bar*baz**
  4301. .
  4302. <p><em><em>foo</em>bar</em>baz**</p>
  4303. .
  4304. The difference is that in the preceding case, the internal delimiters
  4305. [can close emphasis], while in the cases with spaces, they cannot.
  4306. .
  4307. ***foo* bar**
  4308. .
  4309. <p><strong><em>foo</em> bar</strong></p>
  4310. .
  4311. .
  4312. **foo *bar***
  4313. .
  4314. <p><strong>foo <em>bar</em></strong></p>
  4315. .
  4316. Indefinite levels of nesting are possible:
  4317. .
  4318. **foo *bar **baz**
  4319. bim* bop**
  4320. .
  4321. <p><strong>foo <em>bar <strong>baz</strong>
  4322. bim</em> bop</strong></p>
  4323. .
  4324. .
  4325. **foo [*bar*](/url)**
  4326. .
  4327. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  4328. .
  4329. There can be no empty emphasis or strong emphasis:
  4330. .
  4331. __ is not an empty emphasis
  4332. .
  4333. <p>__ is not an empty emphasis</p>
  4334. .
  4335. .
  4336. ____ is not an empty strong emphasis
  4337. .
  4338. <p>____ is not an empty strong emphasis</p>
  4339. .
  4340. Rule 11:
  4341. .
  4342. foo ***
  4343. .
  4344. <p>foo ***</p>
  4345. .
  4346. .
  4347. foo *\**
  4348. .
  4349. <p>foo <em>*</em></p>
  4350. .
  4351. .
  4352. foo *_*
  4353. .
  4354. <p>foo <em>_</em></p>
  4355. .
  4356. .
  4357. foo *****
  4358. .
  4359. <p>foo *****</p>
  4360. .
  4361. .
  4362. foo **\***
  4363. .
  4364. <p>foo <strong>*</strong></p>
  4365. .
  4366. .
  4367. foo **_**
  4368. .
  4369. <p>foo <strong>_</strong></p>
  4370. .
  4371. Note that when delimiters do not match evenly, Rule 11 determines
  4372. that the excess literal `*` characters will appear outside of the
  4373. emphasis, rather than inside it:
  4374. .
  4375. **foo*
  4376. .
  4377. <p>*<em>foo</em></p>
  4378. .
  4379. .
  4380. *foo**
  4381. .
  4382. <p><em>foo</em>*</p>
  4383. .
  4384. .
  4385. ***foo**
  4386. .
  4387. <p>*<strong>foo</strong></p>
  4388. .
  4389. .
  4390. ****foo*
  4391. .
  4392. <p>***<em>foo</em></p>
  4393. .
  4394. .
  4395. **foo***
  4396. .
  4397. <p><strong>foo</strong>*</p>
  4398. .
  4399. .
  4400. *foo****
  4401. .
  4402. <p><em>foo</em>***</p>
  4403. .
  4404. Rule 12:
  4405. .
  4406. foo ___
  4407. .
  4408. <p>foo ___</p>
  4409. .
  4410. .
  4411. foo _\__
  4412. .
  4413. <p>foo <em>_</em></p>
  4414. .
  4415. .
  4416. foo _*_
  4417. .
  4418. <p>foo <em>*</em></p>
  4419. .
  4420. .
  4421. foo _____
  4422. .
  4423. <p>foo _____</p>
  4424. .
  4425. .
  4426. foo __\___
  4427. .
  4428. <p>foo <strong>_</strong></p>
  4429. .
  4430. .
  4431. foo __*__
  4432. .
  4433. <p>foo <strong>*</strong></p>
  4434. .
  4435. .
  4436. __foo_
  4437. .
  4438. <p>_<em>foo</em></p>
  4439. .
  4440. Note that when delimiters do not match evenly, Rule 12 determines
  4441. that the excess literal `_` characters will appear outside of the
  4442. emphasis, rather than inside it:
  4443. .
  4444. _foo__
  4445. .
  4446. <p><em>foo</em>_</p>
  4447. .
  4448. .
  4449. ___foo__
  4450. .
  4451. <p>_<strong>foo</strong></p>
  4452. .
  4453. .
  4454. ____foo_
  4455. .
  4456. <p>___<em>foo</em></p>
  4457. .
  4458. .
  4459. __foo___
  4460. .
  4461. <p><strong>foo</strong>_</p>
  4462. .
  4463. .
  4464. _foo____
  4465. .
  4466. <p><em>foo</em>___</p>
  4467. .
  4468. Rule 13 implies that if you want emphasis nested directly inside
  4469. emphasis, you must use different delimiters:
  4470. .
  4471. **foo**
  4472. .
  4473. <p><strong>foo</strong></p>
  4474. .
  4475. .
  4476. *_foo_*
  4477. .
  4478. <p><em><em>foo</em></em></p>
  4479. .
  4480. .
  4481. __foo__
  4482. .
  4483. <p><strong>foo</strong></p>
  4484. .
  4485. .
  4486. _*foo*_
  4487. .
  4488. <p><em><em>foo</em></em></p>
  4489. .
  4490. However, strong emphasis within strong emphasis is possible without
  4491. switching delimiters:
  4492. .
  4493. ****foo****
  4494. .
  4495. <p><strong><strong>foo</strong></strong></p>
  4496. .
  4497. .
  4498. ____foo____
  4499. .
  4500. <p><strong><strong>foo</strong></strong></p>
  4501. .
  4502. Rule 13 can be applied to arbitrarily long sequences of
  4503. delimiters:
  4504. .
  4505. ******foo******
  4506. .
  4507. <p><strong><strong><strong>foo</strong></strong></strong></p>
  4508. .
  4509. Rule 14:
  4510. .
  4511. ***foo***
  4512. .
  4513. <p><strong><em>foo</em></strong></p>
  4514. .
  4515. .
  4516. _____foo_____
  4517. .
  4518. <p><strong><strong><em>foo</em></strong></strong></p>
  4519. .
  4520. Rule 15:
  4521. .
  4522. *foo _bar* baz_
  4523. .
  4524. <p><em>foo _bar</em> baz_</p>
  4525. .
  4526. .
  4527. **foo*bar**
  4528. .
  4529. <p><em><em>foo</em>bar</em>*</p>
  4530. .
  4531. Rule 16:
  4532. .
  4533. **foo **bar baz**
  4534. .
  4535. <p>**foo <strong>bar baz</strong></p>
  4536. .
  4537. .
  4538. *foo *bar baz*
  4539. .
  4540. <p>*foo <em>bar baz</em></p>
  4541. .
  4542. Rule 17:
  4543. .
  4544. *[bar*](/url)
  4545. .
  4546. <p>*<a href="/url">bar*</a></p>
  4547. .
  4548. .
  4549. _foo [bar_](/url)
  4550. .
  4551. <p>_foo <a href="/url">bar_</a></p>
  4552. .
  4553. .
  4554. *<img src="foo" title="*"/>
  4555. .
  4556. <p>*<img src="foo" title="*"/></p>
  4557. .
  4558. .
  4559. **<a href="**">
  4560. .
  4561. <p>**<a href="**"></p>
  4562. .
  4563. .
  4564. __<a href="__">
  4565. .
  4566. <p>__<a href="__"></p>
  4567. .
  4568. .
  4569. *a `*`*
  4570. .
  4571. <p><em>a <code>*</code></em></p>
  4572. .
  4573. .
  4574. _a `_`_
  4575. .
  4576. <p><em>a <code>_</code></em></p>
  4577. .
  4578. .
  4579. **a<http://foo.bar/?q=**>
  4580. .
  4581. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  4582. .
  4583. .
  4584. __a<http://foo.bar/?q=__>
  4585. .
  4586. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  4587. .
  4588. ## Links
  4589. A link contains [link text] (the visible text), a [link destination]
  4590. (the URI that is the link destination), and optionally a [link title].
  4591. There are two basic kinds of links in Markdown. In [inline link]s the
  4592. destination and title are given immediately after the link text. In
  4593. [reference link]s the destination and title are defined elsewhere in
  4594. the document.
  4595. A [link text](@link-text) consists of a sequence of zero or more
  4596. inline elements enclosed by square brackets (`[` and `]`). The
  4597. following rules apply:
  4598. - Links may not contain other links, at any level of nesting.
  4599. - Brackets are allowed in the [link text] only if (a) they
  4600. are backslash-escaped or (b) they appear as a matched pair of brackets,
  4601. with an open bracket `[`, a sequence of zero or more inlines, and
  4602. a close bracket `]`.
  4603. - Backtick [code span]s, [autolink]s, and raw [HTML tag]s bind more tightly
  4604. than the brackets in link text. Thus, for example,
  4605. `` [foo`]` `` could not be a link text, since the second `]`
  4606. is part of a code span.
  4607. - The brackets in link text bind more tightly than markers for
  4608. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  4609. A [link destination](@link-destination) consists of either
  4610. - a sequence of zero or more characters between an opening `<` and a
  4611. closing `>` that contains no line breaks or unescaped `<` or `>`
  4612. characters, or
  4613. - a nonempty sequence of characters that does not include
  4614. ASCII space or control characters, and includes parentheses
  4615. only if (a) they are backslash-escaped or (b) they are part of
  4616. a balanced pair of unescaped parentheses that is not itself
  4617. inside a balanced pair of unescaped paretheses.
  4618. A [link title](@link-title) consists of either
  4619. - a sequence of zero or more characters between straight double-quote
  4620. characters (`"`), including a `"` character only if it is
  4621. backslash-escaped, or
  4622. - a sequence of zero or more characters between straight single-quote
  4623. characters (`'`), including a `'` character only if it is
  4624. backslash-escaped, or
  4625. - a sequence of zero or more characters between matching parentheses
  4626. (`(...)`), including a `)` character only if it is backslash-escaped.
  4627. Although [link title]s may span multiple lines, they may not contain
  4628. a [blank line].
  4629. An [inline link](@inline-link) consists of a [link text] followed immediately
  4630. by a left parenthesis `(`, optional [whitespace], an optional
  4631. [link destination], an optional [link title] separated from the link
  4632. destination by [whitespace], optional [whitespace], and a right
  4633. parenthesis `)`. The link's text consists of the inlines contained
  4634. in the [link text] (excluding the enclosing square brackets).
  4635. The link's URI consists of the link destination, excluding enclosing
  4636. `<...>` if present, with backslash-escapes in effect as described
  4637. above. The link's title consists of the link title, excluding its
  4638. enclosing delimiters, with backslash-escapes in effect as described
  4639. above.
  4640. Here is a simple inline link:
  4641. .
  4642. [link](/uri "title")
  4643. .
  4644. <p><a href="/uri" title="title">link</a></p>
  4645. .
  4646. The title may be omitted:
  4647. .
  4648. [link](/uri)
  4649. .
  4650. <p><a href="/uri">link</a></p>
  4651. .
  4652. Both the title and the destination may be omitted:
  4653. .
  4654. [link]()
  4655. .
  4656. <p><a href="">link</a></p>
  4657. .
  4658. .
  4659. [link](<>)
  4660. .
  4661. <p><a href="">link</a></p>
  4662. .
  4663. If the destination contains spaces, it must be enclosed in pointy
  4664. braces:
  4665. .
  4666. [link](/my uri)
  4667. .
  4668. <p>[link](/my uri)</p>
  4669. .
  4670. .
  4671. [link](</my uri>)
  4672. .
  4673. <p><a href="/my%20uri">link</a></p>
  4674. .
  4675. The destination cannot contain line breaks, even with pointy braces:
  4676. .
  4677. [link](foo
  4678. bar)
  4679. .
  4680. <p>[link](foo
  4681. bar)</p>
  4682. .
  4683. .
  4684. [link](<foo
  4685. bar>)
  4686. .
  4687. <p>[link](<foo
  4688. bar>)</p>
  4689. .
  4690. One level of balanced parentheses is allowed without escaping:
  4691. .
  4692. [link]((foo)and(bar))
  4693. .
  4694. <p><a href="(foo)and(bar)">link</a></p>
  4695. .
  4696. However, if you have parentheses within parentheses, you need to escape
  4697. or use the `<...>` form:
  4698. .
  4699. [link](foo(and(bar)))
  4700. .
  4701. <p>[link](foo(and(bar)))</p>
  4702. .
  4703. .
  4704. [link](foo(and\(bar\)))
  4705. .
  4706. <p><a href="foo(and(bar))">link</a></p>
  4707. .
  4708. .
  4709. [link](<foo(and(bar))>)
  4710. .
  4711. <p><a href="foo(and(bar))">link</a></p>
  4712. .
  4713. Parentheses and other symbols can also be escaped, as usual
  4714. in Markdown:
  4715. .
  4716. [link](foo\)\:)
  4717. .
  4718. <p><a href="foo):">link</a></p>
  4719. .
  4720. URL-escaping should be left alone inside the destination, as all
  4721. URL-escaped characters are also valid URL characters. HTML entities in
  4722. the destination will be parsed into the corresponding unicode
  4723. codepoints, as usual, and optionally URL-escaped when written as HTML.
  4724. .
  4725. [link](foo%20b&auml;)
  4726. .
  4727. <p><a href="foo%20b%C3%A4">link</a></p>
  4728. .
  4729. Note that, because titles can often be parsed as destinations,
  4730. if you try to omit the destination and keep the title, you'll
  4731. get unexpected results:
  4732. .
  4733. [link]("title")
  4734. .
  4735. <p><a href="%22title%22">link</a></p>
  4736. .
  4737. Titles may be in single quotes, double quotes, or parentheses:
  4738. .
  4739. [link](/url "title")
  4740. [link](/url 'title')
  4741. [link](/url (title))
  4742. .
  4743. <p><a href="/url" title="title">link</a>
  4744. <a href="/url" title="title">link</a>
  4745. <a href="/url" title="title">link</a></p>
  4746. .
  4747. Backslash escapes and entities may be used in titles:
  4748. .
  4749. [link](/url "title \"&quot;")
  4750. .
  4751. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  4752. .
  4753. Nested balanced quotes are not allowed without escaping:
  4754. .
  4755. [link](/url "title "and" title")
  4756. .
  4757. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  4758. .
  4759. But it is easy to work around this by using a different quote type:
  4760. .
  4761. [link](/url 'title "and" title')
  4762. .
  4763. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  4764. .
  4765. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  4766. title, and its test suite included a test demonstrating this.
  4767. But it is hard to see a good rationale for the extra complexity this
  4768. brings, since there are already many ways---backslash escaping,
  4769. entities, or using a different quote type for the enclosing title---to
  4770. write titles containing double quotes. `Markdown.pl`'s handling of
  4771. titles has a number of other strange features. For example, it allows
  4772. single-quoted titles in inline links, but not reference links. And, in
  4773. reference links but not inline links, it allows a title to begin with
  4774. `"` and end with `)`. `Markdown.pl` 1.0.1 even allows titles with no closing
  4775. quotation mark, though 1.0.2b8 does not. It seems preferable to adopt
  4776. a simple, rational rule that works the same way in inline links and
  4777. link reference definitions.)
  4778. [Whitespace] is allowed around the destination and title:
  4779. .
  4780. [link]( /uri
  4781. "title" )
  4782. .
  4783. <p><a href="/uri" title="title">link</a></p>
  4784. .
  4785. But it is not allowed between the link text and the
  4786. following parenthesis:
  4787. .
  4788. [link] (/uri)
  4789. .
  4790. <p>[link] (/uri)</p>
  4791. .
  4792. The link text may contain balanced brackets, but not unbalanced ones,
  4793. unless they are escaped:
  4794. .
  4795. [link [foo [bar]]](/uri)
  4796. .
  4797. <p><a href="/uri">link [foo [bar]]</a></p>
  4798. .
  4799. .
  4800. [link] bar](/uri)
  4801. .
  4802. <p>[link] bar](/uri)</p>
  4803. .
  4804. .
  4805. [link [bar](/uri)
  4806. .
  4807. <p>[link <a href="/uri">bar</a></p>
  4808. .
  4809. .
  4810. [link \[bar](/uri)
  4811. .
  4812. <p><a href="/uri">link [bar</a></p>
  4813. .
  4814. The link text may contain inline content:
  4815. .
  4816. [link *foo **bar** `#`*](/uri)
  4817. .
  4818. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4819. .
  4820. .
  4821. [![moon](moon.jpg)](/uri)
  4822. .
  4823. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4824. .
  4825. However, links may not contain other links, at any level of nesting.
  4826. .
  4827. [foo [bar](/uri)](/uri)
  4828. .
  4829. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  4830. .
  4831. .
  4832. [foo *[bar [baz](/uri)](/uri)*](/uri)
  4833. .
  4834. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  4835. .
  4836. .
  4837. ![[[foo](uri1)](uri2)](uri3)
  4838. .
  4839. <p><img src="uri3" alt="[foo](uri2)" /></p>
  4840. .
  4841. These cases illustrate the precedence of link text grouping over
  4842. emphasis grouping:
  4843. .
  4844. *[foo*](/uri)
  4845. .
  4846. <p>*<a href="/uri">foo*</a></p>
  4847. .
  4848. .
  4849. [foo *bar](baz*)
  4850. .
  4851. <p><a href="baz*">foo *bar</a></p>
  4852. .
  4853. Note that brackets that *aren't* part of links do not take
  4854. precedence:
  4855. .
  4856. *foo [bar* baz]
  4857. .
  4858. <p><em>foo [bar</em> baz]</p>
  4859. .
  4860. These cases illustrate the precedence of HTML tags, code spans,
  4861. and autolinks over link grouping:
  4862. .
  4863. [foo <bar attr="](baz)">
  4864. .
  4865. <p>[foo <bar attr="](baz)"></p>
  4866. .
  4867. .
  4868. [foo`](/uri)`
  4869. .
  4870. <p>[foo<code>](/uri)</code></p>
  4871. .
  4872. .
  4873. [foo<http://example.com/?search=](uri)>
  4874. .
  4875. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  4876. .
  4877. There are three kinds of [reference link](@reference-link)s:
  4878. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  4879. and [shortcut](#shortcut-reference-link).
  4880. A [full reference link](@full-reference-link)
  4881. consists of a [link text], optional [whitespace], and a [link label]
  4882. that [matches] a [link reference definition] elsewhere in the document.
  4883. A [link label](@link-label) begins with a left bracket (`[`) and ends
  4884. with the first right bracket (`]`) that is not backslash-escaped.
  4885. Unescaped square bracket characters are not allowed in
  4886. [link label]s. A link label can have at most 999
  4887. characters inside the square brackets.
  4888. One label [matches](@matches)
  4889. another just in case their normalized forms are equal. To normalize a
  4890. label, perform the *unicode case fold* and collapse consecutive internal
  4891. [whitespace] to a single space. If there are multiple
  4892. matching reference link definitions, the one that comes first in the
  4893. document is used. (It is desirable in such cases to emit a warning.)
  4894. The contents of the first link label are parsed as inlines, which are
  4895. used as the link's text. The link's URI and title are provided by the
  4896. matching [link reference definition].
  4897. Here is a simple example:
  4898. .
  4899. [foo][bar]
  4900. [bar]: /url "title"
  4901. .
  4902. <p><a href="/url" title="title">foo</a></p>
  4903. .
  4904. The rules for the [link text] are the same as with
  4905. [inline link]s. Thus:
  4906. The link text may contain balanced brackets, but not unbalanced ones,
  4907. unless they are escaped:
  4908. .
  4909. [link [foo [bar]]][ref]
  4910. [ref]: /uri
  4911. .
  4912. <p><a href="/uri">link [foo [bar]]</a></p>
  4913. .
  4914. .
  4915. [link \[bar][ref]
  4916. [ref]: /uri
  4917. .
  4918. <p><a href="/uri">link [bar</a></p>
  4919. .
  4920. The link text may contain inline content:
  4921. .
  4922. [link *foo **bar** `#`*][ref]
  4923. [ref]: /uri
  4924. .
  4925. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  4926. .
  4927. .
  4928. [![moon](moon.jpg)][ref]
  4929. [ref]: /uri
  4930. .
  4931. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  4932. .
  4933. However, links may not contain other links, at any level of nesting.
  4934. .
  4935. [foo [bar](/uri)][ref]
  4936. [ref]: /uri
  4937. .
  4938. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  4939. .
  4940. .
  4941. [foo *bar [baz][ref]*][ref]
  4942. [ref]: /uri
  4943. .
  4944. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  4945. .
  4946. (In the examples above, we have two [shortcut reference link]s
  4947. instead of one [full reference link].)
  4948. The following cases illustrate the precedence of link text grouping over
  4949. emphasis grouping:
  4950. .
  4951. *[foo*][ref]
  4952. [ref]: /uri
  4953. .
  4954. <p>*<a href="/uri">foo*</a></p>
  4955. .
  4956. .
  4957. [foo *bar][ref]
  4958. [ref]: /uri
  4959. .
  4960. <p><a href="/uri">foo *bar</a></p>
  4961. .
  4962. These cases illustrate the precedence of HTML tags, code spans,
  4963. and autolinks over link grouping:
  4964. .
  4965. [foo <bar attr="][ref]">
  4966. [ref]: /uri
  4967. .
  4968. <p>[foo <bar attr="][ref]"></p>
  4969. .
  4970. .
  4971. [foo`][ref]`
  4972. [ref]: /uri
  4973. .
  4974. <p>[foo<code>][ref]</code></p>
  4975. .
  4976. .
  4977. [foo<http://example.com/?search=][ref]>
  4978. [ref]: /uri
  4979. .
  4980. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  4981. .
  4982. Matching is case-insensitive:
  4983. .
  4984. [foo][BaR]
  4985. [bar]: /url "title"
  4986. .
  4987. <p><a href="/url" title="title">foo</a></p>
  4988. .
  4989. Unicode case fold is used:
  4990. .
  4991. [Толпой][Толпой] is a Russian word.
  4992. [ТОЛПОЙ]: /url
  4993. .
  4994. <p><a href="/url">Толпой</a> is a Russian word.</p>
  4995. .
  4996. Consecutive internal [whitespace] is treated as one space for
  4997. purposes of determining matching:
  4998. .
  4999. [Foo
  5000. bar]: /url
  5001. [Baz][Foo bar]
  5002. .
  5003. <p><a href="/url">Baz</a></p>
  5004. .
  5005. There can be [whitespace] between the [link text] and the [link label]:
  5006. .
  5007. [foo] [bar]
  5008. [bar]: /url "title"
  5009. .
  5010. <p><a href="/url" title="title">foo</a></p>
  5011. .
  5012. .
  5013. [foo]
  5014. [bar]
  5015. [bar]: /url "title"
  5016. .
  5017. <p><a href="/url" title="title">foo</a></p>
  5018. .
  5019. When there are multiple matching [link reference definition]s,
  5020. the first is used:
  5021. .
  5022. [foo]: /url1
  5023. [foo]: /url2
  5024. [bar][foo]
  5025. .
  5026. <p><a href="/url1">bar</a></p>
  5027. .
  5028. Note that matching is performed on normalized strings, not parsed
  5029. inline content. So the following does not match, even though the
  5030. labels define equivalent inline content:
  5031. .
  5032. [bar][foo\!]
  5033. [foo!]: /url
  5034. .
  5035. <p>[bar][foo!]</p>
  5036. .
  5037. [Link label]s cannot contain brackets, unless they are
  5038. backslash-escaped:
  5039. .
  5040. [foo][ref[]
  5041. [ref[]: /uri
  5042. .
  5043. <p>[foo][ref[]</p>
  5044. <p>[ref[]: /uri</p>
  5045. .
  5046. .
  5047. [foo][ref[bar]]
  5048. [ref[bar]]: /uri
  5049. .
  5050. <p>[foo][ref[bar]]</p>
  5051. <p>[ref[bar]]: /uri</p>
  5052. .
  5053. .
  5054. [[[foo]]]
  5055. [[[foo]]]: /url
  5056. .
  5057. <p>[[[foo]]]</p>
  5058. <p>[[[foo]]]: /url</p>
  5059. .
  5060. .
  5061. [foo][ref\[]
  5062. [ref\[]: /uri
  5063. .
  5064. <p><a href="/uri">foo</a></p>
  5065. .
  5066. A [collapsed reference link](@collapsed-reference-link)
  5067. consists of a [link label] that [matches] a
  5068. [link reference definition] elsewhere in the
  5069. document, optional [whitespace], and the string `[]`.
  5070. The contents of the first link label are parsed as inlines,
  5071. which are used as the link's text. The link's URI and title are
  5072. provided by the matching reference link definition. Thus,
  5073. `[foo][]` is equivalent to `[foo][foo]`.
  5074. .
  5075. [foo][]
  5076. [foo]: /url "title"
  5077. .
  5078. <p><a href="/url" title="title">foo</a></p>
  5079. .
  5080. .
  5081. [*foo* bar][]
  5082. [*foo* bar]: /url "title"
  5083. .
  5084. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5085. .
  5086. The link labels are case-insensitive:
  5087. .
  5088. [Foo][]
  5089. [foo]: /url "title"
  5090. .
  5091. <p><a href="/url" title="title">Foo</a></p>
  5092. .
  5093. As with full reference links, [whitespace] is allowed
  5094. between the two sets of brackets:
  5095. .
  5096. [foo]
  5097. []
  5098. [foo]: /url "title"
  5099. .
  5100. <p><a href="/url" title="title">foo</a></p>
  5101. .
  5102. A [shortcut reference link](@shortcut-reference-link)
  5103. consists of a [link label] that [matches] a
  5104. [link reference definition] elsewhere in the
  5105. document and is not followed by `[]` or a link label.
  5106. The contents of the first link label are parsed as inlines,
  5107. which are used as the link's text. the link's URI and title
  5108. are provided by the matching link reference definition.
  5109. Thus, `[foo]` is equivalent to `[foo][]`.
  5110. .
  5111. [foo]
  5112. [foo]: /url "title"
  5113. .
  5114. <p><a href="/url" title="title">foo</a></p>
  5115. .
  5116. .
  5117. [*foo* bar]
  5118. [*foo* bar]: /url "title"
  5119. .
  5120. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5121. .
  5122. .
  5123. [[*foo* bar]]
  5124. [*foo* bar]: /url "title"
  5125. .
  5126. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  5127. .
  5128. .
  5129. [[bar [foo]
  5130. [foo]: /url
  5131. .
  5132. <p>[[bar <a href="/url">foo</a></p>
  5133. .
  5134. The link labels are case-insensitive:
  5135. .
  5136. [Foo]
  5137. [foo]: /url "title"
  5138. .
  5139. <p><a href="/url" title="title">Foo</a></p>
  5140. .
  5141. A space after the link text should be preserved:
  5142. .
  5143. [foo] bar
  5144. [foo]: /url
  5145. .
  5146. <p><a href="/url">foo</a> bar</p>
  5147. .
  5148. If you just want bracketed text, you can backslash-escape the
  5149. opening bracket to avoid links:
  5150. .
  5151. \[foo]
  5152. [foo]: /url "title"
  5153. .
  5154. <p>[foo]</p>
  5155. .
  5156. Note that this is a link, because a link label ends with the first
  5157. following closing bracket:
  5158. .
  5159. [foo*]: /url
  5160. *[foo*]
  5161. .
  5162. <p>*<a href="/url">foo*</a></p>
  5163. .
  5164. Full references take precedence over shortcut references:
  5165. .
  5166. [foo][bar]
  5167. [foo]: /url1
  5168. [bar]: /url2
  5169. .
  5170. <p><a href="/url2">foo</a></p>
  5171. .
  5172. In the following case `[bar][baz]` is parsed as a reference,
  5173. `[foo]` as normal text:
  5174. .
  5175. [foo][bar][baz]
  5176. [baz]: /url
  5177. .
  5178. <p>[foo]<a href="/url">bar</a></p>
  5179. .
  5180. Here, though, `[foo][bar]` is parsed as a reference, since
  5181. `[bar]` is defined:
  5182. .
  5183. [foo][bar][baz]
  5184. [baz]: /url1
  5185. [bar]: /url2
  5186. .
  5187. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  5188. .
  5189. Here `[foo]` is not parsed as a shortcut reference, because it
  5190. is followed by a link label (even though `[bar]` is not defined):
  5191. .
  5192. [foo][bar][baz]
  5193. [baz]: /url1
  5194. [foo]: /url2
  5195. .
  5196. <p>[foo]<a href="/url1">bar</a></p>
  5197. .
  5198. ## Images
  5199. Syntax for images is like the syntax for links, with one
  5200. difference. Instead of [link text], we have an
  5201. [image description](@image-description). The rules for this are the
  5202. same as for [link text], except that (a) an
  5203. image description starts with `![` rather than `[`, and
  5204. (b) an image description may contain links.
  5205. An image description has inline elements
  5206. as its contents. When an image is rendered to HTML,
  5207. this is standardly used as the image's `alt` attribute.
  5208. .
  5209. ![foo](/url "title")
  5210. .
  5211. <p><img src="/url" alt="foo" title="title" /></p>
  5212. .
  5213. .
  5214. ![foo *bar*]
  5215. [foo *bar*]: train.jpg "train & tracks"
  5216. .
  5217. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5218. .
  5219. .
  5220. ![foo ![bar](/url)](/url2)
  5221. .
  5222. <p><img src="/url2" alt="foo bar" /></p>
  5223. .
  5224. .
  5225. ![foo [bar](/url)](/url2)
  5226. .
  5227. <p><img src="/url2" alt="foo bar" /></p>
  5228. .
  5229. Though this spec is concerned with parsing, not rendering, it is
  5230. recommended that in rendering to HTML, only the plain string content
  5231. of the [image description] be used. Note that in
  5232. the above example, the alt attribute's value is `foo bar`, not `foo
  5233. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  5234. content is rendered, without formatting.
  5235. .
  5236. ![foo *bar*][]
  5237. [foo *bar*]: train.jpg "train & tracks"
  5238. .
  5239. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5240. .
  5241. .
  5242. ![foo *bar*][foobar]
  5243. [FOOBAR]: train.jpg "train & tracks"
  5244. .
  5245. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  5246. .
  5247. .
  5248. ![foo](train.jpg)
  5249. .
  5250. <p><img src="train.jpg" alt="foo" /></p>
  5251. .
  5252. .
  5253. My ![foo bar](/path/to/train.jpg "title" )
  5254. .
  5255. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  5256. .
  5257. .
  5258. ![foo](<url>)
  5259. .
  5260. <p><img src="url" alt="foo" /></p>
  5261. .
  5262. .
  5263. ![](/url)
  5264. .
  5265. <p><img src="/url" alt="" /></p>
  5266. .
  5267. Reference-style:
  5268. .
  5269. ![foo] [bar]
  5270. [bar]: /url
  5271. .
  5272. <p><img src="/url" alt="foo" /></p>
  5273. .
  5274. .
  5275. ![foo] [bar]
  5276. [BAR]: /url
  5277. .
  5278. <p><img src="/url" alt="foo" /></p>
  5279. .
  5280. Collapsed:
  5281. .
  5282. ![foo][]
  5283. [foo]: /url "title"
  5284. .
  5285. <p><img src="/url" alt="foo" title="title" /></p>
  5286. .
  5287. .
  5288. ![*foo* bar][]
  5289. [*foo* bar]: /url "title"
  5290. .
  5291. <p><img src="/url" alt="foo bar" title="title" /></p>
  5292. .
  5293. The labels are case-insensitive:
  5294. .
  5295. ![Foo][]
  5296. [foo]: /url "title"
  5297. .
  5298. <p><img src="/url" alt="Foo" title="title" /></p>
  5299. .
  5300. As with full reference links, [whitespace] is allowed
  5301. between the two sets of brackets:
  5302. .
  5303. ![foo]
  5304. []
  5305. [foo]: /url "title"
  5306. .
  5307. <p><img src="/url" alt="foo" title="title" /></p>
  5308. .
  5309. Shortcut:
  5310. .
  5311. ![foo]
  5312. [foo]: /url "title"
  5313. .
  5314. <p><img src="/url" alt="foo" title="title" /></p>
  5315. .
  5316. .
  5317. ![*foo* bar]
  5318. [*foo* bar]: /url "title"
  5319. .
  5320. <p><img src="/url" alt="foo bar" title="title" /></p>
  5321. .
  5322. Note that link labels cannot contain unescaped brackets:
  5323. .
  5324. ![[foo]]
  5325. [[foo]]: /url "title"
  5326. .
  5327. <p>![[foo]]</p>
  5328. <p>[[foo]]: /url &quot;title&quot;</p>
  5329. .
  5330. The link labels are case-insensitive:
  5331. .
  5332. ![Foo]
  5333. [foo]: /url "title"
  5334. .
  5335. <p><img src="/url" alt="Foo" title="title" /></p>
  5336. .
  5337. If you just want bracketed text, you can backslash-escape the
  5338. opening `!` and `[`:
  5339. .
  5340. \!\[foo]
  5341. [foo]: /url "title"
  5342. .
  5343. <p>![foo]</p>
  5344. .
  5345. If you want a link after a literal `!`, backslash-escape the
  5346. `!`:
  5347. .
  5348. \![foo]
  5349. [foo]: /url "title"
  5350. .
  5351. <p>!<a href="/url" title="title">foo</a></p>
  5352. .
  5353. ## Autolinks
  5354. [Autolink](@autolink)s are absolute URIs and email addresses inside
  5355. `<` and `>`. They are parsed as links, with the URL or email address
  5356. as the link label.
  5357. A [URI autolink](@uri-autolink) consists of `<`, followed by an
  5358. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  5359. a link to the URI, with the URI as the link's label.
  5360. An [absolute URI](@absolute-uri),
  5361. for these purposes, consists of a [scheme] followed by a colon (`:`)
  5362. followed by zero or more characters other than ASCII
  5363. [whitespace] and control characters, `<`, and `>`. If
  5364. the URI includes these characters, you must use percent-encoding
  5365. (e.g. `%20` for a space).
  5366. The following [schemes](@scheme)
  5367. are recognized (case-insensitive):
  5368. `coap`, `doi`, `javascript`, `aaa`, `aaas`, `about`, `acap`, `cap`,
  5369. `cid`, `crid`, `data`, `dav`, `dict`, `dns`, `file`, `ftp`, `geo`, `go`,
  5370. `gopher`, `h323`, `http`, `https`, `iax`, `icap`, `im`, `imap`, `info`,
  5371. `ipp`, `iris`, `iris.beep`, `iris.xpc`, `iris.xpcs`, `iris.lwz`, `ldap`,
  5372. `mailto`, `mid`, `msrp`, `msrps`, `mtqp`, `mupdate`, `news`, `nfs`,
  5373. `ni`, `nih`, `nntp`, `opaquelocktoken`, `pop`, `pres`, `rtsp`,
  5374. `service`, `session`, `shttp`, `sieve`, `sip`, `sips`, `sms`, `snmp`,`
  5375. soap.beep`, `soap.beeps`, `tag`, `tel`, `telnet`, `tftp`, `thismessage`,
  5376. `tn3270`, `tip`, `tv`, `urn`, `vemmi`, `ws`, `wss`, `xcon`,
  5377. `xcon-userid`, `xmlrpc.beep`, `xmlrpc.beeps`, `xmpp`, `z39.50r`,
  5378. `z39.50s`, `adiumxtra`, `afp`, `afs`, `aim`, `apt`,` attachment`, `aw`,
  5379. `beshare`, `bitcoin`, `bolo`, `callto`, `chrome`,` chrome-extension`,
  5380. `com-eventbrite-attendee`, `content`, `cvs`,` dlna-playsingle`,
  5381. `dlna-playcontainer`, `dtn`, `dvb`, `ed2k`, `facetime`, `feed`,
  5382. `finger`, `fish`, `gg`, `git`, `gizmoproject`, `gtalk`, `hcp`, `icon`,
  5383. `ipn`, `irc`, `irc6`, `ircs`, `itms`, `jar`, `jms`, `keyparc`, `lastfm`,
  5384. `ldaps`, `magnet`, `maps`, `market`,` message`, `mms`, `ms-help`,
  5385. `msnim`, `mumble`, `mvn`, `notes`, `oid`, `palm`, `paparazzi`,
  5386. `platform`, `proxy`, `psyc`, `query`, `res`, `resource`, `rmi`, `rsync`,
  5387. `rtmp`, `secondlife`, `sftp`, `sgn`, `skype`, `smb`, `soldat`,
  5388. `spotify`, `ssh`, `steam`, `svn`, `teamspeak`, `things`, `udp`,
  5389. `unreal`, `ut2004`, `ventrilo`, `view-source`, `webcal`, `wtai`,
  5390. `wyciwyg`, `xfire`, `xri`, `ymsgr`.
  5391. Here are some valid autolinks:
  5392. .
  5393. <http://foo.bar.baz>
  5394. .
  5395. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  5396. .
  5397. .
  5398. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  5399. .
  5400. <p><a href="http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean">http://foo.bar.baz/test?q=hello&amp;id=22&amp;boolean</a></p>
  5401. .
  5402. .
  5403. <irc://foo.bar:2233/baz>
  5404. .
  5405. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  5406. .
  5407. Uppercase is also fine:
  5408. .
  5409. <MAILTO:FOO@BAR.BAZ>
  5410. .
  5411. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  5412. .
  5413. Spaces are not allowed in autolinks:
  5414. .
  5415. <http://foo.bar/baz bim>
  5416. .
  5417. <p>&lt;http://foo.bar/baz bim&gt;</p>
  5418. .
  5419. Backslash-escapes do not work inside autolinks:
  5420. .
  5421. <http://example.com/\[\>
  5422. .
  5423. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  5424. .
  5425. An [email autolink](@email-autolink)
  5426. consists of `<`, followed by an [email address],
  5427. followed by `>`. The link's label is the email address,
  5428. and the URL is `mailto:` followed by the email address.
  5429. An [email address](@email-address),
  5430. for these purposes, is anything that matches
  5431. the [non-normative regex from the HTML5
  5432. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  5433. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  5434. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  5435. Examples of email autolinks:
  5436. .
  5437. <foo@bar.example.com>
  5438. .
  5439. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  5440. .
  5441. .
  5442. <foo+special@Bar.baz-bar0.com>
  5443. .
  5444. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  5445. .
  5446. Backslash-escapes do not work inside email autolinks:
  5447. .
  5448. <foo\+@bar.example.com>
  5449. .
  5450. <p>&lt;foo+@bar.example.com&gt;</p>
  5451. .
  5452. These are not autolinks:
  5453. .
  5454. <>
  5455. .
  5456. <p>&lt;&gt;</p>
  5457. .
  5458. .
  5459. <heck://bing.bong>
  5460. .
  5461. <p>&lt;heck://bing.bong&gt;</p>
  5462. .
  5463. .
  5464. < http://foo.bar >
  5465. .
  5466. <p>&lt; http://foo.bar &gt;</p>
  5467. .
  5468. .
  5469. <foo.bar.baz>
  5470. .
  5471. <p>&lt;foo.bar.baz&gt;</p>
  5472. .
  5473. .
  5474. <localhost:5001/foo>
  5475. .
  5476. <p>&lt;localhost:5001/foo&gt;</p>
  5477. .
  5478. .
  5479. http://example.com
  5480. .
  5481. <p>http://example.com</p>
  5482. .
  5483. .
  5484. foo@bar.example.com
  5485. .
  5486. <p>foo@bar.example.com</p>
  5487. .
  5488. ## Raw HTML
  5489. Text between `<` and `>` that looks like an HTML tag is parsed as a
  5490. raw HTML tag and will be rendered in HTML without escaping.
  5491. Tag and attribute names are not limited to current HTML tags,
  5492. so custom tags (and even, say, DocBook tags) may be used.
  5493. Here is the grammar for tags:
  5494. A [tag name](@tag-name) consists of an ASCII letter
  5495. followed by zero or more ASCII letters or digits.
  5496. An [attribute](@attribute) consists of [whitespace],
  5497. an [attribute name], and an optional
  5498. [attribute value specification].
  5499. An [attribute name](@attribute-name)
  5500. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  5501. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  5502. specification restricted to ASCII. HTML5 is laxer.)
  5503. An [attribute value specification](@attribute-value-specification)
  5504. consists of optional [whitespace],
  5505. a `=` character, optional [whitespace], and an [attribute
  5506. value].
  5507. An [attribute value](@attribute-value)
  5508. consists of an [unquoted attribute value],
  5509. a [single-quoted attribute value], or a [double-quoted attribute value].
  5510. An [unquoted attribute value](@unquoted-attribute-value)
  5511. is a nonempty string of characters not
  5512. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  5513. A [single-quoted attribute value](@single-quoted-attribute-value)
  5514. consists of `'`, zero or more
  5515. characters not including `'`, and a final `'`.
  5516. A [double-quoted attribute value](@double-quoted-attribute-value)
  5517. consists of `"`, zero or more
  5518. characters not including `"`, and a final `"`.
  5519. An [open tag](@open-tag) consists of a `<` character, a [tag name],
  5520. zero or more [attributes], optional [whitespace], an optional `/`
  5521. character, and a `>` character.
  5522. A [closing tag](@closing-tag) consists of the string `</`, a
  5523. [tag name], optional [whitespace], and the character `>`.
  5524. An [HTML comment](@html-comment) consists of `<!--` + *text* + `-->`,
  5525. where *text* does not start with `>` or `->`, does not end with `-`,
  5526. and does not contain `--`. (See the
  5527. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  5528. A [processing instruction](@processing-instruction)
  5529. consists of the string `<?`, a string
  5530. of characters not including the string `?>`, and the string
  5531. `?>`.
  5532. A [declaration](@declaration) consists of the
  5533. string `<!`, a name consisting of one or more uppercase ASCII letters,
  5534. [whitespace], a string of characters not including the
  5535. character `>`, and the character `>`.
  5536. A [CDATA section](@cdata-section) consists of
  5537. the string `<![CDATA[`, a string of characters not including the string
  5538. `]]>`, and the string `]]>`.
  5539. An [HTML tag](@html-tag) consists of an [open tag], a [closing tag],
  5540. an [HTML comment], a [processing instruction], a [declaration],
  5541. or a [CDATA section].
  5542. Here are some simple open tags:
  5543. .
  5544. <a><bab><c2c>
  5545. .
  5546. <p><a><bab><c2c></p>
  5547. .
  5548. Empty elements:
  5549. .
  5550. <a/><b2/>
  5551. .
  5552. <p><a/><b2/></p>
  5553. .
  5554. [Whitespace] is allowed:
  5555. .
  5556. <a /><b2
  5557. data="foo" >
  5558. .
  5559. <p><a /><b2
  5560. data="foo" ></p>
  5561. .
  5562. With attributes:
  5563. .
  5564. <a foo="bar" bam = 'baz <em>"</em>'
  5565. _boolean zoop:33=zoop:33 />
  5566. .
  5567. <p><a foo="bar" bam = 'baz <em>"</em>'
  5568. _boolean zoop:33=zoop:33 /></p>
  5569. .
  5570. Illegal tag names, not parsed as HTML:
  5571. .
  5572. <33> <__>
  5573. .
  5574. <p>&lt;33&gt; &lt;__&gt;</p>
  5575. .
  5576. Illegal attribute names:
  5577. .
  5578. <a h*#ref="hi">
  5579. .
  5580. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  5581. .
  5582. Illegal attribute values:
  5583. .
  5584. <a href="hi'> <a href=hi'>
  5585. .
  5586. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  5587. .
  5588. Illegal [whitespace]:
  5589. .
  5590. < a><
  5591. foo><bar/ >
  5592. .
  5593. <p>&lt; a&gt;&lt;
  5594. foo&gt;&lt;bar/ &gt;</p>
  5595. .
  5596. Missing [whitespace]:
  5597. .
  5598. <a href='bar'title=title>
  5599. .
  5600. <p>&lt;a href='bar'title=title&gt;</p>
  5601. .
  5602. Closing tags:
  5603. .
  5604. </a>
  5605. </foo >
  5606. .
  5607. <p></a>
  5608. </foo ></p>
  5609. .
  5610. Illegal attributes in closing tag:
  5611. .
  5612. </a href="foo">
  5613. .
  5614. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  5615. .
  5616. Comments:
  5617. .
  5618. foo <!-- this is a
  5619. comment - with hyphen -->
  5620. .
  5621. <p>foo <!-- this is a
  5622. comment - with hyphen --></p>
  5623. .
  5624. .
  5625. foo <!-- not a comment -- two hyphens -->
  5626. .
  5627. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  5628. .
  5629. Not comments:
  5630. .
  5631. foo <!--> foo -->
  5632. foo <!-- foo--->
  5633. .
  5634. <p>foo &lt;!--&gt; foo --&gt;</p>
  5635. <p>foo &lt;!-- foo---&gt;</p>
  5636. .
  5637. Processing instructions:
  5638. .
  5639. foo <?php echo $a; ?>
  5640. .
  5641. <p>foo <?php echo $a; ?></p>
  5642. .
  5643. Declarations:
  5644. .
  5645. foo <!ELEMENT br EMPTY>
  5646. .
  5647. <p>foo <!ELEMENT br EMPTY></p>
  5648. .
  5649. CDATA sections:
  5650. .
  5651. foo <![CDATA[>&<]]>
  5652. .
  5653. <p>foo <![CDATA[>&<]]></p>
  5654. .
  5655. Entities are preserved in HTML attributes:
  5656. .
  5657. <a href="&ouml;">
  5658. .
  5659. <p><a href="&ouml;"></p>
  5660. .
  5661. Backslash escapes do not work in HTML attributes:
  5662. .
  5663. <a href="\*">
  5664. .
  5665. <p><a href="\*"></p>
  5666. .
  5667. .
  5668. <a href="\"">
  5669. .
  5670. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  5671. .
  5672. ## Hard line breaks
  5673. A line break (not in a code span or HTML tag) that is preceded
  5674. by two or more spaces and does not occur at the end of a block
  5675. is parsed as a [hard line break](@hard-line-break) (rendered
  5676. in HTML as a `<br />` tag):
  5677. .
  5678. foo
  5679. baz
  5680. .
  5681. <p>foo<br />
  5682. baz</p>
  5683. .
  5684. For a more visible alternative, a backslash before the
  5685. [line ending] may be used instead of two spaces:
  5686. .
  5687. foo\
  5688. baz
  5689. .
  5690. <p>foo<br />
  5691. baz</p>
  5692. .
  5693. More than two spaces can be used:
  5694. .
  5695. foo
  5696. baz
  5697. .
  5698. <p>foo<br />
  5699. baz</p>
  5700. .
  5701. Leading spaces at the beginning of the next line are ignored:
  5702. .
  5703. foo
  5704. bar
  5705. .
  5706. <p>foo<br />
  5707. bar</p>
  5708. .
  5709. .
  5710. foo\
  5711. bar
  5712. .
  5713. <p>foo<br />
  5714. bar</p>
  5715. .
  5716. Line breaks can occur inside emphasis, links, and other constructs
  5717. that allow inline content:
  5718. .
  5719. *foo
  5720. bar*
  5721. .
  5722. <p><em>foo<br />
  5723. bar</em></p>
  5724. .
  5725. .
  5726. *foo\
  5727. bar*
  5728. .
  5729. <p><em>foo<br />
  5730. bar</em></p>
  5731. .
  5732. Line breaks do not occur inside code spans
  5733. .
  5734. `code
  5735. span`
  5736. .
  5737. <p><code>code span</code></p>
  5738. .
  5739. .
  5740. `code\
  5741. span`
  5742. .
  5743. <p><code>code\ span</code></p>
  5744. .
  5745. or HTML tags:
  5746. .
  5747. <a href="foo
  5748. bar">
  5749. .
  5750. <p><a href="foo
  5751. bar"></p>
  5752. .
  5753. .
  5754. <a href="foo\
  5755. bar">
  5756. .
  5757. <p><a href="foo\
  5758. bar"></p>
  5759. .
  5760. Hard line breaks are for separating inline content within a block.
  5761. Neither syntax for hard line breaks works at the end of a paragraph or
  5762. other block element:
  5763. .
  5764. foo\
  5765. .
  5766. <p>foo\</p>
  5767. .
  5768. .
  5769. foo
  5770. .
  5771. <p>foo</p>
  5772. .
  5773. .
  5774. ### foo\
  5775. .
  5776. <h3>foo\</h3>
  5777. .
  5778. .
  5779. ### foo
  5780. .
  5781. <h3>foo</h3>
  5782. .
  5783. ## Soft line breaks
  5784. A regular line break (not in a code span or HTML tag) that is not
  5785. preceded by two or more spaces or a backslash is parsed as a
  5786. softbreak. (A softbreak may be rendered in HTML either as a
  5787. [line ending] or as a space. The result will be the same in
  5788. browsers. In the examples here, a [line ending] will be used.)
  5789. .
  5790. foo
  5791. baz
  5792. .
  5793. <p>foo
  5794. baz</p>
  5795. .
  5796. Spaces at the end of the line and beginning of the next line are
  5797. removed:
  5798. .
  5799. foo
  5800. baz
  5801. .
  5802. <p>foo
  5803. baz</p>
  5804. .
  5805. A conforming parser may render a soft line break in HTML either as a
  5806. line break or as a space.
  5807. A renderer may also provide an option to render soft line breaks
  5808. as hard line breaks.
  5809. ## Textual content
  5810. Any characters not given an interpretation by the above rules will
  5811. be parsed as plain textual content.
  5812. .
  5813. hello $.;'there
  5814. .
  5815. <p>hello $.;'there</p>
  5816. .
  5817. .
  5818. Foo χρῆν
  5819. .
  5820. <p>Foo χρῆν</p>
  5821. .
  5822. Internal spaces are preserved verbatim:
  5823. .
  5824. Multiple spaces
  5825. .
  5826. <p>Multiple spaces</p>
  5827. .
  5828. <!-- END TESTS -->
  5829. # Appendix A: A parsing strategy {-}
  5830. ## Overview {-}
  5831. Parsing has two phases:
  5832. 1. In the first phase, lines of input are consumed and the block
  5833. structure of the document---its division into paragraphs, block quotes,
  5834. list items, and so on---is constructed. Text is assigned to these
  5835. blocks but not parsed. Link reference definitions are parsed and a
  5836. map of links is constructed.
  5837. 2. In the second phase, the raw text contents of paragraphs and headers
  5838. are parsed into sequences of Markdown inline elements (strings,
  5839. code spans, links, emphasis, and so on), using the map of link
  5840. references constructed in phase 1.
  5841. ## The document tree {-}
  5842. At each point in processing, the document is represented as a tree of
  5843. **blocks**. The root of the tree is a `document` block. The `document`
  5844. may have any number of other blocks as **children**. These children
  5845. may, in turn, have other blocks as children. The last child of a block
  5846. is normally considered **open**, meaning that subsequent lines of input
  5847. can alter its contents. (Blocks that are not open are **closed**.)
  5848. Here, for example, is a possible document tree, with the open blocks
  5849. marked by arrows:
  5850. ``` tree
  5851. -> document
  5852. -> block_quote
  5853. paragraph
  5854. "Lorem ipsum dolor\nsit amet."
  5855. -> list (type=bullet tight=true bullet_char=-)
  5856. list_item
  5857. paragraph
  5858. "Qui *quodsi iracundia*"
  5859. -> list_item
  5860. -> paragraph
  5861. "aliquando id"
  5862. ```
  5863. ## How source lines alter the document tree {-}
  5864. Each line that is processed has an effect on this tree. The line is
  5865. analyzed and, depending on its contents, the document may be altered
  5866. in one or more of the following ways:
  5867. 1. One or more open blocks may be closed.
  5868. 2. One or more new blocks may be created as children of the
  5869. last open block.
  5870. 3. Text may be added to the last (deepest) open block remaining
  5871. on the tree.
  5872. Once a line has been incorporated into the tree in this way,
  5873. it can be discarded, so input can be read in a stream.
  5874. We can see how this works by considering how the tree above is
  5875. generated by four lines of Markdown:
  5876. ``` markdown
  5877. > Lorem ipsum dolor
  5878. sit amet.
  5879. > - Qui *quodsi iracundia*
  5880. > - aliquando id
  5881. ```
  5882. At the outset, our document model is just
  5883. ``` tree
  5884. -> document
  5885. ```
  5886. The first line of our text,
  5887. ``` markdown
  5888. > Lorem ipsum dolor
  5889. ```
  5890. causes a `block_quote` block to be created as a child of our
  5891. open `document` block, and a `paragraph` block as a child of
  5892. the `block_quote`. Then the text is added to the last open
  5893. block, the `paragraph`:
  5894. ``` tree
  5895. -> document
  5896. -> block_quote
  5897. -> paragraph
  5898. "Lorem ipsum dolor"
  5899. ```
  5900. The next line,
  5901. ``` markdown
  5902. sit amet.
  5903. ```
  5904. is a "lazy continuation" of the open `paragraph`, so it gets added
  5905. to the paragraph's text:
  5906. ``` tree
  5907. -> document
  5908. -> block_quote
  5909. -> paragraph
  5910. "Lorem ipsum dolor\nsit amet."
  5911. ```
  5912. The third line,
  5913. ``` markdown
  5914. > - Qui *quodsi iracundia*
  5915. ```
  5916. causes the `paragraph` block to be closed, and a new `list` block
  5917. opened as a child of the `block_quote`. A `list_item` is also
  5918. added as a child of the `list`, and a `paragraph` as a child of
  5919. the `list_item`. The text is then added to the new `paragraph`:
  5920. ``` tree
  5921. -> document
  5922. -> block_quote
  5923. paragraph
  5924. "Lorem ipsum dolor\nsit amet."
  5925. -> list (type=bullet tight=true bullet_char=-)
  5926. -> list_item
  5927. -> paragraph
  5928. "Qui *quodsi iracundia*"
  5929. ```
  5930. The fourth line,
  5931. ``` markdown
  5932. > - aliquando id
  5933. ```
  5934. causes the `list_item` (and its child the `paragraph`) to be closed,
  5935. and a new `list_item` opened up as child of the `list`. A `paragraph`
  5936. is added as a child of the new `list_item`, to contain the text.
  5937. We thus obtain the final tree:
  5938. ``` tree
  5939. -> document
  5940. -> block_quote
  5941. paragraph
  5942. "Lorem ipsum dolor\nsit amet."
  5943. -> list (type=bullet tight=true bullet_char=-)
  5944. list_item
  5945. paragraph
  5946. "Qui *quodsi iracundia*"
  5947. -> list_item
  5948. -> paragraph
  5949. "aliquando id"
  5950. ```
  5951. ## From block structure to the final document {-}
  5952. Once all of the input has been parsed, all open blocks are closed.
  5953. We then "walk the tree," visiting every node, and parse raw
  5954. string contents of paragraphs and headers as inlines. At this
  5955. point we have seen all the link reference definitions, so we can
  5956. resolve reference links as we go.
  5957. ``` tree
  5958. document
  5959. block_quote
  5960. paragraph
  5961. str "Lorem ipsum dolor"
  5962. softbreak
  5963. str "sit amet."
  5964. list (type=bullet tight=true bullet_char=-)
  5965. list_item
  5966. paragraph
  5967. str "Qui "
  5968. emph
  5969. str "quodsi iracundia"
  5970. list_item
  5971. paragraph
  5972. str "aliquando id"
  5973. ```
  5974. Notice how the [line ending] in the first paragraph has
  5975. been parsed as a `softbreak`, and the asterisks in the first list item
  5976. have become an `emph`.
  5977. The document can be rendered as HTML, or in any other format, given
  5978. an appropriate renderer.