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