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