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