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