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