aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 4571a95eab9078d85999bc245d134c9a10db72c7 (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 punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/` (U+0021–2F),
  252. `:`, `;`, `<`, `=`, `>`, `?`, `@` (U+003A–0040),
  253. `[`, `\`, `]`, `^`, `_`, `` ` `` (U+005B–0060),
  254. `{`, `|`, `}`, or `~` (U+007B–007E).
  255. A [punctuation character](@) is an [ASCII
  256. punctuation character] or anything in
  257. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  258. ## Tabs
  259. Tabs in lines are not expanded to [spaces]. However,
  260. in contexts where whitespace helps to define block structure,
  261. tabs behave as if they were replaced by spaces with a tab stop
  262. of 4 characters.
  263. Thus, for example, a tab can be used instead of four spaces
  264. in an indented code block. (Note, however, that internal
  265. tabs are passed through as literal tabs, not expanded to
  266. spaces.)
  267. ```````````````````````````````` example
  268. →foo→baz→→bim
  269. .
  270. <pre><code>foo→baz→→bim
  271. </code></pre>
  272. ````````````````````````````````
  273. ```````````````````````````````` example
  274. →foo→baz→→bim
  275. .
  276. <pre><code>foo→baz→→bim
  277. </code></pre>
  278. ````````````````````````````````
  279. ```````````````````````````````` example
  280. a→a
  281. ὐ→a
  282. .
  283. <pre><code>a→a
  284. ὐ→a
  285. </code></pre>
  286. ````````````````````````````````
  287. In the following example, a continuation paragraph of a list
  288. item is indented with a tab; this has exactly the same effect
  289. as indentation with four spaces would:
  290. ```````````````````````````````` example
  291. - foo
  292. →bar
  293. .
  294. <ul>
  295. <li>
  296. <p>foo</p>
  297. <p>bar</p>
  298. </li>
  299. </ul>
  300. ````````````````````````````````
  301. ```````````````````````````````` example
  302. - foo
  303. →→bar
  304. .
  305. <ul>
  306. <li>
  307. <p>foo</p>
  308. <pre><code> bar
  309. </code></pre>
  310. </li>
  311. </ul>
  312. ````````````````````````````````
  313. Normally the `>` that begins a block quote may be followed
  314. optionally by a space, which is not considered part of the
  315. content. In the following case `>` is followed by a tab,
  316. which is treated as if it were expanded into three spaces.
  317. Since one of these spaces is considered part of the
  318. delimiter, `foo` is considered to be indented six spaces
  319. inside the block quote context, so we get an indented
  320. code block starting with two spaces.
  321. ```````````````````````````````` example
  322. >→→foo
  323. .
  324. <blockquote>
  325. <pre><code> foo
  326. </code></pre>
  327. </blockquote>
  328. ````````````````````````````````
  329. ```````````````````````````````` example
  330. -→→foo
  331. .
  332. <ul>
  333. <li>
  334. <pre><code> foo
  335. </code></pre>
  336. </li>
  337. </ul>
  338. ````````````````````````````````
  339. ```````````````````````````````` example
  340. foo
  341. →bar
  342. .
  343. <pre><code>foo
  344. bar
  345. </code></pre>
  346. ````````````````````````````````
  347. ```````````````````````````````` example
  348. - foo
  349. - bar
  350. → - baz
  351. .
  352. <ul>
  353. <li>foo
  354. <ul>
  355. <li>bar
  356. <ul>
  357. <li>baz</li>
  358. </ul>
  359. </li>
  360. </ul>
  361. </li>
  362. </ul>
  363. ````````````````````````````````
  364. ```````````````````````````````` example
  365. #→Foo
  366. .
  367. <h1>Foo</h1>
  368. ````````````````````````````````
  369. ```````````````````````````````` example
  370. *→*→*→
  371. .
  372. <hr />
  373. ````````````````````````````````
  374. ## Insecure characters
  375. For security reasons, the Unicode character `U+0000` must be replaced
  376. with the REPLACEMENT CHARACTER (`U+FFFD`).
  377. ## Backslash escapes
  378. Any ASCII punctuation character may be backslash-escaped:
  379. ```````````````````````````````` example
  380. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  381. .
  382. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  383. ````````````````````````````````
  384. Backslashes before other characters are treated as literal
  385. backslashes:
  386. ```````````````````````````````` example
  387. \→\A\a\ \3\φ\«
  388. .
  389. <p>\→\A\a\ \3\φ\«</p>
  390. ````````````````````````````````
  391. Escaped characters are treated as regular characters and do
  392. not have their usual Markdown meanings:
  393. ```````````````````````````````` example
  394. \*not emphasized*
  395. \<br/> not a tag
  396. \[not a link](/foo)
  397. \`not code`
  398. 1\. not a list
  399. \* not a list
  400. \# not a heading
  401. \[foo]: /url "not a reference"
  402. \&ouml; not a character entity
  403. .
  404. <p>*not emphasized*
  405. &lt;br/&gt; not a tag
  406. [not a link](/foo)
  407. `not code`
  408. 1. not a list
  409. * not a list
  410. # not a heading
  411. [foo]: /url &quot;not a reference&quot;
  412. &amp;ouml; not a character entity</p>
  413. ````````````````````````````````
  414. If a backslash is itself escaped, the following character is not:
  415. ```````````````````````````````` example
  416. \\*emphasis*
  417. .
  418. <p>\<em>emphasis</em></p>
  419. ````````````````````````````````
  420. A backslash at the end of the line is a [hard line break]:
  421. ```````````````````````````````` example
  422. foo\
  423. bar
  424. .
  425. <p>foo<br />
  426. bar</p>
  427. ````````````````````````````````
  428. Backslash escapes do not work in code blocks, code spans, autolinks, or
  429. raw HTML:
  430. ```````````````````````````````` example
  431. `` \[\` ``
  432. .
  433. <p><code>\[\`</code></p>
  434. ````````````````````````````````
  435. ```````````````````````````````` example
  436. \[\]
  437. .
  438. <pre><code>\[\]
  439. </code></pre>
  440. ````````````````````````````````
  441. ```````````````````````````````` example
  442. ~~~
  443. \[\]
  444. ~~~
  445. .
  446. <pre><code>\[\]
  447. </code></pre>
  448. ````````````````````````````````
  449. ```````````````````````````````` example
  450. <http://example.com?find=\*>
  451. .
  452. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  453. ````````````````````````````````
  454. ```````````````````````````````` example
  455. <a href="/bar\/)">
  456. .
  457. <a href="/bar\/)">
  458. ````````````````````````````````
  459. But they work in all other contexts, including URLs and link titles,
  460. link references, and [info strings] in [fenced code blocks]:
  461. ```````````````````````````````` example
  462. [foo](/bar\* "ti\*tle")
  463. .
  464. <p><a href="/bar*" title="ti*tle">foo</a></p>
  465. ````````````````````````````````
  466. ```````````````````````````````` example
  467. [foo]
  468. [foo]: /bar\* "ti\*tle"
  469. .
  470. <p><a href="/bar*" title="ti*tle">foo</a></p>
  471. ````````````````````````````````
  472. ```````````````````````````````` example
  473. ``` foo\+bar
  474. foo
  475. ```
  476. .
  477. <pre><code class="language-foo+bar">foo
  478. </code></pre>
  479. ````````````````````````````````
  480. ## Entity and numeric character references
  481. Valid HTML entity references and numeric character references
  482. can be used in place of the corresponding Unicode character,
  483. with the following exceptions:
  484. - Entity and character references are not recognized in code
  485. blocks and code spans.
  486. - Entity and character references cannot stand in place of
  487. special characters that define structural elements in
  488. CommonMark. For example, although `&#42;` can be used
  489. in place of a literal `*` character, `&#42;` cannot replace
  490. `*` in emphasis delimiters, bullet list markers, or thematic
  491. breaks.
  492. Conforming CommonMark parsers need not store information about
  493. whether a particular character was represented in the source
  494. using a Unicode character or an entity reference.
  495. [Entity references](@) consist of `&` + any of the valid
  496. HTML5 entity names + `;`. The
  497. document <https://html.spec.whatwg.org/multipage/entities.json>
  498. is used as an authoritative source for the valid entity
  499. references and their corresponding code points.
  500. ```````````````````````````````` example
  501. &nbsp; &amp; &copy; &AElig; &Dcaron;
  502. &frac34; &HilbertSpace; &DifferentialD;
  503. &ClockwiseContourIntegral; &ngE;
  504. .
  505. <p>  &amp; © Æ Ď
  506. ¾ ℋ ⅆ
  507. ∲ ≧̸</p>
  508. ````````````````````````````````
  509. [Decimal numeric character
  510. references](@)
  511. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  512. numeric character reference is parsed as the corresponding
  513. Unicode character. Invalid Unicode code points will be replaced by
  514. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  515. the code point `U+0000` will also be replaced by `U+FFFD`.
  516. ```````````````````````````````` example
  517. &#35; &#1234; &#992; &#0;
  518. .
  519. <p># Ӓ Ϡ �</p>
  520. ````````````````````````````````
  521. [Hexadecimal numeric character
  522. references](@) consist of `&#` +
  523. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  524. They too are parsed as the corresponding Unicode character (this
  525. time specified with a hexadecimal numeral instead of decimal).
  526. ```````````````````````````````` example
  527. &#X22; &#XD06; &#xcab;
  528. .
  529. <p>&quot; ആ ಫ</p>
  530. ````````````````````````````````
  531. Here are some nonentities:
  532. ```````````````````````````````` example
  533. &nbsp &x; &#; &#x;
  534. &#87654321;
  535. &#abcdef0;
  536. &ThisIsNotDefined; &hi?;
  537. .
  538. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  539. &amp;#87654321;
  540. &amp;#abcdef0;
  541. &amp;ThisIsNotDefined; &amp;hi?;</p>
  542. ````````````````````````````````
  543. Although HTML5 does accept some entity references
  544. without a trailing semicolon (such as `&copy`), these are not
  545. recognized here, because it makes the grammar too ambiguous:
  546. ```````````````````````````````` example
  547. &copy
  548. .
  549. <p>&amp;copy</p>
  550. ````````````````````````````````
  551. Strings that are not on the list of HTML5 named entities are not
  552. recognized as entity references either:
  553. ```````````````````````````````` example
  554. &MadeUpEntity;
  555. .
  556. <p>&amp;MadeUpEntity;</p>
  557. ````````````````````````````````
  558. Entity and numeric character references are recognized in any
  559. context besides code spans or code blocks, including
  560. URLs, [link titles], and [fenced code block][] [info strings]:
  561. ```````````````````````````````` example
  562. <a href="&ouml;&ouml;.html">
  563. .
  564. <a href="&ouml;&ouml;.html">
  565. ````````````````````````````````
  566. ```````````````````````````````` example
  567. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  568. .
  569. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  570. ````````````````````````````````
  571. ```````````````````````````````` example
  572. [foo]
  573. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  574. .
  575. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  576. ````````````````````````````````
  577. ```````````````````````````````` example
  578. ``` f&ouml;&ouml;
  579. foo
  580. ```
  581. .
  582. <pre><code class="language-föö">foo
  583. </code></pre>
  584. ````````````````````````````````
  585. Entity and numeric character references are treated as literal
  586. text in code spans and code blocks:
  587. ```````````````````````````````` example
  588. `f&ouml;&ouml;`
  589. .
  590. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  591. ````````````````````````````````
  592. ```````````````````````````````` example
  593. f&ouml;f&ouml;
  594. .
  595. <pre><code>f&amp;ouml;f&amp;ouml;
  596. </code></pre>
  597. ````````````````````````````````
  598. Entity and numeric character references cannot be used
  599. in place of symbols indicating structure in CommonMark
  600. documents.
  601. ```````````````````````````````` example
  602. &#42;foo&#42;
  603. *foo*
  604. .
  605. <p>*foo*
  606. <em>foo</em></p>
  607. ````````````````````````````````
  608. ```````````````````````````````` example
  609. &#42; foo
  610. * foo
  611. .
  612. <p>* foo</p>
  613. <ul>
  614. <li>foo</li>
  615. </ul>
  616. ````````````````````````````````
  617. ```````````````````````````````` example
  618. foo&#10;&#10;bar
  619. .
  620. <p>foo
  621. bar</p>
  622. ````````````````````````````````
  623. ```````````````````````````````` example
  624. &#9;foo
  625. .
  626. <p>→foo</p>
  627. ````````````````````````````````
  628. ```````````````````````````````` example
  629. [a](url &quot;tit&quot;)
  630. .
  631. <p>[a](url &quot;tit&quot;)</p>
  632. ````````````````````````````````
  633. # Blocks and inlines
  634. We can think of a document as a sequence of
  635. [blocks](@)---structural elements like paragraphs, block
  636. quotations, lists, headings, rules, and code blocks. Some blocks (like
  637. block quotes and list items) contain other blocks; others (like
  638. headings and paragraphs) contain [inline](@) content---text,
  639. links, emphasized text, images, code spans, and so on.
  640. ## Precedence
  641. Indicators of block structure always take precedence over indicators
  642. of inline structure. So, for example, the following is a list with
  643. two items, not a list with one item containing a code span:
  644. ```````````````````````````````` example
  645. - `one
  646. - two`
  647. .
  648. <ul>
  649. <li>`one</li>
  650. <li>two`</li>
  651. </ul>
  652. ````````````````````````````````
  653. This means that parsing can proceed in two steps: first, the block
  654. structure of the document can be discerned; second, text lines inside
  655. paragraphs, headings, and other block constructs can be parsed for inline
  656. structure. The second step requires information about link reference
  657. definitions that will be available only at the end of the first
  658. step. Note that the first step requires processing lines in sequence,
  659. but the second can be parallelized, since the inline parsing of
  660. one block element does not affect the inline parsing of any other.
  661. ## Container blocks and leaf blocks
  662. We can divide blocks into two types:
  663. [container blocks](@),
  664. which can contain other blocks, and [leaf blocks](@),
  665. which cannot.
  666. # Leaf blocks
  667. This section describes the different kinds of leaf block that make up a
  668. Markdown document.
  669. ## Thematic breaks
  670. A line consisting of 0-3 spaces of indentation, followed by a sequence
  671. of three or more matching `-`, `_`, or `*` characters, each followed
  672. optionally by any number of spaces or tabs, forms a
  673. [thematic break](@).
  674. ```````````````````````````````` example
  675. ***
  676. ---
  677. ___
  678. .
  679. <hr />
  680. <hr />
  681. <hr />
  682. ````````````````````````````````
  683. Wrong characters:
  684. ```````````````````````````````` example
  685. +++
  686. .
  687. <p>+++</p>
  688. ````````````````````````````````
  689. ```````````````````````````````` example
  690. ===
  691. .
  692. <p>===</p>
  693. ````````````````````````````````
  694. Not enough characters:
  695. ```````````````````````````````` example
  696. --
  697. **
  698. __
  699. .
  700. <p>--
  701. **
  702. __</p>
  703. ````````````````````````````````
  704. One to three spaces indent are allowed:
  705. ```````````````````````````````` example
  706. ***
  707. ***
  708. ***
  709. .
  710. <hr />
  711. <hr />
  712. <hr />
  713. ````````````````````````````````
  714. Four spaces is too many:
  715. ```````````````````````````````` example
  716. ***
  717. .
  718. <pre><code>***
  719. </code></pre>
  720. ````````````````````````````````
  721. ```````````````````````````````` example
  722. Foo
  723. ***
  724. .
  725. <p>Foo
  726. ***</p>
  727. ````````````````````````````````
  728. More than three characters may be used:
  729. ```````````````````````````````` example
  730. _____________________________________
  731. .
  732. <hr />
  733. ````````````````````````````````
  734. Spaces are allowed between the characters:
  735. ```````````````````````````````` example
  736. - - -
  737. .
  738. <hr />
  739. ````````````````````````````````
  740. ```````````````````````````````` example
  741. ** * ** * ** * **
  742. .
  743. <hr />
  744. ````````````````````````````````
  745. ```````````````````````````````` example
  746. - - - -
  747. .
  748. <hr />
  749. ````````````````````````````````
  750. Spaces are allowed at the end:
  751. ```````````````````````````````` example
  752. - - - -
  753. .
  754. <hr />
  755. ````````````````````````````````
  756. However, no other characters may occur in the line:
  757. ```````````````````````````````` example
  758. _ _ _ _ a
  759. a------
  760. ---a---
  761. .
  762. <p>_ _ _ _ a</p>
  763. <p>a------</p>
  764. <p>---a---</p>
  765. ````````````````````````````````
  766. It is required that all of the [non-whitespace characters] be the same.
  767. So, this is not a thematic break:
  768. ```````````````````````````````` example
  769. *-*
  770. .
  771. <p><em>-</em></p>
  772. ````````````````````````````````
  773. Thematic breaks do not need blank lines before or after:
  774. ```````````````````````````````` example
  775. - foo
  776. ***
  777. - bar
  778. .
  779. <ul>
  780. <li>foo</li>
  781. </ul>
  782. <hr />
  783. <ul>
  784. <li>bar</li>
  785. </ul>
  786. ````````````````````````````````
  787. Thematic breaks can interrupt a paragraph:
  788. ```````````````````````````````` example
  789. Foo
  790. ***
  791. bar
  792. .
  793. <p>Foo</p>
  794. <hr />
  795. <p>bar</p>
  796. ````````````````````````````````
  797. If a line of dashes that meets the above conditions for being a
  798. thematic break could also be interpreted as the underline of a [setext
  799. heading], the interpretation as a
  800. [setext heading] takes precedence. Thus, for example,
  801. this is a setext heading, not a paragraph followed by a thematic break:
  802. ```````````````````````````````` example
  803. Foo
  804. ---
  805. bar
  806. .
  807. <h2>Foo</h2>
  808. <p>bar</p>
  809. ````````````````````````````````
  810. When both a thematic break and a list item are possible
  811. interpretations of a line, the thematic break takes precedence:
  812. ```````````````````````````````` example
  813. * Foo
  814. * * *
  815. * Bar
  816. .
  817. <ul>
  818. <li>Foo</li>
  819. </ul>
  820. <hr />
  821. <ul>
  822. <li>Bar</li>
  823. </ul>
  824. ````````````````````````````````
  825. If you want a thematic break in a list item, use a different bullet:
  826. ```````````````````````````````` example
  827. - Foo
  828. - * * *
  829. .
  830. <ul>
  831. <li>Foo</li>
  832. <li>
  833. <hr />
  834. </li>
  835. </ul>
  836. ````````````````````````````````
  837. ## ATX headings
  838. An [ATX heading](@)
  839. consists of a string of characters, parsed as inline content, between an
  840. opening sequence of 1--6 unescaped `#` characters and an optional
  841. closing sequence of any number of unescaped `#` characters.
  842. The opening sequence of `#` characters must be followed by a
  843. [space] or by the end of line. The optional closing sequence of `#`s must be
  844. preceded by a [space] and may be followed by spaces only. The opening
  845. `#` character may be indented 0-3 spaces. The raw contents of the
  846. heading are stripped of leading and trailing spaces before being parsed
  847. as inline content. The heading level is equal to the number of `#`
  848. characters in the opening sequence.
  849. Simple headings:
  850. ```````````````````````````````` example
  851. # foo
  852. ## foo
  853. ### foo
  854. #### foo
  855. ##### foo
  856. ###### foo
  857. .
  858. <h1>foo</h1>
  859. <h2>foo</h2>
  860. <h3>foo</h3>
  861. <h4>foo</h4>
  862. <h5>foo</h5>
  863. <h6>foo</h6>
  864. ````````````````````````````````
  865. More than six `#` characters is not a heading:
  866. ```````````````````````````````` example
  867. ####### foo
  868. .
  869. <p>####### foo</p>
  870. ````````````````````````````````
  871. At least one space is required between the `#` characters and the
  872. heading's contents, unless the heading is empty. Note that many
  873. implementations currently do not require the space. However, the
  874. space was required by the
  875. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  876. and it helps prevent things like the following from being parsed as
  877. headings:
  878. ```````````````````````````````` example
  879. #5 bolt
  880. #hashtag
  881. .
  882. <p>#5 bolt</p>
  883. <p>#hashtag</p>
  884. ````````````````````````````````
  885. This is not a heading, because the first `#` is escaped:
  886. ```````````````````````````````` example
  887. \## foo
  888. .
  889. <p>## foo</p>
  890. ````````````````````````````````
  891. Contents are parsed as inlines:
  892. ```````````````````````````````` example
  893. # foo *bar* \*baz\*
  894. .
  895. <h1>foo <em>bar</em> *baz*</h1>
  896. ````````````````````````````````
  897. Leading and trailing [whitespace] is ignored in parsing inline content:
  898. ```````````````````````````````` example
  899. # foo
  900. .
  901. <h1>foo</h1>
  902. ````````````````````````````````
  903. One to three spaces indentation are allowed:
  904. ```````````````````````````````` example
  905. ### foo
  906. ## foo
  907. # foo
  908. .
  909. <h3>foo</h3>
  910. <h2>foo</h2>
  911. <h1>foo</h1>
  912. ````````````````````````````````
  913. Four spaces are too much:
  914. ```````````````````````````````` example
  915. # foo
  916. .
  917. <pre><code># foo
  918. </code></pre>
  919. ````````````````````````````````
  920. ```````````````````````````````` example
  921. foo
  922. # bar
  923. .
  924. <p>foo
  925. # bar</p>
  926. ````````````````````````````````
  927. A closing sequence of `#` characters is optional:
  928. ```````````````````````````````` example
  929. ## foo ##
  930. ### bar ###
  931. .
  932. <h2>foo</h2>
  933. <h3>bar</h3>
  934. ````````````````````````````````
  935. It need not be the same length as the opening sequence:
  936. ```````````````````````````````` example
  937. # foo ##################################
  938. ##### foo ##
  939. .
  940. <h1>foo</h1>
  941. <h5>foo</h5>
  942. ````````````````````````````````
  943. Spaces are allowed after the closing sequence:
  944. ```````````````````````````````` example
  945. ### foo ###
  946. .
  947. <h3>foo</h3>
  948. ````````````````````````````````
  949. A sequence of `#` characters with anything but [spaces] following it
  950. is not a closing sequence, but counts as part of the contents of the
  951. heading:
  952. ```````````````````````````````` example
  953. ### foo ### b
  954. .
  955. <h3>foo ### b</h3>
  956. ````````````````````````````````
  957. The closing sequence must be preceded by a space:
  958. ```````````````````````````````` example
  959. # foo#
  960. .
  961. <h1>foo#</h1>
  962. ````````````````````````````````
  963. Backslash-escaped `#` characters do not count as part
  964. of the closing sequence:
  965. ```````````````````````````````` example
  966. ### foo \###
  967. ## foo #\##
  968. # foo \#
  969. .
  970. <h3>foo ###</h3>
  971. <h2>foo ###</h2>
  972. <h1>foo #</h1>
  973. ````````````````````````````````
  974. ATX headings need not be separated from surrounding content by blank
  975. lines, and they can interrupt paragraphs:
  976. ```````````````````````````````` example
  977. ****
  978. ## foo
  979. ****
  980. .
  981. <hr />
  982. <h2>foo</h2>
  983. <hr />
  984. ````````````````````````````````
  985. ```````````````````````````````` example
  986. Foo bar
  987. # baz
  988. Bar foo
  989. .
  990. <p>Foo bar</p>
  991. <h1>baz</h1>
  992. <p>Bar foo</p>
  993. ````````````````````````````````
  994. ATX headings can be empty:
  995. ```````````````````````````````` example
  996. ##
  997. #
  998. ### ###
  999. .
  1000. <h2></h2>
  1001. <h1></h1>
  1002. <h3></h3>
  1003. ````````````````````````````````
  1004. ## Setext headings
  1005. A [setext heading](@) consists of one or more
  1006. lines of text, each containing at least one [non-whitespace
  1007. character], with no more than 3 spaces indentation, followed by
  1008. a [setext heading underline]. The lines of text must be such
  1009. that, were they not followed by the setext heading underline,
  1010. they would be interpreted as a paragraph: they cannot be
  1011. interpretable as a [code fence], [ATX heading][ATX headings],
  1012. [block quote][block quotes], [thematic break][thematic breaks],
  1013. [list item][list items], or [HTML block][HTML blocks].
  1014. A [setext heading underline](@) is a sequence of
  1015. `=` characters or a sequence of `-` characters, with no more than 3
  1016. spaces indentation and any number of trailing spaces. If a line
  1017. containing a single `-` can be interpreted as an
  1018. empty [list items], it should be interpreted this way
  1019. and not as a [setext heading underline].
  1020. The heading is a level 1 heading if `=` characters are used in
  1021. the [setext heading underline], and a level 2 heading if `-`
  1022. characters are used. The contents of the heading are the result
  1023. of parsing the preceding lines of text as CommonMark inline
  1024. content.
  1025. In general, a setext heading need not be preceded or followed by a
  1026. blank line. However, it cannot interrupt a paragraph, so when a
  1027. setext heading comes after a paragraph, a blank line is needed between
  1028. them.
  1029. Simple examples:
  1030. ```````````````````````````````` example
  1031. Foo *bar*
  1032. =========
  1033. Foo *bar*
  1034. ---------
  1035. .
  1036. <h1>Foo <em>bar</em></h1>
  1037. <h2>Foo <em>bar</em></h2>
  1038. ````````````````````````````````
  1039. The content of the header may span more than one line:
  1040. ```````````````````````````````` example
  1041. Foo *bar
  1042. baz*
  1043. ====
  1044. .
  1045. <h1>Foo <em>bar
  1046. baz</em></h1>
  1047. ````````````````````````````````
  1048. The contents are the result of parsing the headings's raw
  1049. content as inlines. The heading's raw content is formed by
  1050. concatenating the lines and removing initial and final
  1051. [whitespace].
  1052. ```````````````````````````````` example
  1053. Foo *bar
  1054. baz*→
  1055. ====
  1056. .
  1057. <h1>Foo <em>bar
  1058. baz</em></h1>
  1059. ````````````````````````````````
  1060. The underlining can be any length:
  1061. ```````````````````````````````` example
  1062. Foo
  1063. -------------------------
  1064. Foo
  1065. =
  1066. .
  1067. <h2>Foo</h2>
  1068. <h1>Foo</h1>
  1069. ````````````````````````````````
  1070. The heading content can be indented up to three spaces, and need
  1071. not line up with the underlining:
  1072. ```````````````````````````````` example
  1073. Foo
  1074. ---
  1075. Foo
  1076. -----
  1077. Foo
  1078. ===
  1079. .
  1080. <h2>Foo</h2>
  1081. <h2>Foo</h2>
  1082. <h1>Foo</h1>
  1083. ````````````````````````````````
  1084. Four spaces indent is too much:
  1085. ```````````````````````````````` example
  1086. Foo
  1087. ---
  1088. Foo
  1089. ---
  1090. .
  1091. <pre><code>Foo
  1092. ---
  1093. Foo
  1094. </code></pre>
  1095. <hr />
  1096. ````````````````````````````````
  1097. The setext heading underline can be indented up to three spaces, and
  1098. may have trailing spaces:
  1099. ```````````````````````````````` example
  1100. Foo
  1101. ----
  1102. .
  1103. <h2>Foo</h2>
  1104. ````````````````````````````````
  1105. Four spaces is too much:
  1106. ```````````````````````````````` example
  1107. Foo
  1108. ---
  1109. .
  1110. <p>Foo
  1111. ---</p>
  1112. ````````````````````````````````
  1113. The setext heading underline cannot contain internal spaces:
  1114. ```````````````````````````````` example
  1115. Foo
  1116. = =
  1117. Foo
  1118. --- -
  1119. .
  1120. <p>Foo
  1121. = =</p>
  1122. <p>Foo</p>
  1123. <hr />
  1124. ````````````````````````````````
  1125. Trailing spaces in the content line do not cause a line break:
  1126. ```````````````````````````````` example
  1127. Foo
  1128. -----
  1129. .
  1130. <h2>Foo</h2>
  1131. ````````````````````````````````
  1132. Nor does a backslash at the end:
  1133. ```````````````````````````````` example
  1134. Foo\
  1135. ----
  1136. .
  1137. <h2>Foo\</h2>
  1138. ````````````````````````````````
  1139. Since indicators of block structure take precedence over
  1140. indicators of inline structure, the following are setext headings:
  1141. ```````````````````````````````` example
  1142. `Foo
  1143. ----
  1144. `
  1145. <a title="a lot
  1146. ---
  1147. of dashes"/>
  1148. .
  1149. <h2>`Foo</h2>
  1150. <p>`</p>
  1151. <h2>&lt;a title=&quot;a lot</h2>
  1152. <p>of dashes&quot;/&gt;</p>
  1153. ````````````````````````````````
  1154. The setext heading underline cannot be a [lazy continuation
  1155. line] in a list item or block quote:
  1156. ```````````````````````````````` example
  1157. > Foo
  1158. ---
  1159. .
  1160. <blockquote>
  1161. <p>Foo</p>
  1162. </blockquote>
  1163. <hr />
  1164. ````````````````````````````````
  1165. ```````````````````````````````` example
  1166. > foo
  1167. bar
  1168. ===
  1169. .
  1170. <blockquote>
  1171. <p>foo
  1172. bar
  1173. ===</p>
  1174. </blockquote>
  1175. ````````````````````````````````
  1176. ```````````````````````````````` example
  1177. - Foo
  1178. ---
  1179. .
  1180. <ul>
  1181. <li>Foo</li>
  1182. </ul>
  1183. <hr />
  1184. ````````````````````````````````
  1185. A blank line is needed between a paragraph and a following
  1186. setext heading, since otherwise the paragraph becomes part
  1187. of the heading's content:
  1188. ```````````````````````````````` example
  1189. Foo
  1190. Bar
  1191. ---
  1192. .
  1193. <h2>Foo
  1194. Bar</h2>
  1195. ````````````````````````````````
  1196. But in general a blank line is not required before or after
  1197. setext headings:
  1198. ```````````````````````````````` example
  1199. ---
  1200. Foo
  1201. ---
  1202. Bar
  1203. ---
  1204. Baz
  1205. .
  1206. <hr />
  1207. <h2>Foo</h2>
  1208. <h2>Bar</h2>
  1209. <p>Baz</p>
  1210. ````````````````````````````````
  1211. Setext headings cannot be empty:
  1212. ```````````````````````````````` example
  1213. ====
  1214. .
  1215. <p>====</p>
  1216. ````````````````````````````````
  1217. Setext heading text lines must not be interpretable as block
  1218. constructs other than paragraphs. So, the line of dashes
  1219. in these examples gets interpreted as a thematic break:
  1220. ```````````````````````````````` example
  1221. ---
  1222. ---
  1223. .
  1224. <hr />
  1225. <hr />
  1226. ````````````````````````````````
  1227. ```````````````````````````````` example
  1228. - foo
  1229. -----
  1230. .
  1231. <ul>
  1232. <li>foo</li>
  1233. </ul>
  1234. <hr />
  1235. ````````````````````````````````
  1236. ```````````````````````````````` example
  1237. foo
  1238. ---
  1239. .
  1240. <pre><code>foo
  1241. </code></pre>
  1242. <hr />
  1243. ````````````````````````````````
  1244. ```````````````````````````````` example
  1245. > foo
  1246. -----
  1247. .
  1248. <blockquote>
  1249. <p>foo</p>
  1250. </blockquote>
  1251. <hr />
  1252. ````````````````````````````````
  1253. If you want a heading with `> foo` as its literal text, you can
  1254. use backslash escapes:
  1255. ```````````````````````````````` example
  1256. \> foo
  1257. ------
  1258. .
  1259. <h2>&gt; foo</h2>
  1260. ````````````````````````````````
  1261. **Compatibility note:** Most existing Markdown implementations
  1262. do not allow the text of setext headings to span multiple lines.
  1263. But there is no consensus about how to interpret
  1264. ``` markdown
  1265. Foo
  1266. bar
  1267. ---
  1268. baz
  1269. ```
  1270. One can find four different interpretations:
  1271. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1272. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1273. 3. paragraph "Foo bar --- baz"
  1274. 4. heading "Foo bar", paragraph "baz"
  1275. We find interpretation 4 most natural, and interpretation 4
  1276. increases the expressive power of CommonMark, by allowing
  1277. multiline headings. Authors who want interpretation 1 can
  1278. put a blank line after the first paragraph:
  1279. ```````````````````````````````` example
  1280. Foo
  1281. bar
  1282. ---
  1283. baz
  1284. .
  1285. <p>Foo</p>
  1286. <h2>bar</h2>
  1287. <p>baz</p>
  1288. ````````````````````````````````
  1289. Authors who want interpretation 2 can put blank lines around
  1290. the thematic break,
  1291. ```````````````````````````````` example
  1292. Foo
  1293. bar
  1294. ---
  1295. baz
  1296. .
  1297. <p>Foo
  1298. bar</p>
  1299. <hr />
  1300. <p>baz</p>
  1301. ````````````````````````````````
  1302. or use a thematic break that cannot count as a [setext heading
  1303. underline], such as
  1304. ```````````````````````````````` example
  1305. Foo
  1306. bar
  1307. * * *
  1308. baz
  1309. .
  1310. <p>Foo
  1311. bar</p>
  1312. <hr />
  1313. <p>baz</p>
  1314. ````````````````````````````````
  1315. Authors who want interpretation 3 can use backslash escapes:
  1316. ```````````````````````````````` example
  1317. Foo
  1318. bar
  1319. \---
  1320. baz
  1321. .
  1322. <p>Foo
  1323. bar
  1324. ---
  1325. baz</p>
  1326. ````````````````````````````````
  1327. ## Indented code blocks
  1328. An [indented code block](@) is composed of one or more
  1329. [indented chunks] separated by blank lines.
  1330. An [indented chunk](@) is a sequence of non-blank lines,
  1331. each indented four or more spaces. The contents of the code block are
  1332. the literal contents of the lines, including trailing
  1333. [line endings], minus four spaces of indentation.
  1334. An indented code block has no [info string].
  1335. An indented code block cannot interrupt a paragraph, so there must be
  1336. a blank line between a paragraph and a following indented code block.
  1337. (A blank line is not needed, however, between a code block and a following
  1338. paragraph.)
  1339. ```````````````````````````````` example
  1340. a simple
  1341. indented code block
  1342. .
  1343. <pre><code>a simple
  1344. indented code block
  1345. </code></pre>
  1346. ````````````````````````````````
  1347. If there is any ambiguity between an interpretation of indentation
  1348. as a code block and as indicating that material belongs to a [list
  1349. item][list items], the list item interpretation takes precedence:
  1350. ```````````````````````````````` example
  1351. - foo
  1352. bar
  1353. .
  1354. <ul>
  1355. <li>
  1356. <p>foo</p>
  1357. <p>bar</p>
  1358. </li>
  1359. </ul>
  1360. ````````````````````````````````
  1361. ```````````````````````````````` example
  1362. 1. foo
  1363. - bar
  1364. .
  1365. <ol>
  1366. <li>
  1367. <p>foo</p>
  1368. <ul>
  1369. <li>bar</li>
  1370. </ul>
  1371. </li>
  1372. </ol>
  1373. ````````````````````````````````
  1374. The contents of a code block are literal text, and do not get parsed
  1375. as Markdown:
  1376. ```````````````````````````````` example
  1377. <a/>
  1378. *hi*
  1379. - one
  1380. .
  1381. <pre><code>&lt;a/&gt;
  1382. *hi*
  1383. - one
  1384. </code></pre>
  1385. ````````````````````````````````
  1386. Here we have three chunks separated by blank lines:
  1387. ```````````````````````````````` example
  1388. chunk1
  1389. chunk2
  1390. chunk3
  1391. .
  1392. <pre><code>chunk1
  1393. chunk2
  1394. chunk3
  1395. </code></pre>
  1396. ````````````````````````````````
  1397. Any initial spaces beyond four will be included in the content, even
  1398. in interior blank lines:
  1399. ```````````````````````````````` example
  1400. chunk1
  1401. chunk2
  1402. .
  1403. <pre><code>chunk1
  1404. chunk2
  1405. </code></pre>
  1406. ````````````````````````````````
  1407. An indented code block cannot interrupt a paragraph. (This
  1408. allows hanging indents and the like.)
  1409. ```````````````````````````````` example
  1410. Foo
  1411. bar
  1412. .
  1413. <p>Foo
  1414. bar</p>
  1415. ````````````````````````````````
  1416. However, any non-blank line with fewer than four leading spaces ends
  1417. the code block immediately. So a paragraph may occur immediately
  1418. after indented code:
  1419. ```````````````````````````````` example
  1420. foo
  1421. bar
  1422. .
  1423. <pre><code>foo
  1424. </code></pre>
  1425. <p>bar</p>
  1426. ````````````````````````````````
  1427. And indented code can occur immediately before and after other kinds of
  1428. blocks:
  1429. ```````````````````````````````` example
  1430. # Heading
  1431. foo
  1432. Heading
  1433. ------
  1434. foo
  1435. ----
  1436. .
  1437. <h1>Heading</h1>
  1438. <pre><code>foo
  1439. </code></pre>
  1440. <h2>Heading</h2>
  1441. <pre><code>foo
  1442. </code></pre>
  1443. <hr />
  1444. ````````````````````````````````
  1445. The first line can be indented more than four spaces:
  1446. ```````````````````````````````` example
  1447. foo
  1448. bar
  1449. .
  1450. <pre><code> foo
  1451. bar
  1452. </code></pre>
  1453. ````````````````````````````````
  1454. Blank lines preceding or following an indented code block
  1455. are not included in it:
  1456. ```````````````````````````````` example
  1457. foo
  1458. .
  1459. <pre><code>foo
  1460. </code></pre>
  1461. ````````````````````````````````
  1462. Trailing spaces are included in the code block's content:
  1463. ```````````````````````````````` example
  1464. foo
  1465. .
  1466. <pre><code>foo
  1467. </code></pre>
  1468. ````````````````````````````````
  1469. ## Fenced code blocks
  1470. A [code fence](@) is a sequence
  1471. of at least three consecutive backtick characters (`` ` ``) or
  1472. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1473. A [fenced code block](@)
  1474. begins with a code fence, indented no more than three spaces.
  1475. The line with the opening code fence may optionally contain some text
  1476. following the code fence; this is trimmed of leading and trailing
  1477. whitespace and called the [info string](@). If the [info string] comes
  1478. after a backtick fence, it may not contain any backtick
  1479. characters. (The reason for this restriction is that otherwise
  1480. some inline code would be incorrectly interpreted as the
  1481. beginning of a fenced code block.)
  1482. The content of the code block consists of all subsequent lines, until
  1483. a closing [code fence] of the same type as the code block
  1484. began with (backticks or tildes), and with at least as many backticks
  1485. or tildes as the opening code fence. If the leading code fence is
  1486. indented N spaces, then up to N spaces of indentation are removed from
  1487. each line of the content (if present). (If a content line is not
  1488. indented, it is preserved unchanged. If it is indented less than N
  1489. spaces, all of the indentation is removed.)
  1490. The closing code fence may be indented up to three spaces, and may be
  1491. followed only by spaces, which are ignored. If the end of the
  1492. containing block (or document) is reached and no closing code fence
  1493. has been found, the code block contains all of the lines after the
  1494. opening code fence until the end of the containing block (or
  1495. document). (An alternative spec would require backtracking in the
  1496. event that a closing code fence is not found. But this makes parsing
  1497. much less efficient, and there seems to be no real down side to the
  1498. behavior described here.)
  1499. A fenced code block may interrupt a paragraph, and does not require
  1500. a blank line either before or after.
  1501. The content of a code fence is treated as literal text, not parsed
  1502. as inlines. The first word of the [info string] is typically used to
  1503. specify the language of the code sample, and rendered in the `class`
  1504. attribute of the `code` tag. However, this spec does not mandate any
  1505. particular treatment of the [info string].
  1506. Here is a simple example with backticks:
  1507. ```````````````````````````````` example
  1508. ```
  1509. <
  1510. >
  1511. ```
  1512. .
  1513. <pre><code>&lt;
  1514. &gt;
  1515. </code></pre>
  1516. ````````````````````````````````
  1517. With tildes:
  1518. ```````````````````````````````` example
  1519. ~~~
  1520. <
  1521. >
  1522. ~~~
  1523. .
  1524. <pre><code>&lt;
  1525. &gt;
  1526. </code></pre>
  1527. ````````````````````````````````
  1528. Fewer than three backticks is not enough:
  1529. ```````````````````````````````` example
  1530. ``
  1531. foo
  1532. ``
  1533. .
  1534. <p><code>foo</code></p>
  1535. ````````````````````````````````
  1536. The closing code fence must use the same character as the opening
  1537. fence:
  1538. ```````````````````````````````` example
  1539. ```
  1540. aaa
  1541. ~~~
  1542. ```
  1543. .
  1544. <pre><code>aaa
  1545. ~~~
  1546. </code></pre>
  1547. ````````````````````````````````
  1548. ```````````````````````````````` example
  1549. ~~~
  1550. aaa
  1551. ```
  1552. ~~~
  1553. .
  1554. <pre><code>aaa
  1555. ```
  1556. </code></pre>
  1557. ````````````````````````````````
  1558. The closing code fence must be at least as long as the opening fence:
  1559. ```````````````````````````````` example
  1560. ````
  1561. aaa
  1562. ```
  1563. ``````
  1564. .
  1565. <pre><code>aaa
  1566. ```
  1567. </code></pre>
  1568. ````````````````````````````````
  1569. ```````````````````````````````` example
  1570. ~~~~
  1571. aaa
  1572. ~~~
  1573. ~~~~
  1574. .
  1575. <pre><code>aaa
  1576. ~~~
  1577. </code></pre>
  1578. ````````````````````````````````
  1579. Unclosed code blocks are closed by the end of the document
  1580. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1581. ```````````````````````````````` example
  1582. ```
  1583. .
  1584. <pre><code></code></pre>
  1585. ````````````````````````````````
  1586. ```````````````````````````````` example
  1587. `````
  1588. ```
  1589. aaa
  1590. .
  1591. <pre><code>
  1592. ```
  1593. aaa
  1594. </code></pre>
  1595. ````````````````````````````````
  1596. ```````````````````````````````` example
  1597. > ```
  1598. > aaa
  1599. bbb
  1600. .
  1601. <blockquote>
  1602. <pre><code>aaa
  1603. </code></pre>
  1604. </blockquote>
  1605. <p>bbb</p>
  1606. ````````````````````````````````
  1607. A code block can have all empty lines as its content:
  1608. ```````````````````````````````` example
  1609. ```
  1610. ```
  1611. .
  1612. <pre><code>
  1613. </code></pre>
  1614. ````````````````````````````````
  1615. A code block can be empty:
  1616. ```````````````````````````````` example
  1617. ```
  1618. ```
  1619. .
  1620. <pre><code></code></pre>
  1621. ````````````````````````````````
  1622. Fences can be indented. If the opening fence is indented,
  1623. content lines will have equivalent opening indentation removed,
  1624. if present:
  1625. ```````````````````````````````` example
  1626. ```
  1627. aaa
  1628. aaa
  1629. ```
  1630. .
  1631. <pre><code>aaa
  1632. aaa
  1633. </code></pre>
  1634. ````````````````````````````````
  1635. ```````````````````````````````` example
  1636. ```
  1637. aaa
  1638. aaa
  1639. aaa
  1640. ```
  1641. .
  1642. <pre><code>aaa
  1643. aaa
  1644. aaa
  1645. </code></pre>
  1646. ````````````````````````````````
  1647. ```````````````````````````````` example
  1648. ```
  1649. aaa
  1650. aaa
  1651. aaa
  1652. ```
  1653. .
  1654. <pre><code>aaa
  1655. aaa
  1656. aaa
  1657. </code></pre>
  1658. ````````````````````````````````
  1659. Four spaces indentation produces an indented code block:
  1660. ```````````````````````````````` example
  1661. ```
  1662. aaa
  1663. ```
  1664. .
  1665. <pre><code>```
  1666. aaa
  1667. ```
  1668. </code></pre>
  1669. ````````````````````````````````
  1670. Closing fences may be indented by 0-3 spaces, and their indentation
  1671. need not match that of the opening fence:
  1672. ```````````````````````````````` example
  1673. ```
  1674. aaa
  1675. ```
  1676. .
  1677. <pre><code>aaa
  1678. </code></pre>
  1679. ````````````````````````````````
  1680. ```````````````````````````````` example
  1681. ```
  1682. aaa
  1683. ```
  1684. .
  1685. <pre><code>aaa
  1686. </code></pre>
  1687. ````````````````````````````````
  1688. This is not a closing fence, because it is indented 4 spaces:
  1689. ```````````````````````````````` example
  1690. ```
  1691. aaa
  1692. ```
  1693. .
  1694. <pre><code>aaa
  1695. ```
  1696. </code></pre>
  1697. ````````````````````````````````
  1698. Code fences (opening and closing) cannot contain internal spaces:
  1699. ```````````````````````````````` example
  1700. ``` ```
  1701. aaa
  1702. .
  1703. <p><code> </code>
  1704. aaa</p>
  1705. ````````````````````````````````
  1706. ```````````````````````````````` example
  1707. ~~~~~~
  1708. aaa
  1709. ~~~ ~~
  1710. .
  1711. <pre><code>aaa
  1712. ~~~ ~~
  1713. </code></pre>
  1714. ````````````````````````````````
  1715. Fenced code blocks can interrupt paragraphs, and can be followed
  1716. directly by paragraphs, without a blank line between:
  1717. ```````````````````````````````` example
  1718. foo
  1719. ```
  1720. bar
  1721. ```
  1722. baz
  1723. .
  1724. <p>foo</p>
  1725. <pre><code>bar
  1726. </code></pre>
  1727. <p>baz</p>
  1728. ````````````````````````````````
  1729. Other blocks can also occur before and after fenced code blocks
  1730. without an intervening blank line:
  1731. ```````````````````````````````` example
  1732. foo
  1733. ---
  1734. ~~~
  1735. bar
  1736. ~~~
  1737. # baz
  1738. .
  1739. <h2>foo</h2>
  1740. <pre><code>bar
  1741. </code></pre>
  1742. <h1>baz</h1>
  1743. ````````````````````````````````
  1744. An [info string] can be provided after the opening code fence.
  1745. Although this spec doesn't mandate any particular treatment of
  1746. the info string, the first word is typically used to specify
  1747. the language of the code block. In HTML output, the language is
  1748. normally indicated by adding a class to the `code` element consisting
  1749. of `language-` followed by the language name.
  1750. ```````````````````````````````` example
  1751. ```ruby
  1752. def foo(x)
  1753. return 3
  1754. end
  1755. ```
  1756. .
  1757. <pre><code class="language-ruby">def foo(x)
  1758. return 3
  1759. end
  1760. </code></pre>
  1761. ````````````````````````````````
  1762. ```````````````````````````````` example
  1763. ~~~~ ruby startline=3 $%@#$
  1764. def foo(x)
  1765. return 3
  1766. end
  1767. ~~~~~~~
  1768. .
  1769. <pre><code class="language-ruby">def foo(x)
  1770. return 3
  1771. end
  1772. </code></pre>
  1773. ````````````````````````````````
  1774. ```````````````````````````````` example
  1775. ````;
  1776. ````
  1777. .
  1778. <pre><code class="language-;"></code></pre>
  1779. ````````````````````````````````
  1780. [Info strings] for backtick code blocks cannot contain backticks:
  1781. ```````````````````````````````` example
  1782. ``` aa ```
  1783. foo
  1784. .
  1785. <p><code>aa</code>
  1786. foo</p>
  1787. ````````````````````````````````
  1788. [Info strings] for tilde code blocks can contain backticks and tildes:
  1789. ```````````````````````````````` example
  1790. ~~~ aa ``` ~~~
  1791. foo
  1792. ~~~
  1793. .
  1794. <pre><code class="language-aa">foo
  1795. </code></pre>
  1796. ````````````````````````````````
  1797. Closing code fences cannot have [info strings]:
  1798. ```````````````````````````````` example
  1799. ```
  1800. ``` aaa
  1801. ```
  1802. .
  1803. <pre><code>``` aaa
  1804. </code></pre>
  1805. ````````````````````````````````
  1806. ## HTML blocks
  1807. An [HTML block](@) is a group of lines that is treated
  1808. as raw HTML (and will not be escaped in HTML output).
  1809. There are seven kinds of [HTML block], which can be defined by their
  1810. start and end conditions. The block begins with a line that meets a
  1811. [start condition](@) (after up to three spaces optional indentation).
  1812. It ends with the first subsequent line that meets a matching [end
  1813. condition](@), or the last line of the document, or the last line of
  1814. the [container block](#container-blocks) containing the current HTML
  1815. block, if no line is encountered that meets the [end condition]. If
  1816. the first line meets both the [start condition] and the [end
  1817. condition], the block will contain just that line.
  1818. 1. **Start condition:** line begins with the string `<script`,
  1819. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1820. the string `>`, or the end of the line.\
  1821. **End condition:** line contains an end tag
  1822. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1823. need not match the start tag).
  1824. 2. **Start condition:** line begins with the string `<!--`.\
  1825. **End condition:** line contains the string `-->`.
  1826. 3. **Start condition:** line begins with the string `<?`.\
  1827. **End condition:** line contains the string `?>`.
  1828. 4. **Start condition:** line begins with the string `<!`
  1829. followed by an uppercase ASCII letter.\
  1830. **End condition:** line contains the character `>`.
  1831. 5. **Start condition:** line begins with the string
  1832. `<![CDATA[`.\
  1833. **End condition:** line contains the string `]]>`.
  1834. 6. **Start condition:** line begins the string `<` or `</`
  1835. followed by one of the strings (case-insensitive) `address`,
  1836. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1837. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1838. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1839. `footer`, `form`, `frame`, `frameset`,
  1840. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1841. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1842. `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1843. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1844. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1845. by [whitespace], the end of the line, the string `>`, or
  1846. the string `/>`.\
  1847. **End condition:** line is followed by a [blank line].
  1848. 7. **Start condition:** line begins with a complete [open tag]
  1849. (with any [tag name] other than `script`,
  1850. `style`, or `pre`) or a complete [closing tag],
  1851. followed only by [whitespace] or the end of the line.\
  1852. **End condition:** line is followed by a [blank line].
  1853. HTML blocks continue until they are closed by their appropriate
  1854. [end condition], or the last line of the document or other [container
  1855. block](#container-blocks). This means any HTML **within an HTML
  1856. block** that might otherwise be recognised as a start condition will
  1857. be ignored by the parser and passed through as-is, without changing
  1858. the parser's state.
  1859. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1860. the parser state; as the HTML block was started in by start condition 6, it
  1861. will end at any blank line. This can be surprising:
  1862. ```````````````````````````````` example
  1863. <table><tr><td>
  1864. <pre>
  1865. **Hello**,
  1866. _world_.
  1867. </pre>
  1868. </td></tr></table>
  1869. .
  1870. <table><tr><td>
  1871. <pre>
  1872. **Hello**,
  1873. <p><em>world</em>.
  1874. </pre></p>
  1875. </td></tr></table>
  1876. ````````````````````````````````
  1877. In this case, the HTML block is terminated by the newline — the `**Hello**`
  1878. text remains verbatim — and regular parsing resumes, with a paragraph,
  1879. emphasised `world` and inline and block HTML following.
  1880. All types of [HTML blocks] except type 7 may interrupt
  1881. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1882. (This restriction is intended to prevent unwanted interpretation
  1883. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1884. Some simple examples follow. Here are some basic HTML blocks
  1885. of type 6:
  1886. ```````````````````````````````` example
  1887. <table>
  1888. <tr>
  1889. <td>
  1890. hi
  1891. </td>
  1892. </tr>
  1893. </table>
  1894. okay.
  1895. .
  1896. <table>
  1897. <tr>
  1898. <td>
  1899. hi
  1900. </td>
  1901. </tr>
  1902. </table>
  1903. <p>okay.</p>
  1904. ````````````````````````````````
  1905. ```````````````````````````````` example
  1906. <div>
  1907. *hello*
  1908. <foo><a>
  1909. .
  1910. <div>
  1911. *hello*
  1912. <foo><a>
  1913. ````````````````````````````````
  1914. A block can also start with a closing tag:
  1915. ```````````````````````````````` example
  1916. </div>
  1917. *foo*
  1918. .
  1919. </div>
  1920. *foo*
  1921. ````````````````````````````````
  1922. Here we have two HTML blocks with a Markdown paragraph between them:
  1923. ```````````````````````````````` example
  1924. <DIV CLASS="foo">
  1925. *Markdown*
  1926. </DIV>
  1927. .
  1928. <DIV CLASS="foo">
  1929. <p><em>Markdown</em></p>
  1930. </DIV>
  1931. ````````````````````````````````
  1932. The tag on the first line can be partial, as long
  1933. as it is split where there would be whitespace:
  1934. ```````````````````````````````` example
  1935. <div id="foo"
  1936. class="bar">
  1937. </div>
  1938. .
  1939. <div id="foo"
  1940. class="bar">
  1941. </div>
  1942. ````````````````````````````````
  1943. ```````````````````````````````` example
  1944. <div id="foo" class="bar
  1945. baz">
  1946. </div>
  1947. .
  1948. <div id="foo" class="bar
  1949. baz">
  1950. </div>
  1951. ````````````````````````````````
  1952. An open tag need not be closed:
  1953. ```````````````````````````````` example
  1954. <div>
  1955. *foo*
  1956. *bar*
  1957. .
  1958. <div>
  1959. *foo*
  1960. <p><em>bar</em></p>
  1961. ````````````````````````````````
  1962. A partial tag need not even be completed (garbage
  1963. in, garbage out):
  1964. ```````````````````````````````` example
  1965. <div id="foo"
  1966. *hi*
  1967. .
  1968. <div id="foo"
  1969. *hi*
  1970. ````````````````````````````````
  1971. ```````````````````````````````` example
  1972. <div class
  1973. foo
  1974. .
  1975. <div class
  1976. foo
  1977. ````````````````````````````````
  1978. The initial tag doesn't even need to be a valid
  1979. tag, as long as it starts like one:
  1980. ```````````````````````````````` example
  1981. <div *???-&&&-<---
  1982. *foo*
  1983. .
  1984. <div *???-&&&-<---
  1985. *foo*
  1986. ````````````````````````````````
  1987. In type 6 blocks, the initial tag need not be on a line by
  1988. itself:
  1989. ```````````````````````````````` example
  1990. <div><a href="bar">*foo*</a></div>
  1991. .
  1992. <div><a href="bar">*foo*</a></div>
  1993. ````````````````````````````````
  1994. ```````````````````````````````` example
  1995. <table><tr><td>
  1996. foo
  1997. </td></tr></table>
  1998. .
  1999. <table><tr><td>
  2000. foo
  2001. </td></tr></table>
  2002. ````````````````````````````````
  2003. Everything until the next blank line or end of document
  2004. gets included in the HTML block. So, in the following
  2005. example, what looks like a Markdown code block
  2006. is actually part of the HTML block, which continues until a blank
  2007. line or the end of the document is reached:
  2008. ```````````````````````````````` example
  2009. <div></div>
  2010. ``` c
  2011. int x = 33;
  2012. ```
  2013. .
  2014. <div></div>
  2015. ``` c
  2016. int x = 33;
  2017. ```
  2018. ````````````````````````````````
  2019. To start an [HTML block] with a tag that is *not* in the
  2020. list of block-level tags in (6), you must put the tag by
  2021. itself on the first line (and it must be complete):
  2022. ```````````````````````````````` example
  2023. <a href="foo">
  2024. *bar*
  2025. </a>
  2026. .
  2027. <a href="foo">
  2028. *bar*
  2029. </a>
  2030. ````````````````````````````````
  2031. In type 7 blocks, the [tag name] can be anything:
  2032. ```````````````````````````````` example
  2033. <Warning>
  2034. *bar*
  2035. </Warning>
  2036. .
  2037. <Warning>
  2038. *bar*
  2039. </Warning>
  2040. ````````````````````````````````
  2041. ```````````````````````````````` example
  2042. <i class="foo">
  2043. *bar*
  2044. </i>
  2045. .
  2046. <i class="foo">
  2047. *bar*
  2048. </i>
  2049. ````````````````````````````````
  2050. ```````````````````````````````` example
  2051. </ins>
  2052. *bar*
  2053. .
  2054. </ins>
  2055. *bar*
  2056. ````````````````````````````````
  2057. These rules are designed to allow us to work with tags that
  2058. can function as either block-level or inline-level tags.
  2059. The `<del>` tag is a nice example. We can surround content with
  2060. `<del>` tags in three different ways. In this case, we get a raw
  2061. HTML block, because the `<del>` tag is on a line by itself:
  2062. ```````````````````````````````` example
  2063. <del>
  2064. *foo*
  2065. </del>
  2066. .
  2067. <del>
  2068. *foo*
  2069. </del>
  2070. ````````````````````````````````
  2071. In this case, we get a raw HTML block that just includes
  2072. the `<del>` tag (because it ends with the following blank
  2073. line). So the contents get interpreted as CommonMark:
  2074. ```````````````````````````````` example
  2075. <del>
  2076. *foo*
  2077. </del>
  2078. .
  2079. <del>
  2080. <p><em>foo</em></p>
  2081. </del>
  2082. ````````````````````````````````
  2083. Finally, in this case, the `<del>` tags are interpreted
  2084. as [raw HTML] *inside* the CommonMark paragraph. (Because
  2085. the tag is not on a line by itself, we get inline HTML
  2086. rather than an [HTML block].)
  2087. ```````````````````````````````` example
  2088. <del>*foo*</del>
  2089. .
  2090. <p><del><em>foo</em></del></p>
  2091. ````````````````````````````````
  2092. HTML tags designed to contain literal content
  2093. (`script`, `style`, `pre`), comments, processing instructions,
  2094. and declarations are treated somewhat differently.
  2095. Instead of ending at the first blank line, these blocks
  2096. end at the first line containing a corresponding end tag.
  2097. As a result, these blocks can contain blank lines:
  2098. A pre tag (type 1):
  2099. ```````````````````````````````` example
  2100. <pre language="haskell"><code>
  2101. import Text.HTML.TagSoup
  2102. main :: IO ()
  2103. main = print $ parseTags tags
  2104. </code></pre>
  2105. okay
  2106. .
  2107. <pre language="haskell"><code>
  2108. import Text.HTML.TagSoup
  2109. main :: IO ()
  2110. main = print $ parseTags tags
  2111. </code></pre>
  2112. <p>okay</p>
  2113. ````````````````````````````````
  2114. A script tag (type 1):
  2115. ```````````````````````````````` example
  2116. <script type="text/javascript">
  2117. // JavaScript example
  2118. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  2119. </script>
  2120. okay
  2121. .
  2122. <script type="text/javascript">
  2123. // JavaScript example
  2124. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  2125. </script>
  2126. <p>okay</p>
  2127. ````````````````````````````````
  2128. A style tag (type 1):
  2129. ```````````````````````````````` example
  2130. <style
  2131. type="text/css">
  2132. h1 {color:red;}
  2133. p {color:blue;}
  2134. </style>
  2135. okay
  2136. .
  2137. <style
  2138. type="text/css">
  2139. h1 {color:red;}
  2140. p {color:blue;}
  2141. </style>
  2142. <p>okay</p>
  2143. ````````````````````````````````
  2144. If there is no matching end tag, the block will end at the
  2145. end of the document (or the enclosing [block quote][block quotes]
  2146. or [list item][list items]):
  2147. ```````````````````````````````` example
  2148. <style
  2149. type="text/css">
  2150. foo
  2151. .
  2152. <style
  2153. type="text/css">
  2154. foo
  2155. ````````````````````````````````
  2156. ```````````````````````````````` example
  2157. > <div>
  2158. > foo
  2159. bar
  2160. .
  2161. <blockquote>
  2162. <div>
  2163. foo
  2164. </blockquote>
  2165. <p>bar</p>
  2166. ````````````````````````````````
  2167. ```````````````````````````````` example
  2168. - <div>
  2169. - foo
  2170. .
  2171. <ul>
  2172. <li>
  2173. <div>
  2174. </li>
  2175. <li>foo</li>
  2176. </ul>
  2177. ````````````````````````````````
  2178. The end tag can occur on the same line as the start tag:
  2179. ```````````````````````````````` example
  2180. <style>p{color:red;}</style>
  2181. *foo*
  2182. .
  2183. <style>p{color:red;}</style>
  2184. <p><em>foo</em></p>
  2185. ````````````````````````````````
  2186. ```````````````````````````````` example
  2187. <!-- foo -->*bar*
  2188. *baz*
  2189. .
  2190. <!-- foo -->*bar*
  2191. <p><em>baz</em></p>
  2192. ````````````````````````````````
  2193. Note that anything on the last line after the
  2194. end tag will be included in the [HTML block]:
  2195. ```````````````````````````````` example
  2196. <script>
  2197. foo
  2198. </script>1. *bar*
  2199. .
  2200. <script>
  2201. foo
  2202. </script>1. *bar*
  2203. ````````````````````````````````
  2204. A comment (type 2):
  2205. ```````````````````````````````` example
  2206. <!-- Foo
  2207. bar
  2208. baz -->
  2209. okay
  2210. .
  2211. <!-- Foo
  2212. bar
  2213. baz -->
  2214. <p>okay</p>
  2215. ````````````````````````````````
  2216. A processing instruction (type 3):
  2217. ```````````````````````````````` example
  2218. <?php
  2219. echo '>';
  2220. ?>
  2221. okay
  2222. .
  2223. <?php
  2224. echo '>';
  2225. ?>
  2226. <p>okay</p>
  2227. ````````````````````````````````
  2228. A declaration (type 4):
  2229. ```````````````````````````````` example
  2230. <!DOCTYPE html>
  2231. .
  2232. <!DOCTYPE html>
  2233. ````````````````````````````````
  2234. CDATA (type 5):
  2235. ```````````````````````````````` example
  2236. <![CDATA[
  2237. function matchwo(a,b)
  2238. {
  2239. if (a < b && a < 0) then {
  2240. return 1;
  2241. } else {
  2242. return 0;
  2243. }
  2244. }
  2245. ]]>
  2246. okay
  2247. .
  2248. <![CDATA[
  2249. function matchwo(a,b)
  2250. {
  2251. if (a < b && a < 0) then {
  2252. return 1;
  2253. } else {
  2254. return 0;
  2255. }
  2256. }
  2257. ]]>
  2258. <p>okay</p>
  2259. ````````````````````````````````
  2260. The opening tag can be indented 1-3 spaces, but not 4:
  2261. ```````````````````````````````` example
  2262. <!-- foo -->
  2263. <!-- foo -->
  2264. .
  2265. <!-- foo -->
  2266. <pre><code>&lt;!-- foo --&gt;
  2267. </code></pre>
  2268. ````````````````````````````````
  2269. ```````````````````````````````` example
  2270. <div>
  2271. <div>
  2272. .
  2273. <div>
  2274. <pre><code>&lt;div&gt;
  2275. </code></pre>
  2276. ````````````````````````````````
  2277. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  2278. preceded by a blank line.
  2279. ```````````````````````````````` example
  2280. Foo
  2281. <div>
  2282. bar
  2283. </div>
  2284. .
  2285. <p>Foo</p>
  2286. <div>
  2287. bar
  2288. </div>
  2289. ````````````````````````````````
  2290. However, a following blank line is needed, except at the end of
  2291. a document, and except for blocks of types 1--5, [above][HTML
  2292. block]:
  2293. ```````````````````````````````` example
  2294. <div>
  2295. bar
  2296. </div>
  2297. *foo*
  2298. .
  2299. <div>
  2300. bar
  2301. </div>
  2302. *foo*
  2303. ````````````````````````````````
  2304. HTML blocks of type 7 cannot interrupt a paragraph:
  2305. ```````````````````````````````` example
  2306. Foo
  2307. <a href="bar">
  2308. baz
  2309. .
  2310. <p>Foo
  2311. <a href="bar">
  2312. baz</p>
  2313. ````````````````````````````````
  2314. This rule differs from John Gruber's original Markdown syntax
  2315. specification, which says:
  2316. > The only restrictions are that block-level HTML elements —
  2317. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2318. > surrounding content by blank lines, and the start and end tags of the
  2319. > block should not be indented with tabs or spaces.
  2320. In some ways Gruber's rule is more restrictive than the one given
  2321. here:
  2322. - It requires that an HTML block be preceded by a blank line.
  2323. - It does not allow the start tag to be indented.
  2324. - It requires a matching end tag, which it also does not allow to
  2325. be indented.
  2326. Most Markdown implementations (including some of Gruber's own) do not
  2327. respect all of these restrictions.
  2328. There is one respect, however, in which Gruber's rule is more liberal
  2329. than the one given here, since it allows blank lines to occur inside
  2330. an HTML block. There are two reasons for disallowing them here.
  2331. First, it removes the need to parse balanced tags, which is
  2332. expensive and can require backtracking from the end of the document
  2333. if no matching end tag is found. Second, it provides a very simple
  2334. and flexible way of including Markdown content inside HTML tags:
  2335. simply separate the Markdown from the HTML using blank lines:
  2336. Compare:
  2337. ```````````````````````````````` example
  2338. <div>
  2339. *Emphasized* text.
  2340. </div>
  2341. .
  2342. <div>
  2343. <p><em>Emphasized</em> text.</p>
  2344. </div>
  2345. ````````````````````````````````
  2346. ```````````````````````````````` example
  2347. <div>
  2348. *Emphasized* text.
  2349. </div>
  2350. .
  2351. <div>
  2352. *Emphasized* text.
  2353. </div>
  2354. ````````````````````````````````
  2355. Some Markdown implementations have adopted a convention of
  2356. interpreting content inside tags as text if the open tag has
  2357. the attribute `markdown=1`. The rule given above seems a simpler and
  2358. more elegant way of achieving the same expressive power, which is also
  2359. much simpler to parse.
  2360. The main potential drawback is that one can no longer paste HTML
  2361. blocks into Markdown documents with 100% reliability. However,
  2362. *in most cases* this will work fine, because the blank lines in
  2363. HTML are usually followed by HTML block tags. For example:
  2364. ```````````````````````````````` example
  2365. <table>
  2366. <tr>
  2367. <td>
  2368. Hi
  2369. </td>
  2370. </tr>
  2371. </table>
  2372. .
  2373. <table>
  2374. <tr>
  2375. <td>
  2376. Hi
  2377. </td>
  2378. </tr>
  2379. </table>
  2380. ````````````````````````````````
  2381. There are problems, however, if the inner tags are indented
  2382. *and* separated by spaces, as then they will be interpreted as
  2383. an indented code block:
  2384. ```````````````````````````````` example
  2385. <table>
  2386. <tr>
  2387. <td>
  2388. Hi
  2389. </td>
  2390. </tr>
  2391. </table>
  2392. .
  2393. <table>
  2394. <tr>
  2395. <pre><code>&lt;td&gt;
  2396. Hi
  2397. &lt;/td&gt;
  2398. </code></pre>
  2399. </tr>
  2400. </table>
  2401. ````````````````````````````````
  2402. Fortunately, blank lines are usually not necessary and can be
  2403. deleted. The exception is inside `<pre>` tags, but as described
  2404. [above][HTML blocks], raw HTML blocks starting with `<pre>`
  2405. *can* contain blank lines.
  2406. ## Link reference definitions
  2407. A [link reference definition](@)
  2408. consists of a [link label], indented up to three spaces, followed
  2409. by a colon (`:`), optional [whitespace] (including up to one
  2410. [line ending]), a [link destination],
  2411. optional [whitespace] (including up to one
  2412. [line ending]), and an optional [link
  2413. title], which if it is present must be separated
  2414. from the [link destination] by [whitespace].
  2415. No further [non-whitespace characters] may occur on the line.
  2416. A [link reference definition]
  2417. does not correspond to a structural element of a document. Instead, it
  2418. defines a label which can be used in [reference links]
  2419. and reference-style [images] elsewhere in the document. [Link
  2420. reference definitions] can come either before or after the links that use
  2421. them.
  2422. ```````````````````````````````` example
  2423. [foo]: /url "title"
  2424. [foo]
  2425. .
  2426. <p><a href="/url" title="title">foo</a></p>
  2427. ````````````````````````````````
  2428. ```````````````````````````````` example
  2429. [foo]:
  2430. /url
  2431. 'the title'
  2432. [foo]
  2433. .
  2434. <p><a href="/url" title="the title">foo</a></p>
  2435. ````````````````````````````````
  2436. ```````````````````````````````` example
  2437. [Foo*bar\]]:my_(url) 'title (with parens)'
  2438. [Foo*bar\]]
  2439. .
  2440. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2441. ````````````````````````````````
  2442. ```````````````````````````````` example
  2443. [Foo bar]:
  2444. <my url>
  2445. 'title'
  2446. [Foo bar]
  2447. .
  2448. <p><a href="my%20url" title="title">Foo bar</a></p>
  2449. ````````````````````````````````
  2450. The title may extend over multiple lines:
  2451. ```````````````````````````````` example
  2452. [foo]: /url '
  2453. title
  2454. line1
  2455. line2
  2456. '
  2457. [foo]
  2458. .
  2459. <p><a href="/url" title="
  2460. title
  2461. line1
  2462. line2
  2463. ">foo</a></p>
  2464. ````````````````````````````````
  2465. However, it may not contain a [blank line]:
  2466. ```````````````````````````````` example
  2467. [foo]: /url 'title
  2468. with blank line'
  2469. [foo]
  2470. .
  2471. <p>[foo]: /url 'title</p>
  2472. <p>with blank line'</p>
  2473. <p>[foo]</p>
  2474. ````````````````````````````````
  2475. The title may be omitted:
  2476. ```````````````````````````````` example
  2477. [foo]:
  2478. /url
  2479. [foo]
  2480. .
  2481. <p><a href="/url">foo</a></p>
  2482. ````````````````````````````````
  2483. The link destination may not be omitted:
  2484. ```````````````````````````````` example
  2485. [foo]:
  2486. [foo]
  2487. .
  2488. <p>[foo]:</p>
  2489. <p>[foo]</p>
  2490. ````````````````````````````````
  2491. However, an empty link destination may be specified using
  2492. angle brackets:
  2493. ```````````````````````````````` example
  2494. [foo]: <>
  2495. [foo]
  2496. .
  2497. <p><a href="">foo</a></p>
  2498. ````````````````````````````````
  2499. The title must be separated from the link destination by
  2500. whitespace:
  2501. ```````````````````````````````` example
  2502. [foo]: <bar>(baz)
  2503. [foo]
  2504. .
  2505. <p>[foo]: <bar>(baz)</p>
  2506. <p>[foo]</p>
  2507. ````````````````````````````````
  2508. Both title and destination can contain backslash escapes
  2509. and literal backslashes:
  2510. ```````````````````````````````` example
  2511. [foo]: /url\bar\*baz "foo\"bar\baz"
  2512. [foo]
  2513. .
  2514. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2515. ````````````````````````````````
  2516. A link can come before its corresponding definition:
  2517. ```````````````````````````````` example
  2518. [foo]
  2519. [foo]: url
  2520. .
  2521. <p><a href="url">foo</a></p>
  2522. ````````````````````````````````
  2523. If there are several matching definitions, the first one takes
  2524. precedence:
  2525. ```````````````````````````````` example
  2526. [foo]
  2527. [foo]: first
  2528. [foo]: second
  2529. .
  2530. <p><a href="first">foo</a></p>
  2531. ````````````````````````````````
  2532. As noted in the section on [Links], matching of labels is
  2533. case-insensitive (see [matches]).
  2534. ```````````````````````````````` example
  2535. [FOO]: /url
  2536. [Foo]
  2537. .
  2538. <p><a href="/url">Foo</a></p>
  2539. ````````````````````````````````
  2540. ```````````````````````````````` example
  2541. [ΑΓΩ]: /φου
  2542. [αγω]
  2543. .
  2544. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2545. ````````````````````````````````
  2546. Here is a link reference definition with no corresponding link.
  2547. It contributes nothing to the document.
  2548. ```````````````````````````````` example
  2549. [foo]: /url
  2550. .
  2551. ````````````````````````````````
  2552. Here is another one:
  2553. ```````````````````````````````` example
  2554. [
  2555. foo
  2556. ]: /url
  2557. bar
  2558. .
  2559. <p>bar</p>
  2560. ````````````````````````````````
  2561. This is not a link reference definition, because there are
  2562. [non-whitespace characters] after the title:
  2563. ```````````````````````````````` example
  2564. [foo]: /url "title" ok
  2565. .
  2566. <p>[foo]: /url &quot;title&quot; ok</p>
  2567. ````````````````````````````````
  2568. This is a link reference definition, but it has no title:
  2569. ```````````````````````````````` example
  2570. [foo]: /url
  2571. "title" ok
  2572. .
  2573. <p>&quot;title&quot; ok</p>
  2574. ````````````````````````````````
  2575. This is not a link reference definition, because it is indented
  2576. four spaces:
  2577. ```````````````````````````````` example
  2578. [foo]: /url "title"
  2579. [foo]
  2580. .
  2581. <pre><code>[foo]: /url &quot;title&quot;
  2582. </code></pre>
  2583. <p>[foo]</p>
  2584. ````````````````````````````````
  2585. This is not a link reference definition, because it occurs inside
  2586. a code block:
  2587. ```````````````````````````````` example
  2588. ```
  2589. [foo]: /url
  2590. ```
  2591. [foo]
  2592. .
  2593. <pre><code>[foo]: /url
  2594. </code></pre>
  2595. <p>[foo]</p>
  2596. ````````````````````````````````
  2597. A [link reference definition] cannot interrupt a paragraph.
  2598. ```````````````````````````````` example
  2599. Foo
  2600. [bar]: /baz
  2601. [bar]
  2602. .
  2603. <p>Foo
  2604. [bar]: /baz</p>
  2605. <p>[bar]</p>
  2606. ````````````````````````````````
  2607. However, it can directly follow other block elements, such as headings
  2608. and thematic breaks, and it need not be followed by a blank line.
  2609. ```````````````````````````````` example
  2610. # [Foo]
  2611. [foo]: /url
  2612. > bar
  2613. .
  2614. <h1><a href="/url">Foo</a></h1>
  2615. <blockquote>
  2616. <p>bar</p>
  2617. </blockquote>
  2618. ````````````````````````````````
  2619. ```````````````````````````````` example
  2620. [foo]: /url
  2621. bar
  2622. ===
  2623. [foo]
  2624. .
  2625. <h1>bar</h1>
  2626. <p><a href="/url">foo</a></p>
  2627. ````````````````````````````````
  2628. ```````````````````````````````` example
  2629. [foo]: /url
  2630. ===
  2631. [foo]
  2632. .
  2633. <p>===
  2634. <a href="/url">foo</a></p>
  2635. ````````````````````````````````
  2636. Several [link reference definitions]
  2637. can occur one after another, without intervening blank lines.
  2638. ```````````````````````````````` example
  2639. [foo]: /foo-url "foo"
  2640. [bar]: /bar-url
  2641. "bar"
  2642. [baz]: /baz-url
  2643. [foo],
  2644. [bar],
  2645. [baz]
  2646. .
  2647. <p><a href="/foo-url" title="foo">foo</a>,
  2648. <a href="/bar-url" title="bar">bar</a>,
  2649. <a href="/baz-url">baz</a></p>
  2650. ````````````````````````````````
  2651. [Link reference definitions] can occur
  2652. inside block containers, like lists and block quotations. They
  2653. affect the entire document, not just the container in which they
  2654. are defined:
  2655. ```````````````````````````````` example
  2656. [foo]
  2657. > [foo]: /url
  2658. .
  2659. <p><a href="/url">foo</a></p>
  2660. <blockquote>
  2661. </blockquote>
  2662. ````````````````````````````````
  2663. Whether something is a [link reference definition] is
  2664. independent of whether the link reference it defines is
  2665. used in the document. Thus, for example, the following
  2666. document contains just a link reference definition, and
  2667. no visible content:
  2668. ```````````````````````````````` example
  2669. [foo]: /url
  2670. .
  2671. ````````````````````````````````
  2672. ## Paragraphs
  2673. A sequence of non-blank lines that cannot be interpreted as other
  2674. kinds of blocks forms a [paragraph](@).
  2675. The contents of the paragraph are the result of parsing the
  2676. paragraph's raw content as inlines. The paragraph's raw content
  2677. is formed by concatenating the lines and removing initial and final
  2678. [whitespace].
  2679. A simple example with two paragraphs:
  2680. ```````````````````````````````` example
  2681. aaa
  2682. bbb
  2683. .
  2684. <p>aaa</p>
  2685. <p>bbb</p>
  2686. ````````````````````````````````
  2687. Paragraphs can contain multiple lines, but no blank lines:
  2688. ```````````````````````````````` example
  2689. aaa
  2690. bbb
  2691. ccc
  2692. ddd
  2693. .
  2694. <p>aaa
  2695. bbb</p>
  2696. <p>ccc
  2697. ddd</p>
  2698. ````````````````````````````````
  2699. Multiple blank lines between paragraph have no effect:
  2700. ```````````````````````````````` example
  2701. aaa
  2702. bbb
  2703. .
  2704. <p>aaa</p>
  2705. <p>bbb</p>
  2706. ````````````````````````````````
  2707. Leading spaces are skipped:
  2708. ```````````````````````````````` example
  2709. aaa
  2710. bbb
  2711. .
  2712. <p>aaa
  2713. bbb</p>
  2714. ````````````````````````````````
  2715. Lines after the first may be indented any amount, since indented
  2716. code blocks cannot interrupt paragraphs.
  2717. ```````````````````````````````` example
  2718. aaa
  2719. bbb
  2720. ccc
  2721. .
  2722. <p>aaa
  2723. bbb
  2724. ccc</p>
  2725. ````````````````````````````````
  2726. However, the first line may be indented at most three spaces,
  2727. or an indented code block will be triggered:
  2728. ```````````````````````````````` example
  2729. aaa
  2730. bbb
  2731. .
  2732. <p>aaa
  2733. bbb</p>
  2734. ````````````````````````````````
  2735. ```````````````````````````````` example
  2736. aaa
  2737. bbb
  2738. .
  2739. <pre><code>aaa
  2740. </code></pre>
  2741. <p>bbb</p>
  2742. ````````````````````````````````
  2743. Final spaces are stripped before inline parsing, so a paragraph
  2744. that ends with two or more spaces will not end with a [hard line
  2745. break]:
  2746. ```````````````````````````````` example
  2747. aaa
  2748. bbb
  2749. .
  2750. <p>aaa<br />
  2751. bbb</p>
  2752. ````````````````````````````````
  2753. ## Blank lines
  2754. [Blank lines] between block-level elements are ignored,
  2755. except for the role they play in determining whether a [list]
  2756. is [tight] or [loose].
  2757. Blank lines at the beginning and end of the document are also ignored.
  2758. ```````````````````````````````` example
  2759. aaa
  2760. # aaa
  2761. .
  2762. <p>aaa</p>
  2763. <h1>aaa</h1>
  2764. ````````````````````````````````
  2765. # Container blocks
  2766. A [container block](#container-blocks) is a block that has other
  2767. blocks as its contents. There are two basic kinds of container blocks:
  2768. [block quotes] and [list items].
  2769. [Lists] are meta-containers for [list items].
  2770. We define the syntax for container blocks recursively. The general
  2771. form of the definition is:
  2772. > If X is a sequence of blocks, then the result of
  2773. > transforming X in such-and-such a way is a container of type Y
  2774. > with these blocks as its content.
  2775. So, we explain what counts as a block quote or list item by explaining
  2776. how these can be *generated* from their contents. This should suffice
  2777. to define the syntax, although it does not give a recipe for *parsing*
  2778. these constructions. (A recipe is provided below in the section entitled
  2779. [A parsing strategy](#appendix-a-parsing-strategy).)
  2780. ## Block quotes
  2781. A [block quote marker](@)
  2782. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2783. with a following space, or (b) a single character `>` not followed by a space.
  2784. The following rules define [block quotes]:
  2785. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2786. of blocks *Bs*, then the result of prepending a [block quote
  2787. marker] to the beginning of each line in *Ls*
  2788. is a [block quote](#block-quotes) containing *Bs*.
  2789. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2790. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2791. the initial [block quote marker] from one or
  2792. more lines in which the next [non-whitespace character] after the [block
  2793. quote marker] is [paragraph continuation
  2794. text] is a block quote with *Bs* as its content.
  2795. [Paragraph continuation text](@) is text
  2796. that will be parsed as part of the content of a paragraph, but does
  2797. not occur at the beginning of the paragraph.
  2798. 3. **Consecutiveness.** A document cannot contain two [block
  2799. quotes] in a row unless there is a [blank line] between them.
  2800. Nothing else counts as a [block quote](#block-quotes).
  2801. Here is a simple example:
  2802. ```````````````````````````````` example
  2803. > # Foo
  2804. > bar
  2805. > baz
  2806. .
  2807. <blockquote>
  2808. <h1>Foo</h1>
  2809. <p>bar
  2810. baz</p>
  2811. </blockquote>
  2812. ````````````````````````````````
  2813. The spaces after the `>` characters can be omitted:
  2814. ```````````````````````````````` example
  2815. ># Foo
  2816. >bar
  2817. > baz
  2818. .
  2819. <blockquote>
  2820. <h1>Foo</h1>
  2821. <p>bar
  2822. baz</p>
  2823. </blockquote>
  2824. ````````````````````````````````
  2825. The `>` characters can be indented 1-3 spaces:
  2826. ```````````````````````````````` example
  2827. > # Foo
  2828. > bar
  2829. > baz
  2830. .
  2831. <blockquote>
  2832. <h1>Foo</h1>
  2833. <p>bar
  2834. baz</p>
  2835. </blockquote>
  2836. ````````````````````````````````
  2837. Four spaces gives us a code block:
  2838. ```````````````````````````````` example
  2839. > # Foo
  2840. > bar
  2841. > baz
  2842. .
  2843. <pre><code>&gt; # Foo
  2844. &gt; bar
  2845. &gt; baz
  2846. </code></pre>
  2847. ````````````````````````````````
  2848. The Laziness clause allows us to omit the `>` before
  2849. [paragraph continuation text]:
  2850. ```````````````````````````````` example
  2851. > # Foo
  2852. > bar
  2853. baz
  2854. .
  2855. <blockquote>
  2856. <h1>Foo</h1>
  2857. <p>bar
  2858. baz</p>
  2859. </blockquote>
  2860. ````````````````````````````````
  2861. A block quote can contain some lazy and some non-lazy
  2862. continuation lines:
  2863. ```````````````````````````````` example
  2864. > bar
  2865. baz
  2866. > foo
  2867. .
  2868. <blockquote>
  2869. <p>bar
  2870. baz
  2871. foo</p>
  2872. </blockquote>
  2873. ````````````````````````````````
  2874. Laziness only applies to lines that would have been continuations of
  2875. paragraphs had they been prepended with [block quote markers].
  2876. For example, the `> ` cannot be omitted in the second line of
  2877. ``` markdown
  2878. > foo
  2879. > ---
  2880. ```
  2881. without changing the meaning:
  2882. ```````````````````````````````` example
  2883. > foo
  2884. ---
  2885. .
  2886. <blockquote>
  2887. <p>foo</p>
  2888. </blockquote>
  2889. <hr />
  2890. ````````````````````````````````
  2891. Similarly, if we omit the `> ` in the second line of
  2892. ``` markdown
  2893. > - foo
  2894. > - bar
  2895. ```
  2896. then the block quote ends after the first line:
  2897. ```````````````````````````````` example
  2898. > - foo
  2899. - bar
  2900. .
  2901. <blockquote>
  2902. <ul>
  2903. <li>foo</li>
  2904. </ul>
  2905. </blockquote>
  2906. <ul>
  2907. <li>bar</li>
  2908. </ul>
  2909. ````````````````````````````````
  2910. For the same reason, we can't omit the `> ` in front of
  2911. subsequent lines of an indented or fenced code block:
  2912. ```````````````````````````````` example
  2913. > foo
  2914. bar
  2915. .
  2916. <blockquote>
  2917. <pre><code>foo
  2918. </code></pre>
  2919. </blockquote>
  2920. <pre><code>bar
  2921. </code></pre>
  2922. ````````````````````````````````
  2923. ```````````````````````````````` example
  2924. > ```
  2925. foo
  2926. ```
  2927. .
  2928. <blockquote>
  2929. <pre><code></code></pre>
  2930. </blockquote>
  2931. <p>foo</p>
  2932. <pre><code></code></pre>
  2933. ````````````````````````````````
  2934. Note that in the following case, we have a [lazy
  2935. continuation line]:
  2936. ```````````````````````````````` example
  2937. > foo
  2938. - bar
  2939. .
  2940. <blockquote>
  2941. <p>foo
  2942. - bar</p>
  2943. </blockquote>
  2944. ````````````````````````````````
  2945. To see why, note that in
  2946. ```markdown
  2947. > foo
  2948. > - bar
  2949. ```
  2950. the `- bar` is indented too far to start a list, and can't
  2951. be an indented code block because indented code blocks cannot
  2952. interrupt paragraphs, so it is [paragraph continuation text].
  2953. A block quote can be empty:
  2954. ```````````````````````````````` example
  2955. >
  2956. .
  2957. <blockquote>
  2958. </blockquote>
  2959. ````````````````````````````````
  2960. ```````````````````````````````` example
  2961. >
  2962. >
  2963. >
  2964. .
  2965. <blockquote>
  2966. </blockquote>
  2967. ````````````````````````````````
  2968. A block quote can have initial or final blank lines:
  2969. ```````````````````````````````` example
  2970. >
  2971. > foo
  2972. >
  2973. .
  2974. <blockquote>
  2975. <p>foo</p>
  2976. </blockquote>
  2977. ````````````````````````````````
  2978. A blank line always separates block quotes:
  2979. ```````````````````````````````` example
  2980. > foo
  2981. > bar
  2982. .
  2983. <blockquote>
  2984. <p>foo</p>
  2985. </blockquote>
  2986. <blockquote>
  2987. <p>bar</p>
  2988. </blockquote>
  2989. ````````````````````````````````
  2990. (Most current Markdown implementations, including John Gruber's
  2991. original `Markdown.pl`, will parse this example as a single block quote
  2992. with two paragraphs. But it seems better to allow the author to decide
  2993. whether two block quotes or one are wanted.)
  2994. Consecutiveness means that if we put these block quotes together,
  2995. we get a single block quote:
  2996. ```````````````````````````````` example
  2997. > foo
  2998. > bar
  2999. .
  3000. <blockquote>
  3001. <p>foo
  3002. bar</p>
  3003. </blockquote>
  3004. ````````````````````````````````
  3005. To get a block quote with two paragraphs, use:
  3006. ```````````````````````````````` example
  3007. > foo
  3008. >
  3009. > bar
  3010. .
  3011. <blockquote>
  3012. <p>foo</p>
  3013. <p>bar</p>
  3014. </blockquote>
  3015. ````````````````````````````````
  3016. Block quotes can interrupt paragraphs:
  3017. ```````````````````````````````` example
  3018. foo
  3019. > bar
  3020. .
  3021. <p>foo</p>
  3022. <blockquote>
  3023. <p>bar</p>
  3024. </blockquote>
  3025. ````````````````````````````````
  3026. In general, blank lines are not needed before or after block
  3027. quotes:
  3028. ```````````````````````````````` example
  3029. > aaa
  3030. ***
  3031. > bbb
  3032. .
  3033. <blockquote>
  3034. <p>aaa</p>
  3035. </blockquote>
  3036. <hr />
  3037. <blockquote>
  3038. <p>bbb</p>
  3039. </blockquote>
  3040. ````````````````````````````````
  3041. However, because of laziness, a blank line is needed between
  3042. a block quote and a following paragraph:
  3043. ```````````````````````````````` example
  3044. > bar
  3045. baz
  3046. .
  3047. <blockquote>
  3048. <p>bar
  3049. baz</p>
  3050. </blockquote>
  3051. ````````````````````````````````
  3052. ```````````````````````````````` example
  3053. > bar
  3054. baz
  3055. .
  3056. <blockquote>
  3057. <p>bar</p>
  3058. </blockquote>
  3059. <p>baz</p>
  3060. ````````````````````````````````
  3061. ```````````````````````````````` example
  3062. > bar
  3063. >
  3064. baz
  3065. .
  3066. <blockquote>
  3067. <p>bar</p>
  3068. </blockquote>
  3069. <p>baz</p>
  3070. ````````````````````````````````
  3071. It is a consequence of the Laziness rule that any number
  3072. of initial `>`s may be omitted on a continuation line of a
  3073. nested block quote:
  3074. ```````````````````````````````` example
  3075. > > > foo
  3076. bar
  3077. .
  3078. <blockquote>
  3079. <blockquote>
  3080. <blockquote>
  3081. <p>foo
  3082. bar</p>
  3083. </blockquote>
  3084. </blockquote>
  3085. </blockquote>
  3086. ````````````````````````````````
  3087. ```````````````````````````````` example
  3088. >>> foo
  3089. > bar
  3090. >>baz
  3091. .
  3092. <blockquote>
  3093. <blockquote>
  3094. <blockquote>
  3095. <p>foo
  3096. bar
  3097. baz</p>
  3098. </blockquote>
  3099. </blockquote>
  3100. </blockquote>
  3101. ````````````````````````````````
  3102. When including an indented code block in a block quote,
  3103. remember that the [block quote marker] includes
  3104. both the `>` and a following space. So *five spaces* are needed after
  3105. the `>`:
  3106. ```````````````````````````````` example
  3107. > code
  3108. > not code
  3109. .
  3110. <blockquote>
  3111. <pre><code>code
  3112. </code></pre>
  3113. </blockquote>
  3114. <blockquote>
  3115. <p>not code</p>
  3116. </blockquote>
  3117. ````````````````````````````````
  3118. ## List items
  3119. A [list marker](@) is a
  3120. [bullet list marker] or an [ordered list marker].
  3121. A [bullet list marker](@)
  3122. is a `-`, `+`, or `*` character.
  3123. An [ordered list marker](@)
  3124. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  3125. `.` character or a `)` character. (The reason for the length
  3126. limit is that with 10 digits we start seeing integer overflows
  3127. in some browsers.)
  3128. The following rules define [list items]:
  3129. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  3130. blocks *Bs* starting with a [non-whitespace character], and *M* is a
  3131. list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  3132. of prepending *M* and the following spaces to the first line of
  3133. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  3134. list item with *Bs* as its contents. The type of the list item
  3135. (bullet or ordered) is determined by the type of its list marker.
  3136. If the list item is ordered, then it is also assigned a start
  3137. number, based on the ordered list marker.
  3138. Exceptions:
  3139. 1. When the first list item in a [list] interrupts
  3140. a paragraph---that is, when it starts on a line that would
  3141. otherwise count as [paragraph continuation text]---then (a)
  3142. the lines *Ls* must not begin with a blank line, and (b) if
  3143. the list item is ordered, the start number must be 1.
  3144. 2. If any line is a [thematic break][thematic breaks] then
  3145. that line is not a list item.
  3146. For example, let *Ls* be the lines
  3147. ```````````````````````````````` example
  3148. A paragraph
  3149. with two lines.
  3150. indented code
  3151. > A block quote.
  3152. .
  3153. <p>A paragraph
  3154. with two lines.</p>
  3155. <pre><code>indented code
  3156. </code></pre>
  3157. <blockquote>
  3158. <p>A block quote.</p>
  3159. </blockquote>
  3160. ````````````````````````````````
  3161. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  3162. that the following is an ordered list item with start number 1,
  3163. and the same contents as *Ls*:
  3164. ```````````````````````````````` example
  3165. 1. A paragraph
  3166. with two lines.
  3167. indented code
  3168. > A block quote.
  3169. .
  3170. <ol>
  3171. <li>
  3172. <p>A paragraph
  3173. with two lines.</p>
  3174. <pre><code>indented code
  3175. </code></pre>
  3176. <blockquote>
  3177. <p>A block quote.</p>
  3178. </blockquote>
  3179. </li>
  3180. </ol>
  3181. ````````````````````````````````
  3182. The most important thing to notice is that the position of
  3183. the text after the list marker determines how much indentation
  3184. is needed in subsequent blocks in the list item. If the list
  3185. marker takes up two spaces, and there are three spaces between
  3186. the list marker and the next [non-whitespace character], then blocks
  3187. must be indented five spaces in order to fall under the list
  3188. item.
  3189. Here are some examples showing how far content must be indented to be
  3190. put under the list item:
  3191. ```````````````````````````````` example
  3192. - one
  3193. two
  3194. .
  3195. <ul>
  3196. <li>one</li>
  3197. </ul>
  3198. <p>two</p>
  3199. ````````````````````````````````
  3200. ```````````````````````````````` example
  3201. - one
  3202. two
  3203. .
  3204. <ul>
  3205. <li>
  3206. <p>one</p>
  3207. <p>two</p>
  3208. </li>
  3209. </ul>
  3210. ````````````````````````````````
  3211. ```````````````````````````````` example
  3212. - one
  3213. two
  3214. .
  3215. <ul>
  3216. <li>one</li>
  3217. </ul>
  3218. <pre><code> two
  3219. </code></pre>
  3220. ````````````````````````````````
  3221. ```````````````````````````````` example
  3222. - one
  3223. two
  3224. .
  3225. <ul>
  3226. <li>
  3227. <p>one</p>
  3228. <p>two</p>
  3229. </li>
  3230. </ul>
  3231. ````````````````````````````````
  3232. It is tempting to think of this in terms of columns: the continuation
  3233. blocks must be indented at least to the column of the first
  3234. [non-whitespace character] after the list marker. However, that is not quite right.
  3235. The spaces after the list marker determine how much relative indentation
  3236. is needed. Which column this indentation reaches will depend on
  3237. how the list item is embedded in other constructions, as shown by
  3238. this example:
  3239. ```````````````````````````````` example
  3240. > > 1. one
  3241. >>
  3242. >> two
  3243. .
  3244. <blockquote>
  3245. <blockquote>
  3246. <ol>
  3247. <li>
  3248. <p>one</p>
  3249. <p>two</p>
  3250. </li>
  3251. </ol>
  3252. </blockquote>
  3253. </blockquote>
  3254. ````````````````````````````````
  3255. Here `two` occurs in the same column as the list marker `1.`,
  3256. but is actually contained in the list item, because there is
  3257. sufficient indentation after the last containing blockquote marker.
  3258. The converse is also possible. In the following example, the word `two`
  3259. occurs far to the right of the initial text of the list item, `one`, but
  3260. it is not considered part of the list item, because it is not indented
  3261. far enough past the blockquote marker:
  3262. ```````````````````````````````` example
  3263. >>- one
  3264. >>
  3265. > > two
  3266. .
  3267. <blockquote>
  3268. <blockquote>
  3269. <ul>
  3270. <li>one</li>
  3271. </ul>
  3272. <p>two</p>
  3273. </blockquote>
  3274. </blockquote>
  3275. ````````````````````````````````
  3276. Note that at least one space is needed between the list marker and
  3277. any following content, so these are not list items:
  3278. ```````````````````````````````` example
  3279. -one
  3280. 2.two
  3281. .
  3282. <p>-one</p>
  3283. <p>2.two</p>
  3284. ````````````````````````````````
  3285. A list item may contain blocks that are separated by more than
  3286. one blank line.
  3287. ```````````````````````````````` example
  3288. - foo
  3289. bar
  3290. .
  3291. <ul>
  3292. <li>
  3293. <p>foo</p>
  3294. <p>bar</p>
  3295. </li>
  3296. </ul>
  3297. ````````````````````````````````
  3298. A list item may contain any kind of block:
  3299. ```````````````````````````````` example
  3300. 1. foo
  3301. ```
  3302. bar
  3303. ```
  3304. baz
  3305. > bam
  3306. .
  3307. <ol>
  3308. <li>
  3309. <p>foo</p>
  3310. <pre><code>bar
  3311. </code></pre>
  3312. <p>baz</p>
  3313. <blockquote>
  3314. <p>bam</p>
  3315. </blockquote>
  3316. </li>
  3317. </ol>
  3318. ````````````````````````````````
  3319. A list item that contains an indented code block will preserve
  3320. empty lines within the code block verbatim.
  3321. ```````````````````````````````` example
  3322. - Foo
  3323. bar
  3324. baz
  3325. .
  3326. <ul>
  3327. <li>
  3328. <p>Foo</p>
  3329. <pre><code>bar
  3330. baz
  3331. </code></pre>
  3332. </li>
  3333. </ul>
  3334. ````````````````````````````````
  3335. Note that ordered list start numbers must be nine digits or less:
  3336. ```````````````````````````````` example
  3337. 123456789. ok
  3338. .
  3339. <ol start="123456789">
  3340. <li>ok</li>
  3341. </ol>
  3342. ````````````````````````````````
  3343. ```````````````````````````````` example
  3344. 1234567890. not ok
  3345. .
  3346. <p>1234567890. not ok</p>
  3347. ````````````````````````````````
  3348. A start number may begin with 0s:
  3349. ```````````````````````````````` example
  3350. 0. ok
  3351. .
  3352. <ol start="0">
  3353. <li>ok</li>
  3354. </ol>
  3355. ````````````````````````````````
  3356. ```````````````````````````````` example
  3357. 003. ok
  3358. .
  3359. <ol start="3">
  3360. <li>ok</li>
  3361. </ol>
  3362. ````````````````````````````````
  3363. A start number may not be negative:
  3364. ```````````````````````````````` example
  3365. -1. not ok
  3366. .
  3367. <p>-1. not ok</p>
  3368. ````````````````````````````````
  3369. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3370. constitute a sequence of blocks *Bs* starting with an indented code
  3371. block, and *M* is a list marker of width *W* followed by
  3372. one space, then the result of prepending *M* and the following
  3373. space to the first line of *Ls*, and indenting subsequent lines of
  3374. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3375. If a line is empty, then it need not be indented. The type of the
  3376. list item (bullet or ordered) is determined by the type of its list
  3377. marker. If the list item is ordered, then it is also assigned a
  3378. start number, based on the ordered list marker.
  3379. An indented code block will have to be indented four spaces beyond
  3380. the edge of the region where text will be included in the list item.
  3381. In the following case that is 6 spaces:
  3382. ```````````````````````````````` example
  3383. - foo
  3384. bar
  3385. .
  3386. <ul>
  3387. <li>
  3388. <p>foo</p>
  3389. <pre><code>bar
  3390. </code></pre>
  3391. </li>
  3392. </ul>
  3393. ````````````````````````````````
  3394. And in this case it is 11 spaces:
  3395. ```````````````````````````````` example
  3396. 10. foo
  3397. bar
  3398. .
  3399. <ol start="10">
  3400. <li>
  3401. <p>foo</p>
  3402. <pre><code>bar
  3403. </code></pre>
  3404. </li>
  3405. </ol>
  3406. ````````````````````````````````
  3407. If the *first* block in the list item is an indented code block,
  3408. then by rule #2, the contents must be indented *one* space after the
  3409. list marker:
  3410. ```````````````````````````````` example
  3411. indented code
  3412. paragraph
  3413. more code
  3414. .
  3415. <pre><code>indented code
  3416. </code></pre>
  3417. <p>paragraph</p>
  3418. <pre><code>more code
  3419. </code></pre>
  3420. ````````````````````````````````
  3421. ```````````````````````````````` example
  3422. 1. indented code
  3423. paragraph
  3424. more code
  3425. .
  3426. <ol>
  3427. <li>
  3428. <pre><code>indented code
  3429. </code></pre>
  3430. <p>paragraph</p>
  3431. <pre><code>more code
  3432. </code></pre>
  3433. </li>
  3434. </ol>
  3435. ````````````````````````````````
  3436. Note that an additional space indent is interpreted as space
  3437. inside the code block:
  3438. ```````````````````````````````` example
  3439. 1. indented code
  3440. paragraph
  3441. more code
  3442. .
  3443. <ol>
  3444. <li>
  3445. <pre><code> indented code
  3446. </code></pre>
  3447. <p>paragraph</p>
  3448. <pre><code>more code
  3449. </code></pre>
  3450. </li>
  3451. </ol>
  3452. ````````````````````````````````
  3453. Note that rules #1 and #2 only apply to two cases: (a) cases
  3454. in which the lines to be included in a list item begin with a
  3455. [non-whitespace character], and (b) cases in which
  3456. they begin with an indented code
  3457. block. In a case like the following, where the first block begins with
  3458. a three-space indent, the rules do not allow us to form a list item by
  3459. indenting the whole thing and prepending a list marker:
  3460. ```````````````````````````````` example
  3461. foo
  3462. bar
  3463. .
  3464. <p>foo</p>
  3465. <p>bar</p>
  3466. ````````````````````````````````
  3467. ```````````````````````````````` example
  3468. - foo
  3469. bar
  3470. .
  3471. <ul>
  3472. <li>foo</li>
  3473. </ul>
  3474. <p>bar</p>
  3475. ````````````````````````````````
  3476. This is not a significant restriction, because when a block begins
  3477. with 1-3 spaces indent, the indentation can always be removed without
  3478. a change in interpretation, allowing rule #1 to be applied. So, in
  3479. the above case:
  3480. ```````````````````````````````` example
  3481. - foo
  3482. bar
  3483. .
  3484. <ul>
  3485. <li>
  3486. <p>foo</p>
  3487. <p>bar</p>
  3488. </li>
  3489. </ul>
  3490. ````````````````````````````````
  3491. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3492. starting with a single [blank line] constitute a (possibly empty)
  3493. sequence of blocks *Bs*, not separated from each other by more than
  3494. one blank line, and *M* is a list marker of width *W*,
  3495. then the result of prepending *M* to the first line of *Ls*, and
  3496. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3497. item with *Bs* as its contents.
  3498. If a line is empty, then it need not be indented. The type of the
  3499. list item (bullet or ordered) is determined by the type of its list
  3500. marker. If the list item is ordered, then it is also assigned a
  3501. start number, based on the ordered list marker.
  3502. Here are some list items that start with a blank line but are not empty:
  3503. ```````````````````````````````` example
  3504. -
  3505. foo
  3506. -
  3507. ```
  3508. bar
  3509. ```
  3510. -
  3511. baz
  3512. .
  3513. <ul>
  3514. <li>foo</li>
  3515. <li>
  3516. <pre><code>bar
  3517. </code></pre>
  3518. </li>
  3519. <li>
  3520. <pre><code>baz
  3521. </code></pre>
  3522. </li>
  3523. </ul>
  3524. ````````````````````````````````
  3525. When the list item starts with a blank line, the number of spaces
  3526. following the list marker doesn't change the required indentation:
  3527. ```````````````````````````````` example
  3528. -
  3529. foo
  3530. .
  3531. <ul>
  3532. <li>foo</li>
  3533. </ul>
  3534. ````````````````````````````````
  3535. A list item can begin with at most one blank line.
  3536. In the following example, `foo` is not part of the list
  3537. item:
  3538. ```````````````````````````````` example
  3539. -
  3540. foo
  3541. .
  3542. <ul>
  3543. <li></li>
  3544. </ul>
  3545. <p>foo</p>
  3546. ````````````````````````````````
  3547. Here is an empty bullet list item:
  3548. ```````````````````````````````` example
  3549. - foo
  3550. -
  3551. - bar
  3552. .
  3553. <ul>
  3554. <li>foo</li>
  3555. <li></li>
  3556. <li>bar</li>
  3557. </ul>
  3558. ````````````````````````````````
  3559. It does not matter whether there are spaces following the [list marker]:
  3560. ```````````````````````````````` example
  3561. - foo
  3562. -
  3563. - bar
  3564. .
  3565. <ul>
  3566. <li>foo</li>
  3567. <li></li>
  3568. <li>bar</li>
  3569. </ul>
  3570. ````````````````````````````````
  3571. Here is an empty ordered list item:
  3572. ```````````````````````````````` example
  3573. 1. foo
  3574. 2.
  3575. 3. bar
  3576. .
  3577. <ol>
  3578. <li>foo</li>
  3579. <li></li>
  3580. <li>bar</li>
  3581. </ol>
  3582. ````````````````````````````````
  3583. A list may start or end with an empty list item:
  3584. ```````````````````````````````` example
  3585. *
  3586. .
  3587. <ul>
  3588. <li></li>
  3589. </ul>
  3590. ````````````````````````````````
  3591. However, an empty list item cannot interrupt a paragraph:
  3592. ```````````````````````````````` example
  3593. foo
  3594. *
  3595. foo
  3596. 1.
  3597. .
  3598. <p>foo
  3599. *</p>
  3600. <p>foo
  3601. 1.</p>
  3602. ````````````````````````````````
  3603. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3604. according to rule #1, #2, or #3, then the result of indenting each line
  3605. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3606. list item with the same contents and attributes. If a line is
  3607. empty, then it need not be indented.
  3608. Indented one space:
  3609. ```````````````````````````````` example
  3610. 1. A paragraph
  3611. with two lines.
  3612. indented code
  3613. > A block quote.
  3614. .
  3615. <ol>
  3616. <li>
  3617. <p>A paragraph
  3618. with two lines.</p>
  3619. <pre><code>indented code
  3620. </code></pre>
  3621. <blockquote>
  3622. <p>A block quote.</p>
  3623. </blockquote>
  3624. </li>
  3625. </ol>
  3626. ````````````````````````````````
  3627. Indented two spaces:
  3628. ```````````````````````````````` example
  3629. 1. A paragraph
  3630. with two lines.
  3631. indented code
  3632. > A block quote.
  3633. .
  3634. <ol>
  3635. <li>
  3636. <p>A paragraph
  3637. with two lines.</p>
  3638. <pre><code>indented code
  3639. </code></pre>
  3640. <blockquote>
  3641. <p>A block quote.</p>
  3642. </blockquote>
  3643. </li>
  3644. </ol>
  3645. ````````````````````````````````
  3646. Indented three spaces:
  3647. ```````````````````````````````` example
  3648. 1. A paragraph
  3649. with two lines.
  3650. indented code
  3651. > A block quote.
  3652. .
  3653. <ol>
  3654. <li>
  3655. <p>A paragraph
  3656. with two lines.</p>
  3657. <pre><code>indented code
  3658. </code></pre>
  3659. <blockquote>
  3660. <p>A block quote.</p>
  3661. </blockquote>
  3662. </li>
  3663. </ol>
  3664. ````````````````````````````````
  3665. Four spaces indent gives a code block:
  3666. ```````````````````````````````` example
  3667. 1. A paragraph
  3668. with two lines.
  3669. indented code
  3670. > A block quote.
  3671. .
  3672. <pre><code>1. A paragraph
  3673. with two lines.
  3674. indented code
  3675. &gt; A block quote.
  3676. </code></pre>
  3677. ````````````````````````````````
  3678. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3679. item](#list-items) with contents *Bs*, then the result of deleting
  3680. some or all of the indentation from one or more lines in which the
  3681. next [non-whitespace character] after the indentation is
  3682. [paragraph continuation text] is a
  3683. list item with the same contents and attributes. The unindented
  3684. lines are called
  3685. [lazy continuation line](@)s.
  3686. Here is an example with [lazy continuation lines]:
  3687. ```````````````````````````````` example
  3688. 1. A paragraph
  3689. with two lines.
  3690. indented code
  3691. > A block quote.
  3692. .
  3693. <ol>
  3694. <li>
  3695. <p>A paragraph
  3696. with two lines.</p>
  3697. <pre><code>indented code
  3698. </code></pre>
  3699. <blockquote>
  3700. <p>A block quote.</p>
  3701. </blockquote>
  3702. </li>
  3703. </ol>
  3704. ````````````````````````````````
  3705. Indentation can be partially deleted:
  3706. ```````````````````````````````` example
  3707. 1. A paragraph
  3708. with two lines.
  3709. .
  3710. <ol>
  3711. <li>A paragraph
  3712. with two lines.</li>
  3713. </ol>
  3714. ````````````````````````````````
  3715. These examples show how laziness can work in nested structures:
  3716. ```````````````````````````````` example
  3717. > 1. > Blockquote
  3718. continued here.
  3719. .
  3720. <blockquote>
  3721. <ol>
  3722. <li>
  3723. <blockquote>
  3724. <p>Blockquote
  3725. continued here.</p>
  3726. </blockquote>
  3727. </li>
  3728. </ol>
  3729. </blockquote>
  3730. ````````````````````````````````
  3731. ```````````````````````````````` example
  3732. > 1. > Blockquote
  3733. > continued here.
  3734. .
  3735. <blockquote>
  3736. <ol>
  3737. <li>
  3738. <blockquote>
  3739. <p>Blockquote
  3740. continued here.</p>
  3741. </blockquote>
  3742. </li>
  3743. </ol>
  3744. </blockquote>
  3745. ````````````````````````````````
  3746. 6. **That's all.** Nothing that is not counted as a list item by rules
  3747. #1--5 counts as a [list item](#list-items).
  3748. The rules for sublists follow from the general rules
  3749. [above][List items]. A sublist must be indented the same number
  3750. of spaces a paragraph would need to be in order to be included
  3751. in the list item.
  3752. So, in this case we need two spaces indent:
  3753. ```````````````````````````````` example
  3754. - foo
  3755. - bar
  3756. - baz
  3757. - boo
  3758. .
  3759. <ul>
  3760. <li>foo
  3761. <ul>
  3762. <li>bar
  3763. <ul>
  3764. <li>baz
  3765. <ul>
  3766. <li>boo</li>
  3767. </ul>
  3768. </li>
  3769. </ul>
  3770. </li>
  3771. </ul>
  3772. </li>
  3773. </ul>
  3774. ````````````````````````````````
  3775. One is not enough:
  3776. ```````````````````````````````` example
  3777. - foo
  3778. - bar
  3779. - baz
  3780. - boo
  3781. .
  3782. <ul>
  3783. <li>foo</li>
  3784. <li>bar</li>
  3785. <li>baz</li>
  3786. <li>boo</li>
  3787. </ul>
  3788. ````````````````````````````````
  3789. Here we need four, because the list marker is wider:
  3790. ```````````````````````````````` example
  3791. 10) foo
  3792. - bar
  3793. .
  3794. <ol start="10">
  3795. <li>foo
  3796. <ul>
  3797. <li>bar</li>
  3798. </ul>
  3799. </li>
  3800. </ol>
  3801. ````````````````````````````````
  3802. Three is not enough:
  3803. ```````````````````````````````` example
  3804. 10) foo
  3805. - bar
  3806. .
  3807. <ol start="10">
  3808. <li>foo</li>
  3809. </ol>
  3810. <ul>
  3811. <li>bar</li>
  3812. </ul>
  3813. ````````````````````````````````
  3814. A list may be the first block in a list item:
  3815. ```````````````````````````````` example
  3816. - - foo
  3817. .
  3818. <ul>
  3819. <li>
  3820. <ul>
  3821. <li>foo</li>
  3822. </ul>
  3823. </li>
  3824. </ul>
  3825. ````````````````````````````````
  3826. ```````````````````````````````` example
  3827. 1. - 2. foo
  3828. .
  3829. <ol>
  3830. <li>
  3831. <ul>
  3832. <li>
  3833. <ol start="2">
  3834. <li>foo</li>
  3835. </ol>
  3836. </li>
  3837. </ul>
  3838. </li>
  3839. </ol>
  3840. ````````````````````````````````
  3841. A list item can contain a heading:
  3842. ```````````````````````````````` example
  3843. - # Foo
  3844. - Bar
  3845. ---
  3846. baz
  3847. .
  3848. <ul>
  3849. <li>
  3850. <h1>Foo</h1>
  3851. </li>
  3852. <li>
  3853. <h2>Bar</h2>
  3854. baz</li>
  3855. </ul>
  3856. ````````````````````````````````
  3857. ### Motivation
  3858. John Gruber's Markdown spec says the following about list items:
  3859. 1. "List markers typically start at the left margin, but may be indented
  3860. by up to three spaces. List markers must be followed by one or more
  3861. spaces or a tab."
  3862. 2. "To make lists look nice, you can wrap items with hanging indents....
  3863. But if you don't want to, you don't have to."
  3864. 3. "List items may consist of multiple paragraphs. Each subsequent
  3865. paragraph in a list item must be indented by either 4 spaces or one
  3866. tab."
  3867. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3868. but here again, Markdown will allow you to be lazy."
  3869. 5. "To put a blockquote within a list item, the blockquote's `>`
  3870. delimiters need to be indented."
  3871. 6. "To put a code block within a list item, the code block needs to be
  3872. indented twice — 8 spaces or two tabs."
  3873. These rules specify that a paragraph under a list item must be indented
  3874. four spaces (presumably, from the left margin, rather than the start of
  3875. the list marker, but this is not said), and that code under a list item
  3876. must be indented eight spaces instead of the usual four. They also say
  3877. that a block quote must be indented, but not by how much; however, the
  3878. example given has four spaces indentation. Although nothing is said
  3879. about other kinds of block-level content, it is certainly reasonable to
  3880. infer that *all* block elements under a list item, including other
  3881. lists, must be indented four spaces. This principle has been called the
  3882. *four-space rule*.
  3883. The four-space rule is clear and principled, and if the reference
  3884. implementation `Markdown.pl` had followed it, it probably would have
  3885. become the standard. However, `Markdown.pl` allowed paragraphs and
  3886. sublists to start with only two spaces indentation, at least on the
  3887. outer level. Worse, its behavior was inconsistent: a sublist of an
  3888. outer-level list needed two spaces indentation, but a sublist of this
  3889. sublist needed three spaces. It is not surprising, then, that different
  3890. implementations of Markdown have developed very different rules for
  3891. determining what comes under a list item. (Pandoc and python-Markdown,
  3892. for example, stuck with Gruber's syntax description and the four-space
  3893. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3894. followed `Markdown.pl`'s behavior more closely.)
  3895. Unfortunately, given the divergences between implementations, there
  3896. is no way to give a spec for list items that will be guaranteed not
  3897. to break any existing documents. However, the spec given here should
  3898. correctly handle lists formatted with either the four-space rule or
  3899. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3900. in a way that is natural for a human to read.
  3901. The strategy here is to let the width and indentation of the list marker
  3902. determine the indentation necessary for blocks to fall under the list
  3903. item, rather than having a fixed and arbitrary number. The writer can
  3904. think of the body of the list item as a unit which gets indented to the
  3905. right enough to fit the list marker (and any indentation on the list
  3906. marker). (The laziness rule, #5, then allows continuation lines to be
  3907. unindented if needed.)
  3908. This rule is superior, we claim, to any rule requiring a fixed level of
  3909. indentation from the margin. The four-space rule is clear but
  3910. unnatural. It is quite unintuitive that
  3911. ``` markdown
  3912. - foo
  3913. bar
  3914. - baz
  3915. ```
  3916. should be parsed as two lists with an intervening paragraph,
  3917. ``` html
  3918. <ul>
  3919. <li>foo</li>
  3920. </ul>
  3921. <p>bar</p>
  3922. <ul>
  3923. <li>baz</li>
  3924. </ul>
  3925. ```
  3926. as the four-space rule demands, rather than a single list,
  3927. ``` html
  3928. <ul>
  3929. <li>
  3930. <p>foo</p>
  3931. <p>bar</p>
  3932. <ul>
  3933. <li>baz</li>
  3934. </ul>
  3935. </li>
  3936. </ul>
  3937. ```
  3938. The choice of four spaces is arbitrary. It can be learned, but it is
  3939. not likely to be guessed, and it trips up beginners regularly.
  3940. Would it help to adopt a two-space rule? The problem is that such
  3941. a rule, together with the rule allowing 1--3 spaces indentation of the
  3942. initial list marker, allows text that is indented *less than* the
  3943. original list marker to be included in the list item. For example,
  3944. `Markdown.pl` parses
  3945. ``` markdown
  3946. - one
  3947. two
  3948. ```
  3949. as a single list item, with `two` a continuation paragraph:
  3950. ``` html
  3951. <ul>
  3952. <li>
  3953. <p>one</p>
  3954. <p>two</p>
  3955. </li>
  3956. </ul>
  3957. ```
  3958. and similarly
  3959. ``` markdown
  3960. > - one
  3961. >
  3962. > two
  3963. ```
  3964. as
  3965. ``` html
  3966. <blockquote>
  3967. <ul>
  3968. <li>
  3969. <p>one</p>
  3970. <p>two</p>
  3971. </li>
  3972. </ul>
  3973. </blockquote>
  3974. ```
  3975. This is extremely unintuitive.
  3976. Rather than requiring a fixed indent from the margin, we could require
  3977. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3978. may itself be indented). This proposal would remove the last anomaly
  3979. discussed. Unlike the spec presented above, it would count the following
  3980. as a list item with a subparagraph, even though the paragraph `bar`
  3981. is not indented as far as the first paragraph `foo`:
  3982. ``` markdown
  3983. 10. foo
  3984. bar
  3985. ```
  3986. Arguably this text does read like a list item with `bar` as a subparagraph,
  3987. which may count in favor of the proposal. However, on this proposal indented
  3988. code would have to be indented six spaces after the list marker. And this
  3989. would break a lot of existing Markdown, which has the pattern:
  3990. ``` markdown
  3991. 1. foo
  3992. indented code
  3993. ```
  3994. where the code is indented eight spaces. The spec above, by contrast, will
  3995. parse this text as expected, since the code block's indentation is measured
  3996. from the beginning of `foo`.
  3997. The one case that needs special treatment is a list item that *starts*
  3998. with indented code. How much indentation is required in that case, since
  3999. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  4000. that in such cases, we require one space indentation from the list marker
  4001. (and then the normal four spaces for the indented code). This will match the
  4002. four-space rule in cases where the list marker plus its initial indentation
  4003. takes four spaces (a common case), but diverge in other cases.
  4004. ## Lists
  4005. A [list](@) is a sequence of one or more
  4006. list items [of the same type]. The list items
  4007. may be separated by any number of blank lines.
  4008. Two list items are [of the same type](@)
  4009. if they begin with a [list marker] of the same type.
  4010. Two list markers are of the
  4011. same type if (a) they are bullet list markers using the same character
  4012. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  4013. delimiter (either `.` or `)`).
  4014. A list is an [ordered list](@)
  4015. if its constituent list items begin with
  4016. [ordered list markers], and a
  4017. [bullet list](@) if its constituent list
  4018. items begin with [bullet list markers].
  4019. The [start number](@)
  4020. of an [ordered list] is determined by the list number of
  4021. its initial list item. The numbers of subsequent list items are
  4022. disregarded.
  4023. A list is [loose](@) if any of its constituent
  4024. list items are separated by blank lines, or if any of its constituent
  4025. list items directly contain two block-level elements with a blank line
  4026. between them. Otherwise a list is [tight](@).
  4027. (The difference in HTML output is that paragraphs in a loose list are
  4028. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  4029. Changing the bullet or ordered list delimiter starts a new list:
  4030. ```````````````````````````````` example
  4031. - foo
  4032. - bar
  4033. + baz
  4034. .
  4035. <ul>
  4036. <li>foo</li>
  4037. <li>bar</li>
  4038. </ul>
  4039. <ul>
  4040. <li>baz</li>
  4041. </ul>
  4042. ````````````````````````````````
  4043. ```````````````````````````````` example
  4044. 1. foo
  4045. 2. bar
  4046. 3) baz
  4047. .
  4048. <ol>
  4049. <li>foo</li>
  4050. <li>bar</li>
  4051. </ol>
  4052. <ol start="3">
  4053. <li>baz</li>
  4054. </ol>
  4055. ````````````````````````````````
  4056. In CommonMark, a list can interrupt a paragraph. That is,
  4057. no blank line is needed to separate a paragraph from a following
  4058. list:
  4059. ```````````````````````````````` example
  4060. Foo
  4061. - bar
  4062. - baz
  4063. .
  4064. <p>Foo</p>
  4065. <ul>
  4066. <li>bar</li>
  4067. <li>baz</li>
  4068. </ul>
  4069. ````````````````````````````````
  4070. `Markdown.pl` does not allow this, through fear of triggering a list
  4071. via a numeral in a hard-wrapped line:
  4072. ``` markdown
  4073. The number of windows in my house is
  4074. 14. The number of doors is 6.
  4075. ```
  4076. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  4077. interrupt a paragraph, even though the same considerations might
  4078. apply.
  4079. In CommonMark, we do allow lists to interrupt paragraphs, for
  4080. two reasons. First, it is natural and not uncommon for people
  4081. to start lists without blank lines:
  4082. ``` markdown
  4083. I need to buy
  4084. - new shoes
  4085. - a coat
  4086. - a plane ticket
  4087. ```
  4088. Second, we are attracted to a
  4089. > [principle of uniformity](@):
  4090. > if a chunk of text has a certain
  4091. > meaning, it will continue to have the same meaning when put into a
  4092. > container block (such as a list item or blockquote).
  4093. (Indeed, the spec for [list items] and [block quotes] presupposes
  4094. this principle.) This principle implies that if
  4095. ``` markdown
  4096. * I need to buy
  4097. - new shoes
  4098. - a coat
  4099. - a plane ticket
  4100. ```
  4101. is a list item containing a paragraph followed by a nested sublist,
  4102. as all Markdown implementations agree it is (though the paragraph
  4103. may be rendered without `<p>` tags, since the list is "tight"),
  4104. then
  4105. ``` markdown
  4106. I need to buy
  4107. - new shoes
  4108. - a coat
  4109. - a plane ticket
  4110. ```
  4111. by itself should be a paragraph followed by a nested sublist.
  4112. Since it is well established Markdown practice to allow lists to
  4113. interrupt paragraphs inside list items, the [principle of
  4114. uniformity] requires us to allow this outside list items as
  4115. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  4116. takes a different approach, requiring blank lines before lists
  4117. even inside other list items.)
  4118. In order to solve of unwanted lists in paragraphs with
  4119. hard-wrapped numerals, we allow only lists starting with `1` to
  4120. interrupt paragraphs. Thus,
  4121. ```````````````````````````````` example
  4122. The number of windows in my house is
  4123. 14. The number of doors is 6.
  4124. .
  4125. <p>The number of windows in my house is
  4126. 14. The number of doors is 6.</p>
  4127. ````````````````````````````````
  4128. We may still get an unintended result in cases like
  4129. ```````````````````````````````` example
  4130. The number of windows in my house is
  4131. 1. The number of doors is 6.
  4132. .
  4133. <p>The number of windows in my house is</p>
  4134. <ol>
  4135. <li>The number of doors is 6.</li>
  4136. </ol>
  4137. ````````````````````````````````
  4138. but this rule should prevent most spurious list captures.
  4139. There can be any number of blank lines between items:
  4140. ```````````````````````````````` example
  4141. - foo
  4142. - bar
  4143. - baz
  4144. .
  4145. <ul>
  4146. <li>
  4147. <p>foo</p>
  4148. </li>
  4149. <li>
  4150. <p>bar</p>
  4151. </li>
  4152. <li>
  4153. <p>baz</p>
  4154. </li>
  4155. </ul>
  4156. ````````````````````````````````
  4157. ```````````````````````````````` example
  4158. - foo
  4159. - bar
  4160. - baz
  4161. bim
  4162. .
  4163. <ul>
  4164. <li>foo
  4165. <ul>
  4166. <li>bar
  4167. <ul>
  4168. <li>
  4169. <p>baz</p>
  4170. <p>bim</p>
  4171. </li>
  4172. </ul>
  4173. </li>
  4174. </ul>
  4175. </li>
  4176. </ul>
  4177. ````````````````````````````````
  4178. To separate consecutive lists of the same type, or to separate a
  4179. list from an indented code block that would otherwise be parsed
  4180. as a subparagraph of the final list item, you can insert a blank HTML
  4181. comment:
  4182. ```````````````````````````````` example
  4183. - foo
  4184. - bar
  4185. <!-- -->
  4186. - baz
  4187. - bim
  4188. .
  4189. <ul>
  4190. <li>foo</li>
  4191. <li>bar</li>
  4192. </ul>
  4193. <!-- -->
  4194. <ul>
  4195. <li>baz</li>
  4196. <li>bim</li>
  4197. </ul>
  4198. ````````````````````````````````
  4199. ```````````````````````````````` example
  4200. - foo
  4201. notcode
  4202. - foo
  4203. <!-- -->
  4204. code
  4205. .
  4206. <ul>
  4207. <li>
  4208. <p>foo</p>
  4209. <p>notcode</p>
  4210. </li>
  4211. <li>
  4212. <p>foo</p>
  4213. </li>
  4214. </ul>
  4215. <!-- -->
  4216. <pre><code>code
  4217. </code></pre>
  4218. ````````````````````````````````
  4219. List items need not be indented to the same level. The following
  4220. list items will be treated as items at the same list level,
  4221. since none is indented enough to belong to the previous list
  4222. item:
  4223. ```````````````````````````````` example
  4224. - a
  4225. - b
  4226. - c
  4227. - d
  4228. - e
  4229. - f
  4230. - g
  4231. .
  4232. <ul>
  4233. <li>a</li>
  4234. <li>b</li>
  4235. <li>c</li>
  4236. <li>d</li>
  4237. <li>e</li>
  4238. <li>f</li>
  4239. <li>g</li>
  4240. </ul>
  4241. ````````````````````````````````
  4242. ```````````````````````````````` example
  4243. 1. a
  4244. 2. b
  4245. 3. c
  4246. .
  4247. <ol>
  4248. <li>
  4249. <p>a</p>
  4250. </li>
  4251. <li>
  4252. <p>b</p>
  4253. </li>
  4254. <li>
  4255. <p>c</p>
  4256. </li>
  4257. </ol>
  4258. ````````````````````````````````
  4259. Note, however, that list items may not be indented more than
  4260. three spaces. Here `- e` is treated as a paragraph continuation
  4261. line, because it is indented more than three spaces:
  4262. ```````````````````````````````` example
  4263. - a
  4264. - b
  4265. - c
  4266. - d
  4267. - e
  4268. .
  4269. <ul>
  4270. <li>a</li>
  4271. <li>b</li>
  4272. <li>c</li>
  4273. <li>d
  4274. - e</li>
  4275. </ul>
  4276. ````````````````````````````````
  4277. And here, `3. c` is treated as in indented code block,
  4278. because it is indented four spaces and preceded by a
  4279. blank line.
  4280. ```````````````````````````````` example
  4281. 1. a
  4282. 2. b
  4283. 3. c
  4284. .
  4285. <ol>
  4286. <li>
  4287. <p>a</p>
  4288. </li>
  4289. <li>
  4290. <p>b</p>
  4291. </li>
  4292. </ol>
  4293. <pre><code>3. c
  4294. </code></pre>
  4295. ````````````````````````````````
  4296. This is a loose list, because there is a blank line between
  4297. two of the list items:
  4298. ```````````````````````````````` example
  4299. - a
  4300. - b
  4301. - c
  4302. .
  4303. <ul>
  4304. <li>
  4305. <p>a</p>
  4306. </li>
  4307. <li>
  4308. <p>b</p>
  4309. </li>
  4310. <li>
  4311. <p>c</p>
  4312. </li>
  4313. </ul>
  4314. ````````````````````````````````
  4315. So is this, with a empty second item:
  4316. ```````````````````````````````` example
  4317. * a
  4318. *
  4319. * c
  4320. .
  4321. <ul>
  4322. <li>
  4323. <p>a</p>
  4324. </li>
  4325. <li></li>
  4326. <li>
  4327. <p>c</p>
  4328. </li>
  4329. </ul>
  4330. ````````````````````````````````
  4331. These are loose lists, even though there is no space between the items,
  4332. because one of the items directly contains two block-level elements
  4333. with a blank line between them:
  4334. ```````````````````````````````` example
  4335. - a
  4336. - b
  4337. c
  4338. - d
  4339. .
  4340. <ul>
  4341. <li>
  4342. <p>a</p>
  4343. </li>
  4344. <li>
  4345. <p>b</p>
  4346. <p>c</p>
  4347. </li>
  4348. <li>
  4349. <p>d</p>
  4350. </li>
  4351. </ul>
  4352. ````````````````````````````````
  4353. ```````````````````````````````` example
  4354. - a
  4355. - b
  4356. [ref]: /url
  4357. - d
  4358. .
  4359. <ul>
  4360. <li>
  4361. <p>a</p>
  4362. </li>
  4363. <li>
  4364. <p>b</p>
  4365. </li>
  4366. <li>
  4367. <p>d</p>
  4368. </li>
  4369. </ul>
  4370. ````````````````````````````````
  4371. This is a tight list, because the blank lines are in a code block:
  4372. ```````````````````````````````` example
  4373. - a
  4374. - ```
  4375. b
  4376. ```
  4377. - c
  4378. .
  4379. <ul>
  4380. <li>a</li>
  4381. <li>
  4382. <pre><code>b
  4383. </code></pre>
  4384. </li>
  4385. <li>c</li>
  4386. </ul>
  4387. ````````````````````````````````
  4388. This is a tight list, because the blank line is between two
  4389. paragraphs of a sublist. So the sublist is loose while
  4390. the outer list is tight:
  4391. ```````````````````````````````` example
  4392. - a
  4393. - b
  4394. c
  4395. - d
  4396. .
  4397. <ul>
  4398. <li>a
  4399. <ul>
  4400. <li>
  4401. <p>b</p>
  4402. <p>c</p>
  4403. </li>
  4404. </ul>
  4405. </li>
  4406. <li>d</li>
  4407. </ul>
  4408. ````````````````````````````````
  4409. This is a tight list, because the blank line is inside the
  4410. block quote:
  4411. ```````````````````````````````` example
  4412. * a
  4413. > b
  4414. >
  4415. * c
  4416. .
  4417. <ul>
  4418. <li>a
  4419. <blockquote>
  4420. <p>b</p>
  4421. </blockquote>
  4422. </li>
  4423. <li>c</li>
  4424. </ul>
  4425. ````````````````````````````````
  4426. This list is tight, because the consecutive block elements
  4427. are not separated by blank lines:
  4428. ```````````````````````````````` example
  4429. - a
  4430. > b
  4431. ```
  4432. c
  4433. ```
  4434. - d
  4435. .
  4436. <ul>
  4437. <li>a
  4438. <blockquote>
  4439. <p>b</p>
  4440. </blockquote>
  4441. <pre><code>c
  4442. </code></pre>
  4443. </li>
  4444. <li>d</li>
  4445. </ul>
  4446. ````````````````````````````````
  4447. A single-paragraph list is tight:
  4448. ```````````````````````````````` example
  4449. - a
  4450. .
  4451. <ul>
  4452. <li>a</li>
  4453. </ul>
  4454. ````````````````````````````````
  4455. ```````````````````````````````` example
  4456. - a
  4457. - b
  4458. .
  4459. <ul>
  4460. <li>a
  4461. <ul>
  4462. <li>b</li>
  4463. </ul>
  4464. </li>
  4465. </ul>
  4466. ````````````````````````````````
  4467. This list is loose, because of the blank line between the
  4468. two block elements in the list item:
  4469. ```````````````````````````````` example
  4470. 1. ```
  4471. foo
  4472. ```
  4473. bar
  4474. .
  4475. <ol>
  4476. <li>
  4477. <pre><code>foo
  4478. </code></pre>
  4479. <p>bar</p>
  4480. </li>
  4481. </ol>
  4482. ````````````````````````````````
  4483. Here the outer list is loose, the inner list tight:
  4484. ```````````````````````````````` example
  4485. * foo
  4486. * bar
  4487. baz
  4488. .
  4489. <ul>
  4490. <li>
  4491. <p>foo</p>
  4492. <ul>
  4493. <li>bar</li>
  4494. </ul>
  4495. <p>baz</p>
  4496. </li>
  4497. </ul>
  4498. ````````````````````````````````
  4499. ```````````````````````````````` example
  4500. - a
  4501. - b
  4502. - c
  4503. - d
  4504. - e
  4505. - f
  4506. .
  4507. <ul>
  4508. <li>
  4509. <p>a</p>
  4510. <ul>
  4511. <li>b</li>
  4512. <li>c</li>
  4513. </ul>
  4514. </li>
  4515. <li>
  4516. <p>d</p>
  4517. <ul>
  4518. <li>e</li>
  4519. <li>f</li>
  4520. </ul>
  4521. </li>
  4522. </ul>
  4523. ````````````````````````````````
  4524. # Inlines
  4525. Inlines are parsed sequentially from the beginning of the character
  4526. stream to the end (left to right, in left-to-right languages).
  4527. Thus, for example, in
  4528. ```````````````````````````````` example
  4529. `hi`lo`
  4530. .
  4531. <p><code>hi</code>lo`</p>
  4532. ````````````````````````````````
  4533. `hi` is parsed as code, leaving the backtick at the end as a literal
  4534. backtick.
  4535. ## Code spans
  4536. A [backtick string](@)
  4537. is a string of one or more backtick characters (`` ` ``) that is neither
  4538. preceded nor followed by a backtick.
  4539. A [code span](@) begins with a backtick string and ends with
  4540. a backtick string of equal length. The contents of the code span are
  4541. the characters between these two backtick strings, normalized in the
  4542. following ways:
  4543. - First, [line endings] are converted to [spaces].
  4544. - If the resulting string both begins *and* ends with a [space]
  4545. character, but does not consist entirely of [space]
  4546. characters, a single [space] character is removed from the
  4547. front and back. This allows you to include code that begins
  4548. or ends with backtick characters, which must be separated by
  4549. whitespace from the opening or closing backtick strings.
  4550. This is a simple code span:
  4551. ```````````````````````````````` example
  4552. `foo`
  4553. .
  4554. <p><code>foo</code></p>
  4555. ````````````````````````````````
  4556. Here two backticks are used, because the code contains a backtick.
  4557. This example also illustrates stripping of a single leading and
  4558. trailing space:
  4559. ```````````````````````````````` example
  4560. `` foo ` bar ``
  4561. .
  4562. <p><code>foo ` bar</code></p>
  4563. ````````````````````````````````
  4564. This example shows the motivation for stripping leading and trailing
  4565. spaces:
  4566. ```````````````````````````````` example
  4567. ` `` `
  4568. .
  4569. <p><code>``</code></p>
  4570. ````````````````````````````````
  4571. Note that only *one* space is stripped:
  4572. ```````````````````````````````` example
  4573. ` `` `
  4574. .
  4575. <p><code> `` </code></p>
  4576. ````````````````````````````````
  4577. The stripping only happens if the space is on both
  4578. sides of the string:
  4579. ```````````````````````````````` example
  4580. ` a`
  4581. .
  4582. <p><code> a</code></p>
  4583. ````````````````````````````````
  4584. Only [spaces], and not [unicode whitespace] in general, are
  4585. stripped in this way:
  4586. ```````````````````````````````` example
  4587. ` b `
  4588. .
  4589. <p><code> b </code></p>
  4590. ````````````````````````````````
  4591. No stripping occurs if the code span contains only spaces:
  4592. ```````````````````````````````` example
  4593. ` `
  4594. ` `
  4595. .
  4596. <p><code> </code>
  4597. <code> </code></p>
  4598. ````````````````````````````````
  4599. [Line endings] are treated like spaces:
  4600. ```````````````````````````````` example
  4601. ``
  4602. foo
  4603. bar
  4604. baz
  4605. ``
  4606. .
  4607. <p><code>foo bar baz</code></p>
  4608. ````````````````````````````````
  4609. ```````````````````````````````` example
  4610. ``
  4611. foo
  4612. ``
  4613. .
  4614. <p><code>foo </code></p>
  4615. ````````````````````````````````
  4616. Interior spaces are not collapsed:
  4617. ```````````````````````````````` example
  4618. `foo bar
  4619. baz`
  4620. .
  4621. <p><code>foo bar baz</code></p>
  4622. ````````````````````````````````
  4623. Note that browsers will typically collapse consecutive spaces
  4624. when rendering `<code>` elements, so it is recommended that
  4625. the following CSS be used:
  4626. code{white-space: pre-wrap;}
  4627. Note that backslash escapes do not work in code spans. All backslashes
  4628. are treated literally:
  4629. ```````````````````````````````` example
  4630. `foo\`bar`
  4631. .
  4632. <p><code>foo\</code>bar`</p>
  4633. ````````````````````````````````
  4634. Backslash escapes are never needed, because one can always choose a
  4635. string of *n* backtick characters as delimiters, where the code does
  4636. not contain any strings of exactly *n* backtick characters.
  4637. ```````````````````````````````` example
  4638. ``foo`bar``
  4639. .
  4640. <p><code>foo`bar</code></p>
  4641. ````````````````````````````````
  4642. ```````````````````````````````` example
  4643. ` foo `` bar `
  4644. .
  4645. <p><code>foo `` bar</code></p>
  4646. ````````````````````````````````
  4647. Code span backticks have higher precedence than any other inline
  4648. constructs except HTML tags and autolinks. Thus, for example, this is
  4649. not parsed as emphasized text, since the second `*` is part of a code
  4650. span:
  4651. ```````````````````````````````` example
  4652. *foo`*`
  4653. .
  4654. <p>*foo<code>*</code></p>
  4655. ````````````````````````````````
  4656. And this is not parsed as a link:
  4657. ```````````````````````````````` example
  4658. [not a `link](/foo`)
  4659. .
  4660. <p>[not a <code>link](/foo</code>)</p>
  4661. ````````````````````````````````
  4662. Code spans, HTML tags, and autolinks have the same precedence.
  4663. Thus, this is code:
  4664. ```````````````````````````````` example
  4665. `<a href="`">`
  4666. .
  4667. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4668. ````````````````````````````````
  4669. But this is an HTML tag:
  4670. ```````````````````````````````` example
  4671. <a href="`">`
  4672. .
  4673. <p><a href="`">`</p>
  4674. ````````````````````````````````
  4675. And this is code:
  4676. ```````````````````````````````` example
  4677. `<http://foo.bar.`baz>`
  4678. .
  4679. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4680. ````````````````````````````````
  4681. But this is an autolink:
  4682. ```````````````````````````````` example
  4683. <http://foo.bar.`baz>`
  4684. .
  4685. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4686. ````````````````````````````````
  4687. When a backtick string is not closed by a matching backtick string,
  4688. we just have literal backticks:
  4689. ```````````````````````````````` example
  4690. ```foo``
  4691. .
  4692. <p>```foo``</p>
  4693. ````````````````````````````````
  4694. ```````````````````````````````` example
  4695. `foo
  4696. .
  4697. <p>`foo</p>
  4698. ````````````````````````````````
  4699. The following case also illustrates the need for opening and
  4700. closing backtick strings to be equal in length:
  4701. ```````````````````````````````` example
  4702. `foo``bar``
  4703. .
  4704. <p>`foo<code>bar</code></p>
  4705. ````````````````````````````````
  4706. ## Emphasis and strong emphasis
  4707. John Gruber's original [Markdown syntax
  4708. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4709. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4710. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4711. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4712. > tag.
  4713. This is enough for most users, but these rules leave much undecided,
  4714. especially when it comes to nested emphasis. The original
  4715. `Markdown.pl` test suite makes it clear that triple `***` and
  4716. `___` delimiters can be used for strong emphasis, and most
  4717. implementations have also allowed the following patterns:
  4718. ``` markdown
  4719. ***strong emph***
  4720. ***strong** in emph*
  4721. ***emph* in strong**
  4722. **in strong *emph***
  4723. *in emph **strong***
  4724. ```
  4725. The following patterns are less widely supported, but the intent
  4726. is clear and they are useful (especially in contexts like bibliography
  4727. entries):
  4728. ``` markdown
  4729. *emph *with emph* in it*
  4730. **strong **with strong** in it**
  4731. ```
  4732. Many implementations have also restricted intraword emphasis to
  4733. the `*` forms, to avoid unwanted emphasis in words containing
  4734. internal underscores. (It is best practice to put these in code
  4735. spans, but users often do not.)
  4736. ``` markdown
  4737. internal emphasis: foo*bar*baz
  4738. no emphasis: foo_bar_baz
  4739. ```
  4740. The rules given below capture all of these patterns, while allowing
  4741. for efficient parsing strategies that do not backtrack.
  4742. First, some definitions. A [delimiter run](@) is either
  4743. a sequence of one or more `*` characters that is not preceded or
  4744. followed by a non-backslash-escaped `*` character, or a sequence
  4745. of one or more `_` characters that is not preceded or followed by
  4746. a non-backslash-escaped `_` character.
  4747. A [left-flanking delimiter run](@) is
  4748. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4749. and either (2a) not followed by a [punctuation character], or
  4750. (2b) followed by a [punctuation character] and
  4751. preceded by [Unicode whitespace] or a [punctuation character].
  4752. For purposes of this definition, the beginning and the end of
  4753. the line count as Unicode whitespace.
  4754. A [right-flanking delimiter run](@) is
  4755. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4756. and either (2a) not preceded by a [punctuation character], or
  4757. (2b) preceded by a [punctuation character] and
  4758. followed by [Unicode whitespace] or a [punctuation character].
  4759. For purposes of this definition, the beginning and the end of
  4760. the line count as Unicode whitespace.
  4761. Here are some examples of delimiter runs.
  4762. - left-flanking but not right-flanking:
  4763. ```
  4764. ***abc
  4765. _abc
  4766. **"abc"
  4767. _"abc"
  4768. ```
  4769. - right-flanking but not left-flanking:
  4770. ```
  4771. abc***
  4772. abc_
  4773. "abc"**
  4774. "abc"_
  4775. ```
  4776. - Both left and right-flanking:
  4777. ```
  4778. abc***def
  4779. "abc"_"def"
  4780. ```
  4781. - Neither left nor right-flanking:
  4782. ```
  4783. abc *** def
  4784. a _ b
  4785. ```
  4786. (The idea of distinguishing left-flanking and right-flanking
  4787. delimiter runs based on the character before and the character
  4788. after comes from Roopesh Chander's
  4789. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4790. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4791. run," and its rules for distinguishing left- and right-flanking runs
  4792. are a bit more complex than the ones given here.)
  4793. The following rules define emphasis and strong emphasis:
  4794. 1. A single `*` character [can open emphasis](@)
  4795. iff (if and only if) it is part of a [left-flanking delimiter run].
  4796. 2. A single `_` character [can open emphasis] iff
  4797. it is part of a [left-flanking delimiter run]
  4798. and either (a) not part of a [right-flanking delimiter run]
  4799. or (b) part of a [right-flanking delimiter run]
  4800. preceded by punctuation.
  4801. 3. A single `*` character [can close emphasis](@)
  4802. iff it is part of a [right-flanking delimiter run].
  4803. 4. A single `_` character [can close emphasis] iff
  4804. it is part of a [right-flanking delimiter run]
  4805. and either (a) not part of a [left-flanking delimiter run]
  4806. or (b) part of a [left-flanking delimiter run]
  4807. followed by punctuation.
  4808. 5. A double `**` [can open strong emphasis](@)
  4809. iff it is part of a [left-flanking delimiter run].
  4810. 6. A double `__` [can open strong emphasis] iff
  4811. it is part of a [left-flanking delimiter run]
  4812. and either (a) not part of a [right-flanking delimiter run]
  4813. or (b) part of a [right-flanking delimiter run]
  4814. preceded by punctuation.
  4815. 7. A double `**` [can close strong emphasis](@)
  4816. iff it is part of a [right-flanking delimiter run].
  4817. 8. A double `__` [can close strong emphasis] iff
  4818. it is part of a [right-flanking delimiter run]
  4819. and either (a) not part of a [left-flanking delimiter run]
  4820. or (b) part of a [left-flanking delimiter run]
  4821. followed by punctuation.
  4822. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4823. with a delimiter that [can close emphasis], and that uses the same
  4824. character (`_` or `*`) as the opening delimiter. The
  4825. opening and closing delimiters must belong to separate
  4826. [delimiter runs]. If one of the delimiters can both
  4827. open and close emphasis, then the sum of the lengths of the
  4828. delimiter runs containing the opening and closing delimiters
  4829. must not be a multiple of 3 unless both lengths are
  4830. multiples of 3.
  4831. 10. Strong emphasis begins with a delimiter that
  4832. [can open strong emphasis] and ends with a delimiter that
  4833. [can close strong emphasis], and that uses the same character
  4834. (`_` or `*`) as the opening delimiter. The
  4835. opening and closing delimiters must belong to separate
  4836. [delimiter runs]. If one of the delimiters can both open
  4837. and close strong emphasis, then the sum of the lengths of
  4838. the delimiter runs containing the opening and closing
  4839. delimiters must not be a multiple of 3 unless both lengths
  4840. are multiples of 3.
  4841. 11. A literal `*` character cannot occur at the beginning or end of
  4842. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4843. is backslash-escaped.
  4844. 12. A literal `_` character cannot occur at the beginning or end of
  4845. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4846. is backslash-escaped.
  4847. Where rules 1--12 above are compatible with multiple parsings,
  4848. the following principles resolve ambiguity:
  4849. 13. The number of nestings should be minimized. Thus, for example,
  4850. an interpretation `<strong>...</strong>` is always preferred to
  4851. `<em><em>...</em></em>`.
  4852. 14. An interpretation `<em><strong>...</strong></em>` is always
  4853. preferred to `<strong><em>...</em></strong>`.
  4854. 15. When two potential emphasis or strong emphasis spans overlap,
  4855. so that the second begins before the first ends and ends after
  4856. the first ends, the first takes precedence. Thus, for example,
  4857. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4858. than `*foo <em>bar* baz</em>`.
  4859. 16. When there are two potential emphasis or strong emphasis spans
  4860. with the same closing delimiter, the shorter one (the one that
  4861. opens later) takes precedence. Thus, for example,
  4862. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4863. rather than `<strong>foo **bar baz</strong>`.
  4864. 17. Inline code spans, links, images, and HTML tags group more tightly
  4865. than emphasis. So, when there is a choice between an interpretation
  4866. that contains one of these elements and one that does not, the
  4867. former always wins. Thus, for example, `*[foo*](bar)` is
  4868. parsed as `*<a href="bar">foo*</a>` rather than as
  4869. `<em>[foo</em>](bar)`.
  4870. These rules can be illustrated through a series of examples.
  4871. Rule 1:
  4872. ```````````````````````````````` example
  4873. *foo bar*
  4874. .
  4875. <p><em>foo bar</em></p>
  4876. ````````````````````````````````
  4877. This is not emphasis, because the opening `*` is followed by
  4878. whitespace, and hence not part of a [left-flanking delimiter run]:
  4879. ```````````````````````````````` example
  4880. a * foo bar*
  4881. .
  4882. <p>a * foo bar*</p>
  4883. ````````````````````````````````
  4884. This is not emphasis, because the opening `*` is preceded
  4885. by an alphanumeric and followed by punctuation, and hence
  4886. not part of a [left-flanking delimiter run]:
  4887. ```````````````````````````````` example
  4888. a*"foo"*
  4889. .
  4890. <p>a*&quot;foo&quot;*</p>
  4891. ````````````````````````````````
  4892. Unicode nonbreaking spaces count as whitespace, too:
  4893. ```````````````````````````````` example
  4894. * a *
  4895. .
  4896. <p>* a *</p>
  4897. ````````````````````````````````
  4898. Intraword emphasis with `*` is permitted:
  4899. ```````````````````````````````` example
  4900. foo*bar*
  4901. .
  4902. <p>foo<em>bar</em></p>
  4903. ````````````````````````````````
  4904. ```````````````````````````````` example
  4905. 5*6*78
  4906. .
  4907. <p>5<em>6</em>78</p>
  4908. ````````````````````````````````
  4909. Rule 2:
  4910. ```````````````````````````````` example
  4911. _foo bar_
  4912. .
  4913. <p><em>foo bar</em></p>
  4914. ````````````````````````````````
  4915. This is not emphasis, because the opening `_` is followed by
  4916. whitespace:
  4917. ```````````````````````````````` example
  4918. _ foo bar_
  4919. .
  4920. <p>_ foo bar_</p>
  4921. ````````````````````````````````
  4922. This is not emphasis, because the opening `_` is preceded
  4923. by an alphanumeric and followed by punctuation:
  4924. ```````````````````````````````` example
  4925. a_"foo"_
  4926. .
  4927. <p>a_&quot;foo&quot;_</p>
  4928. ````````````````````````````````
  4929. Emphasis with `_` is not allowed inside words:
  4930. ```````````````````````````````` example
  4931. foo_bar_
  4932. .
  4933. <p>foo_bar_</p>
  4934. ````````````````````````````````
  4935. ```````````````````````````````` example
  4936. 5_6_78
  4937. .
  4938. <p>5_6_78</p>
  4939. ````````````````````````````````
  4940. ```````````````````````````````` example
  4941. пристаням_стремятся_
  4942. .
  4943. <p>пристаням_стремятся_</p>
  4944. ````````````````````````````````
  4945. Here `_` does not generate emphasis, because the first delimiter run
  4946. is right-flanking and the second left-flanking:
  4947. ```````````````````````````````` example
  4948. aa_"bb"_cc
  4949. .
  4950. <p>aa_&quot;bb&quot;_cc</p>
  4951. ````````````````````````````````
  4952. This is emphasis, even though the opening delimiter is
  4953. both left- and right-flanking, because it is preceded by
  4954. punctuation:
  4955. ```````````````````````````````` example
  4956. foo-_(bar)_
  4957. .
  4958. <p>foo-<em>(bar)</em></p>
  4959. ````````````````````````````````
  4960. Rule 3:
  4961. This is not emphasis, because the closing delimiter does
  4962. not match the opening delimiter:
  4963. ```````````````````````````````` example
  4964. _foo*
  4965. .
  4966. <p>_foo*</p>
  4967. ````````````````````````````````
  4968. This is not emphasis, because the closing `*` is preceded by
  4969. whitespace:
  4970. ```````````````````````````````` example
  4971. *foo bar *
  4972. .
  4973. <p>*foo bar *</p>
  4974. ````````````````````````````````
  4975. A newline also counts as whitespace:
  4976. ```````````````````````````````` example
  4977. *foo bar
  4978. *
  4979. .
  4980. <p>*foo bar
  4981. *</p>
  4982. ````````````````````````````````
  4983. This is not emphasis, because the second `*` is
  4984. preceded by punctuation and followed by an alphanumeric
  4985. (hence it is not part of a [right-flanking delimiter run]:
  4986. ```````````````````````````````` example
  4987. *(*foo)
  4988. .
  4989. <p>*(*foo)</p>
  4990. ````````````````````````````````
  4991. The point of this restriction is more easily appreciated
  4992. with this example:
  4993. ```````````````````````````````` example
  4994. *(*foo*)*
  4995. .
  4996. <p><em>(<em>foo</em>)</em></p>
  4997. ````````````````````````````````
  4998. Intraword emphasis with `*` is allowed:
  4999. ```````````````````````````````` example
  5000. *foo*bar
  5001. .
  5002. <p><em>foo</em>bar</p>
  5003. ````````````````````````````````
  5004. Rule 4:
  5005. This is not emphasis, because the closing `_` is preceded by
  5006. whitespace:
  5007. ```````````````````````````````` example
  5008. _foo bar _
  5009. .
  5010. <p>_foo bar _</p>
  5011. ````````````````````````````````
  5012. This is not emphasis, because the second `_` is
  5013. preceded by punctuation and followed by an alphanumeric:
  5014. ```````````````````````````````` example
  5015. _(_foo)
  5016. .
  5017. <p>_(_foo)</p>
  5018. ````````````````````````````````
  5019. This is emphasis within emphasis:
  5020. ```````````````````````````````` example
  5021. _(_foo_)_
  5022. .
  5023. <p><em>(<em>foo</em>)</em></p>
  5024. ````````````````````````````````
  5025. Intraword emphasis is disallowed for `_`:
  5026. ```````````````````````````````` example
  5027. _foo_bar
  5028. .
  5029. <p>_foo_bar</p>
  5030. ````````````````````````````````
  5031. ```````````````````````````````` example
  5032. _пристаням_стремятся
  5033. .
  5034. <p>_пристаням_стремятся</p>
  5035. ````````````````````````````````
  5036. ```````````````````````````````` example
  5037. _foo_bar_baz_
  5038. .
  5039. <p><em>foo_bar_baz</em></p>
  5040. ````````````````````````````````
  5041. This is emphasis, even though the closing delimiter is
  5042. both left- and right-flanking, because it is followed by
  5043. punctuation:
  5044. ```````````````````````````````` example
  5045. _(bar)_.
  5046. .
  5047. <p><em>(bar)</em>.</p>
  5048. ````````````````````````````````
  5049. Rule 5:
  5050. ```````````````````````````````` example
  5051. **foo bar**
  5052. .
  5053. <p><strong>foo bar</strong></p>
  5054. ````````````````````````````````
  5055. This is not strong emphasis, because the opening delimiter is
  5056. followed by whitespace:
  5057. ```````````````````````````````` example
  5058. ** foo bar**
  5059. .
  5060. <p>** foo bar**</p>
  5061. ````````````````````````````````
  5062. This is not strong emphasis, because the opening `**` is preceded
  5063. by an alphanumeric and followed by punctuation, and hence
  5064. not part of a [left-flanking delimiter run]:
  5065. ```````````````````````````````` example
  5066. a**"foo"**
  5067. .
  5068. <p>a**&quot;foo&quot;**</p>
  5069. ````````````````````````````````
  5070. Intraword strong emphasis with `**` is permitted:
  5071. ```````````````````````````````` example
  5072. foo**bar**
  5073. .
  5074. <p>foo<strong>bar</strong></p>
  5075. ````````````````````````````````
  5076. Rule 6:
  5077. ```````````````````````````````` example
  5078. __foo bar__
  5079. .
  5080. <p><strong>foo bar</strong></p>
  5081. ````````````````````````````````
  5082. This is not strong emphasis, because the opening delimiter is
  5083. followed by whitespace:
  5084. ```````````````````````````````` example
  5085. __ foo bar__
  5086. .
  5087. <p>__ foo bar__</p>
  5088. ````````````````````````````````
  5089. A newline counts as whitespace:
  5090. ```````````````````````````````` example
  5091. __
  5092. foo bar__
  5093. .
  5094. <p>__
  5095. foo bar__</p>
  5096. ````````````````````````````````
  5097. This is not strong emphasis, because the opening `__` is preceded
  5098. by an alphanumeric and followed by punctuation:
  5099. ```````````````````````````````` example
  5100. a__"foo"__
  5101. .
  5102. <p>a__&quot;foo&quot;__</p>
  5103. ````````````````````````````````
  5104. Intraword strong emphasis is forbidden with `__`:
  5105. ```````````````````````````````` example
  5106. foo__bar__
  5107. .
  5108. <p>foo__bar__</p>
  5109. ````````````````````````````````
  5110. ```````````````````````````````` example
  5111. 5__6__78
  5112. .
  5113. <p>5__6__78</p>
  5114. ````````````````````````````````
  5115. ```````````````````````````````` example
  5116. пристаням__стремятся__
  5117. .
  5118. <p>пристаням__стремятся__</p>
  5119. ````````````````````````````````
  5120. ```````````````````````````````` example
  5121. __foo, __bar__, baz__
  5122. .
  5123. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5124. ````````````````````````````````
  5125. This is strong emphasis, even though the opening delimiter is
  5126. both left- and right-flanking, because it is preceded by
  5127. punctuation:
  5128. ```````````````````````````````` example
  5129. foo-__(bar)__
  5130. .
  5131. <p>foo-<strong>(bar)</strong></p>
  5132. ````````````````````````````````
  5133. Rule 7:
  5134. This is not strong emphasis, because the closing delimiter is preceded
  5135. by whitespace:
  5136. ```````````````````````````````` example
  5137. **foo bar **
  5138. .
  5139. <p>**foo bar **</p>
  5140. ````````````````````````````````
  5141. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5142. Rule 11.)
  5143. This is not strong emphasis, because the second `**` is
  5144. preceded by punctuation and followed by an alphanumeric:
  5145. ```````````````````````````````` example
  5146. **(**foo)
  5147. .
  5148. <p>**(**foo)</p>
  5149. ````````````````````````````````
  5150. The point of this restriction is more easily appreciated
  5151. with these examples:
  5152. ```````````````````````````````` example
  5153. *(**foo**)*
  5154. .
  5155. <p><em>(<strong>foo</strong>)</em></p>
  5156. ````````````````````````````````
  5157. ```````````````````````````````` example
  5158. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5159. *Asclepias physocarpa*)**
  5160. .
  5161. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5162. <em>Asclepias physocarpa</em>)</strong></p>
  5163. ````````````````````````````````
  5164. ```````````````````````````````` example
  5165. **foo "*bar*" foo**
  5166. .
  5167. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5168. ````````````````````````````````
  5169. Intraword emphasis:
  5170. ```````````````````````````````` example
  5171. **foo**bar
  5172. .
  5173. <p><strong>foo</strong>bar</p>
  5174. ````````````````````````````````
  5175. Rule 8:
  5176. This is not strong emphasis, because the closing delimiter is
  5177. preceded by whitespace:
  5178. ```````````````````````````````` example
  5179. __foo bar __
  5180. .
  5181. <p>__foo bar __</p>
  5182. ````````````````````````````````
  5183. This is not strong emphasis, because the second `__` is
  5184. preceded by punctuation and followed by an alphanumeric:
  5185. ```````````````````````````````` example
  5186. __(__foo)
  5187. .
  5188. <p>__(__foo)</p>
  5189. ````````````````````````````````
  5190. The point of this restriction is more easily appreciated
  5191. with this example:
  5192. ```````````````````````````````` example
  5193. _(__foo__)_
  5194. .
  5195. <p><em>(<strong>foo</strong>)</em></p>
  5196. ````````````````````````````````
  5197. Intraword strong emphasis is forbidden with `__`:
  5198. ```````````````````````````````` example
  5199. __foo__bar
  5200. .
  5201. <p>__foo__bar</p>
  5202. ````````````````````````````````
  5203. ```````````````````````````````` example
  5204. __пристаням__стремятся
  5205. .
  5206. <p>__пристаням__стремятся</p>
  5207. ````````````````````````````````
  5208. ```````````````````````````````` example
  5209. __foo__bar__baz__
  5210. .
  5211. <p><strong>foo__bar__baz</strong></p>
  5212. ````````````````````````````````
  5213. This is strong emphasis, even though the closing delimiter is
  5214. both left- and right-flanking, because it is followed by
  5215. punctuation:
  5216. ```````````````````````````````` example
  5217. __(bar)__.
  5218. .
  5219. <p><strong>(bar)</strong>.</p>
  5220. ````````````````````````````````
  5221. Rule 9:
  5222. Any nonempty sequence of inline elements can be the contents of an
  5223. emphasized span.
  5224. ```````````````````````````````` example
  5225. *foo [bar](/url)*
  5226. .
  5227. <p><em>foo <a href="/url">bar</a></em></p>
  5228. ````````````````````````````````
  5229. ```````````````````````````````` example
  5230. *foo
  5231. bar*
  5232. .
  5233. <p><em>foo
  5234. bar</em></p>
  5235. ````````````````````````````````
  5236. In particular, emphasis and strong emphasis can be nested
  5237. inside emphasis:
  5238. ```````````````````````````````` example
  5239. _foo __bar__ baz_
  5240. .
  5241. <p><em>foo <strong>bar</strong> baz</em></p>
  5242. ````````````````````````````````
  5243. ```````````````````````````````` example
  5244. _foo _bar_ baz_
  5245. .
  5246. <p><em>foo <em>bar</em> baz</em></p>
  5247. ````````````````````````````````
  5248. ```````````````````````````````` example
  5249. __foo_ bar_
  5250. .
  5251. <p><em><em>foo</em> bar</em></p>
  5252. ````````````````````````````````
  5253. ```````````````````````````````` example
  5254. *foo *bar**
  5255. .
  5256. <p><em>foo <em>bar</em></em></p>
  5257. ````````````````````````````````
  5258. ```````````````````````````````` example
  5259. *foo **bar** baz*
  5260. .
  5261. <p><em>foo <strong>bar</strong> baz</em></p>
  5262. ````````````````````````````````
  5263. ```````````````````````````````` example
  5264. *foo**bar**baz*
  5265. .
  5266. <p><em>foo<strong>bar</strong>baz</em></p>
  5267. ````````````````````````````````
  5268. Note that in the preceding case, the interpretation
  5269. ``` markdown
  5270. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5271. ```
  5272. is precluded by the condition that a delimiter that
  5273. can both open and close (like the `*` after `foo`)
  5274. cannot form emphasis if the sum of the lengths of
  5275. the delimiter runs containing the opening and
  5276. closing delimiters is a multiple of 3 unless
  5277. both lengths are multiples of 3.
  5278. For the same reason, we don't get two consecutive
  5279. emphasis sections in this example:
  5280. ```````````````````````````````` example
  5281. *foo**bar*
  5282. .
  5283. <p><em>foo**bar</em></p>
  5284. ````````````````````````````````
  5285. The same condition ensures that the following
  5286. cases are all strong emphasis nested inside
  5287. emphasis, even when the interior spaces are
  5288. omitted:
  5289. ```````````````````````````````` example
  5290. ***foo** bar*
  5291. .
  5292. <p><em><strong>foo</strong> bar</em></p>
  5293. ````````````````````````````````
  5294. ```````````````````````````````` example
  5295. *foo **bar***
  5296. .
  5297. <p><em>foo <strong>bar</strong></em></p>
  5298. ````````````````````````````````
  5299. ```````````````````````````````` example
  5300. *foo**bar***
  5301. .
  5302. <p><em>foo<strong>bar</strong></em></p>
  5303. ````````````````````````````````
  5304. When the lengths of the interior closing and opening
  5305. delimiter runs are *both* multiples of 3, though,
  5306. they can match to create emphasis:
  5307. ```````````````````````````````` example
  5308. foo***bar***baz
  5309. .
  5310. <p>foo<em><strong>bar</strong></em>baz</p>
  5311. ````````````````````````````````
  5312. ```````````````````````````````` example
  5313. foo******bar*********baz
  5314. .
  5315. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5316. ````````````````````````````````
  5317. Indefinite levels of nesting are possible:
  5318. ```````````````````````````````` example
  5319. *foo **bar *baz* bim** bop*
  5320. .
  5321. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5322. ````````````````````````````````
  5323. ```````````````````````````````` example
  5324. *foo [*bar*](/url)*
  5325. .
  5326. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5327. ````````````````````````````````
  5328. There can be no empty emphasis or strong emphasis:
  5329. ```````````````````````````````` example
  5330. ** is not an empty emphasis
  5331. .
  5332. <p>** is not an empty emphasis</p>
  5333. ````````````````````````````````
  5334. ```````````````````````````````` example
  5335. **** is not an empty strong emphasis
  5336. .
  5337. <p>**** is not an empty strong emphasis</p>
  5338. ````````````````````````````````
  5339. Rule 10:
  5340. Any nonempty sequence of inline elements can be the contents of an
  5341. strongly emphasized span.
  5342. ```````````````````````````````` example
  5343. **foo [bar](/url)**
  5344. .
  5345. <p><strong>foo <a href="/url">bar</a></strong></p>
  5346. ````````````````````````````````
  5347. ```````````````````````````````` example
  5348. **foo
  5349. bar**
  5350. .
  5351. <p><strong>foo
  5352. bar</strong></p>
  5353. ````````````````````````````````
  5354. In particular, emphasis and strong emphasis can be nested
  5355. inside strong emphasis:
  5356. ```````````````````````````````` example
  5357. __foo _bar_ baz__
  5358. .
  5359. <p><strong>foo <em>bar</em> baz</strong></p>
  5360. ````````````````````````````````
  5361. ```````````````````````````````` example
  5362. __foo __bar__ baz__
  5363. .
  5364. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5365. ````````````````````````````````
  5366. ```````````````````````````````` example
  5367. ____foo__ bar__
  5368. .
  5369. <p><strong><strong>foo</strong> bar</strong></p>
  5370. ````````````````````````````````
  5371. ```````````````````````````````` example
  5372. **foo **bar****
  5373. .
  5374. <p><strong>foo <strong>bar</strong></strong></p>
  5375. ````````````````````````````````
  5376. ```````````````````````````````` example
  5377. **foo *bar* baz**
  5378. .
  5379. <p><strong>foo <em>bar</em> baz</strong></p>
  5380. ````````````````````````````````
  5381. ```````````````````````````````` example
  5382. **foo*bar*baz**
  5383. .
  5384. <p><strong>foo<em>bar</em>baz</strong></p>
  5385. ````````````````````````````````
  5386. ```````````````````````````````` example
  5387. ***foo* bar**
  5388. .
  5389. <p><strong><em>foo</em> bar</strong></p>
  5390. ````````````````````````````````
  5391. ```````````````````````````````` example
  5392. **foo *bar***
  5393. .
  5394. <p><strong>foo <em>bar</em></strong></p>
  5395. ````````````````````````````````
  5396. Indefinite levels of nesting are possible:
  5397. ```````````````````````````````` example
  5398. **foo *bar **baz**
  5399. bim* bop**
  5400. .
  5401. <p><strong>foo <em>bar <strong>baz</strong>
  5402. bim</em> bop</strong></p>
  5403. ````````````````````````````````
  5404. ```````````````````````````````` example
  5405. **foo [*bar*](/url)**
  5406. .
  5407. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5408. ````````````````````````````````
  5409. There can be no empty emphasis or strong emphasis:
  5410. ```````````````````````````````` example
  5411. __ is not an empty emphasis
  5412. .
  5413. <p>__ is not an empty emphasis</p>
  5414. ````````````````````````````````
  5415. ```````````````````````````````` example
  5416. ____ is not an empty strong emphasis
  5417. .
  5418. <p>____ is not an empty strong emphasis</p>
  5419. ````````````````````````````````
  5420. Rule 11:
  5421. ```````````````````````````````` example
  5422. foo ***
  5423. .
  5424. <p>foo ***</p>
  5425. ````````````````````````````````
  5426. ```````````````````````````````` example
  5427. foo *\**
  5428. .
  5429. <p>foo <em>*</em></p>
  5430. ````````````````````````````````
  5431. ```````````````````````````````` example
  5432. foo *_*
  5433. .
  5434. <p>foo <em>_</em></p>
  5435. ````````````````````````````````
  5436. ```````````````````````````````` example
  5437. foo *****
  5438. .
  5439. <p>foo *****</p>
  5440. ````````````````````````````````
  5441. ```````````````````````````````` example
  5442. foo **\***
  5443. .
  5444. <p>foo <strong>*</strong></p>
  5445. ````````````````````````````````
  5446. ```````````````````````````````` example
  5447. foo **_**
  5448. .
  5449. <p>foo <strong>_</strong></p>
  5450. ````````````````````````````````
  5451. Note that when delimiters do not match evenly, Rule 11 determines
  5452. that the excess literal `*` characters will appear outside of the
  5453. emphasis, rather than inside it:
  5454. ```````````````````````````````` example
  5455. **foo*
  5456. .
  5457. <p>*<em>foo</em></p>
  5458. ````````````````````````````````
  5459. ```````````````````````````````` example
  5460. *foo**
  5461. .
  5462. <p><em>foo</em>*</p>
  5463. ````````````````````````````````
  5464. ```````````````````````````````` example
  5465. ***foo**
  5466. .
  5467. <p>*<strong>foo</strong></p>
  5468. ````````````````````````````````
  5469. ```````````````````````````````` example
  5470. ****foo*
  5471. .
  5472. <p>***<em>foo</em></p>
  5473. ````````````````````````````````
  5474. ```````````````````````````````` example
  5475. **foo***
  5476. .
  5477. <p><strong>foo</strong>*</p>
  5478. ````````````````````````````````
  5479. ```````````````````````````````` example
  5480. *foo****
  5481. .
  5482. <p><em>foo</em>***</p>
  5483. ````````````````````````````````
  5484. Rule 12:
  5485. ```````````````````````````````` example
  5486. foo ___
  5487. .
  5488. <p>foo ___</p>
  5489. ````````````````````````````````
  5490. ```````````````````````````````` example
  5491. foo _\__
  5492. .
  5493. <p>foo <em>_</em></p>
  5494. ````````````````````````````````
  5495. ```````````````````````````````` example
  5496. foo _*_
  5497. .
  5498. <p>foo <em>*</em></p>
  5499. ````````````````````````````````
  5500. ```````````````````````````````` example
  5501. foo _____
  5502. .
  5503. <p>foo _____</p>
  5504. ````````````````````````````````
  5505. ```````````````````````````````` example
  5506. foo __\___
  5507. .
  5508. <p>foo <strong>_</strong></p>
  5509. ````````````````````````````````
  5510. ```````````````````````````````` example
  5511. foo __*__
  5512. .
  5513. <p>foo <strong>*</strong></p>
  5514. ````````````````````````````````
  5515. ```````````````````````````````` example
  5516. __foo_
  5517. .
  5518. <p>_<em>foo</em></p>
  5519. ````````````````````````````````
  5520. Note that when delimiters do not match evenly, Rule 12 determines
  5521. that the excess literal `_` characters will appear outside of the
  5522. emphasis, rather than inside it:
  5523. ```````````````````````````````` example
  5524. _foo__
  5525. .
  5526. <p><em>foo</em>_</p>
  5527. ````````````````````````````````
  5528. ```````````````````````````````` example
  5529. ___foo__
  5530. .
  5531. <p>_<strong>foo</strong></p>
  5532. ````````````````````````````````
  5533. ```````````````````````````````` example
  5534. ____foo_
  5535. .
  5536. <p>___<em>foo</em></p>
  5537. ````````````````````````````````
  5538. ```````````````````````````````` example
  5539. __foo___
  5540. .
  5541. <p><strong>foo</strong>_</p>
  5542. ````````````````````````````````
  5543. ```````````````````````````````` example
  5544. _foo____
  5545. .
  5546. <p><em>foo</em>___</p>
  5547. ````````````````````````````````
  5548. Rule 13 implies that if you want emphasis nested directly inside
  5549. emphasis, you must use different delimiters:
  5550. ```````````````````````````````` example
  5551. **foo**
  5552. .
  5553. <p><strong>foo</strong></p>
  5554. ````````````````````````````````
  5555. ```````````````````````````````` example
  5556. *_foo_*
  5557. .
  5558. <p><em><em>foo</em></em></p>
  5559. ````````````````````````````````
  5560. ```````````````````````````````` example
  5561. __foo__
  5562. .
  5563. <p><strong>foo</strong></p>
  5564. ````````````````````````````````
  5565. ```````````````````````````````` example
  5566. _*foo*_
  5567. .
  5568. <p><em><em>foo</em></em></p>
  5569. ````````````````````````````````
  5570. However, strong emphasis within strong emphasis is possible without
  5571. switching delimiters:
  5572. ```````````````````````````````` example
  5573. ****foo****
  5574. .
  5575. <p><strong><strong>foo</strong></strong></p>
  5576. ````````````````````````````````
  5577. ```````````````````````````````` example
  5578. ____foo____
  5579. .
  5580. <p><strong><strong>foo</strong></strong></p>
  5581. ````````````````````````````````
  5582. Rule 13 can be applied to arbitrarily long sequences of
  5583. delimiters:
  5584. ```````````````````````````````` example
  5585. ******foo******
  5586. .
  5587. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5588. ````````````````````````````````
  5589. Rule 14:
  5590. ```````````````````````````````` example
  5591. ***foo***
  5592. .
  5593. <p><em><strong>foo</strong></em></p>
  5594. ````````````````````````````````
  5595. ```````````````````````````````` example
  5596. _____foo_____
  5597. .
  5598. <p><em><strong><strong>foo</strong></strong></em></p>
  5599. ````````````````````````````````
  5600. Rule 15:
  5601. ```````````````````````````````` example
  5602. *foo _bar* baz_
  5603. .
  5604. <p><em>foo _bar</em> baz_</p>
  5605. ````````````````````````````````
  5606. ```````````````````````````````` example
  5607. *foo __bar *baz bim__ bam*
  5608. .
  5609. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5610. ````````````````````````````````
  5611. Rule 16:
  5612. ```````````````````````````````` example
  5613. **foo **bar baz**
  5614. .
  5615. <p>**foo <strong>bar baz</strong></p>
  5616. ````````````````````````````````
  5617. ```````````````````````````````` example
  5618. *foo *bar baz*
  5619. .
  5620. <p>*foo <em>bar baz</em></p>
  5621. ````````````````````````````````
  5622. Rule 17:
  5623. ```````````````````````````````` example
  5624. *[bar*](/url)
  5625. .
  5626. <p>*<a href="/url">bar*</a></p>
  5627. ````````````````````````````````
  5628. ```````````````````````````````` example
  5629. _foo [bar_](/url)
  5630. .
  5631. <p>_foo <a href="/url">bar_</a></p>
  5632. ````````````````````````````````
  5633. ```````````````````````````````` example
  5634. *<img src="foo" title="*"/>
  5635. .
  5636. <p>*<img src="foo" title="*"/></p>
  5637. ````````````````````````````````
  5638. ```````````````````````````````` example
  5639. **<a href="**">
  5640. .
  5641. <p>**<a href="**"></p>
  5642. ````````````````````````````````
  5643. ```````````````````````````````` example
  5644. __<a href="__">
  5645. .
  5646. <p>__<a href="__"></p>
  5647. ````````````````````````````````
  5648. ```````````````````````````````` example
  5649. *a `*`*
  5650. .
  5651. <p><em>a <code>*</code></em></p>
  5652. ````````````````````````````````
  5653. ```````````````````````````````` example
  5654. _a `_`_
  5655. .
  5656. <p><em>a <code>_</code></em></p>
  5657. ````````````````````````````````
  5658. ```````````````````````````````` example
  5659. **a<http://foo.bar/?q=**>
  5660. .
  5661. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5662. ````````````````````````````````
  5663. ```````````````````````````````` example
  5664. __a<http://foo.bar/?q=__>
  5665. .
  5666. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5667. ````````````````````````````````
  5668. ## Links
  5669. A link contains [link text] (the visible text), a [link destination]
  5670. (the URI that is the link destination), and optionally a [link title].
  5671. There are two basic kinds of links in Markdown. In [inline links] the
  5672. destination and title are given immediately after the link text. In
  5673. [reference links] the destination and title are defined elsewhere in
  5674. the document.
  5675. A [link text](@) consists of a sequence of zero or more
  5676. inline elements enclosed by square brackets (`[` and `]`). The
  5677. following rules apply:
  5678. - Links may not contain other links, at any level of nesting. If
  5679. multiple otherwise valid link definitions appear nested inside each
  5680. other, the inner-most definition is used.
  5681. - Brackets are allowed in the [link text] only if (a) they
  5682. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5683. with an open bracket `[`, a sequence of zero or more inlines, and
  5684. a close bracket `]`.
  5685. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5686. than the brackets in link text. Thus, for example,
  5687. `` [foo`]` `` could not be a link text, since the second `]`
  5688. is part of a code span.
  5689. - The brackets in link text bind more tightly than markers for
  5690. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5691. A [link destination](@) consists of either
  5692. - a sequence of zero or more characters between an opening `<` and a
  5693. closing `>` that contains no line breaks or unescaped
  5694. `<` or `>` characters, or
  5695. - a nonempty sequence of characters that does not start with
  5696. `<`, does not include ASCII space or control characters, and
  5697. includes parentheses only if (a) they are backslash-escaped or
  5698. (b) they are part of a balanced pair of unescaped parentheses.
  5699. (Implementations may impose limits on parentheses nesting to
  5700. avoid performance issues, but at least three levels of nesting
  5701. should be supported.)
  5702. A [link title](@) consists of either
  5703. - a sequence of zero or more characters between straight double-quote
  5704. characters (`"`), including a `"` character only if it is
  5705. backslash-escaped, or
  5706. - a sequence of zero or more characters between straight single-quote
  5707. characters (`'`), including a `'` character only if it is
  5708. backslash-escaped, or
  5709. - a sequence of zero or more characters between matching parentheses
  5710. (`(...)`), including a `(` or `)` character only if it is
  5711. backslash-escaped.
  5712. Although [link titles] may span multiple lines, they may not contain
  5713. a [blank line].
  5714. An [inline link](@) consists of a [link text] followed immediately
  5715. by a left parenthesis `(`, optional [whitespace], an optional
  5716. [link destination], an optional [link title] separated from the link
  5717. destination by [whitespace], optional [whitespace], and a right
  5718. parenthesis `)`. The link's text consists of the inlines contained
  5719. in the [link text] (excluding the enclosing square brackets).
  5720. The link's URI consists of the link destination, excluding enclosing
  5721. `<...>` if present, with backslash-escapes in effect as described
  5722. above. The link's title consists of the link title, excluding its
  5723. enclosing delimiters, with backslash-escapes in effect as described
  5724. above.
  5725. Here is a simple inline link:
  5726. ```````````````````````````````` example
  5727. [link](/uri "title")
  5728. .
  5729. <p><a href="/uri" title="title">link</a></p>
  5730. ````````````````````````````````
  5731. The title may be omitted:
  5732. ```````````````````````````````` example
  5733. [link](/uri)
  5734. .
  5735. <p><a href="/uri">link</a></p>
  5736. ````````````````````````````````
  5737. Both the title and the destination may be omitted:
  5738. ```````````````````````````````` example
  5739. [link]()
  5740. .
  5741. <p><a href="">link</a></p>
  5742. ````````````````````````````````
  5743. ```````````````````````````````` example
  5744. [link](<>)
  5745. .
  5746. <p><a href="">link</a></p>
  5747. ````````````````````````````````
  5748. The destination can only contain spaces if it is
  5749. enclosed in pointy brackets:
  5750. ```````````````````````````````` example
  5751. [link](/my uri)
  5752. .
  5753. <p>[link](/my uri)</p>
  5754. ````````````````````````````````
  5755. ```````````````````````````````` example
  5756. [link](</my uri>)
  5757. .
  5758. <p><a href="/my%20uri">link</a></p>
  5759. ````````````````````````````````
  5760. The destination cannot contain line breaks,
  5761. even if enclosed in pointy brackets:
  5762. ```````````````````````````````` example
  5763. [link](foo
  5764. bar)
  5765. .
  5766. <p>[link](foo
  5767. bar)</p>
  5768. ````````````````````````````````
  5769. ```````````````````````````````` example
  5770. [link](<foo
  5771. bar>)
  5772. .
  5773. <p>[link](<foo
  5774. bar>)</p>
  5775. ````````````````````````````````
  5776. The destination can contain `)` if it is enclosed
  5777. in pointy brackets:
  5778. ```````````````````````````````` example
  5779. [a](<b)c>)
  5780. .
  5781. <p><a href="b)c">a</a></p>
  5782. ````````````````````````````````
  5783. Pointy brackets that enclose links must be unescaped:
  5784. ```````````````````````````````` example
  5785. [link](<foo\>)
  5786. .
  5787. <p>[link](&lt;foo&gt;)</p>
  5788. ````````````````````````````````
  5789. These are not links, because the opening pointy bracket
  5790. is not matched properly:
  5791. ```````````````````````````````` example
  5792. [a](<b)c
  5793. [a](<b)c>
  5794. [a](<b>c)
  5795. .
  5796. <p>[a](&lt;b)c
  5797. [a](&lt;b)c&gt;
  5798. [a](<b>c)</p>
  5799. ````````````````````````````````
  5800. Parentheses inside the link destination may be escaped:
  5801. ```````````````````````````````` example
  5802. [link](\(foo\))
  5803. .
  5804. <p><a href="(foo)">link</a></p>
  5805. ````````````````````````````````
  5806. Any number of parentheses are allowed without escaping, as long as they are
  5807. balanced:
  5808. ```````````````````````````````` example
  5809. [link](foo(and(bar)))
  5810. .
  5811. <p><a href="foo(and(bar))">link</a></p>
  5812. ````````````````````````````````
  5813. However, if you have unbalanced parentheses, you need to escape or use the
  5814. `<...>` form:
  5815. ```````````````````````````````` example
  5816. [link](foo\(and\(bar\))
  5817. .
  5818. <p><a href="foo(and(bar)">link</a></p>
  5819. ````````````````````````````````
  5820. ```````````````````````````````` example
  5821. [link](<foo(and(bar)>)
  5822. .
  5823. <p><a href="foo(and(bar)">link</a></p>
  5824. ````````````````````````````````
  5825. Parentheses and other symbols can also be escaped, as usual
  5826. in Markdown:
  5827. ```````````````````````````````` example
  5828. [link](foo\)\:)
  5829. .
  5830. <p><a href="foo):">link</a></p>
  5831. ````````````````````````````````
  5832. A link can contain fragment identifiers and queries:
  5833. ```````````````````````````````` example
  5834. [link](#fragment)
  5835. [link](http://example.com#fragment)
  5836. [link](http://example.com?foo=3#frag)
  5837. .
  5838. <p><a href="#fragment">link</a></p>
  5839. <p><a href="http://example.com#fragment">link</a></p>
  5840. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5841. ````````````````````````````````
  5842. Note that a backslash before a non-escapable character is
  5843. just a backslash:
  5844. ```````````````````````````````` example
  5845. [link](foo\bar)
  5846. .
  5847. <p><a href="foo%5Cbar">link</a></p>
  5848. ````````````````````````````````
  5849. URL-escaping should be left alone inside the destination, as all
  5850. URL-escaped characters are also valid URL characters. Entity and
  5851. numerical character references in the destination will be parsed
  5852. into the corresponding Unicode code points, as usual. These may
  5853. be optionally URL-escaped when written as HTML, but this spec
  5854. does not enforce any particular policy for rendering URLs in
  5855. HTML or other formats. Renderers may make different decisions
  5856. about how to escape or normalize URLs in the output.
  5857. ```````````````````````````````` example
  5858. [link](foo%20b&auml;)
  5859. .
  5860. <p><a href="foo%20b%C3%A4">link</a></p>
  5861. ````````````````````````````````
  5862. Note that, because titles can often be parsed as destinations,
  5863. if you try to omit the destination and keep the title, you'll
  5864. get unexpected results:
  5865. ```````````````````````````````` example
  5866. [link]("title")
  5867. .
  5868. <p><a href="%22title%22">link</a></p>
  5869. ````````````````````````````````
  5870. Titles may be in single quotes, double quotes, or parentheses:
  5871. ```````````````````````````````` example
  5872. [link](/url "title")
  5873. [link](/url 'title')
  5874. [link](/url (title))
  5875. .
  5876. <p><a href="/url" title="title">link</a>
  5877. <a href="/url" title="title">link</a>
  5878. <a href="/url" title="title">link</a></p>
  5879. ````````````````````````````````
  5880. Backslash escapes and entity and numeric character references
  5881. may be used in titles:
  5882. ```````````````````````````````` example
  5883. [link](/url "title \"&quot;")
  5884. .
  5885. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5886. ````````````````````````````````
  5887. Titles must be separated from the link using a [whitespace].
  5888. Other [Unicode whitespace] like non-breaking space doesn't work.
  5889. ```````````````````````````````` example
  5890. [link](/url "title")
  5891. .
  5892. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5893. ````````````````````````````````
  5894. Nested balanced quotes are not allowed without escaping:
  5895. ```````````````````````````````` example
  5896. [link](/url "title "and" title")
  5897. .
  5898. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5899. ````````````````````````````````
  5900. But it is easy to work around this by using a different quote type:
  5901. ```````````````````````````````` example
  5902. [link](/url 'title "and" title')
  5903. .
  5904. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5905. ````````````````````````````````
  5906. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5907. title, and its test suite included a test demonstrating this.
  5908. But it is hard to see a good rationale for the extra complexity this
  5909. brings, since there are already many ways---backslash escaping,
  5910. entity and numeric character references, or using a different
  5911. quote type for the enclosing title---to write titles containing
  5912. double quotes. `Markdown.pl`'s handling of titles has a number
  5913. of other strange features. For example, it allows single-quoted
  5914. titles in inline links, but not reference links. And, in
  5915. reference links but not inline links, it allows a title to begin
  5916. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5917. titles with no closing quotation mark, though 1.0.2b8 does not.
  5918. It seems preferable to adopt a simple, rational rule that works
  5919. the same way in inline links and link reference definitions.)
  5920. [Whitespace] is allowed around the destination and title:
  5921. ```````````````````````````````` example
  5922. [link]( /uri
  5923. "title" )
  5924. .
  5925. <p><a href="/uri" title="title">link</a></p>
  5926. ````````````````````````````````
  5927. But it is not allowed between the link text and the
  5928. following parenthesis:
  5929. ```````````````````````````````` example
  5930. [link] (/uri)
  5931. .
  5932. <p>[link] (/uri)</p>
  5933. ````````````````````````````````
  5934. The link text may contain balanced brackets, but not unbalanced ones,
  5935. unless they are escaped:
  5936. ```````````````````````````````` example
  5937. [link [foo [bar]]](/uri)
  5938. .
  5939. <p><a href="/uri">link [foo [bar]]</a></p>
  5940. ````````````````````````````````
  5941. ```````````````````````````````` example
  5942. [link] bar](/uri)
  5943. .
  5944. <p>[link] bar](/uri)</p>
  5945. ````````````````````````````````
  5946. ```````````````````````````````` example
  5947. [link [bar](/uri)
  5948. .
  5949. <p>[link <a href="/uri">bar</a></p>
  5950. ````````````````````````````````
  5951. ```````````````````````````````` example
  5952. [link \[bar](/uri)
  5953. .
  5954. <p><a href="/uri">link [bar</a></p>
  5955. ````````````````````````````````
  5956. The link text may contain inline content:
  5957. ```````````````````````````````` example
  5958. [link *foo **bar** `#`*](/uri)
  5959. .
  5960. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5961. ````````````````````````````````
  5962. ```````````````````````````````` example
  5963. [![moon](moon.jpg)](/uri)
  5964. .
  5965. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5966. ````````````````````````````````
  5967. However, links may not contain other links, at any level of nesting.
  5968. ```````````````````````````````` example
  5969. [foo [bar](/uri)](/uri)
  5970. .
  5971. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5972. ````````````````````````````````
  5973. ```````````````````````````````` example
  5974. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5975. .
  5976. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5977. ````````````````````````````````
  5978. ```````````````````````````````` example
  5979. ![[[foo](uri1)](uri2)](uri3)
  5980. .
  5981. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5982. ````````````````````````````````
  5983. These cases illustrate the precedence of link text grouping over
  5984. emphasis grouping:
  5985. ```````````````````````````````` example
  5986. *[foo*](/uri)
  5987. .
  5988. <p>*<a href="/uri">foo*</a></p>
  5989. ````````````````````````````````
  5990. ```````````````````````````````` example
  5991. [foo *bar](baz*)
  5992. .
  5993. <p><a href="baz*">foo *bar</a></p>
  5994. ````````````````````````````````
  5995. Note that brackets that *aren't* part of links do not take
  5996. precedence:
  5997. ```````````````````````````````` example
  5998. *foo [bar* baz]
  5999. .
  6000. <p><em>foo [bar</em> baz]</p>
  6001. ````````````````````````````````
  6002. These cases illustrate the precedence of HTML tags, code spans,
  6003. and autolinks over link grouping:
  6004. ```````````````````````````````` example
  6005. [foo <bar attr="](baz)">
  6006. .
  6007. <p>[foo <bar attr="](baz)"></p>
  6008. ````````````````````````````````
  6009. ```````````````````````````````` example
  6010. [foo`](/uri)`
  6011. .
  6012. <p>[foo<code>](/uri)</code></p>
  6013. ````````````````````````````````
  6014. ```````````````````````````````` example
  6015. [foo<http://example.com/?search=](uri)>
  6016. .
  6017. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  6018. ````````````````````````````````
  6019. There are three kinds of [reference link](@)s:
  6020. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  6021. and [shortcut](#shortcut-reference-link).
  6022. A [full reference link](@)
  6023. consists of a [link text] immediately followed by a [link label]
  6024. that [matches] a [link reference definition] elsewhere in the document.
  6025. A [link label](@) begins with a left bracket (`[`) and ends
  6026. with the first right bracket (`]`) that is not backslash-escaped.
  6027. Between these brackets there must be at least one [non-whitespace character].
  6028. Unescaped square bracket characters are not allowed inside the
  6029. opening and closing square brackets of [link labels]. A link
  6030. label can have at most 999 characters inside the square
  6031. brackets.
  6032. One label [matches](@)
  6033. another just in case their normalized forms are equal. To normalize a
  6034. label, strip off the opening and closing brackets,
  6035. perform the *Unicode case fold*, strip leading and trailing
  6036. [whitespace] and collapse consecutive internal
  6037. [whitespace] to a single space. If there are multiple
  6038. matching reference link definitions, the one that comes first in the
  6039. document is used. (It is desirable in such cases to emit a warning.)
  6040. The link's URI and title are provided by the matching [link
  6041. reference definition].
  6042. Here is a simple example:
  6043. ```````````````````````````````` example
  6044. [foo][bar]
  6045. [bar]: /url "title"
  6046. .
  6047. <p><a href="/url" title="title">foo</a></p>
  6048. ````````````````````````````````
  6049. The rules for the [link text] are the same as with
  6050. [inline links]. Thus:
  6051. The link text may contain balanced brackets, but not unbalanced ones,
  6052. unless they are escaped:
  6053. ```````````````````````````````` example
  6054. [link [foo [bar]]][ref]
  6055. [ref]: /uri
  6056. .
  6057. <p><a href="/uri">link [foo [bar]]</a></p>
  6058. ````````````````````````````````
  6059. ```````````````````````````````` example
  6060. [link \[bar][ref]
  6061. [ref]: /uri
  6062. .
  6063. <p><a href="/uri">link [bar</a></p>
  6064. ````````````````````````````````
  6065. The link text may contain inline content:
  6066. ```````````````````````````````` example
  6067. [link *foo **bar** `#`*][ref]
  6068. [ref]: /uri
  6069. .
  6070. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6071. ````````````````````````````````
  6072. ```````````````````````````````` example
  6073. [![moon](moon.jpg)][ref]
  6074. [ref]: /uri
  6075. .
  6076. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6077. ````````````````````````````````
  6078. However, links may not contain other links, at any level of nesting.
  6079. ```````````````````````````````` example
  6080. [foo [bar](/uri)][ref]
  6081. [ref]: /uri
  6082. .
  6083. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  6084. ````````````````````````````````
  6085. ```````````````````````````````` example
  6086. [foo *bar [baz][ref]*][ref]
  6087. [ref]: /uri
  6088. .
  6089. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  6090. ````````````````````````````````
  6091. (In the examples above, we have two [shortcut reference links]
  6092. instead of one [full reference link].)
  6093. The following cases illustrate the precedence of link text grouping over
  6094. emphasis grouping:
  6095. ```````````````````````````````` example
  6096. *[foo*][ref]
  6097. [ref]: /uri
  6098. .
  6099. <p>*<a href="/uri">foo*</a></p>
  6100. ````````````````````````````````
  6101. ```````````````````````````````` example
  6102. [foo *bar][ref]*
  6103. [ref]: /uri
  6104. .
  6105. <p><a href="/uri">foo *bar</a>*</p>
  6106. ````````````````````````````````
  6107. These cases illustrate the precedence of HTML tags, code spans,
  6108. and autolinks over link grouping:
  6109. ```````````````````````````````` example
  6110. [foo <bar attr="][ref]">
  6111. [ref]: /uri
  6112. .
  6113. <p>[foo <bar attr="][ref]"></p>
  6114. ````````````````````````````````
  6115. ```````````````````````````````` example
  6116. [foo`][ref]`
  6117. [ref]: /uri
  6118. .
  6119. <p>[foo<code>][ref]</code></p>
  6120. ````````````````````````````````
  6121. ```````````````````````````````` example
  6122. [foo<http://example.com/?search=][ref]>
  6123. [ref]: /uri
  6124. .
  6125. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6126. ````````````````````````````````
  6127. Matching is case-insensitive:
  6128. ```````````````````````````````` example
  6129. [foo][BaR]
  6130. [bar]: /url "title"
  6131. .
  6132. <p><a href="/url" title="title">foo</a></p>
  6133. ````````````````````````````````
  6134. Unicode case fold is used:
  6135. ```````````````````````````````` example
  6136. [ẞ]
  6137. [SS]: /url
  6138. .
  6139. <p><a href="/url">ẞ</a></p>
  6140. ````````````````````````````````
  6141. Consecutive internal [whitespace] is treated as one space for
  6142. purposes of determining matching:
  6143. ```````````````````````````````` example
  6144. [Foo
  6145. bar]: /url
  6146. [Baz][Foo bar]
  6147. .
  6148. <p><a href="/url">Baz</a></p>
  6149. ````````````````````````````````
  6150. No [whitespace] is allowed between the [link text] and the
  6151. [link label]:
  6152. ```````````````````````````````` example
  6153. [foo] [bar]
  6154. [bar]: /url "title"
  6155. .
  6156. <p>[foo] <a href="/url" title="title">bar</a></p>
  6157. ````````````````````````````````
  6158. ```````````````````````````````` example
  6159. [foo]
  6160. [bar]
  6161. [bar]: /url "title"
  6162. .
  6163. <p>[foo]
  6164. <a href="/url" title="title">bar</a></p>
  6165. ````````````````````````````````
  6166. This is a departure from John Gruber's original Markdown syntax
  6167. description, which explicitly allows whitespace between the link
  6168. text and the link label. It brings reference links in line with
  6169. [inline links], which (according to both original Markdown and
  6170. this spec) cannot have whitespace after the link text. More
  6171. importantly, it prevents inadvertent capture of consecutive
  6172. [shortcut reference links]. If whitespace is allowed between the
  6173. link text and the link label, then in the following we will have
  6174. a single reference link, not two shortcut reference links, as
  6175. intended:
  6176. ``` markdown
  6177. [foo]
  6178. [bar]
  6179. [foo]: /url1
  6180. [bar]: /url2
  6181. ```
  6182. (Note that [shortcut reference links] were introduced by Gruber
  6183. himself in a beta version of `Markdown.pl`, but never included
  6184. in the official syntax description. Without shortcut reference
  6185. links, it is harmless to allow space between the link text and
  6186. link label; but once shortcut references are introduced, it is
  6187. too dangerous to allow this, as it frequently leads to
  6188. unintended results.)
  6189. When there are multiple matching [link reference definitions],
  6190. the first is used:
  6191. ```````````````````````````````` example
  6192. [foo]: /url1
  6193. [foo]: /url2
  6194. [bar][foo]
  6195. .
  6196. <p><a href="/url1">bar</a></p>
  6197. ````````````````````````````````
  6198. Note that matching is performed on normalized strings, not parsed
  6199. inline content. So the following does not match, even though the
  6200. labels define equivalent inline content:
  6201. ```````````````````````````````` example
  6202. [bar][foo\!]
  6203. [foo!]: /url
  6204. .
  6205. <p>[bar][foo!]</p>
  6206. ````````````````````````````````
  6207. [Link labels] cannot contain brackets, unless they are
  6208. backslash-escaped:
  6209. ```````````````````````````````` example
  6210. [foo][ref[]
  6211. [ref[]: /uri
  6212. .
  6213. <p>[foo][ref[]</p>
  6214. <p>[ref[]: /uri</p>
  6215. ````````````````````````````````
  6216. ```````````````````````````````` example
  6217. [foo][ref[bar]]
  6218. [ref[bar]]: /uri
  6219. .
  6220. <p>[foo][ref[bar]]</p>
  6221. <p>[ref[bar]]: /uri</p>
  6222. ````````````````````````````````
  6223. ```````````````````````````````` example
  6224. [[[foo]]]
  6225. [[[foo]]]: /url
  6226. .
  6227. <p>[[[foo]]]</p>
  6228. <p>[[[foo]]]: /url</p>
  6229. ````````````````````````````````
  6230. ```````````````````````````````` example
  6231. [foo][ref\[]
  6232. [ref\[]: /uri
  6233. .
  6234. <p><a href="/uri">foo</a></p>
  6235. ````````````````````````````````
  6236. Note that in this example `]` is not backslash-escaped:
  6237. ```````````````````````````````` example
  6238. [bar\\]: /uri
  6239. [bar\\]
  6240. .
  6241. <p><a href="/uri">bar\</a></p>
  6242. ````````````````````````````````
  6243. A [link label] must contain at least one [non-whitespace character]:
  6244. ```````````````````````````````` example
  6245. []
  6246. []: /uri
  6247. .
  6248. <p>[]</p>
  6249. <p>[]: /uri</p>
  6250. ````````````````````````````````
  6251. ```````````````````````````````` example
  6252. [
  6253. ]
  6254. [
  6255. ]: /uri
  6256. .
  6257. <p>[
  6258. ]</p>
  6259. <p>[
  6260. ]: /uri</p>
  6261. ````````````````````````````````
  6262. A [collapsed reference link](@)
  6263. consists of a [link label] that [matches] a
  6264. [link reference definition] elsewhere in the
  6265. document, followed by the string `[]`.
  6266. The contents of the first link label are parsed as inlines,
  6267. which are used as the link's text. The link's URI and title are
  6268. provided by the matching reference link definition. Thus,
  6269. `[foo][]` is equivalent to `[foo][foo]`.
  6270. ```````````````````````````````` example
  6271. [foo][]
  6272. [foo]: /url "title"
  6273. .
  6274. <p><a href="/url" title="title">foo</a></p>
  6275. ````````````````````````````````
  6276. ```````````````````````````````` example
  6277. [*foo* bar][]
  6278. [*foo* bar]: /url "title"
  6279. .
  6280. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6281. ````````````````````````````````
  6282. The link labels are case-insensitive:
  6283. ```````````````````````````````` example
  6284. [Foo][]
  6285. [foo]: /url "title"
  6286. .
  6287. <p><a href="/url" title="title">Foo</a></p>
  6288. ````````````````````````````````
  6289. As with full reference links, [whitespace] is not
  6290. allowed between the two sets of brackets:
  6291. ```````````````````````````````` example
  6292. [foo]
  6293. []
  6294. [foo]: /url "title"
  6295. .
  6296. <p><a href="/url" title="title">foo</a>
  6297. []</p>
  6298. ````````````````````````````````
  6299. A [shortcut reference link](@)
  6300. consists of a [link label] that [matches] a
  6301. [link reference definition] elsewhere in the
  6302. document and is not followed by `[]` or a link label.
  6303. The contents of the first link label are parsed as inlines,
  6304. which are used as the link's text. The link's URI and title
  6305. are provided by the matching link reference definition.
  6306. Thus, `[foo]` is equivalent to `[foo][]`.
  6307. ```````````````````````````````` example
  6308. [foo]
  6309. [foo]: /url "title"
  6310. .
  6311. <p><a href="/url" title="title">foo</a></p>
  6312. ````````````````````````````````
  6313. ```````````````````````````````` example
  6314. [*foo* bar]
  6315. [*foo* bar]: /url "title"
  6316. .
  6317. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6318. ````````````````````````````````
  6319. ```````````````````````````````` example
  6320. [[*foo* bar]]
  6321. [*foo* bar]: /url "title"
  6322. .
  6323. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6324. ````````````````````````````````
  6325. ```````````````````````````````` example
  6326. [[bar [foo]
  6327. [foo]: /url
  6328. .
  6329. <p>[[bar <a href="/url">foo</a></p>
  6330. ````````````````````````````````
  6331. The link labels are case-insensitive:
  6332. ```````````````````````````````` example
  6333. [Foo]
  6334. [foo]: /url "title"
  6335. .
  6336. <p><a href="/url" title="title">Foo</a></p>
  6337. ````````````````````````````````
  6338. A space after the link text should be preserved:
  6339. ```````````````````````````````` example
  6340. [foo] bar
  6341. [foo]: /url
  6342. .
  6343. <p><a href="/url">foo</a> bar</p>
  6344. ````````````````````````````````
  6345. If you just want bracketed text, you can backslash-escape the
  6346. opening bracket to avoid links:
  6347. ```````````````````````````````` example
  6348. \[foo]
  6349. [foo]: /url "title"
  6350. .
  6351. <p>[foo]</p>
  6352. ````````````````````````````````
  6353. Note that this is a link, because a link label ends with the first
  6354. following closing bracket:
  6355. ```````````````````````````````` example
  6356. [foo*]: /url
  6357. *[foo*]
  6358. .
  6359. <p>*<a href="/url">foo*</a></p>
  6360. ````````````````````````````````
  6361. Full and compact references take precedence over shortcut
  6362. references:
  6363. ```````````````````````````````` example
  6364. [foo][bar]
  6365. [foo]: /url1
  6366. [bar]: /url2
  6367. .
  6368. <p><a href="/url2">foo</a></p>
  6369. ````````````````````````````````
  6370. ```````````````````````````````` example
  6371. [foo][]
  6372. [foo]: /url1
  6373. .
  6374. <p><a href="/url1">foo</a></p>
  6375. ````````````````````````````````
  6376. Inline links also take precedence:
  6377. ```````````````````````````````` example
  6378. [foo]()
  6379. [foo]: /url1
  6380. .
  6381. <p><a href="">foo</a></p>
  6382. ````````````````````````````````
  6383. ```````````````````````````````` example
  6384. [foo](not a link)
  6385. [foo]: /url1
  6386. .
  6387. <p><a href="/url1">foo</a>(not a link)</p>
  6388. ````````````````````````````````
  6389. In the following case `[bar][baz]` is parsed as a reference,
  6390. `[foo]` as normal text:
  6391. ```````````````````````````````` example
  6392. [foo][bar][baz]
  6393. [baz]: /url
  6394. .
  6395. <p>[foo]<a href="/url">bar</a></p>
  6396. ````````````````````````````````
  6397. Here, though, `[foo][bar]` is parsed as a reference, since
  6398. `[bar]` is defined:
  6399. ```````````````````````````````` example
  6400. [foo][bar][baz]
  6401. [baz]: /url1
  6402. [bar]: /url2
  6403. .
  6404. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6405. ````````````````````````````````
  6406. Here `[foo]` is not parsed as a shortcut reference, because it
  6407. is followed by a link label (even though `[bar]` is not defined):
  6408. ```````````````````````````````` example
  6409. [foo][bar][baz]
  6410. [baz]: /url1
  6411. [foo]: /url2
  6412. .
  6413. <p>[foo]<a href="/url1">bar</a></p>
  6414. ````````````````````````````````
  6415. ## Images
  6416. Syntax for images is like the syntax for links, with one
  6417. difference. Instead of [link text], we have an
  6418. [image description](@). The rules for this are the
  6419. same as for [link text], except that (a) an
  6420. image description starts with `![` rather than `[`, and
  6421. (b) an image description may contain links.
  6422. An image description has inline elements
  6423. as its contents. When an image is rendered to HTML,
  6424. this is standardly used as the image's `alt` attribute.
  6425. ```````````````````````````````` example
  6426. ![foo](/url "title")
  6427. .
  6428. <p><img src="/url" alt="foo" title="title" /></p>
  6429. ````````````````````````````````
  6430. ```````````````````````````````` example
  6431. ![foo *bar*]
  6432. [foo *bar*]: train.jpg "train & tracks"
  6433. .
  6434. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6435. ````````````````````````````````
  6436. ```````````````````````````````` example
  6437. ![foo ![bar](/url)](/url2)
  6438. .
  6439. <p><img src="/url2" alt="foo bar" /></p>
  6440. ````````````````````````````````
  6441. ```````````````````````````````` example
  6442. ![foo [bar](/url)](/url2)
  6443. .
  6444. <p><img src="/url2" alt="foo bar" /></p>
  6445. ````````````````````````````````
  6446. Though this spec is concerned with parsing, not rendering, it is
  6447. recommended that in rendering to HTML, only the plain string content
  6448. of the [image description] be used. Note that in
  6449. the above example, the alt attribute's value is `foo bar`, not `foo
  6450. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6451. content is rendered, without formatting.
  6452. ```````````````````````````````` example
  6453. ![foo *bar*][]
  6454. [foo *bar*]: train.jpg "train & tracks"
  6455. .
  6456. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6457. ````````````````````````````````
  6458. ```````````````````````````````` example
  6459. ![foo *bar*][foobar]
  6460. [FOOBAR]: train.jpg "train & tracks"
  6461. .
  6462. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6463. ````````````````````````````````
  6464. ```````````````````````````````` example
  6465. ![foo](train.jpg)
  6466. .
  6467. <p><img src="train.jpg" alt="foo" /></p>
  6468. ````````````````````````````````
  6469. ```````````````````````````````` example
  6470. My ![foo bar](/path/to/train.jpg "title" )
  6471. .
  6472. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6473. ````````````````````````````````
  6474. ```````````````````````````````` example
  6475. ![foo](<url>)
  6476. .
  6477. <p><img src="url" alt="foo" /></p>
  6478. ````````````````````````````````
  6479. ```````````````````````````````` example
  6480. ![](/url)
  6481. .
  6482. <p><img src="/url" alt="" /></p>
  6483. ````````````````````````````````
  6484. Reference-style:
  6485. ```````````````````````````````` example
  6486. ![foo][bar]
  6487. [bar]: /url
  6488. .
  6489. <p><img src="/url" alt="foo" /></p>
  6490. ````````````````````````````````
  6491. ```````````````````````````````` example
  6492. ![foo][bar]
  6493. [BAR]: /url
  6494. .
  6495. <p><img src="/url" alt="foo" /></p>
  6496. ````````````````````````````````
  6497. Collapsed:
  6498. ```````````````````````````````` example
  6499. ![foo][]
  6500. [foo]: /url "title"
  6501. .
  6502. <p><img src="/url" alt="foo" title="title" /></p>
  6503. ````````````````````````````````
  6504. ```````````````````````````````` example
  6505. ![*foo* bar][]
  6506. [*foo* bar]: /url "title"
  6507. .
  6508. <p><img src="/url" alt="foo bar" title="title" /></p>
  6509. ````````````````````````````````
  6510. The labels are case-insensitive:
  6511. ```````````````````````````````` example
  6512. ![Foo][]
  6513. [foo]: /url "title"
  6514. .
  6515. <p><img src="/url" alt="Foo" title="title" /></p>
  6516. ````````````````````````````````
  6517. As with reference links, [whitespace] is not allowed
  6518. between the two sets of brackets:
  6519. ```````````````````````````````` example
  6520. ![foo]
  6521. []
  6522. [foo]: /url "title"
  6523. .
  6524. <p><img src="/url" alt="foo" title="title" />
  6525. []</p>
  6526. ````````````````````````````````
  6527. Shortcut:
  6528. ```````````````````````````````` example
  6529. ![foo]
  6530. [foo]: /url "title"
  6531. .
  6532. <p><img src="/url" alt="foo" title="title" /></p>
  6533. ````````````````````````````````
  6534. ```````````````````````````````` example
  6535. ![*foo* bar]
  6536. [*foo* bar]: /url "title"
  6537. .
  6538. <p><img src="/url" alt="foo bar" title="title" /></p>
  6539. ````````````````````````````````
  6540. Note that link labels cannot contain unescaped brackets:
  6541. ```````````````````````````````` example
  6542. ![[foo]]
  6543. [[foo]]: /url "title"
  6544. .
  6545. <p>![[foo]]</p>
  6546. <p>[[foo]]: /url &quot;title&quot;</p>
  6547. ````````````````````````````````
  6548. The link labels are case-insensitive:
  6549. ```````````````````````````````` example
  6550. ![Foo]
  6551. [foo]: /url "title"
  6552. .
  6553. <p><img src="/url" alt="Foo" title="title" /></p>
  6554. ````````````````````````````````
  6555. If you just want a literal `!` followed by bracketed text, you can
  6556. backslash-escape the opening `[`:
  6557. ```````````````````````````````` example
  6558. !\[foo]
  6559. [foo]: /url "title"
  6560. .
  6561. <p>![foo]</p>
  6562. ````````````````````````````````
  6563. If you want a link after a literal `!`, backslash-escape the
  6564. `!`:
  6565. ```````````````````````````````` example
  6566. \![foo]
  6567. [foo]: /url "title"
  6568. .
  6569. <p>!<a href="/url" title="title">foo</a></p>
  6570. ````````````````````````````````
  6571. ## Autolinks
  6572. [Autolink](@)s are absolute URIs and email addresses inside
  6573. `<` and `>`. They are parsed as links, with the URL or email address
  6574. as the link label.
  6575. A [URI autolink](@) consists of `<`, followed by an
  6576. [absolute URI] followed by `>`. It is parsed as
  6577. a link to the URI, with the URI as the link's label.
  6578. An [absolute URI](@),
  6579. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6580. followed by zero or more characters other than ASCII
  6581. [whitespace] and control characters, `<`, and `>`. If
  6582. the URI includes these characters, they must be percent-encoded
  6583. (e.g. `%20` for a space).
  6584. For purposes of this spec, a [scheme](@) is any sequence
  6585. of 2--32 characters beginning with an ASCII letter and followed
  6586. by any combination of ASCII letters, digits, or the symbols plus
  6587. ("+"), period ("."), or hyphen ("-").
  6588. Here are some valid autolinks:
  6589. ```````````````````````````````` example
  6590. <http://foo.bar.baz>
  6591. .
  6592. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6593. ````````````````````````````````
  6594. ```````````````````````````````` example
  6595. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6596. .
  6597. <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>
  6598. ````````````````````````````````
  6599. ```````````````````````````````` example
  6600. <irc://foo.bar:2233/baz>
  6601. .
  6602. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6603. ````````````````````````````````
  6604. Uppercase is also fine:
  6605. ```````````````````````````````` example
  6606. <MAILTO:FOO@BAR.BAZ>
  6607. .
  6608. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6609. ````````````````````````````````
  6610. Note that many strings that count as [absolute URIs] for
  6611. purposes of this spec are not valid URIs, because their
  6612. schemes are not registered or because of other problems
  6613. with their syntax:
  6614. ```````````````````````````````` example
  6615. <a+b+c:d>
  6616. .
  6617. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6618. ````````````````````````````````
  6619. ```````````````````````````````` example
  6620. <made-up-scheme://foo,bar>
  6621. .
  6622. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6623. ````````````````````````````````
  6624. ```````````````````````````````` example
  6625. <http://../>
  6626. .
  6627. <p><a href="http://../">http://../</a></p>
  6628. ````````````````````````````````
  6629. ```````````````````````````````` example
  6630. <localhost:5001/foo>
  6631. .
  6632. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6633. ````````````````````````````````
  6634. Spaces are not allowed in autolinks:
  6635. ```````````````````````````````` example
  6636. <http://foo.bar/baz bim>
  6637. .
  6638. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6639. ````````````````````````````````
  6640. Backslash-escapes do not work inside autolinks:
  6641. ```````````````````````````````` example
  6642. <http://example.com/\[\>
  6643. .
  6644. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6645. ````````````````````````````````
  6646. An [email autolink](@)
  6647. consists of `<`, followed by an [email address],
  6648. followed by `>`. The link's label is the email address,
  6649. and the URL is `mailto:` followed by the email address.
  6650. An [email address](@),
  6651. for these purposes, is anything that matches
  6652. the [non-normative regex from the HTML5
  6653. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6654. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6655. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6656. Examples of email autolinks:
  6657. ```````````````````````````````` example
  6658. <foo@bar.example.com>
  6659. .
  6660. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6661. ````````````````````````````````
  6662. ```````````````````````````````` example
  6663. <foo+special@Bar.baz-bar0.com>
  6664. .
  6665. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6666. ````````````````````````````````
  6667. Backslash-escapes do not work inside email autolinks:
  6668. ```````````````````````````````` example
  6669. <foo\+@bar.example.com>
  6670. .
  6671. <p>&lt;foo+@bar.example.com&gt;</p>
  6672. ````````````````````````````````
  6673. These are not autolinks:
  6674. ```````````````````````````````` example
  6675. <>
  6676. .
  6677. <p>&lt;&gt;</p>
  6678. ````````````````````````````````
  6679. ```````````````````````````````` example
  6680. < http://foo.bar >
  6681. .
  6682. <p>&lt; http://foo.bar &gt;</p>
  6683. ````````````````````````````````
  6684. ```````````````````````````````` example
  6685. <m:abc>
  6686. .
  6687. <p>&lt;m:abc&gt;</p>
  6688. ````````````````````````````````
  6689. ```````````````````````````````` example
  6690. <foo.bar.baz>
  6691. .
  6692. <p>&lt;foo.bar.baz&gt;</p>
  6693. ````````````````````````````````
  6694. ```````````````````````````````` example
  6695. http://example.com
  6696. .
  6697. <p>http://example.com</p>
  6698. ````````````````````````````````
  6699. ```````````````````````````````` example
  6700. foo@bar.example.com
  6701. .
  6702. <p>foo@bar.example.com</p>
  6703. ````````````````````````````````
  6704. ## Raw HTML
  6705. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6706. raw HTML tag and will be rendered in HTML without escaping.
  6707. Tag and attribute names are not limited to current HTML tags,
  6708. so custom tags (and even, say, DocBook tags) may be used.
  6709. Here is the grammar for tags:
  6710. A [tag name](@) consists of an ASCII letter
  6711. followed by zero or more ASCII letters, digits, or
  6712. hyphens (`-`).
  6713. An [attribute](@) consists of [whitespace],
  6714. an [attribute name], and an optional
  6715. [attribute value specification].
  6716. An [attribute name](@)
  6717. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6718. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6719. specification restricted to ASCII. HTML5 is laxer.)
  6720. An [attribute value specification](@)
  6721. consists of optional [whitespace],
  6722. a `=` character, optional [whitespace], and an [attribute
  6723. value].
  6724. An [attribute value](@)
  6725. consists of an [unquoted attribute value],
  6726. a [single-quoted attribute value], or a [double-quoted attribute value].
  6727. An [unquoted attribute value](@)
  6728. is a nonempty string of characters not
  6729. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6730. A [single-quoted attribute value](@)
  6731. consists of `'`, zero or more
  6732. characters not including `'`, and a final `'`.
  6733. A [double-quoted attribute value](@)
  6734. consists of `"`, zero or more
  6735. characters not including `"`, and a final `"`.
  6736. An [open tag](@) consists of a `<` character, a [tag name],
  6737. zero or more [attributes], optional [whitespace], an optional `/`
  6738. character, and a `>` character.
  6739. A [closing tag](@) consists of the string `</`, a
  6740. [tag name], optional [whitespace], and the character `>`.
  6741. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6742. where *text* does not start with `>` or `->`, does not end with `-`,
  6743. and does not contain `--`. (See the
  6744. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6745. A [processing instruction](@)
  6746. consists of the string `<?`, a string
  6747. of characters not including the string `?>`, and the string
  6748. `?>`.
  6749. A [declaration](@) consists of the
  6750. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6751. [whitespace], a string of characters not including the
  6752. character `>`, and the character `>`.
  6753. A [CDATA section](@) consists of
  6754. the string `<![CDATA[`, a string of characters not including the string
  6755. `]]>`, and the string `]]>`.
  6756. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6757. an [HTML comment], a [processing instruction], a [declaration],
  6758. or a [CDATA section].
  6759. Here are some simple open tags:
  6760. ```````````````````````````````` example
  6761. <a><bab><c2c>
  6762. .
  6763. <p><a><bab><c2c></p>
  6764. ````````````````````````````````
  6765. Empty elements:
  6766. ```````````````````````````````` example
  6767. <a/><b2/>
  6768. .
  6769. <p><a/><b2/></p>
  6770. ````````````````````````````````
  6771. [Whitespace] is allowed:
  6772. ```````````````````````````````` example
  6773. <a /><b2
  6774. data="foo" >
  6775. .
  6776. <p><a /><b2
  6777. data="foo" ></p>
  6778. ````````````````````````````````
  6779. With attributes:
  6780. ```````````````````````````````` example
  6781. <a foo="bar" bam = 'baz <em>"</em>'
  6782. _boolean zoop:33=zoop:33 />
  6783. .
  6784. <p><a foo="bar" bam = 'baz <em>"</em>'
  6785. _boolean zoop:33=zoop:33 /></p>
  6786. ````````````````````````````````
  6787. Custom tag names can be used:
  6788. ```````````````````````````````` example
  6789. Foo <responsive-image src="foo.jpg" />
  6790. .
  6791. <p>Foo <responsive-image src="foo.jpg" /></p>
  6792. ````````````````````````````````
  6793. Illegal tag names, not parsed as HTML:
  6794. ```````````````````````````````` example
  6795. <33> <__>
  6796. .
  6797. <p>&lt;33&gt; &lt;__&gt;</p>
  6798. ````````````````````````````````
  6799. Illegal attribute names:
  6800. ```````````````````````````````` example
  6801. <a h*#ref="hi">
  6802. .
  6803. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6804. ````````````````````````````````
  6805. Illegal attribute values:
  6806. ```````````````````````````````` example
  6807. <a href="hi'> <a href=hi'>
  6808. .
  6809. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6810. ````````````````````````````````
  6811. Illegal [whitespace]:
  6812. ```````````````````````````````` example
  6813. < a><
  6814. foo><bar/ >
  6815. <foo bar=baz
  6816. bim!bop />
  6817. .
  6818. <p>&lt; a&gt;&lt;
  6819. foo&gt;&lt;bar/ &gt;
  6820. &lt;foo bar=baz
  6821. bim!bop /&gt;</p>
  6822. ````````````````````````````````
  6823. Missing [whitespace]:
  6824. ```````````````````````````````` example
  6825. <a href='bar'title=title>
  6826. .
  6827. <p>&lt;a href='bar'title=title&gt;</p>
  6828. ````````````````````````````````
  6829. Closing tags:
  6830. ```````````````````````````````` example
  6831. </a></foo >
  6832. .
  6833. <p></a></foo ></p>
  6834. ````````````````````````````````
  6835. Illegal attributes in closing tag:
  6836. ```````````````````````````````` example
  6837. </a href="foo">
  6838. .
  6839. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6840. ````````````````````````````````
  6841. Comments:
  6842. ```````````````````````````````` example
  6843. foo <!-- this is a
  6844. comment - with hyphen -->
  6845. .
  6846. <p>foo <!-- this is a
  6847. comment - with hyphen --></p>
  6848. ````````````````````````````````
  6849. ```````````````````````````````` example
  6850. foo <!-- not a comment -- two hyphens -->
  6851. .
  6852. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6853. ````````````````````````````````
  6854. Not comments:
  6855. ```````````````````````````````` example
  6856. foo <!--> foo -->
  6857. foo <!-- foo--->
  6858. .
  6859. <p>foo &lt;!--&gt; foo --&gt;</p>
  6860. <p>foo &lt;!-- foo---&gt;</p>
  6861. ````````````````````````````````
  6862. Processing instructions:
  6863. ```````````````````````````````` example
  6864. foo <?php echo $a; ?>
  6865. .
  6866. <p>foo <?php echo $a; ?></p>
  6867. ````````````````````````````````
  6868. Declarations:
  6869. ```````````````````````````````` example
  6870. foo <!ELEMENT br EMPTY>
  6871. .
  6872. <p>foo <!ELEMENT br EMPTY></p>
  6873. ````````````````````````````````
  6874. CDATA sections:
  6875. ```````````````````````````````` example
  6876. foo <![CDATA[>&<]]>
  6877. .
  6878. <p>foo <![CDATA[>&<]]></p>
  6879. ````````````````````````````````
  6880. Entity and numeric character references are preserved in HTML
  6881. attributes:
  6882. ```````````````````````````````` example
  6883. foo <a href="&ouml;">
  6884. .
  6885. <p>foo <a href="&ouml;"></p>
  6886. ````````````````````````````````
  6887. Backslash escapes do not work in HTML attributes:
  6888. ```````````````````````````````` example
  6889. foo <a href="\*">
  6890. .
  6891. <p>foo <a href="\*"></p>
  6892. ````````````````````````````````
  6893. ```````````````````````````````` example
  6894. <a href="\"">
  6895. .
  6896. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6897. ````````````````````````````````
  6898. ## Hard line breaks
  6899. A line break (not in a code span or HTML tag) that is preceded
  6900. by two or more spaces and does not occur at the end of a block
  6901. is parsed as a [hard line break](@) (rendered
  6902. in HTML as a `<br />` tag):
  6903. ```````````````````````````````` example
  6904. foo
  6905. baz
  6906. .
  6907. <p>foo<br />
  6908. baz</p>
  6909. ````````````````````````````````
  6910. For a more visible alternative, a backslash before the
  6911. [line ending] may be used instead of two spaces:
  6912. ```````````````````````````````` example
  6913. foo\
  6914. baz
  6915. .
  6916. <p>foo<br />
  6917. baz</p>
  6918. ````````````````````````````````
  6919. More than two spaces can be used:
  6920. ```````````````````````````````` example
  6921. foo
  6922. baz
  6923. .
  6924. <p>foo<br />
  6925. baz</p>
  6926. ````````````````````````````````
  6927. Leading spaces at the beginning of the next line are ignored:
  6928. ```````````````````````````````` example
  6929. foo
  6930. bar
  6931. .
  6932. <p>foo<br />
  6933. bar</p>
  6934. ````````````````````````````````
  6935. ```````````````````````````````` example
  6936. foo\
  6937. bar
  6938. .
  6939. <p>foo<br />
  6940. bar</p>
  6941. ````````````````````````````````
  6942. Line breaks can occur inside emphasis, links, and other constructs
  6943. that allow inline content:
  6944. ```````````````````````````````` example
  6945. *foo
  6946. bar*
  6947. .
  6948. <p><em>foo<br />
  6949. bar</em></p>
  6950. ````````````````````````````````
  6951. ```````````````````````````````` example
  6952. *foo\
  6953. bar*
  6954. .
  6955. <p><em>foo<br />
  6956. bar</em></p>
  6957. ````````````````````````````````
  6958. Line breaks do not occur inside code spans
  6959. ```````````````````````````````` example
  6960. `code
  6961. span`
  6962. .
  6963. <p><code>code span</code></p>
  6964. ````````````````````````````````
  6965. ```````````````````````````````` example
  6966. `code\
  6967. span`
  6968. .
  6969. <p><code>code\ span</code></p>
  6970. ````````````````````````````````
  6971. or HTML tags:
  6972. ```````````````````````````````` example
  6973. <a href="foo
  6974. bar">
  6975. .
  6976. <p><a href="foo
  6977. bar"></p>
  6978. ````````````````````````````````
  6979. ```````````````````````````````` example
  6980. <a href="foo\
  6981. bar">
  6982. .
  6983. <p><a href="foo\
  6984. bar"></p>
  6985. ````````````````````````````````
  6986. Hard line breaks are for separating inline content within a block.
  6987. Neither syntax for hard line breaks works at the end of a paragraph or
  6988. other block element:
  6989. ```````````````````````````````` example
  6990. foo\
  6991. .
  6992. <p>foo\</p>
  6993. ````````````````````````````````
  6994. ```````````````````````````````` example
  6995. foo
  6996. .
  6997. <p>foo</p>
  6998. ````````````````````````````````
  6999. ```````````````````````````````` example
  7000. ### foo\
  7001. .
  7002. <h3>foo\</h3>
  7003. ````````````````````````````````
  7004. ```````````````````````````````` example
  7005. ### foo
  7006. .
  7007. <h3>foo</h3>
  7008. ````````````````````````````````
  7009. ## Soft line breaks
  7010. A regular line break (not in a code span or HTML tag) that is not
  7011. preceded by two or more spaces or a backslash is parsed as a
  7012. [softbreak](@). (A softbreak may be rendered in HTML either as a
  7013. [line ending] or as a space. The result will be the same in
  7014. browsers. In the examples here, a [line ending] will be used.)
  7015. ```````````````````````````````` example
  7016. foo
  7017. baz
  7018. .
  7019. <p>foo
  7020. baz</p>
  7021. ````````````````````````````````
  7022. Spaces at the end of the line and beginning of the next line are
  7023. removed:
  7024. ```````````````````````````````` example
  7025. foo
  7026. baz
  7027. .
  7028. <p>foo
  7029. baz</p>
  7030. ````````````````````````````````
  7031. A conforming parser may render a soft line break in HTML either as a
  7032. line break or as a space.
  7033. A renderer may also provide an option to render soft line breaks
  7034. as hard line breaks.
  7035. ## Textual content
  7036. Any characters not given an interpretation by the above rules will
  7037. be parsed as plain textual content.
  7038. ```````````````````````````````` example
  7039. hello $.;'there
  7040. .
  7041. <p>hello $.;'there</p>
  7042. ````````````````````````````````
  7043. ```````````````````````````````` example
  7044. Foo χρῆν
  7045. .
  7046. <p>Foo χρῆν</p>
  7047. ````````````````````````````````
  7048. Internal spaces are preserved verbatim:
  7049. ```````````````````````````````` example
  7050. Multiple spaces
  7051. .
  7052. <p>Multiple spaces</p>
  7053. ````````````````````````````````
  7054. <!-- END TESTS -->
  7055. # Appendix: A parsing strategy
  7056. In this appendix we describe some features of the parsing strategy
  7057. used in the CommonMark reference implementations.
  7058. ## Overview
  7059. Parsing has two phases:
  7060. 1. In the first phase, lines of input are consumed and the block
  7061. structure of the document---its division into paragraphs, block quotes,
  7062. list items, and so on---is constructed. Text is assigned to these
  7063. blocks but not parsed. Link reference definitions are parsed and a
  7064. map of links is constructed.
  7065. 2. In the second phase, the raw text contents of paragraphs and headings
  7066. are parsed into sequences of Markdown inline elements (strings,
  7067. code spans, links, emphasis, and so on), using the map of link
  7068. references constructed in phase 1.
  7069. At each point in processing, the document is represented as a tree of
  7070. **blocks**. The root of the tree is a `document` block. The `document`
  7071. may have any number of other blocks as **children**. These children
  7072. may, in turn, have other blocks as children. The last child of a block
  7073. is normally considered **open**, meaning that subsequent lines of input
  7074. can alter its contents. (Blocks that are not open are **closed**.)
  7075. Here, for example, is a possible document tree, with the open blocks
  7076. marked by arrows:
  7077. ``` tree
  7078. -> document
  7079. -> block_quote
  7080. paragraph
  7081. "Lorem ipsum dolor\nsit amet."
  7082. -> list (type=bullet tight=true bullet_char=-)
  7083. list_item
  7084. paragraph
  7085. "Qui *quodsi iracundia*"
  7086. -> list_item
  7087. -> paragraph
  7088. "aliquando id"
  7089. ```
  7090. ## Phase 1: block structure
  7091. Each line that is processed has an effect on this tree. The line is
  7092. analyzed and, depending on its contents, the document may be altered
  7093. in one or more of the following ways:
  7094. 1. One or more open blocks may be closed.
  7095. 2. One or more new blocks may be created as children of the
  7096. last open block.
  7097. 3. Text may be added to the last (deepest) open block remaining
  7098. on the tree.
  7099. Once a line has been incorporated into the tree in this way,
  7100. it can be discarded, so input can be read in a stream.
  7101. For each line, we follow this procedure:
  7102. 1. First we iterate through the open blocks, starting with the
  7103. root document, and descending through last children down to the last
  7104. open block. Each block imposes a condition that the line must satisfy
  7105. if the block is to remain open. For example, a block quote requires a
  7106. `>` character. A paragraph requires a non-blank line.
  7107. In this phase we may match all or just some of the open
  7108. blocks. But we cannot close unmatched blocks yet, because we may have a
  7109. [lazy continuation line].
  7110. 2. Next, after consuming the continuation markers for existing
  7111. blocks, we look for new block starts (e.g. `>` for a block quote).
  7112. If we encounter a new block start, we close any blocks unmatched
  7113. in step 1 before creating the new block as a child of the last
  7114. matched container block.
  7115. 3. Finally, we look at the remainder of the line (after block
  7116. markers like `>`, list markers, and indentation have been consumed).
  7117. This is text that can be incorporated into the last open
  7118. block (a paragraph, code block, heading, or raw HTML).
  7119. Setext headings are formed when we see a line of a paragraph
  7120. that is a [setext heading underline].
  7121. Reference link definitions are detected when a paragraph is closed;
  7122. the accumulated text lines are parsed to see if they begin with
  7123. one or more reference link definitions. Any remainder becomes a
  7124. normal paragraph.
  7125. We can see how this works by considering how the tree above is
  7126. generated by four lines of Markdown:
  7127. ``` markdown
  7128. > Lorem ipsum dolor
  7129. sit amet.
  7130. > - Qui *quodsi iracundia*
  7131. > - aliquando id
  7132. ```
  7133. At the outset, our document model is just
  7134. ``` tree
  7135. -> document
  7136. ```
  7137. The first line of our text,
  7138. ``` markdown
  7139. > Lorem ipsum dolor
  7140. ```
  7141. causes a `block_quote` block to be created as a child of our
  7142. open `document` block, and a `paragraph` block as a child of
  7143. the `block_quote`. Then the text is added to the last open
  7144. block, the `paragraph`:
  7145. ``` tree
  7146. -> document
  7147. -> block_quote
  7148. -> paragraph
  7149. "Lorem ipsum dolor"
  7150. ```
  7151. The next line,
  7152. ``` markdown
  7153. sit amet.
  7154. ```
  7155. is a "lazy continuation" of the open `paragraph`, so it gets added
  7156. to the paragraph's text:
  7157. ``` tree
  7158. -> document
  7159. -> block_quote
  7160. -> paragraph
  7161. "Lorem ipsum dolor\nsit amet."
  7162. ```
  7163. The third line,
  7164. ``` markdown
  7165. > - Qui *quodsi iracundia*
  7166. ```
  7167. causes the `paragraph` block to be closed, and a new `list` block
  7168. opened as a child of the `block_quote`. A `list_item` is also
  7169. added as a child of the `list`, and a `paragraph` as a child of
  7170. the `list_item`. The text is then added to the new `paragraph`:
  7171. ``` tree
  7172. -> document
  7173. -> block_quote
  7174. paragraph
  7175. "Lorem ipsum dolor\nsit amet."
  7176. -> list (type=bullet tight=true bullet_char=-)
  7177. -> list_item
  7178. -> paragraph
  7179. "Qui *quodsi iracundia*"
  7180. ```
  7181. The fourth line,
  7182. ``` markdown
  7183. > - aliquando id
  7184. ```
  7185. causes the `list_item` (and its child the `paragraph`) to be closed,
  7186. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7187. is added as a child of the new `list_item`, to contain the text.
  7188. We thus obtain the final tree:
  7189. ``` tree
  7190. -> document
  7191. -> block_quote
  7192. paragraph
  7193. "Lorem ipsum dolor\nsit amet."
  7194. -> list (type=bullet tight=true bullet_char=-)
  7195. list_item
  7196. paragraph
  7197. "Qui *quodsi iracundia*"
  7198. -> list_item
  7199. -> paragraph
  7200. "aliquando id"
  7201. ```
  7202. ## Phase 2: inline structure
  7203. Once all of the input has been parsed, all open blocks are closed.
  7204. We then "walk the tree," visiting every node, and parse raw
  7205. string contents of paragraphs and headings as inlines. At this
  7206. point we have seen all the link reference definitions, so we can
  7207. resolve reference links as we go.
  7208. ``` tree
  7209. document
  7210. block_quote
  7211. paragraph
  7212. str "Lorem ipsum dolor"
  7213. softbreak
  7214. str "sit amet."
  7215. list (type=bullet tight=true bullet_char=-)
  7216. list_item
  7217. paragraph
  7218. str "Qui "
  7219. emph
  7220. str "quodsi iracundia"
  7221. list_item
  7222. paragraph
  7223. str "aliquando id"
  7224. ```
  7225. Notice how the [line ending] in the first paragraph has
  7226. been parsed as a `softbreak`, and the asterisks in the first list item
  7227. have become an `emph`.
  7228. ### An algorithm for parsing nested emphasis and links
  7229. By far the trickiest part of inline parsing is handling emphasis,
  7230. strong emphasis, links, and images. This is done using the following
  7231. algorithm.
  7232. When we're parsing inlines and we hit either
  7233. - a run of `*` or `_` characters, or
  7234. - a `[` or `![`
  7235. we insert a text node with these symbols as its literal content, and we
  7236. add a pointer to this text node to the [delimiter stack](@).
  7237. The [delimiter stack] is a doubly linked list. Each
  7238. element contains a pointer to a text node, plus information about
  7239. - the type of delimiter (`[`, `![`, `*`, `_`)
  7240. - the number of delimiters,
  7241. - whether the delimiter is "active" (all are active to start), and
  7242. - whether the delimiter is a potential opener, a potential closer,
  7243. or both (which depends on what sort of characters precede
  7244. and follow the delimiters).
  7245. When we hit a `]` character, we call the *look for link or image*
  7246. procedure (see below).
  7247. When we hit the end of the input, we call the *process emphasis*
  7248. procedure (see below), with `stack_bottom` = NULL.
  7249. #### *look for link or image*
  7250. Starting at the top of the delimiter stack, we look backwards
  7251. through the stack for an opening `[` or `![` delimiter.
  7252. - If we don't find one, we return a literal text node `]`.
  7253. - If we do find one, but it's not *active*, we remove the inactive
  7254. delimiter from the stack, and return a literal text node `]`.
  7255. - If we find one and it's active, then we parse ahead to see if
  7256. we have an inline link/image, reference link/image, compact reference
  7257. link/image, or shortcut reference link/image.
  7258. + If we don't, then we remove the opening delimiter from the
  7259. delimiter stack and return a literal text node `]`.
  7260. + If we do, then
  7261. * We return a link or image node whose children are the inlines
  7262. after the text node pointed to by the opening delimiter.
  7263. * We run *process emphasis* on these inlines, with the `[` opener
  7264. as `stack_bottom`.
  7265. * We remove the opening delimiter.
  7266. * If we have a link (and not an image), we also set all
  7267. `[` delimiters before the opening delimiter to *inactive*. (This
  7268. will prevent us from getting links within links.)
  7269. #### *process emphasis*
  7270. Parameter `stack_bottom` sets a lower bound to how far we
  7271. descend in the [delimiter stack]. If it is NULL, we can
  7272. go all the way to the bottom. Otherwise, we stop before
  7273. visiting `stack_bottom`.
  7274. Let `current_position` point to the element on the [delimiter stack]
  7275. just above `stack_bottom` (or the first element if `stack_bottom`
  7276. is NULL).
  7277. We keep track of the `openers_bottom` for each delimiter
  7278. type (`*`, `_`) and each length of the closing delimiter run
  7279. (modulo 3). Initialize this to `stack_bottom`.
  7280. Then we repeat the following until we run out of potential
  7281. closers:
  7282. - Move `current_position` forward in the delimiter stack (if needed)
  7283. until we find the first potential closer with delimiter `*` or `_`.
  7284. (This will be the potential closer closest
  7285. to the beginning of the input -- the first one in parse order.)
  7286. - Now, look back in the stack (staying above `stack_bottom` and
  7287. the `openers_bottom` for this delimiter type) for the
  7288. first matching potential opener ("matching" means same delimiter).
  7289. - If one is found:
  7290. + Figure out whether we have emphasis or strong emphasis:
  7291. if both closer and opener spans have length >= 2, we have
  7292. strong, otherwise regular.
  7293. + Insert an emph or strong emph node accordingly, after
  7294. the text node corresponding to the opener.
  7295. + Remove any delimiters between the opener and closer from
  7296. the delimiter stack.
  7297. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7298. from the opening and closing text nodes. If they become empty
  7299. as a result, remove them and remove the corresponding element
  7300. of the delimiter stack. If the closing node is removed, reset
  7301. `current_position` to the next element in the stack.
  7302. - If none is found:
  7303. + Set `openers_bottom` to the element before `current_position`.
  7304. (We know that there are no openers for this kind of closer up to and
  7305. including this point, so this puts a lower bound on future searches.)
  7306. + If the closer at `current_position` is not a potential opener,
  7307. remove it from the delimiter stack (since we know it can't
  7308. be a closer either).
  7309. + Advance `current_position` to the next element in the stack.
  7310. After we're done, we remove all delimiters above `stack_bottom` from the
  7311. delimiter stack.