aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: c62df5bb528a30311bb3c85a300e889ce1ddba15 (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 an 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 paragraphs 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*, and *M* is a list marker of width *W*,
  3496. then the result of prepending *M* to the first line of *Ls*, and
  3497. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3498. item with *Bs* as its contents.
  3499. If a line is empty, then it need not be indented. The type of the
  3500. list item (bullet or ordered) is determined by the type of its list
  3501. marker. If the list item is ordered, then it is also assigned a
  3502. start number, based on the ordered list marker.
  3503. Here are some list items that start with a blank line but are not empty:
  3504. ```````````````````````````````` example
  3505. -
  3506. foo
  3507. -
  3508. ```
  3509. bar
  3510. ```
  3511. -
  3512. baz
  3513. .
  3514. <ul>
  3515. <li>foo</li>
  3516. <li>
  3517. <pre><code>bar
  3518. </code></pre>
  3519. </li>
  3520. <li>
  3521. <pre><code>baz
  3522. </code></pre>
  3523. </li>
  3524. </ul>
  3525. ````````````````````````````````
  3526. When the list item starts with a blank line, the number of spaces
  3527. following the list marker doesn't change the required indentation:
  3528. ```````````````````````````````` example
  3529. -
  3530. foo
  3531. .
  3532. <ul>
  3533. <li>foo</li>
  3534. </ul>
  3535. ````````````````````````````````
  3536. A list item can begin with at most one blank line.
  3537. In the following example, `foo` is not part of the list
  3538. item:
  3539. ```````````````````````````````` example
  3540. -
  3541. foo
  3542. .
  3543. <ul>
  3544. <li></li>
  3545. </ul>
  3546. <p>foo</p>
  3547. ````````````````````````````````
  3548. Here is an empty bullet list item:
  3549. ```````````````````````````````` example
  3550. - foo
  3551. -
  3552. - bar
  3553. .
  3554. <ul>
  3555. <li>foo</li>
  3556. <li></li>
  3557. <li>bar</li>
  3558. </ul>
  3559. ````````````````````````````````
  3560. It does not matter whether there are spaces following the [list marker]:
  3561. ```````````````````````````````` example
  3562. - foo
  3563. -
  3564. - bar
  3565. .
  3566. <ul>
  3567. <li>foo</li>
  3568. <li></li>
  3569. <li>bar</li>
  3570. </ul>
  3571. ````````````````````````````````
  3572. Here is an empty ordered list item:
  3573. ```````````````````````````````` example
  3574. 1. foo
  3575. 2.
  3576. 3. bar
  3577. .
  3578. <ol>
  3579. <li>foo</li>
  3580. <li></li>
  3581. <li>bar</li>
  3582. </ol>
  3583. ````````````````````````````````
  3584. A list may start or end with an empty list item:
  3585. ```````````````````````````````` example
  3586. *
  3587. .
  3588. <ul>
  3589. <li></li>
  3590. </ul>
  3591. ````````````````````````````````
  3592. However, an empty list item cannot interrupt a paragraph:
  3593. ```````````````````````````````` example
  3594. foo
  3595. *
  3596. foo
  3597. 1.
  3598. .
  3599. <p>foo
  3600. *</p>
  3601. <p>foo
  3602. 1.</p>
  3603. ````````````````````````````````
  3604. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3605. according to rule #1, #2, or #3, then the result of indenting each line
  3606. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3607. list item with the same contents and attributes. If a line is
  3608. empty, then it need not be indented.
  3609. Indented one space:
  3610. ```````````````````````````````` example
  3611. 1. A paragraph
  3612. with two lines.
  3613. indented code
  3614. > A block quote.
  3615. .
  3616. <ol>
  3617. <li>
  3618. <p>A paragraph
  3619. with two lines.</p>
  3620. <pre><code>indented code
  3621. </code></pre>
  3622. <blockquote>
  3623. <p>A block quote.</p>
  3624. </blockquote>
  3625. </li>
  3626. </ol>
  3627. ````````````````````````````````
  3628. Indented two spaces:
  3629. ```````````````````````````````` example
  3630. 1. A paragraph
  3631. with two lines.
  3632. indented code
  3633. > A block quote.
  3634. .
  3635. <ol>
  3636. <li>
  3637. <p>A paragraph
  3638. with two lines.</p>
  3639. <pre><code>indented code
  3640. </code></pre>
  3641. <blockquote>
  3642. <p>A block quote.</p>
  3643. </blockquote>
  3644. </li>
  3645. </ol>
  3646. ````````````````````````````````
  3647. Indented three spaces:
  3648. ```````````````````````````````` example
  3649. 1. A paragraph
  3650. with two lines.
  3651. indented code
  3652. > A block quote.
  3653. .
  3654. <ol>
  3655. <li>
  3656. <p>A paragraph
  3657. with two lines.</p>
  3658. <pre><code>indented code
  3659. </code></pre>
  3660. <blockquote>
  3661. <p>A block quote.</p>
  3662. </blockquote>
  3663. </li>
  3664. </ol>
  3665. ````````````````````````````````
  3666. Four spaces indent gives a code block:
  3667. ```````````````````````````````` example
  3668. 1. A paragraph
  3669. with two lines.
  3670. indented code
  3671. > A block quote.
  3672. .
  3673. <pre><code>1. A paragraph
  3674. with two lines.
  3675. indented code
  3676. &gt; A block quote.
  3677. </code></pre>
  3678. ````````````````````````````````
  3679. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3680. item](#list-items) with contents *Bs*, then the result of deleting
  3681. some or all of the indentation from one or more lines in which the
  3682. next [non-whitespace character] after the indentation is
  3683. [paragraph continuation text] is a
  3684. list item with the same contents and attributes. The unindented
  3685. lines are called
  3686. [lazy continuation line](@)s.
  3687. Here is an example with [lazy continuation lines]:
  3688. ```````````````````````````````` example
  3689. 1. A paragraph
  3690. with two lines.
  3691. indented code
  3692. > A block quote.
  3693. .
  3694. <ol>
  3695. <li>
  3696. <p>A paragraph
  3697. with two lines.</p>
  3698. <pre><code>indented code
  3699. </code></pre>
  3700. <blockquote>
  3701. <p>A block quote.</p>
  3702. </blockquote>
  3703. </li>
  3704. </ol>
  3705. ````````````````````````````````
  3706. Indentation can be partially deleted:
  3707. ```````````````````````````````` example
  3708. 1. A paragraph
  3709. with two lines.
  3710. .
  3711. <ol>
  3712. <li>A paragraph
  3713. with two lines.</li>
  3714. </ol>
  3715. ````````````````````````````````
  3716. These examples show how laziness can work in nested structures:
  3717. ```````````````````````````````` example
  3718. > 1. > Blockquote
  3719. continued here.
  3720. .
  3721. <blockquote>
  3722. <ol>
  3723. <li>
  3724. <blockquote>
  3725. <p>Blockquote
  3726. continued here.</p>
  3727. </blockquote>
  3728. </li>
  3729. </ol>
  3730. </blockquote>
  3731. ````````````````````````````````
  3732. ```````````````````````````````` example
  3733. > 1. > Blockquote
  3734. > continued here.
  3735. .
  3736. <blockquote>
  3737. <ol>
  3738. <li>
  3739. <blockquote>
  3740. <p>Blockquote
  3741. continued here.</p>
  3742. </blockquote>
  3743. </li>
  3744. </ol>
  3745. </blockquote>
  3746. ````````````````````````````````
  3747. 6. **That's all.** Nothing that is not counted as a list item by rules
  3748. #1--5 counts as a [list item](#list-items).
  3749. The rules for sublists follow from the general rules
  3750. [above][List items]. A sublist must be indented the same number
  3751. of spaces a paragraph would need to be in order to be included
  3752. in the list item.
  3753. So, in this case we need two spaces indent:
  3754. ```````````````````````````````` example
  3755. - foo
  3756. - bar
  3757. - baz
  3758. - boo
  3759. .
  3760. <ul>
  3761. <li>foo
  3762. <ul>
  3763. <li>bar
  3764. <ul>
  3765. <li>baz
  3766. <ul>
  3767. <li>boo</li>
  3768. </ul>
  3769. </li>
  3770. </ul>
  3771. </li>
  3772. </ul>
  3773. </li>
  3774. </ul>
  3775. ````````````````````````````````
  3776. One is not enough:
  3777. ```````````````````````````````` example
  3778. - foo
  3779. - bar
  3780. - baz
  3781. - boo
  3782. .
  3783. <ul>
  3784. <li>foo</li>
  3785. <li>bar</li>
  3786. <li>baz</li>
  3787. <li>boo</li>
  3788. </ul>
  3789. ````````````````````````````````
  3790. Here we need four, because the list marker is wider:
  3791. ```````````````````````````````` example
  3792. 10) foo
  3793. - bar
  3794. .
  3795. <ol start="10">
  3796. <li>foo
  3797. <ul>
  3798. <li>bar</li>
  3799. </ul>
  3800. </li>
  3801. </ol>
  3802. ````````````````````````````````
  3803. Three is not enough:
  3804. ```````````````````````````````` example
  3805. 10) foo
  3806. - bar
  3807. .
  3808. <ol start="10">
  3809. <li>foo</li>
  3810. </ol>
  3811. <ul>
  3812. <li>bar</li>
  3813. </ul>
  3814. ````````````````````````````````
  3815. A list may be the first block in a list item:
  3816. ```````````````````````````````` example
  3817. - - foo
  3818. .
  3819. <ul>
  3820. <li>
  3821. <ul>
  3822. <li>foo</li>
  3823. </ul>
  3824. </li>
  3825. </ul>
  3826. ````````````````````````````````
  3827. ```````````````````````````````` example
  3828. 1. - 2. foo
  3829. .
  3830. <ol>
  3831. <li>
  3832. <ul>
  3833. <li>
  3834. <ol start="2">
  3835. <li>foo</li>
  3836. </ol>
  3837. </li>
  3838. </ul>
  3839. </li>
  3840. </ol>
  3841. ````````````````````````````````
  3842. A list item can contain a heading:
  3843. ```````````````````````````````` example
  3844. - # Foo
  3845. - Bar
  3846. ---
  3847. baz
  3848. .
  3849. <ul>
  3850. <li>
  3851. <h1>Foo</h1>
  3852. </li>
  3853. <li>
  3854. <h2>Bar</h2>
  3855. baz</li>
  3856. </ul>
  3857. ````````````````````````````````
  3858. ### Motivation
  3859. John Gruber's Markdown spec says the following about list items:
  3860. 1. "List markers typically start at the left margin, but may be indented
  3861. by up to three spaces. List markers must be followed by one or more
  3862. spaces or a tab."
  3863. 2. "To make lists look nice, you can wrap items with hanging indents....
  3864. But if you don't want to, you don't have to."
  3865. 3. "List items may consist of multiple paragraphs. Each subsequent
  3866. paragraph in a list item must be indented by either 4 spaces or one
  3867. tab."
  3868. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3869. but here again, Markdown will allow you to be lazy."
  3870. 5. "To put a blockquote within a list item, the blockquote's `>`
  3871. delimiters need to be indented."
  3872. 6. "To put a code block within a list item, the code block needs to be
  3873. indented twice — 8 spaces or two tabs."
  3874. These rules specify that a paragraph under a list item must be indented
  3875. four spaces (presumably, from the left margin, rather than the start of
  3876. the list marker, but this is not said), and that code under a list item
  3877. must be indented eight spaces instead of the usual four. They also say
  3878. that a block quote must be indented, but not by how much; however, the
  3879. example given has four spaces indentation. Although nothing is said
  3880. about other kinds of block-level content, it is certainly reasonable to
  3881. infer that *all* block elements under a list item, including other
  3882. lists, must be indented four spaces. This principle has been called the
  3883. *four-space rule*.
  3884. The four-space rule is clear and principled, and if the reference
  3885. implementation `Markdown.pl` had followed it, it probably would have
  3886. become the standard. However, `Markdown.pl` allowed paragraphs and
  3887. sublists to start with only two spaces indentation, at least on the
  3888. outer level. Worse, its behavior was inconsistent: a sublist of an
  3889. outer-level list needed two spaces indentation, but a sublist of this
  3890. sublist needed three spaces. It is not surprising, then, that different
  3891. implementations of Markdown have developed very different rules for
  3892. determining what comes under a list item. (Pandoc and python-Markdown,
  3893. for example, stuck with Gruber's syntax description and the four-space
  3894. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3895. followed `Markdown.pl`'s behavior more closely.)
  3896. Unfortunately, given the divergences between implementations, there
  3897. is no way to give a spec for list items that will be guaranteed not
  3898. to break any existing documents. However, the spec given here should
  3899. correctly handle lists formatted with either the four-space rule or
  3900. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3901. in a way that is natural for a human to read.
  3902. The strategy here is to let the width and indentation of the list marker
  3903. determine the indentation necessary for blocks to fall under the list
  3904. item, rather than having a fixed and arbitrary number. The writer can
  3905. think of the body of the list item as a unit which gets indented to the
  3906. right enough to fit the list marker (and any indentation on the list
  3907. marker). (The laziness rule, #5, then allows continuation lines to be
  3908. unindented if needed.)
  3909. This rule is superior, we claim, to any rule requiring a fixed level of
  3910. indentation from the margin. The four-space rule is clear but
  3911. unnatural. It is quite unintuitive that
  3912. ``` markdown
  3913. - foo
  3914. bar
  3915. - baz
  3916. ```
  3917. should be parsed as two lists with an intervening paragraph,
  3918. ``` html
  3919. <ul>
  3920. <li>foo</li>
  3921. </ul>
  3922. <p>bar</p>
  3923. <ul>
  3924. <li>baz</li>
  3925. </ul>
  3926. ```
  3927. as the four-space rule demands, rather than a single list,
  3928. ``` html
  3929. <ul>
  3930. <li>
  3931. <p>foo</p>
  3932. <p>bar</p>
  3933. <ul>
  3934. <li>baz</li>
  3935. </ul>
  3936. </li>
  3937. </ul>
  3938. ```
  3939. The choice of four spaces is arbitrary. It can be learned, but it is
  3940. not likely to be guessed, and it trips up beginners regularly.
  3941. Would it help to adopt a two-space rule? The problem is that such
  3942. a rule, together with the rule allowing 1--3 spaces indentation of the
  3943. initial list marker, allows text that is indented *less than* the
  3944. original list marker to be included in the list item. For example,
  3945. `Markdown.pl` parses
  3946. ``` markdown
  3947. - one
  3948. two
  3949. ```
  3950. as a single list item, with `two` a continuation paragraph:
  3951. ``` html
  3952. <ul>
  3953. <li>
  3954. <p>one</p>
  3955. <p>two</p>
  3956. </li>
  3957. </ul>
  3958. ```
  3959. and similarly
  3960. ``` markdown
  3961. > - one
  3962. >
  3963. > two
  3964. ```
  3965. as
  3966. ``` html
  3967. <blockquote>
  3968. <ul>
  3969. <li>
  3970. <p>one</p>
  3971. <p>two</p>
  3972. </li>
  3973. </ul>
  3974. </blockquote>
  3975. ```
  3976. This is extremely unintuitive.
  3977. Rather than requiring a fixed indent from the margin, we could require
  3978. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3979. may itself be indented). This proposal would remove the last anomaly
  3980. discussed. Unlike the spec presented above, it would count the following
  3981. as a list item with a subparagraph, even though the paragraph `bar`
  3982. is not indented as far as the first paragraph `foo`:
  3983. ``` markdown
  3984. 10. foo
  3985. bar
  3986. ```
  3987. Arguably this text does read like a list item with `bar` as a subparagraph,
  3988. which may count in favor of the proposal. However, on this proposal indented
  3989. code would have to be indented six spaces after the list marker. And this
  3990. would break a lot of existing Markdown, which has the pattern:
  3991. ``` markdown
  3992. 1. foo
  3993. indented code
  3994. ```
  3995. where the code is indented eight spaces. The spec above, by contrast, will
  3996. parse this text as expected, since the code block's indentation is measured
  3997. from the beginning of `foo`.
  3998. The one case that needs special treatment is a list item that *starts*
  3999. with indented code. How much indentation is required in that case, since
  4000. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  4001. that in such cases, we require one space indentation from the list marker
  4002. (and then the normal four spaces for the indented code). This will match the
  4003. four-space rule in cases where the list marker plus its initial indentation
  4004. takes four spaces (a common case), but diverge in other cases.
  4005. ## Lists
  4006. A [list](@) is a sequence of one or more
  4007. list items [of the same type]. The list items
  4008. may be separated by any number of blank lines.
  4009. Two list items are [of the same type](@)
  4010. if they begin with a [list marker] of the same type.
  4011. Two list markers are of the
  4012. same type if (a) they are bullet list markers using the same character
  4013. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  4014. delimiter (either `.` or `)`).
  4015. A list is an [ordered list](@)
  4016. if its constituent list items begin with
  4017. [ordered list markers], and a
  4018. [bullet list](@) if its constituent list
  4019. items begin with [bullet list markers].
  4020. The [start number](@)
  4021. of an [ordered list] is determined by the list number of
  4022. its initial list item. The numbers of subsequent list items are
  4023. disregarded.
  4024. A list is [loose](@) if any of its constituent
  4025. list items are separated by blank lines, or if any of its constituent
  4026. list items directly contain two block-level elements with a blank line
  4027. between them. Otherwise a list is [tight](@).
  4028. (The difference in HTML output is that paragraphs in a loose list are
  4029. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  4030. Changing the bullet or ordered list delimiter starts a new list:
  4031. ```````````````````````````````` example
  4032. - foo
  4033. - bar
  4034. + baz
  4035. .
  4036. <ul>
  4037. <li>foo</li>
  4038. <li>bar</li>
  4039. </ul>
  4040. <ul>
  4041. <li>baz</li>
  4042. </ul>
  4043. ````````````````````````````````
  4044. ```````````````````````````````` example
  4045. 1. foo
  4046. 2. bar
  4047. 3) baz
  4048. .
  4049. <ol>
  4050. <li>foo</li>
  4051. <li>bar</li>
  4052. </ol>
  4053. <ol start="3">
  4054. <li>baz</li>
  4055. </ol>
  4056. ````````````````````````````````
  4057. In CommonMark, a list can interrupt a paragraph. That is,
  4058. no blank line is needed to separate a paragraph from a following
  4059. list:
  4060. ```````````````````````````````` example
  4061. Foo
  4062. - bar
  4063. - baz
  4064. .
  4065. <p>Foo</p>
  4066. <ul>
  4067. <li>bar</li>
  4068. <li>baz</li>
  4069. </ul>
  4070. ````````````````````````````````
  4071. `Markdown.pl` does not allow this, through fear of triggering a list
  4072. via a numeral in a hard-wrapped line:
  4073. ``` markdown
  4074. The number of windows in my house is
  4075. 14. The number of doors is 6.
  4076. ```
  4077. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  4078. interrupt a paragraph, even though the same considerations might
  4079. apply.
  4080. In CommonMark, we do allow lists to interrupt paragraphs, for
  4081. two reasons. First, it is natural and not uncommon for people
  4082. to start lists without blank lines:
  4083. ``` markdown
  4084. I need to buy
  4085. - new shoes
  4086. - a coat
  4087. - a plane ticket
  4088. ```
  4089. Second, we are attracted to a
  4090. > [principle of uniformity](@):
  4091. > if a chunk of text has a certain
  4092. > meaning, it will continue to have the same meaning when put into a
  4093. > container block (such as a list item or blockquote).
  4094. (Indeed, the spec for [list items] and [block quotes] presupposes
  4095. this principle.) This principle implies that if
  4096. ``` markdown
  4097. * I need to buy
  4098. - new shoes
  4099. - a coat
  4100. - a plane ticket
  4101. ```
  4102. is a list item containing a paragraph followed by a nested sublist,
  4103. as all Markdown implementations agree it is (though the paragraph
  4104. may be rendered without `<p>` tags, since the list is "tight"),
  4105. then
  4106. ``` markdown
  4107. I need to buy
  4108. - new shoes
  4109. - a coat
  4110. - a plane ticket
  4111. ```
  4112. by itself should be a paragraph followed by a nested sublist.
  4113. Since it is well established Markdown practice to allow lists to
  4114. interrupt paragraphs inside list items, the [principle of
  4115. uniformity] requires us to allow this outside list items as
  4116. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  4117. takes a different approach, requiring blank lines before lists
  4118. even inside other list items.)
  4119. In order to solve of unwanted lists in paragraphs with
  4120. hard-wrapped numerals, we allow only lists starting with `1` to
  4121. interrupt paragraphs. Thus,
  4122. ```````````````````````````````` example
  4123. The number of windows in my house is
  4124. 14. The number of doors is 6.
  4125. .
  4126. <p>The number of windows in my house is
  4127. 14. The number of doors is 6.</p>
  4128. ````````````````````````````````
  4129. We may still get an unintended result in cases like
  4130. ```````````````````````````````` example
  4131. The number of windows in my house is
  4132. 1. The number of doors is 6.
  4133. .
  4134. <p>The number of windows in my house is</p>
  4135. <ol>
  4136. <li>The number of doors is 6.</li>
  4137. </ol>
  4138. ````````````````````````````````
  4139. but this rule should prevent most spurious list captures.
  4140. There can be any number of blank lines between items:
  4141. ```````````````````````````````` example
  4142. - foo
  4143. - bar
  4144. - baz
  4145. .
  4146. <ul>
  4147. <li>
  4148. <p>foo</p>
  4149. </li>
  4150. <li>
  4151. <p>bar</p>
  4152. </li>
  4153. <li>
  4154. <p>baz</p>
  4155. </li>
  4156. </ul>
  4157. ````````````````````````````````
  4158. ```````````````````````````````` example
  4159. - foo
  4160. - bar
  4161. - baz
  4162. bim
  4163. .
  4164. <ul>
  4165. <li>foo
  4166. <ul>
  4167. <li>bar
  4168. <ul>
  4169. <li>
  4170. <p>baz</p>
  4171. <p>bim</p>
  4172. </li>
  4173. </ul>
  4174. </li>
  4175. </ul>
  4176. </li>
  4177. </ul>
  4178. ````````````````````````````````
  4179. To separate consecutive lists of the same type, or to separate a
  4180. list from an indented code block that would otherwise be parsed
  4181. as a subparagraph of the final list item, you can insert a blank HTML
  4182. comment:
  4183. ```````````````````````````````` example
  4184. - foo
  4185. - bar
  4186. <!-- -->
  4187. - baz
  4188. - bim
  4189. .
  4190. <ul>
  4191. <li>foo</li>
  4192. <li>bar</li>
  4193. </ul>
  4194. <!-- -->
  4195. <ul>
  4196. <li>baz</li>
  4197. <li>bim</li>
  4198. </ul>
  4199. ````````````````````````````````
  4200. ```````````````````````````````` example
  4201. - foo
  4202. notcode
  4203. - foo
  4204. <!-- -->
  4205. code
  4206. .
  4207. <ul>
  4208. <li>
  4209. <p>foo</p>
  4210. <p>notcode</p>
  4211. </li>
  4212. <li>
  4213. <p>foo</p>
  4214. </li>
  4215. </ul>
  4216. <!-- -->
  4217. <pre><code>code
  4218. </code></pre>
  4219. ````````````````````````````````
  4220. List items need not be indented to the same level. The following
  4221. list items will be treated as items at the same list level,
  4222. since none is indented enough to belong to the previous list
  4223. item:
  4224. ```````````````````````````````` example
  4225. - a
  4226. - b
  4227. - c
  4228. - d
  4229. - e
  4230. - f
  4231. - g
  4232. .
  4233. <ul>
  4234. <li>a</li>
  4235. <li>b</li>
  4236. <li>c</li>
  4237. <li>d</li>
  4238. <li>e</li>
  4239. <li>f</li>
  4240. <li>g</li>
  4241. </ul>
  4242. ````````````````````````````````
  4243. ```````````````````````````````` example
  4244. 1. a
  4245. 2. b
  4246. 3. c
  4247. .
  4248. <ol>
  4249. <li>
  4250. <p>a</p>
  4251. </li>
  4252. <li>
  4253. <p>b</p>
  4254. </li>
  4255. <li>
  4256. <p>c</p>
  4257. </li>
  4258. </ol>
  4259. ````````````````````````````````
  4260. Note, however, that list items may not be indented more than
  4261. three spaces. Here `- e` is treated as a paragraph continuation
  4262. line, because it is indented more than three spaces:
  4263. ```````````````````````````````` example
  4264. - a
  4265. - b
  4266. - c
  4267. - d
  4268. - e
  4269. .
  4270. <ul>
  4271. <li>a</li>
  4272. <li>b</li>
  4273. <li>c</li>
  4274. <li>d
  4275. - e</li>
  4276. </ul>
  4277. ````````````````````````````````
  4278. And here, `3. c` is treated as in indented code block,
  4279. because it is indented four spaces and preceded by a
  4280. blank line.
  4281. ```````````````````````````````` example
  4282. 1. a
  4283. 2. b
  4284. 3. c
  4285. .
  4286. <ol>
  4287. <li>
  4288. <p>a</p>
  4289. </li>
  4290. <li>
  4291. <p>b</p>
  4292. </li>
  4293. </ol>
  4294. <pre><code>3. c
  4295. </code></pre>
  4296. ````````````````````````````````
  4297. This is a loose list, because there is a blank line between
  4298. two of the list items:
  4299. ```````````````````````````````` example
  4300. - a
  4301. - b
  4302. - c
  4303. .
  4304. <ul>
  4305. <li>
  4306. <p>a</p>
  4307. </li>
  4308. <li>
  4309. <p>b</p>
  4310. </li>
  4311. <li>
  4312. <p>c</p>
  4313. </li>
  4314. </ul>
  4315. ````````````````````````````````
  4316. So is this, with a empty second item:
  4317. ```````````````````````````````` example
  4318. * a
  4319. *
  4320. * c
  4321. .
  4322. <ul>
  4323. <li>
  4324. <p>a</p>
  4325. </li>
  4326. <li></li>
  4327. <li>
  4328. <p>c</p>
  4329. </li>
  4330. </ul>
  4331. ````````````````````````````````
  4332. These are loose lists, even though there is no space between the items,
  4333. because one of the items directly contains two block-level elements
  4334. with a blank line between them:
  4335. ```````````````````````````````` example
  4336. - a
  4337. - b
  4338. c
  4339. - d
  4340. .
  4341. <ul>
  4342. <li>
  4343. <p>a</p>
  4344. </li>
  4345. <li>
  4346. <p>b</p>
  4347. <p>c</p>
  4348. </li>
  4349. <li>
  4350. <p>d</p>
  4351. </li>
  4352. </ul>
  4353. ````````````````````````````````
  4354. ```````````````````````````````` example
  4355. - a
  4356. - b
  4357. [ref]: /url
  4358. - d
  4359. .
  4360. <ul>
  4361. <li>
  4362. <p>a</p>
  4363. </li>
  4364. <li>
  4365. <p>b</p>
  4366. </li>
  4367. <li>
  4368. <p>d</p>
  4369. </li>
  4370. </ul>
  4371. ````````````````````````````````
  4372. This is a tight list, because the blank lines are in a code block:
  4373. ```````````````````````````````` example
  4374. - a
  4375. - ```
  4376. b
  4377. ```
  4378. - c
  4379. .
  4380. <ul>
  4381. <li>a</li>
  4382. <li>
  4383. <pre><code>b
  4384. </code></pre>
  4385. </li>
  4386. <li>c</li>
  4387. </ul>
  4388. ````````````````````````````````
  4389. This is a tight list, because the blank line is between two
  4390. paragraphs of a sublist. So the sublist is loose while
  4391. the outer list is tight:
  4392. ```````````````````````````````` example
  4393. - a
  4394. - b
  4395. c
  4396. - d
  4397. .
  4398. <ul>
  4399. <li>a
  4400. <ul>
  4401. <li>
  4402. <p>b</p>
  4403. <p>c</p>
  4404. </li>
  4405. </ul>
  4406. </li>
  4407. <li>d</li>
  4408. </ul>
  4409. ````````````````````````````````
  4410. This is a tight list, because the blank line is inside the
  4411. block quote:
  4412. ```````````````````````````````` example
  4413. * a
  4414. > b
  4415. >
  4416. * c
  4417. .
  4418. <ul>
  4419. <li>a
  4420. <blockquote>
  4421. <p>b</p>
  4422. </blockquote>
  4423. </li>
  4424. <li>c</li>
  4425. </ul>
  4426. ````````````````````````````````
  4427. This list is tight, because the consecutive block elements
  4428. are not separated by blank lines:
  4429. ```````````````````````````````` example
  4430. - a
  4431. > b
  4432. ```
  4433. c
  4434. ```
  4435. - d
  4436. .
  4437. <ul>
  4438. <li>a
  4439. <blockquote>
  4440. <p>b</p>
  4441. </blockquote>
  4442. <pre><code>c
  4443. </code></pre>
  4444. </li>
  4445. <li>d</li>
  4446. </ul>
  4447. ````````````````````````````````
  4448. A single-paragraph list is tight:
  4449. ```````````````````````````````` example
  4450. - a
  4451. .
  4452. <ul>
  4453. <li>a</li>
  4454. </ul>
  4455. ````````````````````````````````
  4456. ```````````````````````````````` example
  4457. - a
  4458. - b
  4459. .
  4460. <ul>
  4461. <li>a
  4462. <ul>
  4463. <li>b</li>
  4464. </ul>
  4465. </li>
  4466. </ul>
  4467. ````````````````````````````````
  4468. This list is loose, because of the blank line between the
  4469. two block elements in the list item:
  4470. ```````````````````````````````` example
  4471. 1. ```
  4472. foo
  4473. ```
  4474. bar
  4475. .
  4476. <ol>
  4477. <li>
  4478. <pre><code>foo
  4479. </code></pre>
  4480. <p>bar</p>
  4481. </li>
  4482. </ol>
  4483. ````````````````````````````````
  4484. Here the outer list is loose, the inner list tight:
  4485. ```````````````````````````````` example
  4486. * foo
  4487. * bar
  4488. baz
  4489. .
  4490. <ul>
  4491. <li>
  4492. <p>foo</p>
  4493. <ul>
  4494. <li>bar</li>
  4495. </ul>
  4496. <p>baz</p>
  4497. </li>
  4498. </ul>
  4499. ````````````````````````````````
  4500. ```````````````````````````````` example
  4501. - a
  4502. - b
  4503. - c
  4504. - d
  4505. - e
  4506. - f
  4507. .
  4508. <ul>
  4509. <li>
  4510. <p>a</p>
  4511. <ul>
  4512. <li>b</li>
  4513. <li>c</li>
  4514. </ul>
  4515. </li>
  4516. <li>
  4517. <p>d</p>
  4518. <ul>
  4519. <li>e</li>
  4520. <li>f</li>
  4521. </ul>
  4522. </li>
  4523. </ul>
  4524. ````````````````````````````````
  4525. # Inlines
  4526. Inlines are parsed sequentially from the beginning of the character
  4527. stream to the end (left to right, in left-to-right languages).
  4528. Thus, for example, in
  4529. ```````````````````````````````` example
  4530. `hi`lo`
  4531. .
  4532. <p><code>hi</code>lo`</p>
  4533. ````````````````````````````````
  4534. `hi` is parsed as code, leaving the backtick at the end as a literal
  4535. backtick.
  4536. ## Code spans
  4537. A [backtick string](@)
  4538. is a string of one or more backtick characters (`` ` ``) that is neither
  4539. preceded nor followed by a backtick.
  4540. A [code span](@) begins with a backtick string and ends with
  4541. a backtick string of equal length. The contents of the code span are
  4542. the characters between these two backtick strings, normalized in the
  4543. following ways:
  4544. - First, [line endings] are converted to [spaces].
  4545. - If the resulting string both begins *and* ends with a [space]
  4546. character, but does not consist entirely of [space]
  4547. characters, a single [space] character is removed from the
  4548. front and back. This allows you to include code that begins
  4549. or ends with backtick characters, which must be separated by
  4550. whitespace from the opening or closing backtick strings.
  4551. This is a simple code span:
  4552. ```````````````````````````````` example
  4553. `foo`
  4554. .
  4555. <p><code>foo</code></p>
  4556. ````````````````````````````````
  4557. Here two backticks are used, because the code contains a backtick.
  4558. This example also illustrates stripping of a single leading and
  4559. trailing space:
  4560. ```````````````````````````````` example
  4561. `` foo ` bar ``
  4562. .
  4563. <p><code>foo ` bar</code></p>
  4564. ````````````````````````````````
  4565. This example shows the motivation for stripping leading and trailing
  4566. spaces:
  4567. ```````````````````````````````` example
  4568. ` `` `
  4569. .
  4570. <p><code>``</code></p>
  4571. ````````````````````````````````
  4572. Note that only *one* space is stripped:
  4573. ```````````````````````````````` example
  4574. ` `` `
  4575. .
  4576. <p><code> `` </code></p>
  4577. ````````````````````````````````
  4578. The stripping only happens if the space is on both
  4579. sides of the string:
  4580. ```````````````````````````````` example
  4581. ` a`
  4582. .
  4583. <p><code> a</code></p>
  4584. ````````````````````````````````
  4585. Only [spaces], and not [unicode whitespace] in general, are
  4586. stripped in this way:
  4587. ```````````````````````````````` example
  4588. ` b `
  4589. .
  4590. <p><code> b </code></p>
  4591. ````````````````````````````````
  4592. No stripping occurs if the code span contains only spaces:
  4593. ```````````````````````````````` example
  4594. ` `
  4595. ` `
  4596. .
  4597. <p><code> </code>
  4598. <code> </code></p>
  4599. ````````````````````````````````
  4600. [Line endings] are treated like spaces:
  4601. ```````````````````````````````` example
  4602. ``
  4603. foo
  4604. bar
  4605. baz
  4606. ``
  4607. .
  4608. <p><code>foo bar baz</code></p>
  4609. ````````````````````````````````
  4610. ```````````````````````````````` example
  4611. ``
  4612. foo
  4613. ``
  4614. .
  4615. <p><code>foo </code></p>
  4616. ````````````````````````````````
  4617. Interior spaces are not collapsed:
  4618. ```````````````````````````````` example
  4619. `foo bar
  4620. baz`
  4621. .
  4622. <p><code>foo bar baz</code></p>
  4623. ````````````````````````````````
  4624. Note that browsers will typically collapse consecutive spaces
  4625. when rendering `<code>` elements, so it is recommended that
  4626. the following CSS be used:
  4627. code{white-space: pre-wrap;}
  4628. Note that backslash escapes do not work in code spans. All backslashes
  4629. are treated literally:
  4630. ```````````````````````````````` example
  4631. `foo\`bar`
  4632. .
  4633. <p><code>foo\</code>bar`</p>
  4634. ````````````````````````````````
  4635. Backslash escapes are never needed, because one can always choose a
  4636. string of *n* backtick characters as delimiters, where the code does
  4637. not contain any strings of exactly *n* backtick characters.
  4638. ```````````````````````````````` example
  4639. ``foo`bar``
  4640. .
  4641. <p><code>foo`bar</code></p>
  4642. ````````````````````````````````
  4643. ```````````````````````````````` example
  4644. ` foo `` bar `
  4645. .
  4646. <p><code>foo `` bar</code></p>
  4647. ````````````````````````````````
  4648. Code span backticks have higher precedence than any other inline
  4649. constructs except HTML tags and autolinks. Thus, for example, this is
  4650. not parsed as emphasized text, since the second `*` is part of a code
  4651. span:
  4652. ```````````````````````````````` example
  4653. *foo`*`
  4654. .
  4655. <p>*foo<code>*</code></p>
  4656. ````````````````````````````````
  4657. And this is not parsed as a link:
  4658. ```````````````````````````````` example
  4659. [not a `link](/foo`)
  4660. .
  4661. <p>[not a <code>link](/foo</code>)</p>
  4662. ````````````````````````````````
  4663. Code spans, HTML tags, and autolinks have the same precedence.
  4664. Thus, this is code:
  4665. ```````````````````````````````` example
  4666. `<a href="`">`
  4667. .
  4668. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4669. ````````````````````````````````
  4670. But this is an HTML tag:
  4671. ```````````````````````````````` example
  4672. <a href="`">`
  4673. .
  4674. <p><a href="`">`</p>
  4675. ````````````````````````````````
  4676. And this is code:
  4677. ```````````````````````````````` example
  4678. `<http://foo.bar.`baz>`
  4679. .
  4680. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4681. ````````````````````````````````
  4682. But this is an autolink:
  4683. ```````````````````````````````` example
  4684. <http://foo.bar.`baz>`
  4685. .
  4686. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4687. ````````````````````````````````
  4688. When a backtick string is not closed by a matching backtick string,
  4689. we just have literal backticks:
  4690. ```````````````````````````````` example
  4691. ```foo``
  4692. .
  4693. <p>```foo``</p>
  4694. ````````````````````````````````
  4695. ```````````````````````````````` example
  4696. `foo
  4697. .
  4698. <p>`foo</p>
  4699. ````````````````````````````````
  4700. The following case also illustrates the need for opening and
  4701. closing backtick strings to be equal in length:
  4702. ```````````````````````````````` example
  4703. `foo``bar``
  4704. .
  4705. <p>`foo<code>bar</code></p>
  4706. ````````````````````````````````
  4707. ## Emphasis and strong emphasis
  4708. John Gruber's original [Markdown syntax
  4709. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4710. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4711. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4712. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4713. > tag.
  4714. This is enough for most users, but these rules leave much undecided,
  4715. especially when it comes to nested emphasis. The original
  4716. `Markdown.pl` test suite makes it clear that triple `***` and
  4717. `___` delimiters can be used for strong emphasis, and most
  4718. implementations have also allowed the following patterns:
  4719. ``` markdown
  4720. ***strong emph***
  4721. ***strong** in emph*
  4722. ***emph* in strong**
  4723. **in strong *emph***
  4724. *in emph **strong***
  4725. ```
  4726. The following patterns are less widely supported, but the intent
  4727. is clear and they are useful (especially in contexts like bibliography
  4728. entries):
  4729. ``` markdown
  4730. *emph *with emph* in it*
  4731. **strong **with strong** in it**
  4732. ```
  4733. Many implementations have also restricted intraword emphasis to
  4734. the `*` forms, to avoid unwanted emphasis in words containing
  4735. internal underscores. (It is best practice to put these in code
  4736. spans, but users often do not.)
  4737. ``` markdown
  4738. internal emphasis: foo*bar*baz
  4739. no emphasis: foo_bar_baz
  4740. ```
  4741. The rules given below capture all of these patterns, while allowing
  4742. for efficient parsing strategies that do not backtrack.
  4743. First, some definitions. A [delimiter run](@) is either
  4744. a sequence of one or more `*` characters that is not preceded or
  4745. followed by a non-backslash-escaped `*` character, or a sequence
  4746. of one or more `_` characters that is not preceded or followed by
  4747. a non-backslash-escaped `_` character.
  4748. A [left-flanking delimiter run](@) is
  4749. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4750. and either (2a) not followed by a [punctuation character], or
  4751. (2b) followed by a [punctuation character] and
  4752. preceded by [Unicode whitespace] or a [punctuation character].
  4753. For purposes of this definition, the beginning and the end of
  4754. the line count as Unicode whitespace.
  4755. A [right-flanking delimiter run](@) is
  4756. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4757. and either (2a) not preceded by a [punctuation character], or
  4758. (2b) preceded by a [punctuation character] and
  4759. followed by [Unicode whitespace] or a [punctuation character].
  4760. For purposes of this definition, the beginning and the end of
  4761. the line count as Unicode whitespace.
  4762. Here are some examples of delimiter runs.
  4763. - left-flanking but not right-flanking:
  4764. ```
  4765. ***abc
  4766. _abc
  4767. **"abc"
  4768. _"abc"
  4769. ```
  4770. - right-flanking but not left-flanking:
  4771. ```
  4772. abc***
  4773. abc_
  4774. "abc"**
  4775. "abc"_
  4776. ```
  4777. - Both left and right-flanking:
  4778. ```
  4779. abc***def
  4780. "abc"_"def"
  4781. ```
  4782. - Neither left nor right-flanking:
  4783. ```
  4784. abc *** def
  4785. a _ b
  4786. ```
  4787. (The idea of distinguishing left-flanking and right-flanking
  4788. delimiter runs based on the character before and the character
  4789. after comes from Roopesh Chander's
  4790. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4791. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4792. run," and its rules for distinguishing left- and right-flanking runs
  4793. are a bit more complex than the ones given here.)
  4794. The following rules define emphasis and strong emphasis:
  4795. 1. A single `*` character [can open emphasis](@)
  4796. iff (if and only if) it is part of a [left-flanking delimiter run].
  4797. 2. A single `_` character [can open emphasis] iff
  4798. it is part of a [left-flanking delimiter run]
  4799. and either (a) not part of a [right-flanking delimiter run]
  4800. or (b) part of a [right-flanking delimiter run]
  4801. preceded by punctuation.
  4802. 3. A single `*` character [can close emphasis](@)
  4803. iff it is part of a [right-flanking delimiter run].
  4804. 4. A single `_` character [can close emphasis] iff
  4805. it is part of a [right-flanking delimiter run]
  4806. and either (a) not part of a [left-flanking delimiter run]
  4807. or (b) part of a [left-flanking delimiter run]
  4808. followed by punctuation.
  4809. 5. A double `**` [can open strong emphasis](@)
  4810. iff it is part of a [left-flanking delimiter run].
  4811. 6. A double `__` [can open strong emphasis] iff
  4812. it is part of a [left-flanking delimiter run]
  4813. and either (a) not part of a [right-flanking delimiter run]
  4814. or (b) part of a [right-flanking delimiter run]
  4815. preceded by punctuation.
  4816. 7. A double `**` [can close strong emphasis](@)
  4817. iff it is part of a [right-flanking delimiter run].
  4818. 8. A double `__` [can close strong emphasis] iff
  4819. it is part of a [right-flanking delimiter run]
  4820. and either (a) not part of a [left-flanking delimiter run]
  4821. or (b) part of a [left-flanking delimiter run]
  4822. followed by punctuation.
  4823. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4824. with a delimiter that [can close emphasis], and that uses the same
  4825. character (`_` or `*`) as the opening delimiter. The
  4826. opening and closing delimiters must belong to separate
  4827. [delimiter runs]. If one of the delimiters can both
  4828. open and close emphasis, then the sum of the lengths of the
  4829. delimiter runs containing the opening and closing delimiters
  4830. must not be a multiple of 3 unless both lengths are
  4831. multiples of 3.
  4832. 10. Strong emphasis begins with a delimiter that
  4833. [can open strong emphasis] and ends with a delimiter that
  4834. [can close strong emphasis], and that uses the same character
  4835. (`_` or `*`) as the opening delimiter. The
  4836. opening and closing delimiters must belong to separate
  4837. [delimiter runs]. If one of the delimiters can both open
  4838. and close strong emphasis, then the sum of the lengths of
  4839. the delimiter runs containing the opening and closing
  4840. delimiters must not be a multiple of 3 unless both lengths
  4841. are multiples of 3.
  4842. 11. A literal `*` character cannot occur at the beginning or end of
  4843. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4844. is backslash-escaped.
  4845. 12. A literal `_` character cannot occur at the beginning or end of
  4846. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4847. is backslash-escaped.
  4848. Where rules 1--12 above are compatible with multiple parsings,
  4849. the following principles resolve ambiguity:
  4850. 13. The number of nestings should be minimized. Thus, for example,
  4851. an interpretation `<strong>...</strong>` is always preferred to
  4852. `<em><em>...</em></em>`.
  4853. 14. An interpretation `<em><strong>...</strong></em>` is always
  4854. preferred to `<strong><em>...</em></strong>`.
  4855. 15. When two potential emphasis or strong emphasis spans overlap,
  4856. so that the second begins before the first ends and ends after
  4857. the first ends, the first takes precedence. Thus, for example,
  4858. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4859. than `*foo <em>bar* baz</em>`.
  4860. 16. When there are two potential emphasis or strong emphasis spans
  4861. with the same closing delimiter, the shorter one (the one that
  4862. opens later) takes precedence. Thus, for example,
  4863. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4864. rather than `<strong>foo **bar baz</strong>`.
  4865. 17. Inline code spans, links, images, and HTML tags group more tightly
  4866. than emphasis. So, when there is a choice between an interpretation
  4867. that contains one of these elements and one that does not, the
  4868. former always wins. Thus, for example, `*[foo*](bar)` is
  4869. parsed as `*<a href="bar">foo*</a>` rather than as
  4870. `<em>[foo</em>](bar)`.
  4871. These rules can be illustrated through a series of examples.
  4872. Rule 1:
  4873. ```````````````````````````````` example
  4874. *foo bar*
  4875. .
  4876. <p><em>foo bar</em></p>
  4877. ````````````````````````````````
  4878. This is not emphasis, because the opening `*` is followed by
  4879. whitespace, and hence not part of a [left-flanking delimiter run]:
  4880. ```````````````````````````````` example
  4881. a * foo bar*
  4882. .
  4883. <p>a * foo bar*</p>
  4884. ````````````````````````````````
  4885. This is not emphasis, because the opening `*` is preceded
  4886. by an alphanumeric and followed by punctuation, and hence
  4887. not part of a [left-flanking delimiter run]:
  4888. ```````````````````````````````` example
  4889. a*"foo"*
  4890. .
  4891. <p>a*&quot;foo&quot;*</p>
  4892. ````````````````````````````````
  4893. Unicode nonbreaking spaces count as whitespace, too:
  4894. ```````````````````````````````` example
  4895. * a *
  4896. .
  4897. <p>* a *</p>
  4898. ````````````````````````````````
  4899. Intraword emphasis with `*` is permitted:
  4900. ```````````````````````````````` example
  4901. foo*bar*
  4902. .
  4903. <p>foo<em>bar</em></p>
  4904. ````````````````````````````````
  4905. ```````````````````````````````` example
  4906. 5*6*78
  4907. .
  4908. <p>5<em>6</em>78</p>
  4909. ````````````````````````````````
  4910. Rule 2:
  4911. ```````````````````````````````` example
  4912. _foo bar_
  4913. .
  4914. <p><em>foo bar</em></p>
  4915. ````````````````````````````````
  4916. This is not emphasis, because the opening `_` is followed by
  4917. whitespace:
  4918. ```````````````````````````````` example
  4919. _ foo bar_
  4920. .
  4921. <p>_ foo bar_</p>
  4922. ````````````````````````````````
  4923. This is not emphasis, because the opening `_` is preceded
  4924. by an alphanumeric and followed by punctuation:
  4925. ```````````````````````````````` example
  4926. a_"foo"_
  4927. .
  4928. <p>a_&quot;foo&quot;_</p>
  4929. ````````````````````````````````
  4930. Emphasis with `_` is not allowed inside words:
  4931. ```````````````````````````````` example
  4932. foo_bar_
  4933. .
  4934. <p>foo_bar_</p>
  4935. ````````````````````````````````
  4936. ```````````````````````````````` example
  4937. 5_6_78
  4938. .
  4939. <p>5_6_78</p>
  4940. ````````````````````````````````
  4941. ```````````````````````````````` example
  4942. пристаням_стремятся_
  4943. .
  4944. <p>пристаням_стремятся_</p>
  4945. ````````````````````````````````
  4946. Here `_` does not generate emphasis, because the first delimiter run
  4947. is right-flanking and the second left-flanking:
  4948. ```````````````````````````````` example
  4949. aa_"bb"_cc
  4950. .
  4951. <p>aa_&quot;bb&quot;_cc</p>
  4952. ````````````````````````````````
  4953. This is emphasis, even though the opening delimiter is
  4954. both left- and right-flanking, because it is preceded by
  4955. punctuation:
  4956. ```````````````````````````````` example
  4957. foo-_(bar)_
  4958. .
  4959. <p>foo-<em>(bar)</em></p>
  4960. ````````````````````````````````
  4961. Rule 3:
  4962. This is not emphasis, because the closing delimiter does
  4963. not match the opening delimiter:
  4964. ```````````````````````````````` example
  4965. _foo*
  4966. .
  4967. <p>_foo*</p>
  4968. ````````````````````````````````
  4969. This is not emphasis, because the closing `*` is preceded by
  4970. whitespace:
  4971. ```````````````````````````````` example
  4972. *foo bar *
  4973. .
  4974. <p>*foo bar *</p>
  4975. ````````````````````````````````
  4976. A newline also counts as whitespace:
  4977. ```````````````````````````````` example
  4978. *foo bar
  4979. *
  4980. .
  4981. <p>*foo bar
  4982. *</p>
  4983. ````````````````````````````````
  4984. This is not emphasis, because the second `*` is
  4985. preceded by punctuation and followed by an alphanumeric
  4986. (hence it is not part of a [right-flanking delimiter run]:
  4987. ```````````````````````````````` example
  4988. *(*foo)
  4989. .
  4990. <p>*(*foo)</p>
  4991. ````````````````````````````````
  4992. The point of this restriction is more easily appreciated
  4993. with this example:
  4994. ```````````````````````````````` example
  4995. *(*foo*)*
  4996. .
  4997. <p><em>(<em>foo</em>)</em></p>
  4998. ````````````````````````````````
  4999. Intraword emphasis with `*` is allowed:
  5000. ```````````````````````````````` example
  5001. *foo*bar
  5002. .
  5003. <p><em>foo</em>bar</p>
  5004. ````````````````````````````````
  5005. Rule 4:
  5006. This is not emphasis, because the closing `_` is preceded by
  5007. whitespace:
  5008. ```````````````````````````````` example
  5009. _foo bar _
  5010. .
  5011. <p>_foo bar _</p>
  5012. ````````````````````````````````
  5013. This is not emphasis, because the second `_` is
  5014. preceded by punctuation and followed by an alphanumeric:
  5015. ```````````````````````````````` example
  5016. _(_foo)
  5017. .
  5018. <p>_(_foo)</p>
  5019. ````````````````````````````````
  5020. This is emphasis within emphasis:
  5021. ```````````````````````````````` example
  5022. _(_foo_)_
  5023. .
  5024. <p><em>(<em>foo</em>)</em></p>
  5025. ````````````````````````````````
  5026. Intraword emphasis is disallowed for `_`:
  5027. ```````````````````````````````` example
  5028. _foo_bar
  5029. .
  5030. <p>_foo_bar</p>
  5031. ````````````````````````````````
  5032. ```````````````````````````````` example
  5033. _пристаням_стремятся
  5034. .
  5035. <p>_пристаням_стремятся</p>
  5036. ````````````````````````````````
  5037. ```````````````````````````````` example
  5038. _foo_bar_baz_
  5039. .
  5040. <p><em>foo_bar_baz</em></p>
  5041. ````````````````````````````````
  5042. This is emphasis, even though the closing delimiter is
  5043. both left- and right-flanking, because it is followed by
  5044. punctuation:
  5045. ```````````````````````````````` example
  5046. _(bar)_.
  5047. .
  5048. <p><em>(bar)</em>.</p>
  5049. ````````````````````````````````
  5050. Rule 5:
  5051. ```````````````````````````````` example
  5052. **foo bar**
  5053. .
  5054. <p><strong>foo bar</strong></p>
  5055. ````````````````````````````````
  5056. This is not strong emphasis, because the opening delimiter is
  5057. followed by whitespace:
  5058. ```````````````````````````````` example
  5059. ** foo bar**
  5060. .
  5061. <p>** foo bar**</p>
  5062. ````````````````````````````````
  5063. This is not strong emphasis, because the opening `**` is preceded
  5064. by an alphanumeric and followed by punctuation, and hence
  5065. not part of a [left-flanking delimiter run]:
  5066. ```````````````````````````````` example
  5067. a**"foo"**
  5068. .
  5069. <p>a**&quot;foo&quot;**</p>
  5070. ````````````````````````````````
  5071. Intraword strong emphasis with `**` is permitted:
  5072. ```````````````````````````````` example
  5073. foo**bar**
  5074. .
  5075. <p>foo<strong>bar</strong></p>
  5076. ````````````````````````````````
  5077. Rule 6:
  5078. ```````````````````````````````` example
  5079. __foo bar__
  5080. .
  5081. <p><strong>foo bar</strong></p>
  5082. ````````````````````````````````
  5083. This is not strong emphasis, because the opening delimiter is
  5084. followed by whitespace:
  5085. ```````````````````````````````` example
  5086. __ foo bar__
  5087. .
  5088. <p>__ foo bar__</p>
  5089. ````````````````````````````````
  5090. A newline counts as whitespace:
  5091. ```````````````````````````````` example
  5092. __
  5093. foo bar__
  5094. .
  5095. <p>__
  5096. foo bar__</p>
  5097. ````````````````````````````````
  5098. This is not strong emphasis, because the opening `__` is preceded
  5099. by an alphanumeric and followed by punctuation:
  5100. ```````````````````````````````` example
  5101. a__"foo"__
  5102. .
  5103. <p>a__&quot;foo&quot;__</p>
  5104. ````````````````````````````````
  5105. Intraword strong emphasis is forbidden with `__`:
  5106. ```````````````````````````````` example
  5107. foo__bar__
  5108. .
  5109. <p>foo__bar__</p>
  5110. ````````````````````````````````
  5111. ```````````````````````````````` example
  5112. 5__6__78
  5113. .
  5114. <p>5__6__78</p>
  5115. ````````````````````````````````
  5116. ```````````````````````````````` example
  5117. пристаням__стремятся__
  5118. .
  5119. <p>пристаням__стремятся__</p>
  5120. ````````````````````````````````
  5121. ```````````````````````````````` example
  5122. __foo, __bar__, baz__
  5123. .
  5124. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5125. ````````````````````````````````
  5126. This is strong emphasis, even though the opening delimiter is
  5127. both left- and right-flanking, because it is preceded by
  5128. punctuation:
  5129. ```````````````````````````````` example
  5130. foo-__(bar)__
  5131. .
  5132. <p>foo-<strong>(bar)</strong></p>
  5133. ````````````````````````````````
  5134. Rule 7:
  5135. This is not strong emphasis, because the closing delimiter is preceded
  5136. by whitespace:
  5137. ```````````````````````````````` example
  5138. **foo bar **
  5139. .
  5140. <p>**foo bar **</p>
  5141. ````````````````````````````````
  5142. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5143. Rule 11.)
  5144. This is not strong emphasis, because the second `**` is
  5145. preceded by punctuation and followed by an alphanumeric:
  5146. ```````````````````````````````` example
  5147. **(**foo)
  5148. .
  5149. <p>**(**foo)</p>
  5150. ````````````````````````````````
  5151. The point of this restriction is more easily appreciated
  5152. with these examples:
  5153. ```````````````````````````````` example
  5154. *(**foo**)*
  5155. .
  5156. <p><em>(<strong>foo</strong>)</em></p>
  5157. ````````````````````````````````
  5158. ```````````````````````````````` example
  5159. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5160. *Asclepias physocarpa*)**
  5161. .
  5162. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5163. <em>Asclepias physocarpa</em>)</strong></p>
  5164. ````````````````````````````````
  5165. ```````````````````````````````` example
  5166. **foo "*bar*" foo**
  5167. .
  5168. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5169. ````````````````````````````````
  5170. Intraword emphasis:
  5171. ```````````````````````````````` example
  5172. **foo**bar
  5173. .
  5174. <p><strong>foo</strong>bar</p>
  5175. ````````````````````````````````
  5176. Rule 8:
  5177. This is not strong emphasis, because the closing delimiter is
  5178. preceded by whitespace:
  5179. ```````````````````````````````` example
  5180. __foo bar __
  5181. .
  5182. <p>__foo bar __</p>
  5183. ````````````````````````````````
  5184. This is not strong emphasis, because the second `__` is
  5185. preceded by punctuation and followed by an alphanumeric:
  5186. ```````````````````````````````` example
  5187. __(__foo)
  5188. .
  5189. <p>__(__foo)</p>
  5190. ````````````````````````````````
  5191. The point of this restriction is more easily appreciated
  5192. with this example:
  5193. ```````````````````````````````` example
  5194. _(__foo__)_
  5195. .
  5196. <p><em>(<strong>foo</strong>)</em></p>
  5197. ````````````````````````````````
  5198. Intraword strong emphasis is forbidden with `__`:
  5199. ```````````````````````````````` example
  5200. __foo__bar
  5201. .
  5202. <p>__foo__bar</p>
  5203. ````````````````````````````````
  5204. ```````````````````````````````` example
  5205. __пристаням__стремятся
  5206. .
  5207. <p>__пристаням__стремятся</p>
  5208. ````````````````````````````````
  5209. ```````````````````````````````` example
  5210. __foo__bar__baz__
  5211. .
  5212. <p><strong>foo__bar__baz</strong></p>
  5213. ````````````````````````````````
  5214. This is strong emphasis, even though the closing delimiter is
  5215. both left- and right-flanking, because it is followed by
  5216. punctuation:
  5217. ```````````````````````````````` example
  5218. __(bar)__.
  5219. .
  5220. <p><strong>(bar)</strong>.</p>
  5221. ````````````````````````````````
  5222. Rule 9:
  5223. Any nonempty sequence of inline elements can be the contents of an
  5224. emphasized span.
  5225. ```````````````````````````````` example
  5226. *foo [bar](/url)*
  5227. .
  5228. <p><em>foo <a href="/url">bar</a></em></p>
  5229. ````````````````````````````````
  5230. ```````````````````````````````` example
  5231. *foo
  5232. bar*
  5233. .
  5234. <p><em>foo
  5235. bar</em></p>
  5236. ````````````````````````````````
  5237. In particular, emphasis and strong emphasis can be nested
  5238. inside emphasis:
  5239. ```````````````````````````````` example
  5240. _foo __bar__ baz_
  5241. .
  5242. <p><em>foo <strong>bar</strong> baz</em></p>
  5243. ````````````````````````````````
  5244. ```````````````````````````````` example
  5245. _foo _bar_ baz_
  5246. .
  5247. <p><em>foo <em>bar</em> baz</em></p>
  5248. ````````````````````````````````
  5249. ```````````````````````````````` example
  5250. __foo_ bar_
  5251. .
  5252. <p><em><em>foo</em> bar</em></p>
  5253. ````````````````````````````````
  5254. ```````````````````````````````` example
  5255. *foo *bar**
  5256. .
  5257. <p><em>foo <em>bar</em></em></p>
  5258. ````````````````````````````````
  5259. ```````````````````````````````` example
  5260. *foo **bar** baz*
  5261. .
  5262. <p><em>foo <strong>bar</strong> baz</em></p>
  5263. ````````````````````````````````
  5264. ```````````````````````````````` example
  5265. *foo**bar**baz*
  5266. .
  5267. <p><em>foo<strong>bar</strong>baz</em></p>
  5268. ````````````````````````````````
  5269. Note that in the preceding case, the interpretation
  5270. ``` markdown
  5271. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5272. ```
  5273. is precluded by the condition that a delimiter that
  5274. can both open and close (like the `*` after `foo`)
  5275. cannot form emphasis if the sum of the lengths of
  5276. the delimiter runs containing the opening and
  5277. closing delimiters is a multiple of 3 unless
  5278. both lengths are multiples of 3.
  5279. For the same reason, we don't get two consecutive
  5280. emphasis sections in this example:
  5281. ```````````````````````````````` example
  5282. *foo**bar*
  5283. .
  5284. <p><em>foo**bar</em></p>
  5285. ````````````````````````````````
  5286. The same condition ensures that the following
  5287. cases are all strong emphasis nested inside
  5288. emphasis, even when the interior spaces are
  5289. omitted:
  5290. ```````````````````````````````` example
  5291. ***foo** bar*
  5292. .
  5293. <p><em><strong>foo</strong> bar</em></p>
  5294. ````````````````````````````````
  5295. ```````````````````````````````` example
  5296. *foo **bar***
  5297. .
  5298. <p><em>foo <strong>bar</strong></em></p>
  5299. ````````````````````````````````
  5300. ```````````````````````````````` example
  5301. *foo**bar***
  5302. .
  5303. <p><em>foo<strong>bar</strong></em></p>
  5304. ````````````````````````````````
  5305. When the lengths of the interior closing and opening
  5306. delimiter runs are *both* multiples of 3, though,
  5307. they can match to create emphasis:
  5308. ```````````````````````````````` example
  5309. foo***bar***baz
  5310. .
  5311. <p>foo<em><strong>bar</strong></em>baz</p>
  5312. ````````````````````````````````
  5313. ```````````````````````````````` example
  5314. foo******bar*********baz
  5315. .
  5316. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5317. ````````````````````````````````
  5318. Indefinite levels of nesting are possible:
  5319. ```````````````````````````````` example
  5320. *foo **bar *baz* bim** bop*
  5321. .
  5322. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5323. ````````````````````````````````
  5324. ```````````````````````````````` example
  5325. *foo [*bar*](/url)*
  5326. .
  5327. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5328. ````````````````````````````````
  5329. There can be no empty emphasis or strong emphasis:
  5330. ```````````````````````````````` example
  5331. ** is not an empty emphasis
  5332. .
  5333. <p>** is not an empty emphasis</p>
  5334. ````````````````````````````````
  5335. ```````````````````````````````` example
  5336. **** is not an empty strong emphasis
  5337. .
  5338. <p>**** is not an empty strong emphasis</p>
  5339. ````````````````````````````````
  5340. Rule 10:
  5341. Any nonempty sequence of inline elements can be the contents of an
  5342. strongly emphasized span.
  5343. ```````````````````````````````` example
  5344. **foo [bar](/url)**
  5345. .
  5346. <p><strong>foo <a href="/url">bar</a></strong></p>
  5347. ````````````````````````````````
  5348. ```````````````````````````````` example
  5349. **foo
  5350. bar**
  5351. .
  5352. <p><strong>foo
  5353. bar</strong></p>
  5354. ````````````````````````````````
  5355. In particular, emphasis and strong emphasis can be nested
  5356. inside strong emphasis:
  5357. ```````````````````````````````` example
  5358. __foo _bar_ baz__
  5359. .
  5360. <p><strong>foo <em>bar</em> baz</strong></p>
  5361. ````````````````````````````````
  5362. ```````````````````````````````` example
  5363. __foo __bar__ baz__
  5364. .
  5365. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5366. ````````````````````````````````
  5367. ```````````````````````````````` example
  5368. ____foo__ bar__
  5369. .
  5370. <p><strong><strong>foo</strong> bar</strong></p>
  5371. ````````````````````````````````
  5372. ```````````````````````````````` example
  5373. **foo **bar****
  5374. .
  5375. <p><strong>foo <strong>bar</strong></strong></p>
  5376. ````````````````````````````````
  5377. ```````````````````````````````` example
  5378. **foo *bar* baz**
  5379. .
  5380. <p><strong>foo <em>bar</em> baz</strong></p>
  5381. ````````````````````````````````
  5382. ```````````````````````````````` example
  5383. **foo*bar*baz**
  5384. .
  5385. <p><strong>foo<em>bar</em>baz</strong></p>
  5386. ````````````````````````````````
  5387. ```````````````````````````````` example
  5388. ***foo* bar**
  5389. .
  5390. <p><strong><em>foo</em> bar</strong></p>
  5391. ````````````````````````````````
  5392. ```````````````````````````````` example
  5393. **foo *bar***
  5394. .
  5395. <p><strong>foo <em>bar</em></strong></p>
  5396. ````````````````````````````````
  5397. Indefinite levels of nesting are possible:
  5398. ```````````````````````````````` example
  5399. **foo *bar **baz**
  5400. bim* bop**
  5401. .
  5402. <p><strong>foo <em>bar <strong>baz</strong>
  5403. bim</em> bop</strong></p>
  5404. ````````````````````````````````
  5405. ```````````````````````````````` example
  5406. **foo [*bar*](/url)**
  5407. .
  5408. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5409. ````````````````````````````````
  5410. There can be no empty emphasis or strong emphasis:
  5411. ```````````````````````````````` example
  5412. __ is not an empty emphasis
  5413. .
  5414. <p>__ is not an empty emphasis</p>
  5415. ````````````````````````````````
  5416. ```````````````````````````````` example
  5417. ____ is not an empty strong emphasis
  5418. .
  5419. <p>____ is not an empty strong emphasis</p>
  5420. ````````````````````````````````
  5421. Rule 11:
  5422. ```````````````````````````````` example
  5423. foo ***
  5424. .
  5425. <p>foo ***</p>
  5426. ````````````````````````````````
  5427. ```````````````````````````````` example
  5428. foo *\**
  5429. .
  5430. <p>foo <em>*</em></p>
  5431. ````````````````````````````````
  5432. ```````````````````````````````` example
  5433. foo *_*
  5434. .
  5435. <p>foo <em>_</em></p>
  5436. ````````````````````````````````
  5437. ```````````````````````````````` example
  5438. foo *****
  5439. .
  5440. <p>foo *****</p>
  5441. ````````````````````````````````
  5442. ```````````````````````````````` example
  5443. foo **\***
  5444. .
  5445. <p>foo <strong>*</strong></p>
  5446. ````````````````````````````````
  5447. ```````````````````````````````` example
  5448. foo **_**
  5449. .
  5450. <p>foo <strong>_</strong></p>
  5451. ````````````````````````````````
  5452. Note that when delimiters do not match evenly, Rule 11 determines
  5453. that the excess literal `*` characters will appear outside of the
  5454. emphasis, rather than inside it:
  5455. ```````````````````````````````` example
  5456. **foo*
  5457. .
  5458. <p>*<em>foo</em></p>
  5459. ````````````````````````````````
  5460. ```````````````````````````````` example
  5461. *foo**
  5462. .
  5463. <p><em>foo</em>*</p>
  5464. ````````````````````````````````
  5465. ```````````````````````````````` example
  5466. ***foo**
  5467. .
  5468. <p>*<strong>foo</strong></p>
  5469. ````````````````````````````````
  5470. ```````````````````````````````` example
  5471. ****foo*
  5472. .
  5473. <p>***<em>foo</em></p>
  5474. ````````````````````````````````
  5475. ```````````````````````````````` example
  5476. **foo***
  5477. .
  5478. <p><strong>foo</strong>*</p>
  5479. ````````````````````````````````
  5480. ```````````````````````````````` example
  5481. *foo****
  5482. .
  5483. <p><em>foo</em>***</p>
  5484. ````````````````````````````````
  5485. Rule 12:
  5486. ```````````````````````````````` example
  5487. foo ___
  5488. .
  5489. <p>foo ___</p>
  5490. ````````````````````````````````
  5491. ```````````````````````````````` example
  5492. foo _\__
  5493. .
  5494. <p>foo <em>_</em></p>
  5495. ````````````````````````````````
  5496. ```````````````````````````````` example
  5497. foo _*_
  5498. .
  5499. <p>foo <em>*</em></p>
  5500. ````````````````````````````````
  5501. ```````````````````````````````` example
  5502. foo _____
  5503. .
  5504. <p>foo _____</p>
  5505. ````````````````````````````````
  5506. ```````````````````````````````` example
  5507. foo __\___
  5508. .
  5509. <p>foo <strong>_</strong></p>
  5510. ````````````````````````````````
  5511. ```````````````````````````````` example
  5512. foo __*__
  5513. .
  5514. <p>foo <strong>*</strong></p>
  5515. ````````````````````````````````
  5516. ```````````````````````````````` example
  5517. __foo_
  5518. .
  5519. <p>_<em>foo</em></p>
  5520. ````````````````````````````````
  5521. Note that when delimiters do not match evenly, Rule 12 determines
  5522. that the excess literal `_` characters will appear outside of the
  5523. emphasis, rather than inside it:
  5524. ```````````````````````````````` example
  5525. _foo__
  5526. .
  5527. <p><em>foo</em>_</p>
  5528. ````````````````````````````````
  5529. ```````````````````````````````` example
  5530. ___foo__
  5531. .
  5532. <p>_<strong>foo</strong></p>
  5533. ````````````````````````````````
  5534. ```````````````````````````````` example
  5535. ____foo_
  5536. .
  5537. <p>___<em>foo</em></p>
  5538. ````````````````````````````````
  5539. ```````````````````````````````` example
  5540. __foo___
  5541. .
  5542. <p><strong>foo</strong>_</p>
  5543. ````````````````````````````````
  5544. ```````````````````````````````` example
  5545. _foo____
  5546. .
  5547. <p><em>foo</em>___</p>
  5548. ````````````````````````````````
  5549. Rule 13 implies that if you want emphasis nested directly inside
  5550. emphasis, you must use different delimiters:
  5551. ```````````````````````````````` example
  5552. **foo**
  5553. .
  5554. <p><strong>foo</strong></p>
  5555. ````````````````````````````````
  5556. ```````````````````````````````` example
  5557. *_foo_*
  5558. .
  5559. <p><em><em>foo</em></em></p>
  5560. ````````````````````````````````
  5561. ```````````````````````````````` example
  5562. __foo__
  5563. .
  5564. <p><strong>foo</strong></p>
  5565. ````````````````````````````````
  5566. ```````````````````````````````` example
  5567. _*foo*_
  5568. .
  5569. <p><em><em>foo</em></em></p>
  5570. ````````````````````````````````
  5571. However, strong emphasis within strong emphasis is possible without
  5572. switching delimiters:
  5573. ```````````````````````````````` example
  5574. ****foo****
  5575. .
  5576. <p><strong><strong>foo</strong></strong></p>
  5577. ````````````````````````````````
  5578. ```````````````````````````````` example
  5579. ____foo____
  5580. .
  5581. <p><strong><strong>foo</strong></strong></p>
  5582. ````````````````````````````````
  5583. Rule 13 can be applied to arbitrarily long sequences of
  5584. delimiters:
  5585. ```````````````````````````````` example
  5586. ******foo******
  5587. .
  5588. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5589. ````````````````````````````````
  5590. Rule 14:
  5591. ```````````````````````````````` example
  5592. ***foo***
  5593. .
  5594. <p><em><strong>foo</strong></em></p>
  5595. ````````````````````````````````
  5596. ```````````````````````````````` example
  5597. _____foo_____
  5598. .
  5599. <p><em><strong><strong>foo</strong></strong></em></p>
  5600. ````````````````````````````````
  5601. Rule 15:
  5602. ```````````````````````````````` example
  5603. *foo _bar* baz_
  5604. .
  5605. <p><em>foo _bar</em> baz_</p>
  5606. ````````````````````````````````
  5607. ```````````````````````````````` example
  5608. *foo __bar *baz bim__ bam*
  5609. .
  5610. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5611. ````````````````````````````````
  5612. Rule 16:
  5613. ```````````````````````````````` example
  5614. **foo **bar baz**
  5615. .
  5616. <p>**foo <strong>bar baz</strong></p>
  5617. ````````````````````````````````
  5618. ```````````````````````````````` example
  5619. *foo *bar baz*
  5620. .
  5621. <p>*foo <em>bar baz</em></p>
  5622. ````````````````````````````````
  5623. Rule 17:
  5624. ```````````````````````````````` example
  5625. *[bar*](/url)
  5626. .
  5627. <p>*<a href="/url">bar*</a></p>
  5628. ````````````````````````````````
  5629. ```````````````````````````````` example
  5630. _foo [bar_](/url)
  5631. .
  5632. <p>_foo <a href="/url">bar_</a></p>
  5633. ````````````````````````````````
  5634. ```````````````````````````````` example
  5635. *<img src="foo" title="*"/>
  5636. .
  5637. <p>*<img src="foo" title="*"/></p>
  5638. ````````````````````````````````
  5639. ```````````````````````````````` example
  5640. **<a href="**">
  5641. .
  5642. <p>**<a href="**"></p>
  5643. ````````````````````````````````
  5644. ```````````````````````````````` example
  5645. __<a href="__">
  5646. .
  5647. <p>__<a href="__"></p>
  5648. ````````````````````````````````
  5649. ```````````````````````````````` example
  5650. *a `*`*
  5651. .
  5652. <p><em>a <code>*</code></em></p>
  5653. ````````````````````````````````
  5654. ```````````````````````````````` example
  5655. _a `_`_
  5656. .
  5657. <p><em>a <code>_</code></em></p>
  5658. ````````````````````````````````
  5659. ```````````````````````````````` example
  5660. **a<http://foo.bar/?q=**>
  5661. .
  5662. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5663. ````````````````````````````````
  5664. ```````````````````````````````` example
  5665. __a<http://foo.bar/?q=__>
  5666. .
  5667. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5668. ````````````````````````````````
  5669. ## Links
  5670. A link contains [link text] (the visible text), a [link destination]
  5671. (the URI that is the link destination), and optionally a [link title].
  5672. There are two basic kinds of links in Markdown. In [inline links] the
  5673. destination and title are given immediately after the link text. In
  5674. [reference links] the destination and title are defined elsewhere in
  5675. the document.
  5676. A [link text](@) consists of a sequence of zero or more
  5677. inline elements enclosed by square brackets (`[` and `]`). The
  5678. following rules apply:
  5679. - Links may not contain other links, at any level of nesting. If
  5680. multiple otherwise valid link definitions appear nested inside each
  5681. other, the inner-most definition is used.
  5682. - Brackets are allowed in the [link text] only if (a) they
  5683. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5684. with an open bracket `[`, a sequence of zero or more inlines, and
  5685. a close bracket `]`.
  5686. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5687. than the brackets in link text. Thus, for example,
  5688. `` [foo`]` `` could not be a link text, since the second `]`
  5689. is part of a code span.
  5690. - The brackets in link text bind more tightly than markers for
  5691. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5692. A [link destination](@) consists of either
  5693. - a sequence of zero or more characters between an opening `<` and a
  5694. closing `>` that contains no line breaks or unescaped
  5695. `<` or `>` characters, or
  5696. - a nonempty sequence of characters that does not start with `<`,
  5697. does not include [ASCII control characters][ASCII control character]
  5698. or [whitespace][], and includes parentheses only if (a) they are
  5699. backslash-escaped or (b) they are part of a balanced pair of
  5700. unescaped parentheses.
  5701. (Implementations may impose limits on parentheses nesting to
  5702. avoid performance issues, but at least three levels of nesting
  5703. should be supported.)
  5704. A [link title](@) consists of either
  5705. - a sequence of zero or more characters between straight double-quote
  5706. characters (`"`), including a `"` character only if it is
  5707. backslash-escaped, or
  5708. - a sequence of zero or more characters between straight single-quote
  5709. characters (`'`), including a `'` character only if it is
  5710. backslash-escaped, or
  5711. - a sequence of zero or more characters between matching parentheses
  5712. (`(...)`), including a `(` or `)` character only if it is
  5713. backslash-escaped.
  5714. Although [link titles] may span multiple lines, they may not contain
  5715. a [blank line].
  5716. An [inline link](@) consists of a [link text] followed immediately
  5717. by a left parenthesis `(`, optional [whitespace], an optional
  5718. [link destination], an optional [link title] separated from the link
  5719. destination by [whitespace], optional [whitespace], and a right
  5720. parenthesis `)`. The link's text consists of the inlines contained
  5721. in the [link text] (excluding the enclosing square brackets).
  5722. The link's URI consists of the link destination, excluding enclosing
  5723. `<...>` if present, with backslash-escapes in effect as described
  5724. above. The link's title consists of the link title, excluding its
  5725. enclosing delimiters, with backslash-escapes in effect as described
  5726. above.
  5727. Here is a simple inline link:
  5728. ```````````````````````````````` example
  5729. [link](/uri "title")
  5730. .
  5731. <p><a href="/uri" title="title">link</a></p>
  5732. ````````````````````````````````
  5733. The title, the link text and even
  5734. the destination may be omitted:
  5735. ```````````````````````````````` example
  5736. [link](/uri)
  5737. .
  5738. <p><a href="/uri">link</a></p>
  5739. ````````````````````````````````
  5740. ```````````````````````````````` example
  5741. [](./target.md)
  5742. .
  5743. <p><a href="./target.md"></a></p>
  5744. ````````````````````````````````
  5745. ```````````````````````````````` example
  5746. [link]()
  5747. .
  5748. <p><a href="">link</a></p>
  5749. ````````````````````````````````
  5750. ```````````````````````````````` example
  5751. [link](<>)
  5752. .
  5753. <p><a href="">link</a></p>
  5754. ````````````````````````````````
  5755. ```````````````````````````````` example
  5756. []()
  5757. .
  5758. <p><a href=""></a></p>
  5759. ````````````````````````````````
  5760. The destination can only contain spaces if it is
  5761. enclosed in pointy brackets:
  5762. ```````````````````````````````` example
  5763. [link](/my uri)
  5764. .
  5765. <p>[link](/my uri)</p>
  5766. ````````````````````````````````
  5767. ```````````````````````````````` example
  5768. [link](</my uri>)
  5769. .
  5770. <p><a href="/my%20uri">link</a></p>
  5771. ````````````````````````````````
  5772. The destination cannot contain line breaks,
  5773. even if enclosed in pointy brackets:
  5774. ```````````````````````````````` example
  5775. [link](foo
  5776. bar)
  5777. .
  5778. <p>[link](foo
  5779. bar)</p>
  5780. ````````````````````````````````
  5781. ```````````````````````````````` example
  5782. [link](<foo
  5783. bar>)
  5784. .
  5785. <p>[link](<foo
  5786. bar>)</p>
  5787. ````````````````````````````````
  5788. The destination can contain `)` if it is enclosed
  5789. in pointy brackets:
  5790. ```````````````````````````````` example
  5791. [a](<b)c>)
  5792. .
  5793. <p><a href="b)c">a</a></p>
  5794. ````````````````````````````````
  5795. Pointy brackets that enclose links must be unescaped:
  5796. ```````````````````````````````` example
  5797. [link](<foo\>)
  5798. .
  5799. <p>[link](&lt;foo&gt;)</p>
  5800. ````````````````````````````````
  5801. These are not links, because the opening pointy bracket
  5802. is not matched properly:
  5803. ```````````````````````````````` example
  5804. [a](<b)c
  5805. [a](<b)c>
  5806. [a](<b>c)
  5807. .
  5808. <p>[a](&lt;b)c
  5809. [a](&lt;b)c&gt;
  5810. [a](<b>c)</p>
  5811. ````````````````````````````````
  5812. Parentheses inside the link destination may be escaped:
  5813. ```````````````````````````````` example
  5814. [link](\(foo\))
  5815. .
  5816. <p><a href="(foo)">link</a></p>
  5817. ````````````````````````````````
  5818. Any number of parentheses are allowed without escaping, as long as they are
  5819. balanced:
  5820. ```````````````````````````````` example
  5821. [link](foo(and(bar)))
  5822. .
  5823. <p><a href="foo(and(bar))">link</a></p>
  5824. ````````````````````````````````
  5825. However, if you have unbalanced parentheses, you need to escape or use the
  5826. `<...>` form:
  5827. ```````````````````````````````` example
  5828. [link](foo(and(bar))
  5829. .
  5830. <p>[link](foo(and(bar))</p>
  5831. ````````````````````````````````
  5832. ```````````````````````````````` example
  5833. [link](foo\(and\(bar\))
  5834. .
  5835. <p><a href="foo(and(bar)">link</a></p>
  5836. ````````````````````````````````
  5837. ```````````````````````````````` example
  5838. [link](<foo(and(bar)>)
  5839. .
  5840. <p><a href="foo(and(bar)">link</a></p>
  5841. ````````````````````````````````
  5842. Parentheses and other symbols can also be escaped, as usual
  5843. in Markdown:
  5844. ```````````````````````````````` example
  5845. [link](foo\)\:)
  5846. .
  5847. <p><a href="foo):">link</a></p>
  5848. ````````````````````````````````
  5849. A link can contain fragment identifiers and queries:
  5850. ```````````````````````````````` example
  5851. [link](#fragment)
  5852. [link](http://example.com#fragment)
  5853. [link](http://example.com?foo=3#frag)
  5854. .
  5855. <p><a href="#fragment">link</a></p>
  5856. <p><a href="http://example.com#fragment">link</a></p>
  5857. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5858. ````````````````````````````````
  5859. Note that a backslash before a non-escapable character is
  5860. just a backslash:
  5861. ```````````````````````````````` example
  5862. [link](foo\bar)
  5863. .
  5864. <p><a href="foo%5Cbar">link</a></p>
  5865. ````````````````````````````````
  5866. URL-escaping should be left alone inside the destination, as all
  5867. URL-escaped characters are also valid URL characters. Entity and
  5868. numerical character references in the destination will be parsed
  5869. into the corresponding Unicode code points, as usual. These may
  5870. be optionally URL-escaped when written as HTML, but this spec
  5871. does not enforce any particular policy for rendering URLs in
  5872. HTML or other formats. Renderers may make different decisions
  5873. about how to escape or normalize URLs in the output.
  5874. ```````````````````````````````` example
  5875. [link](foo%20b&auml;)
  5876. .
  5877. <p><a href="foo%20b%C3%A4">link</a></p>
  5878. ````````````````````````````````
  5879. Note that, because titles can often be parsed as destinations,
  5880. if you try to omit the destination and keep the title, you'll
  5881. get unexpected results:
  5882. ```````````````````````````````` example
  5883. [link]("title")
  5884. .
  5885. <p><a href="%22title%22">link</a></p>
  5886. ````````````````````````````````
  5887. Titles may be in single quotes, double quotes, or parentheses:
  5888. ```````````````````````````````` example
  5889. [link](/url "title")
  5890. [link](/url 'title')
  5891. [link](/url (title))
  5892. .
  5893. <p><a href="/url" title="title">link</a>
  5894. <a href="/url" title="title">link</a>
  5895. <a href="/url" title="title">link</a></p>
  5896. ````````````````````````````````
  5897. Backslash escapes and entity and numeric character references
  5898. may be used in titles:
  5899. ```````````````````````````````` example
  5900. [link](/url "title \"&quot;")
  5901. .
  5902. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5903. ````````````````````````````````
  5904. Titles must be separated from the link using a [whitespace].
  5905. Other [Unicode whitespace] like non-breaking space doesn't work.
  5906. ```````````````````````````````` example
  5907. [link](/url "title")
  5908. .
  5909. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5910. ````````````````````````````````
  5911. Nested balanced quotes are not allowed without escaping:
  5912. ```````````````````````````````` example
  5913. [link](/url "title "and" title")
  5914. .
  5915. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5916. ````````````````````````````````
  5917. But it is easy to work around this by using a different quote type:
  5918. ```````````````````````````````` example
  5919. [link](/url 'title "and" title')
  5920. .
  5921. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5922. ````````````````````````````````
  5923. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5924. title, and its test suite included a test demonstrating this.
  5925. But it is hard to see a good rationale for the extra complexity this
  5926. brings, since there are already many ways---backslash escaping,
  5927. entity and numeric character references, or using a different
  5928. quote type for the enclosing title---to write titles containing
  5929. double quotes. `Markdown.pl`'s handling of titles has a number
  5930. of other strange features. For example, it allows single-quoted
  5931. titles in inline links, but not reference links. And, in
  5932. reference links but not inline links, it allows a title to begin
  5933. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5934. titles with no closing quotation mark, though 1.0.2b8 does not.
  5935. It seems preferable to adopt a simple, rational rule that works
  5936. the same way in inline links and link reference definitions.)
  5937. [Whitespace] is allowed around the destination and title:
  5938. ```````````````````````````````` example
  5939. [link]( /uri
  5940. "title" )
  5941. .
  5942. <p><a href="/uri" title="title">link</a></p>
  5943. ````````````````````````````````
  5944. But it is not allowed between the link text and the
  5945. following parenthesis:
  5946. ```````````````````````````````` example
  5947. [link] (/uri)
  5948. .
  5949. <p>[link] (/uri)</p>
  5950. ````````````````````````````````
  5951. The link text may contain balanced brackets, but not unbalanced ones,
  5952. unless they are escaped:
  5953. ```````````````````````````````` example
  5954. [link [foo [bar]]](/uri)
  5955. .
  5956. <p><a href="/uri">link [foo [bar]]</a></p>
  5957. ````````````````````````````````
  5958. ```````````````````````````````` example
  5959. [link] bar](/uri)
  5960. .
  5961. <p>[link] bar](/uri)</p>
  5962. ````````````````````````````````
  5963. ```````````````````````````````` example
  5964. [link [bar](/uri)
  5965. .
  5966. <p>[link <a href="/uri">bar</a></p>
  5967. ````````````````````````````````
  5968. ```````````````````````````````` example
  5969. [link \[bar](/uri)
  5970. .
  5971. <p><a href="/uri">link [bar</a></p>
  5972. ````````````````````````````````
  5973. The link text may contain inline content:
  5974. ```````````````````````````````` example
  5975. [link *foo **bar** `#`*](/uri)
  5976. .
  5977. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5978. ````````````````````````````````
  5979. ```````````````````````````````` example
  5980. [![moon](moon.jpg)](/uri)
  5981. .
  5982. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5983. ````````````````````````````````
  5984. However, links may not contain other links, at any level of nesting.
  5985. ```````````````````````````````` example
  5986. [foo [bar](/uri)](/uri)
  5987. .
  5988. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5989. ````````````````````````````````
  5990. ```````````````````````````````` example
  5991. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5992. .
  5993. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5994. ````````````````````````````````
  5995. ```````````````````````````````` example
  5996. ![[[foo](uri1)](uri2)](uri3)
  5997. .
  5998. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5999. ````````````````````````````````
  6000. These cases illustrate the precedence of link text grouping over
  6001. emphasis grouping:
  6002. ```````````````````````````````` example
  6003. *[foo*](/uri)
  6004. .
  6005. <p>*<a href="/uri">foo*</a></p>
  6006. ````````````````````````````````
  6007. ```````````````````````````````` example
  6008. [foo *bar](baz*)
  6009. .
  6010. <p><a href="baz*">foo *bar</a></p>
  6011. ````````````````````````````````
  6012. Note that brackets that *aren't* part of links do not take
  6013. precedence:
  6014. ```````````````````````````````` example
  6015. *foo [bar* baz]
  6016. .
  6017. <p><em>foo [bar</em> baz]</p>
  6018. ````````````````````````````````
  6019. These cases illustrate the precedence of HTML tags, code spans,
  6020. and autolinks over link grouping:
  6021. ```````````````````````````````` example
  6022. [foo <bar attr="](baz)">
  6023. .
  6024. <p>[foo <bar attr="](baz)"></p>
  6025. ````````````````````````````````
  6026. ```````````````````````````````` example
  6027. [foo`](/uri)`
  6028. .
  6029. <p>[foo<code>](/uri)</code></p>
  6030. ````````````````````````````````
  6031. ```````````````````````````````` example
  6032. [foo<http://example.com/?search=](uri)>
  6033. .
  6034. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  6035. ````````````````````````````````
  6036. There are three kinds of [reference link](@)s:
  6037. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  6038. and [shortcut](#shortcut-reference-link).
  6039. A [full reference link](@)
  6040. consists of a [link text] immediately followed by a [link label]
  6041. that [matches] a [link reference definition] elsewhere in the document.
  6042. A [link label](@) begins with a left bracket (`[`) and ends
  6043. with the first right bracket (`]`) that is not backslash-escaped.
  6044. Between these brackets there must be at least one [non-whitespace character].
  6045. Unescaped square bracket characters are not allowed inside the
  6046. opening and closing square brackets of [link labels]. A link
  6047. label can have at most 999 characters inside the square
  6048. brackets.
  6049. One label [matches](@)
  6050. another just in case their normalized forms are equal. To normalize a
  6051. label, strip off the opening and closing brackets,
  6052. perform the *Unicode case fold*, strip leading and trailing
  6053. [whitespace] and collapse consecutive internal
  6054. [whitespace] to a single space. If there are multiple
  6055. matching reference link definitions, the one that comes first in the
  6056. document is used. (It is desirable in such cases to emit a warning.)
  6057. The link's URI and title are provided by the matching [link
  6058. reference definition].
  6059. Here is a simple example:
  6060. ```````````````````````````````` example
  6061. [foo][bar]
  6062. [bar]: /url "title"
  6063. .
  6064. <p><a href="/url" title="title">foo</a></p>
  6065. ````````````````````````````````
  6066. The rules for the [link text] are the same as with
  6067. [inline links]. Thus:
  6068. The link text may contain balanced brackets, but not unbalanced ones,
  6069. unless they are escaped:
  6070. ```````````````````````````````` example
  6071. [link [foo [bar]]][ref]
  6072. [ref]: /uri
  6073. .
  6074. <p><a href="/uri">link [foo [bar]]</a></p>
  6075. ````````````````````````````````
  6076. ```````````````````````````````` example
  6077. [link \[bar][ref]
  6078. [ref]: /uri
  6079. .
  6080. <p><a href="/uri">link [bar</a></p>
  6081. ````````````````````````````````
  6082. The link text may contain inline content:
  6083. ```````````````````````````````` example
  6084. [link *foo **bar** `#`*][ref]
  6085. [ref]: /uri
  6086. .
  6087. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6088. ````````````````````````````````
  6089. ```````````````````````````````` example
  6090. [![moon](moon.jpg)][ref]
  6091. [ref]: /uri
  6092. .
  6093. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6094. ````````````````````````````````
  6095. However, links may not contain other links, at any level of nesting.
  6096. ```````````````````````````````` example
  6097. [foo [bar](/uri)][ref]
  6098. [ref]: /uri
  6099. .
  6100. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  6101. ````````````````````````````````
  6102. ```````````````````````````````` example
  6103. [foo *bar [baz][ref]*][ref]
  6104. [ref]: /uri
  6105. .
  6106. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  6107. ````````````````````````````````
  6108. (In the examples above, we have two [shortcut reference links]
  6109. instead of one [full reference link].)
  6110. The following cases illustrate the precedence of link text grouping over
  6111. emphasis grouping:
  6112. ```````````````````````````````` example
  6113. *[foo*][ref]
  6114. [ref]: /uri
  6115. .
  6116. <p>*<a href="/uri">foo*</a></p>
  6117. ````````````````````````````````
  6118. ```````````````````````````````` example
  6119. [foo *bar][ref]*
  6120. [ref]: /uri
  6121. .
  6122. <p><a href="/uri">foo *bar</a>*</p>
  6123. ````````````````````````````````
  6124. These cases illustrate the precedence of HTML tags, code spans,
  6125. and autolinks over link grouping:
  6126. ```````````````````````````````` example
  6127. [foo <bar attr="][ref]">
  6128. [ref]: /uri
  6129. .
  6130. <p>[foo <bar attr="][ref]"></p>
  6131. ````````````````````````````````
  6132. ```````````````````````````````` example
  6133. [foo`][ref]`
  6134. [ref]: /uri
  6135. .
  6136. <p>[foo<code>][ref]</code></p>
  6137. ````````````````````````````````
  6138. ```````````````````````````````` example
  6139. [foo<http://example.com/?search=][ref]>
  6140. [ref]: /uri
  6141. .
  6142. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6143. ````````````````````````````````
  6144. Matching is case-insensitive:
  6145. ```````````````````````````````` example
  6146. [foo][BaR]
  6147. [bar]: /url "title"
  6148. .
  6149. <p><a href="/url" title="title">foo</a></p>
  6150. ````````````````````````````````
  6151. Unicode case fold is used:
  6152. ```````````````````````````````` example
  6153. [ẞ]
  6154. [SS]: /url
  6155. .
  6156. <p><a href="/url">ẞ</a></p>
  6157. ````````````````````````````````
  6158. Consecutive internal [whitespace] is treated as one space for
  6159. purposes of determining matching:
  6160. ```````````````````````````````` example
  6161. [Foo
  6162. bar]: /url
  6163. [Baz][Foo bar]
  6164. .
  6165. <p><a href="/url">Baz</a></p>
  6166. ````````````````````````````````
  6167. No [whitespace] is allowed between the [link text] and the
  6168. [link label]:
  6169. ```````````````````````````````` example
  6170. [foo] [bar]
  6171. [bar]: /url "title"
  6172. .
  6173. <p>[foo] <a href="/url" title="title">bar</a></p>
  6174. ````````````````````````````````
  6175. ```````````````````````````````` example
  6176. [foo]
  6177. [bar]
  6178. [bar]: /url "title"
  6179. .
  6180. <p>[foo]
  6181. <a href="/url" title="title">bar</a></p>
  6182. ````````````````````````````````
  6183. This is a departure from John Gruber's original Markdown syntax
  6184. description, which explicitly allows whitespace between the link
  6185. text and the link label. It brings reference links in line with
  6186. [inline links], which (according to both original Markdown and
  6187. this spec) cannot have whitespace after the link text. More
  6188. importantly, it prevents inadvertent capture of consecutive
  6189. [shortcut reference links]. If whitespace is allowed between the
  6190. link text and the link label, then in the following we will have
  6191. a single reference link, not two shortcut reference links, as
  6192. intended:
  6193. ``` markdown
  6194. [foo]
  6195. [bar]
  6196. [foo]: /url1
  6197. [bar]: /url2
  6198. ```
  6199. (Note that [shortcut reference links] were introduced by Gruber
  6200. himself in a beta version of `Markdown.pl`, but never included
  6201. in the official syntax description. Without shortcut reference
  6202. links, it is harmless to allow space between the link text and
  6203. link label; but once shortcut references are introduced, it is
  6204. too dangerous to allow this, as it frequently leads to
  6205. unintended results.)
  6206. When there are multiple matching [link reference definitions],
  6207. the first is used:
  6208. ```````````````````````````````` example
  6209. [foo]: /url1
  6210. [foo]: /url2
  6211. [bar][foo]
  6212. .
  6213. <p><a href="/url1">bar</a></p>
  6214. ````````````````````````````````
  6215. Note that matching is performed on normalized strings, not parsed
  6216. inline content. So the following does not match, even though the
  6217. labels define equivalent inline content:
  6218. ```````````````````````````````` example
  6219. [bar][foo\!]
  6220. [foo!]: /url
  6221. .
  6222. <p>[bar][foo!]</p>
  6223. ````````````````````````````````
  6224. [Link labels] cannot contain brackets, unless they are
  6225. backslash-escaped:
  6226. ```````````````````````````````` example
  6227. [foo][ref[]
  6228. [ref[]: /uri
  6229. .
  6230. <p>[foo][ref[]</p>
  6231. <p>[ref[]: /uri</p>
  6232. ````````````````````````````````
  6233. ```````````````````````````````` example
  6234. [foo][ref[bar]]
  6235. [ref[bar]]: /uri
  6236. .
  6237. <p>[foo][ref[bar]]</p>
  6238. <p>[ref[bar]]: /uri</p>
  6239. ````````````````````````````````
  6240. ```````````````````````````````` example
  6241. [[[foo]]]
  6242. [[[foo]]]: /url
  6243. .
  6244. <p>[[[foo]]]</p>
  6245. <p>[[[foo]]]: /url</p>
  6246. ````````````````````````````````
  6247. ```````````````````````````````` example
  6248. [foo][ref\[]
  6249. [ref\[]: /uri
  6250. .
  6251. <p><a href="/uri">foo</a></p>
  6252. ````````````````````````````````
  6253. Note that in this example `]` is not backslash-escaped:
  6254. ```````````````````````````````` example
  6255. [bar\\]: /uri
  6256. [bar\\]
  6257. .
  6258. <p><a href="/uri">bar\</a></p>
  6259. ````````````````````````````````
  6260. A [link label] must contain at least one [non-whitespace character]:
  6261. ```````````````````````````````` example
  6262. []
  6263. []: /uri
  6264. .
  6265. <p>[]</p>
  6266. <p>[]: /uri</p>
  6267. ````````````````````````````````
  6268. ```````````````````````````````` example
  6269. [
  6270. ]
  6271. [
  6272. ]: /uri
  6273. .
  6274. <p>[
  6275. ]</p>
  6276. <p>[
  6277. ]: /uri</p>
  6278. ````````````````````````````````
  6279. A [collapsed reference link](@)
  6280. consists of a [link label] that [matches] a
  6281. [link reference definition] elsewhere in the
  6282. document, followed by the string `[]`.
  6283. The contents of the first link label are parsed as inlines,
  6284. which are used as the link's text. The link's URI and title are
  6285. provided by the matching reference link definition. Thus,
  6286. `[foo][]` is equivalent to `[foo][foo]`.
  6287. ```````````````````````````````` example
  6288. [foo][]
  6289. [foo]: /url "title"
  6290. .
  6291. <p><a href="/url" title="title">foo</a></p>
  6292. ````````````````````````````````
  6293. ```````````````````````````````` example
  6294. [*foo* bar][]
  6295. [*foo* bar]: /url "title"
  6296. .
  6297. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6298. ````````````````````````````````
  6299. The link labels are case-insensitive:
  6300. ```````````````````````````````` example
  6301. [Foo][]
  6302. [foo]: /url "title"
  6303. .
  6304. <p><a href="/url" title="title">Foo</a></p>
  6305. ````````````````````````````````
  6306. As with full reference links, [whitespace] is not
  6307. allowed between the two sets of brackets:
  6308. ```````````````````````````````` example
  6309. [foo]
  6310. []
  6311. [foo]: /url "title"
  6312. .
  6313. <p><a href="/url" title="title">foo</a>
  6314. []</p>
  6315. ````````````````````````````````
  6316. A [shortcut reference link](@)
  6317. consists of a [link label] that [matches] a
  6318. [link reference definition] elsewhere in the
  6319. document and is not followed by `[]` or a link label.
  6320. The contents of the first link label are parsed as inlines,
  6321. which are used as the link's text. The link's URI and title
  6322. are provided by the matching link reference definition.
  6323. Thus, `[foo]` is equivalent to `[foo][]`.
  6324. ```````````````````````````````` example
  6325. [foo]
  6326. [foo]: /url "title"
  6327. .
  6328. <p><a href="/url" title="title">foo</a></p>
  6329. ````````````````````````````````
  6330. ```````````````````````````````` example
  6331. [*foo* bar]
  6332. [*foo* bar]: /url "title"
  6333. .
  6334. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6335. ````````````````````````````````
  6336. ```````````````````````````````` example
  6337. [[*foo* bar]]
  6338. [*foo* bar]: /url "title"
  6339. .
  6340. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6341. ````````````````````````````````
  6342. ```````````````````````````````` example
  6343. [[bar [foo]
  6344. [foo]: /url
  6345. .
  6346. <p>[[bar <a href="/url">foo</a></p>
  6347. ````````````````````````````````
  6348. The link labels are case-insensitive:
  6349. ```````````````````````````````` example
  6350. [Foo]
  6351. [foo]: /url "title"
  6352. .
  6353. <p><a href="/url" title="title">Foo</a></p>
  6354. ````````````````````````````````
  6355. A space after the link text should be preserved:
  6356. ```````````````````````````````` example
  6357. [foo] bar
  6358. [foo]: /url
  6359. .
  6360. <p><a href="/url">foo</a> bar</p>
  6361. ````````````````````````````````
  6362. If you just want bracketed text, you can backslash-escape the
  6363. opening bracket to avoid links:
  6364. ```````````````````````````````` example
  6365. \[foo]
  6366. [foo]: /url "title"
  6367. .
  6368. <p>[foo]</p>
  6369. ````````````````````````````````
  6370. Note that this is a link, because a link label ends with the first
  6371. following closing bracket:
  6372. ```````````````````````````````` example
  6373. [foo*]: /url
  6374. *[foo*]
  6375. .
  6376. <p>*<a href="/url">foo*</a></p>
  6377. ````````````````````````````````
  6378. Full and compact references take precedence over shortcut
  6379. references:
  6380. ```````````````````````````````` example
  6381. [foo][bar]
  6382. [foo]: /url1
  6383. [bar]: /url2
  6384. .
  6385. <p><a href="/url2">foo</a></p>
  6386. ````````````````````````````````
  6387. ```````````````````````````````` example
  6388. [foo][]
  6389. [foo]: /url1
  6390. .
  6391. <p><a href="/url1">foo</a></p>
  6392. ````````````````````````````````
  6393. Inline links also take precedence:
  6394. ```````````````````````````````` example
  6395. [foo]()
  6396. [foo]: /url1
  6397. .
  6398. <p><a href="">foo</a></p>
  6399. ````````````````````````````````
  6400. ```````````````````````````````` example
  6401. [foo](not a link)
  6402. [foo]: /url1
  6403. .
  6404. <p><a href="/url1">foo</a>(not a link)</p>
  6405. ````````````````````````````````
  6406. In the following case `[bar][baz]` is parsed as a reference,
  6407. `[foo]` as normal text:
  6408. ```````````````````````````````` example
  6409. [foo][bar][baz]
  6410. [baz]: /url
  6411. .
  6412. <p>[foo]<a href="/url">bar</a></p>
  6413. ````````````````````````````````
  6414. Here, though, `[foo][bar]` is parsed as a reference, since
  6415. `[bar]` is defined:
  6416. ```````````````````````````````` example
  6417. [foo][bar][baz]
  6418. [baz]: /url1
  6419. [bar]: /url2
  6420. .
  6421. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6422. ````````````````````````````````
  6423. Here `[foo]` is not parsed as a shortcut reference, because it
  6424. is followed by a link label (even though `[bar]` is not defined):
  6425. ```````````````````````````````` example
  6426. [foo][bar][baz]
  6427. [baz]: /url1
  6428. [foo]: /url2
  6429. .
  6430. <p>[foo]<a href="/url1">bar</a></p>
  6431. ````````````````````````````````
  6432. ## Images
  6433. Syntax for images is like the syntax for links, with one
  6434. difference. Instead of [link text], we have an
  6435. [image description](@). The rules for this are the
  6436. same as for [link text], except that (a) an
  6437. image description starts with `![` rather than `[`, and
  6438. (b) an image description may contain links.
  6439. An image description has inline elements
  6440. as its contents. When an image is rendered to HTML,
  6441. this is standardly used as the image's `alt` attribute.
  6442. ```````````````````````````````` example
  6443. ![foo](/url "title")
  6444. .
  6445. <p><img src="/url" alt="foo" title="title" /></p>
  6446. ````````````````````````````````
  6447. ```````````````````````````````` example
  6448. ![foo *bar*]
  6449. [foo *bar*]: train.jpg "train & tracks"
  6450. .
  6451. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6452. ````````````````````````````````
  6453. ```````````````````````````````` example
  6454. ![foo ![bar](/url)](/url2)
  6455. .
  6456. <p><img src="/url2" alt="foo bar" /></p>
  6457. ````````````````````````````````
  6458. ```````````````````````````````` example
  6459. ![foo [bar](/url)](/url2)
  6460. .
  6461. <p><img src="/url2" alt="foo bar" /></p>
  6462. ````````````````````````````````
  6463. Though this spec is concerned with parsing, not rendering, it is
  6464. recommended that in rendering to HTML, only the plain string content
  6465. of the [image description] be used. Note that in
  6466. the above example, the alt attribute's value is `foo bar`, not `foo
  6467. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6468. content is rendered, without formatting.
  6469. ```````````````````````````````` example
  6470. ![foo *bar*][]
  6471. [foo *bar*]: train.jpg "train & tracks"
  6472. .
  6473. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6474. ````````````````````````````````
  6475. ```````````````````````````````` example
  6476. ![foo *bar*][foobar]
  6477. [FOOBAR]: train.jpg "train & tracks"
  6478. .
  6479. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6480. ````````````````````````````````
  6481. ```````````````````````````````` example
  6482. ![foo](train.jpg)
  6483. .
  6484. <p><img src="train.jpg" alt="foo" /></p>
  6485. ````````````````````````````````
  6486. ```````````````````````````````` example
  6487. My ![foo bar](/path/to/train.jpg "title" )
  6488. .
  6489. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6490. ````````````````````````````````
  6491. ```````````````````````````````` example
  6492. ![foo](<url>)
  6493. .
  6494. <p><img src="url" alt="foo" /></p>
  6495. ````````````````````````````````
  6496. ```````````````````````````````` example
  6497. ![](/url)
  6498. .
  6499. <p><img src="/url" alt="" /></p>
  6500. ````````````````````````````````
  6501. Reference-style:
  6502. ```````````````````````````````` example
  6503. ![foo][bar]
  6504. [bar]: /url
  6505. .
  6506. <p><img src="/url" alt="foo" /></p>
  6507. ````````````````````````````````
  6508. ```````````````````````````````` example
  6509. ![foo][bar]
  6510. [BAR]: /url
  6511. .
  6512. <p><img src="/url" alt="foo" /></p>
  6513. ````````````````````````````````
  6514. Collapsed:
  6515. ```````````````````````````````` example
  6516. ![foo][]
  6517. [foo]: /url "title"
  6518. .
  6519. <p><img src="/url" alt="foo" title="title" /></p>
  6520. ````````````````````````````````
  6521. ```````````````````````````````` example
  6522. ![*foo* bar][]
  6523. [*foo* bar]: /url "title"
  6524. .
  6525. <p><img src="/url" alt="foo bar" title="title" /></p>
  6526. ````````````````````````````````
  6527. The labels are case-insensitive:
  6528. ```````````````````````````````` example
  6529. ![Foo][]
  6530. [foo]: /url "title"
  6531. .
  6532. <p><img src="/url" alt="Foo" title="title" /></p>
  6533. ````````````````````````````````
  6534. As with reference links, [whitespace] is not allowed
  6535. between the two sets of brackets:
  6536. ```````````````````````````````` example
  6537. ![foo]
  6538. []
  6539. [foo]: /url "title"
  6540. .
  6541. <p><img src="/url" alt="foo" title="title" />
  6542. []</p>
  6543. ````````````````````````````````
  6544. Shortcut:
  6545. ```````````````````````````````` example
  6546. ![foo]
  6547. [foo]: /url "title"
  6548. .
  6549. <p><img src="/url" alt="foo" title="title" /></p>
  6550. ````````````````````````````````
  6551. ```````````````````````````````` example
  6552. ![*foo* bar]
  6553. [*foo* bar]: /url "title"
  6554. .
  6555. <p><img src="/url" alt="foo bar" title="title" /></p>
  6556. ````````````````````````````````
  6557. Note that link labels cannot contain unescaped brackets:
  6558. ```````````````````````````````` example
  6559. ![[foo]]
  6560. [[foo]]: /url "title"
  6561. .
  6562. <p>![[foo]]</p>
  6563. <p>[[foo]]: /url &quot;title&quot;</p>
  6564. ````````````````````````````````
  6565. The link labels are case-insensitive:
  6566. ```````````````````````````````` example
  6567. ![Foo]
  6568. [foo]: /url "title"
  6569. .
  6570. <p><img src="/url" alt="Foo" title="title" /></p>
  6571. ````````````````````````````````
  6572. If you just want a literal `!` followed by bracketed text, you can
  6573. backslash-escape the opening `[`:
  6574. ```````````````````````````````` example
  6575. !\[foo]
  6576. [foo]: /url "title"
  6577. .
  6578. <p>![foo]</p>
  6579. ````````````````````````````````
  6580. If you want a link after a literal `!`, backslash-escape the
  6581. `!`:
  6582. ```````````````````````````````` example
  6583. \![foo]
  6584. [foo]: /url "title"
  6585. .
  6586. <p>!<a href="/url" title="title">foo</a></p>
  6587. ````````````````````````````````
  6588. ## Autolinks
  6589. [Autolink](@)s are absolute URIs and email addresses inside
  6590. `<` and `>`. They are parsed as links, with the URL or email address
  6591. as the link label.
  6592. A [URI autolink](@) consists of `<`, followed by an
  6593. [absolute URI] followed by `>`. It is parsed as
  6594. a link to the URI, with the URI as the link's label.
  6595. An [absolute URI](@),
  6596. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6597. followed by zero or more characters other [ASCII control
  6598. characters][ASCII control character] or [whitespace][] , `<`, and `>`.
  6599. If the URI includes these characters, they must be percent-encoded
  6600. (e.g. `%20` for a space).
  6601. For purposes of this spec, a [scheme](@) is any sequence
  6602. of 2--32 characters beginning with an ASCII letter and followed
  6603. by any combination of ASCII letters, digits, or the symbols plus
  6604. ("+"), period ("."), or hyphen ("-").
  6605. Here are some valid autolinks:
  6606. ```````````````````````````````` example
  6607. <http://foo.bar.baz>
  6608. .
  6609. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6610. ````````````````````````````````
  6611. ```````````````````````````````` example
  6612. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6613. .
  6614. <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>
  6615. ````````````````````````````````
  6616. ```````````````````````````````` example
  6617. <irc://foo.bar:2233/baz>
  6618. .
  6619. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6620. ````````````````````````````````
  6621. Uppercase is also fine:
  6622. ```````````````````````````````` example
  6623. <MAILTO:FOO@BAR.BAZ>
  6624. .
  6625. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6626. ````````````````````````````````
  6627. Note that many strings that count as [absolute URIs] for
  6628. purposes of this spec are not valid URIs, because their
  6629. schemes are not registered or because of other problems
  6630. with their syntax:
  6631. ```````````````````````````````` example
  6632. <a+b+c:d>
  6633. .
  6634. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6635. ````````````````````````````````
  6636. ```````````````````````````````` example
  6637. <made-up-scheme://foo,bar>
  6638. .
  6639. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6640. ````````````````````````````````
  6641. ```````````````````````````````` example
  6642. <http://../>
  6643. .
  6644. <p><a href="http://../">http://../</a></p>
  6645. ````````````````````````````````
  6646. ```````````````````````````````` example
  6647. <localhost:5001/foo>
  6648. .
  6649. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6650. ````````````````````````````````
  6651. Spaces are not allowed in autolinks:
  6652. ```````````````````````````````` example
  6653. <http://foo.bar/baz bim>
  6654. .
  6655. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6656. ````````````````````````````````
  6657. Backslash-escapes do not work inside autolinks:
  6658. ```````````````````````````````` example
  6659. <http://example.com/\[\>
  6660. .
  6661. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6662. ````````````````````````````````
  6663. An [email autolink](@)
  6664. consists of `<`, followed by an [email address],
  6665. followed by `>`. The link's label is the email address,
  6666. and the URL is `mailto:` followed by the email address.
  6667. An [email address](@),
  6668. for these purposes, is anything that matches
  6669. the [non-normative regex from the HTML5
  6670. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6671. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6672. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6673. Examples of email autolinks:
  6674. ```````````````````````````````` example
  6675. <foo@bar.example.com>
  6676. .
  6677. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6678. ````````````````````````````````
  6679. ```````````````````````````````` example
  6680. <foo+special@Bar.baz-bar0.com>
  6681. .
  6682. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6683. ````````````````````````````````
  6684. Backslash-escapes do not work inside email autolinks:
  6685. ```````````````````````````````` example
  6686. <foo\+@bar.example.com>
  6687. .
  6688. <p>&lt;foo+@bar.example.com&gt;</p>
  6689. ````````````````````````````````
  6690. These are not autolinks:
  6691. ```````````````````````````````` example
  6692. <>
  6693. .
  6694. <p>&lt;&gt;</p>
  6695. ````````````````````````````````
  6696. ```````````````````````````````` example
  6697. < http://foo.bar >
  6698. .
  6699. <p>&lt; http://foo.bar &gt;</p>
  6700. ````````````````````````````````
  6701. ```````````````````````````````` example
  6702. <m:abc>
  6703. .
  6704. <p>&lt;m:abc&gt;</p>
  6705. ````````````````````````````````
  6706. ```````````````````````````````` example
  6707. <foo.bar.baz>
  6708. .
  6709. <p>&lt;foo.bar.baz&gt;</p>
  6710. ````````````````````````````````
  6711. ```````````````````````````````` example
  6712. http://example.com
  6713. .
  6714. <p>http://example.com</p>
  6715. ````````````````````````````````
  6716. ```````````````````````````````` example
  6717. foo@bar.example.com
  6718. .
  6719. <p>foo@bar.example.com</p>
  6720. ````````````````````````````````
  6721. ## Raw HTML
  6722. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6723. raw HTML tag and will be rendered in HTML without escaping.
  6724. Tag and attribute names are not limited to current HTML tags,
  6725. so custom tags (and even, say, DocBook tags) may be used.
  6726. Here is the grammar for tags:
  6727. A [tag name](@) consists of an ASCII letter
  6728. followed by zero or more ASCII letters, digits, or
  6729. hyphens (`-`).
  6730. An [attribute](@) consists of [whitespace],
  6731. an [attribute name], and an optional
  6732. [attribute value specification].
  6733. An [attribute name](@)
  6734. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6735. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6736. specification restricted to ASCII. HTML5 is laxer.)
  6737. An [attribute value specification](@)
  6738. consists of optional [whitespace],
  6739. a `=` character, optional [whitespace], and an [attribute
  6740. value].
  6741. An [attribute value](@)
  6742. consists of an [unquoted attribute value],
  6743. a [single-quoted attribute value], or a [double-quoted attribute value].
  6744. An [unquoted attribute value](@)
  6745. is a nonempty string of characters not
  6746. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6747. A [single-quoted attribute value](@)
  6748. consists of `'`, zero or more
  6749. characters not including `'`, and a final `'`.
  6750. A [double-quoted attribute value](@)
  6751. consists of `"`, zero or more
  6752. characters not including `"`, and a final `"`.
  6753. An [open tag](@) consists of a `<` character, a [tag name],
  6754. zero or more [attributes], optional [whitespace], an optional `/`
  6755. character, and a `>` character.
  6756. A [closing tag](@) consists of the string `</`, a
  6757. [tag name], optional [whitespace], and the character `>`.
  6758. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6759. where *text* does not start with `>` or `->`, does not end with `-`,
  6760. and does not contain `--`. (See the
  6761. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6762. A [processing instruction](@)
  6763. consists of the string `<?`, a string
  6764. of characters not including the string `?>`, and the string
  6765. `?>`.
  6766. A [declaration](@) consists of the string `<!`, an ASCII letter, zero or more
  6767. characters not including the character `>`, and the character `>`.
  6768. A [CDATA section](@) consists of
  6769. the string `<![CDATA[`, a string of characters not including the string
  6770. `]]>`, and the string `]]>`.
  6771. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6772. an [HTML comment], a [processing instruction], a [declaration],
  6773. or a [CDATA section].
  6774. Here are some simple open tags:
  6775. ```````````````````````````````` example
  6776. <a><bab><c2c>
  6777. .
  6778. <p><a><bab><c2c></p>
  6779. ````````````````````````````````
  6780. Empty elements:
  6781. ```````````````````````````````` example
  6782. <a/><b2/>
  6783. .
  6784. <p><a/><b2/></p>
  6785. ````````````````````````````````
  6786. [Whitespace] is allowed:
  6787. ```````````````````````````````` example
  6788. <a /><b2
  6789. data="foo" >
  6790. .
  6791. <p><a /><b2
  6792. data="foo" ></p>
  6793. ````````````````````````````````
  6794. With attributes:
  6795. ```````````````````````````````` example
  6796. <a foo="bar" bam = 'baz <em>"</em>'
  6797. _boolean zoop:33=zoop:33 />
  6798. .
  6799. <p><a foo="bar" bam = 'baz <em>"</em>'
  6800. _boolean zoop:33=zoop:33 /></p>
  6801. ````````````````````````````````
  6802. Custom tag names can be used:
  6803. ```````````````````````````````` example
  6804. Foo <responsive-image src="foo.jpg" />
  6805. .
  6806. <p>Foo <responsive-image src="foo.jpg" /></p>
  6807. ````````````````````````````````
  6808. Illegal tag names, not parsed as HTML:
  6809. ```````````````````````````````` example
  6810. <33> <__>
  6811. .
  6812. <p>&lt;33&gt; &lt;__&gt;</p>
  6813. ````````````````````````````````
  6814. Illegal attribute names:
  6815. ```````````````````````````````` example
  6816. <a h*#ref="hi">
  6817. .
  6818. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6819. ````````````````````````````````
  6820. Illegal attribute values:
  6821. ```````````````````````````````` example
  6822. <a href="hi'> <a href=hi'>
  6823. .
  6824. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6825. ````````````````````````````````
  6826. Illegal [whitespace]:
  6827. ```````````````````````````````` example
  6828. < a><
  6829. foo><bar/ >
  6830. <foo bar=baz
  6831. bim!bop />
  6832. .
  6833. <p>&lt; a&gt;&lt;
  6834. foo&gt;&lt;bar/ &gt;
  6835. &lt;foo bar=baz
  6836. bim!bop /&gt;</p>
  6837. ````````````````````````````````
  6838. Missing [whitespace]:
  6839. ```````````````````````````````` example
  6840. <a href='bar'title=title>
  6841. .
  6842. <p>&lt;a href='bar'title=title&gt;</p>
  6843. ````````````````````````````````
  6844. Closing tags:
  6845. ```````````````````````````````` example
  6846. </a></foo >
  6847. .
  6848. <p></a></foo ></p>
  6849. ````````````````````````````````
  6850. Illegal attributes in closing tag:
  6851. ```````````````````````````````` example
  6852. </a href="foo">
  6853. .
  6854. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6855. ````````````````````````````````
  6856. Comments:
  6857. ```````````````````````````````` example
  6858. foo <!-- this is a
  6859. comment - with hyphen -->
  6860. .
  6861. <p>foo <!-- this is a
  6862. comment - with hyphen --></p>
  6863. ````````````````````````````````
  6864. ```````````````````````````````` example
  6865. foo <!-- not a comment -- two hyphens -->
  6866. .
  6867. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6868. ````````````````````````````````
  6869. Not comments:
  6870. ```````````````````````````````` example
  6871. foo <!--> foo -->
  6872. foo <!-- foo--->
  6873. .
  6874. <p>foo &lt;!--&gt; foo --&gt;</p>
  6875. <p>foo &lt;!-- foo---&gt;</p>
  6876. ````````````````````````````````
  6877. Processing instructions:
  6878. ```````````````````````````````` example
  6879. foo <?php echo $a; ?>
  6880. .
  6881. <p>foo <?php echo $a; ?></p>
  6882. ````````````````````````````````
  6883. Declarations:
  6884. ```````````````````````````````` example
  6885. foo <!ELEMENT br EMPTY>
  6886. .
  6887. <p>foo <!ELEMENT br EMPTY></p>
  6888. ````````````````````````````````
  6889. CDATA sections:
  6890. ```````````````````````````````` example
  6891. foo <![CDATA[>&<]]>
  6892. .
  6893. <p>foo <![CDATA[>&<]]></p>
  6894. ````````````````````````````````
  6895. Entity and numeric character references are preserved in HTML
  6896. attributes:
  6897. ```````````````````````````````` example
  6898. foo <a href="&ouml;">
  6899. .
  6900. <p>foo <a href="&ouml;"></p>
  6901. ````````````````````````````````
  6902. Backslash escapes do not work in HTML attributes:
  6903. ```````````````````````````````` example
  6904. foo <a href="\*">
  6905. .
  6906. <p>foo <a href="\*"></p>
  6907. ````````````````````````````````
  6908. ```````````````````````````````` example
  6909. <a href="\"">
  6910. .
  6911. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6912. ````````````````````````````````
  6913. ## Hard line breaks
  6914. A line break (not in a code span or HTML tag) that is preceded
  6915. by two or more spaces and does not occur at the end of a block
  6916. is parsed as a [hard line break](@) (rendered
  6917. in HTML as a `<br />` tag):
  6918. ```````````````````````````````` example
  6919. foo
  6920. baz
  6921. .
  6922. <p>foo<br />
  6923. baz</p>
  6924. ````````````````````````````````
  6925. For a more visible alternative, a backslash before the
  6926. [line ending] may be used instead of two spaces:
  6927. ```````````````````````````````` example
  6928. foo\
  6929. baz
  6930. .
  6931. <p>foo<br />
  6932. baz</p>
  6933. ````````````````````````````````
  6934. More than two spaces can be used:
  6935. ```````````````````````````````` example
  6936. foo
  6937. baz
  6938. .
  6939. <p>foo<br />
  6940. baz</p>
  6941. ````````````````````````````````
  6942. Leading spaces at the beginning of the next line are ignored:
  6943. ```````````````````````````````` example
  6944. foo
  6945. bar
  6946. .
  6947. <p>foo<br />
  6948. bar</p>
  6949. ````````````````````````````````
  6950. ```````````````````````````````` example
  6951. foo\
  6952. bar
  6953. .
  6954. <p>foo<br />
  6955. bar</p>
  6956. ````````````````````````````````
  6957. Line breaks can occur inside emphasis, links, and other constructs
  6958. that allow inline content:
  6959. ```````````````````````````````` example
  6960. *foo
  6961. bar*
  6962. .
  6963. <p><em>foo<br />
  6964. bar</em></p>
  6965. ````````````````````````````````
  6966. ```````````````````````````````` example
  6967. *foo\
  6968. bar*
  6969. .
  6970. <p><em>foo<br />
  6971. bar</em></p>
  6972. ````````````````````````````````
  6973. Line breaks do not occur inside code spans
  6974. ```````````````````````````````` example
  6975. `code
  6976. span`
  6977. .
  6978. <p><code>code span</code></p>
  6979. ````````````````````````````````
  6980. ```````````````````````````````` example
  6981. `code\
  6982. span`
  6983. .
  6984. <p><code>code\ span</code></p>
  6985. ````````````````````````````````
  6986. or HTML tags:
  6987. ```````````````````````````````` example
  6988. <a href="foo
  6989. bar">
  6990. .
  6991. <p><a href="foo
  6992. bar"></p>
  6993. ````````````````````````````````
  6994. ```````````````````````````````` example
  6995. <a href="foo\
  6996. bar">
  6997. .
  6998. <p><a href="foo\
  6999. bar"></p>
  7000. ````````````````````````````````
  7001. Hard line breaks are for separating inline content within a block.
  7002. Neither syntax for hard line breaks works at the end of a paragraph or
  7003. other block element:
  7004. ```````````````````````````````` example
  7005. foo\
  7006. .
  7007. <p>foo\</p>
  7008. ````````````````````````````````
  7009. ```````````````````````````````` example
  7010. foo
  7011. .
  7012. <p>foo</p>
  7013. ````````````````````````````````
  7014. ```````````````````````````````` example
  7015. ### foo\
  7016. .
  7017. <h3>foo\</h3>
  7018. ````````````````````````````````
  7019. ```````````````````````````````` example
  7020. ### foo
  7021. .
  7022. <h3>foo</h3>
  7023. ````````````````````````````````
  7024. ## Soft line breaks
  7025. A regular line break (not in a code span or HTML tag) that is not
  7026. preceded by two or more spaces or a backslash is parsed as a
  7027. [softbreak](@). (A softbreak may be rendered in HTML either as a
  7028. [line ending] or as a space. The result will be the same in
  7029. browsers. In the examples here, a [line ending] will be used.)
  7030. ```````````````````````````````` example
  7031. foo
  7032. baz
  7033. .
  7034. <p>foo
  7035. baz</p>
  7036. ````````````````````````````````
  7037. Spaces at the end of the line and beginning of the next line are
  7038. removed:
  7039. ```````````````````````````````` example
  7040. foo
  7041. baz
  7042. .
  7043. <p>foo
  7044. baz</p>
  7045. ````````````````````````````````
  7046. A conforming parser may render a soft line break in HTML either as a
  7047. line break or as a space.
  7048. A renderer may also provide an option to render soft line breaks
  7049. as hard line breaks.
  7050. ## Textual content
  7051. Any characters not given an interpretation by the above rules will
  7052. be parsed as plain textual content.
  7053. ```````````````````````````````` example
  7054. hello $.;'there
  7055. .
  7056. <p>hello $.;'there</p>
  7057. ````````````````````````````````
  7058. ```````````````````````````````` example
  7059. Foo χρῆν
  7060. .
  7061. <p>Foo χρῆν</p>
  7062. ````````````````````````````````
  7063. Internal spaces are preserved verbatim:
  7064. ```````````````````````````````` example
  7065. Multiple spaces
  7066. .
  7067. <p>Multiple spaces</p>
  7068. ````````````````````````````````
  7069. <!-- END TESTS -->
  7070. # Appendix: A parsing strategy
  7071. In this appendix we describe some features of the parsing strategy
  7072. used in the CommonMark reference implementations.
  7073. ## Overview
  7074. Parsing has two phases:
  7075. 1. In the first phase, lines of input are consumed and the block
  7076. structure of the document---its division into paragraphs, block quotes,
  7077. list items, and so on---is constructed. Text is assigned to these
  7078. blocks but not parsed. Link reference definitions are parsed and a
  7079. map of links is constructed.
  7080. 2. In the second phase, the raw text contents of paragraphs and headings
  7081. are parsed into sequences of Markdown inline elements (strings,
  7082. code spans, links, emphasis, and so on), using the map of link
  7083. references constructed in phase 1.
  7084. At each point in processing, the document is represented as a tree of
  7085. **blocks**. The root of the tree is a `document` block. The `document`
  7086. may have any number of other blocks as **children**. These children
  7087. may, in turn, have other blocks as children. The last child of a block
  7088. is normally considered **open**, meaning that subsequent lines of input
  7089. can alter its contents. (Blocks that are not open are **closed**.)
  7090. Here, for example, is a possible document tree, with the open blocks
  7091. marked by arrows:
  7092. ``` tree
  7093. -> document
  7094. -> block_quote
  7095. paragraph
  7096. "Lorem ipsum dolor\nsit amet."
  7097. -> list (type=bullet tight=true bullet_char=-)
  7098. list_item
  7099. paragraph
  7100. "Qui *quodsi iracundia*"
  7101. -> list_item
  7102. -> paragraph
  7103. "aliquando id"
  7104. ```
  7105. ## Phase 1: block structure
  7106. Each line that is processed has an effect on this tree. The line is
  7107. analyzed and, depending on its contents, the document may be altered
  7108. in one or more of the following ways:
  7109. 1. One or more open blocks may be closed.
  7110. 2. One or more new blocks may be created as children of the
  7111. last open block.
  7112. 3. Text may be added to the last (deepest) open block remaining
  7113. on the tree.
  7114. Once a line has been incorporated into the tree in this way,
  7115. it can be discarded, so input can be read in a stream.
  7116. For each line, we follow this procedure:
  7117. 1. First we iterate through the open blocks, starting with the
  7118. root document, and descending through last children down to the last
  7119. open block. Each block imposes a condition that the line must satisfy
  7120. if the block is to remain open. For example, a block quote requires a
  7121. `>` character. A paragraph requires a non-blank line.
  7122. In this phase we may match all or just some of the open
  7123. blocks. But we cannot close unmatched blocks yet, because we may have a
  7124. [lazy continuation line].
  7125. 2. Next, after consuming the continuation markers for existing
  7126. blocks, we look for new block starts (e.g. `>` for a block quote).
  7127. If we encounter a new block start, we close any blocks unmatched
  7128. in step 1 before creating the new block as a child of the last
  7129. matched container block.
  7130. 3. Finally, we look at the remainder of the line (after block
  7131. markers like `>`, list markers, and indentation have been consumed).
  7132. This is text that can be incorporated into the last open
  7133. block (a paragraph, code block, heading, or raw HTML).
  7134. Setext headings are formed when we see a line of a paragraph
  7135. that is a [setext heading underline].
  7136. Reference link definitions are detected when a paragraph is closed;
  7137. the accumulated text lines are parsed to see if they begin with
  7138. one or more reference link definitions. Any remainder becomes a
  7139. normal paragraph.
  7140. We can see how this works by considering how the tree above is
  7141. generated by four lines of Markdown:
  7142. ``` markdown
  7143. > Lorem ipsum dolor
  7144. sit amet.
  7145. > - Qui *quodsi iracundia*
  7146. > - aliquando id
  7147. ```
  7148. At the outset, our document model is just
  7149. ``` tree
  7150. -> document
  7151. ```
  7152. The first line of our text,
  7153. ``` markdown
  7154. > Lorem ipsum dolor
  7155. ```
  7156. causes a `block_quote` block to be created as a child of our
  7157. open `document` block, and a `paragraph` block as a child of
  7158. the `block_quote`. Then the text is added to the last open
  7159. block, the `paragraph`:
  7160. ``` tree
  7161. -> document
  7162. -> block_quote
  7163. -> paragraph
  7164. "Lorem ipsum dolor"
  7165. ```
  7166. The next line,
  7167. ``` markdown
  7168. sit amet.
  7169. ```
  7170. is a "lazy continuation" of the open `paragraph`, so it gets added
  7171. to the paragraph's text:
  7172. ``` tree
  7173. -> document
  7174. -> block_quote
  7175. -> paragraph
  7176. "Lorem ipsum dolor\nsit amet."
  7177. ```
  7178. The third line,
  7179. ``` markdown
  7180. > - Qui *quodsi iracundia*
  7181. ```
  7182. causes the `paragraph` block to be closed, and a new `list` block
  7183. opened as a child of the `block_quote`. A `list_item` is also
  7184. added as a child of the `list`, and a `paragraph` as a child of
  7185. the `list_item`. The text is then added to the new `paragraph`:
  7186. ``` tree
  7187. -> document
  7188. -> block_quote
  7189. paragraph
  7190. "Lorem ipsum dolor\nsit amet."
  7191. -> list (type=bullet tight=true bullet_char=-)
  7192. -> list_item
  7193. -> paragraph
  7194. "Qui *quodsi iracundia*"
  7195. ```
  7196. The fourth line,
  7197. ``` markdown
  7198. > - aliquando id
  7199. ```
  7200. causes the `list_item` (and its child the `paragraph`) to be closed,
  7201. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7202. is added as a child of the new `list_item`, to contain the text.
  7203. We thus obtain the final tree:
  7204. ``` tree
  7205. -> document
  7206. -> block_quote
  7207. paragraph
  7208. "Lorem ipsum dolor\nsit amet."
  7209. -> list (type=bullet tight=true bullet_char=-)
  7210. list_item
  7211. paragraph
  7212. "Qui *quodsi iracundia*"
  7213. -> list_item
  7214. -> paragraph
  7215. "aliquando id"
  7216. ```
  7217. ## Phase 2: inline structure
  7218. Once all of the input has been parsed, all open blocks are closed.
  7219. We then "walk the tree," visiting every node, and parse raw
  7220. string contents of paragraphs and headings as inlines. At this
  7221. point we have seen all the link reference definitions, so we can
  7222. resolve reference links as we go.
  7223. ``` tree
  7224. document
  7225. block_quote
  7226. paragraph
  7227. str "Lorem ipsum dolor"
  7228. softbreak
  7229. str "sit amet."
  7230. list (type=bullet tight=true bullet_char=-)
  7231. list_item
  7232. paragraph
  7233. str "Qui "
  7234. emph
  7235. str "quodsi iracundia"
  7236. list_item
  7237. paragraph
  7238. str "aliquando id"
  7239. ```
  7240. Notice how the [line ending] in the first paragraph has
  7241. been parsed as a `softbreak`, and the asterisks in the first list item
  7242. have become an `emph`.
  7243. ### An algorithm for parsing nested emphasis and links
  7244. By far the trickiest part of inline parsing is handling emphasis,
  7245. strong emphasis, links, and images. This is done using the following
  7246. algorithm.
  7247. When we're parsing inlines and we hit either
  7248. - a run of `*` or `_` characters, or
  7249. - a `[` or `![`
  7250. we insert a text node with these symbols as its literal content, and we
  7251. add a pointer to this text node to the [delimiter stack](@).
  7252. The [delimiter stack] is a doubly linked list. Each
  7253. element contains a pointer to a text node, plus information about
  7254. - the type of delimiter (`[`, `![`, `*`, `_`)
  7255. - the number of delimiters,
  7256. - whether the delimiter is "active" (all are active to start), and
  7257. - whether the delimiter is a potential opener, a potential closer,
  7258. or both (which depends on what sort of characters precede
  7259. and follow the delimiters).
  7260. When we hit a `]` character, we call the *look for link or image*
  7261. procedure (see below).
  7262. When we hit the end of the input, we call the *process emphasis*
  7263. procedure (see below), with `stack_bottom` = NULL.
  7264. #### *look for link or image*
  7265. Starting at the top of the delimiter stack, we look backwards
  7266. through the stack for an opening `[` or `![` delimiter.
  7267. - If we don't find one, we return a literal text node `]`.
  7268. - If we do find one, but it's not *active*, we remove the inactive
  7269. delimiter from the stack, and return a literal text node `]`.
  7270. - If we find one and it's active, then we parse ahead to see if
  7271. we have an inline link/image, reference link/image, compact reference
  7272. link/image, or shortcut reference link/image.
  7273. + If we don't, then we remove the opening delimiter from the
  7274. delimiter stack and return a literal text node `]`.
  7275. + If we do, then
  7276. * We return a link or image node whose children are the inlines
  7277. after the text node pointed to by the opening delimiter.
  7278. * We run *process emphasis* on these inlines, with the `[` opener
  7279. as `stack_bottom`.
  7280. * We remove the opening delimiter.
  7281. * If we have a link (and not an image), we also set all
  7282. `[` delimiters before the opening delimiter to *inactive*. (This
  7283. will prevent us from getting links within links.)
  7284. #### *process emphasis*
  7285. Parameter `stack_bottom` sets a lower bound to how far we
  7286. descend in the [delimiter stack]. If it is NULL, we can
  7287. go all the way to the bottom. Otherwise, we stop before
  7288. visiting `stack_bottom`.
  7289. Let `current_position` point to the element on the [delimiter stack]
  7290. just above `stack_bottom` (or the first element if `stack_bottom`
  7291. is NULL).
  7292. We keep track of the `openers_bottom` for each delimiter
  7293. type (`*`, `_`) and each length of the closing delimiter run
  7294. (modulo 3). Initialize this to `stack_bottom`.
  7295. Then we repeat the following until we run out of potential
  7296. closers:
  7297. - Move `current_position` forward in the delimiter stack (if needed)
  7298. until we find the first potential closer with delimiter `*` or `_`.
  7299. (This will be the potential closer closest
  7300. to the beginning of the input -- the first one in parse order.)
  7301. - Now, look back in the stack (staying above `stack_bottom` and
  7302. the `openers_bottom` for this delimiter type) for the
  7303. first matching potential opener ("matching" means same delimiter).
  7304. - If one is found:
  7305. + Figure out whether we have emphasis or strong emphasis:
  7306. if both closer and opener spans have length >= 2, we have
  7307. strong, otherwise regular.
  7308. + Insert an emph or strong emph node accordingly, after
  7309. the text node corresponding to the opener.
  7310. + Remove any delimiters between the opener and closer from
  7311. the delimiter stack.
  7312. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7313. from the opening and closing text nodes. If they become empty
  7314. as a result, remove them and remove the corresponding element
  7315. of the delimiter stack. If the closing node is removed, reset
  7316. `current_position` to the next element in the stack.
  7317. - If none is found:
  7318. + Set `openers_bottom` to the element before `current_position`.
  7319. (We know that there are no openers for this kind of closer up to and
  7320. including this point, so this puts a lower bound on future searches.)
  7321. + If the closer at `current_position` is not a potential opener,
  7322. remove it from the delimiter stack (since we know it can't
  7323. be a closer either).
  7324. + Advance `current_position` to the next element in the stack.
  7325. After we're done, we remove all delimiters above `stack_bottom` from the
  7326. delimiter stack.