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