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