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