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