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