ory.
aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 21bef8c52b517d409f1af0b6838b84a5a0e65934 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.17
  5. date: 2015-01-24
  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 `spec2md.pl` can be used to turn `spec.txt` into pandoc
  152. Markdown, 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, depending on the platform, a
  165. newline (`U+000A`), carriage return (`U+000D`), or
  166. carriage return + newline.
  167. For security reasons, a conforming parser must strip or replace the
  168. Unicode character `U+0000`.
  169. A line containing no characters, or a line containing only spaces
  170. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@blank-line).
  171. The following definitions of character classes will be used in this spec:
  172. A [whitespace character](@whitespace-character) is a space
  173. (`U+0020`), tab (`U+0009`), carriage return (`U+000D`), or
  174. newline (`U+000A`).
  175. [Whitespace](@whitespace) is a sequence of one or more [whitespace
  176. character]s.
  177. A [unicode whitespace character](@unicode-whitespace-character) is
  178. any code point in the unicode `Zs` class, or a tab (`U+0009`),
  179. carriage return (`U+000D`), newline (`U+000A`), or form feed
  180. (`U+000C`).
  181. [Unicode whitespace](@unicode-whitespace) is a sequence of one
  182. or more [unicode whitespace character]s.
  183. A [non-space character](@non-space-character) is anything but `U+0020`.
  184. An [ASCII punctuation character](@ascii-punctuation-character)
  185. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  186. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  187. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  188. A [punctuation character](@punctuation-character) is an [ASCII
  189. punctuation character] or anything in
  190. the unicode classes `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  191. ## Tab expansion
  192. Tabs in lines are expanded to spaces, with a tab stop of 4 characters:
  193. .
  194. →foo→baz→→bim
  195. .
  196. <pre><code>foo baz bim
  197. </code></pre>
  198. .
  199. .
  200. a→a
  201. ὐ→a
  202. .
  203. <pre><code>a a
  204. ὐ a
  205. </code></pre>
  206. .
  207. # Blocks and inlines
  208. We can think of a document as a sequence of
  209. [blocks](@block)---structural
  210. elements like paragraphs, block quotations,
  211. lists, headers, rules, and code blocks. Blocks can contain other
  212. blocks, or they can contain [inline](@inline) content:
  213. words, spaces, links, emphasized text, images, and inline code.
  214. ## Precedence
  215. Indicators of block structure always take precedence over indicators
  216. of inline structure. So, for example, the following is a list with
  217. two items, not a list with one item containing a code span:
  218. .
  219. - `one
  220. - two`
  221. .
  222. <ul>
  223. <li>`one</li>
  224. <li>two`</li>
  225. </ul>
  226. .
  227. This means that parsing can proceed in two steps: first, the block
  228. structure of the document can be discerned; second, text lines inside
  229. paragraphs, headers, and other block constructs can be parsed for inline
  230. structure. The second step requires information about link reference
  231. definitions that will be available only at the end of the first
  232. step. Note that the first step requires processing lines in sequence,
  233. but the second can be parallelized, since the inline parsing of
  234. one block element does not affect the inline parsing of any other.
  235. ## Container blocks and leaf blocks
  236. We can divide blocks into two types:
  237. [container block](@container-block)s,
  238. which can contain other blocks, and [leaf block](@leaf-block)s,
  239. which cannot.
  240. # Leaf blocks
  241. This section describes the different kinds of leaf block that make up a
  242. Markdown document.
  243. ## Horizontal rules
  244. A line consisting of 0-3 spaces of indentation, followed by a sequence
  245. of three or more matching `-`, `_`, or `*` characters, each followed
  246. optionally by any number of spaces, forms a
  247. [horizontal rule](@horizontal-rule).
  248. .
  249. ***
  250. ---
  251. ___
  252. .
  253. <hr />
  254. <hr />
  255. <hr />
  256. .
  257. Wrong characters:
  258. .
  259. +++
  260. .
  261. <p>+++</p>
  262. .
  263. .
  264. ===
  265. .
  266. <p>===</p>
  267. .
  268. Not enough characters:
  269. .
  270. --
  271. **
  272. __
  273. .
  274. <p>--
  275. **
  276. __</p>
  277. .
  278. One to three spaces indent are allowed:
  279. .
  280. ***
  281. ***
  282. ***
  283. .
  284. <hr />
  285. <hr />
  286. <hr />
  287. .
  288. Four spaces is too many:
  289. .
  290. ***
  291. .
  292. <pre><code>***
  293. </code></pre>
  294. .
  295. .
  296. Foo
  297. ***
  298. .
  299. <p>Foo
  300. ***</p>
  301. .
  302. More than three characters may be used:
  303. .
  304. _____________________________________
  305. .
  306. <hr />
  307. .
  308. Spaces are allowed between the characters:
  309. .
  310. - - -
  311. .
  312. <hr />
  313. .
  314. .
  315. ** * ** * ** * **
  316. .
  317. <hr />
  318. .
  319. .
  320. - - - -
  321. .
  322. <hr />
  323. .
  324. Spaces are allowed at the end:
  325. .
  326. - - - -
  327. .
  328. <hr />
  329. .
  330. However, no other characters may occur in the line:
  331. .
  332. _ _ _ _ a
  333. a------
  334. ---a---
  335. .
  336. <p>_ _ _ _ a</p>
  337. <p>a------</p>
  338. <p>---a---</p>
  339. .
  340. It is required that all of the [non-space character]s be the same.
  341. So, this is not a horizontal rule:
  342. .
  343. *-*
  344. .
  345. <p><em>-</em></p>
  346. .
  347. Horizontal rules do not need blank lines before or after:
  348. .
  349. - foo
  350. ***
  351. - bar
  352. .
  353. <ul>
  354. <li>foo</li>
  355. </ul>
  356. <hr />
  357. <ul>
  358. <li>bar</li>
  359. </ul>
  360. .
  361. Horizontal rules can interrupt a paragraph:
  362. .
  363. Foo
  364. ***
  365. bar
  366. .
  367. <p>Foo</p>
  368. <hr />
  369. <p>bar</p>
  370. .
  371. If a line of dashes that meets the above conditions for being a
  372. horizontal rule could also be interpreted as the underline of a [setext
  373. header], the interpretation as a
  374. [setext header] takes precedence. Thus, for example,
  375. this is a setext header, not a paragraph followed by a horizontal rule:
  376. .
  377. Foo
  378. ---
  379. bar
  380. .
  381. <h2>Foo</h2>
  382. <p>bar</p>
  383. .
  384. When both a horizontal rule and a list item are possible
  385. interpretations of a line, the horizontal rule takes precedence:
  386. .
  387. * Foo
  388. * * *
  389. * Bar
  390. .
  391. <ul>
  392. <li>Foo</li>
  393. </ul>
  394. <hr />
  395. <ul>
  396. <li>Bar</li>
  397. </ul>
  398. .
  399. If you want a horizontal rule in a list item, use a different bullet:
  400. .
  401. - Foo
  402. - * * *
  403. .
  404. <ul>
  405. <li>Foo</li>
  406. <li>
  407. <hr />
  408. </li>
  409. </ul>
  410. .
  411. ## ATX headers
  412. An [ATX header](@atx-header)
  413. consists of a string of characters, parsed as inline content, between an
  414. opening sequence of 1--6 unescaped `#` characters and an optional
  415. closing sequence of any number of `#` characters. The opening sequence
  416. of `#` characters cannot be followed directly by a
  417. [non-space character].
  418. The optional closing sequence of `#`s must be preceded by a space and may be
  419. followed by spaces only. The opening `#` character may be indented 0-3
  420. spaces. The raw contents of the header are stripped of leading and
  421. trailing spaces before being parsed as inline content. The header level
  422. is equal to the number of `#` characters in the opening sequence.
  423. Simple headers:
  424. .
  425. # foo
  426. ## foo
  427. ### foo
  428. #### foo
  429. ##### foo
  430. ###### foo
  431. .
  432. <h1>foo</h1>
  433. <h2>foo</h2>
  434. <h3>foo</h3>
  435. <h4>foo</h4>
  436. <h5>foo</h5>
  437. <h6>foo</h6>
  438. .
  439. More than six `#` characters is not a header:
  440. .
  441. ####### foo
  442. .
  443. <p>####### foo</p>
  444. .
  445. A space is required between the `#` characters and the header's
  446. contents. Note that many implementations currently do not require
  447. the space. However, the space was required by the [original ATX
  448. implementation](http://www.aaronsw.com/2002/atx/atx.py), and it helps
  449. prevent things like the following from being parsed as headers:
  450. .
  451. #5 bolt
  452. .
  453. <p>#5 bolt</p>
  454. .
  455. This is not a header, because the first `#` is escaped:
  456. .
  457. \## foo
  458. .
  459. <p>## foo</p>
  460. .
  461. Contents are parsed as inlines:
  462. .
  463. # foo *bar* \*baz\*
  464. .
  465. <h1>foo <em>bar</em> *baz*</h1>
  466. .
  467. Leading and trailing blanks are ignored in parsing inline content:
  468. .
  469. # foo
  470. .
  471. <h1>foo</h1>
  472. .
  473. One to three spaces indentation are allowed:
  474. .
  475. ### foo
  476. ## foo
  477. # foo
  478. .
  479. <h3>foo</h3>
  480. <h2>foo</h2>
  481. <h1>foo</h1>
  482. .
  483. Four spaces are too much:
  484. .
  485. # foo
  486. .
  487. <pre><code># foo
  488. </code></pre>
  489. .
  490. .
  491. foo
  492. # bar
  493. .
  494. <p>foo
  495. # bar</p>
  496. .
  497. A closing sequence of `#` characters is optional:
  498. .
  499. ## foo ##
  500. ### bar ###
  501. .
  502. <h2>foo</h2>
  503. <h3>bar</h3>
  504. .
  505. It need not be the same length as the opening sequence:
  506. .
  507. # foo ##################################
  508. ##### foo ##
  509. .
  510. <h1>foo</h1>
  511. <h5>foo</h5>
  512. .
  513. Spaces are allowed after the closing sequence:
  514. .
  515. ### foo ###
  516. .
  517. <h3>foo</h3>
  518. .
  519. A sequence of `#` characters with a
  520. [non-space character] following it
  521. is not a closing sequence, but counts as part of the contents of the
  522. header:
  523. .
  524. ### foo ### b
  525. .
  526. <h3>foo ### b</h3>
  527. .
  528. The closing sequence must be preceded by a space:
  529. .
  530. # foo#
  531. .
  532. <h1>foo#</h1>
  533. .
  534. Backslash-escaped `#` characters do not count as part
  535. of the closing sequence:
  536. .
  537. ### foo \###
  538. ## foo #\##
  539. # foo \#
  540. .
  541. <h3>foo ###</h3>
  542. <h2>foo ###</h2>
  543. <h1>foo #</h1>
  544. .
  545. ATX headers need not be separated from surrounding content by blank
  546. lines, and they can interrupt paragraphs:
  547. .
  548. ****
  549. ## foo
  550. ****
  551. .
  552. <hr />
  553. <h2>foo</h2>
  554. <hr />
  555. .
  556. .
  557. Foo bar
  558. # baz
  559. Bar foo
  560. .
  561. <p>Foo bar</p>
  562. <h1>baz</h1>
  563. <p>Bar foo</p>
  564. .
  565. ATX headers can be empty:
  566. .
  567. ##
  568. #
  569. ### ###
  570. .
  571. <h2></h2>
  572. <h1></h1>
  573. <h3></h3>
  574. .
  575. ## Setext headers
  576. A [setext header](@setext-header)
  577. consists of a line of text, containing at least one
  578. [non-space character],
  579. with no more than 3 spaces indentation, followed by a [setext header
  580. underline]. The line of text must be
  581. one that, were it not followed by the setext header underline,
  582. would be interpreted as part of a paragraph: it cannot be a code
  583. block, header, blockquote, horizontal rule, or list.
  584. A [setext header underline](@setext-header-underline) is a sequence of
  585. `=` characters or a sequence of `-` characters, with no more than 3
  586. spaces indentation and any number of trailing spaces. If a line
  587. containing a single `-` can be interpreted as an
  588. empty [list items], it should be interpreted this way
  589. and not as a [setext header underline].
  590. The header is a level 1 header if `=` characters are used in the
  591. [setext header underline], and a level 2
  592. header if `-` characters are used. The contents of the header are the
  593. result of parsing the first line as Markdown inline content.
  594. In general, a setext header need not be preceded or followed by a
  595. blank line. However, it cannot interrupt a paragraph, so when a
  596. setext header comes after a paragraph, a blank line is needed between
  597. them.
  598. Simple examples:
  599. .
  600. Foo *bar*
  601. =========
  602. Foo *bar*
  603. ---------
  604. .
  605. <h1>Foo <em>bar</em></h1>
  606. <h2>Foo <em>bar</em></h2>
  607. .
  608. The underlining can be any length:
  609. .
  610. Foo
  611. -------------------------
  612. Foo
  613. =
  614. .
  615. <h2>Foo</h2>
  616. <h1>Foo</h1>
  617. .
  618. The header content can be indented up to three spaces, and need
  619. not line up with the underlining:
  620. .
  621. Foo
  622. ---
  623. Foo
  624. -----
  625. Foo
  626. ===
  627. .
  628. <h2>Foo</h2>
  629. <h2>Foo</h2>
  630. <h1>Foo</h1>
  631. .
  632. Four spaces indent is too much:
  633. .
  634. Foo
  635. ---
  636. Foo
  637. ---
  638. .
  639. <pre><code>Foo
  640. ---
  641. Foo
  642. </code></pre>
  643. <hr />
  644. .
  645. The setext header underline can be indented up to three spaces, and
  646. may have trailing spaces:
  647. .
  648. Foo
  649. ----
  650. .
  651. <h2>Foo</h2>
  652. .
  653. Four spaces is too much:
  654. .
  655. Foo
  656. ---
  657. .
  658. <p>Foo
  659. ---</p>
  660. .
  661. The setext header underline cannot contain internal spaces:
  662. .
  663. Foo
  664. = =
  665. Foo
  666. --- -
  667. .
  668. <p>Foo
  669. = =</p>
  670. <p>Foo</p>
  671. <hr />
  672. .
  673. Trailing spaces in the content line do not cause a line break:
  674. .
  675. Foo
  676. -----
  677. .
  678. <h2>Foo</h2>
  679. .
  680. Nor does a backslash at the end:
  681. .
  682. Foo\
  683. ----
  684. .
  685. <h2>Foo\</h2>
  686. .
  687. Since indicators of block structure take precedence over
  688. indicators of inline structure, the following are setext headers:
  689. .
  690. `Foo
  691. ----
  692. `
  693. <a title="a lot
  694. ---
  695. of dashes"/>
  696. .
  697. <h2>`Foo</h2>
  698. <p>`</p>
  699. <h2>&lt;a title=&quot;a lot</h2>
  700. <p>of dashes&quot;/&gt;</p>
  701. .
  702. The setext header underline cannot be a [lazy continuation
  703. line] in a list item or block quote:
  704. .
  705. > Foo
  706. ---
  707. .
  708. <blockquote>
  709. <p>Foo</p>
  710. </blockquote>
  711. <hr />
  712. .
  713. .
  714. - Foo
  715. ---
  716. .
  717. <ul>
  718. <li>Foo</li>
  719. </ul>
  720. <hr />
  721. .
  722. A setext header cannot interrupt a paragraph:
  723. .
  724. Foo
  725. Bar
  726. ---
  727. Foo
  728. Bar
  729. ===
  730. .
  731. <p>Foo
  732. Bar</p>
  733. <hr />
  734. <p>Foo
  735. Bar
  736. ===</p>
  737. .
  738. But in general a blank line is not required before or after:
  739. .
  740. ---
  741. Foo
  742. ---
  743. Bar
  744. ---
  745. Baz
  746. .
  747. <hr />
  748. <h2>Foo</h2>
  749. <h2>Bar</h2>
  750. <p>Baz</p>
  751. .
  752. Setext headers cannot be empty:
  753. .
  754. ====
  755. .
  756. <p>====</p>
  757. .
  758. Setext header text lines must not be interpretable as block
  759. constructs other than paragraphs. So, the line of dashes
  760. in these examples gets interpreted as a horizontal rule:
  761. .
  762. ---
  763. ---
  764. .
  765. <hr />
  766. <hr />
  767. .
  768. .
  769. - foo
  770. -----
  771. .
  772. <ul>
  773. <li>foo</li>
  774. </ul>
  775. <hr />
  776. .
  777. .
  778. foo
  779. ---
  780. .
  781. <pre><code>foo
  782. </code></pre>
  783. <hr />
  784. .
  785. .
  786. > foo
  787. -----
  788. .
  789. <blockquote>
  790. <p>foo</p>
  791. </blockquote>
  792. <hr />
  793. .
  794. If you want a header with `> foo` as its literal text, you can
  795. use backslash escapes:
  796. .
  797. \> foo
  798. ------
  799. .
  800. <h2>&gt; foo</h2>
  801. .
  802. ## Indented code blocks
  803. An [indented code block](@indented-code-block) is composed of one or more
  804. [indented chunk]s separated by blank lines.
  805. An [indented chunk](@indented-chunk) is a sequence of non-blank lines,
  806. each indented four or more spaces. The contents of the code block are
  807. the literal contents of the lines, including trailing
  808. [line ending]s, minus four spaces of indentation.
  809. An indented code block has no [info string].
  810. An indented code block cannot interrupt a paragraph, so there must be
  811. a blank line between a paragraph and a following indented code block.
  812. (A blank line is not needed, however, between a code block and a following
  813. paragraph.)
  814. .
  815. a simple
  816. indented code block
  817. .
  818. <pre><code>a simple
  819. indented code block
  820. </code></pre>
  821. .
  822. The contents are literal text, and do not get parsed as Markdown:
  823. .
  824. <a/>
  825. *hi*
  826. - one
  827. .
  828. <pre><code>&lt;a/&gt;
  829. *hi*
  830. - one
  831. </code></pre>
  832. .
  833. Here we have three chunks separated by blank lines:
  834. .
  835. chunk1
  836. chunk2
  837. chunk3
  838. .
  839. <pre><code>chunk1
  840. chunk2
  841. chunk3
  842. </code></pre>
  843. .
  844. Any initial spaces beyond four will be included in the content, even
  845. in interior blank lines:
  846. .
  847. chunk1
  848. chunk2
  849. .
  850. <pre><code>chunk1
  851. chunk2
  852. </code></pre>
  853. .
  854. An indented code block cannot interrupt a paragraph. (This
  855. allows hanging indents and the like.)
  856. .
  857. Foo
  858. bar
  859. .
  860. <p>Foo
  861. bar</p>
  862. .
  863. However, any non-blank line with fewer than four leading spaces ends
  864. the code block immediately. So a paragraph may occur immediately
  865. after indented code:
  866. .
  867. foo
  868. bar
  869. .
  870. <pre><code>foo
  871. </code></pre>
  872. <p>bar</p>
  873. .
  874. And indented code can occur immediately before and after other kinds of
  875. blocks:
  876. .
  877. # Header
  878. foo
  879. Header
  880. ------
  881. foo
  882. ----
  883. .
  884. <h1>Header</h1>
  885. <pre><code>foo
  886. </code></pre>
  887. <h2>Header</h2>
  888. <pre><code>foo
  889. </code></pre>
  890. <hr />
  891. .
  892. The first line can be indented more than four spaces:
  893. .
  894. foo
  895. bar
  896. .
  897. <pre><code> foo
  898. bar
  899. </code></pre>
  900. .
  901. Blank lines preceding or following an indented code block
  902. are not included in it:
  903. .
  904. foo
  905. .
  906. <pre><code>foo
  907. </code></pre>
  908. .
  909. Trailing spaces are included in the code block's content:
  910. .
  911. foo
  912. .
  913. <pre><code>foo
  914. </code></pre>
  915. .
  916. ## Fenced code blocks
  917. A [code fence](@code-fence) is a sequence
  918. of at least three consecutive backtick characters (`` ` ``) or
  919. tildes (`~`). (Tildes and backticks cannot be mixed.)
  920. A [fenced code block](@fenced-code-block)
  921. begins with a code fence, indented no more than three spaces.
  922. The line with the opening code fence may optionally contain some text
  923. following the code fence; this is trimmed of leading and trailing
  924. spaces and called the [info string](@info-string).
  925. The [info string] may not contain any backtick
  926. characters. (The reason for this restriction is that otherwise
  927. some inline code would be incorrectly interpreted as the
  928. beginning of a fenced code block.)
  929. The content of the code block consists of all subsequent lines, until
  930. a closing [code fence] of the same type as the code block
  931. began with (backticks or tildes), and with at least as many backticks
  932. or tildes as the opening code fence. If the leading code fence is
  933. indented N spaces, then up to N spaces of indentation are removed from
  934. each line of the content (if present). (If a content line is not
  935. indented, it is preserved unchanged. If it is indented less than N
  936. spaces, all of the indentation is removed.)
  937. The closing code fence may be indented up to three spaces, and may be
  938. followed only by spaces, which are ignored. If the end of the
  939. containing block (or document) is reached and no closing code fence
  940. has been found, the code block contains all of the lines after the
  941. opening code fence until the end of the containing block (or
  942. document). (An alternative spec would require backtracking in the
  943. event that a closing code fence is not found. But this makes parsing
  944. much less efficient, and there seems to be no real down side to the
  945. behavior described here.)
  946. A fenced code block may interrupt a paragraph, and does not require
  947. a blank line either before or after.
  948. The content of a code fence is treated as literal text, not parsed
  949. as inlines. The first word of the [info string] is typically used to
  950. specify the language of the code sample, and rendered in the `class`
  951. attribute of the `code` tag. However, this spec does not mandate any
  952. particular treatment of the [info string].
  953. Here is a simple example with backticks:
  954. .
  955. ```
  956. <
  957. >
  958. ```
  959. .
  960. <pre><code>&lt;
  961. &gt;
  962. </code></pre>
  963. .
  964. With tildes:
  965. .
  966. ~~~
  967. <
  968. >
  969. ~~~
  970. .
  971. <pre><code>&lt;
  972. &gt;
  973. </code></pre>
  974. .
  975. The closing code fence must use the same character as the opening
  976. fence:
  977. .
  978. ```
  979. aaa
  980. ~~~
  981. ```
  982. .
  983. <pre><code>aaa
  984. ~~~
  985. </code></pre>
  986. .
  987. .
  988. ~~~
  989. aaa
  990. ```
  991. ~~~
  992. .
  993. <pre><code>aaa
  994. ```
  995. </code></pre>
  996. .
  997. The closing code fence must be at least as long as the opening fence:
  998. .
  999. ````
  1000. aaa
  1001. ```
  1002. ``````
  1003. .
  1004. <pre><code>aaa
  1005. ```
  1006. </code></pre>
  1007. .
  1008. .
  1009. ~~~~
  1010. aaa
  1011. ~~~
  1012. ~~~~
  1013. .
  1014. <pre><code>aaa
  1015. ~~~
  1016. </code></pre>
  1017. .
  1018. Unclosed code blocks are closed by the end of the document:
  1019. .
  1020. ```
  1021. .
  1022. <pre><code></code></pre>
  1023. .
  1024. .
  1025. `````
  1026. ```
  1027. aaa
  1028. .
  1029. <pre><code>
  1030. ```
  1031. aaa
  1032. </code></pre>
  1033. .
  1034. A code block can have all empty lines as its content:
  1035. .
  1036. ```
  1037. ```
  1038. .
  1039. <pre><code>
  1040. </code></pre>
  1041. .
  1042. A code block can be empty:
  1043. .
  1044. ```
  1045. ```
  1046. .
  1047. <pre><code></code></pre>
  1048. .
  1049. Fences can be indented. If the opening fence is indented,
  1050. content lines will have equivalent opening indentation removed,
  1051. if present:
  1052. .
  1053. ```
  1054. aaa
  1055. aaa
  1056. ```
  1057. .
  1058. <pre><code>aaa
  1059. aaa
  1060. </code></pre>
  1061. .
  1062. .
  1063. ```
  1064. aaa
  1065. aaa
  1066. aaa
  1067. ```
  1068. .
  1069. <pre><code>aaa
  1070. aaa
  1071. aaa
  1072. </code></pre>
  1073. .
  1074. .
  1075. ```
  1076. aaa
  1077. aaa
  1078. aaa
  1079. ```
  1080. .
  1081. <pre><code>aaa
  1082. aaa
  1083. aaa
  1084. </code></pre>
  1085. .
  1086. Four spaces indentation produces an indented code block:
  1087. .
  1088. ```
  1089. aaa
  1090. ```
  1091. .
  1092. <pre><code>```
  1093. aaa
  1094. ```
  1095. </code></pre>
  1096. .
  1097. Closing fences may be indented by 0-3 spaces, and their indentation
  1098. need not match that of the opening fence:
  1099. .
  1100. ```
  1101. aaa
  1102. ```
  1103. .
  1104. <pre><code>aaa
  1105. </code></pre>
  1106. .
  1107. .
  1108. ```
  1109. aaa
  1110. ```
  1111. .
  1112. <pre><code>aaa
  1113. </code></pre>
  1114. .
  1115. This is not a closing fence, because it is indented 4 spaces:
  1116. .
  1117. ```
  1118. aaa
  1119. ```
  1120. .
  1121. <pre><code>aaa
  1122. ```
  1123. </code></pre>
  1124. .
  1125. Code fences (opening and closing) cannot contain internal spaces:
  1126. .
  1127. ``` ```
  1128. aaa
  1129. .
  1130. <p><code></code>
  1131. aaa</p>
  1132. .
  1133. .
  1134. ~~~~~~
  1135. aaa
  1136. ~~~ ~~
  1137. .
  1138. <pre><code>aaa
  1139. ~~~ ~~
  1140. </code></pre>
  1141. .
  1142. Fenced code blocks can interrupt paragraphs, and can be followed
  1143. directly by paragraphs, without a blank line between:
  1144. .
  1145. foo
  1146. ```
  1147. bar
  1148. ```
  1149. baz
  1150. .
  1151. <p>foo</p>
  1152. <pre><code>bar
  1153. </code></pre>
  1154. <p>baz</p>
  1155. .
  1156. Other blocks can also occur before and after fenced code blocks
  1157. without an intervening blank line:
  1158. .
  1159. foo
  1160. ---
  1161. ~~~
  1162. bar
  1163. ~~~
  1164. # baz
  1165. .
  1166. <h2>foo</h2>
  1167. <pre><code>bar
  1168. </code></pre>
  1169. <h1>baz</h1>
  1170. .
  1171. An [info string] can be provided after the opening code fence.
  1172. Opening and closing spaces will be stripped, and the first word, prefixed
  1173. with `language-`, is used as the value for the `class` attribute of the
  1174. `code` element within the enclosing `pre` element.
  1175. .
  1176. ```ruby
  1177. def foo(x)
  1178. return 3
  1179. end
  1180. ```
  1181. .
  1182. <pre><code class="language-ruby">def foo(x)
  1183. return 3
  1184. end
  1185. </code></pre>
  1186. .
  1187. .
  1188. ~~~~ ruby startline=3 $%@#$
  1189. def foo(x)
  1190. return 3
  1191. end
  1192. ~~~~~~~
  1193. .
  1194. <pre><code class="language-ruby">def foo(x)
  1195. return 3
  1196. end
  1197. </code></pre>
  1198. .
  1199. .
  1200. ````;
  1201. ````
  1202. .
  1203. <pre><code class="language-;"></code></pre>
  1204. .
  1205. [Info string]s for backtick code blocks cannot contain backticks:
  1206. .
  1207. ``` aa ```
  1208. foo
  1209. .
  1210. <p><code>aa</code>
  1211. foo</p>
  1212. .
  1213. Closing code fences cannot have [info string]s:
  1214. .
  1215. ```
  1216. ``` aaa
  1217. ```
  1218. .
  1219. <pre><code>``` aaa
  1220. </code></pre>
  1221. .
  1222. ## HTML blocks
  1223. An [HTML block tag](@html-block-tag) is
  1224. an [open tag] or [closing tag] whose tag
  1225. name is one of the following (case-insensitive):
  1226. `article`, `header`, `aside`, `hgroup`, `blockquote`, `hr`, `iframe`,
  1227. `body`, `li`, `map`, `button`, `object`, `canvas`, `ol`, `caption`,
  1228. `output`, `col`, `p`, `colgroup`, `pre`, `dd`, `progress`, `div`,
  1229. `section`, `dl`, `table`, `td`, `dt`, `tbody`, `embed`, `textarea`,
  1230. `fieldset`, `tfoot`, `figcaption`, `th`, `figure`, `thead`, `footer`,
  1231. `tr`, `form`, `ul`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `video`,
  1232. `script`, `style`.
  1233. An [HTML block](@html-block) begins with an
  1234. [HTML block tag], [HTML comment], [processing instruction],
  1235. [declaration], or [CDATA section].
  1236. It ends when a [blank line] or the end of the
  1237. input is encountered. The initial line may be indented up to three
  1238. spaces, and subsequent lines may have any indentation. The contents
  1239. of the HTML block are interpreted as raw HTML, and will not be escaped
  1240. in HTML output.
  1241. Some simple examples:
  1242. .
  1243. <table>
  1244. <tr>
  1245. <td>
  1246. hi
  1247. </td>
  1248. </tr>
  1249. </table>
  1250. okay.
  1251. .
  1252. <table>
  1253. <tr>
  1254. <td>
  1255. hi
  1256. </td>
  1257. </tr>
  1258. </table>
  1259. <p>okay.</p>
  1260. .
  1261. .
  1262. <div>
  1263. *hello*
  1264. <foo><a>
  1265. .
  1266. <div>
  1267. *hello*
  1268. <foo><a>
  1269. .
  1270. Here we have two HTML blocks with a Markdown paragraph between them:
  1271. .
  1272. <DIV CLASS="foo">
  1273. *Markdown*
  1274. </DIV>
  1275. .
  1276. <DIV CLASS="foo">
  1277. <p><em>Markdown</em></p>
  1278. </DIV>
  1279. .
  1280. In the following example, what looks like a Markdown code block
  1281. is actually part of the HTML block, which continues until a blank
  1282. line or the end of the document is reached:
  1283. .
  1284. <div></div>
  1285. ``` c
  1286. int x = 33;
  1287. ```
  1288. .
  1289. <div></div>
  1290. ``` c
  1291. int x = 33;
  1292. ```
  1293. .
  1294. A comment:
  1295. .
  1296. <!-- Foo
  1297. bar
  1298. baz -->
  1299. .
  1300. <!-- Foo
  1301. bar
  1302. baz -->
  1303. .
  1304. A processing instruction:
  1305. .
  1306. <?php
  1307. echo '>';
  1308. ?>
  1309. .
  1310. <?php
  1311. echo '>';
  1312. ?>
  1313. .
  1314. CDATA:
  1315. .
  1316. <![CDATA[
  1317. function matchwo(a,b)
  1318. {
  1319. if (a < b && a < 0) then
  1320. {
  1321. return 1;
  1322. }
  1323. else
  1324. {
  1325. return 0;
  1326. }
  1327. }
  1328. ]]>
  1329. .
  1330. <![CDATA[
  1331. function matchwo(a,b)
  1332. {
  1333. if (a < b && a < 0) then
  1334. {
  1335. return 1;
  1336. }
  1337. else
  1338. {
  1339. return 0;
  1340. }
  1341. }
  1342. ]]>
  1343. .
  1344. The opening tag can be indented 1-3 spaces, but not 4:
  1345. .
  1346. <!-- foo -->
  1347. <!-- foo -->
  1348. .
  1349. <!-- foo -->
  1350. <pre><code>&lt;!-- foo --&gt;
  1351. </code></pre>
  1352. .
  1353. An HTML block can interrupt a paragraph, and need not be preceded
  1354. by a blank line.
  1355. .
  1356. Foo
  1357. <div>
  1358. bar
  1359. </div>
  1360. .
  1361. <p>Foo</p>
  1362. <div>
  1363. bar
  1364. </div>
  1365. .
  1366. However, a following blank line is always needed, except at the end of
  1367. a document:
  1368. .
  1369. <div>
  1370. bar
  1371. </div>
  1372. *foo*
  1373. .
  1374. <div>
  1375. bar
  1376. </div>
  1377. *foo*
  1378. .
  1379. An incomplete HTML block tag may also start an HTML block:
  1380. .
  1381. <div class
  1382. foo
  1383. .
  1384. <div class
  1385. foo
  1386. .
  1387. This rule differs from John Gruber's original Markdown syntax
  1388. specification, which says:
  1389. > The only restrictions are that block-level HTML elements —
  1390. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  1391. > surrounding content by blank lines, and the start and end tags of the
  1392. > block should not be indented with tabs or spaces.
  1393. In some ways Gruber's rule is more restrictive than the one given
  1394. here:
  1395. - It requires that an HTML block be preceded by a blank line.
  1396. - It does not allow the start tag to be indented.
  1397. - It requires a matching end tag, which it also does not allow to
  1398. be indented.
  1399. Indeed, most Markdown implementations, including some of Gruber's
  1400. own perl implementations, do not impose these restrictions.
  1401. There is one respect, however, in which Gruber's rule is more liberal
  1402. than the one given here, since it allows blank lines to occur inside
  1403. an HTML block. There are two reasons for disallowing them here.
  1404. First, it removes the need to parse balanced tags, which is
  1405. expensive and can require backtracking from the end of the document
  1406. if no matching end tag is found. Second, it provides a very simple
  1407. and flexible way of including Markdown content inside HTML tags:
  1408. simply separate the Markdown from the HTML using blank lines:
  1409. .
  1410. <div>
  1411. *Emphasized* text.
  1412. </div>
  1413. .
  1414. <div>
  1415. <p><em>Emphasized</em> text.</p>
  1416. </div>
  1417. .
  1418. Compare:
  1419. .
  1420. <div>
  1421. *Emphasized* text.
  1422. </div>
  1423. .
  1424. <div>
  1425. *Emphasized* text.
  1426. </div>
  1427. .
  1428. Some Markdown implementations have adopted a convention of
  1429. interpreting content inside tags as text if the open tag has
  1430. the attribute `markdown=1`. The rule given above seems a simpler and
  1431. more elegant way of achieving the same expressive power, which is also
  1432. much simpler to parse.
  1433. The main potential drawback is that one can no longer paste HTML
  1434. blocks into Markdown documents with 100% reliability. However,
  1435. *in most cases* this will work fine, because the blank lines in
  1436. HTML are usually followed by HTML block tags. For example:
  1437. .
  1438. <table>
  1439. <tr>
  1440. <td>
  1441. Hi
  1442. </td>
  1443. </tr>
  1444. </table>
  1445. .
  1446. <table>
  1447. <tr>
  1448. <td>
  1449. Hi
  1450. </td>
  1451. </tr>
  1452. </table>
  1453. .
  1454. Moreover, blank lines are usually not necessary and can be
  1455. deleted. The exception is inside `<pre>` tags; here, one can
  1456. replace the blank lines with `&#10;` entities.
  1457. So there is no important loss of expressive power with the new rule.
  1458. ## Link reference definitions
  1459. A [link reference definition](@link-reference-definition)
  1460. consists of a [link label], indented up to three spaces, followed
  1461. by a colon (`:`), optional [whitespace] (including up to one
  1462. [line ending]), a [link destination],
  1463. optional [whitespace] (including up to one
  1464. [line ending]), and an optional [link
  1465. title], which if it is present must be separated
  1466. from the [link destination] by [whitespace].
  1467. No further [non-space character]s may occur on the line.
  1468. A [link reference-definition]
  1469. does not correspond to a structural element of a document. Instead, it
  1470. defines a label which can be used in [reference link]s
  1471. and reference-style [images] elsewhere in the document. [Link
  1472. reference definitions] can come either before or after the links that use
  1473. them.
  1474. .
  1475. [foo]: /url "title"
  1476. [foo]
  1477. .
  1478. <p><a href="/url" title="title">foo</a></p>
  1479. .
  1480. .
  1481. [foo]:
  1482. /url
  1483. 'the title'
  1484. [foo]
  1485. .
  1486. <p><a href="/url" title="the title">foo</a></p>
  1487. .
  1488. .
  1489. [Foo*bar\]]:my_(url) 'title (with parens)'
  1490. [Foo*bar\]]
  1491. .
  1492. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  1493. .
  1494. .
  1495. [Foo bar]:
  1496. <my url>
  1497. 'title'
  1498. [Foo bar]
  1499. .
  1500. <p><a href="my%20url" title="title">Foo bar</a></p>
  1501. .
  1502. The title may extend over multiple lines:
  1503. .
  1504. [foo]: /url '
  1505. title
  1506. line1
  1507. line2
  1508. '
  1509. [foo]
  1510. .
  1511. <p><a href="/url" title="
  1512. title
  1513. line1
  1514. line2
  1515. ">foo</a></p>
  1516. .
  1517. However, it may not contain a [blank line]:
  1518. .
  1519. [foo]: /url 'title
  1520. with blank line'
  1521. [foo]
  1522. .
  1523. <p>[foo]: /url 'title</p>
  1524. <p>with blank line'</p>
  1525. <p>[foo]</p>
  1526. .
  1527. The title may be omitted:
  1528. .
  1529. [foo]:
  1530. /url
  1531. [foo]
  1532. .
  1533. <p><a href="/url">foo</a></p>
  1534. .
  1535. The link destination may not be omitted:
  1536. .
  1537. [foo]:
  1538. [foo]
  1539. .
  1540. <p>[foo]:</p>
  1541. <p>[foo]</p>
  1542. .
  1543. A link can come before its corresponding definition:
  1544. .
  1545. [foo]
  1546. [foo]: url
  1547. .
  1548. <p><a href="url">foo</a></p>
  1549. .
  1550. If there are several matching definitions, the first one takes
  1551. precedence:
  1552. .
  1553. [foo]
  1554. [foo]: first
  1555. [foo]: second
  1556. .
  1557. <p><a href="first">foo</a></p>
  1558. .
  1559. As noted in the section on [Links], matching of labels is
  1560. case-insensitive (see [matches]).
  1561. .
  1562. [FOO]: /url
  1563. [Foo]
  1564. .
  1565. <p><a href="/url">Foo</a></p>
  1566. .
  1567. .
  1568. [ΑΓΩ]: /φου
  1569. [αγω]
  1570. .
  1571. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  1572. .
  1573. Here is a link reference definition with no corresponding link.
  1574. It contributes nothing to the document.
  1575. .
  1576. [foo]: /url
  1577. .
  1578. .
  1579. Here is another one:
  1580. .
  1581. [
  1582. foo
  1583. ]: /url
  1584. bar
  1585. .
  1586. <p>bar</p>
  1587. .
  1588. This is not a link reference definition, because there are
  1589. [non-space character]s after the title:
  1590. .
  1591. [foo]: /url "title" ok
  1592. .
  1593. <p>[foo]: /url &quot;title&quot; ok</p>
  1594. .
  1595. This is not a link reference definition, because it is indented
  1596. four spaces:
  1597. .
  1598. [foo]: /url "title"
  1599. [foo]
  1600. .
  1601. <pre><code>[foo]: /url &quot;title&quot;
  1602. </code></pre>
  1603. <p>[foo]</p>
  1604. .
  1605. This is not a link reference definition, because it occurs inside
  1606. a code block:
  1607. .
  1608. ```
  1609. [foo]: /url
  1610. ```
  1611. [foo]
  1612. .
  1613. <pre><code>[foo]: /url
  1614. </code></pre>
  1615. <p>[foo]</p>
  1616. .
  1617. A [link reference definition] cannot interrupt a paragraph.
  1618. .
  1619. Foo
  1620. [bar]: /baz
  1621. [bar]
  1622. .
  1623. <p>Foo
  1624. [bar]: /baz</p>
  1625. <p>[bar]</p>
  1626. .
  1627. However, it can directly follow other block elements, such as headers
  1628. and horizontal rules, and it need not be followed by a blank line.
  1629. .
  1630. # [Foo]
  1631. [foo]: /url
  1632. > bar
  1633. .
  1634. <h1><a href="/url">Foo</a></h1>
  1635. <blockquote>
  1636. <p>bar</p>
  1637. </blockquote>
  1638. .
  1639. Several [link reference definition]s
  1640. can occur one after another, without intervening blank lines.
  1641. .
  1642. [foo]: /foo-url "foo"
  1643. [bar]: /bar-url
  1644. "bar"
  1645. [baz]: /baz-url
  1646. [foo],
  1647. [bar],
  1648. [baz]
  1649. .
  1650. <p><a href="/foo-url" title="foo">foo</a>,
  1651. <a href="/bar-url" title="bar">bar</a>,
  1652. <a href="/baz-url">baz</a></p>
  1653. .
  1654. [Link reference definition]s can occur
  1655. inside block containers, like lists and block quotations. They
  1656. affect the entire document, not just the container in which they
  1657. are defined:
  1658. .
  1659. [foo]
  1660. > [foo]: /url
  1661. .
  1662. <p><a href="/url">foo</a></p>
  1663. <blockquote>
  1664. </blockquote>
  1665. .
  1666. ## Paragraphs
  1667. A sequence of non-blank lines that cannot be interpreted as other
  1668. kinds of blocks forms a [paragraph](@paragraph).
  1669. The contents of the paragraph are the result of parsing the
  1670. paragraph's raw content as inlines. The paragraph's raw content
  1671. is formed by concatenating the lines and removing initial and final
  1672. [whitespace].
  1673. A simple example with two paragraphs:
  1674. .
  1675. aaa
  1676. bbb
  1677. .
  1678. <p>aaa</p>
  1679. <p>bbb</p>
  1680. .
  1681. Paragraphs can contain multiple lines, but no blank lines:
  1682. .
  1683. aaa
  1684. bbb
  1685. ccc
  1686. ddd
  1687. .
  1688. <p>aaa
  1689. bbb</p>
  1690. <p>ccc
  1691. ddd</p>
  1692. .
  1693. Multiple blank lines between paragraph have no effect:
  1694. .
  1695. aaa
  1696. bbb
  1697. .
  1698. <p>aaa</p>
  1699. <p>bbb</p>
  1700. .
  1701. Leading spaces are skipped:
  1702. .
  1703. aaa
  1704. bbb
  1705. .
  1706. <p>aaa
  1707. bbb</p>
  1708. .
  1709. Lines after the first may be indented any amount, since indented
  1710. code blocks cannot interrupt paragraphs.
  1711. .
  1712. aaa
  1713. bbb
  1714. ccc
  1715. .
  1716. <p>aaa
  1717. bbb
  1718. ccc</p>
  1719. .
  1720. However, the first line may be indented at most three spaces,
  1721. or an indented code block will be triggered:
  1722. .
  1723. aaa
  1724. bbb
  1725. .
  1726. <p>aaa
  1727. bbb</p>
  1728. .
  1729. .
  1730. aaa
  1731. bbb
  1732. .
  1733. <pre><code>aaa
  1734. </code></pre>
  1735. <p>bbb</p>
  1736. .
  1737. Final spaces are stripped before inline parsing, so a paragraph
  1738. that ends with two or more spaces will not end with a [hard line
  1739. break]:
  1740. .
  1741. aaa
  1742. bbb
  1743. .
  1744. <p>aaa<br />
  1745. bbb</p>
  1746. .
  1747. ## Blank lines
  1748. [Blank line]s between block-level elements are ignored,
  1749. except for the role they play in determining whether a [list]
  1750. is [tight] or [loose].
  1751. Blank lines at the beginning and end of the document are also ignored.
  1752. .
  1753. aaa
  1754. # aaa
  1755. .
  1756. <p>aaa</p>
  1757. <h1>aaa</h1>
  1758. .
  1759. # Container blocks
  1760. A [container block] is a block that has other
  1761. blocks as its contents. There are two basic kinds of container blocks:
  1762. [block quotes] and [list items].
  1763. [Lists] are meta-containers for [list items].
  1764. We define the syntax for container blocks recursively. The general
  1765. form of the definition is:
  1766. > If X is a sequence of blocks, then the result of
  1767. > transforming X in such-and-such a way is a container of type Y
  1768. > with these blocks as its content.
  1769. So, we explain what counts as a block quote or list item by explaining
  1770. how these can be *generated* from their contents. This should suffice
  1771. to define the syntax, although it does not give a recipe for *parsing*
  1772. these constructions. (A recipe is provided below in the section entitled
  1773. [A parsing strategy](#appendix-a-a-parsing-strategy).)
  1774. ## Block quotes
  1775. A [block quote marker](@block-quote-marker)
  1776. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  1777. with a following space, or (b) a single character `>` not followed by a space.
  1778. The following rules define [block quotes]:
  1779. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  1780. of blocks *Bs*, then the result of prepending a [block quote
  1781. marker] to the beginning of each line in *Ls*
  1782. is a [block quote](#block-quotes) containing *Bs*.
  1783. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  1784. quote](#block-quotes) with contents *Bs*, then the result of deleting
  1785. the initial [block quote marker] from one or
  1786. more lines in which the next [non-space character] after the [block
  1787. quote marker] is [paragraph continuation
  1788. text] is a block quote with *Bs* as its content.
  1789. [Paragraph continuation text](@paragraph-continuation-text) is text
  1790. that will be parsed as part of the content of a paragraph, but does
  1791. not occur at the beginning of the paragraph.
  1792. 3. **Consecutiveness.** A document cannot contain two [block
  1793. quotes] in a row unless there is a [blank line] between them.
  1794. Nothing else counts as a [block quote](#block-quotes).
  1795. Here is a simple example:
  1796. .
  1797. > # Foo
  1798. > bar
  1799. > baz
  1800. .
  1801. <blockquote>
  1802. <h1>Foo</h1>
  1803. <p>bar
  1804. baz</p>
  1805. </blockquote>
  1806. .
  1807. The spaces after the `>` characters can be omitted:
  1808. .
  1809. ># Foo
  1810. >bar
  1811. > baz
  1812. .
  1813. <blockquote>
  1814. <h1>Foo</h1>
  1815. <p>bar
  1816. baz</p>
  1817. </blockquote>
  1818. .
  1819. The `>` characters can be indented 1-3 spaces:
  1820. .
  1821. > # Foo
  1822. > bar
  1823. > baz
  1824. .
  1825. <blockquote>
  1826. <h1>Foo</h1>
  1827. <p>bar
  1828. baz</p>
  1829. </blockquote>
  1830. .
  1831. Four spaces gives us a code block:
  1832. .
  1833. > # Foo
  1834. > bar
  1835. > baz
  1836. .
  1837. <pre><code>&gt; # Foo
  1838. &gt; bar
  1839. &gt; baz
  1840. </code></pre>
  1841. .
  1842. The Laziness clause allows us to omit the `>` before a
  1843. paragraph continuation line:
  1844. .
  1845. > # Foo
  1846. > bar
  1847. baz
  1848. .
  1849. <blockquote>
  1850. <h1>Foo</h1>
  1851. <p>bar
  1852. baz</p>
  1853. </blockquote>
  1854. .
  1855. A block quote can contain some lazy and some non-lazy
  1856. continuation lines:
  1857. .
  1858. > bar
  1859. baz
  1860. > foo
  1861. .
  1862. <blockquote>
  1863. <p>bar
  1864. baz
  1865. foo</p>
  1866. </blockquote>
  1867. .
  1868. Laziness only applies to lines that are continuations of
  1869. paragraphs. Lines containing characters or indentation that indicate
  1870. block structure cannot be lazy.
  1871. .
  1872. > foo
  1873. ---
  1874. .
  1875. <blockquote>
  1876. <p>foo</p>
  1877. </blockquote>
  1878. <hr />
  1879. .
  1880. .
  1881. > - foo
  1882. - bar
  1883. .
  1884. <blockquote>
  1885. <ul>
  1886. <li>foo</li>
  1887. </ul>
  1888. </blockquote>
  1889. <ul>
  1890. <li>bar</li>
  1891. </ul>
  1892. .
  1893. .
  1894. > foo
  1895. bar
  1896. .
  1897. <blockquote>
  1898. <pre><code>foo
  1899. </code></pre>
  1900. </blockquote>
  1901. <pre><code>bar
  1902. </code></pre>
  1903. .
  1904. .
  1905. > ```
  1906. foo
  1907. ```
  1908. .
  1909. <blockquote>
  1910. <pre><code></code></pre>
  1911. </blockquote>
  1912. <p>foo</p>
  1913. <pre><code></code></pre>
  1914. .
  1915. A block quote can be empty:
  1916. .
  1917. >
  1918. .
  1919. <blockquote>
  1920. </blockquote>
  1921. .
  1922. .
  1923. >
  1924. >
  1925. >
  1926. .
  1927. <blockquote>
  1928. </blockquote>
  1929. .
  1930. A block quote can have initial or final blank lines:
  1931. .
  1932. >
  1933. > foo
  1934. >
  1935. .
  1936. <blockquote>
  1937. <p>foo</p>
  1938. </blockquote>
  1939. .
  1940. A blank line always separates block quotes:
  1941. .
  1942. > foo
  1943. > bar
  1944. .
  1945. <blockquote>
  1946. <p>foo</p>
  1947. </blockquote>
  1948. <blockquote>
  1949. <p>bar</p>
  1950. </blockquote>
  1951. .
  1952. (Most current Markdown implementations, including John Gruber's
  1953. original `Markdown.pl`, will parse this example as a single block quote
  1954. with two paragraphs. But it seems better to allow the author to decide
  1955. whether two block quotes or one are wanted.)
  1956. Consecutiveness means that if we put these block quotes together,
  1957. we get a single block quote:
  1958. .
  1959. > foo
  1960. > bar
  1961. .
  1962. <blockquote>
  1963. <p>foo
  1964. bar</p>
  1965. </blockquote>
  1966. .
  1967. To get a block quote with two paragraphs, use:
  1968. .
  1969. > foo
  1970. >
  1971. > bar
  1972. .
  1973. <blockquote>
  1974. <p>foo</p>
  1975. <p>bar</p>
  1976. </blockquote>
  1977. .
  1978. Block quotes can interrupt paragraphs:
  1979. .
  1980. foo
  1981. > bar
  1982. .
  1983. <p>foo</p>
  1984. <blockquote>
  1985. <p>bar</p>
  1986. </blockquote>
  1987. .
  1988. In general, blank lines are not needed before or after block
  1989. quotes:
  1990. .
  1991. > aaa
  1992. ***
  1993. > bbb
  1994. .
  1995. <blockquote>
  1996. <p>aaa</p>
  1997. </blockquote>
  1998. <hr />
  1999. <blockquote>
  2000. <p>bbb</p>
  2001. </blockquote>
  2002. .
  2003. However, because of laziness, a blank line is needed between
  2004. a block quote and a following paragraph:
  2005. .
  2006. > bar
  2007. baz
  2008. .
  2009. <blockquote>
  2010. <p>bar
  2011. baz</p>
  2012. </blockquote>
  2013. .
  2014. .
  2015. > bar
  2016. baz
  2017. .
  2018. <blockquote>
  2019. <p>bar</p>
  2020. </blockquote>
  2021. <p>baz</p>
  2022. .
  2023. .
  2024. > bar
  2025. >
  2026. baz
  2027. .
  2028. <blockquote>
  2029. <p>bar</p>
  2030. </blockquote>
  2031. <p>baz</p>
  2032. .
  2033. It is a consequence of the Laziness rule that any number
  2034. of initial `>`s may be omitted on a continuation line of a
  2035. nested block quote:
  2036. .
  2037. > > > foo
  2038. bar
  2039. .
  2040. <blockquote>
  2041. <blockquote>
  2042. <blockquote>
  2043. <p>foo
  2044. bar</p>
  2045. </blockquote>
  2046. </blockquote>
  2047. </blockquote>
  2048. .
  2049. .
  2050. >>> foo
  2051. > bar
  2052. >>baz
  2053. .
  2054. <blockquote>
  2055. <blockquote>
  2056. <blockquote>
  2057. <p>foo
  2058. bar
  2059. baz</p>
  2060. </blockquote>
  2061. </blockquote>
  2062. </blockquote>
  2063. .
  2064. When including an indented code block in a block quote,
  2065. remember that the [block quote marker] includes
  2066. both the `>` and a following space. So *five spaces* are needed after
  2067. the `>`:
  2068. .
  2069. > code
  2070. > not code
  2071. .
  2072. <blockquote>
  2073. <pre><code>code
  2074. </code></pre>
  2075. </blockquote>
  2076. <blockquote>
  2077. <p>not code</p>
  2078. </blockquote>
  2079. .
  2080. ## List items
  2081. A [list marker](@list-marker) is a
  2082. [bullet list marker] or an [ordered list marker].
  2083. A [bullet list marker](@bullet-list-marker)
  2084. is a `-`, `+`, or `*` character.
  2085. An [ordered list marker](@ordered-list-marker)
  2086. is a sequence of one of more digits (`0-9`), followed by either a
  2087. `.` character or a `)` character.
  2088. The following rules define [list items]:
  2089. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2090. blocks *Bs* starting with a [non-space character] and not separated
  2091. from each other by more than one blank line, and *M* is a list
  2092. marker *M* of width *W* followed by 0 < *N* < 5 spaces, then the result
  2093. of prepending *M* and the following spaces to the first line of
  2094. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2095. list item with *Bs* as its contents. The type of the list item
  2096. (bullet or ordered) is determined by the type of its list marker.
  2097. If the list item is ordered, then it is also assigned a start
  2098. number, based on the ordered list marker.
  2099. For example, let *Ls* be the lines
  2100. .
  2101. A paragraph
  2102. with two lines.
  2103. indented code
  2104. > A block quote.
  2105. .
  2106. <p>A paragraph
  2107. with two lines.</p>
  2108. <pre><code>indented code
  2109. </code></pre>
  2110. <blockquote>
  2111. <p>A block quote.</p>
  2112. </blockquote>
  2113. .
  2114. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2115. that the following is an ordered list item with start number 1,
  2116. and the same contents as *Ls*:
  2117. .
  2118. 1. A paragraph
  2119. with two lines.
  2120. indented code
  2121. > A block quote.
  2122. .
  2123. <ol>
  2124. <li>
  2125. <p>A paragraph
  2126. with two lines.</p>
  2127. <pre><code>indented code
  2128. </code></pre>
  2129. <blockquote>
  2130. <p>A block quote.</p>
  2131. </blockquote>
  2132. </li>
  2133. </ol>
  2134. .
  2135. The most important thing to notice is that the position of
  2136. the text after the list marker determines how much indentation
  2137. is needed in subsequent blocks in the list item. If the list
  2138. marker takes up two spaces, and there are three spaces between
  2139. the list marker and the next [non-space character], then blocks
  2140. must be indented five spaces in order to fall under the list
  2141. item.
  2142. Here are some examples showing how far content must be indented to be
  2143. put under the list item:
  2144. .
  2145. - one
  2146. two
  2147. .
  2148. <ul>
  2149. <li>one</li>
  2150. </ul>
  2151. <p>two</p>
  2152. .
  2153. .
  2154. - one
  2155. two
  2156. .
  2157. <ul>
  2158. <li>
  2159. <p>one</p>
  2160. <p>two</p>
  2161. </li>
  2162. </ul>
  2163. .
  2164. .
  2165. - one
  2166. two
  2167. .
  2168. <ul>
  2169. <li>one</li>
  2170. </ul>
  2171. <pre><code> two
  2172. </code></pre>
  2173. .
  2174. .
  2175. - one
  2176. two
  2177. .
  2178. <ul>
  2179. <li>
  2180. <p>one</p>
  2181. <p>two</p>
  2182. </li>
  2183. </ul>
  2184. .
  2185. It is tempting to think of this in terms of columns: the continuation
  2186. blocks must be indented at least to the column of the first
  2187. [non-space character] after the list marker. However, that is not quite right.
  2188. The spaces after the list marker determine how much relative indentation
  2189. is needed. Which column this indentation reaches will depend on
  2190. how the list item is embedded in other constructions, as shown by
  2191. this example:
  2192. .
  2193. > > 1. one
  2194. >>
  2195. >> two
  2196. .
  2197. <blockquote>
  2198. <blockquote>
  2199. <ol>
  2200. <li>
  2201. <p>one</p>
  2202. <p>two</p>
  2203. </li>
  2204. </ol>
  2205. </blockquote>
  2206. </blockquote>
  2207. .
  2208. Here `two` occurs in the same column as the list marker `1.`,
  2209. but is actually contained in the list item, because there is
  2210. sufficent indentation after the last containing blockquote marker.
  2211. The converse is also possible. In the following example, the word `two`
  2212. occurs far to the right of the initial text of the list item, `one`, but
  2213. it is not considered part of the list item, because it is not indented
  2214. far enough past the blockquote marker:
  2215. .
  2216. >>- one
  2217. >>
  2218. > > two
  2219. .
  2220. <blockquote>
  2221. <blockquote>
  2222. <ul>
  2223. <li>one</li>
  2224. </ul>
  2225. <p>two</p>
  2226. </blockquote>
  2227. </blockquote>
  2228. .
  2229. Note that at least one space is needed between the list marker and
  2230. any following content, so these are not list items:
  2231. .
  2232. -one
  2233. 2.two
  2234. .
  2235. <p>-one</p>
  2236. <p>2.two</p>
  2237. .
  2238. A list item may not contain blocks that are separated by more than
  2239. one blank line. Thus, two blank lines will end a list, unless the
  2240. two blanks are contained in a [fenced code block].
  2241. .
  2242. - foo
  2243. bar
  2244. - foo
  2245. bar
  2246. - ```
  2247. foo
  2248. bar
  2249. ```
  2250. - baz
  2251. + ```
  2252. foo
  2253. bar
  2254. ```
  2255. .
  2256. <ul>
  2257. <li>
  2258. <p>foo</p>
  2259. <p>bar</p>
  2260. </li>
  2261. <li>
  2262. <p>foo</p>
  2263. </li>
  2264. </ul>
  2265. <p>bar</p>
  2266. <ul>
  2267. <li>
  2268. <pre><code>foo
  2269. bar
  2270. </code></pre>
  2271. </li>
  2272. <li>
  2273. <p>baz</p>
  2274. <ul>
  2275. <li>
  2276. <pre><code>foo
  2277. bar
  2278. </code></pre>
  2279. </li>
  2280. </ul>
  2281. </li>
  2282. </ul>
  2283. .
  2284. A list item may contain any kind of block:
  2285. .
  2286. 1. foo
  2287. ```
  2288. bar
  2289. ```
  2290. baz
  2291. > bam
  2292. .
  2293. <ol>
  2294. <li>
  2295. <p>foo</p>
  2296. <pre><code>bar
  2297. </code></pre>
  2298. <p>baz</p>
  2299. <blockquote>
  2300. <p>bam</p>
  2301. </blockquote>
  2302. </li>
  2303. </ol>
  2304. .
  2305. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  2306. constitute a sequence of blocks *Bs* starting with an indented code
  2307. block and not separated from each other by more than one blank line,
  2308. and *M* is a list marker *M* of width *W* followed by
  2309. one space, then the result of prepending *M* and the following
  2310. space to the first line of *Ls*, and indenting subsequent lines of
  2311. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  2312. If a line is empty, then it need not be indented. The type of the
  2313. list item (bullet or ordered) is determined by the type of its list
  2314. marker. If the list item is ordered, then it is also assigned a
  2315. start number, based on the ordered list marker.
  2316. An indented code block will have to be indented four spaces beyond
  2317. the edge of the region where text will be included in the list item.
  2318. In the following case that is 6 spaces:
  2319. .
  2320. - foo
  2321. bar
  2322. .
  2323. <ul>
  2324. <li>
  2325. <p>foo</p>
  2326. <pre><code>bar
  2327. </code></pre>
  2328. </li>
  2329. </ul>
  2330. .
  2331. And in this case it is 11 spaces:
  2332. .
  2333. 10. foo
  2334. bar
  2335. .
  2336. <ol start="10">
  2337. <li>
  2338. <p>foo</p>
  2339. <pre><code>bar
  2340. </code></pre>
  2341. </li>
  2342. </ol>
  2343. .
  2344. If the *first* block in the list item is an indented code block,
  2345. then by rule #2, the contents must be indented *one* space after the
  2346. list marker:
  2347. .
  2348. indented code
  2349. paragraph
  2350. more code
  2351. .
  2352. <pre><code>indented code
  2353. </code></pre>
  2354. <p>paragraph</p>
  2355. <pre><code>more code
  2356. </code></pre>
  2357. .
  2358. .
  2359. 1. indented code
  2360. paragraph
  2361. more code
  2362. .
  2363. <ol>
  2364. <li>
  2365. <pre><code>indented code
  2366. </code></pre>
  2367. <p>paragraph</p>
  2368. <pre><code>more code
  2369. </code></pre>
  2370. </li>
  2371. </ol>
  2372. .
  2373. Note that an additional space indent is interpreted as space
  2374. inside the code block:
  2375. .
  2376. 1. indented code
  2377. paragraph
  2378. more code
  2379. .
  2380. <ol>
  2381. <li>
  2382. <pre><code> indented code
  2383. </code></pre>
  2384. <p>paragraph</p>
  2385. <pre><code>more code
  2386. </code></pre>
  2387. </li>
  2388. </ol>
  2389. .
  2390. Note that rules #1 and #2 only apply to two cases: (a) cases
  2391. in which the lines to be included in a list item begin with a
  2392. [non-space character], and (b) cases in which
  2393. they begin with an indented code
  2394. block. In a case like the following, where the first block begins with
  2395. a three-space indent, the rules do not allow us to form a list item by
  2396. indenting the whole thing and prepending a list marker:
  2397. .
  2398. foo
  2399. bar
  2400. .
  2401. <p>foo</p>
  2402. <p>bar</p>
  2403. .
  2404. .
  2405. - foo
  2406. bar
  2407. .
  2408. <ul>
  2409. <li>foo</li>
  2410. </ul>
  2411. <p>bar</p>
  2412. .
  2413. This is not a significant restriction, because when a block begins
  2414. with 1-3 spaces indent, the indentation can always be removed without
  2415. a change in interpretation, allowing rule #1 to be applied. So, in
  2416. the above case:
  2417. .
  2418. - foo
  2419. bar
  2420. .
  2421. <ul>
  2422. <li>
  2423. <p>foo</p>
  2424. <p>bar</p>
  2425. </li>
  2426. </ul>
  2427. .
  2428. 3. **Empty list item.** A [list marker] followed by a
  2429. line containing only [whitespace] is a list item with no contents.
  2430. Here is an empty bullet list item:
  2431. .
  2432. - foo
  2433. -
  2434. - bar
  2435. .
  2436. <ul>
  2437. <li>foo</li>
  2438. <li></li>
  2439. <li>bar</li>
  2440. </ul>
  2441. .
  2442. It does not matter whether there are spaces following the [list marker]:
  2443. .
  2444. - foo
  2445. -
  2446. - bar
  2447. .
  2448. <ul>
  2449. <li>foo</li>
  2450. <li></li>
  2451. <li>bar</li>
  2452. </ul>
  2453. .
  2454. Here is an empty ordered list item:
  2455. .
  2456. 1. foo
  2457. 2.
  2458. 3. bar
  2459. .
  2460. <ol>
  2461. <li>foo</li>
  2462. <li></li>
  2463. <li>bar</li>
  2464. </ol>
  2465. .
  2466. A list may start or end with an empty list item:
  2467. .
  2468. *
  2469. .
  2470. <ul>
  2471. <li></li>
  2472. </ul>
  2473. .
  2474. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  2475. according to rule #1, #2, or #3, then the result of indenting each line
  2476. of *L* by 1-3 spaces (the same for each line) also constitutes a
  2477. list item with the same contents and attributes. If a line is
  2478. empty, then it need not be indented.
  2479. Indented one space:
  2480. .
  2481. 1. A paragraph
  2482. with two lines.
  2483. indented code
  2484. > A block quote.
  2485. .
  2486. <ol>
  2487. <li>
  2488. <p>A paragraph
  2489. with two lines.</p>
  2490. <pre><code>indented code
  2491. </code></pre>
  2492. <blockquote>
  2493. <p>A block quote.</p>
  2494. </blockquote>
  2495. </li>
  2496. </ol>
  2497. .
  2498. Indented two spaces:
  2499. .
  2500. 1. A paragraph
  2501. with two lines.
  2502. indented code
  2503. > A block quote.
  2504. .
  2505. <ol>
  2506. <li>
  2507. <p>A paragraph
  2508. with two lines.</p>
  2509. <pre><code>indented code
  2510. </code></pre>
  2511. <blockquote>
  2512. <p>A block quote.</p>
  2513. </blockquote>
  2514. </li>
  2515. </ol>
  2516. .
  2517. Indented three spaces:
  2518. .
  2519. 1. A paragraph
  2520. with two lines.
  2521. indented code
  2522. > A block quote.
  2523. .
  2524. <ol>
  2525. <li>
  2526. <p>A paragraph
  2527. with two lines.</p>
  2528. <pre><code>indented code
  2529. </code></pre>
  2530. <blockquote>
  2531. <p>A block quote.</p>
  2532. </blockquote>
  2533. </li>
  2534. </ol>
  2535. .
  2536. Four spaces indent gives a code block:
  2537. .
  2538. 1. A paragraph
  2539. with two lines.
  2540. indented code
  2541. > A block quote.
  2542. .
  2543. <pre><code>1. A paragraph
  2544. with two lines.
  2545. indented code
  2546. &gt; A block quote.
  2547. </code></pre>
  2548. .
  2549. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  2550. item](#list-items) with contents *Bs*, then the result of deleting
  2551. some or all of the indentation from one or more lines in which the
  2552. next [non-space character] after the indentation is
  2553. [paragraph continuation text] is a
  2554. list item with the same contents and attributes. The unindented
  2555. lines are called
  2556. [lazy continuation line](@lazy-continuation-line)s.
  2557. Here is an example with [lazy continuation line]s:
  2558. .
  2559. 1. A paragraph
  2560. with two lines.
  2561. indented code
  2562. > A block quote.
  2563. .
  2564. <ol>
  2565. <li>
  2566. <p>A paragraph
  2567. with two lines.</p>
  2568. <pre><code>indented code
  2569. </code></pre>
  2570. <blockquote>
  2571. <p>A block quote.</p>
  2572. </blockquote>
  2573. </li>
  2574. </ol>
  2575. .
  2576. Indentation can be partially deleted:
  2577. .
  2578. 1. A paragraph
  2579. with two lines.
  2580. .
  2581. <ol>
  2582. <li>A paragraph
  2583. with two lines.</li>
  2584. </ol>
  2585. .
  2586. These examples show how laziness can work in nested structures:
  2587. .
  2588. > 1. > Blockquote
  2589. continued here.
  2590. .
  2591. <blockquote>
  2592. <ol>
  2593. <li>
  2594. <blockquote>
  2595. <p>Blockquote
  2596. continued here.</p>
  2597. </blockquote>
  2598. </li>
  2599. </ol>
  2600. </blockquote>
  2601. .
  2602. .
  2603. > 1. > Blockquote
  2604. > continued here.
  2605. .
  2606. <blockquote>
  2607. <ol>
  2608. <li>
  2609. <blockquote>
  2610. <p>Blockquote
  2611. continued here.</p>
  2612. </blockquote>
  2613. </li>
  2614. </ol>
  2615. </blockquote>
  2616. .
  2617. 6. **That's all.** Nothing that is not counted as a list item by rules
  2618. #1--5 counts as a [list item](#list-items).
  2619. The rules for sublists follow from the general rules above. A sublist
  2620. must be indented the same number of spaces a paragraph would need to be
  2621. in order to be included in the list item.
  2622. So, in this case we need two spaces indent:
  2623. .
  2624. - foo
  2625. - bar
  2626. - baz
  2627. .
  2628. <ul>
  2629. <li>foo
  2630. <ul>
  2631. <li>bar
  2632. <ul>
  2633. <li>baz</li>
  2634. </ul>
  2635. </li>
  2636. </ul>
  2637. </li>
  2638. </ul>
  2639. .
  2640. One is not enough:
  2641. .
  2642. - foo
  2643. - bar
  2644. - baz
  2645. .
  2646. <ul>
  2647. <li>foo</li>
  2648. <li>bar</li>
  2649. <li>baz</li>
  2650. </ul>
  2651. .
  2652. Here we need four, because the list marker is wider:
  2653. .
  2654. 10) foo
  2655. - bar
  2656. .
  2657. <ol start="10">
  2658. <li>foo
  2659. <ul>
  2660. <li>bar</li>
  2661. </ul>
  2662. </li>
  2663. </ol>
  2664. .
  2665. Three is not enough:
  2666. .
  2667. 10) foo
  2668. - bar
  2669. .
  2670. <ol start="10">
  2671. <li>foo</li>
  2672. </ol>
  2673. <ul>
  2674. <li>bar</li>
  2675. </ul>
  2676. .
  2677. A list may be the first block in a list item:
  2678. .
  2679. - - foo
  2680. .
  2681. <ul>
  2682. <li>
  2683. <ul>
  2684. <li>foo</li>
  2685. </ul>
  2686. </li>
  2687. </ul>
  2688. .
  2689. .
  2690. 1. - 2. foo
  2691. .
  2692. <ol>
  2693. <li>
  2694. <ul>
  2695. <li>
  2696. <ol start="2">
  2697. <li>foo</li>
  2698. </ol>
  2699. </li>
  2700. </ul>
  2701. </li>
  2702. </ol>
  2703. .
  2704. A list item can contain a header:
  2705. .
  2706. - # Foo
  2707. - Bar
  2708. ---
  2709. baz
  2710. .
  2711. <ul>
  2712. <li>
  2713. <h1>Foo</h1>
  2714. </li>
  2715. <li>
  2716. <h2>Bar</h2>
  2717. baz</li>
  2718. </ul>
  2719. .
  2720. ### Motivation
  2721. John Gruber's Markdown spec says the following about list items:
  2722. 1. "List markers typically start at the left margin, but may be indented
  2723. by up to three spaces. List markers must be followed by one or more
  2724. spaces or a tab."
  2725. 2. "To make lists look nice, you can wrap items with hanging indents....
  2726. But if you don't want to, you don't have to."
  2727. 3. "List items may consist of multiple paragraphs. Each subsequent
  2728. paragraph in a list item must be indented by either 4 spaces or one
  2729. tab."
  2730. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  2731. but here again, Markdown will allow you to be lazy."
  2732. 5. "To put a blockquote within a list item, the blockquote's `>`
  2733. delimiters need to be indented."
  2734. 6. "To put a code block within a list item, the code block needs to be
  2735. indented twice — 8 spaces or two tabs."
  2736. These rules specify that a paragraph under a list item must be indented
  2737. four spaces (presumably, from the left margin, rather than the start of
  2738. the list marker, but this is not said), and that code under a list item
  2739. must be indented eight spaces instead of the usual four. They also say
  2740. that a block quote must be indented, but not by how much; however, the
  2741. example given has four spaces indentation. Although nothing is said
  2742. about other kinds of block-level content, it is certainly reasonable to
  2743. infer that *all* block elements under a list item, including other
  2744. lists, must be indented four spaces. This principle has been called the
  2745. *four-space rule*.
  2746. The four-space rule is clear and principled, and if the reference
  2747. implementation `Markdown.pl` had followed it, it probably would have
  2748. become the standard. However, `Markdown.pl` allowed paragraphs and
  2749. sublists to start with only two spaces indentation, at least on the
  2750. outer level. Worse, its behavior was inconsistent: a sublist of an
  2751. outer-level list needed two spaces indentation, but a sublist of this
  2752. sublist needed three spaces. It is not surprising, then, that different
  2753. implementations of Markdown have developed very different rules for
  2754. determining what comes under a list item. (Pandoc and python-Markdown,
  2755. for example, stuck with Gruber's syntax description and the four-space
  2756. rule, while discount, redcarpet, marked, PHP Markdown, and others
  2757. followed `Markdown.pl`'s behavior more closely.)
  2758. Unfortunately, given the divergences between implementations, there
  2759. is no way to give a spec for list items that will be guaranteed not
  2760. to break any existing documents. However, the spec given here should
  2761. correctly handle lists formatted with either the four-space rule or
  2762. the more forgiving `Markdown.pl` behavior, provided they are laid out
  2763. in a way that is natural for a human to read.
  2764. The strategy here is to let the width and indentation of the list marker
  2765. determine the indentation necessary for blocks to fall under the list
  2766. item, rather than having a fixed and arbitrary number. The writer can
  2767. think of the body of the list item as a unit which gets indented to the
  2768. right enough to fit the list marker (and any indentation on the list
  2769. marker). (The laziness rule, #5, then allows continuation lines to be
  2770. unindented if needed.)
  2771. This rule is superior, we claim, to any rule requiring a fixed level of
  2772. indentation from the margin. The four-space rule is clear but
  2773. unnatural. It is quite unintuitive that
  2774. ``` markdown
  2775. - foo
  2776. bar
  2777. - baz
  2778. ```
  2779. should be parsed as two lists with an intervening paragraph,
  2780. ``` html
  2781. <ul>
  2782. <li>foo</li>
  2783. </ul>
  2784. <p>bar</p>
  2785. <ul>
  2786. <li>baz</li>
  2787. </ul>
  2788. ```
  2789. as the four-space rule demands, rather than a single list,
  2790. ``` html
  2791. <ul>
  2792. <li>
  2793. <p>foo</p>
  2794. <p>bar</p>
  2795. <ul>
  2796. <li>baz</li>
  2797. </ul>
  2798. </li>
  2799. </ul>
  2800. ```
  2801. The choice of four spaces is arbitrary. It can be learned, but it is
  2802. not likely to be guessed, and it trips up beginners regularly.
  2803. Would it help to adopt a two-space rule? The problem is that such
  2804. a rule, together with the rule allowing 1--3 spaces indentation of the
  2805. initial list marker, allows text that is indented *less than* the
  2806. original list marker to be included in the list item. For example,
  2807. `Markdown.pl` parses
  2808. ``` markdown
  2809. - one
  2810. two
  2811. ```
  2812. as a single list item, with `two` a continuation paragraph:
  2813. ``` html
  2814. <ul>
  2815. <li>
  2816. <p>one</p>
  2817. <p>two</p>
  2818. </li>
  2819. </ul>
  2820. ```
  2821. and similarly
  2822. ``` markdown
  2823. > - one
  2824. >
  2825. > two
  2826. ```
  2827. as
  2828. ``` html
  2829. <blockquote>
  2830. <ul>
  2831. <li>
  2832. <p>one</p>
  2833. <p>two</p>
  2834. </li>
  2835. </ul>
  2836. </blockquote>
  2837. ```
  2838. This is extremely unintuitive.
  2839. Rather than requiring a fixed indent from the margin, we could require
  2840. a fixed indent (say, two spaces, or even one space) from the list marker (which
  2841. may itself be indented). This proposal would remove the last anomaly
  2842. discussed. Unlike the spec presented above, it would count the following
  2843. as a list item with a subparagraph, even though the paragraph `bar`
  2844. is not indented as far as the first paragraph `foo`:
  2845. ``` markdown
  2846. 10. foo
  2847. bar
  2848. ```
  2849. Arguably this text does read like a list item with `bar` as a subparagraph,
  2850. which may count in favor of the proposal. However, on this proposal indented
  2851. code would have to be indented six spaces after the list marker. And this
  2852. would break a lot of existing Markdown, which has the pattern:
  2853. ``` markdown
  2854. 1. foo
  2855. indented code
  2856. ```
  2857. where the code is indented eight spaces. The spec above, by contrast, will
  2858. parse this text as expected, since the code block's indentation is measured
  2859. from the beginning of `foo`.
  2860. The one case that needs special treatment is a list item that *starts*
  2861. with indented code. How much indentation is required in that case, since
  2862. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  2863. that in such cases, we require one space indentation from the list marker
  2864. (and then the normal four spaces for the indented code). This will match the
  2865. four-space rule in cases where the list marker plus its initial indentation
  2866. takes four spaces (a common case), but diverge in other cases.
  2867. ## Lists
  2868. A [list](@list) is a sequence of one or more
  2869. list items [of the same type]. The list items
  2870. may be separated by single [blank lines], but two
  2871. blank lines end all containing lists.
  2872. Two list items are [of the same type](@of-the-same-type)
  2873. if they begin with a [list marker] of the same type.
  2874. Two list markers are of the
  2875. same type if (a) they are bullet list markers using the same character
  2876. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  2877. delimiter (either `.` or `)`).
  2878. A list is an [ordered list](@ordered-list)
  2879. if its constituent list items begin with
  2880. [ordered list marker]s, and a
  2881. [bullet list](@bullet-list) if its constituent list
  2882. items begin with [bullet list marker]s.
  2883. The [start number](@start-number)
  2884. of an [ordered list] is determined by the list number of
  2885. its initial list item. The numbers of subsequent list items are
  2886. disregarded.
  2887. A list is [loose](@loose) if it any of its constituent
  2888. list items are separated by blank lines, or if any of its constituent
  2889. list items directly contain two block-level elements with a blank line
  2890. between them. Otherwise a list is [tight](@tight).
  2891. (The difference in HTML output is that paragraphs in a loose list are
  2892. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  2893. Changing the bullet or ordered list delimiter starts a new list:
  2894. .
  2895. - foo
  2896. - bar
  2897. + baz
  2898. .
  2899. <ul>
  2900. <li>foo</li>
  2901. <li>bar</li>
  2902. </ul>
  2903. <ul>
  2904. <li>baz</li>
  2905. </ul>
  2906. .
  2907. .
  2908. 1. foo
  2909. 2. bar
  2910. 3) baz
  2911. .
  2912. <ol>
  2913. <li>foo</li>
  2914. <li>bar</li>
  2915. </ol>
  2916. <ol start="3">
  2917. <li>baz</li>
  2918. </ol>
  2919. .
  2920. In CommonMark, a list can interrupt a paragraph. That is,
  2921. no blank line is needed to separate a paragraph from a following
  2922. list:
  2923. .
  2924. Foo
  2925. - bar
  2926. - baz
  2927. .
  2928. <p>Foo</p>
  2929. <ul>
  2930. <li>bar</li>
  2931. <li>baz</li>
  2932. </ul>
  2933. .
  2934. `Markdown.pl` does not allow this, through fear of triggering a list
  2935. via a numeral in a hard-wrapped line:
  2936. .
  2937. The number of windows in my house is
  2938. 14. The number of doors is 6.
  2939. .
  2940. <p>The number of windows in my house is</p>
  2941. <ol start="14">
  2942. <li>The number of doors is 6.</li>
  2943. </ol>
  2944. .
  2945. Oddly, `Markdown.pl` *does* allow a blockquote to interrupt a paragraph,
  2946. even though the same considerations might apply. We think that the two
  2947. cases should be treated the same. Here are two reasons for allowing
  2948. lists to interrupt paragraphs:
  2949. First, it is natural and not uncommon for people to start lists without
  2950. blank lines:
  2951. I need to buy
  2952. - new shoes
  2953. - a coat
  2954. - a plane ticket
  2955. Second, we are attracted to a
  2956. > [principle of uniformity](@principle-of-uniformity):
  2957. > if a chunk of text has a certain
  2958. > meaning, it will continue to have the same meaning when put into a
  2959. > container block (such as a list item or blockquote).
  2960. (Indeed, the spec for [list items] and [block quotes] presupposes
  2961. this principle.) This principle implies that if
  2962. * I need to buy
  2963. - new shoes
  2964. - a coat
  2965. - a plane ticket
  2966. is a list item containing a paragraph followed by a nested sublist,
  2967. as all Markdown implementations agree it is (though the paragraph
  2968. may be rendered without `<p>` tags, since the list is "tight"),
  2969. then
  2970. I need to buy
  2971. - new shoes
  2972. - a coat
  2973. - a plane ticket
  2974. by itself should be a paragraph followed by a nested sublist.
  2975. Our adherence to the [principle of uniformity]
  2976. thus inclines us to think that there are two coherent packages:
  2977. 1. Require blank lines before *all* lists and blockquotes,
  2978. including lists that occur as sublists inside other list items.
  2979. 2. Require blank lines in none of these places.
  2980. [reStructuredText](http://docutils.sourceforge.net/rst.html) takes
  2981. the first approach, for which there is much to be said. But the second
  2982. seems more consistent with established practice with Markdown.
  2983. There can be blank lines between items, but two blank lines end
  2984. a list:
  2985. .
  2986. - foo
  2987. - bar
  2988. - baz
  2989. .
  2990. <ul>
  2991. <li>
  2992. <p>foo</p>
  2993. </li>
  2994. <li>
  2995. <p>bar</p>
  2996. </li>
  2997. </ul>
  2998. <ul>
  2999. <li>baz</li>
  3000. </ul>
  3001. .
  3002. As illustrated above in the section on [list items],
  3003. two blank lines between blocks *within* a list item will also end a
  3004. list:
  3005. .
  3006. - foo
  3007. bar
  3008. - baz
  3009. .
  3010. <ul>
  3011. <li>foo</li>
  3012. </ul>
  3013. <p>bar</p>
  3014. <ul>
  3015. <li>baz</li>
  3016. </ul>
  3017. .
  3018. Indeed, two blank lines will end *all* containing lists:
  3019. .
  3020. - foo
  3021. - bar
  3022. - baz
  3023. bim
  3024. .
  3025. <ul>
  3026. <li>foo
  3027. <ul>
  3028. <li>bar
  3029. <ul>
  3030. <li>baz</li>
  3031. </ul>
  3032. </li>
  3033. </ul>
  3034. </li>
  3035. </ul>
  3036. <pre><code> bim
  3037. </code></pre>
  3038. .
  3039. Thus, two blank lines can be used to separate consecutive lists of
  3040. the same type, or to separate a list from an indented code block
  3041. that would otherwise be parsed as a subparagraph of the final list
  3042. item:
  3043. .
  3044. - foo
  3045. - bar
  3046. - baz
  3047. - bim
  3048. .
  3049. <ul>
  3050. <li>foo</li>
  3051. <li>bar</li>
  3052. </ul>
  3053. <ul>
  3054. <li>baz</li>
  3055. <li>bim</li>
  3056. </ul>
  3057. .
  3058. .
  3059. - foo
  3060. notcode
  3061. - foo
  3062. code
  3063. .
  3064. <ul>
  3065. <li>
  3066. <p>foo</p>
  3067. <p>notcode</p>
  3068. </li>
  3069. <li>
  3070. <p>foo</p>
  3071. </li>
  3072. </ul>
  3073. <pre><code>code
  3074. </code></pre>
  3075. .
  3076. List items need not be indented to the same level. The following
  3077. list items will be treated as items at the same list level,
  3078. since none is indented enough to belong to the previous list
  3079. item:
  3080. .
  3081. - a
  3082. - b
  3083. - c
  3084. - d
  3085. - e
  3086. - f
  3087. - g
  3088. .
  3089. <ul>
  3090. <li>a</li>
  3091. <li>b</li>
  3092. <li>c</li>
  3093. <li>d</li>
  3094. <li>e</li>
  3095. <li>f</li>
  3096. <li>g</li>
  3097. </ul>
  3098. .
  3099. This is a loose list, because there is a blank line between
  3100. two of the list items:
  3101. .
  3102. - a
  3103. - b
  3104. - c
  3105. .
  3106. <ul>
  3107. <li>
  3108. <p>a</p>
  3109. </li>
  3110. <li>
  3111. <p>b</p>
  3112. </li>
  3113. <li>
  3114. <p>c</p>
  3115. </li>
  3116. </ul>
  3117. .
  3118. So is this, with a empty second item:
  3119. .
  3120. * a
  3121. *
  3122. * c
  3123. .
  3124. <ul>
  3125. <li>
  3126. <p>a</p>
  3127. </li>
  3128. <li></li>
  3129. <li>
  3130. <p>c</p>
  3131. </li>
  3132. </ul>
  3133. .
  3134. These are loose lists, even though there is no space between the items,
  3135. because one of the items directly contains two block-level elements
  3136. with a blank line between them:
  3137. .
  3138. - a
  3139. - b
  3140. c
  3141. - d
  3142. .
  3143. <ul>
  3144. <li>
  3145. <p>a</p>
  3146. </li>
  3147. <li>
  3148. <p>b</p>
  3149. <p>c</p>
  3150. </li>
  3151. <li>
  3152. <p>d</p>
  3153. </li>
  3154. </ul>
  3155. .
  3156. .
  3157. - a
  3158. - b
  3159. [ref]: /url
  3160. - d
  3161. .
  3162. <ul>
  3163. <li>
  3164. <p>a</p>
  3165. </li>
  3166. <li>
  3167. <p>b</p>
  3168. </li>
  3169. <li>
  3170. <p>d</p>
  3171. </li>
  3172. </ul>
  3173. .
  3174. This is a tight list, because the blank lines are in a code block:
  3175. .
  3176. - a
  3177. - ```
  3178. b
  3179. ```
  3180. - c
  3181. .
  3182. <ul>
  3183. <li>a</li>
  3184. <li>
  3185. <pre><code>b
  3186. </code></pre>
  3187. </li>
  3188. <li>c</li>
  3189. </ul>
  3190. .
  3191. This is a tight list, because the blank line is between two
  3192. paragraphs of a sublist. So the sublist is loose while
  3193. the outer list is tight:
  3194. .
  3195. - a
  3196. - b
  3197. c
  3198. - d
  3199. .
  3200. <ul>
  3201. <li>a
  3202. <ul>
  3203. <li>
  3204. <p>b</p>
  3205. <p>c</p>
  3206. </li>
  3207. </ul>
  3208. </li>
  3209. <li>d</li>
  3210. </ul>
  3211. .
  3212. This is a tight list, because the blank line is inside the
  3213. block quote:
  3214. .
  3215. * a
  3216. > b
  3217. >
  3218. * c
  3219. .
  3220. <ul>
  3221. <li>a
  3222. <blockquote>
  3223. <p>b</p>
  3224. </blockquote>
  3225. </li>
  3226. <li>c</li>
  3227. </ul>
  3228. .
  3229. This list is tight, because the consecutive block elements
  3230. are not separated by blank lines:
  3231. .
  3232. - a
  3233. > b
  3234. ```
  3235. c
  3236. ```
  3237. - d
  3238. .
  3239. <ul>
  3240. <li>a
  3241. <blockquote>
  3242. <p>b</p>
  3243. </blockquote>
  3244. <pre><code>c
  3245. </code></pre>
  3246. </li>
  3247. <li>d</li>
  3248. </ul>
  3249. .
  3250. A single-paragraph list is tight:
  3251. .
  3252. - a
  3253. .
  3254. <ul>
  3255. <li>a</li>
  3256. </ul>
  3257. .
  3258. .
  3259. - a
  3260. - b
  3261. .
  3262. <ul>
  3263. <li>a
  3264. <ul>
  3265. <li>b</li>
  3266. </ul>
  3267. </li>
  3268. </ul>
  3269. .
  3270. This list is loose, because of the blank line between the
  3271. two block elements in the list item:
  3272. .
  3273. 1. ```
  3274. foo
  3275. ```
  3276. bar
  3277. .
  3278. <ol>
  3279. <li>
  3280. <pre><code>foo
  3281. </code></pre>
  3282. <p>bar</p>
  3283. </li>
  3284. </ol>
  3285. .
  3286. Here the outer list is loose, the inner list tight:
  3287. .
  3288. * foo
  3289. * bar
  3290. baz
  3291. .
  3292. <ul>
  3293. <li>
  3294. <p>foo</p>
  3295. <ul>
  3296. <li>bar</li>
  3297. </ul>
  3298. <p>baz</p>
  3299. </li>
  3300. </ul>
  3301. .
  3302. .
  3303. - a
  3304. - b
  3305. - c
  3306. - d
  3307. - e
  3308. - f
  3309. .
  3310. <ul>
  3311. <li>
  3312. <p>a</p>
  3313. <ul>
  3314. <li>b</li>
  3315. <li>c</li>
  3316. </ul>
  3317. </li>
  3318. <li>
  3319. <p>d</p>
  3320. <ul>
  3321. <li>e</li>
  3322. <li>f</li>
  3323. </ul>
  3324. </li>
  3325. </ul>
  3326. .
  3327. # Inlines
  3328. Inlines are parsed sequentially from the beginning of the character
  3329. stream to the end (left to right, in left-to-right languages).
  3330. Thus, for example, in
  3331. .
  3332. `hi`lo`
  3333. .
  3334. <p><code>hi</code>lo`</p>
  3335. .
  3336. `hi` is parsed as code, leaving the backtick at the end as a literal
  3337. backtick.
  3338. ## Backslash escapes
  3339. Any ASCII punctuation character may be backslash-escaped:
  3340. .
  3341. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  3342. .
  3343. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  3344. .
  3345. Backslashes before other characters are treated as literal
  3346. backslashes:
  3347. .
  3348. \→\A\a\ \3\φ\«
  3349. .
  3350. <p>\ \A\a\ \3\φ\«</p>
  3351. .
  3352. Escaped characters are treated as regular characters and do
  3353. not have their usual Markdown meanings:
  3354. .
  3355. \*not emphasized*
  3356. \<br/> not a tag
  3357. \[not a link](/foo)
  3358. \`not code`
  3359. 1\. not a list
  3360. \* not a list
  3361. \# not a header
  3362. \[foo]: /url "not a reference"
  3363. .
  3364. <p>*not emphasized*
  3365. &lt;br/&gt; not a tag
  3366. [not a link](/foo)
  3367. `not code`
  3368. 1. not a list
  3369. * not a list
  3370. # not a header
  3371. [foo]: /url &quot;not a reference&quot;</p>
  3372. .
  3373. If a backslash is itself escaped, the following character is not:
  3374. .
  3375. \\*emphasis*
  3376. .
  3377. <p>\<em>emphasis</em></p>
  3378. .
  3379. A backslash at the end of the line is a [hard line break]:
  3380. .
  3381. foo\
  3382. bar
  3383. .
  3384. <p>foo<br />
  3385. bar</p>
  3386. .
  3387. Backslash escapes do not work in code blocks, code spans, autolinks, or
  3388. raw HTML:
  3389. .
  3390. `` \[\` ``
  3391. .
  3392. <p><code>\[\`</code></p>
  3393. .
  3394. .
  3395. \[\]
  3396. .
  3397. <pre><code>\[\]
  3398. </code></pre>
  3399. .
  3400. .
  3401. ~~~
  3402. \[\]
  3403. ~~~
  3404. .
  3405. <pre><code>\[\]
  3406. </code></pre>
  3407. .
  3408. .
  3409. <http://example.com?find=\*>
  3410. .
  3411. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  3412. .
  3413. .
  3414. <a href="/bar\/)">
  3415. .
  3416. <p><a href="/bar\/)"></p>
  3417. .
  3418. But they work in all other contexts, including URLs and link titles,
  3419. link references, and [info string]s in [fenced code block]s:
  3420. .
  3421. [foo](/bar\* "ti\*tle")
  3422. .
  3423. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3424. .
  3425. .
  3426. [foo]
  3427. [foo]: /bar\* "ti\*tle"
  3428. .
  3429. <p><a href="/bar*" title="ti*tle">foo</a></p>
  3430. .
  3431. .
  3432. ``` foo\+bar
  3433. foo
  3434. ```
  3435. .
  3436. <pre><code class="language-foo+bar">foo
  3437. </code></pre>
  3438. .
  3439. ## Entities
  3440. With the goal of making this standard as HTML-agnostic as possible, all
  3441. valid HTML entities (except in code blocks and code spans)
  3442. are recognized as such and converted into unicode characters before
  3443. they are stored in the AST. This means that renderers to formats other
  3444. than HTML need not be HTML-entity aware. HTML renderers may either escape
  3445. unicode characters as entities or leave them as they are. (However,
  3446. `"`, `&`, `<`, and `>` must always be rendered as entities.)
  3447. [Named entities](@name-entities) consist of `&`
  3448. + any of the valid HTML5 entity names + `;`. The
  3449. [following document](https://html.spec.whatwg.org/multipage/entities.json)
  3450. is used as an authoritative source of the valid entity names and their
  3451. corresponding codepoints.
  3452. .
  3453. &nbsp; &amp; &copy; &AElig; &Dcaron; &frac34; &HilbertSpace; &DifferentialD; &ClockwiseContourIntegral;
  3454. .
  3455. <p>  &amp; © Æ Ď ¾ ℋ ⅆ ∲</p>
  3456. .
  3457. [Decimal entities](@decimal-entities)
  3458. consist of `&#` + a string of 1--8 arabic digits + `;`. Again, these
  3459. entities need to be recognised and tranformed into their corresponding
  3460. UTF8 codepoints. Invalid Unicode codepoints will be written as the
  3461. "unknown codepoint" character (`0xFFFD`)
  3462. .
  3463. &#35; &#1234; &#992; &#98765432;
  3464. .
  3465. <p># Ӓ Ϡ �</p>
  3466. .
  3467. [Hexadecimal entities](@hexadecimal-entities)
  3468. consist of `&#` + either `X` or `x` + a string of 1-8 hexadecimal digits
  3469. + `;`. They will also be parsed and turned into their corresponding UTF8 values in the AST.
  3470. .
  3471. &#X22; &#XD06; &#xcab;
  3472. .
  3473. <p>&quot; ആ ಫ</p>
  3474. .
  3475. Here are some nonentities:
  3476. .
  3477. &nbsp &x; &#; &#x; &ThisIsWayTooLongToBeAnEntityIsntIt; &hi?;
  3478. .
  3479. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x; &amp;ThisIsWayTooLongToBeAnEntityIsntIt; &amp;hi?;</p>
  3480. .
  3481. Although HTML5 does accept some entities without a trailing semicolon
  3482. (such as `&copy`), these are not recognized as entities here, because it
  3483. makes the grammar too ambiguous:
  3484. .
  3485. &copy
  3486. .
  3487. <p>&amp;copy</p>
  3488. .
  3489. Strings that are not on the list of HTML5 named entities are not
  3490. recognized as entities either:
  3491. .
  3492. &MadeUpEntity;
  3493. .
  3494. <p>&amp;MadeUpEntity;</p>
  3495. .
  3496. Entities are recognized in any context besides code spans or
  3497. code blocks, including raw HTML, URLs, [link title]s, and
  3498. [fenced code block] [info string]s:
  3499. .
  3500. <a href="&ouml;&ouml;.html">
  3501. .
  3502. <p><a href="&ouml;&ouml;.html"></p>
  3503. .
  3504. .
  3505. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  3506. .
  3507. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3508. .
  3509. .
  3510. [foo]
  3511. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  3512. .
  3513. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  3514. .
  3515. .
  3516. ``` f&ouml;&ouml;
  3517. foo
  3518. ```
  3519. .
  3520. <pre><code class="language-föö">foo
  3521. </code></pre>
  3522. .
  3523. Entities are treated as literal text in code spans and code blocks:
  3524. .
  3525. `f&ouml;&ouml;`
  3526. .
  3527. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  3528. .
  3529. .
  3530. f&ouml;f&ouml;
  3531. .
  3532. <pre><code>f&amp;ouml;f&amp;ouml;
  3533. </code></pre>
  3534. .
  3535. ## Code spans
  3536. A [backtick string](@backtick-string)
  3537. is a string of one or more backtick characters (`` ` ``) that is neither
  3538. preceded nor followed by a backtick.
  3539. A [code span](@code-span) begins with a backtick string and ends with
  3540. a backtick string of equal length. The contents of the code span are
  3541. the characters between the two backtick strings, with leading and
  3542. trailing spaces and [line ending]s removed, and
  3543. [whitespace] collapsed to single spaces.
  3544. This is a simple code span:
  3545. .
  3546. `foo`
  3547. .
  3548. <p><code>foo</code></p>
  3549. .
  3550. Here two backticks are used, because the code contains a backtick.
  3551. This example also illustrates stripping of leading and trailing spaces:
  3552. .
  3553. `` foo ` bar ``
  3554. .
  3555. <p><code>foo ` bar</code></p>
  3556. .
  3557. This example shows the motivation for stripping leading and trailing
  3558. spaces:
  3559. .
  3560. ` `` `
  3561. .
  3562. <p><code>``</code></p>
  3563. .
  3564. [Line ending]s are treated like spaces:
  3565. .
  3566. ``
  3567. foo
  3568. ``
  3569. .
  3570. <p><code>foo</code></p>
  3571. .
  3572. Interior spaces and [line ending]s are collapsed into
  3573. single spaces, just as they would be by a browser:
  3574. .
  3575. `foo bar
  3576. baz`
  3577. .
  3578. <p><code>foo bar baz</code></p>
  3579. .
  3580. Q: Why not just leave the spaces, since browsers will collapse them
  3581. anyway? A: Because we might be targeting a non-HTML format, and we
  3582. shouldn't rely on HTML-specific rendering assumptions.
  3583. (Existing implementations differ in their treatment of internal
  3584. spaces and [line ending]s. Some, including `Markdown.pl` and
  3585. `showdown`, convert an internal [line ending] into a
  3586. `<br />` tag. But this makes things difficult for those who like to
  3587. hard-wrap their paragraphs, since a line break in the midst of a code
  3588. span will cause an unintended line break in the output. Others just
  3589. leave internal spaces as they are, which is fine if only HTML is being
  3590. targeted.)
  3591. .
  3592. `foo `` bar`
  3593. .
  3594. <p><code>foo `` bar</code></p>
  3595. .
  3596. Note that backslash escapes do not work in code spans. All backslashes
  3597. are treated literally:
  3598. .
  3599. `foo\`bar`
  3600. .
  3601. <p><code>foo\</code>bar`</p>
  3602. .
  3603. Backslash escapes are never needed, because one can always choose a
  3604. string of *n* backtick characters as delimiters, where the code does
  3605. not contain any strings of exactly *n* backtick characters.
  3606. Code span backticks have higher precedence than any other inline
  3607. constructs except HTML tags and autolinks. Thus, for example, this is
  3608. not parsed as emphasized text, since the second `*` is part of a code
  3609. span:
  3610. .
  3611. *foo`*`
  3612. .
  3613. <p>*foo<code>*</code></p>
  3614. .
  3615. And this is not parsed as a link:
  3616. .
  3617. [not a `link](/foo`)
  3618. .
  3619. <p>[not a <code>link](/foo</code>)</p>
  3620. .
  3621. Code spans, HTML tags, and autolinks have the same precedence.
  3622. Thus, this is code:
  3623. .
  3624. `<a href="`">`
  3625. .
  3626. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  3627. .
  3628. But this is an HTML tag:
  3629. .
  3630. <a href="`">`
  3631. .
  3632. <p><a href="`">`</p>
  3633. .
  3634. And this is code:
  3635. .
  3636. `<http://foo.bar.`baz>`
  3637. .
  3638. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  3639. .
  3640. But this is an autolink:
  3641. .
  3642. <http://foo.bar.`baz>`
  3643. .
  3644. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  3645. .
  3646. When a backtick string is not closed by a matching backtick string,
  3647. we just have literal backticks:
  3648. .
  3649. ```foo``
  3650. .
  3651. <p>```foo``</p>
  3652. .
  3653. .
  3654. `foo
  3655. .
  3656. <p>`foo</p>
  3657. .
  3658. ## Emphasis and strong emphasis
  3659. John Gruber's original [Markdown syntax
  3660. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  3661. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  3662. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  3663. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  3664. > tag.
  3665. This is enough for most users, but these rules leave much undecided,
  3666. especially when it comes to nested emphasis. The original
  3667. `Markdown.pl` test suite makes it clear that triple `***` and
  3668. `___` delimiters can be used for strong emphasis, and most
  3669. implementations have also allowed the following patterns:
  3670. ``` markdown
  3671. ***strong emph***
  3672. ***strong** in emph*
  3673. ***emph* in strong**
  3674. **in strong *emph***
  3675. *in emph **strong***
  3676. ```
  3677. The following patterns are less widely supported, but the intent
  3678. is clear and they are useful (especially in contexts like bibliography
  3679. entries):
  3680. ``` markdown
  3681. *emph *with emph* in it*
  3682. **strong **with strong** in it**
  3683. ```
  3684. Many implementations have also restricted intraword emphasis to
  3685. the `*` forms, to avoid unwanted emphasis in words containing
  3686. internal underscores. (It is best practice to put these in code
  3687. spans, but users often do not.)
  3688. ``` markdown
  3689. internal emphasis: foo*bar*baz
  3690. no emphasis: foo_bar_baz
  3691. ```
  3692. The rules given below capture all of these patterns, while allowing
  3693. for efficient parsing strategies that do not backtrack.
  3694. First, some definitions. A [delimiter run](@delimiter-run) is either
  3695. a sequence of one or more `*` characters that is not preceded or
  3696. followed by a `*` character, or a sequence of one or more `_`
  3697. characters that is not preceded or followed by a `_` character.
  3698. A [left-flanking delimiter run](@left-flanking-delimiter-run) is
  3699. a [delimiter run] that is (a) not followed by [unicode whitespace],
  3700. and (b) either not followed by a [punctuation character], or
  3701. preceded by [unicode whitespace] or a [punctuation character].
  3702. A [right-flanking delimiter run](@right-flanking-delimiter-run) is
  3703. a [delimiter run] that is (a) not preceded by [unicode whitespace],
  3704. and (b) either not preceded by a [punctuation character], or
  3705. followed by [unicode whitespace] or a [punctuation character].
  3706. Here are some examples of delimiter runs.
  3707. - left-flanking but not right-flanking:
  3708. ```
  3709. ***abc
  3710. _abc
  3711. **"abc"
  3712. _"abc"
  3713. ```
  3714. - right-flanking but not left-flanking:
  3715. ```
  3716. abc***
  3717. abc_
  3718. "abc"**
  3719. _"abc"
  3720. ```
  3721. - Both right and right-flanking:
  3722. ```
  3723. abc***def
  3724. "abc"_"def"
  3725. ```
  3726. - Neither right nor right-flanking:
  3727. ```
  3728. abc *** def
  3729. a _ b
  3730. ```
  3731. (The idea of distinguishing left-flanking and right-flanking
  3732. delimiter runs based on the character before and the character
  3733. after comes from Roopesh Chander's
  3734. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  3735. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  3736. run," and its rules for distinguishing left- and right-flanking runs
  3737. are a bit more complex than the ones given here.)
  3738. The following rules define emphasis and strong emphasis:
  3739. 1. A single `*` character [can open emphasis](@can-open-emphasis)
  3740. iff it is part of a [left-flanking delimiter run].
  3741. 2. A single `_` character [can open emphasis] iff
  3742. it is part of a [left-flanking delimiter run]
  3743. and not part of a [right-flanking delimiter run].
  3744. 3. A single `*` character [can close emphasis](@can-close-emphasis)
  3745. iff it is part of a [right-flanking delimiter run].
  3746. 4. A single `_` character [can close emphasis]
  3747. iff it is part of a [right-flanking delimiter run]
  3748. and not part of a [left-flanking delimiter run].
  3749. 5. A double `**` [can open strong emphasis](@can-open-strong-emphasis)
  3750. iff it is part of a [left-flanking delimiter run].
  3751. 6. A double `__` [can open strong emphasis]
  3752. iff it is part of a [left-flanking delimiter run]
  3753. and not part of a [right-flanking delimiter run].
  3754. 7. A double `**` [can close strong emphasis](@can-close-strong-emphasis)
  3755. iff it is part of a [right-flanking delimiter run].
  3756. 8. A double `__` [can close strong emphasis]
  3757. iff it is part of a [right-flanking delimiter run]
  3758. and not part of a [left-flanking delimiter run].
  3759. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  3760. with a delimiter that [can close emphasis], and that uses the same
  3761. character (`_` or `*`) as the opening delimiter. There must
  3762. be a nonempty sequence of inlines between the open delimiter
  3763. and the closing delimiter; these form the contents of the emphasis
  3764. inline.
  3765. 10. Strong emphasis begins with a delimiter that
  3766. [can open strong emphasis] and ends with a delimiter that
  3767. [can close strong emphasis], and that uses the same character
  3768. (`_` or `*`) as the opening delimiter.
  3769. There must be a nonempty sequence of inlines between the open
  3770. delimiter and the closing delimiter; these form the contents of
  3771. the strong emphasis inline.
  3772. 11. A literal `*` character cannot occur at the beginning or end of
  3773. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  3774. is backslash-escaped.
  3775. 12. A literal `_` character cannot occur at the beginning or end of
  3776. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  3777. is backslash-escaped.
  3778. Where rules 1--12 above are compatible with multiple parsings,
  3779. the following principles resolve ambiguity:
  3780. 13. The number of nestings should be minimized. Thus, for example,
  3781. an interpretation `<strong>...</strong>` is always preferred to
  3782. `<em><em>...</em></em>`.
  3783. 14. An interpretation `<strong><em>...</em></strong>` is always
  3784. preferred to `<em><strong>..</strong></em>`.
  3785. 15. When two potential emphasis or strong emphasis spans overlap,
  3786. so that the second begins before the first ends and ends after
  3787. the first ends, the first takes precedence. Thus, for example,
  3788. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  3789. than `*foo <em>bar* baz</em>`. For the same reason,
  3790. `**foo*bar**` is parsed as `<em><em>foo</em>bar</em>*`
  3791. rather than `<strong>foo*bar</strong>`.
  3792. 16. When there are two potential emphasis or strong emphasis spans
  3793. with the same closing delimiter, the shorter one (the one that
  3794. opens later) takes precedence. Thus, for example,
  3795. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  3796. rather than `<strong>foo **bar baz</strong>`.
  3797. 17. Inline code spans, links, images, and HTML tags group more tightly
  3798. than emphasis. So, when there is a choice between an interpretation
  3799. that contains one of these elements and one that does not, the
  3800. former always wins. Thus, for example, `*[foo*](bar)` is
  3801. parsed as `*<a href="bar">foo*</a>` rather than as
  3802. `<em>[foo</em>](bar)`.
  3803. These rules can be illustrated through a series of examples.
  3804. Rule 1:
  3805. .
  3806. *foo bar*
  3807. .
  3808. <p><em>foo bar</em></p>
  3809. .
  3810. This is not emphasis, because the opening `*` is followed by
  3811. whitespace, and hence not part of a [left-flanking delimiter run]:
  3812. .
  3813. a * foo bar*
  3814. .
  3815. <p>a * foo bar*</p>
  3816. .
  3817. This is not emphasis, because the opening `*` is preceded
  3818. by an alphanumeric and followed by punctuation, and hence
  3819. not part of a [left-flanking delimiter run]:
  3820. .
  3821. a*"foo"*
  3822. .
  3823. <p>a*&quot;foo&quot;*</p>
  3824. .
  3825. Unicode nonbreaking spaces count as whitespace, too:
  3826. .
  3827. * a *
  3828. .
  3829. <p>* a *</p>
  3830. .
  3831. Intraword emphasis with `*` is permitted:
  3832. .
  3833. foo*bar*
  3834. .
  3835. <p>foo<em>bar</em></p>
  3836. .
  3837. .
  3838. 5*6*78
  3839. .
  3840. <p>5<em>6</em>78</p>
  3841. .
  3842. Rule 2:
  3843. .
  3844. _foo bar_
  3845. .
  3846. <p><em>foo bar</em></p>
  3847. .
  3848. This is not emphasis, because the opening `_` is followed by
  3849. whitespace:
  3850. .
  3851. _ foo bar_
  3852. .
  3853. <p>_ foo bar_</p>
  3854. .
  3855. This is not emphasis, because the opening `_` is preceded
  3856. by an alphanumeric and followed by punctuation:
  3857. .
  3858. a_"foo"_
  3859. .
  3860. <p>a_&quot;foo&quot;_</p>
  3861. .
  3862. Emphasis with `_` is not allowed inside words:
  3863. .
  3864. foo_bar_
  3865. .
  3866. <p>foo_bar_</p>
  3867. .
  3868. .
  3869. 5_6_78
  3870. .
  3871. <p>5_6_78</p>
  3872. .
  3873. .
  3874. пристаням_стремятся_
  3875. .
  3876. <p>пристаням_стремятся_</p>
  3877. .
  3878. Here `_` does not generate emphasis, because the first delimiter run
  3879. is right-flanking and the second left-flanking:
  3880. .
  3881. aa_"bb"_cc
  3882. .
  3883. <p>aa_&quot;bb&quot;_cc</p>
  3884. .
  3885. Here there is no emphasis, because the delimiter runs are
  3886. both left- and right-flanking:
  3887. .
  3888. "aa"_"bb"_"cc"
  3889. .
  3890. <p>&quot;aa&quot;_&quot;bb&quot;_&quot;cc&quot;</p>
  3891. .
  3892. Rule 3:
  3893. This is not emphasis, because the closing delimiter does
  3894. not match the opening delimiter:
  3895. .
  3896. _foo*
  3897. .
  3898. <p>_foo*</p>
  3899. .
  3900. This is not emphasis, because the closing `*` is preceded by
  3901. whitespace:
  3902. .
  3903. *foo bar *
  3904. .
  3905. <p>*foo bar *</p>
  3906. .
  3907. A newline also counts as whitespace:
  3908. .
  3909. *foo bar
  3910. *
  3911. .
  3912. <p>*foo bar</p>
  3913. <ul>
  3914. <li></li>
  3915. </ul>
  3916. .
  3917. This is not emphasis, because the second `*` is
  3918. preceded by punctuation and followed by an alphanumeric
  3919. (hence it is not part of a [right-flanking delimiter run]:
  3920. .
  3921. *(*foo)
  3922. .
  3923. <p>*(*foo)</p>
  3924. .
  3925. The point of this restriction is more easily appreciated
  3926. with this example:
  3927. .
  3928. *(*foo*)*
  3929. .
  3930. <p><em>(<em>foo</em>)</em></p>
  3931. .
  3932. Intraword emphasis with `*` is allowed:
  3933. .
  3934. *foo*bar
  3935. .
  3936. <p><em>foo</em>bar</p>
  3937. .
  3938. Rule 4:
  3939. This is not emphasis, because the closing `_` is preceded by
  3940. whitespace:
  3941. .
  3942. _foo bar _
  3943. .
  3944. <p>_foo bar _</p>
  3945. .
  3946. This is not emphasis, because the second `_` is
  3947. preceded by punctuation and followed by an alphanumeric:
  3948. .
  3949. _(_foo)
  3950. .
  3951. <p>_(_foo)</p>
  3952. .
  3953. This is emphasis within emphasis:
  3954. .
  3955. _(_foo_)_
  3956. .
  3957. <p><em>(<em>foo</em>)</em></p>
  3958. .
  3959. Intraword emphasis is disallowed for `_`:
  3960. .
  3961. _foo_bar
  3962. .
  3963. <p>_foo_bar</p>
  3964. .
  3965. .
  3966. _пристаням_стремятся
  3967. .
  3968. <p>_пристаням_стремятся</p>
  3969. .
  3970. .
  3971. _foo_bar_baz_
  3972. .
  3973. <p><em>foo_bar_baz</em></p>
  3974. .
  3975. Rule 5:
  3976. .
  3977. **foo bar**
  3978. .
  3979. <p><strong>foo bar</strong></p>
  3980. .
  3981. This is not strong emphasis, because the opening delimiter is
  3982. followed by whitespace:
  3983. .
  3984. ** foo bar**
  3985. .
  3986. <p>** foo bar**</p>
  3987. .
  3988. This is not strong emphasis, because the opening `**` is preceded
  3989. by an alphanumeric and followed by punctuation, and hence
  3990. not part of a [left-flanking delimiter run]:
  3991. .
  3992. a**"foo"**
  3993. .
  3994. <p>a**&quot;foo&quot;**</p>
  3995. .
  3996. Intraword strong emphasis with `**` is permitted:
  3997. .
  3998. foo**bar**
  3999. .
  4000. <p>foo<strong>bar</strong></p>
  4001. .
  4002. Rule 6:
  4003. .
  4004. __foo bar__
  4005. .
  4006. <p><strong>foo bar</strong></p>
  4007. .
  4008. This is not strong emphasis, because the opening delimiter is
  4009. followed by whitespace:
  4010. .
  4011. __ foo bar__
  4012. .
  4013. <p>__ foo bar__</p>
  4014. .
  4015. A newline counts as whitespace:
  4016. .
  4017. __
  4018. foo bar__
  4019. .
  4020. <p>__
  4021. foo bar__</p>
  4022. .
  4023. This is not strong emphasis, because the opening `__` is preceded
  4024. by an alphanumeric and followed by punctuation:
  4025. .
  4026. a__"foo"__
  4027. .
  4028. <p>a__&quot;foo&quot;__</p>
  4029. .
  4030. Intraword strong emphasis is forbidden with `__`:
  4031. .
  4032. foo__bar__
  4033. .
  4034. <p>foo__bar__</p>
  4035. .
  4036. .
  4037. 5__6__78
  4038. .
  4039. <p>5__6__78</p>
  4040. .
  4041. .
  4042. пристаням__стремятся__
  4043. .
  4044. <p>пристаням__стремятся__</p>
  4045. .
  4046. .
  4047. __foo, __bar__, baz__
  4048. .
  4049. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4050. .
  4051. Rule 7:
  4052. This is not strong emphasis, because the closing delimiter is preceded
  4053. by whitespace:
  4054. .
  4055. **foo bar **
  4056. .
  4057. <p>**foo bar **</p>
  4058. .
  4059. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4060. Rule 11.)
  4061. This is not strong emphasis, because the second `**` is
  4062. preceded by punctuation and followed by an alphanumeric:
  4063. .
  4064. **(**foo)
  4065. .
  4066. <p>**(**foo)</p>
  4067. .
  4068. The point of this restriction is more easily appreciated
  4069. with these examples:
  4070. .
  4071. *(**foo**)*
  4072. .
  4073. <p><em>(<strong>foo</strong>)</em></p>
  4074. .
  4075. .
  4076. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4077. *Asclepias physocarpa*)**
  4078. .
  4079. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4080. <em>Asclepias physocarpa</em>)</strong></p>
  4081. .
  4082. .
  4083. **foo "*bar*" foo**
  4084. .
  4085. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4086. .
  4087. Intraword emphasis:
  4088. .
  4089. **foo**bar
  4090. .
  4091. <p><strong>foo</strong>bar</p>
  4092. .
  4093. Rule 8:
  4094. This is not strong emphasis, because the closing delimiter is
  4095. preceded by whitespace:
  4096. .
  4097. __foo bar __
  4098. .
  4099. <p>__foo bar __</p>
  4100. .
  4101. This is not strong emphasis, because the second `__` is
  4102. preceded by punctuation and followed by an alphanumeric:
  4103. .
  4104. __(__foo)
  4105. .
  4106. <p>__(__foo)</p>
  4107. .
  4108. The point of this restriction is more easily appreciated
  4109. with this example:
  4110. .
  4111. _(__foo__)_
  4112. .
  4113. <p><em>(<strong>foo</strong>)</em></p>
  4114. .
  4115. Intraword strong emphasis is forbidden with `__`:
  4116. .
  4117. __foo__bar
  4118. .
  4119. <p>__foo__bar</p>
  4120. .
  4121. .
  4122. __пристаням__стремятся
  4123. .
  4124. <p>__пристаням__стремятся</p>
  4125. .
  4126. .
  4127. __foo__bar__baz__
  4128. .
  4129. <p><strong>foo__bar__baz</strong></p>
  4130. .
  4131. Rule 9:
  4132. Any nonempty sequence of inline elements can be the contents of an
  4133. emphasized span.
  4134. .
  4135. *foo [bar](/url)*
  4136. .
  4137. <p><em>foo <a href="/url">bar</a></em></p>
  4138. .
  4139. .
  4140. *foo
  4141. bar*
  4142. .
  4143. <p><em>foo
  4144. bar</em></p>
  4145. .
  4146. In particular, emphasis and strong emphasis can be nested
  4147. inside emphasis:
  4148. .
  4149. _foo __bar__ baz_
  4150. .
  4151. <p><em>foo <strong>bar</strong> baz</em></p>
  4152. .
  4153. .
  4154. _foo _bar_ baz_
  4155. .
  4156. <p><em>foo <em>bar</em> baz</em></p>
  4157. .
  4158. .
  4159. __foo_ bar_
  4160. .
  4161. <p><em><em>foo</em> bar</em></p>
  4162. .
  4163. .
  4164. *foo *bar**
  4165. .
  4166. <p><em>foo <em>bar</em></em></p>
  4167. .
  4168. .
  4169. *foo **bar** baz*
  4170. .
  4171. <p><em>foo <strong>bar</strong> baz</em></p>
  4172. .
  4173. But note:
  4174. .
  4175. *foo**bar**baz*
  4176. .
  4177. <p><em>foo</em><em>bar</em><em>baz</em></p>
  4178. .
  4179. The difference is that in the preceding case, the internal delimiters
  4180. [can close emphasis], while in the cases with spaces, they cannot.
  4181. .
  4182. ***foo** bar*
  4183. .
  4184. <p><em><strong>foo</strong> bar</em></p>
  4185. .
  4186. .
  4187. *foo **bar***
  4188. .
  4189. <p><em>foo <strong>bar</strong></em></p>
  4190. .
  4191. Note, however, that in the following case we get no strong
  4192. emphasis, because the opening delimiter is closed by the first
  4193. `*` before `bar`:
  4194. .
  4195. *foo**bar***
  4196. .
  4197. <p><em>foo</em><em>bar</em>**</p>
  4198. .
  4199. Indefinite levels of nesting are possible:
  4200. .
  4201. *foo **bar *baz* bim** bop*
  4202. .
  4203. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  4204. .
  4205. .
  4206. *foo [*bar*](/url)*
  4207. .
  4208. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  4209. .
  4210. There can be no empty emphasis or strong emphasis:
  4211. .
  4212. ** is not an empty emphasis
  4213. .
  4214. <p>** is not an empty emphasis</p>
  4215. .
  4216. .
  4217. **** is not an empty strong emphasis
  4218. .
  4219. <p>**** is not an empty strong emphasis</p>
  4220. .
  4221. Rule 10:
  4222. Any nonempty sequence of inline elements can be the contents of an
  4223. strongly emphasized span.
  4224. .