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