aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 8f9ac0be654c1f506dc182d495fa4ed95410ce9e (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.29
  5. date: '2019-04-06'
  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 for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a GitHub wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII control character](@) is a character between `U+0000–1F` (both
  250. including) or `U+007F`.
  251. An [ASCII punctuation character](@)
  252. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  253. `*`, `+`, `,`, `-`, `.`, `/` (U+0021–2F),
  254. `:`, `;`, `<`, `=`, `>`, `?`, `@` (U+003A–0040),
  255. `[`, `\`, `]`, `^`, `_`, `` ` `` (U+005B–0060),
  256. `{`, `|`, `}`, or `~` (U+007B–007E).
  257. A [punctuation character](@) is an [ASCII
  258. punctuation character] or anything in
  259. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  260. ## Tabs
  261. Tabs in lines are not expanded to [spaces]. However,
  262. in contexts where whitespace helps to define block structure,
  263. tabs behave as if they were replaced by spaces with a tab stop
  264. of 4 characters.
  265. Thus, for example, a tab can be used instead of four spaces
  266. in an indented code block. (Note, however, that internal
  267. tabs are passed through as literal tabs, not expanded to
  268. spaces.)
  269. ```````````````````````````````` example
  270. →foo→baz→→bim
  271. .
  272. <pre><code>foo→baz→→bim
  273. </code></pre>
  274. ````````````````````````````````
  275. ```````````````````````````````` example
  276. →foo→baz→→bim
  277. .
  278. <pre><code>foo→baz→→bim
  279. </code></pre>
  280. ````````````````````````````````
  281. ```````````````````````````````` example
  282. a→a
  283. ὐ→a
  284. .
  285. <pre><code>a→a
  286. ὐ→a
  287. </code></pre>
  288. ````````````````````````````````
  289. In the following example, a continuation paragraph of a list
  290. item is indented with a tab; this has exactly the same effect
  291. as indentation with four spaces would:
  292. ```````````````````````````````` example
  293. - foo
  294. →bar
  295. .
  296. <ul>
  297. <li>
  298. <p>foo</p>
  299. <p>bar</p>
  300. </li>
  301. </ul>
  302. ````````````````````````````````
  303. ```````````````````````````````` example
  304. - foo
  305. →→bar
  306. .
  307. <ul>
  308. <li>
  309. <p>foo</p>
  310. <pre><code> bar
  311. </code></pre>
  312. </li>
  313. </ul>
  314. ````````````````````````````````
  315. Normally the `>` that begins a block quote may be followed
  316. optionally by a space, which is not considered part of the
  317. content. In the following case `>` is followed by a tab,
  318. which is treated as if it were expanded into three spaces.
  319. Since one of these spaces is considered part of the
  320. delimiter, `foo` is considered to be indented six spaces
  321. inside the block quote context, so we get an indented
  322. code block starting with two spaces.
  323. ```````````````````````````````` example
  324. >→→foo
  325. .
  326. <blockquote>
  327. <pre><code> foo
  328. </code></pre>
  329. </blockquote>
  330. ````````````````````````````````
  331. ```````````````````````````````` example
  332. -→→foo
  333. .
  334. <ul>
  335. <li>
  336. <pre><code> foo
  337. </code></pre>
  338. </li>
  339. </ul>
  340. ````````````````````````````````
  341. ```````````````````````````````` example
  342. foo
  343. →bar
  344. .
  345. <pre><code>foo
  346. bar
  347. </code></pre>
  348. ````````````````````````````````
  349. ```````````````````````````````` example
  350. - foo
  351. - bar
  352. → - baz
  353. .
  354. <ul>
  355. <li>foo
  356. <ul>
  357. <li>bar
  358. <ul>
  359. <li>baz</li>
  360. </ul>
  361. </li>
  362. </ul>
  363. </li>
  364. </ul>
  365. ````````````````````````````````
  366. ```````````````````````````````` example
  367. #→Foo
  368. .
  369. <h1>Foo</h1>
  370. ````````````````````````````````
  371. ```````````````````````````````` example
  372. *→*→*→
  373. .
  374. <hr />
  375. ````````````````````````````````
  376. ## Insecure characters
  377. For security reasons, the Unicode character `U+0000` must be replaced
  378. with the REPLACEMENT CHARACTER (`U+FFFD`).
  379. ## Backslash escapes
  380. Any ASCII punctuation character may be backslash-escaped:
  381. ```````````````````````````````` example
  382. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  383. .
  384. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  385. ````````````````````````````````
  386. Backslashes before other characters are treated as literal
  387. backslashes:
  388. ```````````````````````````````` example
  389. \→\A\a\ \3\φ\«
  390. .
  391. <p>\→\A\a\ \3\φ\«</p>
  392. ````````````````````````````````
  393. Escaped characters are treated as regular characters and do
  394. not have their usual Markdown meanings:
  395. ```````````````````````````````` example
  396. \*not emphasized*
  397. \<br/> not a tag
  398. \[not a link](/foo)
  399. \`not code`
  400. 1\. not a list
  401. \* not a list
  402. \# not a heading
  403. \[foo]: /url "not a reference"
  404. \&ouml; not a character entity
  405. .
  406. <p>*not emphasized*
  407. &lt;br/&gt; not a tag
  408. [not a link](/foo)
  409. `not code`
  410. 1. not a list
  411. * not a list
  412. # not a heading
  413. [foo]: /url &quot;not a reference&quot;
  414. &amp;ouml; not a character entity</p>
  415. ````````````````````````````````
  416. If a backslash is itself escaped, the following character is not:
  417. ```````````````````````````````` example
  418. \\*emphasis*
  419. .
  420. <p>\<em>emphasis</em></p>
  421. ````````````````````````````````
  422. A backslash at the end of the line is a [hard line break]:
  423. ```````````````````````````````` example
  424. foo\
  425. bar
  426. .
  427. <p>foo<br />
  428. bar</p>
  429. ````````````````````````````````
  430. Backslash escapes do not work in code blocks, code spans, autolinks, or
  431. raw HTML:
  432. ```````````````````````````````` example
  433. `` \[\` ``
  434. .
  435. <p><code>\[\`</code></p>
  436. ````````````````````````````````
  437. ```````````````````````````````` example
  438. \[\]
  439. .
  440. <pre><code>\[\]
  441. </code></pre>
  442. ````````````````````````````````
  443. ```````````````````````````````` example
  444. ~~~
  445. \[\]
  446. ~~~
  447. .
  448. <pre><code>\[\]
  449. </code></pre>
  450. ````````````````````````````````
  451. ```````````````````````````````` example
  452. <http://example.com?find=\*>
  453. .
  454. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  455. ````````````````````````````````
  456. ```````````````````````````````` example
  457. <a href="/bar\/)">
  458. .
  459. <a href="/bar\/)">
  460. ````````````````````````````````
  461. But they work in all other contexts, including URLs and link titles,
  462. link references, and [info strings] in [fenced code blocks]:
  463. ```````````````````````````````` example
  464. [foo](/bar\* "ti\*tle")
  465. .
  466. <p><a href="/bar*" title="ti*tle">foo</a></p>
  467. ````````````````````````````````
  468. ```````````````````````````````` example
  469. [foo]
  470. [foo]: /bar\* "ti\*tle"
  471. .
  472. <p><a href="/bar*" title="ti*tle">foo</a></p>
  473. ````````````````````````````````
  474. ```````````````````````````````` example
  475. ``` foo\+bar
  476. foo
  477. ```
  478. .
  479. <pre><code class="language-foo+bar">foo
  480. </code></pre>
  481. ````````````````````````````````
  482. ## Entity and numeric character references
  483. Valid HTML entity references and numeric character references
  484. can be used in place of the corresponding Unicode character,
  485. with the following exceptions:
  486. - Entity and character references are not recognized in code
  487. blocks and code spans.
  488. - Entity and character references cannot stand in place of
  489. special characters that define structural elements in
  490. CommonMark. For example, although `&#42;` can be used
  491. in place of a literal `*` character, `&#42;` cannot replace
  492. `*` in emphasis delimiters, bullet list markers, or thematic
  493. breaks.
  494. Conforming CommonMark parsers need not store information about
  495. whether a particular character was represented in the source
  496. using a Unicode character or an entity reference.
  497. [Entity references](@) consist of `&` + any of the valid
  498. HTML5 entity names + `;`. The
  499. document <https://html.spec.whatwg.org/entities.json>
  500. is used as an authoritative source for the valid entity
  501. references and their corresponding code points.
  502. ```````````````````````````````` example
  503. &nbsp; &amp; &copy; &AElig; &Dcaron;
  504. &frac34; &HilbertSpace; &DifferentialD;
  505. &ClockwiseContourIntegral; &ngE;
  506. .
  507. <p>  &amp; © Æ Ď
  508. ¾ ℋ ⅆ
  509. ∲ ≧̸</p>
  510. ````````````````````````````````
  511. [Decimal numeric character
  512. references](@)
  513. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  514. numeric character reference is parsed as the corresponding
  515. Unicode character. Invalid Unicode code points will be replaced by
  516. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  517. the code point `U+0000` will also be replaced by `U+FFFD`.
  518. ```````````````````````````````` example
  519. &#35; &#1234; &#992; &#0;
  520. .
  521. <p># Ӓ Ϡ �</p>
  522. ````````````````````````````````
  523. [Hexadecimal numeric character
  524. references](@) consist of `&#` +
  525. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  526. They too are parsed as the corresponding Unicode character (this
  527. time specified with a hexadecimal numeral instead of decimal).
  528. ```````````````````````````````` example
  529. &#X22; &#XD06; &#xcab;
  530. .
  531. <p>&quot; ആ ಫ</p>
  532. ````````````````````````````````
  533. Here are some nonentities:
  534. ```````````````````````````````` example
  535. &nbsp &x; &#; &#x;
  536. &#87654321;
  537. &#abcdef0;
  538. &ThisIsNotDefined; &hi?;
  539. .
  540. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  541. &amp;#87654321;
  542. &amp;#abcdef0;
  543. &amp;ThisIsNotDefined; &amp;hi?;</p>
  544. ````````````````````````````````
  545. Although HTML5 does accept some entity references
  546. without a trailing semicolon (such as `&copy`), these are not
  547. recognized here, because it makes the grammar too ambiguous:
  548. ```````````````````````````````` example
  549. &copy
  550. .
  551. <p>&amp;copy</p>
  552. ````````````````````````````````
  553. Strings that are not on the list of HTML5 named entities are not
  554. recognized as entity references either:
  555. ```````````````````````````````` example
  556. &MadeUpEntity;
  557. .
  558. <p>&amp;MadeUpEntity;</p>
  559. ````````````````````````````````
  560. Entity and numeric character references are recognized in any
  561. context besides code spans or code blocks, including
  562. URLs, [link titles], and [fenced code block][] [info strings]:
  563. ```````````````````````````````` example
  564. <a href="&ouml;&ouml;.html">
  565. .
  566. <a href="&ouml;&ouml;.html">
  567. ````````````````````````````````
  568. ```````````````````````````````` example
  569. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  570. .
  571. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  572. ````````````````````````````````
  573. ```````````````````````````````` example
  574. [foo]
  575. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  576. .
  577. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  578. ````````````````````````````````
  579. ```````````````````````````````` example
  580. ``` f&ouml;&ouml;
  581. foo
  582. ```
  583. .
  584. <pre><code class="language-föö">foo
  585. </code></pre>
  586. ````````````````````````````````
  587. Entity and numeric character references are treated as literal
  588. text in code spans and code blocks:
  589. ```````````````````````````````` example
  590. `f&ouml;&ouml;`
  591. .
  592. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  593. ````````````````````````````````
  594. ```````````````````````````````` example
  595. f&ouml;f&ouml;
  596. .
  597. <pre><code>f&amp;ouml;f&amp;ouml;
  598. </code></pre>
  599. ````````````````````````````````
  600. Entity and numeric character references cannot be used
  601. in place of symbols indicating structure in CommonMark
  602. documents.
  603. ```````````````````````````````` example
  604. &#42;foo&#42;
  605. *foo*
  606. .
  607. <p>*foo*
  608. <em>foo</em></p>
  609. ````````````````````````````````
  610. ```````````````````````````````` example
  611. &#42; foo
  612. * foo
  613. .
  614. <p>* foo</p>
  615. <ul>
  616. <li>foo</li>
  617. </ul>
  618. ````````````````````````````````
  619. ```````````````````````````````` example
  620. foo&#10;&#10;bar
  621. .
  622. <p>foo
  623. bar</p>
  624. ````````````````````````````````
  625. ```````````````````````````````` example
  626. &#9;foo
  627. .
  628. <p>→foo</p>
  629. ````````````````````````````````
  630. ```````````````````````````````` example
  631. [a](url &quot;tit&quot;)
  632. .
  633. <p>[a](url &quot;tit&quot;)</p>
  634. ````````````````````````````````
  635. # Blocks and inlines
  636. We can think of a document as a sequence of
  637. [blocks](@)---structural elements like paragraphs, block
  638. quotations, lists, headings, rules, and code blocks. Some blocks (like
  639. block quotes and list items) contain other blocks; others (like
  640. headings and paragraphs) contain [inline](@) content---text,
  641. links, emphasized text, images, code spans, and so on.
  642. ## Precedence
  643. Indicators of block structure always take precedence over indicators
  644. of inline structure. So, for example, the following is a list with
  645. two items, not a list with one item containing a code span:
  646. ```````````````````````````````` example
  647. - `one
  648. - two`
  649. .
  650. <ul>
  651. <li>`one</li>
  652. <li>two`</li>
  653. </ul>
  654. ````````````````````````````````
  655. This means that parsing can proceed in two steps: first, the block
  656. structure of the document can be discerned; second, text lines inside
  657. paragraphs, headings, and other block constructs can be parsed for inline
  658. structure. The second step requires information about link reference
  659. definitions that will be available only at the end of the first
  660. step. Note that the first step requires processing lines in sequence,
  661. but the second can be parallelized, since the inline parsing of
  662. one block element does not affect the inline parsing of any other.
  663. ## Container blocks and leaf blocks
  664. We can divide blocks into two types:
  665. [container blocks](@),
  666. which can contain other blocks, and [leaf blocks](@),
  667. which cannot.
  668. # Leaf blocks
  669. This section describes the different kinds of leaf block that make up a
  670. Markdown document.
  671. ## Thematic breaks
  672. A line consisting of 0-3 spaces of indentation, followed by a sequence
  673. of three or more matching `-`, `_`, or `*` characters, each followed
  674. optionally by any number of spaces or tabs, forms a
  675. [thematic break](@).
  676. ```````````````````````````````` example
  677. ***
  678. ---
  679. ___
  680. .
  681. <hr />
  682. <hr />
  683. <hr />
  684. ````````````````````````````````
  685. Wrong characters:
  686. ```````````````````````````````` example
  687. +++
  688. .
  689. <p>+++</p>
  690. ````````````````````````````````
  691. ```````````````````````````````` example
  692. ===
  693. .
  694. <p>===</p>
  695. ````````````````````````````````
  696. Not enough characters:
  697. ```````````````````````````````` example
  698. --
  699. **
  700. __
  701. .
  702. <p>--
  703. **
  704. __</p>
  705. ````````````````````````````````
  706. One to three spaces indent are allowed:
  707. ```````````````````````````````` example
  708. ***
  709. ***
  710. ***
  711. .
  712. <hr />
  713. <hr />
  714. <hr />
  715. ````````````````````````````````
  716. Four spaces is too many:
  717. ```````````````````````````````` example
  718. ***
  719. .
  720. <pre><code>***
  721. </code></pre>
  722. ````````````````````````````````
  723. ```````````````````````````````` example
  724. Foo
  725. ***
  726. .
  727. <p>Foo
  728. ***</p>
  729. ````````````````````````````````
  730. More than three characters may be used:
  731. ```````````````````````````````` example
  732. _____________________________________
  733. .
  734. <hr />
  735. ````````````````````````````````
  736. Spaces are allowed between the characters:
  737. ```````````````````````````````` example
  738. - - -
  739. .
  740. <hr />
  741. ````````````````````````````````
  742. ```````````````````````````````` example
  743. ** * ** * ** * **
  744. .
  745. <hr />
  746. ````````````````````````````````
  747. ```````````````````````````````` example
  748. - - - -
  749. .
  750. <hr />
  751. ````````````````````````````````
  752. Spaces are allowed at the end:
  753. ```````````````````````````````` example
  754. - - - -
  755. .
  756. <hr />
  757. ````````````````````````````````
  758. However, no other characters may occur in the line:
  759. ```````````````````````````````` example
  760. _ _ _ _ a
  761. a------
  762. ---a---
  763. .
  764. <p>_ _ _ _ a</p>
  765. <p>a------</p>
  766. <p>---a---</p>
  767. ````````````````````````````````
  768. It is required that all of the [non-whitespace characters] be the same.
  769. So, this is not a thematic break:
  770. ```````````````````````````````` example
  771. *-*
  772. .
  773. <p><em>-</em></p>
  774. ````````````````````````````````
  775. Thematic breaks do not need blank lines before or after:
  776. ```````````````````````````````` example
  777. - foo
  778. ***
  779. - bar
  780. .
  781. <ul>
  782. <li>foo</li>
  783. </ul>
  784. <hr />
  785. <ul>
  786. <li>bar</li>
  787. </ul>
  788. ````````````````````````````````
  789. Thematic breaks can interrupt a paragraph:
  790. ```````````````````````````````` example
  791. Foo
  792. ***
  793. bar
  794. .
  795. <p>Foo</p>
  796. <hr />
  797. <p>bar</p>
  798. ````````````````````````````````
  799. If a line of dashes that meets the above conditions for being a
  800. thematic break could also be interpreted as the underline of a [setext
  801. heading], the interpretation as a
  802. [setext heading] takes precedence. Thus, for example,
  803. this is a setext heading, not a paragraph followed by a thematic break:
  804. ```````````````````````````````` example
  805. Foo
  806. ---
  807. bar
  808. .
  809. <h2>Foo</h2>
  810. <p>bar</p>
  811. ````````````````````````````````
  812. When both a thematic break and a list item are possible
  813. interpretations of a line, the thematic break takes precedence:
  814. ```````````````````````````````` example
  815. * Foo
  816. * * *
  817. * Bar
  818. .
  819. <ul>
  820. <li>Foo</li>
  821. </ul>
  822. <hr />
  823. <ul>
  824. <li>Bar</li>
  825. </ul>
  826. ````````````````````````````````
  827. If you want a thematic break in a list item, use a different bullet:
  828. ```````````````````````````````` example
  829. - Foo
  830. - * * *
  831. .
  832. <ul>
  833. <li>Foo</li>
  834. <li>
  835. <hr />
  836. </li>
  837. </ul>
  838. ````````````````````````````````
  839. ## ATX headings
  840. An [ATX heading](@)
  841. consists of a string of characters, parsed as inline content, between an
  842. opening sequence of 1--6 unescaped `#` characters and an optional
  843. closing sequence of any number of unescaped `#` characters.
  844. The opening sequence of `#` characters must be followed by a
  845. [space] or by the end of line. The optional closing sequence of `#`s must be
  846. preceded by a [space] and may be followed by spaces only. The opening
  847. `#` character may be indented 0-3 spaces. The raw contents of the
  848. heading are stripped of leading and trailing spaces before being parsed
  849. as inline content. The heading level is equal to the number of `#`
  850. characters in the opening sequence.
  851. Simple headings:
  852. ```````````````````````````````` example
  853. # foo
  854. ## foo
  855. ### foo
  856. #### foo
  857. ##### foo
  858. ###### foo
  859. .
  860. <h1>foo</h1>
  861. <h2>foo</h2>
  862. <h3>foo</h3>
  863. <h4>foo</h4>
  864. <h5>foo</h5>
  865. <h6>foo</h6>
  866. ````````````````````````````````
  867. More than six `#` characters is not a heading:
  868. ```````````````````````````````` example
  869. ####### foo
  870. .
  871. <p>####### foo</p>
  872. ````````````````````````````````
  873. At least one space is required between the `#` characters and the
  874. heading's contents, unless the heading is empty. Note that many
  875. implementations currently do not require the space. However, the
  876. space was required by the
  877. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  878. and it helps prevent things like the following from being parsed as
  879. headings:
  880. ```````````````````````````````` example
  881. #5 bolt
  882. #hashtag
  883. .
  884. <p>#5 bolt</p>
  885. <p>#hashtag</p>
  886. ````````````````````````````````
  887. This is not a heading, because the first `#` is escaped:
  888. ```````````````````````````````` example
  889. \## foo
  890. .
  891. <p>## foo</p>
  892. ````````````````````````````````
  893. Contents are parsed as inlines:
  894. ```````````````````````````````` example
  895. # foo *bar* \*baz\*
  896. .
  897. <h1>foo <em>bar</em> *baz*</h1>
  898. ````````````````````````````````
  899. Leading and trailing [whitespace] is ignored in parsing inline content:
  900. ```````````````````````````````` example
  901. # foo
  902. .
  903. <h1>foo</h1>
  904. ````````````````````````````````
  905. One to three spaces indentation are allowed:
  906. ```````````````````````````````` example
  907. ### foo
  908. ## foo
  909. # foo
  910. .
  911. <h3>foo</h3>
  912. <h2>foo</h2>
  913. <h1>foo</h1>
  914. ````````````````````````````````
  915. Four spaces are too much:
  916. ```````````````````````````````` example
  917. # foo
  918. .
  919. <pre><code># foo
  920. </code></pre>
  921. ````````````````````````````````
  922. ```````````````````````````````` example
  923. foo
  924. # bar
  925. .
  926. <p>foo
  927. # bar</p>
  928. ````````````````````````````````
  929. A closing sequence of `#` characters is optional:
  930. ```````````````````````````````` example
  931. ## foo ##
  932. ### bar ###
  933. .
  934. <h2>foo</h2>
  935. <h3>bar</h3>
  936. ````````````````````````````````
  937. It need not be the same length as the opening sequence:
  938. ```````````````````````````````` example
  939. # foo ##################################
  940. ##### foo ##
  941. .
  942. <h1>foo</h1>
  943. <h5>foo</h5>
  944. ````````````````````````````````
  945. Spaces are allowed after the closing sequence:
  946. ```````````````````````````````` example
  947. ### foo ###
  948. .
  949. <h3>foo</h3>
  950. ````````````````````````````````
  951. A sequence of `#` characters with anything but [spaces] following it
  952. is not a closing sequence, but counts as part of the contents of the
  953. heading:
  954. ```````````````````````````````` example
  955. ### foo ### b
  956. .
  957. <h3>foo ### b</h3>
  958. ````````````````````````````````
  959. The closing sequence must be preceded by a space:
  960. ```````````````````````````````` example
  961. # foo#
  962. .
  963. <h1>foo#</h1>
  964. ````````````````````````````````
  965. Backslash-escaped `#` characters do not count as part
  966. of the closing sequence:
  967. ```````````````````````````````` example
  968. ### foo \###
  969. ## foo #\##
  970. # foo \#
  971. .
  972. <h3>foo ###</h3>
  973. <h2>foo ###</h2>
  974. <h1>foo #</h1>
  975. ````````````````````````````````
  976. ATX headings need not be separated from surrounding content by blank
  977. lines, and they can interrupt paragraphs:
  978. ```````````````````````````````` example
  979. ****
  980. ## foo
  981. ****
  982. .
  983. <hr />
  984. <h2>foo</h2>
  985. <hr />
  986. ````````````````````````````````
  987. ```````````````````````````````` example
  988. Foo bar
  989. # baz
  990. Bar foo
  991. .
  992. <p>Foo bar</p>
  993. <h1>baz</h1>
  994. <p>Bar foo</p>
  995. ````````````````````````````````
  996. ATX headings can be empty:
  997. ```````````````````````````````` example
  998. ##
  999. #
  1000. ### ###
  1001. .
  1002. <h2></h2>
  1003. <h1></h1>
  1004. <h3></h3>
  1005. ````````````````````````````````
  1006. ## Setext headings
  1007. A [setext heading](@) consists of one or more
  1008. lines of text, each containing at least one [non-whitespace
  1009. character], with no more than 3 spaces indentation, followed by
  1010. a [setext heading underline]. The lines of text must be such
  1011. that, were they not followed by the setext heading underline,
  1012. they would be interpreted as a paragraph: they cannot be
  1013. interpretable as a [code fence], [ATX heading][ATX headings],
  1014. [block quote][block quotes], [thematic break][thematic breaks],
  1015. [list item][list items], or [HTML block][HTML blocks].
  1016. A [setext heading underline](@) is a sequence of
  1017. `=` characters or a sequence of `-` characters, with no more than 3
  1018. spaces indentation and any number of trailing spaces. If a line
  1019. containing a single `-` can be interpreted as an
  1020. empty [list items], it should be interpreted this way
  1021. and not as a [setext heading underline].
  1022. The heading is a level 1 heading if `=` characters are used in
  1023. the [setext heading underline], and a level 2 heading if `-`
  1024. characters are used. The contents of the heading are the result
  1025. of parsing the preceding lines of text as CommonMark inline
  1026. content.
  1027. In general, a setext heading need not be preceded or followed by a
  1028. blank line. However, it cannot interrupt a paragraph, so when a
  1029. setext heading comes after a paragraph, a blank line is needed between
  1030. them.
  1031. Simple examples:
  1032. ```````````````````````````````` example
  1033. Foo *bar*
  1034. =========
  1035. Foo *bar*
  1036. ---------
  1037. .
  1038. <h1>Foo <em>bar</em></h1>
  1039. <h2>Foo <em>bar</em></h2>
  1040. ````````````````````````````````
  1041. The content of the header may span more than one line:
  1042. ```````````````````````````````` example
  1043. Foo *bar
  1044. baz*
  1045. ====
  1046. .
  1047. <h1>Foo <em>bar
  1048. baz</em></h1>
  1049. ````````````````````````````````
  1050. The contents are the result of parsing the headings's raw
  1051. content as inlines. The heading's raw content is formed by
  1052. concatenating the lines and removing initial and final
  1053. [whitespace].
  1054. ```````````````````````````````` example
  1055. Foo *bar
  1056. baz*→
  1057. ====
  1058. .
  1059. <h1>Foo <em>bar
  1060. baz</em></h1>
  1061. ````````````````````````````````
  1062. The underlining can be any length:
  1063. ```````````````````````````````` example
  1064. Foo
  1065. -------------------------
  1066. Foo
  1067. =
  1068. .
  1069. <h2>Foo</h2>
  1070. <h1>Foo</h1>
  1071. ````````````````````````````````
  1072. The heading content can be indented up to three spaces, and need
  1073. not line up with the underlining:
  1074. ```````````````````````````````` example
  1075. Foo
  1076. ---
  1077. Foo
  1078. -----
  1079. Foo
  1080. ===
  1081. .
  1082. <h2>Foo</h2>
  1083. <h2>Foo</h2>
  1084. <h1>Foo</h1>
  1085. ````````````````````````````````
  1086. Four spaces indent is too much:
  1087. ```````````````````````````````` example
  1088. Foo
  1089. ---
  1090. Foo
  1091. ---
  1092. .
  1093. <pre><code>Foo
  1094. ---
  1095. Foo
  1096. </code></pre>
  1097. <hr />
  1098. ````````````````````````````````
  1099. The setext heading underline can be indented up to three spaces, and
  1100. may have trailing spaces:
  1101. ```````````````````````````````` example
  1102. Foo
  1103. ----
  1104. .
  1105. <h2>Foo</h2>
  1106. ````````````````````````````````
  1107. Four spaces is too much:
  1108. ```````````````````````````````` example
  1109. Foo
  1110. ---
  1111. .
  1112. <p>Foo
  1113. ---</p>
  1114. ````````````````````````````````
  1115. The setext heading underline cannot contain internal spaces:
  1116. ```````````````````````````````` example
  1117. Foo
  1118. = =
  1119. Foo
  1120. --- -
  1121. .
  1122. <p>Foo
  1123. = =</p>
  1124. <p>Foo</p>
  1125. <hr />
  1126. ````````````````````````````````
  1127. Trailing spaces in the content line do not cause a line break:
  1128. ```````````````````````````````` example
  1129. Foo
  1130. -----
  1131. .
  1132. <h2>Foo</h2>
  1133. ````````````````````````````````
  1134. Nor does a backslash at the end:
  1135. ```````````````````````````````` example
  1136. Foo\
  1137. ----
  1138. .
  1139. <h2>Foo\</h2>
  1140. ````````````````````````````````
  1141. Since indicators of block structure take precedence over
  1142. indicators of inline structure, the following are setext headings:
  1143. ```````````````````````````````` example
  1144. `Foo
  1145. ----
  1146. `
  1147. <a title="a lot
  1148. ---
  1149. of dashes"/>
  1150. .
  1151. <h2>`Foo</h2>
  1152. <p>`</p>
  1153. <h2>&lt;a title=&quot;a lot</h2>
  1154. <p>of dashes&quot;/&gt;</p>
  1155. ````````````````````````````````
  1156. The setext heading underline cannot be a [lazy continuation
  1157. line] in a list item or block quote:
  1158. ```````````````````````````````` example
  1159. > Foo
  1160. ---
  1161. .
  1162. <blockquote>
  1163. <p>Foo</p>
  1164. </blockquote>
  1165. <hr />
  1166. ````````````````````````````````
  1167. ```````````````````````````````` example
  1168. > foo
  1169. bar
  1170. ===
  1171. .
  1172. <blockquote>
  1173. <p>foo
  1174. bar
  1175. ===</p>
  1176. </blockquote>
  1177. ````````````````````````````````
  1178. ```````````````````````````````` example
  1179. - Foo
  1180. ---
  1181. .
  1182. <ul>
  1183. <li>Foo</li>
  1184. </ul>
  1185. <hr />
  1186. ````````````````````````````````
  1187. A blank line is needed between a paragraph and a following
  1188. setext heading, since otherwise the paragraph becomes part
  1189. of the heading's content:
  1190. ```````````````````````````````` example
  1191. Foo
  1192. Bar
  1193. ---
  1194. .
  1195. <h2>Foo
  1196. Bar</h2>
  1197. ````````````````````````````````
  1198. But in general a blank line is not required before or after
  1199. setext headings:
  1200. ```````````````````````````````` example
  1201. ---
  1202. Foo
  1203. ---
  1204. Bar
  1205. ---
  1206. Baz
  1207. .
  1208. <hr />
  1209. <h2>Foo</h2>
  1210. <h2>Bar</h2>
  1211. <p>Baz</p>
  1212. ````````````````````````````````
  1213. Setext headings cannot be empty:
  1214. ```````````````````````````````` example
  1215. ====
  1216. .
  1217. <p>====</p>
  1218. ````````````````````````````````
  1219. Setext heading text lines must not be interpretable as block
  1220. constructs other than paragraphs. So, the line of dashes
  1221. in these examples gets interpreted as a thematic break:
  1222. ```````````````````````````````` example
  1223. ---
  1224. ---
  1225. .
  1226. <hr />
  1227. <hr />
  1228. ````````````````````````````````
  1229. ```````````````````````````````` example
  1230. - foo
  1231. -----
  1232. .
  1233. <ul>
  1234. <li>foo</li>
  1235. </ul>
  1236. <hr />
  1237. ````````````````````````````````
  1238. ```````````````````````````````` example
  1239. foo
  1240. ---
  1241. .
  1242. <pre><code>foo
  1243. </code></pre>
  1244. <hr />
  1245. ````````````````````````````````
  1246. ```````````````````````````````` example
  1247. > foo
  1248. -----
  1249. .
  1250. <blockquote>
  1251. <p>foo</p>
  1252. </blockquote>
  1253. <hr />
  1254. ````````````````````````````````
  1255. If you want a heading with `> foo` as its literal text, you can
  1256. use backslash escapes:
  1257. ```````````````````````````````` example
  1258. \> foo
  1259. ------
  1260. .
  1261. <h2>&gt; foo</h2>
  1262. ````````````````````````````````
  1263. **Compatibility note:** Most existing Markdown implementations
  1264. do not allow the text of setext headings to span multiple lines.
  1265. But there is no consensus about how to interpret
  1266. ``` markdown
  1267. Foo
  1268. bar
  1269. ---
  1270. baz
  1271. ```
  1272. One can find four different interpretations:
  1273. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1274. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1275. 3. paragraph "Foo bar --- baz"
  1276. 4. heading "Foo bar", paragraph "baz"
  1277. We find interpretation 4 most natural, and interpretation 4
  1278. increases the expressive power of CommonMark, by allowing
  1279. multiline headings. Authors who want interpretation 1 can
  1280. put a blank line after the first paragraph:
  1281. ```````````````````````````````` example
  1282. Foo
  1283. bar
  1284. ---
  1285. baz
  1286. .
  1287. <p>Foo</p>
  1288. <h2>bar</h2>
  1289. <p>baz</p>
  1290. ````````````````````````````````
  1291. Authors who want interpretation 2 can put blank lines around
  1292. the thematic break,
  1293. ```````````````````````````````` example
  1294. Foo
  1295. bar
  1296. ---
  1297. baz
  1298. .
  1299. <p>Foo
  1300. bar</p>
  1301. <hr />
  1302. <p>baz</p>
  1303. ````````````````````````````````
  1304. or use a thematic break that cannot count as a [setext heading
  1305. underline], such as
  1306. ```````````````````````````````` example
  1307. Foo
  1308. bar
  1309. * * *
  1310. baz
  1311. .
  1312. <p>Foo
  1313. bar</p>
  1314. <hr />
  1315. <p>baz</p>
  1316. ````````````````````````````````
  1317. Authors who want interpretation 3 can use backslash escapes:
  1318. ```````````````````````````````` example
  1319. Foo
  1320. bar
  1321. \---
  1322. baz
  1323. .
  1324. <p>Foo
  1325. bar
  1326. ---
  1327. baz</p>
  1328. ````````````````````````````````
  1329. ## Indented code blocks
  1330. An [indented code block](@) is composed of one or more
  1331. [indented chunks] separated by blank lines.
  1332. An [indented chunk](@) is a sequence of non-blank lines,
  1333. each indented four or more spaces. The contents of the code block are
  1334. the literal contents of the lines, including trailing
  1335. [line endings], minus four spaces of indentation.
  1336. An indented code block has no [info string].
  1337. An indented code block cannot interrupt a paragraph, so there must be
  1338. a blank line between a paragraph and a following indented code block.
  1339. (A blank line is not needed, however, between a code block and a following
  1340. paragraph.)
  1341. ```````````````````````````````` example
  1342. a simple
  1343. indented code block
  1344. .
  1345. <pre><code>a simple
  1346. indented code block
  1347. </code></pre>
  1348. ````````````````````````````````
  1349. If there is any ambiguity between an interpretation of indentation
  1350. as a code block and as indicating that material belongs to a [list
  1351. item][list items], the list item interpretation takes precedence:
  1352. ```````````````````````````````` example
  1353. - foo
  1354. bar
  1355. .
  1356. <ul>
  1357. <li>
  1358. <p>foo</p>
  1359. <p>bar</p>
  1360. </li>
  1361. </ul>
  1362. ````````````````````````````````
  1363. ```````````````````````````````` example
  1364. 1. foo
  1365. - bar
  1366. .
  1367. <ol>
  1368. <li>
  1369. <p>foo</p>
  1370. <ul>
  1371. <li>bar</li>
  1372. </ul>
  1373. </li>
  1374. </ol>
  1375. ````````````````````````````````
  1376. The contents of a code block are literal text, and do not get parsed
  1377. as Markdown:
  1378. ```````````````````````````````` example
  1379. <a/>
  1380. *hi*
  1381. - one
  1382. .
  1383. <pre><code>&lt;a/&gt;
  1384. *hi*
  1385. - one
  1386. </code></pre>
  1387. ````````````````````````````````
  1388. Here we have three chunks separated by blank lines:
  1389. ```````````````````````````````` example
  1390. chunk1
  1391. chunk2
  1392. chunk3
  1393. .
  1394. <pre><code>chunk1
  1395. chunk2
  1396. chunk3
  1397. </code></pre>
  1398. ````````````````````````````````
  1399. Any initial spaces beyond four will be included in the content, even
  1400. in interior blank lines:
  1401. ```````````````````````````````` example
  1402. chunk1
  1403. chunk2
  1404. .
  1405. <pre><code>chunk1
  1406. chunk2
  1407. </code></pre>
  1408. ````````````````````````````````
  1409. An indented code block cannot interrupt a paragraph. (This
  1410. allows hanging indents and the like.)
  1411. ```````````````````````````````` example
  1412. Foo
  1413. bar
  1414. .
  1415. <p>Foo
  1416. bar</p>
  1417. ````````````````````````````````
  1418. However, any non-blank line with fewer than four leading spaces ends
  1419. the code block immediately. So a paragraph may occur immediately
  1420. after indented code:
  1421. ```````````````````````````````` example
  1422. foo
  1423. bar
  1424. .
  1425. <pre><code>foo
  1426. </code></pre>
  1427. <p>bar</p>
  1428. ````````````````````````````````
  1429. And indented code can occur immediately before and after other kinds of
  1430. blocks:
  1431. ```````````````````````````````` example
  1432. # Heading
  1433. foo
  1434. Heading
  1435. ------
  1436. foo
  1437. ----
  1438. .
  1439. <h1>Heading</h1>
  1440. <pre><code>foo
  1441. </code></pre>
  1442. <h2>Heading</h2>
  1443. <pre><code>foo
  1444. </code></pre>
  1445. <hr />
  1446. ````````````````````````````````
  1447. The first line can be indented more than four spaces:
  1448. ```````````````````````````````` example
  1449. foo
  1450. bar
  1451. .
  1452. <pre><code> foo
  1453. bar
  1454. </code></pre>
  1455. ````````````````````````````````
  1456. Blank lines preceding or following an indented code block
  1457. are not included in it:
  1458. ```````````````````````````````` example
  1459. foo
  1460. .
  1461. <pre><code>foo
  1462. </code></pre>
  1463. ````````````````````````````````
  1464. Trailing spaces are included in the code block's content:
  1465. ```````````````````````````````` example
  1466. foo
  1467. .
  1468. <pre><code>foo
  1469. </code></pre>
  1470. ````````````````````````````````
  1471. ## Fenced code blocks
  1472. A [code fence](@) is a sequence
  1473. of at least three consecutive backtick characters (`` ` ``) or
  1474. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1475. A [fenced code block](@)
  1476. begins with a code fence, indented no more than three spaces.
  1477. The line with the opening code fence may optionally contain some text
  1478. following the code fence; this is trimmed of leading and trailing
  1479. whitespace and called the [info string](@). If the [info string] comes
  1480. after a backtick fence, it may not contain any backtick
  1481. characters. (The reason for this restriction is that otherwise
  1482. some inline code would be incorrectly interpreted as the
  1483. beginning of a fenced code block.)
  1484. The content of the code block consists of all subsequent lines, until
  1485. a closing [code fence] of the same type as the code block
  1486. began with (backticks or tildes), and with at least as many backticks
  1487. or tildes as the opening code fence. If the leading code fence is
  1488. indented N spaces, then up to N spaces of indentation are removed from
  1489. each line of the content (if present). (If a content line is not
  1490. indented, it is preserved unchanged. If it is indented less than N
  1491. spaces, all of the indentation is removed.)
  1492. The closing code fence may be indented up to three spaces, and may be
  1493. followed only by spaces, which are ignored. If the end of the
  1494. containing block (or document) is reached and no closing code fence
  1495. has been found, the code block contains all of the lines after the
  1496. opening code fence until the end of the containing block (or
  1497. document). (An alternative spec would require backtracking in the
  1498. event that a closing code fence is not found. But this makes parsing
  1499. much less efficient, and there seems to be no real down side to the
  1500. behavior described here.)
  1501. A fenced code block may interrupt a paragraph, and does not require
  1502. a blank line either before or after.
  1503. The content of a code fence is treated as literal text, not parsed
  1504. as inlines. The first word of the [info string] is typically used to
  1505. specify the language of the code sample, and rendered in the `class`
  1506. attribute of the `code` tag. However, this spec does not mandate any
  1507. particular treatment of the [info string].
  1508. Here is a simple example with backticks:
  1509. ```````````````````````````````` example
  1510. ```
  1511. <
  1512. >
  1513. ```
  1514. .
  1515. <pre><code>&lt;
  1516. &gt;
  1517. </code></pre>
  1518. ````````````````````````````````
  1519. With tildes:
  1520. ```````````````````````````````` example
  1521. ~~~
  1522. <
  1523. >
  1524. ~~~
  1525. .
  1526. <pre><code>&lt;
  1527. &gt;
  1528. </code></pre>
  1529. ````````````````````````````````
  1530. Fewer than three backticks is not enough:
  1531. ```````````````````````````````` example
  1532. ``
  1533. foo
  1534. ``
  1535. .
  1536. <p><code>foo</code></p>
  1537. ````````````````````````````````
  1538. The closing code fence must use the same character as the opening
  1539. fence:
  1540. ```````````````````````````````` example
  1541. ```
  1542. aaa
  1543. ~~~
  1544. ```
  1545. .
  1546. <pre><code>aaa
  1547. ~~~
  1548. </code></pre>
  1549. ````````````````````````````````
  1550. ```````````````````````````````` example
  1551. ~~~
  1552. aaa
  1553. ```
  1554. ~~~
  1555. .
  1556. <pre><code>aaa
  1557. ```
  1558. </code></pre>
  1559. ````````````````````````````````
  1560. The closing code fence must be at least as long as the opening fence:
  1561. ```````````````````````````````` example
  1562. ````
  1563. aaa
  1564. ```
  1565. ``````
  1566. .
  1567. <pre><code>aaa
  1568. ```
  1569. </code></pre>
  1570. ````````````````````````````````
  1571. ```````````````````````````````` example
  1572. ~~~~
  1573. aaa
  1574. ~~~
  1575. ~~~~
  1576. .
  1577. <pre><code>aaa
  1578. ~~~
  1579. </code></pre>
  1580. ````````````````````````````````
  1581. Unclosed code blocks are closed by the end of the document
  1582. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1583. ```````````````````````````````` example
  1584. ```
  1585. .
  1586. <pre><code></code></pre>
  1587. ````````````````````````````````
  1588. ```````````````````````````````` example
  1589. `````
  1590. ```
  1591. aaa
  1592. .
  1593. <pre><code>
  1594. ```
  1595. aaa
  1596. </code></pre>
  1597. ````````````````````````````````
  1598. ```````````````````````````````` example
  1599. > ```
  1600. > aaa
  1601. bbb
  1602. .
  1603. <blockquote>
  1604. <pre><code>aaa
  1605. </code></pre>
  1606. </blockquote>
  1607. <p>bbb</p>
  1608. ````````````````````````````````
  1609. A code block can have all empty lines as its content:
  1610. ```````````````````````````````` example
  1611. ```
  1612. ```
  1613. .
  1614. <pre><code>
  1615. </code></pre>
  1616. ````````````````````````````````
  1617. A code block can be empty:
  1618. ```````````````````````````````` example
  1619. ```
  1620. ```
  1621. .
  1622. <pre><code></code></pre>
  1623. ````````````````````````````````
  1624. Fences can be indented. If the opening fence is indented,
  1625. content lines will have equivalent opening indentation removed,
  1626. if present:
  1627. ```````````````````````````````` example
  1628. ```
  1629. aaa
  1630. aaa
  1631. ```
  1632. .
  1633. <pre><code>aaa
  1634. aaa
  1635. </code></pre>
  1636. ````````````````````````````````
  1637. ```````````````````````````````` example
  1638. ```
  1639. aaa
  1640. aaa
  1641. aaa
  1642. ```
  1643. .
  1644. <pre><code>aaa
  1645. aaa
  1646. aaa
  1647. </code></pre>
  1648. ````````````````````````````````
  1649. ```````````````````````````````` example
  1650. ```
  1651. aaa
  1652. aaa
  1653. aaa
  1654. ```
  1655. .
  1656. <pre><code>aaa
  1657. aaa
  1658. aaa
  1659. </code></pre>
  1660. ````````````````````````````````
  1661. Four spaces indentation produces an indented code block:
  1662. ```````````````````````````````` example
  1663. ```
  1664. aaa
  1665. ```
  1666. .
  1667. <pre><code>```
  1668. aaa
  1669. ```
  1670. </code></pre>
  1671. ````````````````````````````````
  1672. Closing fences may be indented by 0-3 spaces, and their indentation
  1673. need not match that of the opening fence:
  1674. ```````````````````````````````` example
  1675. ```
  1676. aaa
  1677. ```
  1678. .
  1679. <pre><code>aaa
  1680. </code></pre>
  1681. ````````````````````````````````
  1682. ```````````````````````````````` example
  1683. ```
  1684. aaa
  1685. ```
  1686. .
  1687. <pre><code>aaa
  1688. </code></pre>
  1689. ````````````````````````````````
  1690. This is not a closing fence, because it is indented 4 spaces:
  1691. ```````````````````````````````` example
  1692. ```
  1693. aaa
  1694. ```
  1695. .
  1696. <pre><code>aaa
  1697. ```
  1698. </code></pre>
  1699. ````````````````````````````````
  1700. Code fences (opening and closing) cannot contain internal spaces:
  1701. ```````````````````````````````` example
  1702. ``` ```
  1703. aaa
  1704. .
  1705. <p><code> </code>
  1706. aaa</p>
  1707. ````````````````````````````````
  1708. ```````````````````````````````` example
  1709. ~~~~~~
  1710. aaa
  1711. ~~~ ~~
  1712. .
  1713. <pre><code>aaa
  1714. ~~~ ~~
  1715. </code></pre>
  1716. ````````````````````````````````
  1717. Fenced code blocks can interrupt paragraphs, and can be followed
  1718. directly by paragraphs, without a blank line between:
  1719. ```````````````````````````````` example
  1720. foo
  1721. ```
  1722. bar
  1723. ```
  1724. baz
  1725. .
  1726. <p>foo</p>
  1727. <pre><code>bar
  1728. </code></pre>
  1729. <p>baz</p>
  1730. ````````````````````````````````
  1731. Other blocks can also occur before and after fenced code blocks
  1732. without an intervening blank line:
  1733. ```````````````````````````````` example
  1734. foo
  1735. ---
  1736. ~~~
  1737. bar
  1738. ~~~
  1739. # baz
  1740. .
  1741. <h2>foo</h2>
  1742. <pre><code>bar
  1743. </code></pre>
  1744. <h1>baz</h1>
  1745. ````````````````````````````````
  1746. An [info string] can be provided after the opening code fence.
  1747. Although this spec doesn't mandate any particular treatment of
  1748. the info string, the first word is typically used to specify
  1749. the language of the code block. In HTML output, the language is
  1750. normally indicated by adding a class to the `code` element consisting
  1751. of `language-` followed by the language name.
  1752. ```````````````````````````````` example
  1753. ```ruby
  1754. def foo(x)
  1755. return 3
  1756. end
  1757. ```
  1758. .
  1759. <pre><code class="language-ruby">def foo(x)
  1760. return 3
  1761. end
  1762. </code></pre>
  1763. ````````````````````````````````
  1764. ```````````````````````````````` example
  1765. ~~~~ ruby startline=3 $%@#$
  1766. def foo(x)
  1767. return 3
  1768. end
  1769. ~~~~~~~
  1770. .
  1771. <pre><code class="language-ruby">def foo(x)
  1772. return 3
  1773. end
  1774. </code></pre>
  1775. ````````````````````````````````
  1776. ```````````````````````````````` example
  1777. ````;
  1778. ````
  1779. .
  1780. <pre><code class="language-;"></code></pre>
  1781. ````````````````````````````````
  1782. [Info strings] for backtick code blocks cannot contain backticks:
  1783. ```````````````````````````````` example
  1784. ``` aa ```
  1785. foo
  1786. .
  1787. <p><code>aa</code>
  1788. foo</p>
  1789. ````````````````````````````````
  1790. [Info strings] for tilde code blocks can contain backticks and tildes:
  1791. ```````````````````````````````` example
  1792. ~~~ aa ``` ~~~
  1793. foo
  1794. ~~~
  1795. .
  1796. <pre><code class="language-aa">foo
  1797. </code></pre>
  1798. ````````````````````````````````
  1799. Closing code fences cannot have [info strings]:
  1800. ```````````````````````````````` example
  1801. ```
  1802. ``` aaa
  1803. ```
  1804. .
  1805. <pre><code>``` aaa
  1806. </code></pre>
  1807. ````````````````````````````````
  1808. ## HTML blocks
  1809. An [HTML block](@) is a group of lines that is treated
  1810. as raw HTML (and will not be escaped in HTML output).
  1811. There are seven kinds of [HTML block], which can be defined by their
  1812. start and end conditions. The block begins with a line that meets a
  1813. [start condition](@) (after up to three spaces optional indentation).
  1814. It ends with the first subsequent line that meets a matching [end
  1815. condition](@), or the last line of the document, or the last line of
  1816. the [container block](#container-blocks) containing the current HTML
  1817. block, if no line is encountered that meets the [end condition]. If
  1818. the first line meets both the [start condition] and the [end
  1819. condition], the block will contain just that line.
  1820. 1. **Start condition:** line begins with the string `<script`,
  1821. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1822. the string `>`, or the end of the line.\
  1823. **End condition:** line contains an end tag
  1824. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1825. need not match the start tag).
  1826. 2. **Start condition:** line begins with the string `<!--`.\
  1827. **End condition:** line contains the string `-->`.
  1828. 3. **Start condition:** line begins with the string `<?`.\
  1829. **End condition:** line contains the string `?>`.
  1830. 4. **Start condition:** line begins with the string `<!`
  1831. followed by an ASCII letter.\
  1832. **End condition:** line contains the character `>`.
  1833. 5. **Start condition:** line begins with the string
  1834. `<![CDATA[`.\
  1835. **End condition:** line contains the string `]]>`.
  1836. 6. **Start condition:** line begins the string `<` or `</`
  1837. followed by one of the strings (case-insensitive) `address`,
  1838. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1839. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1840. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1841. `footer`, `form`, `frame`, `frameset`,
  1842. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1843. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1844. `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1845. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1846. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1847. by [whitespace], the end of the line, the string `>`, or
  1848. the string `/>`.\
  1849. **End condition:** line is followed by a [blank line].
  1850. 7. **Start condition:** line begins with a complete [open tag]
  1851. (with any [tag name] other than `script`,
  1852. `style`, or `pre`) or a complete [closing tag],
  1853. followed only by [whitespace] or the end of the line.\
  1854. **End condition:** line is followed by a [blank line].
  1855. HTML blocks continue until they are closed by their appropriate
  1856. [end condition], or the last line of the document or other [container
  1857. block](#container-blocks). This means any HTML **within an HTML
  1858. block** that might otherwise be recognised as a start condition will
  1859. be ignored by the parser and passed through as-is, without changing
  1860. the parser's state.
  1861. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1862. the parser state; as the HTML block was started in by start condition 6, it
  1863. will end at any blank line. This can be surprising:
  1864. ```````````````````````````````` example
  1865. <table><tr><td>
  1866. <pre>
  1867. **Hello**,
  1868. _world_.
  1869. </pre>
  1870. </td></tr></table>
  1871. .
  1872. <table><tr><td>
  1873. <pre>
  1874. **Hello**,
  1875. <p><em>world</em>.
  1876. </pre></p>
  1877. </td></tr></table>
  1878. ````````````````````````````````
  1879. In this case, the HTML block is terminated by the newline — the `**Hello**`
  1880. text remains verbatim — and regular parsing resumes, with a paragraph,
  1881. emphasised `world` and inline and block HTML following.
  1882. All types of [HTML blocks] except type 7 may interrupt
  1883. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1884. (This restriction is intended to prevent unwanted interpretation
  1885. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1886. Some simple examples follow. Here are some basic HTML blocks
  1887. of type 6:
  1888. ```````````````````````````````` example
  1889. <table>
  1890. <tr>
  1891. <td>
  1892. hi
  1893. </td>
  1894. </tr>
  1895. </table>
  1896. okay.
  1897. .
  1898. <table>
  1899. <tr>
  1900. <td>
  1901. hi
  1902. </td>
  1903. </tr>
  1904. </table>
  1905. <p>okay.</p>
  1906. ````````````````````````````````
  1907. ```````````````````````````````` example
  1908. <div>
  1909. *hello*
  1910. <foo><a>
  1911. .
  1912. <div>
  1913. *hello*
  1914. <foo><a>
  1915. ````````````````````````````````
  1916. A block can also start with a closing tag:
  1917. ```````````````````````````````` example
  1918. </div>
  1919. *foo*
  1920. .
  1921. </div>
  1922. *foo*
  1923. ````````````````````````````````
  1924. Here we have two HTML blocks with a Markdown paragraph between them:
  1925. ```````````````````````````````` example
  1926. <DIV CLASS="foo">
  1927. *Markdown*
  1928. </DIV>
  1929. .
  1930. <DIV CLASS="foo">
  1931. <p><em>Markdown</em></p>
  1932. </DIV>
  1933. ````````````````````````````````
  1934. The tag on the first line can be partial, as long
  1935. as it is split where there would be whitespace:
  1936. ```````````````````````````````` example
  1937. <div id="foo"
  1938. class="bar">
  1939. </div>
  1940. .
  1941. <div id="foo"
  1942. class="bar">
  1943. </div>
  1944. ````````````````````````````````
  1945. ```````````````````````````````` example
  1946. <div id="foo" class="bar
  1947. baz">
  1948. </div>
  1949. .
  1950. <div id="foo" class="bar
  1951. baz">
  1952. </div>
  1953. ````````````````````````````````
  1954. An open tag need not be closed:
  1955. ```````````````````````````````` example
  1956. <div>
  1957. *foo*
  1958. *bar*
  1959. .
  1960. <div>
  1961. *foo*
  1962. <p><em>bar</em></p>
  1963. ````````````````````````````````
  1964. A partial tag need not even be completed (garbage
  1965. in, garbage out):
  1966. ```````````````````````````````` example
  1967. <div id="foo"
  1968. *hi*
  1969. .
  1970. <div id="foo"
  1971. *hi*
  1972. ````````````````````````````````
  1973. ```````````````````````````````` example
  1974. <div class
  1975. foo
  1976. .
  1977. <div class
  1978. foo
  1979. ````````````````````````````````
  1980. The initial tag doesn't even need to be a valid
  1981. tag, as long as it starts like one:
  1982. ```````````````````````````````` example
  1983. <div *???-&&&-<---
  1984. *foo*
  1985. .
  1986. <div *???-&&&-<---
  1987. *foo*
  1988. ````````````````````````````````
  1989. In type 6 blocks, the initial tag need not be on a line by
  1990. itself:
  1991. ```````````````````````````````` example
  1992. <div><a href="bar">*foo*</a></div>
  1993. .
  1994. <div><a href="bar">*foo*</a></div>
  1995. ````````````````````````````````
  1996. ```````````````````````````````` example
  1997. <table><tr><td>
  1998. foo
  1999. </td></tr></table>
  2000. .
  2001. <table><tr><td>
  2002. foo
  2003. </td></tr></table>
  2004. ````````````````````````````````
  2005. Everything until the next blank line or end of document
  2006. gets included in the HTML block. So, in the following
  2007. example, what looks like a Markdown code block
  2008. is actually part of the HTML block, which continues until a blank
  2009. line or the end of the document is reached:
  2010. ```````````````````````````````` example
  2011. <div></div>
  2012. ``` c
  2013. int x = 33;
  2014. ```
  2015. .
  2016. <div></div>
  2017. ``` c
  2018. int x = 33;
  2019. ```
  2020. ````````````````````````````````
  2021. To start an [HTML block] with a tag that is *not* in the
  2022. list of block-level tags in (6), you must put the tag by
  2023. itself on the first line (and it must be complete):
  2024. ```````````````````````````````` example
  2025. <a href="foo">
  2026. *bar*
  2027. </a>
  2028. .
  2029. <a href="foo">
  2030. *bar*
  2031. </a>
  2032. ````````````````````````````````
  2033. In type 7 blocks, the [tag name] can be anything:
  2034. ```````````````````````````````` example
  2035. <Warning>
  2036. *bar*
  2037. </Warning>
  2038. .
  2039. <Warning>
  2040. *bar*
  2041. </Warning>
  2042. ````````````````````````````````
  2043. ```````````````````````````````` example
  2044. <i class="foo">
  2045. *bar*
  2046. </i>
  2047. .
  2048. <i class="foo">
  2049. *bar*
  2050. </i>
  2051. ````````````````````````````````
  2052. ```````````````````````````````` example
  2053. </ins>
  2054. *bar*
  2055. .
  2056. </ins>
  2057. *bar*
  2058. ````````````````````````````````
  2059. These rules are designed to allow us to work with tags that
  2060. can function as either block-level or inline-level tags.
  2061. The `<del>` tag is a nice example. We can surround content with
  2062. `<del>` tags in three different ways. In this case, we get a raw
  2063. HTML block, because the `<del>` tag is on a line by itself:
  2064. ```````````````````````````````` example
  2065. <del>
  2066. *foo*
  2067. </del>
  2068. .
  2069. <del>
  2070. *foo*
  2071. </del>
  2072. ````````````````````````````````
  2073. In this case, we get a raw HTML block that just includes
  2074. the `<del>` tag (because it ends with the following blank
  2075. line). So the contents get interpreted as CommonMark:
  2076. ```````````````````````````````` example
  2077. <del>
  2078. *foo*
  2079. </del>
  2080. .
  2081. <del>
  2082. <p><em>foo</em></p>
  2083. </del>
  2084. ````````````````````````````````
  2085. Finally, in this case, the `<del>` tags are interpreted
  2086. as [raw HTML] *inside* the CommonMark paragraph. (Because
  2087. the tag is not on a line by itself, we get inline HTML
  2088. rather than an [HTML block].)
  2089. ```````````````````````````````` example
  2090. <del>*foo*</del>
  2091. .
  2092. <p><del><em>foo</em></del></p>
  2093. ````````````````````````````````
  2094. HTML tags designed to contain literal content
  2095. (`script`, `style`, `pre`), comments, processing instructions,
  2096. and declarations are treated somewhat differently.
  2097. Instead of ending at the first blank line, these blocks
  2098. end at the first line containing a corresponding end tag.
  2099. As a result, these blocks can contain blank lines:
  2100. A pre tag (type 1):
  2101. ```````````````````````````````` example
  2102. <pre language="haskell"><code>
  2103. import Text.HTML.TagSoup
  2104. main :: IO ()
  2105. main = print $ parseTags tags
  2106. </code></pre>
  2107. okay
  2108. .
  2109. <pre language="haskell"><code>
  2110. import Text.HTML.TagSoup
  2111. main :: IO ()
  2112. main = print $ parseTags tags
  2113. </code></pre>
  2114. <p>okay</p>
  2115. ````````````````````````````````
  2116. A script tag (type 1):
  2117. ```````````````````````````````` example
  2118. <script type="text/javascript">
  2119. // JavaScript example
  2120. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  2121. </script>
  2122. okay
  2123. .
  2124. <script type="text/javascript">
  2125. // JavaScript example
  2126. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  2127. </script>
  2128. <p>okay</p>
  2129. ````````````````````````````````
  2130. A style tag (type 1):
  2131. ```````````````````````````````` example
  2132. <style
  2133. type="text/css">
  2134. h1 {color:red;}
  2135. p {color:blue;}
  2136. </style>
  2137. okay
  2138. .
  2139. <style
  2140. type="text/css">
  2141. h1 {color:red;}
  2142. p {color:blue;}
  2143. </style>
  2144. <p>okay</p>
  2145. ````````````````````````````````
  2146. If there is no matching end tag, the block will end at the
  2147. end of the document (or the enclosing [block quote][block quotes]
  2148. or [list item][list items]):
  2149. ```````````````````````````````` example
  2150. <style
  2151. type="text/css">
  2152. foo
  2153. .
  2154. <style
  2155. type="text/css">
  2156. foo
  2157. ````````````````````````````````
  2158. ```````````````````````````````` example
  2159. > <div>
  2160. > foo
  2161. bar
  2162. .
  2163. <blockquote>
  2164. <div>
  2165. foo
  2166. </blockquote>
  2167. <p>bar</p>
  2168. ````````````````````````````````
  2169. ```````````````````````````````` example
  2170. - <div>
  2171. - foo
  2172. .
  2173. <ul>
  2174. <li>
  2175. <div>
  2176. </li>
  2177. <li>foo</li>
  2178. </ul>
  2179. ````````````````````````````````
  2180. The end tag can occur on the same line as the start tag:
  2181. ```````````````````````````````` example
  2182. <style>p{color:red;}</style>
  2183. *foo*
  2184. .
  2185. <style>p{color:red;}</style>
  2186. <p><em>foo</em></p>
  2187. ````````````````````````````````
  2188. ```````````````````````````````` example
  2189. <!-- foo -->*bar*
  2190. *baz*
  2191. .
  2192. <!-- foo -->*bar*
  2193. <p><em>baz</em></p>
  2194. ````````````````````````````````
  2195. Note that anything on the last line after the
  2196. end tag will be included in the [HTML block]:
  2197. ```````````````````````````````` example
  2198. <script>
  2199. foo
  2200. </script>1. *bar*
  2201. .
  2202. <script>
  2203. foo
  2204. </script>1. *bar*
  2205. ````````````````````````````````
  2206. A comment (type 2):
  2207. ```````````````````````````````` example
  2208. <!-- Foo
  2209. bar
  2210. baz -->
  2211. okay
  2212. .
  2213. <!-- Foo
  2214. bar
  2215. baz -->
  2216. <p>okay</p>
  2217. ````````````````````````````````
  2218. A processing instruction (type 3):
  2219. ```````````````````````````````` example
  2220. <?php
  2221. echo '>';
  2222. ?>
  2223. okay
  2224. .
  2225. <?php
  2226. echo '>';
  2227. ?>
  2228. <p>okay</p>
  2229. ````````````````````````````````
  2230. A declaration (type 4):
  2231. ```````````````````````````````` example
  2232. <!DOCTYPE html>
  2233. .
  2234. <!DOCTYPE html>
  2235. ````````````````````````````````
  2236. CDATA (type 5):
  2237. ```````````````````````````````` example
  2238. <![CDATA[
  2239. function matchwo(a,b)
  2240. {
  2241. if (a < b && a < 0) then {
  2242. return 1;
  2243. } else {
  2244. return 0;
  2245. }
  2246. }
  2247. ]]>
  2248. okay
  2249. .
  2250. <![CDATA[
  2251. function matchwo(a,b)
  2252. {
  2253. if (a < b && a < 0) then {
  2254. return 1;
  2255. } else {
  2256. return 0;
  2257. }
  2258. }
  2259. ]]>
  2260. <p>okay</p>
  2261. ````````````````````````````````
  2262. The opening tag can be indented 1-3 spaces, but not 4:
  2263. ```````````````````````````````` example
  2264. <!-- foo -->
  2265. <!-- foo -->
  2266. .
  2267. <!-- foo -->
  2268. <pre><code>&lt;!-- foo --&gt;
  2269. </code></pre>
  2270. ````````````````````````````````
  2271. ```````````````````````````````` example
  2272. <div>
  2273. <div>
  2274. .
  2275. <div>
  2276. <pre><code>&lt;div&gt;
  2277. </code></pre>
  2278. ````````````````````````````````
  2279. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  2280. preceded by a blank line.
  2281. ```````````````````````````````` example
  2282. Foo
  2283. <div>
  2284. bar
  2285. </div>
  2286. .
  2287. <p>Foo</p>
  2288. <div>
  2289. bar
  2290. </div>
  2291. ````````````````````````````````
  2292. However, a following blank line is needed, except at the end of
  2293. a document, and except for blocks of types 1--5, [above][HTML
  2294. block]:
  2295. ```````````````````````````````` example
  2296. <div>
  2297. bar
  2298. </div>
  2299. *foo*
  2300. .
  2301. <div>
  2302. bar
  2303. </div>
  2304. *foo*
  2305. ````````````````````````````````
  2306. HTML blocks of type 7 cannot interrupt a paragraph:
  2307. ```````````````````````````````` example
  2308. Foo
  2309. <a href="bar">
  2310. baz
  2311. .
  2312. <p>Foo
  2313. <a href="bar">
  2314. baz</p>
  2315. ````````````````````````````````
  2316. This rule differs from John Gruber's original Markdown syntax
  2317. specification, which says:
  2318. > The only restrictions are that block-level HTML elements —
  2319. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2320. > surrounding content by blank lines, and the start and end tags of the
  2321. > block should not be indented with tabs or spaces.
  2322. In some ways Gruber's rule is more restrictive than the one given
  2323. here:
  2324. - It requires that an HTML block be preceded by a blank line.
  2325. - It does not allow the start tag to be indented.
  2326. - It requires a matching end tag, which it also does not allow to
  2327. be indented.
  2328. Most Markdown implementations (including some of Gruber's own) do not
  2329. respect all of these restrictions.
  2330. There is one respect, however, in which Gruber's rule is more liberal
  2331. than the one given here, since it allows blank lines to occur inside
  2332. an HTML block. There are two reasons for disallowing them here.
  2333. First, it removes the need to parse balanced tags, which is
  2334. expensive and can require backtracking from the end of the document
  2335. if no matching end tag is found. Second, it provides a very simple
  2336. and flexible way of including Markdown content inside HTML tags:
  2337. simply separate the Markdown from the HTML using blank lines:
  2338. Compare:
  2339. ```````````````````````````````` example
  2340. <div>
  2341. *Emphasized* text.
  2342. </div>
  2343. .
  2344. <div>
  2345. <p><em>Emphasized</em> text.</p>
  2346. </div>
  2347. ````````````````````````````````
  2348. ```````````````````````````````` example
  2349. <div>
  2350. *Emphasized* text.
  2351. </div>
  2352. .
  2353. <div>
  2354. *Emphasized* text.
  2355. </div>
  2356. ````````````````````````````````
  2357. Some Markdown implementations have adopted a convention of
  2358. interpreting content inside tags as text if the open tag has
  2359. the attribute `markdown=1`. The rule given above seems a simpler and
  2360. more elegant way of achieving the same expressive power, which is also
  2361. much simpler to parse.
  2362. The main potential drawback is that one can no longer paste HTML
  2363. blocks into Markdown documents with 100% reliability. However,
  2364. *in most cases* this will work fine, because the blank lines in
  2365. HTML are usually followed by HTML block tags. For example:
  2366. ```````````````````````````````` example
  2367. <table>
  2368. <tr>
  2369. <td>
  2370. Hi
  2371. </td>
  2372. </tr>
  2373. </table>
  2374. .
  2375. <table>
  2376. <tr>
  2377. <td>
  2378. Hi
  2379. </td>
  2380. </tr>
  2381. </table>
  2382. ````````````````````````````````
  2383. There are problems, however, if the inner tags are indented
  2384. *and* separated by spaces, as then they will be interpreted as
  2385. an indented code block:
  2386. ```````````````````````````````` example
  2387. <table>
  2388. <tr>
  2389. <td>
  2390. Hi
  2391. </td>
  2392. </tr>
  2393. </table>
  2394. .
  2395. <table>
  2396. <tr>
  2397. <pre><code>&lt;td&gt;
  2398. Hi
  2399. &lt;/td&gt;
  2400. </code></pre>
  2401. </tr>
  2402. </table>
  2403. ````````````````````````````````
  2404. Fortunately, blank lines are usually not necessary and can be
  2405. deleted. The exception is inside `<pre>` tags, but as described
  2406. [above][HTML blocks], raw HTML blocks starting with `<pre>`
  2407. *can* contain blank lines.
  2408. ## Link reference definitions
  2409. A [link reference definition](@)
  2410. consists of a [link label], indented up to three spaces, followed
  2411. by a colon (`:`), optional [whitespace] (including up to one
  2412. [line ending]), a [link destination],
  2413. optional [whitespace] (including up to one
  2414. [line ending]), and an optional [link
  2415. title], which if it is present must be separated
  2416. from the [link destination] by [whitespace].
  2417. No further [non-whitespace characters] may occur on the line.
  2418. A [link reference definition]
  2419. does not correspond to a structural element of a document. Instead, it
  2420. defines a label which can be used in [reference links]
  2421. and reference-style [images] elsewhere in the document. [Link
  2422. reference definitions] can come either before or after the links that use
  2423. them.
  2424. ```````````````````````````````` example
  2425. [foo]: /url "title"
  2426. [foo]
  2427. .
  2428. <p><a href="/url" title="title">foo</a></p>
  2429. ````````````````````````````````
  2430. ```````````````````````````````` example
  2431. [foo]:
  2432. /url
  2433. 'the title'
  2434. [foo]
  2435. .
  2436. <p><a href="/url" title="the title">foo</a></p>
  2437. ````````````````````````````````
  2438. ```````````````````````````````` example
  2439. [Foo*bar\]]:my_(url) 'title (with parens)'
  2440. [Foo*bar\]]
  2441. .
  2442. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2443. ````````````````````````````````
  2444. ```````````````````````````````` example
  2445. [Foo bar]:
  2446. <my url>
  2447. 'title'
  2448. [Foo bar]
  2449. .
  2450. <p><a href="my%20url" title="title">Foo bar</a></p>
  2451. ````````````````````````````````
  2452. The title may extend over multiple lines:
  2453. ```````````````````````````````` example
  2454. [foo]: /url '
  2455. title
  2456. line1
  2457. line2
  2458. '
  2459. [foo]
  2460. .
  2461. <p><a href="/url" title="
  2462. title
  2463. line1
  2464. line2
  2465. ">foo</a></p>
  2466. ````````````````````````````````
  2467. However, it may not contain a [blank line]:
  2468. ```````````````````````````````` example
  2469. [foo]: /url 'title
  2470. with blank line'
  2471. [foo]
  2472. .
  2473. <p>[foo]: /url 'title</p>
  2474. <p>with blank line'</p>
  2475. <p>[foo]</p>
  2476. ````````````````````````````````
  2477. The title may be omitted:
  2478. ```````````````````````````````` example
  2479. [foo]:
  2480. /url
  2481. [foo]
  2482. .
  2483. <p><a href="/url">foo</a></p>
  2484. ````````````````````````````````
  2485. The link destination may not be omitted:
  2486. ```````````````````````````````` example
  2487. [foo]:
  2488. [foo]
  2489. .
  2490. <p>[foo]:</p>
  2491. <p>[foo]</p>
  2492. ````````````````````````````````
  2493. However, an empty link destination may be specified using
  2494. angle brackets:
  2495. ```````````````````````````````` example
  2496. [foo]: <>
  2497. [foo]
  2498. .
  2499. <p><a href="">foo</a></p>
  2500. ````````````````````````````````
  2501. The title must be separated from the link destination by
  2502. whitespace:
  2503. ```````````````````````````````` example
  2504. [foo]: <bar>(baz)
  2505. [foo]
  2506. .
  2507. <p>[foo]: <bar>(baz)</p>
  2508. <p>[foo]</p>
  2509. ````````````````````````````````
  2510. Both title and destination can contain backslash escapes
  2511. and literal backslashes:
  2512. ```````````````````````````````` example
  2513. [foo]: /url\bar\*baz "foo\"bar\baz"
  2514. [foo]
  2515. .
  2516. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2517. ````````````````````````````````
  2518. A link can come before its corresponding definition:
  2519. ```````````````````````````````` example
  2520. [foo]
  2521. [foo]: url
  2522. .
  2523. <p><a href="url">foo</a></p>
  2524. ````````````````````````````````
  2525. If there are several matching definitions, the first one takes
  2526. precedence:
  2527. ```````````````````````````````` example
  2528. [foo]
  2529. [foo]: first
  2530. [foo]: second
  2531. .
  2532. <p><a href="first">foo</a></p>
  2533. ````````````````````````````````
  2534. As noted in the section on [Links], matching of labels is
  2535. case-insensitive (see [matches]).
  2536. ```````````````````````````````` example
  2537. [FOO]: /url
  2538. [Foo]
  2539. .
  2540. <p><a href="/url">Foo</a></p>
  2541. ````````````````````````````````
  2542. ```````````````````````````````` example
  2543. [ΑΓΩ]: /φου
  2544. [αγω]
  2545. .
  2546. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2547. ````````````````````````````````
  2548. Here is a link reference definition with no corresponding link.
  2549. It contributes nothing to the document.
  2550. ```````````````````````````````` example
  2551. [foo]: /url
  2552. .
  2553. ````````````````````````````````
  2554. Here is another one:
  2555. ```````````````````````````````` example
  2556. [
  2557. foo
  2558. ]: /url
  2559. bar
  2560. .
  2561. <p>bar</p>
  2562. ````````````````````````````````
  2563. This is not a link reference definition, because there are
  2564. [non-whitespace characters] after the title:
  2565. ```````````````````````````````` example
  2566. [foo]: /url "title" ok
  2567. .
  2568. <p>[foo]: /url &quot;title&quot; ok</p>
  2569. ````````````````````````````````
  2570. This is a link reference definition, but it has no title:
  2571. ```````````````````````````````` example
  2572. [foo]: /url
  2573. "title" ok
  2574. .
  2575. <p>&quot;title&quot; ok</p>
  2576. ````````````````````````````````
  2577. This is not a link reference definition, because it is indented
  2578. four spaces:
  2579. ```````````````````````````````` example
  2580. [foo]: /url "title"
  2581. [foo]
  2582. .
  2583. <pre><code>[foo]: /url &quot;title&quot;
  2584. </code></pre>
  2585. <p>[foo]</p>
  2586. ````````````````````````````````
  2587. This is not a link reference definition, because it occurs inside
  2588. a code block:
  2589. ```````````````````````````````` example
  2590. ```
  2591. [foo]: /url
  2592. ```
  2593. [foo]
  2594. .
  2595. <pre><code>[foo]: /url
  2596. </code></pre>
  2597. <p>[foo]</p>
  2598. ````````````````````````````````
  2599. A [link reference definition] cannot interrupt a paragraph.
  2600. ```````````````````````````````` example
  2601. Foo
  2602. [bar]: /baz
  2603. [bar]
  2604. .
  2605. <p>Foo
  2606. [bar]: /baz</p>
  2607. <p>[bar]</p>
  2608. ````````````````````````````````
  2609. However, it can directly follow other block elements, such as headings
  2610. and thematic breaks, and it need not be followed by a blank line.
  2611. ```````````````````````````````` example
  2612. # [Foo]
  2613. [foo]: /url
  2614. > bar
  2615. .
  2616. <h1><a href="/url">Foo</a></h1>
  2617. <blockquote>
  2618. <p>bar</p>
  2619. </blockquote>
  2620. ````````````````````````````````
  2621. ```````````````````````````````` example
  2622. [foo]: /url
  2623. bar
  2624. ===
  2625. [foo]
  2626. .
  2627. <h1>bar</h1>
  2628. <p><a href="/url">foo</a></p>
  2629. ````````````````````````````````
  2630. ```````````````````````````````` example
  2631. [foo]: /url
  2632. ===
  2633. [foo]
  2634. .
  2635. <p>===
  2636. <a href="/url">foo</a></p>
  2637. ````````````````````````````````
  2638. Several [link reference definitions]
  2639. can occur one after another, without intervening blank lines.
  2640. ```````````````````````````````` example
  2641. [foo]: /foo-url "foo"
  2642. [bar]: /bar-url
  2643. "bar"
  2644. [baz]: /baz-url
  2645. [foo],
  2646. [bar],
  2647. [baz]
  2648. .
  2649. <p><a href="/foo-url" title="foo">foo</a>,
  2650. <a href="/bar-url" title="bar">bar</a>,
  2651. <a href="/baz-url">baz</a></p>
  2652. ````````````````````````````````
  2653. [Link reference definitions] can occur
  2654. inside block containers, like lists and block quotations. They
  2655. affect the entire document, not just the container in which they
  2656. are defined:
  2657. ```````````````````````````````` example
  2658. [foo]
  2659. > [foo]: /url
  2660. .
  2661. <p><a href="/url">foo</a></p>
  2662. <blockquote>
  2663. </blockquote>
  2664. ````````````````````````````````
  2665. Whether something is a [link reference definition] is
  2666. independent of whether the link reference it defines is
  2667. used in the document. Thus, for example, the following
  2668. document contains just a link reference definition, and
  2669. no visible content:
  2670. ```````````````````````````````` example
  2671. [foo]: /url
  2672. .
  2673. ````````````````````````````````
  2674. ## Paragraphs
  2675. A sequence of non-blank lines that cannot be interpreted as other
  2676. kinds of blocks forms a [paragraph](@).
  2677. The contents of the paragraph are the result of parsing the
  2678. paragraph's raw content as inlines. The paragraph's raw content
  2679. is formed by concatenating the lines and removing initial and final
  2680. [whitespace].
  2681. A simple example with two paragraphs:
  2682. ```````````````````````````````` example
  2683. aaa
  2684. bbb
  2685. .
  2686. <p>aaa</p>
  2687. <p>bbb</p>
  2688. ````````````````````````````````
  2689. Paragraphs can contain multiple lines, but no blank lines:
  2690. ```````````````````````````````` example
  2691. aaa
  2692. bbb
  2693. ccc
  2694. ddd
  2695. .
  2696. <p>aaa
  2697. bbb</p>
  2698. <p>ccc
  2699. ddd</p>
  2700. ````````````````````````````````
  2701. Multiple blank lines between paragraph have no effect:
  2702. ```````````````````````````````` example
  2703. aaa
  2704. bbb
  2705. .
  2706. <p>aaa</p>
  2707. <p>bbb</p>
  2708. ````````````````````````````````
  2709. Leading spaces are skipped:
  2710. ```````````````````````````````` example
  2711. aaa
  2712. bbb
  2713. .
  2714. <p>aaa
  2715. bbb</p>
  2716. ````````````````````````````````
  2717. Lines after the first may be indented any amount, since indented
  2718. code blocks cannot interrupt paragraphs.
  2719. ```````````````````````````````` example
  2720. aaa
  2721. bbb
  2722. ccc
  2723. .
  2724. <p>aaa
  2725. bbb
  2726. ccc</p>
  2727. ````````````````````````````````
  2728. However, the first line may be indented at most three spaces,
  2729. or an indented code block will be triggered:
  2730. ```````````````````````````````` example
  2731. aaa
  2732. bbb
  2733. .
  2734. <p>aaa
  2735. bbb</p>
  2736. ````````````````````````````````
  2737. ```````````````````````````````` example
  2738. aaa
  2739. bbb
  2740. .
  2741. <pre><code>aaa
  2742. </code></pre>
  2743. <p>bbb</p>
  2744. ````````````````````````````````
  2745. Final spaces are stripped before inline parsing, so a paragraph
  2746. that ends with two or more spaces will not end with a [hard line
  2747. break]:
  2748. ```````````````````````````````` example
  2749. aaa
  2750. bbb
  2751. .
  2752. <p>aaa<br />
  2753. bbb</p>
  2754. ````````````````````````````````
  2755. ## Blank lines
  2756. [Blank lines] between block-level elements are ignored,
  2757. except for the role they play in determining whether a [list]
  2758. is [tight] or [loose].
  2759. Blank lines at the beginning and end of the document are also ignored.
  2760. ```````````````````````````````` example
  2761. aaa
  2762. # aaa
  2763. .
  2764. <p>aaa</p>
  2765. <h1>aaa</h1>
  2766. ````````````````````````````````
  2767. # Container blocks
  2768. A [container block](#container-blocks) is a block that has other
  2769. blocks as its contents. There are two basic kinds of container blocks:
  2770. [block quotes] and [list items].
  2771. [Lists] are meta-containers for [list items].
  2772. We define the syntax for container blocks recursively. The general
  2773. form of the definition is:
  2774. > If X is a sequence of blocks, then the result of
  2775. > transforming X in such-and-such a way is a container of type Y
  2776. > with these blocks as its content.
  2777. So, we explain what counts as a block quote or list item by explaining
  2778. how these can be *generated* from their contents. This should suffice
  2779. to define the syntax, although it does not give a recipe for *parsing*
  2780. these constructions. (A recipe is provided below in the section entitled
  2781. [A parsing strategy](#appendix-a-parsing-strategy).)
  2782. ## Block quotes
  2783. A [block quote marker](@)
  2784. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2785. with a following space, or (b) a single character `>` not followed by a space.
  2786. The following rules define [block quotes]:
  2787. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2788. of blocks *Bs*, then the result of prepending a [block quote
  2789. marker] to the beginning of each line in *Ls*
  2790. is a [block quote](#block-quotes) containing *Bs*.
  2791. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2792. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2793. the initial [block quote marker] from one or
  2794. more lines in which the next [non-whitespace character] after the [block
  2795. quote marker] is [paragraph continuation
  2796. text] is a block quote with *Bs* as its content.
  2797. [Paragraph continuation text](@) is text
  2798. that will be parsed as part of the content of a paragraph, but does
  2799. not occur at the beginning of the paragraph.
  2800. 3. **Consecutiveness.** A document cannot contain two [block
  2801. quotes] in a row unless there is a [blank line] between them.
  2802. Nothing else counts as a [block quote](#block-quotes).
  2803. Here is a simple example:
  2804. ```````````````````````````````` example
  2805. > # Foo
  2806. > bar
  2807. > baz
  2808. .
  2809. <blockquote>
  2810. <h1>Foo</h1>
  2811. <p>bar
  2812. baz</p>
  2813. </blockquote>
  2814. ````````````````````````````````
  2815. The spaces after the `>` characters can be omitted:
  2816. ```````````````````````````````` example
  2817. ># Foo
  2818. >bar
  2819. > baz
  2820. .
  2821. <blockquote>
  2822. <h1>Foo</h1>
  2823. <p>bar
  2824. baz</p>
  2825. </blockquote>
  2826. ````````````````````````````````
  2827. The `>` characters can be indented 1-3 spaces:
  2828. ```````````````````````````````` example
  2829. > # Foo
  2830. > bar
  2831. > baz
  2832. .
  2833. <blockquote>
  2834. <h1>Foo</h1>
  2835. <p>bar
  2836. baz</p>
  2837. </blockquote>
  2838. ````````````````````````````````
  2839. Four spaces gives us a code block:
  2840. ```````````````````````````````` example
  2841. > # Foo
  2842. > bar
  2843. > baz
  2844. .
  2845. <pre><code>&gt; # Foo
  2846. &gt; bar
  2847. &gt; baz
  2848. </code></pre>
  2849. ````````````````````````````````
  2850. The Laziness clause allows us to omit the `>` before
  2851. [paragraph continuation text]:
  2852. ```````````````````````````````` example
  2853. > # Foo
  2854. > bar
  2855. baz
  2856. .
  2857. <blockquote>
  2858. <h1>Foo</h1>
  2859. <p>bar
  2860. baz</p>
  2861. </blockquote>
  2862. ````````````````````````````````
  2863. A block quote can contain some lazy and some non-lazy
  2864. continuation lines:
  2865. ```````````````````````````````` example
  2866. > bar
  2867. baz
  2868. > foo
  2869. .
  2870. <blockquote>
  2871. <p>bar
  2872. baz
  2873. foo</p>
  2874. </blockquote>
  2875. ````````````````````````````````
  2876. Laziness only applies to lines that would have been continuations of
  2877. paragraphs had they been prepended with [block quote markers].
  2878. For example, the `> ` cannot be omitted in the second line of
  2879. ``` markdown
  2880. > foo
  2881. > ---
  2882. ```
  2883. without changing the meaning:
  2884. ```````````````````````````````` example
  2885. > foo
  2886. ---
  2887. .
  2888. <blockquote>
  2889. <p>foo</p>
  2890. </blockquote>
  2891. <hr />
  2892. ````````````````````````````````
  2893. Similarly, if we omit the `> ` in the second line of
  2894. ``` markdown
  2895. > - foo
  2896. > - bar
  2897. ```
  2898. then the block quote ends after the first line:
  2899. ```````````````````````````````` example
  2900. > - foo
  2901. - bar
  2902. .
  2903. <blockquote>
  2904. <ul>
  2905. <li>foo</li>
  2906. </ul>
  2907. </blockquote>
  2908. <ul>
  2909. <li>bar</li>
  2910. </ul>
  2911. ````````````````````````````````
  2912. For the same reason, we can't omit the `> ` in front of
  2913. subsequent lines of an indented or fenced code block:
  2914. ```````````````````````````````` example
  2915. > foo
  2916. bar
  2917. .
  2918. <blockquote>
  2919. <pre><code>foo
  2920. </code></pre>
  2921. </blockquote>
  2922. <pre><code>bar
  2923. </code></pre>
  2924. ````````````````````````````````
  2925. ```````````````````````````````` example
  2926. > ```
  2927. foo
  2928. ```
  2929. .
  2930. <blockquote>
  2931. <pre><code></code></pre>
  2932. </blockquote>
  2933. <p>foo</p>
  2934. <pre><code></code></pre>
  2935. ````````````````````````````````
  2936. Note that in the following case, we have a [lazy
  2937. continuation line]:
  2938. ```````````````````````````````` example
  2939. > foo
  2940. - bar
  2941. .
  2942. <blockquote>
  2943. <p>foo
  2944. - bar</p>
  2945. </blockquote>
  2946. ````````````````````````````````
  2947. To see why, note that in
  2948. ```markdown
  2949. > foo
  2950. > - bar
  2951. ```
  2952. the `- bar` is indented too far to start a list, and can't
  2953. be an indented code block because indented code blocks cannot
  2954. interrupt paragraphs, so it is [paragraph continuation text].
  2955. A block quote can be empty:
  2956. ```````````````````````````````` example
  2957. >
  2958. .
  2959. <blockquote>
  2960. </blockquote>
  2961. ````````````````````````````````
  2962. ```````````````````````````````` example
  2963. >
  2964. >
  2965. >
  2966. .
  2967. <blockquote>
  2968. </blockquote>
  2969. ````````````````````````````````
  2970. A block quote can have initial or final blank lines:
  2971. ```````````````````````````````` example
  2972. >
  2973. > foo
  2974. >
  2975. .
  2976. <blockquote>
  2977. <p>foo</p>
  2978. </blockquote>
  2979. ````````````````````````````````
  2980. A blank line always separates block quotes:
  2981. ```````````````````````````````` example
  2982. > foo
  2983. > bar
  2984. .
  2985. <blockquote>
  2986. <p>foo</p>
  2987. </blockquote>
  2988. <blockquote>
  2989. <p>bar</p>
  2990. </blockquote>
  2991. ````````````````````````````````
  2992. (Most current Markdown implementations, including John Gruber's
  2993. original `Markdown.pl`, will parse this example as a single block quote
  2994. with two paragraphs. But it seems better to allow the author to decide
  2995. whether two block quotes or one are wanted.)
  2996. Consecutiveness means that if we put these block quotes together,
  2997. we get a single block quote:
  2998. ```````````````````````````````` example
  2999. > foo
  3000. > bar
  3001. .
  3002. <blockquote>
  3003. <p>foo
  3004. bar</p>
  3005. </blockquote>
  3006. ````````````````````````````````
  3007. To get a block quote with two paragraphs, use:
  3008. ```````````````````````````````` example
  3009. > foo
  3010. >
  3011. > bar
  3012. .
  3013. <blockquote>
  3014. <p>foo</p>
  3015. <p>bar</p>
  3016. </blockquote>
  3017. ````````````````````````````````
  3018. Block quotes can interrupt paragraphs:
  3019. ```````````````````````````````` example
  3020. foo
  3021. > bar
  3022. .
  3023. <p>foo</p>
  3024. <blockquote>
  3025. <p>bar</p>
  3026. </blockquote>
  3027. ````````````````````````````````
  3028. In general, blank lines are not needed before or after block
  3029. quotes:
  3030. ```````````````````````````````` example
  3031. > aaa
  3032. ***
  3033. > bbb
  3034. .
  3035. <blockquote>
  3036. <p>aaa</p>
  3037. </blockquote>
  3038. <hr />
  3039. <blockquote>
  3040. <p>bbb</p>
  3041. </blockquote>
  3042. ````````````````````````````````
  3043. However, because of laziness, a blank line is needed between
  3044. a block quote and a following paragraph:
  3045. ```````````````````````````````` example
  3046. > bar
  3047. baz
  3048. .
  3049. <blockquote>
  3050. <p>bar
  3051. baz</p>
  3052. </blockquote>
  3053. ````````````````````````````````
  3054. ```````````````````````````````` example
  3055. > bar
  3056. baz
  3057. .
  3058. <blockquote>
  3059. <p>bar</p>
  3060. </blockquote>
  3061. <p>baz</p>
  3062. ````````````````````````````````
  3063. ```````````````````````````````` example
  3064. > bar
  3065. >
  3066. baz
  3067. .
  3068. <blockquote>
  3069. <p>bar</p>
  3070. </blockquote>
  3071. <p>baz</p>
  3072. ````````````````````````````````
  3073. It is a consequence of the Laziness rule that any number
  3074. of initial `>`s may be omitted on a continuation line of a
  3075. nested block quote:
  3076. ```````````````````````````````` example
  3077. > > > foo
  3078. bar
  3079. .
  3080. <blockquote>
  3081. <blockquote>
  3082. <blockquote>
  3083. <p>foo
  3084. bar</p>
  3085. </blockquote>
  3086. </blockquote>
  3087. </blockquote>
  3088. ````````````````````````````````
  3089. ```````````````````````````````` example
  3090. >>> foo
  3091. > bar
  3092. >>baz
  3093. .
  3094. <blockquote>
  3095. <blockquote>
  3096. <blockquote>
  3097. <p>foo
  3098. bar
  3099. baz</p>
  3100. </blockquote>
  3101. </blockquote>
  3102. </blockquote>
  3103. ````````````````````````````````
  3104. When including an indented code block in a block quote,
  3105. remember that the [block quote marker] includes
  3106. both the `>` and a following space. So *five spaces* are needed after
  3107. the `>`:
  3108. ```````````````````````````````` example
  3109. > code
  3110. > not code
  3111. .
  3112. <blockquote>
  3113. <pre><code>code
  3114. </code></pre>
  3115. </blockquote>
  3116. <blockquote>
  3117. <p>not code</p>
  3118. </blockquote>
  3119. ````````````````````````````````
  3120. ## List items
  3121. A [list marker](@) is a
  3122. [bullet list marker] or an [ordered list marker].
  3123. A [bullet list marker](@)
  3124. is a `-`, `+`, or `*` character.
  3125. An [ordered list marker](@)
  3126. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  3127. `.` character or a `)` character. (The reason for the length
  3128. limit is that with 10 digits we start seeing integer overflows
  3129. in some browsers.)
  3130. The following rules define [list items]:
  3131. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  3132. blocks *Bs* starting with a [non-whitespace character], and *M* is a
  3133. list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  3134. of prepending *M* and the following spaces to the first line of
  3135. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  3136. list item with *Bs* as its contents. The type of the list item
  3137. (bullet or ordered) is determined by the type of its list marker.
  3138. If the list item is ordered, then it is also assigned a start
  3139. number, based on the ordered list marker.
  3140. Exceptions:
  3141. 1. When the first list item in a [list] interrupts
  3142. a paragraph---that is, when it starts on a line that would
  3143. otherwise count as [paragraph continuation text]---then (a)
  3144. the lines *Ls* must not begin with a blank line, and (b) if
  3145. the list item is ordered, the start number must be 1.
  3146. 2. If any line is a [thematic break][thematic breaks] then
  3147. that line is not a list item.
  3148. For example, let *Ls* be the lines
  3149. ```````````````````````````````` example
  3150. A paragraph
  3151. with two lines.
  3152. indented code
  3153. > A block quote.
  3154. .
  3155. <p>A paragraph
  3156. with two lines.</p>
  3157. <pre><code>indented code
  3158. </code></pre>
  3159. <blockquote>
  3160. <p>A block quote.</p>
  3161. </blockquote>
  3162. ````````````````````````````````
  3163. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  3164. that the following is an ordered list item with start number 1,
  3165. and the same contents as *Ls*:
  3166. ```````````````````````````````` example
  3167. 1. A paragraph
  3168. with two lines.
  3169. indented code
  3170. > A block quote.
  3171. .
  3172. <ol>
  3173. <li>
  3174. <p>A paragraph
  3175. with two lines.</p>
  3176. <pre><code>indented code
  3177. </code></pre>
  3178. <blockquote>
  3179. <p>A block quote.</p>
  3180. </blockquote>
  3181. </li>
  3182. </ol>
  3183. ````````````````````````````````
  3184. The most important thing to notice is that the position of
  3185. the text after the list marker determines how much indentation
  3186. is needed in subsequent blocks in the list item. If the list
  3187. marker takes up two spaces, and there are three spaces between
  3188. the list marker and the next [non-whitespace character], then blocks
  3189. must be indented five spaces in order to fall under the list
  3190. item.
  3191. Here are some examples showing how far content must be indented to be
  3192. put under the list item:
  3193. ```````````````````````````````` example
  3194. - one
  3195. two
  3196. .
  3197. <ul>
  3198. <li>one</li>
  3199. </ul>
  3200. <p>two</p>
  3201. ````````````````````````````````
  3202. ```````````````````````````````` example
  3203. - one
  3204. two
  3205. .
  3206. <ul>
  3207. <li>
  3208. <p>one</p>
  3209. <p>two</p>
  3210. </li>
  3211. </ul>
  3212. ````````````````````````````````
  3213. ```````````````````````````````` example
  3214. - one
  3215. two
  3216. .
  3217. <ul>
  3218. <li>one</li>
  3219. </ul>
  3220. <pre><code> two
  3221. </code></pre>
  3222. ````````````````````````````````
  3223. ```````````````````````````````` example
  3224. - one
  3225. two
  3226. .
  3227. <ul>
  3228. <li>
  3229. <p>one</p>
  3230. <p>two</p>
  3231. </li>
  3232. </ul>
  3233. ````````````````````````````````
  3234. It is tempting to think of this in terms of columns: the continuation
  3235. blocks must be indented at least to the column of the first
  3236. [non-whitespace character] after the list marker. However, that is not quite right.
  3237. The spaces after the list marker determine how much relative indentation
  3238. is needed. Which column this indentation reaches will depend on
  3239. how the list item is embedded in other constructions, as shown by
  3240. this example:
  3241. ```````````````````````````````` example
  3242. > > 1. one
  3243. >>
  3244. >> two
  3245. .
  3246. <blockquote>
  3247. <blockquote>
  3248. <ol>
  3249. <li>
  3250. <p>one</p>
  3251. <p>two</p>
  3252. </li>
  3253. </ol>
  3254. </blockquote>
  3255. </blockquote>
  3256. ````````````````````````````````
  3257. Here `two` occurs in the same column as the list marker `1.`,
  3258. but is actually contained in the list item, because there is
  3259. sufficient indentation after the last containing blockquote marker.
  3260. The converse is also possible. In the following example, the word `two`
  3261. occurs far to the right of the initial text of the list item, `one`, but
  3262. it is not considered part of the list item, because it is not indented
  3263. far enough past the blockquote marker:
  3264. ```````````````````````````````` example
  3265. >>- one
  3266. >>
  3267. > > two
  3268. .
  3269. <blockquote>
  3270. <blockquote>
  3271. <ul>
  3272. <li>one</li>
  3273. </ul>
  3274. <p>two</p>
  3275. </blockquote>
  3276. </blockquote>
  3277. ````````````````````````````````
  3278. Note that at least one space is needed between the list marker and
  3279. any following content, so these are not list items:
  3280. ```````````````````````````````` example
  3281. -one
  3282. 2.two
  3283. .
  3284. <p>-one</p>
  3285. <p>2.two</p>
  3286. ````````````````````````````````
  3287. A list item may contain blocks that are separated by more than
  3288. one blank line.
  3289. ```````````````````````````````` example
  3290. - foo
  3291. bar
  3292. .
  3293. <ul>
  3294. <li>
  3295. <p>foo</p>
  3296. <p>bar</p>
  3297. </li>
  3298. </ul>
  3299. ````````````````````````````````
  3300. A list item may contain any kind of block:
  3301. ```````````````````````````````` example
  3302. 1. foo
  3303. ```
  3304. bar
  3305. ```
  3306. baz
  3307. > bam
  3308. .
  3309. <ol>
  3310. <li>
  3311. <p>foo</p>
  3312. <pre><code>bar
  3313. </code></pre>
  3314. <p>baz</p>
  3315. <blockquote>
  3316. <p>bam</p>
  3317. </blockquote>
  3318. </li>
  3319. </ol>
  3320. ````````````````````````````````
  3321. A list item that contains an indented code block will preserve
  3322. empty lines within the code block verbatim.
  3323. ```````````````````````````````` example
  3324. - Foo
  3325. bar
  3326. baz
  3327. .
  3328. <ul>
  3329. <li>
  3330. <p>Foo</p>
  3331. <pre><code>bar
  3332. baz
  3333. </code></pre>
  3334. </li>
  3335. </ul>
  3336. ````````````````````````````````
  3337. Note that ordered list start numbers must be nine digits or less:
  3338. ```````````````````````````````` example
  3339. 123456789. ok
  3340. .
  3341. <ol start="123456789">
  3342. <li>ok</li>
  3343. </ol>
  3344. ````````````````````````````````
  3345. ```````````````````````````````` example
  3346. 1234567890. not ok
  3347. .
  3348. <p>1234567890. not ok</p>
  3349. ````````````````````````````````
  3350. A start number may begin with 0s:
  3351. ```````````````````````````````` example
  3352. 0. ok
  3353. .
  3354. <ol start="0">
  3355. <li>ok</li>
  3356. </ol>
  3357. ````````````````````````````````
  3358. ```````````````````````````````` example
  3359. 003. ok
  3360. .
  3361. <ol start="3">
  3362. <li>ok</li>
  3363. </ol>
  3364. ````````````````````````````````
  3365. A start number may not be negative:
  3366. ```````````````````````````````` example
  3367. -1. not ok
  3368. .
  3369. <p>-1. not ok</p>
  3370. ````````````````````````````````
  3371. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3372. constitute a sequence of blocks *Bs* starting with an indented code
  3373. block, and *M* is a list marker of width *W* followed by
  3374. one space, then the result of prepending *M* and the following
  3375. space to the first line of *Ls*, and indenting subsequent lines of
  3376. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3377. If a line is empty, then it need not be indented. The type of the
  3378. list item (bullet or ordered) is determined by the type of its list
  3379. marker. If the list item is ordered, then it is also assigned a
  3380. start number, based on the ordered list marker.
  3381. An indented code block will have to be indented four spaces beyond
  3382. the edge of the region where text will be included in the list item.
  3383. In the following case that is 6 spaces:
  3384. ```````````````````````````````` example
  3385. - foo
  3386. bar
  3387. .
  3388. <ul>
  3389. <li>
  3390. <p>foo</p>
  3391. <pre><code>bar
  3392. </code></pre>
  3393. </li>
  3394. </ul>
  3395. ````````````````````````````````
  3396. And in this case it is 11 spaces:
  3397. ```````````````````````````````` example
  3398. 10. foo
  3399. bar
  3400. .
  3401. <ol start="10">
  3402. <li>
  3403. <p>foo</p>
  3404. <pre><code>bar
  3405. </code></pre>
  3406. </li>
  3407. </ol>
  3408. ````````````````````````````````
  3409. If the *first* block in the list item is an indented code block,
  3410. then by rule #2, the contents must be indented *one* space after the
  3411. list marker:
  3412. ```````````````````````````````` example
  3413. indented code
  3414. paragraph
  3415. more code
  3416. .
  3417. <pre><code>indented code
  3418. </code></pre>
  3419. <p>paragraph</p>
  3420. <pre><code>more code
  3421. </code></pre>
  3422. ````````````````````````````````
  3423. ```````````````````````````````` example
  3424. 1. indented code
  3425. paragraph
  3426. more code
  3427. .
  3428. <ol>
  3429. <li>
  3430. <pre><code>indented code
  3431. </code></pre>
  3432. <p>paragraph</p>
  3433. <pre><code>more code
  3434. </code></pre>
  3435. </li>
  3436. </ol>
  3437. ````````````````````````````````
  3438. Note that an additional space indent is interpreted as space
  3439. inside the code block:
  3440. ```````````````````````````````` example
  3441. 1. indented code
  3442. paragraph
  3443. more code
  3444. .
  3445. <ol>
  3446. <li>
  3447. <pre><code> indented code
  3448. </code></pre>
  3449. <p>paragraph</p>
  3450. <pre><code>more code
  3451. </code></pre>
  3452. </li>
  3453. </ol>
  3454. ````````````````````````````````
  3455. Note that rules #1 and #2 only apply to two cases: (a) cases
  3456. in which the lines to be included in a list item begin with a
  3457. [non-whitespace character], and (b) cases in which
  3458. they begin with an indented code
  3459. block. In a case like the following, where the first block begins with
  3460. a three-space indent, the rules do not allow us to form a list item by
  3461. indenting the whole thing and prepending a list marker:
  3462. ```````````````````````````````` example
  3463. foo
  3464. bar
  3465. .
  3466. <p>foo</p>
  3467. <p>bar</p>
  3468. ````````````````````````````````
  3469. ```````````````````````````````` example
  3470. - foo
  3471. bar
  3472. .
  3473. <ul>
  3474. <li>foo</li>
  3475. </ul>
  3476. <p>bar</p>
  3477. ````````````````````````````````
  3478. This is not a significant restriction, because when a block begins
  3479. with 1-3 spaces indent, the indentation can always be removed without
  3480. a change in interpretation, allowing rule #1 to be applied. So, in
  3481. the above case:
  3482. ```````````````````````````````` example
  3483. - foo
  3484. bar
  3485. .
  3486. <ul>
  3487. <li>
  3488. <p>foo</p>
  3489. <p>bar</p>
  3490. </li>
  3491. </ul>
  3492. ````````````````````````````````
  3493. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3494. starting with a single [blank line] constitute a (possibly empty)
  3495. sequence of blocks *Bs*, not separated from each other by more than
  3496. one blank line, and *M* is a list marker of width *W*,
  3497. then the result of prepending *M* to the first line of *Ls*, and
  3498. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3499. item with *Bs* as its contents.
  3500. If a line is empty, then it need not be indented. The type of the
  3501. list item (bullet or ordered) is determined by the type of its list
  3502. marker. If the list item is ordered, then it is also assigned a
  3503. start number, based on the ordered list marker.
  3504. Here are some list items that start with a blank line but are not empty:
  3505. ```````````````````````````````` example
  3506. -
  3507. foo
  3508. -
  3509. ```
  3510. bar
  3511. ```
  3512. -
  3513. baz
  3514. .
  3515. <ul>
  3516. <li>foo</li>
  3517. <li>
  3518. <pre><code>bar
  3519. </code></pre>
  3520. </li>
  3521. <li>
  3522. <pre><code>baz
  3523. </code></pre>
  3524. </li>
  3525. </ul>
  3526. ````````````````````````````````
  3527. When the list item starts with a blank line, the number of spaces
  3528. following the list marker doesn't change the required indentation:
  3529. ```````````````````````````````` example
  3530. -
  3531. foo
  3532. .
  3533. <ul>
  3534. <li>foo</li>
  3535. </ul>
  3536. ````````````````````````````````
  3537. A list item can begin with at most one blank line.
  3538. In the following example, `foo` is not part of the list
  3539. item:
  3540. ```````````````````````````````` example
  3541. -
  3542. foo
  3543. .
  3544. <ul>
  3545. <li></li>
  3546. </ul>
  3547. <p>foo</p>
  3548. ````````````````````````````````
  3549. Here is an empty bullet list item:
  3550. ```````````````````````````````` example
  3551. - foo
  3552. -
  3553. - bar
  3554. .
  3555. <ul>
  3556. <li>foo</li>
  3557. <li></li>
  3558. <li>bar</li>
  3559. </ul>
  3560. ````````````````````````````````
  3561. It does not matter whether there are spaces following the [list marker]:
  3562. ```````````````````````````````` example
  3563. - foo
  3564. -
  3565. - bar
  3566. .
  3567. <ul>
  3568. <li>foo</li>
  3569. <li></li>
  3570. <li>bar</li>
  3571. </ul>
  3572. ````````````````````````````````
  3573. Here is an empty ordered list item:
  3574. ```````````````````````````````` example
  3575. 1. foo
  3576. 2.
  3577. 3. bar
  3578. .
  3579. <ol>
  3580. <li>foo</li>
  3581. <li></li>
  3582. <li>bar</li>
  3583. </ol>
  3584. ````````````````````````````````
  3585. A list may start or end with an empty list item:
  3586. ```````````````````````````````` example
  3587. *
  3588. .
  3589. <ul>
  3590. <li></li>
  3591. </ul>
  3592. ````````````````````````````````
  3593. However, an empty list item cannot interrupt a paragraph:
  3594. ```````````````````````````````` example
  3595. foo
  3596. *
  3597. foo
  3598. 1.
  3599. .
  3600. <p>foo
  3601. *</p>
  3602. <p>foo
  3603. 1.</p>
  3604. ````````````````````````````````
  3605. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3606. according to rule #1, #2, or #3, then the result of indenting each line
  3607. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3608. list item with the same contents and attributes. If a line is
  3609. empty, then it need not be indented.
  3610. Indented one space:
  3611. ```````````````````````````````` example
  3612. 1. A paragraph
  3613. with two lines.
  3614. indented code
  3615. > A block quote.
  3616. .
  3617. <ol>
  3618. <li>
  3619. <p>A paragraph
  3620. with two lines.</p>
  3621. <pre><code>indented code
  3622. </code></pre>
  3623. <blockquote>
  3624. <p>A block quote.</p>
  3625. </blockquote>
  3626. </li>
  3627. </ol>
  3628. ````````````````````````````````
  3629. Indented two spaces:
  3630. ```````````````````````````````` example
  3631. 1. A paragraph
  3632. with two lines.
  3633. indented code
  3634. > A block quote.
  3635. .
  3636. <ol>
  3637. <li>
  3638. <p>A paragraph
  3639. with two lines.</p>
  3640. <pre><code>indented code
  3641. </code></pre>
  3642. <blockquote>
  3643. <p>A block quote.</p>
  3644. </blockquote>
  3645. </li>
  3646. </ol>
  3647. ````````````````````````````````
  3648. Indented three spaces:
  3649. ```````````````````````````````` example
  3650. 1. A paragraph
  3651. with two lines.
  3652. indented code
  3653. > A block quote.
  3654. .
  3655. <ol>
  3656. <li>
  3657. <p>A paragraph
  3658. with two lines.</p>
  3659. <pre><code>indented code
  3660. </code></pre>
  3661. <blockquote>
  3662. <p>A block quote.</p>
  3663. </blockquote>
  3664. </li>
  3665. </ol>
  3666. ````````````````````````````````
  3667. Four spaces indent gives a code block:
  3668. ```````````````````````````````` example
  3669. 1. A paragraph
  3670. with two lines.
  3671. indented code
  3672. > A block quote.
  3673. .
  3674. <pre><code>1. A paragraph
  3675. with two lines.
  3676. indented code
  3677. &gt; A block quote.
  3678. </code></pre>
  3679. ````````````````````````````````
  3680. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3681. item](#list-items) with contents *Bs*, then the result of deleting
  3682. some or all of the indentation from one or more lines in which the
  3683. next [non-whitespace character] after the indentation is
  3684. [paragraph continuation text] is a
  3685. list item with the same contents and attributes. The unindented
  3686. lines are called
  3687. [lazy continuation line](@)s.
  3688. Here is an example with [lazy continuation lines]:
  3689. ```````````````````````````````` example
  3690. 1. A paragraph
  3691. with two lines.
  3692. indented code
  3693. > A block quote.
  3694. .
  3695. <ol>
  3696. <li>
  3697. <p>A paragraph
  3698. with two lines.</p>
  3699. <pre><code>indented code
  3700. </code></pre>
  3701. <blockquote>
  3702. <p>A block quote.</p>
  3703. </blockquote>
  3704. </li>
  3705. </ol>
  3706. ````````````````````````````````
  3707. Indentation can be partially deleted:
  3708. ```````````````````````````````` example
  3709. 1. A paragraph
  3710. with two lines.
  3711. .
  3712. <ol>
  3713. <li>A paragraph
  3714. with two lines.</li>
  3715. </ol>
  3716. ````````````````````````````````
  3717. These examples show how laziness can work in nested structures:
  3718. ```````````````````````````````` example
  3719. > 1. > Blockquote
  3720. continued here.
  3721. .
  3722. <blockquote>
  3723. <ol>
  3724. <li>
  3725. <blockquote>
  3726. <p>Blockquote
  3727. continued here.</p>
  3728. </blockquote>
  3729. </li>
  3730. </ol>
  3731. </blockquote>
  3732. ````````````````````````````````
  3733. ```````````````````````````````` example
  3734. > 1. > Blockquote
  3735. > continued here.
  3736. .
  3737. <blockquote>
  3738. <ol>
  3739. <li>
  3740. <blockquote>
  3741. <p>Blockquote
  3742. continued here.</p>
  3743. </blockquote>
  3744. </li>
  3745. </ol>
  3746. </blockquote>
  3747. ````````````````````````````````
  3748. 6. **That's all.** Nothing that is not counted as a list item by rules
  3749. #1--5 counts as a [list item](#list-items).
  3750. The rules for sublists follow from the general rules
  3751. [above][List items]. A sublist must be indented the same number
  3752. of spaces a paragraph would need to be in order to be included
  3753. in the list item.
  3754. So, in this case we need two spaces indent:
  3755. ```````````````````````````````` example
  3756. - foo
  3757. - bar
  3758. - baz
  3759. - boo
  3760. .
  3761. <ul>
  3762. <li>foo
  3763. <ul>
  3764. <li>bar
  3765. <ul>
  3766. <li>baz
  3767. <ul>
  3768. <li>boo</li>
  3769. </ul>
  3770. </li>
  3771. </ul>
  3772. </li>
  3773. </ul>
  3774. </li>
  3775. </ul>
  3776. ````````````````````````````````
  3777. One is not enough:
  3778. ```````````````````````````````` example
  3779. - foo
  3780. - bar
  3781. - baz
  3782. - boo
  3783. .
  3784. <ul>
  3785. <li>foo</li>
  3786. <li>bar</li>
  3787. <li>baz</li>
  3788. <li>boo</li>
  3789. </ul>
  3790. ````````````````````````````````
  3791. Here we need four, because the list marker is wider:
  3792. ```````````````````````````````` example
  3793. 10) foo
  3794. - bar
  3795. .
  3796. <ol start="10">
  3797. <li>foo
  3798. <ul>
  3799. <li>bar</li>
  3800. </ul>
  3801. </li>
  3802. </ol>
  3803. ````````````````````````````````
  3804. Three is not enough:
  3805. ```````````````````````````````` example
  3806. 10) foo
  3807. - bar
  3808. .
  3809. <ol start="10">
  3810. <li>foo</li>
  3811. </ol>
  3812. <ul>
  3813. <li>bar</li>
  3814. </ul>
  3815. ````````````````````````````````
  3816. A list may be the first block in a list item:
  3817. ```````````````````````````````` example
  3818. - - foo
  3819. .
  3820. <ul>
  3821. <li>
  3822. <ul>
  3823. <li>foo</li>
  3824. </ul>
  3825. </li>
  3826. </ul>
  3827. ````````````````````````````````
  3828. ```````````````````````````````` example
  3829. 1. - 2. foo
  3830. .
  3831. <ol>
  3832. <li>
  3833. <ul>
  3834. <li>
  3835. <ol start="2">
  3836. <li>foo</li>
  3837. </ol>
  3838. </li>
  3839. </ul>
  3840. </li>
  3841. </ol>
  3842. ````````````````````````````````
  3843. A list item can contain a heading:
  3844. ```````````````````````````````` example
  3845. - # Foo
  3846. - Bar
  3847. ---
  3848. baz
  3849. .
  3850. <ul>
  3851. <li>
  3852. <h1>Foo</h1>
  3853. </li>
  3854. <li>
  3855. <h2>Bar</h2>
  3856. baz</li>
  3857. </ul>
  3858. ````````````````````````````````
  3859. ### Motivation
  3860. John Gruber's Markdown spec says the following about list items:
  3861. 1. "List markers typically start at the left margin, but may be indented
  3862. by up to three spaces. List markers must be followed by one or more
  3863. spaces or a tab."
  3864. 2. "To make lists look nice, you can wrap items with hanging indents....
  3865. But if you don't want to, you don't have to."
  3866. 3. "List items may consist of multiple paragraphs. Each subsequent
  3867. paragraph in a list item must be indented by either 4 spaces or one
  3868. tab."
  3869. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3870. but here again, Markdown will allow you to be lazy."
  3871. 5. "To put a blockquote within a list item, the blockquote's `>`
  3872. delimiters need to be indented."
  3873. 6. "To put a code block within a list item, the code block needs to be
  3874. indented twice — 8 spaces or two tabs."
  3875. These rules specify that a paragraph under a list item must be indented
  3876. four spaces (presumably, from the left margin, rather than the start of
  3877. the list marker, but this is not said), and that code under a list item
  3878. must be indented eight spaces instead of the usual four. They also say
  3879. that a block quote must be indented, but not by how much; however, the
  3880. example given has four spaces indentation. Although nothing is said
  3881. about other kinds of block-level content, it is certainly reasonable to
  3882. infer that *all* block elements under a list item, including other
  3883. lists, must be indented four spaces. This principle has been called the
  3884. *four-space rule*.
  3885. The four-space rule is clear and principled, and if the reference
  3886. implementation `Markdown.pl` had followed it, it probably would have
  3887. become the standard. However, `Markdown.pl` allowed paragraphs and
  3888. sublists to start with only two spaces indentation, at least on the
  3889. outer level. Worse, its behavior was inconsistent: a sublist of an
  3890. outer-level list needed two spaces indentation, but a sublist of this
  3891. sublist needed three spaces. It is not surprising, then, that different
  3892. implementations of Markdown have developed very different rules for
  3893. determining what comes under a list item. (Pandoc and python-Markdown,
  3894. for example, stuck with Gruber's syntax description and the four-space
  3895. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3896. followed `Markdown.pl`'s behavior more closely.)
  3897. Unfortunately, given the divergences between implementations, there
  3898. is no way to give a spec for list items that will be guaranteed not
  3899. to break any existing documents. However, the spec given here should
  3900. correctly handle lists formatted with either the four-space rule or
  3901. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3902. in a way that is natural for a human to read.
  3903. The strategy here is to let the width and indentation of the list marker
  3904. determine the indentation necessary for blocks to fall under the list
  3905. item, rather than having a fixed and arbitrary number. The writer can
  3906. think of the body of the list item as a unit which gets indented to the
  3907. right enough to fit the list marker (and any indentation on the list
  3908. marker). (The laziness rule, #5, then allows continuation lines to be
  3909. unindented if needed.)
  3910. This rule is superior, we claim, to any rule requiring a fixed level of
  3911. indentation from the margin. The four-space rule is clear but
  3912. unnatural. It is quite unintuitive that
  3913. ``` markdown
  3914. - foo
  3915. bar
  3916. - baz
  3917. ```
  3918. should be parsed as two lists with an intervening paragraph,
  3919. ``` html
  3920. <ul>
  3921. <li>foo</li>
  3922. </ul>
  3923. <p>bar</p>
  3924. <ul>
  3925. <li>baz</li>
  3926. </ul>
  3927. ```
  3928. as the four-space rule demands, rather than a single list,
  3929. ``` html
  3930. <ul>
  3931. <li>
  3932. <p>foo</p>
  3933. <p>bar</p>
  3934. <ul>
  3935. <li>baz</li>
  3936. </ul>
  3937. </li>
  3938. </ul>
  3939. ```
  3940. The choice of four spaces is arbitrary. It can be learned, but it is
  3941. not likely to be guessed, and it trips up beginners regularly.
  3942. Would it help to adopt a two-space rule? The problem is that such
  3943. a rule, together with the rule allowing 1--3 spaces indentation of the
  3944. initial list marker, allows text that is indented *less than* the
  3945. original list marker to be included in the list item. For example,
  3946. `Markdown.pl` parses
  3947. ``` markdown
  3948. - one
  3949. two
  3950. ```
  3951. as a single list item, with `two` a continuation paragraph:
  3952. ``` html
  3953. <ul>
  3954. <li>
  3955. <p>one</p>
  3956. <p>two</p>
  3957. </li>
  3958. </ul>
  3959. ```
  3960. and similarly
  3961. ``` markdown
  3962. > - one
  3963. >
  3964. > two
  3965. ```
  3966. as
  3967. ``` html
  3968. <blockquote>
  3969. <ul>
  3970. <li>
  3971. <p>one</p>
  3972. <p>two</p>
  3973. </li>
  3974. </ul>
  3975. </blockquote>
  3976. ```
  3977. This is extremely unintuitive.
  3978. Rather than requiring a fixed indent from the margin, we could require
  3979. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3980. may itself be indented). This proposal would remove the last anomaly
  3981. discussed. Unlike the spec presented above, it would count the following
  3982. as a list item with a subparagraph, even though the paragraph `bar`
  3983. is not indented as far as the first paragraph `foo`:
  3984. ``` markdown
  3985. 10. foo
  3986. bar
  3987. ```
  3988. Arguably this text does read like a list item with `bar` as a subparagraph,
  3989. which may count in favor of the proposal. However, on this proposal indented
  3990. code would have to be indented six spaces after the list marker. And this
  3991. would break a lot of existing Markdown, which has the pattern:
  3992. ``` markdown
  3993. 1. foo
  3994. indented code
  3995. ```
  3996. where the code is indented eight spaces. The spec above, by contrast, will
  3997. parse this text as expected, since the code block's indentation is measured
  3998. from the beginning of `foo`.
  3999. The one case that needs special treatment is a list item that *starts*
  4000. with indented code. How much indentation is required in that case, since
  4001. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  4002. that in such cases, we require one space indentation from the list marker
  4003. (and then the normal four spaces for the indented code). This will match the
  4004. four-space rule in cases where the list marker plus its initial indentation
  4005. takes four spaces (a common case), but diverge in other cases.
  4006. ## Lists
  4007. A [list](@) is a sequence of one or more
  4008. list items [of the same type]. The list items
  4009. may be separated by any number of blank lines.
  4010. Two list items are [of the same type](@)
  4011. if they begin with a [list marker] of the same type.
  4012. Two list markers are of the
  4013. same type if (a) they are bullet list markers using the same character
  4014. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  4015. delimiter (either `.` or `)`).
  4016. A list is an [ordered list](@)
  4017. if its constituent list items begin with
  4018. [ordered list markers], and a
  4019. [bullet list](@) if its constituent list
  4020. items begin with [bullet list markers].
  4021. The [start number](@)
  4022. of an [ordered list] is determined by the list number of
  4023. its initial list item. The numbers of subsequent list items are
  4024. disregarded.
  4025. A list is [loose](@) if any of its constituent
  4026. list items are separated by blank lines, or if any of its constituent
  4027. list items directly contain two block-level elements with a blank line
  4028. between them. Otherwise a list is [tight](@).
  4029. (The difference in HTML output is that paragraphs in a loose list are
  4030. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  4031. Changing the bullet or ordered list delimiter starts a new list:
  4032. ```````````````````````````````` example
  4033. - foo
  4034. - bar
  4035. + baz
  4036. .
  4037. <ul>
  4038. <li>foo</li>
  4039. <li>bar</li>
  4040. </ul>
  4041. <ul>
  4042. <li>baz</li>
  4043. </ul>
  4044. ````````````````````````````````
  4045. ```````````````````````````````` example
  4046. 1. foo
  4047. 2. bar
  4048. 3) baz
  4049. .
  4050. <ol>
  4051. <li>foo</li>
  4052. <li>bar</li>
  4053. </ol>
  4054. <ol start="3">
  4055. <li>baz</li>
  4056. </ol>
  4057. ````````````````````````````````
  4058. In CommonMark, a list can interrupt a paragraph. That is,
  4059. no blank line is needed to separate a paragraph from a following
  4060. list:
  4061. ```````````````````````````````` example
  4062. Foo
  4063. - bar
  4064. - baz
  4065. .
  4066. <p>Foo</p>
  4067. <ul>
  4068. <li>bar</li>
  4069. <li>baz</li>
  4070. </ul>
  4071. ````````````````````````````````
  4072. `Markdown.pl` does not allow this, through fear of triggering a list
  4073. via a numeral in a hard-wrapped line:
  4074. ``` markdown
  4075. The number of windows in my house is
  4076. 14. The number of doors is 6.
  4077. ```
  4078. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  4079. interrupt a paragraph, even though the same considerations might
  4080. apply.
  4081. In CommonMark, we do allow lists to interrupt paragraphs, for
  4082. two reasons. First, it is natural and not uncommon for people
  4083. to start lists without blank lines:
  4084. ``` markdown
  4085. I need to buy
  4086. - new shoes
  4087. - a coat
  4088. - a plane ticket
  4089. ```
  4090. Second, we are attracted to a
  4091. > [principle of uniformity](@):
  4092. > if a chunk of text has a certain
  4093. > meaning, it will continue to have the same meaning when put into a
  4094. > container block (such as a list item or blockquote).
  4095. (Indeed, the spec for [list items] and [block quotes] presupposes
  4096. this principle.) This principle implies that if
  4097. ``` markdown
  4098. * I need to buy
  4099. - new shoes
  4100. - a coat
  4101. - a plane ticket
  4102. ```
  4103. is a list item containing a paragraph followed by a nested sublist,
  4104. as all Markdown implementations agree it is (though the paragraph
  4105. may be rendered without `<p>` tags, since the list is "tight"),
  4106. then
  4107. ``` markdown
  4108. I need to buy
  4109. - new shoes
  4110. - a coat
  4111. - a plane ticket
  4112. ```
  4113. by itself should be a paragraph followed by a nested sublist.
  4114. Since it is well established Markdown practice to allow lists to
  4115. interrupt paragraphs inside list items, the [principle of
  4116. uniformity] requires us to allow this outside list items as
  4117. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  4118. takes a different approach, requiring blank lines before lists
  4119. even inside other list items.)
  4120. In order to solve of unwanted lists in paragraphs with
  4121. hard-wrapped numerals, we allow only lists starting with `1` to
  4122. interrupt paragraphs. Thus,
  4123. ```````````````````````````````` example
  4124. The number of windows in my house is
  4125. 14. The number of doors is 6.
  4126. .
  4127. <p>The number of windows in my house is
  4128. 14. The number of doors is 6.</p>
  4129. ````````````````````````````````
  4130. We may still get an unintended result in cases like
  4131. ```````````````````````````````` example
  4132. The number of windows in my house is
  4133. 1. The number of doors is 6.
  4134. .
  4135. <p>The number of windows in my house is</p>
  4136. <ol>
  4137. <li>The number of doors is 6.</li>
  4138. </ol>
  4139. ````````````````````````````````
  4140. but this rule should prevent most spurious list captures.
  4141. There can be any number of blank lines between items:
  4142. ```````````````````````````````` example
  4143. - foo
  4144. - bar
  4145. - baz
  4146. .
  4147. <ul>
  4148. <li>
  4149. <p>foo</p>
  4150. </li>
  4151. <li>
  4152. <p>bar</p>
  4153. </li>
  4154. <li>
  4155. <p>baz</p>
  4156. </li>
  4157. </ul>
  4158. ````````````````````````````````
  4159. ```````````````````````````````` example
  4160. - foo
  4161. - bar
  4162. - baz
  4163. bim
  4164. .
  4165. <ul>
  4166. <li>foo
  4167. <ul>
  4168. <li>bar
  4169. <ul>
  4170. <li>
  4171. <p>baz</p>
  4172. <p>bim</p>
  4173. </li>
  4174. </ul>
  4175. </li>
  4176. </ul>
  4177. </li>
  4178. </ul>
  4179. ````````````````````````````````
  4180. To separate consecutive lists of the same type, or to separate a
  4181. list from an indented code block that would otherwise be parsed
  4182. as a subparagraph of the final list item, you can insert a blank HTML
  4183. comment:
  4184. ```````````````````````````````` example
  4185. - foo
  4186. - bar
  4187. <!-- -->
  4188. - baz
  4189. - bim
  4190. .
  4191. <ul>
  4192. <li>foo</li>
  4193. <li>bar</li>
  4194. </ul>
  4195. <!-- -->
  4196. <ul>
  4197. <li>baz</li>
  4198. <li>bim</li>
  4199. </ul>
  4200. ````````````````````````````````
  4201. ```````````````````````````````` example
  4202. - foo
  4203. notcode
  4204. - foo
  4205. <!-- -->
  4206. code
  4207. .
  4208. <ul>
  4209. <li>
  4210. <p>foo</p>
  4211. <p>notcode</p>
  4212. </li>
  4213. <li>
  4214. <p>foo</p>
  4215. </li>
  4216. </ul>
  4217. <!-- -->
  4218. <pre><code>code
  4219. </code></pre>
  4220. ````````````````````````````````
  4221. List items need not be indented to the same level. The following
  4222. list items will be treated as items at the same list level,
  4223. since none is indented enough to belong to the previous list
  4224. item:
  4225. ```````````````````````````````` example
  4226. - a
  4227. - b
  4228. - c
  4229. - d
  4230. - e
  4231. - f
  4232. - g
  4233. .
  4234. <ul>
  4235. <li>a</li>
  4236. <li>b</li>
  4237. <li>c</li>
  4238. <li>d</li>
  4239. <li>e</li>
  4240. <li>f</li>
  4241. <li>g</li>
  4242. </ul>
  4243. ````````````````````````````````
  4244. ```````````````````````````````` example
  4245. 1. a
  4246. 2. b
  4247. 3. c
  4248. .
  4249. <ol>
  4250. <li>
  4251. <p>a</p>
  4252. </li>
  4253. <li>
  4254. <p>b</p>
  4255. </li>
  4256. <li>
  4257. <p>c</p>
  4258. </li>
  4259. </ol>
  4260. ````````````````````````````````
  4261. Note, however, that list items may not be indented more than
  4262. three spaces. Here `- e` is treated as a paragraph continuation
  4263. line, because it is indented more than three spaces:
  4264. ```````````````````````````````` example
  4265. - a
  4266. - b
  4267. - c
  4268. - d
  4269. - e
  4270. .
  4271. <ul>
  4272. <li>a</li>
  4273. <li>b</li>
  4274. <li>c</li>
  4275. <li>d
  4276. - e</li>
  4277. </ul>
  4278. ````````````````````````````````
  4279. And here, `3. c` is treated as in indented code block,
  4280. because it is indented four spaces and preceded by a
  4281. blank line.
  4282. ```````````````````````````````` example
  4283. 1. a
  4284. 2. b
  4285. 3. c
  4286. .
  4287. <ol>
  4288. <li>
  4289. <p>a</p>
  4290. </li>
  4291. <li>
  4292. <p>b</p>
  4293. </li>
  4294. </ol>
  4295. <pre><code>3. c
  4296. </code></pre>
  4297. ````````````````````````````````
  4298. This is a loose list, because there is a blank line between
  4299. two of the list items:
  4300. ```````````````````````````````` example
  4301. - a
  4302. - b
  4303. - c
  4304. .
  4305. <ul>
  4306. <li>
  4307. <p>a</p>
  4308. </li>
  4309. <li>
  4310. <p>b</p>
  4311. </li>
  4312. <li>
  4313. <p>c</p>
  4314. </li>
  4315. </ul>
  4316. ````````````````````````````````
  4317. So is this, with a empty second item:
  4318. ```````````````````````````````` example
  4319. * a
  4320. *
  4321. * c
  4322. .
  4323. <ul>
  4324. <li>
  4325. <p>a</p>
  4326. </li>
  4327. <li></li>
  4328. <li>
  4329. <p>c</p>
  4330. </li>
  4331. </ul>
  4332. ````````````````````````````````
  4333. These are loose lists, even though there is no space between the items,
  4334. because one of the items directly contains two block-level elements
  4335. with a blank line between them:
  4336. ```````````````````````````````` example
  4337. - a
  4338. - b
  4339. c
  4340. - d
  4341. .
  4342. <ul>
  4343. <li>
  4344. <p>a</p>
  4345. </li>
  4346. <li>
  4347. <p>b</p>
  4348. <p>c</p>
  4349. </li>
  4350. <li>
  4351. <p>d</p>
  4352. </li>
  4353. </ul>
  4354. ````````````````````````````````
  4355. ```````````````````````````````` example
  4356. - a
  4357. - b
  4358. [ref]: /url
  4359. - d
  4360. .
  4361. <ul>
  4362. <li>
  4363. <p>a</p>
  4364. </li>
  4365. <li>
  4366. <p>b</p>
  4367. </li>
  4368. <li>
  4369. <p>d</p>
  4370. </li>
  4371. </ul>
  4372. ````````````````````````````````
  4373. This is a tight list, because the blank lines are in a code block:
  4374. ```````````````````````````````` example
  4375. - a
  4376. - ```
  4377. b
  4378. ```
  4379. - c
  4380. .
  4381. <ul>
  4382. <li>a</li>
  4383. <li>
  4384. <pre><code>b
  4385. </code></pre>
  4386. </li>
  4387. <li>c</li>
  4388. </ul>
  4389. ````````````````````````````````
  4390. This is a tight list, because the blank line is between two
  4391. paragraphs of a sublist. So the sublist is loose while
  4392. the outer list is tight:
  4393. ```````````````````````````````` example
  4394. - a
  4395. - b
  4396. c
  4397. - d
  4398. .
  4399. <ul>
  4400. <li>a
  4401. <ul>
  4402. <li>
  4403. <p>b</p>
  4404. <p>c</p>
  4405. </li>
  4406. </ul>
  4407. </li>
  4408. <li>d</li>
  4409. </ul>
  4410. ````````````````````````````````
  4411. This is a tight list, because the blank line is inside the
  4412. block quote:
  4413. ```````````````````````````````` example
  4414. * a
  4415. > b
  4416. >
  4417. * c
  4418. .
  4419. <ul>
  4420. <li>a
  4421. <blockquote>
  4422. <p>b</p>
  4423. </blockquote>
  4424. </li>
  4425. <li>c</li>
  4426. </ul>
  4427. ````````````````````````````````
  4428. This list is tight, because the consecutive block elements
  4429. are not separated by blank lines:
  4430. ```````````````````````````````` example
  4431. - a
  4432. > b
  4433. ```
  4434. c
  4435. ```
  4436. - d
  4437. .
  4438. <ul>
  4439. <li>a
  4440. <blockquote>
  4441. <p>b</p>
  4442. </blockquote>
  4443. <pre><code>c
  4444. </code></pre>
  4445. </li>
  4446. <li>d</li>
  4447. </ul>
  4448. ````````````````````````````````
  4449. A single-paragraph list is tight:
  4450. ```````````````````````````````` example
  4451. - a
  4452. .
  4453. <ul>
  4454. <li>a</li>
  4455. </ul>
  4456. ````````````````````````````````
  4457. ```````````````````````````````` example
  4458. - a
  4459. - b
  4460. .
  4461. <ul>
  4462. <li>a
  4463. <ul>
  4464. <li>b</li>
  4465. </ul>
  4466. </li>
  4467. </ul>
  4468. ````````````````````````````````
  4469. This list is loose, because of the blank line between the
  4470. two block elements in the list item:
  4471. ```````````````````````````````` example
  4472. 1. ```
  4473. foo
  4474. ```
  4475. bar
  4476. .
  4477. <ol>
  4478. <li>
  4479. <pre><code>foo
  4480. </code></pre>
  4481. <p>bar</p>
  4482. </li>
  4483. </ol>
  4484. ````````````````````````````````
  4485. Here the outer list is loose, the inner list tight:
  4486. ```````````````````````````````` example
  4487. * foo
  4488. * bar
  4489. baz
  4490. .
  4491. <ul>
  4492. <li>
  4493. <p>foo</p>
  4494. <ul>
  4495. <li>bar</li>
  4496. </ul>
  4497. <p>baz</p>
  4498. </li>
  4499. </ul>
  4500. ````````````````````````````````
  4501. ```````````````````````````````` example
  4502. - a
  4503. - b
  4504. - c
  4505. - d
  4506. - e
  4507. - f
  4508. .
  4509. <ul>
  4510. <li>
  4511. <p>a</p>
  4512. <ul>
  4513. <li>b</li>
  4514. <li>c</li>
  4515. </ul>
  4516. </li>
  4517. <li>
  4518. <p>d</p>
  4519. <ul>
  4520. <li>e</li>
  4521. <li>f</li>
  4522. </ul>
  4523. </li>
  4524. </ul>
  4525. ````````````````````````````````
  4526. # Inlines
  4527. Inlines are parsed sequentially from the beginning of the character
  4528. stream to the end (left to right, in left-to-right languages).
  4529. Thus, for example, in
  4530. ```````````````````````````````` example
  4531. `hi`lo`
  4532. .
  4533. <p><code>hi</code>lo`</p>
  4534. ````````````````````````````````
  4535. `hi` is parsed as code, leaving the backtick at the end as a literal
  4536. backtick.
  4537. ## Code spans
  4538. A [backtick string](@)
  4539. is a string of one or more backtick characters (`` ` ``) that is neither
  4540. preceded nor followed by a backtick.
  4541. A [code span](@) begins with a backtick string and ends with
  4542. a backtick string of equal length. The contents of the code span are
  4543. the characters between these two backtick strings, normalized in the
  4544. following ways:
  4545. - First, [line endings] are converted to [spaces].
  4546. - If the resulting string both begins *and* ends with a [space]
  4547. character, but does not consist entirely of [space]
  4548. characters, a single [space] character is removed from the
  4549. front and back. This allows you to include code that begins
  4550. or ends with backtick characters, which must be separated by
  4551. whitespace from the opening or closing backtick strings.
  4552. This is a simple code span:
  4553. ```````````````````````````````` example
  4554. `foo`
  4555. .
  4556. <p><code>foo</code></p>
  4557. ````````````````````````````````
  4558. Here two backticks are used, because the code contains a backtick.
  4559. This example also illustrates stripping of a single leading and
  4560. trailing space:
  4561. ```````````````````````````````` example
  4562. `` foo ` bar ``
  4563. .
  4564. <p><code>foo ` bar</code></p>
  4565. ````````````````````````````````
  4566. This example shows the motivation for stripping leading and trailing
  4567. spaces:
  4568. ```````````````````````````````` example
  4569. ` `` `
  4570. .
  4571. <p><code>``</code></p>
  4572. ````````````````````````````````
  4573. Note that only *one* space is stripped:
  4574. ```````````````````````````````` example
  4575. ` `` `
  4576. .
  4577. <p><code> `` </code></p>
  4578. ````````````````````````````````
  4579. The stripping only happens if the space is on both
  4580. sides of the string:
  4581. ```````````````````````````````` example
  4582. ` a`
  4583. .
  4584. <p><code> a</code></p>
  4585. ````````````````````````````````
  4586. Only [spaces], and not [unicode whitespace] in general, are
  4587. stripped in this way:
  4588. ```````````````````````````````` example
  4589. ` b `
  4590. .
  4591. <p><code> b </code></p>
  4592. ````````````````````````````````
  4593. No stripping occurs if the code span contains only spaces:
  4594. ```````````````````````````````` example
  4595. ` `
  4596. ` `
  4597. .
  4598. <p><code> </code>
  4599. <code> </code></p>
  4600. ````````````````````````````````
  4601. [Line endings] are treated like spaces:
  4602. ```````````````````````````````` example
  4603. ``
  4604. foo
  4605. bar
  4606. baz
  4607. ``
  4608. .
  4609. <p><code>foo bar baz</code></p>
  4610. ````````````````````````````````
  4611. ```````````````````````````````` example
  4612. ``
  4613. foo
  4614. ``
  4615. .
  4616. <p><code>foo </code></p>
  4617. ````````````````````````````````
  4618. Interior spaces are not collapsed:
  4619. ```````````````````````````````` example
  4620. `foo bar
  4621. baz`
  4622. .
  4623. <p><code>foo bar baz</code></p>
  4624. ````````````````````````````````
  4625. Note that browsers will typically collapse consecutive spaces
  4626. when rendering `<code>` elements, so it is recommended that
  4627. the following CSS be used:
  4628. code{white-space: pre-wrap;}
  4629. Note that backslash escapes do not work in code spans. All backslashes
  4630. are treated literally:
  4631. ```````````````````````````````` example
  4632. `foo\`bar`
  4633. .
  4634. <p><code>foo\</code>bar`</p>
  4635. ````````````````````````````````
  4636. Backslash escapes are never needed, because one can always choose a
  4637. string of *n* backtick characters as delimiters, where the code does
  4638. not contain any strings of exactly *n* backtick characters.
  4639. ```````````````````````````````` example
  4640. ``foo`bar``
  4641. .
  4642. <p><code>foo`bar</code></p>
  4643. ````````````````````````````````
  4644. ```````````````````````````````` example
  4645. ` foo `` bar `
  4646. .
  4647. <p><code>foo `` bar</code></p>
  4648. ````````````````````````````````
  4649. Code span backticks have higher precedence than any other inline
  4650. constructs except HTML tags and autolinks. Thus, for example, this is
  4651. not parsed as emphasized text, since the second `*` is part of a code
  4652. span:
  4653. ```````````````````````````````` example
  4654. *foo`*`
  4655. .
  4656. <p>*foo<code>*</code></p>
  4657. ````````````````````````````````
  4658. And this is not parsed as a link:
  4659. ```````````````````````````````` example
  4660. [not a `link](/foo`)
  4661. .
  4662. <p>[not a <code>link](/foo</code>)</p>
  4663. ````````````````````````````````
  4664. Code spans, HTML tags, and autolinks have the same precedence.
  4665. Thus, this is code:
  4666. ```````````````````````````````` example
  4667. `<a href="`">`
  4668. .
  4669. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4670. ````````````````````````````````
  4671. But this is an HTML tag:
  4672. ```````````````````````````````` example
  4673. <a href="`">`
  4674. .
  4675. <p><a href="`">`</p>
  4676. ````````````````````````````````
  4677. And this is code:
  4678. ```````````````````````````````` example
  4679. `<http://foo.bar.`baz>`
  4680. .
  4681. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4682. ````````````````````````````````
  4683. But this is an autolink:
  4684. ```````````````````````````````` example
  4685. <http://foo.bar.`baz>`
  4686. .
  4687. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4688. ````````````````````````````````
  4689. When a backtick string is not closed by a matching backtick string,
  4690. we just have literal backticks:
  4691. ```````````````````````````````` example
  4692. ```foo``
  4693. .
  4694. <p>```foo``</p>
  4695. ````````````````````````````````
  4696. ```````````````````````````````` example
  4697. `foo
  4698. .
  4699. <p>`foo</p>
  4700. ````````````````````````````````
  4701. The following case also illustrates the need for opening and
  4702. closing backtick strings to be equal in length:
  4703. ```````````````````````````````` example
  4704. `foo``bar``
  4705. .
  4706. <p>`foo<code>bar</code></p>
  4707. ````````````````````````````````
  4708. ## Emphasis and strong emphasis
  4709. John Gruber's original [Markdown syntax
  4710. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4711. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4712. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4713. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4714. > tag.
  4715. This is enough for most users, but these rules leave much undecided,
  4716. especially when it comes to nested emphasis. The original
  4717. `Markdown.pl` test suite makes it clear that triple `***` and
  4718. `___` delimiters can be used for strong emphasis, and most
  4719. implementations have also allowed the following patterns:
  4720. ``` markdown
  4721. ***strong emph***
  4722. ***strong** in emph*
  4723. ***emph* in strong**
  4724. **in strong *emph***
  4725. *in emph **strong***
  4726. ```
  4727. The following patterns are less widely supported, but the intent
  4728. is clear and they are useful (especially in contexts like bibliography
  4729. entries):
  4730. ``` markdown
  4731. *emph *with emph* in it*
  4732. **strong **with strong** in it**
  4733. ```
  4734. Many implementations have also restricted intraword emphasis to
  4735. the `*` forms, to avoid unwanted emphasis in words containing
  4736. internal underscores. (It is best practice to put these in code
  4737. spans, but users often do not.)
  4738. ``` markdown
  4739. internal emphasis: foo*bar*baz
  4740. no emphasis: foo_bar_baz
  4741. ```
  4742. The rules given below capture all of these patterns, while allowing
  4743. for efficient parsing strategies that do not backtrack.
  4744. First, some definitions. A [delimiter run](@) is either
  4745. a sequence of one or more `*` characters that is not preceded or
  4746. followed by a non-backslash-escaped `*` character, or a sequence
  4747. of one or more `_` characters that is not preceded or followed by
  4748. a non-backslash-escaped `_` character.
  4749. A [left-flanking delimiter run](@) is
  4750. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4751. and either (2a) not followed by a [punctuation character], or
  4752. (2b) followed by a [punctuation character] and
  4753. preceded by [Unicode whitespace] or a [punctuation character].
  4754. For purposes of this definition, the beginning and the end of
  4755. the line count as Unicode whitespace.
  4756. A [right-flanking delimiter run](@) is
  4757. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4758. and either (2a) not preceded by a [punctuation character], or
  4759. (2b) preceded by a [punctuation character] and
  4760. followed by [Unicode whitespace] or a [punctuation character].
  4761. For purposes of this definition, the beginning and the end of
  4762. the line count as Unicode whitespace.
  4763. Here are some examples of delimiter runs.
  4764. - left-flanking but not right-flanking:
  4765. ```
  4766. ***abc
  4767. _abc
  4768. **"abc"
  4769. _"abc"
  4770. ```
  4771. - right-flanking but not left-flanking:
  4772. ```
  4773. abc***
  4774. abc_
  4775. "abc"**
  4776. "abc"_
  4777. ```
  4778. - Both left and right-flanking:
  4779. ```
  4780. abc***def
  4781. "abc"_"def"
  4782. ```
  4783. - Neither left nor right-flanking:
  4784. ```
  4785. abc *** def
  4786. a _ b
  4787. ```
  4788. (The idea of distinguishing left-flanking and right-flanking
  4789. delimiter runs based on the character before and the character
  4790. after comes from Roopesh Chander's
  4791. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4792. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4793. run," and its rules for distinguishing left- and right-flanking runs
  4794. are a bit more complex than the ones given here.)
  4795. The following rules define emphasis and strong emphasis:
  4796. 1. A single `*` character [can open emphasis](@)
  4797. iff (if and only if) it is part of a [left-flanking delimiter run].
  4798. 2. A single `_` character [can open emphasis] iff
  4799. it is part of a [left-flanking delimiter run]
  4800. and either (a) not part of a [right-flanking delimiter run]
  4801. or (b) part of a [right-flanking delimiter run]
  4802. preceded by punctuation.
  4803. 3. A single `*` character [can close emphasis](@)
  4804. iff it is part of a [right-flanking delimiter run].
  4805. 4. A single `_` character [can close emphasis] iff
  4806. it is part of a [right-flanking delimiter run]
  4807. and either (a) not part of a [left-flanking delimiter run]
  4808. or (b) part of a [left-flanking delimiter run]
  4809. followed by punctuation.
  4810. 5. A double `**` [can open strong emphasis](@)
  4811. iff it is part of a [left-flanking delimiter run].
  4812. 6. A double `__` [can open strong emphasis] iff
  4813. it is part of a [left-flanking delimiter run]
  4814. and either (a) not part of a [right-flanking delimiter run]
  4815. or (b) part of a [right-flanking delimiter run]
  4816. preceded by punctuation.
  4817. 7. A double `**` [can close strong emphasis](@)
  4818. iff it is part of a [right-flanking delimiter run].
  4819. 8. A double `__` [can close strong emphasis] iff
  4820. it is part of a [right-flanking delimiter run]
  4821. and either (a) not part of a [left-flanking delimiter run]
  4822. or (b) part of a [left-flanking delimiter run]
  4823. followed by punctuation.
  4824. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4825. with a delimiter that [can close emphasis], and that uses the same
  4826. character (`_` or `*`) as the opening delimiter. The
  4827. opening and closing delimiters must belong to separate
  4828. [delimiter runs]. If one of the delimiters can both
  4829. open and close emphasis, then the sum of the lengths of the
  4830. delimiter runs containing the opening and closing delimiters
  4831. must not be a multiple of 3 unless both lengths are
  4832. multiples of 3.
  4833. 10. Strong emphasis begins with a delimiter that
  4834. [can open strong emphasis] and ends with a delimiter that
  4835. [can close strong emphasis], and that uses the same character
  4836. (`_` or `*`) as the opening delimiter. The
  4837. opening and closing delimiters must belong to separate
  4838. [delimiter runs]. If one of the delimiters can both open
  4839. and close strong emphasis, then the sum of the lengths of
  4840. the delimiter runs containing the opening and closing
  4841. delimiters must not be a multiple of 3 unless both lengths
  4842. are multiples of 3.
  4843. 11. A literal `*` character cannot occur at the beginning or end of
  4844. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4845. is backslash-escaped.
  4846. 12. A literal `_` character cannot occur at the beginning or end of
  4847. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4848. is backslash-escaped.
  4849. Where rules 1--12 above are compatible with multiple parsings,
  4850. the following principles resolve ambiguity:
  4851. 13. The number of nestings should be minimized. Thus, for example,
  4852. an interpretation `<strong>...</strong>` is always preferred to
  4853. `<em><em>...</em></em>`.
  4854. 14. An interpretation `<em><strong>...</strong></em>` is always
  4855. preferred to `<strong><em>...</em></strong>`.
  4856. 15. When two potential emphasis or strong emphasis spans overlap,
  4857. so that the second begins before the first ends and ends after
  4858. the first ends, the first takes precedence. Thus, for example,
  4859. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4860. than `*foo <em>bar* baz</em>`.
  4861. 16. When there are two potential emphasis or strong emphasis spans
  4862. with the same closing delimiter, the shorter one (the one that
  4863. opens later) takes precedence. Thus, for example,
  4864. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4865. rather than `<strong>foo **bar baz</strong>`.
  4866. 17. Inline code spans, links, images, and HTML tags group more tightly
  4867. than emphasis. So, when there is a choice between an interpretation
  4868. that contains one of these elements and one that does not, the
  4869. former always wins. Thus, for example, `*[foo*](bar)` is
  4870. parsed as `*<a href="bar">foo*</a>` rather than as
  4871. `<em>[foo</em>](bar)`.
  4872. These rules can be illustrated through a series of examples.
  4873. Rule 1:
  4874. ```````````````````````````````` example
  4875. *foo bar*
  4876. .
  4877. <p><em>foo bar</em></p>
  4878. ````````````````````````````````
  4879. This is not emphasis, because the opening `*` is followed by
  4880. whitespace, and hence not part of a [left-flanking delimiter run]:
  4881. ```````````````````````````````` example
  4882. a * foo bar*
  4883. .
  4884. <p>a * foo bar*</p>
  4885. ````````````````````````````````
  4886. This is not emphasis, because the opening `*` is preceded
  4887. by an alphanumeric and followed by punctuation, and hence
  4888. not part of a [left-flanking delimiter run]:
  4889. ```````````````````````````````` example
  4890. a*"foo"*
  4891. .
  4892. <p>a*&quot;foo&quot;*</p>
  4893. ````````````````````````````````
  4894. Unicode nonbreaking spaces count as whitespace, too:
  4895. ```````````````````````````````` example
  4896. * a *
  4897. .
  4898. <p>* a *</p>
  4899. ````````````````````````````````
  4900. Intraword emphasis with `*` is permitted:
  4901. ```````````````````````````````` example
  4902. foo*bar*
  4903. .
  4904. <p>foo<em>bar</em></p>
  4905. ````````````````````````````````
  4906. ```````````````````````````````` example
  4907. 5*6*78
  4908. .
  4909. <p>5<em>6</em>78</p>
  4910. ````````````````````````````````
  4911. Rule 2:
  4912. ```````````````````````````````` example
  4913. _foo bar_
  4914. .
  4915. <p><em>foo bar</em></p>
  4916. ````````````````````````````````
  4917. This is not emphasis, because the opening `_` is followed by
  4918. whitespace:
  4919. ```````````````````````````````` example
  4920. _ foo bar_
  4921. .
  4922. <p>_ foo bar_</p>
  4923. ````````````````````````````````
  4924. This is not emphasis, because the opening `_` is preceded
  4925. by an alphanumeric and followed by punctuation:
  4926. ```````````````````````````````` example
  4927. a_"foo"_
  4928. .
  4929. <p>a_&quot;foo&quot;_</p>
  4930. ````````````````````````````````
  4931. Emphasis with `_` is not allowed inside words:
  4932. ```````````````````````````````` example
  4933. foo_bar_
  4934. .
  4935. <p>foo_bar_</p>
  4936. ````````````````````````````````
  4937. ```````````````````````````````` example
  4938. 5_6_78
  4939. .
  4940. <p>5_6_78</p>
  4941. ````````````````````````````````
  4942. ```````````````````````````````` example
  4943. пристаням_стремятся_
  4944. .
  4945. <p>пристаням_стремятся_</p>
  4946. ````````````````````````````````
  4947. Here `_` does not generate emphasis, because the first delimiter run
  4948. is right-flanking and the second left-flanking:
  4949. ```````````````````````````````` example
  4950. aa_"bb"_cc
  4951. .
  4952. <p>aa_&quot;bb&quot;_cc</p>
  4953. ````````````````````````````````
  4954. This is emphasis, even though the opening delimiter is
  4955. both left- and right-flanking, because it is preceded by
  4956. punctuation:
  4957. ```````````````````````````````` example
  4958. foo-_(bar)_
  4959. .
  4960. <p>foo-<em>(bar)</em></p>
  4961. ````````````````````````````````
  4962. Rule 3:
  4963. This is not emphasis, because the closing delimiter does
  4964. not match the opening delimiter:
  4965. ```````````````````````````````` example
  4966. _foo*
  4967. .
  4968. <p>_foo*</p>
  4969. ````````````````````````````````
  4970. This is not emphasis, because the closing `*` is preceded by
  4971. whitespace:
  4972. ```````````````````````````````` example
  4973. *foo bar *
  4974. .
  4975. <p>*foo bar *</p>
  4976. ````````````````````````````````
  4977. A newline also counts as whitespace:
  4978. ```````````````````````````````` example
  4979. *foo bar
  4980. *
  4981. .
  4982. <p>*foo bar
  4983. *</p>
  4984. ````````````````````````````````
  4985. This is not emphasis, because the second `*` is
  4986. preceded by punctuation and followed by an alphanumeric
  4987. (hence it is not part of a [right-flanking delimiter run]:
  4988. ```````````````````````````````` example
  4989. *(*foo)
  4990. .
  4991. <p>*(*foo)</p>
  4992. ````````````````````````````````
  4993. The point of this restriction is more easily appreciated
  4994. with this example:
  4995. ```````````````````````````````` example
  4996. *(*foo*)*
  4997. .
  4998. <p><em>(<em>foo</em>)</em></p>
  4999. ````````````````````````````````
  5000. Intraword emphasis with `*` is allowed:
  5001. ```````````````````````````````` example
  5002. *foo*bar
  5003. .
  5004. <p><em>foo</em>bar</p>
  5005. ````````````````````````````````
  5006. Rule 4:
  5007. This is not emphasis, because the closing `_` is preceded by
  5008. whitespace:
  5009. ```````````````````````````````` example
  5010. _foo bar _
  5011. .
  5012. <p>_foo bar _</p>
  5013. ````````````````````````````````
  5014. This is not emphasis, because the second `_` is
  5015. preceded by punctuation and followed by an alphanumeric:
  5016. ```````````````````````````````` example
  5017. _(_foo)
  5018. .
  5019. <p>_(_foo)</p>
  5020. ````````````````````````````````
  5021. This is emphasis within emphasis:
  5022. ```````````````````````````````` example
  5023. _(_foo_)_
  5024. .
  5025. <p><em>(<em>foo</em>)</em></p>
  5026. ````````````````````````````````
  5027. Intraword emphasis is disallowed for `_`:
  5028. ```````````````````````````````` example
  5029. _foo_bar
  5030. .
  5031. <p>_foo_bar</p>
  5032. ````````````````````````````````
  5033. ```````````````````````````````` example
  5034. _пристаням_стремятся
  5035. .
  5036. <p>_пристаням_стремятся</p>
  5037. ````````````````````````````````
  5038. ```````````````````````````````` example
  5039. _foo_bar_baz_
  5040. .
  5041. <p><em>foo_bar_baz</em></p>
  5042. ````````````````````````````````
  5043. This is emphasis, even though the closing delimiter is
  5044. both left- and right-flanking, because it is followed by
  5045. punctuation:
  5046. ```````````````````````````````` example
  5047. _(bar)_.
  5048. .
  5049. <p><em>(bar)</em>.</p>
  5050. ````````````````````````````````
  5051. Rule 5:
  5052. ```````````````````````````````` example
  5053. **foo bar**
  5054. .
  5055. <p><strong>foo bar</strong></p>
  5056. ````````````````````````````````
  5057. This is not strong emphasis, because the opening delimiter is
  5058. followed by whitespace:
  5059. ```````````````````````````````` example
  5060. ** foo bar**
  5061. .
  5062. <p>** foo bar**</p>
  5063. ````````````````````````````````
  5064. This is not strong emphasis, because the opening `**` is preceded
  5065. by an alphanumeric and followed by punctuation, and hence
  5066. not part of a [left-flanking delimiter run]:
  5067. ```````````````````````````````` example
  5068. a**"foo"**
  5069. .
  5070. <p>a**&quot;foo&quot;**</p>
  5071. ````````````````````````````````
  5072. Intraword strong emphasis with `**` is permitted:
  5073. ```````````````````````````````` example
  5074. foo**bar**
  5075. .
  5076. <p>foo<strong>bar</strong></p>
  5077. ````````````````````````````````
  5078. Rule 6:
  5079. ```````````````````````````````` example
  5080. __foo bar__
  5081. .
  5082. <p><strong>foo bar</strong></p>
  5083. ````````````````````````````````
  5084. This is not strong emphasis, because the opening delimiter is
  5085. followed by whitespace:
  5086. ```````````````````````````````` example
  5087. __ foo bar__
  5088. .
  5089. <p>__ foo bar__</p>
  5090. ````````````````````````````````
  5091. A newline counts as whitespace:
  5092. ```````````````````````````````` example
  5093. __
  5094. foo bar__
  5095. .
  5096. <p>__
  5097. foo bar__</p>
  5098. ````````````````````````````````
  5099. This is not strong emphasis, because the opening `__` is preceded
  5100. by an alphanumeric and followed by punctuation:
  5101. ```````````````````````````````` example
  5102. a__"foo"__
  5103. .
  5104. <p>a__&quot;foo&quot;__</p>
  5105. ````````````````````````````````
  5106. Intraword strong emphasis is forbidden with `__`:
  5107. ```````````````````````````````` example
  5108. foo__bar__
  5109. .
  5110. <p>foo__bar__</p>
  5111. ````````````````````````````````
  5112. ```````````````````````````````` example
  5113. 5__6__78
  5114. .
  5115. <p>5__6__78</p>
  5116. ````````````````````````````````
  5117. ```````````````````````````````` example
  5118. пристаням__стремятся__
  5119. .
  5120. <p>пристаням__стремятся__</p>
  5121. ````````````````````````````````
  5122. ```````````````````````````````` example
  5123. __foo, __bar__, baz__
  5124. .
  5125. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5126. ````````````````````````````````
  5127. This is strong emphasis, even though the opening delimiter is
  5128. both left- and right-flanking, because it is preceded by
  5129. punctuation:
  5130. ```````````````````````````````` example
  5131. foo-__(bar)__
  5132. .
  5133. <p>foo-<strong>(bar)</strong></p>
  5134. ````````````````````````````````
  5135. Rule 7:
  5136. This is not strong emphasis, because the closing delimiter is preceded
  5137. by whitespace:
  5138. ```````````````````````````````` example
  5139. **foo bar **
  5140. .
  5141. <p>**foo bar **</p>
  5142. ````````````````````````````````
  5143. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5144. Rule 11.)
  5145. This is not strong emphasis, because the second `**` is
  5146. preceded by punctuation and followed by an alphanumeric:
  5147. ```````````````````````````````` example
  5148. **(**foo)
  5149. .
  5150. <p>**(**foo)</p>
  5151. ````````````````````````````````
  5152. The point of this restriction is more easily appreciated
  5153. with these examples:
  5154. ```````````````````````````````` example
  5155. *(**foo**)*
  5156. .
  5157. <p><em>(<strong>foo</strong>)</em></p>
  5158. ````````````````````````````````
  5159. ```````````````````````````````` example
  5160. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5161. *Asclepias physocarpa*)**
  5162. .
  5163. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5164. <em>Asclepias physocarpa</em>)</strong></p>
  5165. ````````````````````````````````
  5166. ```````````````````````````````` example
  5167. **foo "*bar*" foo**
  5168. .
  5169. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5170. ````````````````````````````````
  5171. Intraword emphasis:
  5172. ```````````````````````````````` example
  5173. **foo**bar
  5174. .
  5175. <p><strong>foo</strong>bar</p>
  5176. ````````````````````````````````
  5177. Rule 8:
  5178. This is not strong emphasis, because the closing delimiter is
  5179. preceded by whitespace:
  5180. ```````````````````````````````` example
  5181. __foo bar __
  5182. .
  5183. <p>__foo bar __</p>
  5184. ````````````````````````````````
  5185. This is not strong emphasis, because the second `__` is
  5186. preceded by punctuation and followed by an alphanumeric:
  5187. ```````````````````````````````` example
  5188. __(__foo)
  5189. .
  5190. <p>__(__foo)</p>
  5191. ````````````````````````````````
  5192. The point of this restriction is more easily appreciated
  5193. with this example:
  5194. ```````````````````````````````` example
  5195. _(__foo__)_
  5196. .
  5197. <p><em>(<strong>foo</strong>)</em></p>
  5198. ````````````````````````````````
  5199. Intraword strong emphasis is forbidden with `__`:
  5200. ```````````````````````````````` example
  5201. __foo__bar
  5202. .
  5203. <p>__foo__bar</p>
  5204. ````````````````````````````````
  5205. ```````````````````````````````` example
  5206. __пристаням__стремятся
  5207. .
  5208. <p>__пристаням__стремятся</p>
  5209. ````````````````````````````````
  5210. ```````````````````````````````` example
  5211. __foo__bar__baz__
  5212. .
  5213. <p><strong>foo__bar__baz</strong></p>
  5214. ````````````````````````````````
  5215. This is strong emphasis, even though the closing delimiter is
  5216. both left- and right-flanking, because it is followed by
  5217. punctuation:
  5218. ```````````````````````````````` example
  5219. __(bar)__.
  5220. .
  5221. <p><strong>(bar)</strong>.</p>
  5222. ````````````````````````````````
  5223. Rule 9:
  5224. Any nonempty sequence of inline elements can be the contents of an
  5225. emphasized span.
  5226. ```````````````````````````````` example
  5227. *foo [bar](/url)*
  5228. .
  5229. <p><em>foo <a href="/url">bar</a></em></p>
  5230. ````````````````````````````````
  5231. ```````````````````````````````` example
  5232. *foo
  5233. bar*
  5234. .
  5235. <p><em>foo
  5236. bar</em></p>
  5237. ````````````````````````````````
  5238. In particular, emphasis and strong emphasis can be nested
  5239. inside emphasis:
  5240. ```````````````````````````````` example
  5241. _foo __bar__ baz_
  5242. .
  5243. <p><em>foo <strong>bar</strong> baz</em></p>
  5244. ````````````````````````````````
  5245. ```````````````````````````````` example
  5246. _foo _bar_ baz_
  5247. .
  5248. <p><em>foo <em>bar</em> baz</em></p>
  5249. ````````````````````````````````
  5250. ```````````````````````````````` example
  5251. __foo_ bar_
  5252. .
  5253. <p><em><em>foo</em> bar</em></p>
  5254. ````````````````````````````````
  5255. ```````````````````````````````` example
  5256. *foo *bar**
  5257. .
  5258. <p><em>foo <em>bar</em></em></p>
  5259. ````````````````````````````````
  5260. ```````````````````````````````` example
  5261. *foo **bar** baz*
  5262. .
  5263. <p><em>foo <strong>bar</strong> baz</em></p>
  5264. ````````````````````````````````
  5265. ```````````````````````````````` example
  5266. *foo**bar**baz*
  5267. .
  5268. <p><em>foo<strong>bar</strong>baz</em></p>
  5269. ````````````````````````````````
  5270. Note that in the preceding case, the interpretation
  5271. ``` markdown
  5272. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5273. ```
  5274. is precluded by the condition that a delimiter that
  5275. can both open and close (like the `*` after `foo`)
  5276. cannot form emphasis if the sum of the lengths of
  5277. the delimiter runs containing the opening and
  5278. closing delimiters is a multiple of 3 unless
  5279. both lengths are multiples of 3.
  5280. For the same reason, we don't get two consecutive
  5281. emphasis sections in this example:
  5282. ```````````````````````````````` example
  5283. *foo**bar*
  5284. .
  5285. <p><em>foo**bar</em></p>
  5286. ````````````````````````````````
  5287. The same condition ensures that the following
  5288. cases are all strong emphasis nested inside
  5289. emphasis, even when the interior spaces are
  5290. omitted:
  5291. ```````````````````````````````` example
  5292. ***foo** bar*
  5293. .
  5294. <p><em><strong>foo</strong> bar</em></p>
  5295. ````````````````````````````````
  5296. ```````````````````````````````` example
  5297. *foo **bar***
  5298. .
  5299. <p><em>foo <strong>bar</strong></em></p>
  5300. ````````````````````````````````
  5301. ```````````````````````````````` example
  5302. *foo**bar***
  5303. .
  5304. <p><em>foo<strong>bar</strong></em></p>
  5305. ````````````````````````````````
  5306. When the lengths of the interior closing and opening
  5307. delimiter runs are *both* multiples of 3, though,
  5308. they can match to create emphasis:
  5309. ```````````````````````````````` example
  5310. foo***bar***baz
  5311. .
  5312. <p>foo<em><strong>bar</strong></em>baz</p>
  5313. ````````````````````````````````
  5314. ```````````````````````````````` example
  5315. foo******bar*********baz
  5316. .
  5317. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5318. ````````````````````````````````
  5319. Indefinite levels of nesting are possible:
  5320. ```````````````````````````````` example
  5321. *foo **bar *baz* bim** bop*
  5322. .
  5323. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5324. ````````````````````````````````
  5325. ```````````````````````````````` example
  5326. *foo [*bar*](/url)*
  5327. .
  5328. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5329. ````````````````````````````````
  5330. There can be no empty emphasis or strong emphasis:
  5331. ```````````````````````````````` example
  5332. ** is not an empty emphasis
  5333. .
  5334. <p>** is not an empty emphasis</p>
  5335. ````````````````````````````````
  5336. ```````````````````````````````` example
  5337. **** is not an empty strong emphasis
  5338. .
  5339. <p>**** is not an empty strong emphasis</p>
  5340. ````````````````````````````````
  5341. Rule 10:
  5342. Any nonempty sequence of inline elements can be the contents of an
  5343. strongly emphasized span.
  5344. ```````````````````````````````` example
  5345. **foo [bar](/url)**
  5346. .
  5347. <p><strong>foo <a href="/url">bar</a></strong></p>
  5348. ````````````````````````````````
  5349. ```````````````````````````````` example
  5350. **foo
  5351. bar**
  5352. .
  5353. <p><strong>foo
  5354. bar</strong></p>
  5355. ````````````````````````````````
  5356. In particular, emphasis and strong emphasis can be nested
  5357. inside strong emphasis:
  5358. ```````````````````````````````` example
  5359. __foo _bar_ baz__
  5360. .
  5361. <p><strong>foo <em>bar</em> baz</strong></p>
  5362. ````````````````````````````````
  5363. ```````````````````````````````` example
  5364. __foo __bar__ baz__
  5365. .
  5366. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5367. ````````````````````````````````
  5368. ```````````````````````````````` example
  5369. ____foo__ bar__
  5370. .
  5371. <p><strong><strong>foo</strong> bar</strong></p>
  5372. ````````````````````````````````
  5373. ```````````````````````````````` example
  5374. **foo **bar****
  5375. .
  5376. <p><strong>foo <strong>bar</strong></strong></p>
  5377. ````````````````````````````````
  5378. ```````````````````````````````` example
  5379. **foo *bar* baz**
  5380. .
  5381. <p><strong>foo <em>bar</em> baz</strong></p>
  5382. ````````````````````````````````
  5383. ```````````````````````````````` example
  5384. **foo*bar*baz**
  5385. .
  5386. <p><strong>foo<em>bar</em>baz</strong></p>
  5387. ````````````````````````````````
  5388. ```````````````````````````````` example
  5389. ***foo* bar**
  5390. .
  5391. <p><strong><em>foo</em> bar</strong></p>
  5392. ````````````````````````````````
  5393. ```````````````````````````````` example
  5394. **foo *bar***
  5395. .
  5396. <p><strong>foo <em>bar</em></strong></p>
  5397. ````````````````````````````````
  5398. Indefinite levels of nesting are possible:
  5399. ```````````````````````````````` example
  5400. **foo *bar **baz**
  5401. bim* bop**
  5402. .
  5403. <p><strong>foo <em>bar <strong>baz</strong>
  5404. bim</em> bop</strong></p>
  5405. ````````````````````````````````
  5406. ```````````````````````````````` example
  5407. **foo [*bar*](/url)**
  5408. .
  5409. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5410. ````````````````````````````````
  5411. There can be no empty emphasis or strong emphasis:
  5412. ```````````````````````````````` example
  5413. __ is not an empty emphasis
  5414. .
  5415. <p>__ is not an empty emphasis</p>
  5416. ````````````````````````````````
  5417. ```````````````````````````````` example
  5418. ____ is not an empty strong emphasis
  5419. .
  5420. <p>____ is not an empty strong emphasis</p>
  5421. ````````````````````````````````
  5422. Rule 11:
  5423. ```````````````````````````````` example
  5424. foo ***
  5425. .
  5426. <p>foo ***</p>
  5427. ````````````````````````````````
  5428. ```````````````````````````````` example
  5429. foo *\**
  5430. .
  5431. <p>foo <em>*</em></p>
  5432. ````````````````````````````````
  5433. ```````````````````````````````` example
  5434. foo *_*
  5435. .
  5436. <p>foo <em>_</em></p>
  5437. ````````````````````````````````
  5438. ```````````````````````````````` example
  5439. foo *****
  5440. .
  5441. <p>foo *****</p>
  5442. ````````````````````````````````
  5443. ```````````````````````````````` example
  5444. foo **\***
  5445. .
  5446. <p>foo <strong>*</strong></p>
  5447. ````````````````````````````````
  5448. ```````````````````````````````` example
  5449. foo **_**
  5450. .
  5451. <p>foo <strong>_</strong></p>
  5452. ````````````````````````````````
  5453. Note that when delimiters do not match evenly, Rule 11 determines
  5454. that the excess literal `*` characters will appear outside of the
  5455. emphasis, rather than inside it:
  5456. ```````````````````````````````` example
  5457. **foo*
  5458. .
  5459. <p>*<em>foo</em></p>
  5460. ````````````````````````````````
  5461. ```````````````````````````````` example
  5462. *foo**
  5463. .
  5464. <p><em>foo</em>*</p>
  5465. ````````````````````````````````
  5466. ```````````````````````````````` example
  5467. ***foo**
  5468. .
  5469. <p>*<strong>foo</strong></p>
  5470. ````````````````````````````````
  5471. ```````````````````````````````` example
  5472. ****foo*
  5473. .
  5474. <p>***<em>foo</em></p>
  5475. ````````````````````````````````
  5476. ```````````````````````````````` example
  5477. **foo***
  5478. .
  5479. <p><strong>foo</strong>*</p>
  5480. ````````````````````````````````
  5481. ```````````````````````````````` example
  5482. *foo****
  5483. .
  5484. <p><em>foo</em>***</p>
  5485. ````````````````````````````````
  5486. Rule 12:
  5487. ```````````````````````````````` example
  5488. foo ___
  5489. .
  5490. <p>foo ___</p>
  5491. ````````````````````````````````
  5492. ```````````````````````````````` example
  5493. foo _\__
  5494. .
  5495. <p>foo <em>_</em></p>
  5496. ````````````````````````````````
  5497. ```````````````````````````````` example
  5498. foo _*_
  5499. .
  5500. <p>foo <em>*</em></p>
  5501. ````````````````````````````````
  5502. ```````````````````````````````` example
  5503. foo _____
  5504. .
  5505. <p>foo _____</p>
  5506. ````````````````````````````````
  5507. ```````````````````````````````` example
  5508. foo __\___
  5509. .
  5510. <p>foo <strong>_</strong></p>
  5511. ````````````````````````````````
  5512. ```````````````````````````````` example
  5513. foo __*__
  5514. .
  5515. <p>foo <strong>*</strong></p>
  5516. ````````````````````````````````
  5517. ```````````````````````````````` example
  5518. __foo_
  5519. .
  5520. <p>_<em>foo</em></p>
  5521. ````````````````````````````````
  5522. Note that when delimiters do not match evenly, Rule 12 determines
  5523. that the excess literal `_` characters will appear outside of the
  5524. emphasis, rather than inside it:
  5525. ```````````````````````````````` example
  5526. _foo__
  5527. .
  5528. <p><em>foo</em>_</p>
  5529. ````````````````````````````````
  5530. ```````````````````````````````` example
  5531. ___foo__
  5532. .
  5533. <p>_<strong>foo</strong></p>
  5534. ````````````````````````````````
  5535. ```````````````````````````````` example
  5536. ____foo_
  5537. .
  5538. <p>___<em>foo</em></p>
  5539. ````````````````````````````````
  5540. ```````````````````````````````` example
  5541. __foo___
  5542. .
  5543. <p><strong>foo</strong>_</p>
  5544. ````````````````````````````````
  5545. ```````````````````````````````` example
  5546. _foo____
  5547. .
  5548. <p><em>foo</em>___</p>
  5549. ````````````````````````````````
  5550. Rule 13 implies that if you want emphasis nested directly inside
  5551. emphasis, you must use different delimiters:
  5552. ```````````````````````````````` example
  5553. **foo**
  5554. .
  5555. <p><strong>foo</strong></p>
  5556. ````````````````````````````````
  5557. ```````````````````````````````` example
  5558. *_foo_*
  5559. .
  5560. <p><em><em>foo</em></em></p>
  5561. ````````````````````````````````
  5562. ```````````````````````````````` example
  5563. __foo__
  5564. .
  5565. <p><strong>foo</strong></p>
  5566. ````````````````````````````````
  5567. ```````````````````````````````` example
  5568. _*foo*_
  5569. .
  5570. <p><em><em>foo</em></em></p>
  5571. ````````````````````````````````
  5572. However, strong emphasis within strong emphasis is possible without
  5573. switching delimiters:
  5574. ```````````````````````````````` example
  5575. ****foo****
  5576. .
  5577. <p><strong><strong>foo</strong></strong></p>
  5578. ````````````````````````````````
  5579. ```````````````````````````````` example
  5580. ____foo____
  5581. .
  5582. <p><strong><strong>foo</strong></strong></p>
  5583. ````````````````````````````````
  5584. Rule 13 can be applied to arbitrarily long sequences of
  5585. delimiters:
  5586. ```````````````````````````````` example
  5587. ******foo******
  5588. .
  5589. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5590. ````````````````````````````````
  5591. Rule 14:
  5592. ```````````````````````````````` example
  5593. ***foo***
  5594. .
  5595. <p><em><strong>foo</strong></em></p>
  5596. ````````````````````````````````
  5597. ```````````````````````````````` example
  5598. _____foo_____
  5599. .
  5600. <p><em><strong><strong>foo</strong></strong></em></p>
  5601. ````````````````````````````````
  5602. Rule 15:
  5603. ```````````````````````````````` example
  5604. *foo _bar* baz_
  5605. .
  5606. <p><em>foo _bar</em> baz_</p>
  5607. ````````````````````````````````
  5608. ```````````````````````````````` example
  5609. *foo __bar *baz bim__ bam*
  5610. .
  5611. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5612. ````````````````````````````````
  5613. Rule 16:
  5614. ```````````````````````````````` example
  5615. **foo **bar baz**
  5616. .
  5617. <p>**foo <strong>bar baz</strong></p>
  5618. ````````````````````````````````
  5619. ```````````````````````````````` example
  5620. *foo *bar baz*
  5621. .
  5622. <p>*foo <em>bar baz</em></p>
  5623. ````````````````````````````````
  5624. Rule 17:
  5625. ```````````````````````````````` example
  5626. *[bar*](/url)
  5627. .
  5628. <p>*<a href="/url">bar*</a></p>
  5629. ````````````````````````````````
  5630. ```````````````````````````````` example
  5631. _foo [bar_](/url)
  5632. .
  5633. <p>_foo <a href="/url">bar_</a></p>
  5634. ````````````````````````````````
  5635. ```````````````````````````````` example
  5636. *<img src="foo" title="*"/>
  5637. .
  5638. <p>*<img src="foo" title="*"/></p>
  5639. ````````````````````````````````
  5640. ```````````````````````````````` example
  5641. **<a href="**">
  5642. .
  5643. <p>**<a href="**"></p>
  5644. ````````````````````````````````
  5645. ```````````````````````````````` example
  5646. __<a href="__">
  5647. .
  5648. <p>__<a href="__"></p>
  5649. ````````````````````````````````
  5650. ```````````````````````````````` example
  5651. *a `*`*
  5652. .
  5653. <p><em>a <code>*</code></em></p>
  5654. ````````````````````````````````
  5655. ```````````````````````````````` example
  5656. _a `_`_
  5657. .
  5658. <p><em>a <code>_</code></em></p>
  5659. ````````````````````````````````
  5660. ```````````````````````````````` example
  5661. **a<http://foo.bar/?q=**>
  5662. .
  5663. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5664. ````````````````````````````````
  5665. ```````````````````````````````` example
  5666. __a<http://foo.bar/?q=__>
  5667. .
  5668. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5669. ````````````````````````````````
  5670. ## Links
  5671. A link contains [link text] (the visible text), a [link destination]
  5672. (the URI that is the link destination), and optionally a [link title].
  5673. There are two basic kinds of links in Markdown. In [inline links] the
  5674. destination and title are given immediately after the link text. In
  5675. [reference links] the destination and title are defined elsewhere in
  5676. the document.
  5677. A [link text](@) consists of a sequence of zero or more
  5678. inline elements enclosed by square brackets (`[` and `]`). The
  5679. following rules apply:
  5680. - Links may not contain other links, at any level of nesting. If
  5681. multiple otherwise valid link definitions appear nested inside each
  5682. other, the inner-most definition is used.
  5683. - Brackets are allowed in the [link text] only if (a) they
  5684. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5685. with an open bracket `[`, a sequence of zero or more inlines, and
  5686. a close bracket `]`.
  5687. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5688. than the brackets in link text. Thus, for example,
  5689. `` [foo`]` `` could not be a link text, since the second `]`
  5690. is part of a code span.
  5691. - The brackets in link text bind more tightly than markers for
  5692. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5693. A [link destination](@) consists of either
  5694. - a sequence of zero or more characters between an opening `<` and a
  5695. closing `>` that contains no line breaks or unescaped
  5696. `<` or `>` characters, or
  5697. - a nonempty sequence of characters that does not start with `<`,
  5698. does not include [ASCII control characters][ASCII control character]
  5699. or [whitespace][], and includes parentheses only if (a) they are
  5700. backslash-escaped or (b) they are part of a balanced pair of
  5701. unescaped parentheses.
  5702. (Implementations may impose limits on parentheses nesting to
  5703. avoid performance issues, but at least three levels of nesting
  5704. should be supported.)
  5705. A [link title](@) consists of either
  5706. - a sequence of zero or more characters between straight double-quote
  5707. characters (`"`), including a `"` character only if it is
  5708. backslash-escaped, or
  5709. - a sequence of zero or more characters between straight single-quote
  5710. characters (`'`), including a `'` character only if it is
  5711. backslash-escaped, or
  5712. - a sequence of zero or more characters between matching parentheses
  5713. (`(...)`), including a `(` or `)` character only if it is
  5714. backslash-escaped.
  5715. Although [link titles] may span multiple lines, they may not contain
  5716. a [blank line].
  5717. An [inline link](@) consists of a [link text] followed immediately
  5718. by a left parenthesis `(`, optional [whitespace], an optional
  5719. [link destination], an optional [link title] separated from the link
  5720. destination by [whitespace], optional [whitespace], and a right
  5721. parenthesis `)`. The link's text consists of the inlines contained
  5722. in the [link text] (excluding the enclosing square brackets).
  5723. The link's URI consists of the link destination, excluding enclosing
  5724. `<...>` if present, with backslash-escapes in effect as described
  5725. above. The link's title consists of the link title, excluding its
  5726. enclosing delimiters, with backslash-escapes in effect as described
  5727. above.
  5728. Here is a simple inline link:
  5729. ```````````````````````````````` example
  5730. [link](/uri "title")
  5731. .
  5732. <p><a href="/uri" title="title">link</a></p>
  5733. ````````````````````````````````
  5734. The title may be omitted:
  5735. ```````````````````````````````` example
  5736. [link](/uri)
  5737. .
  5738. <p><a href="/uri">link</a></p>
  5739. ````````````````````````````````
  5740. Both the title and the destination may be omitted:
  5741. ```````````````````````````````` example
  5742. [link]()
  5743. .
  5744. <p><a href="">link</a></p>
  5745. ````````````````````````````````
  5746. ```````````````````````````````` example
  5747. [link](<>)
  5748. .
  5749. <p><a href="">link</a></p>
  5750. ````````````````````````````````
  5751. The destination can only contain spaces if it is
  5752. enclosed in pointy brackets:
  5753. ```````````````````````````````` example
  5754. [link](/my uri)
  5755. .
  5756. <p>[link](/my uri)</p>
  5757. ````````````````````````````````
  5758. ```````````````````````````````` example
  5759. [link](</my uri>)
  5760. .
  5761. <p><a href="/my%20uri">link</a></p>
  5762. ````````````````````````````````
  5763. The destination cannot contain line breaks,
  5764. even if enclosed in pointy brackets:
  5765. ```````````````````````````````` example
  5766. [link](foo
  5767. bar)
  5768. .
  5769. <p>[link](foo
  5770. bar)</p>
  5771. ````````````````````````````````
  5772. ```````````````````````````````` example
  5773. [link](<foo
  5774. bar>)
  5775. .
  5776. <p>[link](<foo
  5777. bar>)</p>
  5778. ````````````````````````````````
  5779. The destination can contain `)` if it is enclosed
  5780. in pointy brackets:
  5781. ```````````````````````````````` example
  5782. [a](<b)c>)
  5783. .
  5784. <p><a href="b)c">a</a></p>
  5785. ````````````````````````````````
  5786. Pointy brackets that enclose links must be unescaped:
  5787. ```````````````````````````````` example
  5788. [link](<foo\>)
  5789. .
  5790. <p>[link](&lt;foo&gt;)</p>
  5791. ````````````````````````````````
  5792. These are not links, because the opening pointy bracket
  5793. is not matched properly:
  5794. ```````````````````````````````` example
  5795. [a](<b)c
  5796. [a](<b)c>
  5797. [a](<b>c)
  5798. .
  5799. <p>[a](&lt;b)c
  5800. [a](&lt;b)c&gt;
  5801. [a](<b>c)</p>
  5802. ````````````````````````````````
  5803. Parentheses inside the link destination may be escaped:
  5804. ```````````````````````````````` example
  5805. [link](\(foo\))
  5806. .
  5807. <p><a href="(foo)">link</a></p>
  5808. ````````````````````````````````
  5809. Any number of parentheses are allowed without escaping, as long as they are
  5810. balanced:
  5811. ```````````````````````````````` example
  5812. [link](foo(and(bar)))
  5813. .
  5814. <p><a href="foo(and(bar))">link</a></p>
  5815. ````````````````````````````````
  5816. However, if you have unbalanced parentheses, you need to escape or use the
  5817. `<...>` form:
  5818. ```````````````````````````````` example
  5819. [link](foo\(and\(bar\))
  5820. .
  5821. <p><a href="foo(and(bar)">link</a></p>
  5822. ````````````````````````````````
  5823. ```````````````````````````````` example
  5824. [link](<foo(and(bar)>)
  5825. .
  5826. <p><a href="foo(and(bar)">link</a></p>
  5827. ````````````````````````````````
  5828. Parentheses and other symbols can also be escaped, as usual
  5829. in Markdown:
  5830. ```````````````````````````````` example
  5831. [link](foo\)\:)
  5832. .
  5833. <p><a href="foo):">link</a></p>
  5834. ````````````````````````````````
  5835. A link can contain fragment identifiers and queries:
  5836. ```````````````````````````````` example
  5837. [link](#fragment)
  5838. [link](http://example.com#fragment)
  5839. [link](http://example.com?foo=3#frag)
  5840. .
  5841. <p><a href="#fragment">link</a></p>
  5842. <p><a href="http://example.com#fragment">link</a></p>
  5843. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5844. ````````````````````````````````
  5845. Note that a backslash before a non-escapable character is
  5846. just a backslash:
  5847. ```````````````````````````````` example
  5848. [link](foo\bar)
  5849. .
  5850. <p><a href="foo%5Cbar">link</a></p>
  5851. ````````````````````````````````
  5852. URL-escaping should be left alone inside the destination, as all
  5853. URL-escaped characters are also valid URL characters. Entity and
  5854. numerical character references in the destination will be parsed
  5855. into the corresponding Unicode code points, as usual. These may
  5856. be optionally URL-escaped when written as HTML, but this spec
  5857. does not enforce any particular policy for rendering URLs in
  5858. HTML or other formats. Renderers may make different decisions
  5859. about how to escape or normalize URLs in the output.
  5860. ```````````````````````````````` example
  5861. [link](foo%20b&auml;)
  5862. .
  5863. <p><a href="foo%20b%C3%A4">link</a></p>
  5864. ````````````````````````````````
  5865. Note that, because titles can often be parsed as destinations,
  5866. if you try to omit the destination and keep the title, you'll
  5867. get unexpected results:
  5868. ```````````````````````````````` example
  5869. [link]("title")
  5870. .
  5871. <p><a href="%22title%22">link</a></p>
  5872. ````````````````````````````````
  5873. Titles may be in single quotes, double quotes, or parentheses:
  5874. ```````````````````````````````` example
  5875. [link](/url "title")
  5876. [link](/url 'title')
  5877. [link](/url (title))
  5878. .
  5879. <p><a href="/url" title="title">link</a>
  5880. <a href="/url" title="title">link</a>
  5881. <a href="/url" title="title">link</a></p>
  5882. ````````````````````````````````
  5883. Backslash escapes and entity and numeric character references
  5884. may be used in titles:
  5885. ```````````````````````````````` example
  5886. [link](/url "title \"&quot;")
  5887. .
  5888. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5889. ````````````````````````````````
  5890. Titles must be separated from the link using a [whitespace].
  5891. Other [Unicode whitespace] like non-breaking space doesn't work.
  5892. ```````````````````````````````` example
  5893. [link](/url "title")
  5894. .
  5895. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5896. ````````````````````````````````
  5897. Nested balanced quotes are not allowed without escaping:
  5898. ```````````````````````````````` example
  5899. [link](/url "title "and" title")
  5900. .
  5901. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5902. ````````````````````````````````
  5903. But it is easy to work around this by using a different quote type:
  5904. ```````````````````````````````` example
  5905. [link](/url 'title "and" title')
  5906. .
  5907. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5908. ````````````````````````````````
  5909. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5910. title, and its test suite included a test demonstrating this.
  5911. But it is hard to see a good rationale for the extra complexity this
  5912. brings, since there are already many ways---backslash escaping,
  5913. entity and numeric character references, or using a different
  5914. quote type for the enclosing title---to write titles containing
  5915. double quotes. `Markdown.pl`'s handling of titles has a number
  5916. of other strange features. For example, it allows single-quoted
  5917. titles in inline links, but not reference links. And, in
  5918. reference links but not inline links, it allows a title to begin
  5919. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5920. titles with no closing quotation mark, though 1.0.2b8 does not.
  5921. It seems preferable to adopt a simple, rational rule that works
  5922. the same way in inline links and link reference definitions.)
  5923. [Whitespace] is allowed around the destination and title:
  5924. ```````````````````````````````` example
  5925. [link]( /uri
  5926. "title" )
  5927. .
  5928. <p><a href="/uri" title="title">link</a></p>
  5929. ````````````````````````````````
  5930. But it is not allowed between the link text and the
  5931. following parenthesis:
  5932. ```````````````````````````````` example
  5933. [link] (/uri)
  5934. .
  5935. <p>[link] (/uri)</p>
  5936. ````````````````````````````````
  5937. The link text may contain balanced brackets, but not unbalanced ones,
  5938. unless they are escaped:
  5939. ```````````````````````````````` example
  5940. [link [foo [bar]]](/uri)
  5941. .
  5942. <p><a href="/uri">link [foo [bar]]</a></p>
  5943. ````````````````````````````````
  5944. ```````````````````````````````` example
  5945. [link] bar](/uri)
  5946. .
  5947. <p>[link] bar](/uri)</p>
  5948. ````````````````````````````````
  5949. ```````````````````````````````` example
  5950. [link [bar](/uri)
  5951. .
  5952. <p>[link <a href="/uri">bar</a></p>
  5953. ````````````````````````````````
  5954. ```````````````````````````````` example
  5955. [link \[bar](/uri)
  5956. .
  5957. <p><a href="/uri">link [bar</a></p>
  5958. ````````````````````````````````
  5959. The link text may contain inline content:
  5960. ```````````````````````````````` example
  5961. [link *foo **bar** `#`*](/uri)
  5962. .
  5963. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5964. ````````````````````````````````
  5965. ```````````````````````````````` example
  5966. [![moon](moon.jpg)](/uri)
  5967. .
  5968. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5969. ````````````````````````````````
  5970. However, links may not contain other links, at any level of nesting.
  5971. ```````````````````````````````` example
  5972. [foo [bar](/uri)](/uri)
  5973. .
  5974. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5975. ````````````````````````````````
  5976. ```````````````````````````````` example
  5977. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5978. .
  5979. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5980. ````````````````````````````````
  5981. ```````````````````````````````` example
  5982. ![[[foo](uri1)](uri2)](uri3)
  5983. .
  5984. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5985. ````````````````````````````````
  5986. These cases illustrate the precedence of link text grouping over
  5987. emphasis grouping:
  5988. ```````````````````````````````` example
  5989. *[foo*](/uri)
  5990. .
  5991. <p>*<a href="/uri">foo*</a></p>
  5992. ````````````````````````````````
  5993. ```````````````````````````````` example
  5994. [foo *bar](baz*)
  5995. .
  5996. <p><a href="baz*">foo *bar</a></p>
  5997. ````````````````````````````````
  5998. Note that brackets that *aren't* part of links do not take
  5999. precedence:
  6000. ```````````````````````````````` example
  6001. *foo [bar* baz]
  6002. .
  6003. <p><em>foo [bar</em> baz]</p>
  6004. ````````````````````````````````
  6005. These cases illustrate the precedence of HTML tags, code spans,
  6006. and autolinks over link grouping:
  6007. ```````````````````````````````` example
  6008. [foo <bar attr="](baz)">
  6009. .
  6010. <p>[foo <bar attr="](baz)"></p>
  6011. ````````````````````````````````
  6012. ```````````````````````````````` example
  6013. [foo`](/uri)`
  6014. .
  6015. <p>[foo<code>](/uri)</code></p>
  6016. ````````````````````````````````
  6017. ```````````````````````````````` example
  6018. [foo<http://example.com/?search=](uri)>
  6019. .
  6020. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  6021. ````````````````````````````````
  6022. There are three kinds of [reference link](@)s:
  6023. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  6024. and [shortcut](#shortcut-reference-link).
  6025. A [full reference link](@)
  6026. consists of a [link text] immediately followed by a [link label]
  6027. that [matches] a [link reference definition] elsewhere in the document.
  6028. A [link label](@) begins with a left bracket (`[`) and ends
  6029. with the first right bracket (`]`) that is not backslash-escaped.
  6030. Between these brackets there must be at least one [non-whitespace character].
  6031. Unescaped square bracket characters are not allowed inside the
  6032. opening and closing square brackets of [link labels]. A link
  6033. label can have at most 999 characters inside the square
  6034. brackets.
  6035. One label [matches](@)
  6036. another just in case their normalized forms are equal. To normalize a
  6037. label, strip off the opening and closing brackets,
  6038. perform the *Unicode case fold*, strip leading and trailing
  6039. [whitespace] and collapse consecutive internal
  6040. [whitespace] to a single space. If there are multiple
  6041. matching reference link definitions, the one that comes first in the
  6042. document is used. (It is desirable in such cases to emit a warning.)
  6043. The link's URI and title are provided by the matching [link
  6044. reference definition].
  6045. Here is a simple example:
  6046. ```````````````````````````````` example
  6047. [foo][bar]
  6048. [bar]: /url "title"
  6049. .
  6050. <p><a href="/url" title="title">foo</a></p>
  6051. ````````````````````````````````
  6052. The rules for the [link text] are the same as with
  6053. [inline links]. Thus:
  6054. The link text may contain balanced brackets, but not unbalanced ones,
  6055. unless they are escaped:
  6056. ```````````````````````````````` example
  6057. [link [foo [bar]]][ref]
  6058. [ref]: /uri
  6059. .
  6060. <p><a href="/uri">link [foo [bar]]</a></p>
  6061. ````````````````````````````````
  6062. ```````````````````````````````` example
  6063. [link \[bar][ref]
  6064. [ref]: /uri
  6065. .
  6066. <p><a href="/uri">link [bar</a></p>
  6067. ````````````````````````````````
  6068. The link text may contain inline content:
  6069. ```````````````````````````````` example
  6070. [link *foo **bar** `#`*][ref]
  6071. [ref]: /uri
  6072. .
  6073. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6074. ````````````````````````````````
  6075. ```````````````````````````````` example
  6076. [![moon](moon.jpg)][ref]
  6077. [ref]: /uri
  6078. .
  6079. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6080. ````````````````````````````````
  6081. However, links may not contain other links, at any level of nesting.
  6082. ```````````````````````````````` example
  6083. [foo [bar](/uri)][ref]
  6084. [ref]: /uri
  6085. .
  6086. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  6087. ````````````````````````````````
  6088. ```````````````````````````````` example
  6089. [foo *bar [baz][ref]*][ref]
  6090. [ref]: /uri
  6091. .
  6092. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  6093. ````````````````````````````````
  6094. (In the examples above, we have two [shortcut reference links]
  6095. instead of one [full reference link].)
  6096. The following cases illustrate the precedence of link text grouping over
  6097. emphasis grouping:
  6098. ```````````````````````````````` example
  6099. *[foo*][ref]
  6100. [ref]: /uri
  6101. .
  6102. <p>*<a href="/uri">foo*</a></p>
  6103. ````````````````````````````````
  6104. ```````````````````````````````` example
  6105. [foo *bar][ref]*
  6106. [ref]: /uri
  6107. .
  6108. <p><a href="/uri">foo *bar</a>*</p>
  6109. ````````````````````````````````
  6110. These cases illustrate the precedence of HTML tags, code spans,
  6111. and autolinks over link grouping:
  6112. ```````````````````````````````` example
  6113. [foo <bar attr="][ref]">
  6114. [ref]: /uri
  6115. .
  6116. <p>[foo <bar attr="][ref]"></p>
  6117. ````````````````````````````````
  6118. ```````````````````````````````` example
  6119. [foo`][ref]`
  6120. [ref]: /uri
  6121. .
  6122. <p>[foo<code>][ref]</code></p>
  6123. ````````````````````````````````
  6124. ```````````````````````````````` example
  6125. [foo<http://example.com/?search=][ref]>
  6126. [ref]: /uri
  6127. .
  6128. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6129. ````````````````````````````````
  6130. Matching is case-insensitive:
  6131. ```````````````````````````````` example
  6132. [foo][BaR]
  6133. [bar]: /url "title"
  6134. .
  6135. <p><a href="/url" title="title">foo</a></p>
  6136. ````````````````````````````````
  6137. Unicode case fold is used:
  6138. ```````````````````````````````` example
  6139. [ẞ]
  6140. [SS]: /url
  6141. .
  6142. <p><a href="/url">ẞ</a></p>
  6143. ````````````````````````````````
  6144. Consecutive internal [whitespace] is treated as one space for
  6145. purposes of determining matching:
  6146. ```````````````````````````````` example
  6147. [Foo
  6148. bar]: /url
  6149. [Baz][Foo bar]
  6150. .
  6151. <p><a href="/url">Baz</a></p>
  6152. ````````````````````````````````
  6153. No [whitespace] is allowed between the [link text] and the
  6154. [link label]:
  6155. ```````````````````````````````` example
  6156. [foo] [bar]
  6157. [bar]: /url "title"
  6158. .
  6159. <p>[foo] <a href="/url" title="title">bar</a></p>
  6160. ````````````````````````````````
  6161. ```````````````````````````````` example
  6162. [foo]
  6163. [bar]
  6164. [bar]: /url "title"
  6165. .
  6166. <p>[foo]
  6167. <a href="/url" title="title">bar</a></p>
  6168. ````````````````````````````````
  6169. This is a departure from John Gruber's original Markdown syntax
  6170. description, which explicitly allows whitespace between the link
  6171. text and the link label. It brings reference links in line with
  6172. [inline links], which (according to both original Markdown and
  6173. this spec) cannot have whitespace after the link text. More
  6174. importantly, it prevents inadvertent capture of consecutive
  6175. [shortcut reference links]. If whitespace is allowed between the
  6176. link text and the link label, then in the following we will have
  6177. a single reference link, not two shortcut reference links, as
  6178. intended:
  6179. ``` markdown
  6180. [foo]
  6181. [bar]
  6182. [foo]: /url1
  6183. [bar]: /url2
  6184. ```
  6185. (Note that [shortcut reference links] were introduced by Gruber
  6186. himself in a beta version of `Markdown.pl`, but never included
  6187. in the official syntax description. Without shortcut reference
  6188. links, it is harmless to allow space between the link text and
  6189. link label; but once shortcut references are introduced, it is
  6190. too dangerous to allow this, as it frequently leads to
  6191. unintended results.)
  6192. When there are multiple matching [link reference definitions],
  6193. the first is used:
  6194. ```````````````````````````````` example
  6195. [foo]: /url1
  6196. [foo]: /url2
  6197. [bar][foo]
  6198. .
  6199. <p><a href="/url1">bar</a></p>
  6200. ````````````````````````````````
  6201. Note that matching is performed on normalized strings, not parsed
  6202. inline content. So the following does not match, even though the
  6203. labels define equivalent inline content:
  6204. ```````````````````````````````` example
  6205. [bar][foo\!]
  6206. [foo!]: /url
  6207. .
  6208. <p>[bar][foo!]</p>
  6209. ````````````````````````````````
  6210. [Link labels] cannot contain brackets, unless they are
  6211. backslash-escaped:
  6212. ```````````````````````````````` example
  6213. [foo][ref[]
  6214. [ref[]: /uri
  6215. .
  6216. <p>[foo][ref[]</p>
  6217. <p>[ref[]: /uri</p>
  6218. ````````````````````````````````
  6219. ```````````````````````````````` example
  6220. [foo][ref[bar]]
  6221. [ref[bar]]: /uri
  6222. .
  6223. <p>[foo][ref[bar]]</p>
  6224. <p>[ref[bar]]: /uri</p>
  6225. ````````````````````````````````
  6226. ```````````````````````````````` example
  6227. [[[foo]]]
  6228. [[[foo]]]: /url
  6229. .
  6230. <p>[[[foo]]]</p>
  6231. <p>[[[foo]]]: /url</p>
  6232. ````````````````````````````````
  6233. ```````````````````````````````` example
  6234. [foo][ref\[]
  6235. [ref\[]: /uri
  6236. .
  6237. <p><a href="/uri">foo</a></p>
  6238. ````````````````````````````````
  6239. Note that in this example `]` is not backslash-escaped:
  6240. ```````````````````````````````` example
  6241. [bar\\]: /uri
  6242. [bar\\]
  6243. .
  6244. <p><a href="/uri">bar\</a></p>
  6245. ````````````````````````````````
  6246. A [link label] must contain at least one [non-whitespace character]:
  6247. ```````````````````````````````` example
  6248. []
  6249. []: /uri
  6250. .
  6251. <p>[]</p>
  6252. <p>[]: /uri</p>
  6253. ````````````````````````````````
  6254. ```````````````````````````````` example
  6255. [
  6256. ]
  6257. [
  6258. ]: /uri
  6259. .
  6260. <p>[
  6261. ]</p>
  6262. <p>[
  6263. ]: /uri</p>
  6264. ````````````````````````````````
  6265. A [collapsed reference link](@)
  6266. consists of a [link label] that [matches] a
  6267. [link reference definition] elsewhere in the
  6268. document, followed by the string `[]`.
  6269. The contents of the first link label are parsed as inlines,
  6270. which are used as the link's text. The link's URI and title are
  6271. provided by the matching reference link definition. Thus,
  6272. `[foo][]` is equivalent to `[foo][foo]`.
  6273. ```````````````````````````````` example
  6274. [foo][]
  6275. [foo]: /url "title"
  6276. .
  6277. <p><a href="/url" title="title">foo</a></p>
  6278. ````````````````````````````````
  6279. ```````````````````````````````` example
  6280. [*foo* bar][]
  6281. [*foo* bar]: /url "title"
  6282. .
  6283. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6284. ````````````````````````````````
  6285. The link labels are case-insensitive:
  6286. ```````````````````````````````` example
  6287. [Foo][]
  6288. [foo]: /url "title"
  6289. .
  6290. <p><a href="/url" title="title">Foo</a></p>
  6291. ````````````````````````````````
  6292. As with full reference links, [whitespace] is not
  6293. allowed between the two sets of brackets:
  6294. ```````````````````````````````` example
  6295. [foo]
  6296. []
  6297. [foo]: /url "title"
  6298. .
  6299. <p><a href="/url" title="title">foo</a>
  6300. []</p>
  6301. ````````````````````````````````
  6302. A [shortcut reference link](@)
  6303. consists of a [link label] that [matches] a
  6304. [link reference definition] elsewhere in the
  6305. document and is not followed by `[]` or a link label.
  6306. The contents of the first link label are parsed as inlines,
  6307. which are used as the link's text. The link's URI and title
  6308. are provided by the matching link reference definition.
  6309. Thus, `[foo]` is equivalent to `[foo][]`.
  6310. ```````````````````````````````` example
  6311. [foo]
  6312. [foo]: /url "title"
  6313. .
  6314. <p><a href="/url" title="title">foo</a></p>
  6315. ````````````````````````````````
  6316. ```````````````````````````````` example
  6317. [*foo* bar]
  6318. [*foo* bar]: /url "title"
  6319. .
  6320. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6321. ````````````````````````````````
  6322. ```````````````````````````````` example
  6323. [[*foo* bar]]
  6324. [*foo* bar]: /url "title"
  6325. .
  6326. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6327. ````````````````````````````````
  6328. ```````````````````````````````` example
  6329. [[bar [foo]
  6330. [foo]: /url
  6331. .
  6332. <p>[[bar <a href="/url">foo</a></p>
  6333. ````````````````````````````````
  6334. The link labels are case-insensitive:
  6335. ```````````````````````````````` example
  6336. [Foo]
  6337. [foo]: /url "title"
  6338. .
  6339. <p><a href="/url" title="title">Foo</a></p>
  6340. ````````````````````````````````
  6341. A space after the link text should be preserved:
  6342. ```````````````````````````````` example
  6343. [foo] bar
  6344. [foo]: /url
  6345. .
  6346. <p><a href="/url">foo</a> bar</p>
  6347. ````````````````````````````````
  6348. If you just want bracketed text, you can backslash-escape the
  6349. opening bracket to avoid links:
  6350. ```````````````````````````````` example
  6351. \[foo]
  6352. [foo]: /url "title"
  6353. .
  6354. <p>[foo]</p>
  6355. ````````````````````````````````
  6356. Note that this is a link, because a link label ends with the first
  6357. following closing bracket:
  6358. ```````````````````````````````` example
  6359. [foo*]: /url
  6360. *[foo*]
  6361. .
  6362. <p>*<a href="/url">foo*</a></p>
  6363. ````````````````````````````````
  6364. Full and compact references take precedence over shortcut
  6365. references:
  6366. ```````````````````````````````` example
  6367. [foo][bar]
  6368. [foo]: /url1
  6369. [bar]: /url2
  6370. .
  6371. <p><a href="/url2">foo</a></p>
  6372. ````````````````````````````````
  6373. ```````````````````````````````` example
  6374. [foo][]
  6375. [foo]: /url1
  6376. .
  6377. <p><a href="/url1">foo</a></p>
  6378. ````````````````````````````````
  6379. Inline links also take precedence:
  6380. ```````````````````````````````` example
  6381. [foo]()
  6382. [foo]: /url1
  6383. .
  6384. <p><a href="">foo</a></p>
  6385. ````````````````````````````````
  6386. ```````````````````````````````` example
  6387. [foo](not a link)
  6388. [foo]: /url1
  6389. .
  6390. <p><a href="/url1">foo</a>(not a link)</p>
  6391. ````````````````````````````````
  6392. In the following case `[bar][baz]` is parsed as a reference,
  6393. `[foo]` as normal text:
  6394. ```````````````````````````````` example
  6395. [foo][bar][baz]
  6396. [baz]: /url
  6397. .
  6398. <p>[foo]<a href="/url">bar</a></p>
  6399. ````````````````````````````````
  6400. Here, though, `[foo][bar]` is parsed as a reference, since
  6401. `[bar]` is defined:
  6402. ```````````````````````````````` example
  6403. [foo][bar][baz]
  6404. [baz]: /url1
  6405. [bar]: /url2
  6406. .
  6407. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6408. ````````````````````````````````
  6409. Here `[foo]` is not parsed as a shortcut reference, because it
  6410. is followed by a link label (even though `[bar]` is not defined):
  6411. ```````````````````````````````` example
  6412. [foo][bar][baz]
  6413. [baz]: /url1
  6414. [foo]: /url2
  6415. .
  6416. <p>[foo]<a href="/url1">bar</a></p>
  6417. ````````````````````````````````
  6418. ## Images
  6419. Syntax for images is like the syntax for links, with one
  6420. difference. Instead of [link text], we have an
  6421. [image description](@). The rules for this are the
  6422. same as for [link text], except that (a) an
  6423. image description starts with `![` rather than `[`, and
  6424. (b) an image description may contain links.
  6425. An image description has inline elements
  6426. as its contents. When an image is rendered to HTML,
  6427. this is standardly used as the image's `alt` attribute.
  6428. ```````````````````````````````` example
  6429. ![foo](/url "title")
  6430. .
  6431. <p><img src="/url" alt="foo" title="title" /></p>
  6432. ````````````````````````````````
  6433. ```````````````````````````````` example
  6434. ![foo *bar*]
  6435. [foo *bar*]: train.jpg "train & tracks"
  6436. .
  6437. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6438. ````````````````````````````````
  6439. ```````````````````````````````` example
  6440. ![foo ![bar](/url)](/url2)
  6441. .
  6442. <p><img src="/url2" alt="foo bar" /></p>
  6443. ````````````````````````````````
  6444. ```````````````````````````````` example
  6445. ![foo [bar](/url)](/url2)
  6446. .
  6447. <p><img src="/url2" alt="foo bar" /></p>
  6448. ````````````````````````````````
  6449. Though this spec is concerned with parsing, not rendering, it is
  6450. recommended that in rendering to HTML, only the plain string content
  6451. of the [image description] be used. Note that in
  6452. the above example, the alt attribute's value is `foo bar`, not `foo
  6453. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6454. content is rendered, without formatting.
  6455. ```````````````````````````````` example
  6456. ![foo *bar*][]
  6457. [foo *bar*]: train.jpg "train & tracks"
  6458. .
  6459. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6460. ````````````````````````````````
  6461. ```````````````````````````````` example
  6462. ![foo *bar*][foobar]
  6463. [FOOBAR]: train.jpg "train & tracks"
  6464. .
  6465. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6466. ````````````````````````````````
  6467. ```````````````````````````````` example
  6468. ![foo](train.jpg)
  6469. .
  6470. <p><img src="train.jpg" alt="foo" /></p>
  6471. ````````````````````````````````
  6472. ```````````````````````````````` example
  6473. My ![foo bar](/path/to/train.jpg "title" )
  6474. .
  6475. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6476. ````````````````````````````````
  6477. ```````````````````````````````` example
  6478. ![foo](<url>)
  6479. .
  6480. <p><img src="url" alt="foo" /></p>
  6481. ````````````````````````````````
  6482. ```````````````````````````````` example
  6483. ![](/url)
  6484. .
  6485. <p><img src="/url" alt="" /></p>
  6486. ````````````````````````````````
  6487. Reference-style:
  6488. ```````````````````````````````` example
  6489. ![foo][bar]
  6490. [bar]: /url
  6491. .
  6492. <p><img src="/url" alt="foo" /></p>
  6493. ````````````````````````````````
  6494. ```````````````````````````````` example
  6495. ![foo][bar]
  6496. [BAR]: /url
  6497. .
  6498. <p><img src="/url" alt="foo" /></p>
  6499. ````````````````````````````````
  6500. Collapsed:
  6501. ```````````````````````````````` example
  6502. ![foo][]
  6503. [foo]: /url "title"
  6504. .
  6505. <p><img src="/url" alt="foo" title="title" /></p>
  6506. ````````````````````````````````
  6507. ```````````````````````````````` example
  6508. ![*foo* bar][]
  6509. [*foo* bar]: /url "title"
  6510. .
  6511. <p><img src="/url" alt="foo bar" title="title" /></p>
  6512. ````````````````````````````````
  6513. The labels are case-insensitive:
  6514. ```````````````````````````````` example
  6515. ![Foo][]
  6516. [foo]: /url "title"
  6517. .
  6518. <p><img src="/url" alt="Foo" title="title" /></p>
  6519. ````````````````````````````````
  6520. As with reference links, [whitespace] is not allowed
  6521. between the two sets of brackets:
  6522. ```````````````````````````````` example
  6523. ![foo]
  6524. []
  6525. [foo]: /url "title"
  6526. .
  6527. <p><img src="/url" alt="foo" title="title" />
  6528. []</p>
  6529. ````````````````````````````````
  6530. Shortcut:
  6531. ```````````````````````````````` example
  6532. ![foo]
  6533. [foo]: /url "title"
  6534. .
  6535. <p><img src="/url" alt="foo" title="title" /></p>
  6536. ````````````````````````````````
  6537. ```````````````````````````````` example
  6538. ![*foo* bar]
  6539. [*foo* bar]: /url "title"
  6540. .
  6541. <p><img src="/url" alt="foo bar" title="title" /></p>
  6542. ````````````````````````````````
  6543. Note that link labels cannot contain unescaped brackets:
  6544. ```````````````````````````````` example
  6545. ![[foo]]
  6546. [[foo]]: /url "title"
  6547. .
  6548. <p>![[foo]]</p>
  6549. <p>[[foo]]: /url &quot;title&quot;</p>
  6550. ````````````````````````````````
  6551. The link labels are case-insensitive:
  6552. ```````````````````````````````` example
  6553. ![Foo]
  6554. [foo]: /url "title"
  6555. .
  6556. <p><img src="/url" alt="Foo" title="title" /></p>
  6557. ````````````````````````````````
  6558. If you just want a literal `!` followed by bracketed text, you can
  6559. backslash-escape the opening `[`:
  6560. ```````````````````````````````` example
  6561. !\[foo]
  6562. [foo]: /url "title"
  6563. .
  6564. <p>![foo]</p>
  6565. ````````````````````````````````
  6566. If you want a link after a literal `!`, backslash-escape the
  6567. `!`:
  6568. ```````````````````````````````` example
  6569. \![foo]
  6570. [foo]: /url "title"
  6571. .
  6572. <p>!<a href="/url" title="title">foo</a></p>
  6573. ````````````````````````````````
  6574. ## Autolinks
  6575. [Autolink](@)s are absolute URIs and email addresses inside
  6576. `<` and `>`. They are parsed as links, with the URL or email address
  6577. as the link label.
  6578. A [URI autolink](@) consists of `<`, followed by an
  6579. [absolute URI] followed by `>`. It is parsed as
  6580. a link to the URI, with the URI as the link's label.
  6581. An [absolute URI](@),
  6582. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6583. followed by zero or more characters other [ASCII control
  6584. characters][ASCII control character] or [whitespace][] , `<`, and `>`.
  6585. If the URI includes these characters, they must be percent-encoded
  6586. (e.g. `%20` for a space).
  6587. For purposes of this spec, a [scheme](@) is any sequence
  6588. of 2--32 characters beginning with an ASCII letter and followed
  6589. by any combination of ASCII letters, digits, or the symbols plus
  6590. ("+"), period ("."), or hyphen ("-").
  6591. Here are some valid autolinks:
  6592. ```````````````````````````````` example
  6593. <http://foo.bar.baz>
  6594. .
  6595. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6596. ````````````````````````````````
  6597. ```````````````````````````````` example
  6598. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6599. .
  6600. <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>
  6601. ````````````````````````````````
  6602. ```````````````````````````````` example
  6603. <irc://foo.bar:2233/baz>
  6604. .
  6605. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6606. ````````````````````````````````
  6607. Uppercase is also fine:
  6608. ```````````````````````````````` example
  6609. <MAILTO:FOO@BAR.BAZ>
  6610. .
  6611. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6612. ````````````````````````````````
  6613. Note that many strings that count as [absolute URIs] for
  6614. purposes of this spec are not valid URIs, because their
  6615. schemes are not registered or because of other problems
  6616. with their syntax:
  6617. ```````````````````````````````` example
  6618. <a+b+c:d>
  6619. .
  6620. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6621. ````````````````````````````````
  6622. ```````````````````````````````` example
  6623. <made-up-scheme://foo,bar>
  6624. .
  6625. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6626. ````````````````````````````````
  6627. ```````````````````````````````` example
  6628. <http://../>
  6629. .
  6630. <p><a href="http://../">http://../</a></p>
  6631. ````````````````````````````````
  6632. ```````````````````````````````` example
  6633. <localhost:5001/foo>
  6634. .
  6635. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6636. ````````````````````````````````
  6637. Spaces are not allowed in autolinks:
  6638. ```````````````````````````````` example
  6639. <http://foo.bar/baz bim>
  6640. .
  6641. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6642. ````````````````````````````````
  6643. Backslash-escapes do not work inside autolinks:
  6644. ```````````````````````````````` example
  6645. <http://example.com/\[\>
  6646. .
  6647. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6648. ````````````````````````````````
  6649. An [email autolink](@)
  6650. consists of `<`, followed by an [email address],
  6651. followed by `>`. The link's label is the email address,
  6652. and the URL is `mailto:` followed by the email address.
  6653. An [email address](@),
  6654. for these purposes, is anything that matches
  6655. the [non-normative regex from the HTML5
  6656. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6657. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6658. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6659. Examples of email autolinks:
  6660. ```````````````````````````````` example
  6661. <foo@bar.example.com>
  6662. .
  6663. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6664. ````````````````````````````````
  6665. ```````````````````````````````` example
  6666. <foo+special@Bar.baz-bar0.com>
  6667. .
  6668. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6669. ````````````````````````````````
  6670. Backslash-escapes do not work inside email autolinks:
  6671. ```````````````````````````````` example
  6672. <foo\+@bar.example.com>
  6673. .
  6674. <p>&lt;foo+@bar.example.com&gt;</p>
  6675. ````````````````````````````````
  6676. These are not autolinks:
  6677. ```````````````````````````````` example
  6678. <>
  6679. .
  6680. <p>&lt;&gt;</p>
  6681. ````````````````````````````````
  6682. ```````````````````````````````` example
  6683. < http://foo.bar >
  6684. .
  6685. <p>&lt; http://foo.bar &gt;</p>
  6686. ````````````````````````````````
  6687. ```````````````````````````````` example
  6688. <m:abc>
  6689. .
  6690. <p>&lt;m:abc&gt;</p>
  6691. ````````````````````````````````
  6692. ```````````````````````````````` example
  6693. <foo.bar.baz>
  6694. .
  6695. <p>&lt;foo.bar.baz&gt;</p>
  6696. ````````````````````````````````
  6697. ```````````````````````````````` example
  6698. http://example.com
  6699. .
  6700. <p>http://example.com</p>
  6701. ````````````````````````````````
  6702. ```````````````````````````````` example
  6703. foo@bar.example.com
  6704. .
  6705. <p>foo@bar.example.com</p>
  6706. ````````````````````````````````
  6707. ## Raw HTML
  6708. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6709. raw HTML tag and will be rendered in HTML without escaping.
  6710. Tag and attribute names are not limited to current HTML tags,
  6711. so custom tags (and even, say, DocBook tags) may be used.
  6712. Here is the grammar for tags:
  6713. A [tag name](@) consists of an ASCII letter
  6714. followed by zero or more ASCII letters, digits, or
  6715. hyphens (`-`).
  6716. An [attribute](@) consists of [whitespace],
  6717. an [attribute name], and an optional
  6718. [attribute value specification].
  6719. An [attribute name](@)
  6720. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6721. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6722. specification restricted to ASCII. HTML5 is laxer.)
  6723. An [attribute value specification](@)
  6724. consists of optional [whitespace],
  6725. a `=` character, optional [whitespace], and an [attribute
  6726. value].
  6727. An [attribute value](@)
  6728. consists of an [unquoted attribute value],
  6729. a [single-quoted attribute value], or a [double-quoted attribute value].
  6730. An [unquoted attribute value](@)
  6731. is a nonempty string of characters not
  6732. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6733. A [single-quoted attribute value](@)
  6734. consists of `'`, zero or more
  6735. characters not including `'`, and a final `'`.
  6736. A [double-quoted attribute value](@)
  6737. consists of `"`, zero or more
  6738. characters not including `"`, and a final `"`.
  6739. An [open tag](@) consists of a `<` character, a [tag name],
  6740. zero or more [attributes], optional [whitespace], an optional `/`
  6741. character, and a `>` character.
  6742. A [closing tag](@) consists of the string `</`, a
  6743. [tag name], optional [whitespace], and the character `>`.
  6744. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6745. where *text* does not start with `>` or `->`, does not end with `-`,
  6746. and does not contain `--`. (See the
  6747. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6748. A [processing instruction](@)
  6749. consists of the string `<?`, a string
  6750. of characters not including the string `?>`, and the string
  6751. `?>`.
  6752. A [declaration](@) consists of the string `<!`, an ASCII letter, zero or more
  6753. characters not including the character `>`, and the character `>`.
  6754. A [CDATA section](@) consists of
  6755. the string `<![CDATA[`, a string of characters not including the string
  6756. `]]>`, and the string `]]>`.
  6757. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6758. an [HTML comment], a [processing instruction], a [declaration],
  6759. or a [CDATA section].
  6760. Here are some simple open tags:
  6761. ```````````````````````````````` example
  6762. <a><bab><c2c>
  6763. .
  6764. <p><a><bab><c2c></p>
  6765. ````````````````````````````````
  6766. Empty elements:
  6767. ```````````````````````````````` example
  6768. <a/><b2/>
  6769. .
  6770. <p><a/><b2/></p>
  6771. ````````````````````````````````
  6772. [Whitespace] is allowed:
  6773. ```````````````````````````````` example
  6774. <a /><b2
  6775. data="foo" >
  6776. .
  6777. <p><a /><b2
  6778. data="foo" ></p>
  6779. ````````````````````````````````
  6780. With attributes:
  6781. ```````````````````````````````` example
  6782. <a foo="bar" bam = 'baz <em>"</em>'
  6783. _boolean zoop:33=zoop:33 />
  6784. .
  6785. <p><a foo="bar" bam = 'baz <em>"</em>'
  6786. _boolean zoop:33=zoop:33 /></p>
  6787. ````````````````````````````````
  6788. Custom tag names can be used:
  6789. ```````````````````````````````` example
  6790. Foo <responsive-image src="foo.jpg" />
  6791. .
  6792. <p>Foo <responsive-image src="foo.jpg" /></p>
  6793. ````````````````````````````````
  6794. Illegal tag names, not parsed as HTML:
  6795. ```````````````````````````````` example
  6796. <33> <__>
  6797. .
  6798. <p>&lt;33&gt; &lt;__&gt;</p>
  6799. ````````````````````````````````
  6800. Illegal attribute names:
  6801. ```````````````````````````````` example
  6802. <a h*#ref="hi">
  6803. .
  6804. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6805. ````````````````````````````````
  6806. Illegal attribute values:
  6807. ```````````````````````````````` example
  6808. <a href="hi'> <a href=hi'>
  6809. .
  6810. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6811. ````````````````````````````````
  6812. Illegal [whitespace]:
  6813. ```````````````````````````````` example
  6814. < a><
  6815. foo><bar/ >
  6816. <foo bar=baz
  6817. bim!bop />
  6818. .
  6819. <p>&lt; a&gt;&lt;
  6820. foo&gt;&lt;bar/ &gt;
  6821. &lt;foo bar=baz
  6822. bim!bop /&gt;</p>
  6823. ````````````````````````````````
  6824. Missing [whitespace]:
  6825. ```````````````````````````````` example
  6826. <a href='bar'title=title>
  6827. .
  6828. <p>&lt;a href='bar'title=title&gt;</p>
  6829. ````````````````````````````````
  6830. Closing tags:
  6831. ```````````````````````````````` example
  6832. </a></foo >
  6833. .
  6834. <p></a></foo ></p>
  6835. ````````````````````````````````
  6836. Illegal attributes in closing tag:
  6837. ```````````````````````````````` example
  6838. </a href="foo">
  6839. .
  6840. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6841. ````````````````````````````````
  6842. Comments:
  6843. ```````````````````````````````` example
  6844. foo <!-- this is a
  6845. comment - with hyphen -->
  6846. .
  6847. <p>foo <!-- this is a
  6848. comment - with hyphen --></p>
  6849. ````````````````````````````````
  6850. ```````````````````````````````` example
  6851. foo <!-- not a comment -- two hyphens -->
  6852. .
  6853. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6854. ````````````````````````````````
  6855. Not comments:
  6856. ```````````````````````````````` example
  6857. foo <!--> foo -->
  6858. foo <!-- foo--->
  6859. .
  6860. <p>foo &lt;!--&gt; foo --&gt;</p>
  6861. <p>foo &lt;!-- foo---&gt;</p>
  6862. ````````````````````````````````
  6863. Processing instructions:
  6864. ```````````````````````````````` example
  6865. foo <?php echo $a; ?>
  6866. .
  6867. <p>foo <?php echo $a; ?></p>
  6868. ````````````````````````````````
  6869. Declarations:
  6870. ```````````````````````````````` example
  6871. foo <!ELEMENT br EMPTY>
  6872. .
  6873. <p>foo <!ELEMENT br EMPTY></p>
  6874. ````````````````````````````````
  6875. CDATA sections:
  6876. ```````````````````````````````` example
  6877. foo <![CDATA[>&<]]>
  6878. .
  6879. <p>foo <![CDATA[>&<]]></p>
  6880. ````````````````````````````````
  6881. Entity and numeric character references are preserved in HTML
  6882. attributes:
  6883. ```````````````````````````````` example
  6884. foo <a href="&ouml;">
  6885. .
  6886. <p>foo <a href="&ouml;"></p>
  6887. ````````````````````````````````
  6888. Backslash escapes do not work in HTML attributes:
  6889. ```````````````````````````````` example
  6890. foo <a href="\*">
  6891. .
  6892. <p>foo <a href="\*"></p>
  6893. ````````````````````````````````
  6894. ```````````````````````````````` example
  6895. <a href="\"">
  6896. .
  6897. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6898. ````````````````````````````````
  6899. ## Hard line breaks
  6900. A line break (not in a code span or HTML tag) that is preceded
  6901. by two or more spaces and does not occur at the end of a block
  6902. is parsed as a [hard line break](@) (rendered
  6903. in HTML as a `<br />` tag):
  6904. ```````````````````````````````` example
  6905. foo
  6906. baz
  6907. .
  6908. <p>foo<br />
  6909. baz</p>
  6910. ````````````````````````````````
  6911. For a more visible alternative, a backslash before the
  6912. [line ending] may be used instead of two spaces:
  6913. ```````````````````````````````` example
  6914. foo\
  6915. baz
  6916. .
  6917. <p>foo<br />
  6918. baz</p>
  6919. ````````````````````````````````
  6920. More than two spaces can be used:
  6921. ```````````````````````````````` example
  6922. foo
  6923. baz
  6924. .
  6925. <p>foo<br />
  6926. baz</p>
  6927. ````````````````````````````````
  6928. Leading spaces at the beginning of the next line are ignored:
  6929. ```````````````````````````````` example
  6930. foo
  6931. bar
  6932. .
  6933. <p>foo<br />
  6934. bar</p>
  6935. ````````````````````````````````
  6936. ```````````````````````````````` example
  6937. foo\
  6938. bar
  6939. .
  6940. <p>foo<br />
  6941. bar</p>
  6942. ````````````````````````````````
  6943. Line breaks can occur inside emphasis, links, and other constructs
  6944. that allow inline content:
  6945. ```````````````````````````````` example
  6946. *foo
  6947. bar*
  6948. .
  6949. <p><em>foo<br />
  6950. bar</em></p>
  6951. ````````````````````````````````
  6952. ```````````````````````````````` example
  6953. *foo\
  6954. bar*
  6955. .
  6956. <p><em>foo<br />
  6957. bar</em></p>
  6958. ````````````````````````````````
  6959. Line breaks do not occur inside code spans
  6960. ```````````````````````````````` example
  6961. `code
  6962. span`
  6963. .
  6964. <p><code>code span</code></p>
  6965. ````````````````````````````````
  6966. ```````````````````````````````` example
  6967. `code\
  6968. span`
  6969. .
  6970. <p><code>code\ span</code></p>
  6971. ````````````````````````````````
  6972. or HTML tags:
  6973. ```````````````````````````````` example
  6974. <a href="foo
  6975. bar">
  6976. .
  6977. <p><a href="foo
  6978. bar"></p>
  6979. ````````````````````````````````
  6980. ```````````````````````````````` example
  6981. <a href="foo\
  6982. bar">
  6983. .
  6984. <p><a href="foo\
  6985. bar"></p>
  6986. ````````````````````````````````
  6987. Hard line breaks are for separating inline content within a block.
  6988. Neither syntax for hard line breaks works at the end of a paragraph or
  6989. other block element:
  6990. ```````````````````````````````` example
  6991. foo\
  6992. .
  6993. <p>foo\</p>
  6994. ````````````````````````````````
  6995. ```````````````````````````````` example
  6996. foo
  6997. .
  6998. <p>foo</p>
  6999. ````````````````````````````````
  7000. ```````````````````````````````` example
  7001. ### foo\
  7002. .
  7003. <h3>foo\</h3>
  7004. ````````````````````````````````
  7005. ```````````````````````````````` example
  7006. ### foo
  7007. .
  7008. <h3>foo</h3>
  7009. ````````````````````````````````
  7010. ## Soft line breaks
  7011. A regular line break (not in a code span or HTML tag) that is not
  7012. preceded by two or more spaces or a backslash is parsed as a
  7013. [softbreak](@). (A softbreak may be rendered in HTML either as a
  7014. [line ending] or as a space. The result will be the same in
  7015. browsers. In the examples here, a [line ending] will be used.)
  7016. ```````````````````````````````` example
  7017. foo
  7018. baz
  7019. .
  7020. <p>foo
  7021. baz</p>
  7022. ````````````````````````````````
  7023. Spaces at the end of the line and beginning of the next line are
  7024. removed:
  7025. ```````````````````````````````` example
  7026. foo
  7027. baz
  7028. .
  7029. <p>foo
  7030. baz</p>
  7031. ````````````````````````````````
  7032. A conforming parser may render a soft line break in HTML either as a
  7033. line break or as a space.
  7034. A renderer may also provide an option to render soft line breaks
  7035. as hard line breaks.
  7036. ## Textual content
  7037. Any characters not given an interpretation by the above rules will
  7038. be parsed as plain textual content.
  7039. ```````````````````````````````` example
  7040. hello $.;'there
  7041. .
  7042. <p>hello $.;'there</p>
  7043. ````````````````````````````````
  7044. ```````````````````````````````` example
  7045. Foo χρῆν
  7046. .
  7047. <p>Foo χρῆν</p>
  7048. ````````````````````````````````
  7049. Internal spaces are preserved verbatim:
  7050. ```````````````````````````````` example
  7051. Multiple spaces
  7052. .
  7053. <p>Multiple spaces</p>
  7054. ````````````````````````````````
  7055. <!-- END TESTS -->
  7056. # Appendix: A parsing strategy
  7057. In this appendix we describe some features of the parsing strategy
  7058. used in the CommonMark reference implementations.
  7059. ## Overview
  7060. Parsing has two phases:
  7061. 1. In the first phase, lines of input are consumed and the block
  7062. structure of the document---its division into paragraphs, block quotes,
  7063. list items, and so on---is constructed. Text is assigned to these
  7064. blocks but not parsed. Link reference definitions are parsed and a
  7065. map of links is constructed.
  7066. 2. In the second phase, the raw text contents of paragraphs and headings
  7067. are parsed into sequences of Markdown inline elements (strings,
  7068. code spans, links, emphasis, and so on), using the map of link
  7069. references constructed in phase 1.
  7070. At each point in processing, the document is represented as a tree of
  7071. **blocks**. The root of the tree is a `document` block. The `document`
  7072. may have any number of other blocks as **children**. These children
  7073. may, in turn, have other blocks as children. The last child of a block
  7074. is normally considered **open**, meaning that subsequent lines of input
  7075. can alter its contents. (Blocks that are not open are **closed**.)
  7076. Here, for example, is a possible document tree, with the open blocks
  7077. marked by arrows:
  7078. ``` tree
  7079. -> document
  7080. -> block_quote
  7081. paragraph
  7082. "Lorem ipsum dolor\nsit amet."
  7083. -> list (type=bullet tight=true bullet_char=-)
  7084. list_item
  7085. paragraph
  7086. "Qui *quodsi iracundia*"
  7087. -> list_item
  7088. -> paragraph
  7089. "aliquando id"
  7090. ```
  7091. ## Phase 1: block structure
  7092. Each line that is processed has an effect on this tree. The line is
  7093. analyzed and, depending on its contents, the document may be altered
  7094. in one or more of the following ways:
  7095. 1. One or more open blocks may be closed.
  7096. 2. One or more new blocks may be created as children of the
  7097. last open block.
  7098. 3. Text may be added to the last (deepest) open block remaining
  7099. on the tree.
  7100. Once a line has been incorporated into the tree in this way,
  7101. it can be discarded, so input can be read in a stream.
  7102. For each line, we follow this procedure:
  7103. 1. First we iterate through the open blocks, starting with the
  7104. root document, and descending through last children down to the last
  7105. open block. Each block imposes a condition that the line must satisfy
  7106. if the block is to remain open. For example, a block quote requires a
  7107. `>` character. A paragraph requires a non-blank line.
  7108. In this phase we may match all or just some of the open
  7109. blocks. But we cannot close unmatched blocks yet, because we may have a
  7110. [lazy continuation line].
  7111. 2. Next, after consuming the continuation markers for existing
  7112. blocks, we look for new block starts (e.g. `>` for a block quote).
  7113. If we encounter a new block start, we close any blocks unmatched
  7114. in step 1 before creating the new block as a child of the last
  7115. matched container block.
  7116. 3. Finally, we look at the remainder of the line (after block
  7117. markers like `>`, list markers, and indentation have been consumed).
  7118. This is text that can be incorporated into the last open
  7119. block (a paragraph, code block, heading, or raw HTML).
  7120. Setext headings are formed when we see a line of a paragraph
  7121. that is a [setext heading underline].
  7122. Reference link definitions are detected when a paragraph is closed;
  7123. the accumulated text lines are parsed to see if they begin with
  7124. one or more reference link definitions. Any remainder becomes a
  7125. normal paragraph.
  7126. We can see how this works by considering how the tree above is
  7127. generated by four lines of Markdown:
  7128. ``` markdown
  7129. > Lorem ipsum dolor
  7130. sit amet.
  7131. > - Qui *quodsi iracundia*
  7132. > - aliquando id
  7133. ```
  7134. At the outset, our document model is just
  7135. ``` tree
  7136. -> document
  7137. ```
  7138. The first line of our text,
  7139. ``` markdown
  7140. > Lorem ipsum dolor
  7141. ```
  7142. causes a `block_quote` block to be created as a child of our
  7143. open `document` block, and a `paragraph` block as a child of
  7144. the `block_quote`. Then the text is added to the last open
  7145. block, the `paragraph`:
  7146. ``` tree
  7147. -> document
  7148. -> block_quote
  7149. -> paragraph
  7150. "Lorem ipsum dolor"
  7151. ```
  7152. The next line,
  7153. ``` markdown
  7154. sit amet.
  7155. ```
  7156. is a "lazy continuation" of the open `paragraph`, so it gets added
  7157. to the paragraph's text:
  7158. ``` tree
  7159. -> document
  7160. -> block_quote
  7161. -> paragraph
  7162. "Lorem ipsum dolor\nsit amet."
  7163. ```
  7164. The third line,
  7165. ``` markdown
  7166. > - Qui *quodsi iracundia*
  7167. ```
  7168. causes the `paragraph` block to be closed, and a new `list` block
  7169. opened as a child of the `block_quote`. A `list_item` is also
  7170. added as a child of the `list`, and a `paragraph` as a child of
  7171. the `list_item`. The text is then added to the new `paragraph`:
  7172. ``` tree
  7173. -> document
  7174. -> block_quote
  7175. paragraph
  7176. "Lorem ipsum dolor\nsit amet."
  7177. -> list (type=bullet tight=true bullet_char=-)
  7178. -> list_item
  7179. -> paragraph
  7180. "Qui *quodsi iracundia*"
  7181. ```
  7182. The fourth line,
  7183. ``` markdown
  7184. > - aliquando id
  7185. ```
  7186. causes the `list_item` (and its child the `paragraph`) to be closed,
  7187. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7188. is added as a child of the new `list_item`, to contain the text.
  7189. We thus obtain the final tree:
  7190. ``` tree
  7191. -> document
  7192. -> block_quote
  7193. paragraph
  7194. "Lorem ipsum dolor\nsit amet."
  7195. -> list (type=bullet tight=true bullet_char=-)
  7196. list_item
  7197. paragraph
  7198. "Qui *quodsi iracundia*"
  7199. -> list_item
  7200. -> paragraph
  7201. "aliquando id"
  7202. ```
  7203. ## Phase 2: inline structure
  7204. Once all of the input has been parsed, all open blocks are closed.
  7205. We then "walk the tree," visiting every node, and parse raw
  7206. string contents of paragraphs and headings as inlines. At this
  7207. point we have seen all the link reference definitions, so we can
  7208. resolve reference links as we go.
  7209. ``` tree
  7210. document
  7211. block_quote
  7212. paragraph
  7213. str "Lorem ipsum dolor"
  7214. softbreak
  7215. str "sit amet."
  7216. list (type=bullet tight=true bullet_char=-)
  7217. list_item
  7218. paragraph
  7219. str "Qui "
  7220. emph
  7221. str "quodsi iracundia"
  7222. list_item
  7223. paragraph
  7224. str "aliquando id"
  7225. ```
  7226. Notice how the [line ending] in the first paragraph has
  7227. been parsed as a `softbreak`, and the asterisks in the first list item
  7228. have become an `emph`.
  7229. ### An algorithm for parsing nested emphasis and links
  7230. By far the trickiest part of inline parsing is handling emphasis,
  7231. strong emphasis, links, and images. This is done using the following
  7232. algorithm.
  7233. When we're parsing inlines and we hit either
  7234. - a run of `*` or `_` characters, or
  7235. - a `[` or `![`
  7236. we insert a text node with these symbols as its literal content, and we
  7237. add a pointer to this text node to the [delimiter stack](@).
  7238. The [delimiter stack] is a doubly linked list. Each
  7239. element contains a pointer to a text node, plus information about
  7240. - the type of delimiter (`[`, `![`, `*`, `_`)
  7241. - the number of delimiters,
  7242. - whether the delimiter is "active" (all are active to start), and
  7243. - whether the delimiter is a potential opener, a potential closer,
  7244. or both (which depends on what sort of characters precede
  7245. and follow the delimiters).
  7246. When we hit a `]` character, we call the *look for link or image*
  7247. procedure (see below).
  7248. When we hit the end of the input, we call the *process emphasis*
  7249. procedure (see below), with `stack_bottom` = NULL.
  7250. #### *look for link or image*
  7251. Starting at the top of the delimiter stack, we look backwards
  7252. through the stack for an opening `[` or `![` delimiter.
  7253. - If we don't find one, we return a literal text node `]`.
  7254. - If we do find one, but it's not *active*, we remove the inactive
  7255. delimiter from the stack, and return a literal text node `]`.
  7256. - If we find one and it's active, then we parse ahead to see if
  7257. we have an inline link/image, reference link/image, compact reference
  7258. link/image, or shortcut reference link/image.
  7259. + If we don't, then we remove the opening delimiter from the
  7260. delimiter stack and return a literal text node `]`.
  7261. + If we do, then
  7262. * We return a link or image node whose children are the inlines
  7263. after the text node pointed to by the opening delimiter.
  7264. * We run *process emphasis* on these inlines, with the `[` opener
  7265. as `stack_bottom`.
  7266. * We remove the opening delimiter.
  7267. * If we have a link (and not an image), we also set all
  7268. `[` delimiters before the opening delimiter to *inactive*. (This
  7269. will prevent us from getting links within links.)
  7270. #### *process emphasis*
  7271. Parameter `stack_bottom` sets a lower bound to how far we
  7272. descend in the [delimiter stack]. If it is NULL, we can
  7273. go all the way to the bottom. Otherwise, we stop before
  7274. visiting `stack_bottom`.
  7275. Let `current_position` point to the element on the [delimiter stack]
  7276. just above `stack_bottom` (or the first element if `stack_bottom`
  7277. is NULL).
  7278. We keep track of the `openers_bottom` for each delimiter
  7279. type (`*`, `_`) and each length of the closing delimiter run
  7280. (modulo 3). Initialize this to `stack_bottom`.
  7281. Then we repeat the following until we run out of potential
  7282. closers:
  7283. - Move `current_position` forward in the delimiter stack (if needed)
  7284. until we find the first potential closer with delimiter `*` or `_`.
  7285. (This will be the potential closer closest
  7286. to the beginning of the input -- the first one in parse order.)
  7287. - Now, look back in the stack (staying above `stack_bottom` and
  7288. the `openers_bottom` for this delimiter type) for the
  7289. first matching potential opener ("matching" means same delimiter).
  7290. - If one is found:
  7291. + Figure out whether we have emphasis or strong emphasis:
  7292. if both closer and opener spans have length >= 2, we have
  7293. strong, otherwise regular.
  7294. + Insert an emph or strong emph node accordingly, after
  7295. the text node corresponding to the opener.
  7296. + Remove any delimiters between the opener and closer from
  7297. the delimiter stack.
  7298. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7299. from the opening and closing text nodes. If they become empty
  7300. as a result, remove them and remove the corresponding element
  7301. of the delimiter stack. If the closing node is removed, reset
  7302. `current_position` to the next element in the stack.
  7303. - If none is found:
  7304. + Set `openers_bottom` to the element before `current_position`.
  7305. (We know that there are no openers for this kind of closer up to and
  7306. including this point, so this puts a lower bound on future searches.)
  7307. + If the closer at `current_position` is not a potential opener,
  7308. remove it from the delimiter stack (since we know it can't
  7309. be a closer either).
  7310. + Advance `current_position` to the next element in the stack.
  7311. After we're done, we remove all delimiters above `stack_bottom` from the
  7312. delimiter stack.