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