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