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