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