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