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