aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 36459b022cd117b93a907139aff83797e4a163a6 (plain)
  1. ---
  2. title: CommonMark-RDF Spec
  3. author: John MacFarlane & Jonas Smedegaard
  4. version: 0.29.0alpha1
  5. date: '2020-06-30'
  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 line feed (`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 line feed (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a line feed, or a carriage return and a
  231. following line feed.
  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 [Unicode whitespace character](@) is
  236. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  237. line feed (`U+000A`), form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Unicode whitespace](@) is a sequence of one or more
  239. [Unicode whitespace characters].
  240. A [tab](@) is `U+0009`.
  241. A [space](@) is `U+0020`.
  242. An [ASCII control character](@) is a character between `U+0000–1F` (both
  243. including) or `U+007F`.
  244. An [ASCII punctuation character](@)
  245. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  246. `*`, `+`, `,`, `-`, `.`, `/` (U+0021–2F),
  247. `:`, `;`, `<`, `=`, `>`, `?`, `@` (U+003A–0040),
  248. `[`, `\`, `]`, `^`, `_`, `` ` `` (U+005B–0060),
  249. `{`, `|`, `}`, or `~` (U+007B–007E).
  250. A [Unicode punctuation character](@) is an [ASCII
  251. punctuation character] or anything in
  252. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  253. ## Tabs
  254. Tabs in lines are not expanded to [spaces]. However,
  255. in contexts where spaces help to define block structure,
  256. tabs behave as if they were replaced by spaces with a tab stop
  257. of 4 characters.
  258. Thus, for example, a tab can be used instead of four spaces
  259. in an indented code block. (Note, however, that internal
  260. tabs are passed through as literal tabs, not expanded to
  261. spaces.)
  262. ```````````````````````````````` example
  263. →foo→baz→→bim
  264. .
  265. <pre><code>foo→baz→→bim
  266. </code></pre>
  267. ````````````````````````````````
  268. ```````````````````````````````` example
  269. →foo→baz→→bim
  270. .
  271. <pre><code>foo→baz→→bim
  272. </code></pre>
  273. ````````````````````````````````
  274. ```````````````````````````````` example
  275. a→a
  276. ὐ→a
  277. .
  278. <pre><code>a→a
  279. ὐ→a
  280. </code></pre>
  281. ````````````````````````````````
  282. In the following example, a continuation paragraph of a list
  283. item is indented with a tab; this has exactly the same effect
  284. as indentation with four spaces would:
  285. ```````````````````````````````` example
  286. - foo
  287. →bar
  288. .
  289. <ul>
  290. <li>
  291. <p>foo</p>
  292. <p>bar</p>
  293. </li>
  294. </ul>
  295. ````````````````````````````````
  296. ```````````````````````````````` example
  297. - foo
  298. →→bar
  299. .
  300. <ul>
  301. <li>
  302. <p>foo</p>
  303. <pre><code> bar
  304. </code></pre>
  305. </li>
  306. </ul>
  307. ````````````````````````````````
  308. Normally the `>` that begins a block quote may be followed
  309. optionally by a space, which is not considered part of the
  310. content. In the following case `>` is followed by a tab,
  311. which is treated as if it were expanded into three spaces.
  312. Since one of these spaces is considered part of the
  313. delimiter, `foo` is considered to be indented six spaces
  314. inside the block quote context, so we get an indented
  315. code block starting with two spaces.
  316. ```````````````````````````````` example
  317. >→→foo
  318. .
  319. <blockquote>
  320. <pre><code> foo
  321. </code></pre>
  322. </blockquote>
  323. ````````````````````````````````
  324. ```````````````````````````````` example
  325. -→→foo
  326. .
  327. <ul>
  328. <li>
  329. <pre><code> foo
  330. </code></pre>
  331. </li>
  332. </ul>
  333. ````````````````````````````````
  334. ```````````````````````````````` example
  335. foo
  336. →bar
  337. .
  338. <pre><code>foo
  339. bar
  340. </code></pre>
  341. ````````````````````````````````
  342. ```````````````````````````````` example
  343. - foo
  344. - bar
  345. → - baz
  346. .
  347. <ul>
  348. <li>foo
  349. <ul>
  350. <li>bar
  351. <ul>
  352. <li>baz</li>
  353. </ul>
  354. </li>
  355. </ul>
  356. </li>
  357. </ul>
  358. ````````````````````````````````
  359. ```````````````````````````````` example
  360. #→Foo
  361. .
  362. <h1>Foo</h1>
  363. ````````````````````````````````
  364. ```````````````````````````````` example
  365. *→*→*→
  366. .
  367. <hr />
  368. ````````````````````````````````
  369. ## Insecure characters
  370. For security reasons, the Unicode character `U+0000` must be replaced
  371. with the REPLACEMENT CHARACTER (`U+FFFD`).
  372. ## Backslash escapes
  373. Any ASCII punctuation character may be backslash-escaped:
  374. ```````````````````````````````` example
  375. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  376. .
  377. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  378. ````````````````````````````````
  379. Backslashes before other characters are treated as literal
  380. backslashes:
  381. ```````````````````````````````` example
  382. \→\A\a\ \3\φ\«
  383. .
  384. <p>\→\A\a\ \3\φ\«</p>
  385. ````````````````````````````````
  386. Escaped characters are treated as regular characters and do
  387. not have their usual Markdown meanings:
  388. ```````````````````````````````` example
  389. \*not emphasized*
  390. \<br/> not a tag
  391. \[not a link](/foo)
  392. \`not code`
  393. 1\. not a list
  394. \* not a list
  395. \# not a heading
  396. \[foo]: /url "not a reference"
  397. \&ouml; not a character entity
  398. .
  399. <p>*not emphasized*
  400. &lt;br/&gt; not a tag
  401. [not a link](/foo)
  402. `not code`
  403. 1. not a list
  404. * not a list
  405. # not a heading
  406. [foo]: /url &quot;not a reference&quot;
  407. &amp;ouml; not a character entity</p>
  408. ````````````````````````````````
  409. If a backslash is itself escaped, the following character is not:
  410. ```````````````````````````````` example
  411. \\*emphasis*
  412. .
  413. <p>\<em>emphasis</em></p>
  414. ````````````````````````````````
  415. A backslash at the end of the line is a [hard line break]:
  416. ```````````````````````````````` example
  417. foo\
  418. bar
  419. .
  420. <p>foo<br />
  421. bar</p>
  422. ````````````````````````````````
  423. Backslash escapes do not work in code blocks, code spans, autolinks, or
  424. raw HTML:
  425. ```````````````````````````````` example
  426. `` \[\` ``
  427. .
  428. <p><code>\[\`</code></p>
  429. ````````````````````````````````
  430. ```````````````````````````````` example
  431. \[\]
  432. .
  433. <pre><code>\[\]
  434. </code></pre>
  435. ````````````````````````````````
  436. ```````````````````````````````` example
  437. ~~~
  438. \[\]
  439. ~~~
  440. .
  441. <pre><code>\[\]
  442. </code></pre>
  443. ````````````````````````````````
  444. ```````````````````````````````` example
  445. <http://example.com?find=\*>
  446. .
  447. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  448. ````````````````````````````````
  449. ```````````````````````````````` example
  450. <a href="/bar\/)">
  451. .
  452. <a href="/bar\/)">
  453. ````````````````````````````````
  454. But they work in all other contexts, including URLs and link titles,
  455. link references, and [info strings] in [fenced code blocks]:
  456. ```````````````````````````````` example
  457. [foo](/bar\* "ti\*tle")
  458. .
  459. <p><a href="/bar*" title="ti*tle">foo</a></p>
  460. ````````````````````````````````
  461. ```````````````````````````````` example
  462. [foo]
  463. [foo]: /bar\* "ti\*tle"
  464. .
  465. <p><a href="/bar*" title="ti*tle">foo</a></p>
  466. ````````````````````````````````
  467. ```````````````````````````````` example
  468. ``` foo\+bar
  469. foo
  470. ```
  471. .
  472. <pre><code class="language-foo+bar">foo
  473. </code></pre>
  474. ````````````````````````````````
  475. ## Entity and numeric character references
  476. Valid HTML entity references and numeric character references
  477. can be used in place of the corresponding Unicode character,
  478. with the following exceptions:
  479. - Entity and character references are not recognized in code
  480. blocks and code spans.
  481. - Entity and character references cannot stand in place of
  482. special characters that define structural elements in
  483. CommonMark. For example, although `&#42;` can be used
  484. in place of a literal `*` character, `&#42;` cannot replace
  485. `*` in emphasis delimiters, bullet list markers, or thematic
  486. breaks.
  487. Conforming CommonMark parsers need not store information about
  488. whether a particular character was represented in the source
  489. using a Unicode character or an entity reference.
  490. [Entity references](@) consist of `&` + any of the valid
  491. HTML5 entity names + `;`. The
  492. document <https://html.spec.whatwg.org/entities.json>
  493. is used as an authoritative source for the valid entity
  494. references and their corresponding code points.
  495. ```````````````````````````````` example
  496. &nbsp; &amp; &copy; &AElig; &Dcaron;
  497. &frac34; &HilbertSpace; &DifferentialD;
  498. &ClockwiseContourIntegral; &ngE;
  499. .
  500. <p>  &amp; © Æ Ď
  501. ¾ ℋ ⅆ
  502. ∲ ≧̸</p>
  503. ````````````````````````````````
  504. [Decimal numeric character
  505. references](@)
  506. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  507. numeric character reference is parsed as the corresponding
  508. Unicode character. Invalid Unicode code points will be replaced by
  509. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  510. the code point `U+0000` will also be replaced by `U+FFFD`.
  511. ```````````````````````````````` example
  512. &#35; &#1234; &#992; &#0;
  513. .
  514. <p># Ӓ Ϡ �</p>
  515. ````````````````````````````````
  516. [Hexadecimal numeric character
  517. references](@) consist of `&#` +
  518. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  519. They too are parsed as the corresponding Unicode character (this
  520. time specified with a hexadecimal numeral instead of decimal).
  521. ```````````````````````````````` example
  522. &#X22; &#XD06; &#xcab;
  523. .
  524. <p>&quot; ആ ಫ</p>
  525. ````````````````````````````````
  526. Here are some nonentities:
  527. ```````````````````````````````` example
  528. &nbsp &x; &#; &#x;
  529. &#87654321;
  530. &#abcdef0;
  531. &ThisIsNotDefined; &hi?;
  532. .
  533. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  534. &amp;#87654321;
  535. &amp;#abcdef0;
  536. &amp;ThisIsNotDefined; &amp;hi?;</p>
  537. ````````````````````````````````
  538. Although HTML5 does accept some entity references
  539. without a trailing semicolon (such as `&copy`), these are not
  540. recognized here, because it makes the grammar too ambiguous:
  541. ```````````````````````````````` example
  542. &copy
  543. .
  544. <p>&amp;copy</p>
  545. ````````````````````````````````
  546. Strings that are not on the list of HTML5 named entities are not
  547. recognized as entity references either:
  548. ```````````````````````````````` example
  549. &MadeUpEntity;
  550. .
  551. <p>&amp;MadeUpEntity;</p>
  552. ````````````````````````````````
  553. Entity and numeric character references are recognized in any
  554. context besides code spans or code blocks, including
  555. URLs, [link titles], and [fenced code block][] [info strings]:
  556. ```````````````````````````````` example
  557. <a href="&ouml;&ouml;.html">
  558. .
  559. <a href="&ouml;&ouml;.html">
  560. ````````````````````````````````
  561. ```````````````````````````````` example
  562. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  563. .
  564. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  565. ````````````````````````````````
  566. ```````````````````````````````` example
  567. [foo]
  568. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  569. .
  570. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  571. ````````````````````````````````
  572. ```````````````````````````````` example
  573. ``` f&ouml;&ouml;
  574. foo
  575. ```
  576. .
  577. <pre><code class="language-föö">foo
  578. </code></pre>
  579. ````````````````````````````````
  580. Entity and numeric character references are treated as literal
  581. text in code spans and code blocks:
  582. ```````````````````````````````` example
  583. `f&ouml;&ouml;`
  584. .
  585. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  586. ````````````````````````````````
  587. ```````````````````````````````` example
  588. f&ouml;f&ouml;
  589. .
  590. <pre><code>f&amp;ouml;f&amp;ouml;
  591. </code></pre>
  592. ````````````````````````````````
  593. Entity and numeric character references cannot be used
  594. in place of symbols indicating structure in CommonMark
  595. documents.
  596. ```````````````````````````````` example
  597. &#42;foo&#42;
  598. *foo*
  599. .
  600. <p>*foo*
  601. <em>foo</em></p>
  602. ````````````````````````````````
  603. ```````````````````````````````` example
  604. &#42; foo
  605. * foo
  606. .
  607. <p>* foo</p>
  608. <ul>
  609. <li>foo</li>
  610. </ul>
  611. ````````````````````````````````
  612. ```````````````````````````````` example
  613. foo&#10;&#10;bar
  614. .
  615. <p>foo
  616. bar</p>
  617. ````````````````````````````````
  618. ```````````````````````````````` example
  619. &#9;foo
  620. .
  621. <p>→foo</p>
  622. ````````````````````````````````
  623. ```````````````````````````````` example
  624. [a](url &quot;tit&quot;)
  625. .
  626. <p>[a](url &quot;tit&quot;)</p>
  627. ````````````````````````````````
  628. # Blocks and inlines
  629. We can think of a document as a sequence of
  630. [blocks](@)---structural elements like paragraphs, block
  631. quotations, lists, headings, rules, and code blocks. Some blocks (like
  632. block quotes and list items) contain other blocks; others (like
  633. headings and paragraphs) contain [inline](@) content---text,
  634. links, emphasized text, images, code spans, and so on.
  635. ## Precedence
  636. Indicators of block structure always take precedence over indicators
  637. of inline structure. So, for example, the following is a list with
  638. two items, not a list with one item containing a code span:
  639. ```````````````````````````````` example
  640. - `one
  641. - two`
  642. .
  643. <ul>
  644. <li>`one</li>
  645. <li>two`</li>
  646. </ul>
  647. ````````````````````````````````
  648. This means that parsing can proceed in two steps: first, the block
  649. structure of the document can be discerned; second, text lines inside
  650. paragraphs, headings, and other block constructs can be parsed for inline
  651. structure. The second step requires information about link reference
  652. definitions that will be available only at the end of the first
  653. step. Note that the first step requires processing lines in sequence,
  654. but the second can be parallelized, since the inline parsing of
  655. one block element does not affect the inline parsing of any other.
  656. ## Container blocks and leaf blocks
  657. We can divide blocks into two types:
  658. [container blocks](@),
  659. which can contain other blocks, and [leaf blocks](@),
  660. which cannot.
  661. # Leaf blocks
  662. This section describes the different kinds of leaf block that make up a
  663. Markdown document.
  664. ## Thematic breaks
  665. A line consisting of optionally up to three spaces of indentation, followed by a
  666. sequence of three or more matching `-`, `_`, or `*` characters, each followed
  667. optionally by any number of spaces or tabs, forms a
  668. [thematic break](@).
  669. ```````````````````````````````` example
  670. ***
  671. ---
  672. ___
  673. .
  674. <hr />
  675. <hr />
  676. <hr />
  677. ````````````````````````````````
  678. Wrong characters:
  679. ```````````````````````````````` example
  680. +++
  681. .
  682. <p>+++</p>
  683. ````````````````````````````````
  684. ```````````````````````````````` example
  685. ===
  686. .
  687. <p>===</p>
  688. ````````````````````````````````
  689. Not enough characters:
  690. ```````````````````````````````` example
  691. --
  692. **
  693. __
  694. .
  695. <p>--
  696. **
  697. __</p>
  698. ````````````````````````````````
  699. Up to three spaces of indentation are allowed:
  700. ```````````````````````````````` example
  701. ***
  702. ***
  703. ***
  704. .
  705. <hr />
  706. <hr />
  707. <hr />
  708. ````````````````````````````````
  709. Four spaces of indentation is too many:
  710. ```````````````````````````````` example
  711. ***
  712. .
  713. <pre><code>***
  714. </code></pre>
  715. ````````````````````````````````
  716. ```````````````````````````````` example
  717. Foo
  718. ***
  719. .
  720. <p>Foo
  721. ***</p>
  722. ````````````````````````````````
  723. More than three characters may be used:
  724. ```````````````````````````````` example
  725. _____________________________________
  726. .
  727. <hr />
  728. ````````````````````````````````
  729. Spaces and tabs are allowed between the characters:
  730. ```````````````````````````````` example
  731. - - -
  732. .
  733. <hr />
  734. ````````````````````````````````
  735. ```````````````````````````````` example
  736. ** * ** * ** * **
  737. .
  738. <hr />
  739. ````````````````````````````````
  740. ```````````````````````````````` example
  741. - - - -
  742. .
  743. <hr />
  744. ````````````````````````````````
  745. Spaces and tabs are allowed at the end:
  746. ```````````````````````````````` example
  747. - - - -
  748. .
  749. <hr />
  750. ````````````````````````````````
  751. However, no other characters may occur in the line:
  752. ```````````````````````````````` example
  753. _ _ _ _ a
  754. a------
  755. ---a---
  756. .
  757. <p>_ _ _ _ a</p>
  758. <p>a------</p>
  759. <p>---a---</p>
  760. ````````````````````````````````
  761. It is required that all of the characters other than spaces or tabs be the same.
  762. So, this is not a thematic break:
  763. ```````````````````````````````` example
  764. *-*
  765. .
  766. <p><em>-</em></p>
  767. ````````````````````````````````
  768. Thematic breaks do not need blank lines before or after:
  769. ```````````````````````````````` example
  770. - foo
  771. ***
  772. - bar
  773. .
  774. <ul>
  775. <li>foo</li>
  776. </ul>
  777. <hr />
  778. <ul>
  779. <li>bar</li>
  780. </ul>
  781. ````````````````````````````````
  782. Thematic breaks can interrupt a paragraph:
  783. ```````````````````````````````` example
  784. Foo
  785. ***
  786. bar
  787. .
  788. <p>Foo</p>
  789. <hr />
  790. <p>bar</p>
  791. ````````````````````````````````
  792. If a line of dashes that meets the above conditions for being a
  793. thematic break could also be interpreted as the underline of a [setext
  794. heading], the interpretation as a
  795. [setext heading] takes precedence. Thus, for example,
  796. this is a setext heading, not a paragraph followed by a thematic break:
  797. ```````````````````````````````` example
  798. Foo
  799. ---
  800. bar
  801. .
  802. <h2>Foo</h2>
  803. <p>bar</p>
  804. ````````````````````````````````
  805. When both a thematic break and a list item are possible
  806. interpretations of a line, the thematic break takes precedence:
  807. ```````````````````````````````` example
  808. * Foo
  809. * * *
  810. * Bar
  811. .
  812. <ul>
  813. <li>Foo</li>
  814. </ul>
  815. <hr />
  816. <ul>
  817. <li>Bar</li>
  818. </ul>
  819. ````````````````````````````````
  820. If you want a thematic break in a list item, use a different bullet:
  821. ```````````````````````````````` example
  822. - Foo
  823. - * * *
  824. .
  825. <ul>
  826. <li>Foo</li>
  827. <li>
  828. <hr />
  829. </li>
  830. </ul>
  831. ````````````````````````````````
  832. ## ATX headings
  833. An [ATX heading](@)
  834. consists of a string of characters, parsed as inline content, between an
  835. opening sequence of 1--6 unescaped `#` characters and an optional
  836. closing sequence of any number of unescaped `#` characters.
  837. The opening sequence of `#` characters must be followed by spaces or tabs, or
  838. by the end of line. The optional closing sequence of `#`s must be preceded by
  839. spaces or tabs and may be followed by spaces or tabs only. The opening
  840. `#` character may be preceded by up to three spaces of indentation. The raw
  841. contents of the heading are stripped of leading and trailing space or tabs
  842. before being parsed as inline content. The heading level is equal to the number
  843. of `#` characters in the opening sequence.
  844. Simple headings:
  845. ```````````````````````````````` example
  846. # foo
  847. ## foo
  848. ### foo
  849. #### foo
  850. ##### foo
  851. ###### foo
  852. .
  853. <h1>foo</h1>
  854. <h2>foo</h2>
  855. <h3>foo</h3>
  856. <h4>foo</h4>
  857. <h5>foo</h5>
  858. <h6>foo</h6>
  859. ````````````````````````````````
  860. More than six `#` characters is not a heading:
  861. ```````````````````````````````` example
  862. ####### foo
  863. .
  864. <p>####### foo</p>
  865. ````````````````````````````````
  866. At least one space or tab is required between the `#` characters and the
  867. heading's contents, unless the heading is empty. Note that many
  868. implementations currently do not require the space. However, the
  869. space was required by the
  870. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  871. and it helps prevent things like the following from being parsed as
  872. headings:
  873. ```````````````````````````````` example
  874. #5 bolt
  875. #hashtag
  876. .
  877. <p>#5 bolt</p>
  878. <p>#hashtag</p>
  879. ````````````````````````````````
  880. This is not a heading, because the first `#` is escaped:
  881. ```````````````````````````````` example
  882. \## foo
  883. .
  884. <p>## foo</p>
  885. ````````````````````````````````
  886. Contents are parsed as inlines:
  887. ```````````````````````````````` example
  888. # foo *bar* \*baz\*
  889. .
  890. <h1>foo <em>bar</em> *baz*</h1>
  891. ````````````````````````````````
  892. Leading and trailing spaces or tabs are ignored in parsing inline content:
  893. ```````````````````````````````` example
  894. # foo
  895. .
  896. <h1>foo</h1>
  897. ````````````````````````````````
  898. Up to three spaces of indentation are allowed:
  899. ```````````````````````````````` example
  900. ### foo
  901. ## foo
  902. # foo
  903. .
  904. <h3>foo</h3>
  905. <h2>foo</h2>
  906. <h1>foo</h1>
  907. ````````````````````````````````
  908. Four spaces of indentation is too many:
  909. ```````````````````````````````` example
  910. # foo
  911. .
  912. <pre><code># foo
  913. </code></pre>
  914. ````````````````````````````````
  915. ```````````````````````````````` example
  916. foo
  917. # bar
  918. .
  919. <p>foo
  920. # bar</p>
  921. ````````````````````````````````
  922. A closing sequence of `#` characters is optional:
  923. ```````````````````````````````` example
  924. ## foo ##
  925. ### bar ###
  926. .
  927. <h2>foo</h2>
  928. <h3>bar</h3>
  929. ````````````````````````````````
  930. It need not be the same length as the opening sequence:
  931. ```````````````````````````````` example
  932. # foo ##################################
  933. ##### foo ##
  934. .
  935. <h1>foo</h1>
  936. <h5>foo</h5>
  937. ````````````````````````````````
  938. Spaces or tabs are allowed after the closing sequence:
  939. ```````````````````````````````` example
  940. ### foo ###
  941. .
  942. <h3>foo</h3>
  943. ````````````````````````````````
  944. A sequence of `#` characters with anything but spaces or tabs following it
  945. is not a closing sequence, but counts as part of the contents of the
  946. heading:
  947. ```````````````````````````````` example
  948. ### foo ### b
  949. .
  950. <h3>foo ### b</h3>
  951. ````````````````````````````````
  952. The closing sequence must be preceded by a space or tab:
  953. ```````````````````````````````` example
  954. # foo#
  955. .
  956. <h1>foo#</h1>
  957. ````````````````````````````````
  958. Backslash-escaped `#` characters do not count as part
  959. of the closing sequence:
  960. ```````````````````````````````` example
  961. ### foo \###
  962. ## foo #\##
  963. # foo \#
  964. .
  965. <h3>foo ###</h3>
  966. <h2>foo ###</h2>
  967. <h1>foo #</h1>
  968. ````````````````````````````````
  969. ATX headings need not be separated from surrounding content by blank
  970. lines, and they can interrupt paragraphs:
  971. ```````````````````````````````` example
  972. ****
  973. ## foo
  974. ****
  975. .
  976. <hr />
  977. <h2>foo</h2>
  978. <hr />
  979. ````````````````````````````````
  980. ```````````````````````````````` example
  981. Foo bar
  982. # baz
  983. Bar foo
  984. .
  985. <p>Foo bar</p>
  986. <h1>baz</h1>
  987. <p>Bar foo</p>
  988. ````````````````````````````````
  989. ATX headings can be empty:
  990. ```````````````````````````````` example
  991. ##
  992. #
  993. ### ###
  994. .
  995. <h2></h2>
  996. <h1></h1>
  997. <h3></h3>
  998. ````````````````````````````````
  999. ## Setext headings
  1000. A [setext heading](@) consists of one or more
  1001. lines of text, not interrupted by a blank line, of which the first line does not
  1002. have more than 3 spaces of indentation, followed by
  1003. a [setext heading underline]. The lines of text must be such
  1004. that, were they not followed by the setext heading underline,
  1005. they would be interpreted as a paragraph: they cannot be
  1006. interpretable as a [code fence], [ATX heading][ATX headings],
  1007. [block quote][block quotes], [thematic break][thematic breaks],
  1008. [list item][list items], or [HTML block][HTML blocks].
  1009. A [setext heading underline](@) is a sequence of
  1010. `=` characters or a sequence of `-` characters, with no more than 3
  1011. spaces of indentation and any number of trailing spaces or tabs. If a line
  1012. containing a single `-` can be interpreted as an
  1013. empty [list items], it should be interpreted this way
  1014. and not as a [setext heading underline].
  1015. The heading is a level 1 heading if `=` characters are used in
  1016. the [setext heading underline], and a level 2 heading if `-`
  1017. characters are used. The contents of the heading are the result
  1018. of parsing the preceding lines of text as CommonMark inline
  1019. content.
  1020. In general, a setext heading need not be preceded or followed by a
  1021. blank line. However, it cannot interrupt a paragraph, so when a
  1022. setext heading comes after a paragraph, a blank line is needed between
  1023. them.
  1024. Simple examples:
  1025. ```````````````````````````````` example
  1026. Foo *bar*
  1027. =========
  1028. Foo *bar*
  1029. ---------
  1030. .
  1031. <h1>Foo <em>bar</em></h1>
  1032. <h2>Foo <em>bar</em></h2>
  1033. ````````````````````````````````
  1034. The content of the header may span more than one line:
  1035. ```````````````````````````````` example
  1036. Foo *bar
  1037. baz*
  1038. ====
  1039. .
  1040. <h1>Foo <em>bar
  1041. baz</em></h1>
  1042. ````````````````````````````````
  1043. The contents are the result of parsing the headings's raw
  1044. content as inlines. The heading's raw content is formed by
  1045. concatenating the lines and removing initial and final
  1046. spaces or tabs.
  1047. ```````````````````````````````` example
  1048. Foo *bar
  1049. baz*→
  1050. ====
  1051. .
  1052. <h1>Foo <em>bar
  1053. baz</em></h1>
  1054. ````````````````````````````````
  1055. The underlining can be any length:
  1056. ```````````````````````````````` example
  1057. Foo
  1058. -------------------------
  1059. Foo
  1060. =
  1061. .
  1062. <h2>Foo</h2>
  1063. <h1>Foo</h1>
  1064. ````````````````````````````````
  1065. The heading content can be preceded by up to three spaces of indentation, and
  1066. need not line up with the underlining:
  1067. ```````````````````````````````` example
  1068. Foo
  1069. ---
  1070. Foo
  1071. -----
  1072. Foo
  1073. ===
  1074. .
  1075. <h2>Foo</h2>
  1076. <h2>Foo</h2>
  1077. <h1>Foo</h1>
  1078. ````````````````````````````````
  1079. Four spaces of indentation is too many:
  1080. ```````````````````````````````` example
  1081. Foo
  1082. ---
  1083. Foo
  1084. ---
  1085. .
  1086. <pre><code>Foo
  1087. ---
  1088. Foo
  1089. </code></pre>
  1090. <hr />
  1091. ````````````````````````````````
  1092. The setext heading underline can be preceded by up to three spaces of
  1093. indentation, and may have trailing spaces or tabs:
  1094. ```````````````````````````````` example
  1095. Foo
  1096. ----
  1097. .
  1098. <h2>Foo</h2>
  1099. ````````````````````````````````
  1100. Four spaces of indentation is too many:
  1101. ```````````````````````````````` example
  1102. Foo
  1103. ---
  1104. .
  1105. <p>Foo
  1106. ---</p>
  1107. ````````````````````````````````
  1108. The setext heading underline cannot contain internal spaces or tabs:
  1109. ```````````````````````````````` example
  1110. Foo
  1111. = =
  1112. Foo
  1113. --- -
  1114. .
  1115. <p>Foo
  1116. = =</p>
  1117. <p>Foo</p>
  1118. <hr />
  1119. ````````````````````````````````
  1120. Trailing spaces or tabs in the content line do not cause a hard line break:
  1121. ```````````````````````````````` example
  1122. Foo
  1123. -----
  1124. .
  1125. <h2>Foo</h2>
  1126. ````````````````````````````````
  1127. Nor does a backslash at the end:
  1128. ```````````````````````````````` example
  1129. Foo\
  1130. ----
  1131. .
  1132. <h2>Foo\</h2>
  1133. ````````````````````````````````
  1134. Since indicators of block structure take precedence over
  1135. indicators of inline structure, the following are setext headings:
  1136. ```````````````````````````````` example
  1137. `Foo
  1138. ----
  1139. `
  1140. <a title="a lot
  1141. ---
  1142. of dashes"/>
  1143. .
  1144. <h2>`Foo</h2>
  1145. <p>`</p>
  1146. <h2>&lt;a title=&quot;a lot</h2>
  1147. <p>of dashes&quot;/&gt;</p>
  1148. ````````````````````````````````
  1149. The setext heading underline cannot be a [lazy continuation
  1150. line] in a list item or block quote:
  1151. ```````````````````````````````` example
  1152. > Foo
  1153. ---
  1154. .
  1155. <blockquote>
  1156. <p>Foo</p>
  1157. </blockquote>
  1158. <hr />
  1159. ````````````````````````````````
  1160. ```````````````````````````````` example
  1161. > foo
  1162. bar
  1163. ===
  1164. .
  1165. <blockquote>
  1166. <p>foo
  1167. bar
  1168. ===</p>
  1169. </blockquote>
  1170. ````````````````````````````````
  1171. ```````````````````````````````` example
  1172. - Foo
  1173. ---
  1174. .
  1175. <ul>
  1176. <li>Foo</li>
  1177. </ul>
  1178. <hr />
  1179. ````````````````````````````````
  1180. A blank line is needed between a paragraph and a following
  1181. setext heading, since otherwise the paragraph becomes part
  1182. of the heading's content:
  1183. ```````````````````````````````` example
  1184. Foo
  1185. Bar
  1186. ---
  1187. .
  1188. <h2>Foo
  1189. Bar</h2>
  1190. ````````````````````````````````
  1191. But in general a blank line is not required before or after
  1192. setext headings:
  1193. ```````````````````````````````` example
  1194. ---
  1195. Foo
  1196. ---
  1197. Bar
  1198. ---
  1199. Baz
  1200. .
  1201. <hr />
  1202. <h2>Foo</h2>
  1203. <h2>Bar</h2>
  1204. <p>Baz</p>
  1205. ````````````````````````````````
  1206. Setext headings cannot be empty:
  1207. ```````````````````````````````` example
  1208. ====
  1209. .
  1210. <p>====</p>
  1211. ````````````````````````````````
  1212. Setext heading text lines must not be interpretable as block
  1213. constructs other than paragraphs. So, the line of dashes
  1214. in these examples gets interpreted as a thematic break:
  1215. ```````````````````````````````` example
  1216. ---
  1217. ---
  1218. .
  1219. <hr />
  1220. <hr />
  1221. ````````````````````````````````
  1222. ```````````````````````````````` example
  1223. - foo
  1224. -----
  1225. .
  1226. <ul>
  1227. <li>foo</li>
  1228. </ul>
  1229. <hr />
  1230. ````````````````````````````````
  1231. ```````````````````````````````` example
  1232. foo
  1233. ---
  1234. .
  1235. <pre><code>foo
  1236. </code></pre>
  1237. <hr />
  1238. ````````````````````````````````
  1239. ```````````````````````````````` example
  1240. > foo
  1241. -----
  1242. .
  1243. <blockquote>
  1244. <p>foo</p>
  1245. </blockquote>
  1246. <hr />
  1247. ````````````````````````````````
  1248. If you want a heading with `> foo` as its literal text, you can
  1249. use backslash escapes:
  1250. ```````````````````````````````` example
  1251. \> foo
  1252. ------
  1253. .
  1254. <h2>&gt; foo</h2>
  1255. ````````````````````````````````
  1256. **Compatibility note:** Most existing Markdown implementations
  1257. do not allow the text of setext headings to span multiple lines.
  1258. But there is no consensus about how to interpret
  1259. ``` markdown
  1260. Foo
  1261. bar
  1262. ---
  1263. baz
  1264. ```
  1265. One can find four different interpretations:
  1266. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1267. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1268. 3. paragraph "Foo bar --- baz"
  1269. 4. heading "Foo bar", paragraph "baz"
  1270. We find interpretation 4 most natural, and interpretation 4
  1271. increases the expressive power of CommonMark, by allowing
  1272. multiline headings. Authors who want interpretation 1 can
  1273. put a blank line after the first paragraph:
  1274. ```````````````````````````````` example
  1275. Foo
  1276. bar
  1277. ---
  1278. baz
  1279. .
  1280. <p>Foo</p>
  1281. <h2>bar</h2>
  1282. <p>baz</p>
  1283. ````````````````````````````````
  1284. Authors who want interpretation 2 can put blank lines around
  1285. the thematic break,
  1286. ```````````````````````````````` example
  1287. Foo
  1288. bar
  1289. ---
  1290. baz
  1291. .
  1292. <p>Foo
  1293. bar</p>
  1294. <hr />
  1295. <p>baz</p>
  1296. ````````````````````````````````
  1297. or use a thematic break that cannot count as a [setext heading
  1298. underline], such as
  1299. ```````````````````````````````` example
  1300. Foo
  1301. bar
  1302. * * *
  1303. baz
  1304. .
  1305. <p>Foo
  1306. bar</p>
  1307. <hr />
  1308. <p>baz</p>
  1309. ````````````````````````````````
  1310. Authors who want interpretation 3 can use backslash escapes:
  1311. ```````````````````````````````` example
  1312. Foo
  1313. bar
  1314. \---
  1315. baz
  1316. .
  1317. <p>Foo
  1318. bar
  1319. ---
  1320. baz</p>
  1321. ````````````````````````````````
  1322. ## Indented code blocks
  1323. An [indented code block](@) is composed of one or more
  1324. [indented chunks] separated by blank lines.
  1325. An [indented chunk](@) is a sequence of non-blank lines,
  1326. each preceded by four or more spaces of indentation. The contents of the code
  1327. block are the literal contents of the lines, including trailing
  1328. [line endings], minus four spaces of indentation.
  1329. An indented code block has no [info string].
  1330. An indented code block cannot interrupt a paragraph, so there must be
  1331. a blank line between a paragraph and a following indented code block.
  1332. (A blank line is not needed, however, between a code block and a following
  1333. paragraph.)
  1334. ```````````````````````````````` example
  1335. a simple
  1336. indented code block
  1337. .
  1338. <pre><code>a simple
  1339. indented code block
  1340. </code></pre>
  1341. ````````````````````````````````
  1342. If there is any ambiguity between an interpretation of indentation
  1343. as a code block and as indicating that material belongs to a [list
  1344. item][list items], the list item interpretation takes precedence:
  1345. ```````````````````````````````` example
  1346. - foo
  1347. bar
  1348. .
  1349. <ul>
  1350. <li>
  1351. <p>foo</p>
  1352. <p>bar</p>
  1353. </li>
  1354. </ul>
  1355. ````````````````````````````````
  1356. ```````````````````````````````` example
  1357. 1. foo
  1358. - bar
  1359. .
  1360. <ol>
  1361. <li>
  1362. <p>foo</p>
  1363. <ul>
  1364. <li>bar</li>
  1365. </ul>
  1366. </li>
  1367. </ol>
  1368. ````````````````````````````````
  1369. The contents of a code block are literal text, and do not get parsed
  1370. as Markdown:
  1371. ```````````````````````````````` example
  1372. <a/>
  1373. *hi*
  1374. - one
  1375. .
  1376. <pre><code>&lt;a/&gt;
  1377. *hi*
  1378. - one
  1379. </code></pre>
  1380. ````````````````````````````````
  1381. Here we have three chunks separated by blank lines:
  1382. ```````````````````````````````` example
  1383. chunk1
  1384. chunk2
  1385. chunk3
  1386. .
  1387. <pre><code>chunk1
  1388. chunk2
  1389. chunk3
  1390. </code></pre>
  1391. ````````````````````````````````
  1392. Any initial spaces or tabs beyond four spaces of indentation will be included in
  1393. the content, even in interior blank lines:
  1394. ```````````````````````````````` example
  1395. chunk1
  1396. chunk2
  1397. .
  1398. <pre><code>chunk1
  1399. chunk2
  1400. </code></pre>
  1401. ````````````````````````````````
  1402. An indented code block cannot interrupt a paragraph. (This
  1403. allows hanging indents and the like.)
  1404. ```````````````````````````````` example
  1405. Foo
  1406. bar
  1407. .
  1408. <p>Foo
  1409. bar</p>
  1410. ````````````````````````````````
  1411. However, any non-blank line with fewer than four spaces of indentation ends
  1412. the code block immediately. So a paragraph may occur immediately
  1413. after indented code:
  1414. ```````````````````````````````` example
  1415. foo
  1416. bar
  1417. .
  1418. <pre><code>foo
  1419. </code></pre>
  1420. <p>bar</p>
  1421. ````````````````````````````````
  1422. And indented code can occur immediately before and after other kinds of
  1423. blocks:
  1424. ```````````````````````````````` example
  1425. # Heading
  1426. foo
  1427. Heading
  1428. ------
  1429. foo
  1430. ----
  1431. .
  1432. <h1>Heading</h1>
  1433. <pre><code>foo
  1434. </code></pre>
  1435. <h2>Heading</h2>
  1436. <pre><code>foo
  1437. </code></pre>
  1438. <hr />
  1439. ````````````````````````````````
  1440. The first line can be preceded by more than four spaces of indentation:
  1441. ```````````````````````````````` example
  1442. foo
  1443. bar
  1444. .
  1445. <pre><code> foo
  1446. bar
  1447. </code></pre>
  1448. ````````````````````````````````
  1449. Blank lines preceding or following an indented code block
  1450. are not included in it:
  1451. ```````````````````````````````` example
  1452. foo
  1453. .
  1454. <pre><code>foo
  1455. </code></pre>
  1456. ````````````````````````````````
  1457. Trailing spaces or tabs are included in the code block's content:
  1458. ```````````````````````````````` example
  1459. foo
  1460. .
  1461. <pre><code>foo
  1462. </code></pre>
  1463. ````````````````````````````````
  1464. ## Fenced code blocks
  1465. A [code fence](@) is a sequence
  1466. of at least three consecutive backtick characters (`` ` ``) or
  1467. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1468. A [fenced code block](@)
  1469. begins with a code fence, preceded by up to three spaces of indentation.
  1470. The line with the opening code fence may optionally contain some text
  1471. following the code fence; this is trimmed of leading and trailing
  1472. spaces or tabs and called the [info string](@). If the [info string] comes
  1473. after a backtick fence, it may not contain any backtick
  1474. characters. (The reason for this restriction is that otherwise
  1475. some inline code would be incorrectly interpreted as the
  1476. beginning of a fenced code block.)
  1477. The content of the code block consists of all subsequent lines, until
  1478. a closing [code fence] of the same type as the code block
  1479. began with (backticks or tildes), and with at least as many backticks
  1480. or tildes as the opening code fence. If the leading code fence is
  1481. preceded by N spaces of indentation, then up to N spaces of indentation are
  1482. removed from each line of the content (if present). (If a content line is not
  1483. indented, it is preserved unchanged. If it is indented N spaces or less, all
  1484. of the indentation is removed.)
  1485. The closing code fence may be preceded by up to three spaces of indentation, and
  1486. may be followed only by spaces or tabs, which are ignored. If the end of the
  1487. containing block (or document) is reached and no closing code fence
  1488. has been found, the code block contains all of the lines after the
  1489. opening code fence until the end of the containing block (or
  1490. document). (An alternative spec would require backtracking in the
  1491. event that a closing code fence is not found. But this makes parsing
  1492. much less efficient, and there seems to be no real down side to the
  1493. behavior described here.)
  1494. A fenced code block may interrupt a paragraph, and does not require
  1495. a blank line either before or after.
  1496. The content of a code fence is treated as literal text, not parsed
  1497. as inlines. The first word of the [info string] is typically used to
  1498. specify the language of the code sample, and rendered in the `class`
  1499. attribute of the `code` tag. However, this spec does not mandate any
  1500. particular treatment of the [info string].
  1501. Here is a simple example with backticks:
  1502. ```````````````````````````````` example
  1503. ```
  1504. <
  1505. >
  1506. ```
  1507. .
  1508. <pre><code>&lt;
  1509. &gt;
  1510. </code></pre>
  1511. ````````````````````````````````
  1512. With tildes:
  1513. ```````````````````````````````` example
  1514. ~~~
  1515. <
  1516. >
  1517. ~~~
  1518. .
  1519. <pre><code>&lt;
  1520. &gt;
  1521. </code></pre>
  1522. ````````````````````````````````
  1523. Fewer than three backticks is not enough:
  1524. ```````````````````````````````` example
  1525. ``
  1526. foo
  1527. ``
  1528. .
  1529. <p><code>foo</code></p>
  1530. ````````````````````````````````
  1531. The closing code fence must use the same character as the opening
  1532. fence:
  1533. ```````````````````````````````` example
  1534. ```
  1535. aaa
  1536. ~~~
  1537. ```
  1538. .
  1539. <pre><code>aaa
  1540. ~~~
  1541. </code></pre>
  1542. ````````````````````````````````
  1543. ```````````````````````````````` example
  1544. ~~~
  1545. aaa
  1546. ```
  1547. ~~~
  1548. .
  1549. <pre><code>aaa
  1550. ```
  1551. </code></pre>
  1552. ````````````````````````````````
  1553. The closing code fence must be at least as long as the opening fence:
  1554. ```````````````````````````````` example
  1555. ````
  1556. aaa
  1557. ```
  1558. ``````
  1559. .
  1560. <pre><code>aaa
  1561. ```
  1562. </code></pre>
  1563. ````````````````````````````````
  1564. ```````````````````````````````` example
  1565. ~~~~
  1566. aaa
  1567. ~~~
  1568. ~~~~
  1569. .
  1570. <pre><code>aaa
  1571. ~~~
  1572. </code></pre>
  1573. ````````````````````````````````
  1574. Unclosed code blocks are closed by the end of the document
  1575. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1576. ```````````````````````````````` example
  1577. ```
  1578. .
  1579. <pre><code></code></pre>
  1580. ````````````````````````````````
  1581. ```````````````````````````````` example
  1582. `````
  1583. ```
  1584. aaa
  1585. .
  1586. <pre><code>
  1587. ```
  1588. aaa
  1589. </code></pre>
  1590. ````````````````````````````````
  1591. ```````````````````````````````` example
  1592. > ```
  1593. > aaa
  1594. bbb
  1595. .
  1596. <blockquote>
  1597. <pre><code>aaa
  1598. </code></pre>
  1599. </blockquote>
  1600. <p>bbb</p>
  1601. ````````````````````````````````
  1602. A code block can have all empty lines as its content:
  1603. ```````````````````````````````` example
  1604. ```
  1605. ```
  1606. .
  1607. <pre><code>
  1608. </code></pre>
  1609. ````````````````````````````````
  1610. A code block can be empty:
  1611. ```````````````````````````````` example
  1612. ```
  1613. ```
  1614. .
  1615. <pre><code></code></pre>
  1616. ````````````````````````````````
  1617. Fences can be indented. If the opening fence is indented,
  1618. content lines will have equivalent opening indentation removed,
  1619. if present:
  1620. ```````````````````````````````` example
  1621. ```
  1622. aaa
  1623. aaa
  1624. ```
  1625. .
  1626. <pre><code>aaa
  1627. aaa
  1628. </code></pre>
  1629. ````````````````````````````````
  1630. ```````````````````````````````` example
  1631. ```
  1632. aaa
  1633. aaa
  1634. aaa
  1635. ```
  1636. .
  1637. <pre><code>aaa
  1638. aaa
  1639. aaa
  1640. </code></pre>
  1641. ````````````````````````````````
  1642. ```````````````````````````````` example
  1643. ```
  1644. aaa
  1645. aaa
  1646. aaa
  1647. ```
  1648. .
  1649. <pre><code>aaa
  1650. aaa
  1651. aaa
  1652. </code></pre>
  1653. ````````````````````````````````
  1654. Four spaces of indentation is too many:
  1655. ```````````````````````````````` example
  1656. ```
  1657. aaa
  1658. ```
  1659. .
  1660. <pre><code>```
  1661. aaa
  1662. ```
  1663. </code></pre>
  1664. ````````````````````````````````
  1665. Closing fences may be preceded by up to three spaces of indentation, and their
  1666. indentation need not match that of the opening fence:
  1667. ```````````````````````````````` example
  1668. ```
  1669. aaa
  1670. ```
  1671. .
  1672. <pre><code>aaa
  1673. </code></pre>
  1674. ````````````````````````````````
  1675. ```````````````````````````````` example
  1676. ```
  1677. aaa
  1678. ```
  1679. .
  1680. <pre><code>aaa
  1681. </code></pre>
  1682. ````````````````````````````````
  1683. This is not a closing fence, because it is indented 4 spaces:
  1684. ```````````````````````````````` example
  1685. ```
  1686. aaa
  1687. ```
  1688. .
  1689. <pre><code>aaa
  1690. ```
  1691. </code></pre>
  1692. ````````````````````````````````
  1693. Code fences (opening and closing) cannot contain internal spaces or tabs:
  1694. ```````````````````````````````` example
  1695. ``` ```
  1696. aaa
  1697. .
  1698. <p><code> </code>
  1699. aaa</p>
  1700. ````````````````````````````````
  1701. ```````````````````````````````` example
  1702. ~~~~~~
  1703. aaa
  1704. ~~~ ~~
  1705. .
  1706. <pre><code>aaa
  1707. ~~~ ~~
  1708. </code></pre>
  1709. ````````````````````````````````
  1710. Fenced code blocks can interrupt paragraphs, and can be followed
  1711. directly by paragraphs, without a blank line between:
  1712. ```````````````````````````````` example
  1713. foo
  1714. ```
  1715. bar
  1716. ```
  1717. baz
  1718. .
  1719. <p>foo</p>
  1720. <pre><code>bar
  1721. </code></pre>
  1722. <p>baz</p>
  1723. ````````````````````````````````
  1724. Other blocks can also occur before and after fenced code blocks
  1725. without an intervening blank line:
  1726. ```````````````````````````````` example
  1727. foo
  1728. ---
  1729. ~~~
  1730. bar
  1731. ~~~
  1732. # baz
  1733. .
  1734. <h2>foo</h2>
  1735. <pre><code>bar
  1736. </code></pre>
  1737. <h1>baz</h1>
  1738. ````````````````````````````````
  1739. An [info string] can be provided after the opening code fence.
  1740. Although this spec doesn't mandate any particular treatment of
  1741. the info string, the first word is typically used to specify
  1742. the language of the code block. In HTML output, the language is
  1743. normally indicated by adding a class to the `code` element consisting
  1744. of `language-` followed by the language name.
  1745. ```````````````````````````````` example
  1746. ```ruby
  1747. def foo(x)
  1748. return 3
  1749. end
  1750. ```
  1751. .
  1752. <pre><code class="language-ruby">def foo(x)
  1753. return 3
  1754. end
  1755. </code></pre>
  1756. ````````````````````````````````
  1757. ```````````````````````````````` example
  1758. ~~~~ ruby startline=3 $%@#$
  1759. def foo(x)
  1760. return 3
  1761. end
  1762. ~~~~~~~
  1763. .
  1764. <pre><code class="language-ruby">def foo(x)
  1765. return 3
  1766. end
  1767. </code></pre>
  1768. ````````````````````````````````
  1769. ```````````````````````````````` example
  1770. ````;
  1771. ````
  1772. .
  1773. <pre><code class="language-;"></code></pre>
  1774. ````````````````````````````````
  1775. [Info strings] for backtick code blocks cannot contain backticks:
  1776. ```````````````````````````````` example
  1777. ``` aa ```
  1778. foo
  1779. .
  1780. <p><code>aa</code>
  1781. foo</p>
  1782. ````````````````````````````````
  1783. [Info strings] for tilde code blocks can contain backticks and tildes:
  1784. ```````````````````````````````` example
  1785. ~~~ aa ``` ~~~
  1786. foo
  1787. ~~~
  1788. .
  1789. <pre><code class="language-aa">foo
  1790. </code></pre>
  1791. ````````````````````````````````
  1792. Closing code fences cannot have [info strings]:
  1793. ```````````````````````````````` example
  1794. ```
  1795. ``` aaa
  1796. ```
  1797. .
  1798. <pre><code>``` aaa
  1799. </code></pre>
  1800. ````````````````````````````````
  1801. ## HTML blocks
  1802. An [HTML block](@) is a group of lines that is treated
  1803. as raw HTML (and will not be escaped in HTML output).
  1804. There are seven kinds of [HTML block], which can be defined by their
  1805. start and end conditions. The block begins with a line that meets a
  1806. [start condition](@) (after up to three optional spaces of indentation).
  1807. It ends with the first subsequent line that meets a matching [end
  1808. condition](@), or the last line of the document, or the last line of
  1809. the [container block](#container-blocks) containing the current HTML
  1810. block, if no line is encountered that meets the [end condition]. If
  1811. the first line meets both the [start condition] and the [end
  1812. condition], the block will contain just that line.
  1813. 1. **Start condition:** line begins with the string `<script`,
  1814. `<pre`, or `<style` (case-insensitive), followed by a space, a tab,
  1815. the string `>`, or the end of the line.\
  1816. **End condition:** line contains an end tag
  1817. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1818. need not match the start tag).
  1819. 2. **Start condition:** line begins with the string `<!--`.\
  1820. **End condition:** line contains the string `-->`.
  1821. 3. **Start condition:** line begins with the string `<?`.\
  1822. **End condition:** line contains the string `?>`.
  1823. 4. **Start condition:** line begins with the string `<!`
  1824. followed by an ASCII letter.\
  1825. **End condition:** line contains the character `>`.
  1826. 5. **Start condition:** line begins with the string
  1827. `<![CDATA[`.\
  1828. **End condition:** line contains the string `]]>`.
  1829. 6. **Start condition:** line begins the string `<` or `</`
  1830. followed by one of the strings (case-insensitive) `address`,
  1831. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1832. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1833. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1834. `footer`, `form`, `frame`, `frameset`,
  1835. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1836. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1837. `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1838. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1839. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1840. by a space, a tab, the end of the line, the string `>`, or
  1841. the string `/>`.\
  1842. **End condition:** line is followed by a [blank line].
  1843. 7. **Start condition:** line begins with a complete [open tag]
  1844. (with any [tag name] other than `script`,
  1845. `style`, or `pre`) or a complete [closing tag],
  1846. followed only by a space, a tab, or the end of the line.\
  1847. **End condition:** line is followed by a [blank line].
  1848. HTML blocks continue until they are closed by their appropriate
  1849. [end condition], or the last line of the document or other [container
  1850. block](#container-blocks). This means any HTML **within an HTML
  1851. block** that might otherwise be recognised as a start condition will
  1852. be ignored by the parser and passed through as-is, without changing
  1853. the parser's state.
  1854. For instance, `<pre>` within an HTML block started by `<table>` will not affect
  1855. the parser state; as the HTML block was started in by start condition 6, it
  1856. will end at any blank line. This can be surprising:
  1857. ```````````````````````````````` example
  1858. <table><tr><td>
  1859. <pre>
  1860. **Hello**,
  1861. _world_.
  1862. </pre>
  1863. </td></tr></table>
  1864. .
  1865. <table><tr><td>
  1866. <pre>
  1867. **Hello**,
  1868. <p><em>world</em>.
  1869. </pre></p>
  1870. </td></tr></table>
  1871. ````````````````````````````````
  1872. In this case, the HTML block is terminated by the blank line — the `**Hello**`
  1873. text remains verbatim — and regular parsing resumes, with a paragraph,
  1874. emphasised `world` and inline and block HTML following.
  1875. All types of [HTML blocks] except type 7 may interrupt
  1876. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1877. (This restriction is intended to prevent unwanted interpretation
  1878. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1879. Some simple examples follow. Here are some basic HTML blocks
  1880. of type 6:
  1881. ```````````````````````````````` example
  1882. <table>
  1883. <tr>
  1884. <td>
  1885. hi
  1886. </td>
  1887. </tr>
  1888. </table>
  1889. okay.
  1890. .
  1891. <table>
  1892. <tr>
  1893. <td>
  1894. hi
  1895. </td>
  1896. </tr>
  1897. </table>
  1898. <p>okay.</p>
  1899. ````````````````````````````````
  1900. ```````````````````````````````` example
  1901. <div>
  1902. *hello*
  1903. <foo><a>
  1904. .
  1905. <div>
  1906. *hello*
  1907. <foo><a>
  1908. ````````````````````````````````
  1909. A block can also start with a closing tag:
  1910. ```````````````````````````````` example
  1911. </div>
  1912. *foo*
  1913. .
  1914. </div>
  1915. *foo*
  1916. ````````````````````````````````
  1917. Here we have two HTML blocks with a Markdown paragraph between them:
  1918. ```````````````````````````````` example
  1919. <DIV CLASS="foo">
  1920. *Markdown*
  1921. </DIV>
  1922. .
  1923. <DIV CLASS="foo">
  1924. <p><em>Markdown</em></p>
  1925. </DIV>
  1926. ````````````````````````````````
  1927. The tag on the first line can be partial, as long
  1928. as it is split where there would be whitespace:
  1929. ```````````````````````````````` example
  1930. <div id="foo"
  1931. class="bar">
  1932. </div>
  1933. .
  1934. <div id="foo"
  1935. class="bar">
  1936. </div>
  1937. ````````````````````````````````
  1938. ```````````````````````````````` example
  1939. <div id="foo" class="bar
  1940. baz">
  1941. </div>
  1942. .
  1943. <div id="foo" class="bar
  1944. baz">
  1945. </div>
  1946. ````````````````````````````````
  1947. An open tag need not be closed:
  1948. ```````````````````````````````` example
  1949. <div>
  1950. *foo*
  1951. *bar*
  1952. .
  1953. <div>
  1954. *foo*
  1955. <p><em>bar</em></p>
  1956. ````````````````````````````````
  1957. A partial tag need not even be completed (garbage
  1958. in, garbage out):
  1959. ```````````````````````````````` example
  1960. <div id="foo"
  1961. *hi*
  1962. .
  1963. <div id="foo"
  1964. *hi*
  1965. ````````````````````````````````
  1966. ```````````````````````````````` example
  1967. <div class
  1968. foo
  1969. .
  1970. <div class
  1971. foo
  1972. ````````````````````````````````
  1973. The initial tag doesn't even need to be a valid
  1974. tag, as long as it starts like one:
  1975. ```````````````````````````````` example
  1976. <div *???-&&&-<---
  1977. *foo*
  1978. .
  1979. <div *???-&&&-<---
  1980. *foo*
  1981. ````````````````````````````````
  1982. In type 6 blocks, the initial tag need not be on a line by
  1983. itself:
  1984. ```````````````````````````````` example
  1985. <div><a href="bar">*foo*</a></div>
  1986. .
  1987. <div><a href="bar">*foo*</a></div>
  1988. ````````````````````````````````
  1989. ```````````````````````````````` example
  1990. <table><tr><td>
  1991. foo
  1992. </td></tr></table>
  1993. .
  1994. <table><tr><td>
  1995. foo
  1996. </td></tr></table>
  1997. ````````````````````````````````
  1998. Everything until the next blank line or end of document
  1999. gets included in the HTML block. So, in the following
  2000. example, what looks like a Markdown code block
  2001. is actually part of the HTML block, which continues until a blank
  2002. line or the end of the document is reached:
  2003. ```````````````````````````````` example
  2004. <div></div>
  2005. ``` c
  2006. int x = 33;
  2007. ```
  2008. .
  2009. <div></div>
  2010. ``` c
  2011. int x = 33;
  2012. ```
  2013. ````````````````````````````````
  2014. To start an [HTML block] with a tag that is *not* in the
  2015. list of block-level tags in (6), you must put the tag by
  2016. itself on the first line (and it must be complete):
  2017. ```````````````````````````````` example
  2018. <a href="foo">
  2019. *bar*
  2020. </a>
  2021. .
  2022. <a href="foo">
  2023. *bar*
  2024. </a>
  2025. ````````````````````````````````
  2026. In type 7 blocks, the [tag name] can be anything:
  2027. ```````````````````````````````` example
  2028. <Warning>
  2029. *bar*
  2030. </Warning>
  2031. .
  2032. <Warning>
  2033. *bar*
  2034. </Warning>
  2035. ````````````````````````````````
  2036. ```````````````````````````````` example
  2037. <i class="foo">
  2038. *bar*
  2039. </i>
  2040. .
  2041. <i class="foo">
  2042. *bar*
  2043. </i>
  2044. ````````````````````````````````
  2045. ```````````````````````````````` example
  2046. </ins>
  2047. *bar*
  2048. .
  2049. </ins>
  2050. *bar*
  2051. ````````````````````````````````
  2052. These rules are designed to allow us to work with tags that
  2053. can function as either block-level or inline-level tags.
  2054. The `<del>` tag is a nice example. We can surround content with
  2055. `<del>` tags in three different ways. In this case, we get a raw
  2056. HTML block, because the `<del>` tag is on a line by itself:
  2057. ```````````````````````````````` example
  2058. <del>
  2059. *foo*
  2060. </del>
  2061. .
  2062. <del>
  2063. *foo*
  2064. </del>
  2065. ````````````````````````````````
  2066. In this case, we get a raw HTML block that just includes
  2067. the `<del>` tag (because it ends with the following blank
  2068. line). So the contents get interpreted as CommonMark:
  2069. ```````````````````````````````` example
  2070. <del>
  2071. *foo*
  2072. </del>
  2073. .
  2074. <del>
  2075. <p><em>foo</em></p>
  2076. </del>
  2077. ````````````````````````````````
  2078. Finally, in this case, the `<del>` tags are interpreted
  2079. as [raw HTML] *inside* the CommonMark paragraph. (Because
  2080. the tag is not on a line by itself, we get inline HTML
  2081. rather than an [HTML block].)
  2082. ```````````````````````````````` example
  2083. <del>*foo*</del>
  2084. .
  2085. <p><del><em>foo</em></del></p>
  2086. ````````````````````````````````
  2087. HTML tags designed to contain literal content
  2088. (`script`, `style`, `pre`), comments, processing instructions,
  2089. and declarations are treated somewhat differently.
  2090. Instead of ending at the first blank line, these blocks
  2091. end at the first line containing a corresponding end tag.
  2092. As a result, these blocks can contain blank lines:
  2093. A pre tag (type 1):
  2094. ```````````````````````````````` example
  2095. <pre language="haskell"><code>
  2096. import Text.HTML.TagSoup
  2097. main :: IO ()
  2098. main = print $ parseTags tags
  2099. </code></pre>
  2100. okay
  2101. .
  2102. <pre language="haskell"><code>
  2103. import Text.HTML.TagSoup
  2104. main :: IO ()
  2105. main = print $ parseTags tags
  2106. </code></pre>
  2107. <p>okay</p>
  2108. ````````````````````````````````
  2109. A script tag (type 1):
  2110. ```````````````````````````````` example
  2111. <script type="text/javascript">
  2112. // JavaScript example
  2113. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  2114. </script>
  2115. okay
  2116. .
  2117. <script type="text/javascript">
  2118. // JavaScript example
  2119. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  2120. </script>
  2121. <p>okay</p>
  2122. ````````````````````````````````
  2123. A style tag (type 1):
  2124. ```````````````````````````````` example
  2125. <style
  2126. type="text/css">
  2127. h1 {color:red;}
  2128. p {color:blue;}
  2129. </style>
  2130. okay
  2131. .
  2132. <style
  2133. type="text/css">
  2134. h1 {color:red;}
  2135. p {color:blue;}
  2136. </style>
  2137. <p>okay</p>
  2138. ````````````````````````````````
  2139. If there is no matching end tag, the block will end at the
  2140. end of the document (or the enclosing [block quote][block quotes]
  2141. or [list item][list items]):
  2142. ```````````````````````````````` example
  2143. <style
  2144. type="text/css">
  2145. foo
  2146. .
  2147. <style
  2148. type="text/css">
  2149. foo
  2150. ````````````````````````````````
  2151. ```````````````````````````````` example
  2152. > <div>
  2153. > foo
  2154. bar
  2155. .
  2156. <blockquote>
  2157. <div>
  2158. foo
  2159. </blockquote>
  2160. <p>bar</p>
  2161. ````````````````````````````````
  2162. ```````````````````````````````` example
  2163. - <div>
  2164. - foo
  2165. .
  2166. <ul>
  2167. <li>
  2168. <div>
  2169. </li>
  2170. <li>foo</li>
  2171. </ul>
  2172. ````````````````````````````````
  2173. The end tag can occur on the same line as the start tag:
  2174. ```````````````````````````````` example
  2175. <style>p{color:red;}</style>
  2176. *foo*
  2177. .
  2178. <style>p{color:red;}</style>
  2179. <p><em>foo</em></p>
  2180. ````````````````````````````````
  2181. ```````````````````````````````` example
  2182. <!-- foo -->*bar*
  2183. *baz*
  2184. .
  2185. <!-- foo -->*bar*
  2186. <p><em>baz</em></p>
  2187. ````````````````````````````````
  2188. Note that anything on the last line after the
  2189. end tag will be included in the [HTML block]:
  2190. ```````````````````````````````` example
  2191. <script>
  2192. foo
  2193. </script>1. *bar*
  2194. .
  2195. <script>
  2196. foo
  2197. </script>1. *bar*
  2198. ````````````````````````````````
  2199. A comment (type 2):
  2200. ```````````````````````````````` example
  2201. <!-- Foo
  2202. bar
  2203. baz -->
  2204. okay
  2205. .
  2206. <!-- Foo
  2207. bar
  2208. baz -->
  2209. <p>okay</p>
  2210. ````````````````````````````````
  2211. A processing instruction (type 3):
  2212. ```````````````````````````````` example
  2213. <?php
  2214. echo '>';
  2215. ?>
  2216. okay
  2217. .
  2218. <?php
  2219. echo '>';
  2220. ?>
  2221. <p>okay</p>
  2222. ````````````````````````````````
  2223. A declaration (type 4):
  2224. ```````````````````````````````` example
  2225. <!DOCTYPE html>
  2226. .
  2227. <!DOCTYPE html>
  2228. ````````````````````````````````
  2229. CDATA (type 5):
  2230. ```````````````````````````````` example
  2231. <![CDATA[
  2232. function matchwo(a,b)
  2233. {
  2234. if (a < b && a < 0) then {
  2235. return 1;
  2236. } else {
  2237. return 0;
  2238. }
  2239. }
  2240. ]]>
  2241. okay
  2242. .
  2243. <![CDATA[
  2244. function matchwo(a,b)
  2245. {
  2246. if (a < b && a < 0) then {
  2247. return 1;
  2248. } else {
  2249. return 0;
  2250. }
  2251. }
  2252. ]]>
  2253. <p>okay</p>
  2254. ````````````````````````````````
  2255. The opening tag can be preceded by up to three spaces of indentation, but not
  2256. four:
  2257. ```````````````````````````````` example
  2258. <!-- foo -->
  2259. <!-- foo -->
  2260. .
  2261. <!-- foo -->
  2262. <pre><code>&lt;!-- foo --&gt;
  2263. </code></pre>
  2264. ````````````````````````````````
  2265. ```````````````````````````````` example
  2266. <div>
  2267. <div>
  2268. .
  2269. <div>
  2270. <pre><code>&lt;div&gt;
  2271. </code></pre>
  2272. ````````````````````````````````
  2273. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  2274. preceded by a blank line.
  2275. ```````````````````````````````` example
  2276. Foo
  2277. <div>
  2278. bar
  2279. </div>
  2280. .
  2281. <p>Foo</p>
  2282. <div>
  2283. bar
  2284. </div>
  2285. ````````````````````````````````
  2286. However, a following blank line is needed, except at the end of
  2287. a document, and except for blocks of types 1--5, [above][HTML
  2288. block]:
  2289. ```````````````````````````````` example
  2290. <div>
  2291. bar
  2292. </div>
  2293. *foo*
  2294. .
  2295. <div>
  2296. bar
  2297. </div>
  2298. *foo*
  2299. ````````````````````````````````
  2300. HTML blocks of type 7 cannot interrupt a paragraph:
  2301. ```````````````````````````````` example
  2302. Foo
  2303. <a href="bar">
  2304. baz
  2305. .
  2306. <p>Foo
  2307. <a href="bar">
  2308. baz</p>
  2309. ````````````````````````````````
  2310. This rule differs from John Gruber's original Markdown syntax
  2311. specification, which says:
  2312. > The only restrictions are that block-level HTML elements —
  2313. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2314. > surrounding content by blank lines, and the start and end tags of the
  2315. > block should not be indented with spaces or tabs.
  2316. In some ways Gruber's rule is more restrictive than the one given
  2317. here:
  2318. - It requires that an HTML block be preceded by a blank line.
  2319. - It does not allow the start tag to be indented.
  2320. - It requires a matching end tag, which it also does not allow to
  2321. be indented.
  2322. Most Markdown implementations (including some of Gruber's own) do not
  2323. respect all of these restrictions.
  2324. There is one respect, however, in which Gruber's rule is more liberal
  2325. than the one given here, since it allows blank lines to occur inside
  2326. an HTML block. There are two reasons for disallowing them here.
  2327. First, it removes the need to parse balanced tags, which is
  2328. expensive and can require backtracking from the end of the document
  2329. if no matching end tag is found. Second, it provides a very simple
  2330. and flexible way of including Markdown content inside HTML tags:
  2331. simply separate the Markdown from the HTML using blank lines:
  2332. Compare:
  2333. ```````````````````````````````` example
  2334. <div>
  2335. *Emphasized* text.
  2336. </div>
  2337. .
  2338. <div>
  2339. <p><em>Emphasized</em> text.</p>
  2340. </div>
  2341. ````````````````````````````````
  2342. ```````````````````````````````` example
  2343. <div>
  2344. *Emphasized* text.
  2345. </div>
  2346. .
  2347. <div>
  2348. *Emphasized* text.
  2349. </div>
  2350. ````````````````````````````````
  2351. Some Markdown implementations have adopted a convention of
  2352. interpreting content inside tags as text if the open tag has
  2353. the attribute `markdown=1`. The rule given above seems a simpler and
  2354. more elegant way of achieving the same expressive power, which is also
  2355. much simpler to parse.
  2356. The main potential drawback is that one can no longer paste HTML
  2357. blocks into Markdown documents with 100% reliability. However,
  2358. *in most cases* this will work fine, because the blank lines in
  2359. HTML are usually followed by HTML block tags. For example:
  2360. ```````````````````````````````` example
  2361. <table>
  2362. <tr>
  2363. <td>
  2364. Hi
  2365. </td>
  2366. </tr>
  2367. </table>
  2368. .
  2369. <table>
  2370. <tr>
  2371. <td>
  2372. Hi
  2373. </td>
  2374. </tr>
  2375. </table>
  2376. ````````````````````````````````
  2377. There are problems, however, if the inner tags are indented
  2378. *and* separated by spaces, as then they will be interpreted as
  2379. an indented code block:
  2380. ```````````````````````````````` example
  2381. <table>
  2382. <tr>
  2383. <td>
  2384. Hi
  2385. </td>
  2386. </tr>
  2387. </table>
  2388. .
  2389. <table>
  2390. <tr>
  2391. <pre><code>&lt;td&gt;
  2392. Hi
  2393. &lt;/td&gt;
  2394. </code></pre>
  2395. </tr>
  2396. </table>
  2397. ````````````````````````````````
  2398. Fortunately, blank lines are usually not necessary and can be
  2399. deleted. The exception is inside `<pre>` tags, but as described
  2400. [above][HTML blocks], raw HTML blocks starting with `<pre>`
  2401. *can* contain blank lines.
  2402. ## Link reference definitions
  2403. A [link reference definition](@)
  2404. consists of a [link label], optionally preceded by up to three spaces of
  2405. indentation, followed
  2406. by a colon (`:`), optional spaces or tabs (including up to one
  2407. [line ending]), a [link destination],
  2408. an optional [annotation reference set],
  2409. which if it is present must be separated
  2410. from the [link destination] by spaces or tabs,
  2411. optional spaces or tabs (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] and [annotation set] by spaces or tabs.
  2415. No further character may occur.
  2416. If [annotation set] is present then [link destination] is optional.
  2417. A [link reference definition]
  2418. does not correspond to a structural element of a document. Instead, it
  2419. defines a label which can be used in [reference links]
  2420. and reference-style [images] elsewhere in the document. [Link
  2421. reference definitions] can come either before or after the links that use
  2422. them.
  2423. ```````````````````````````````` example
  2424. [foo]: /url "title"
  2425. [foo]
  2426. .
  2427. <p><a href="/url" title="title">foo</a></p>
  2428. ````````````````````````````````
  2429. ```````````````````````````````` example
  2430. [foo]:
  2431. /url
  2432. 'the title'
  2433. [foo]
  2434. .
  2435. <p><a href="/url" title="the title">foo</a></p>
  2436. ````````````````````````````````
  2437. ```````````````````````````````` example
  2438. [Foo*bar\]]:my_(url) 'title (with parens)'
  2439. [Foo*bar\]]
  2440. .
  2441. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2442. ````````````````````````````````
  2443. ```````````````````````````````` example
  2444. [Foo bar]:
  2445. <my url>
  2446. 'title'
  2447. [Foo bar]
  2448. .
  2449. <p><a href="my%20url" title="title">Foo bar</a></p>
  2450. ````````````````````````````````
  2451. [Link reference definition] with [annotation hint]:
  2452. ```````````````````````````````` example
  2453. [foo]: /url {.ex:hint} "title"
  2454. [foo]
  2455. .
  2456. <p><a href="/url" typeof="ex:hint" title="title">foo</a></p>
  2457. ````````````````````````````````
  2458. ```````````````````````````````` example
  2459. [foo]:
  2460. /url
  2461. {ex:hint}
  2462. 'the title'
  2463. [foo]
  2464. .
  2465. <p><a href="/url" property="ex:hint" title="the title">foo</a></p>
  2466. ````````````````````````````````
  2467. ```````````````````````````````` example
  2468. [Foo*bar\]]:my_(url) {=ex:hint} 'title (with parens)'
  2469. [Foo*bar\]]
  2470. .
  2471. <p><a href="my_(url)" resource="ex:hint" title="title (with parens)">Foo*bar]</a></p>
  2472. ````````````````````````````````
  2473. ```````````````````````````````` example
  2474. [Foo bar]:
  2475. <my url>
  2476. {.<my ødd iri>}
  2477. 'title'
  2478. [Foo bar]
  2479. .
  2480. <p><a href="my%20ødd%20iri" typeof="µ%20iri" title="title">Foo bar</a></p>
  2481. ````````````````````````````````
  2482. The title may extend over multiple lines:
  2483. ```````````````````````````````` example
  2484. [foo]: /url '
  2485. title
  2486. line1
  2487. line2
  2488. '
  2489. [foo]
  2490. .
  2491. <p><a href="/url" title="
  2492. title
  2493. line1
  2494. line2
  2495. ">foo</a></p>
  2496. ````````````````````````````````
  2497. However, it may not contain a [blank line]:
  2498. ```````````````````````````````` example
  2499. [foo]: /url 'title
  2500. with blank line'
  2501. [foo]
  2502. .
  2503. <p>[foo]: /url 'title</p>
  2504. <p>with blank line'</p>
  2505. <p>[foo]</p>
  2506. ````````````````````````````````
  2507. The annotation may extend over multiple lines:
  2508. ```````````````````````````````` example
  2509. [foo]: /url {
  2510. .ex:first
  2511. =ex:second
  2512. }
  2513. [foo]
  2514. .
  2515. <p><a href="/url" typeof="ex:first" property="ex:second">foo</a></p>
  2516. ````````````````````````````````
  2517. However, it may not contain a [blank line]:
  2518. ```````````````````````````````` example
  2519. [foo]: /url {.ex:hint
  2520. =ex:hint_after_blank_line}
  2521. [foo]
  2522. .
  2523. <p>[foo]: /url {.ex:hint</p>
  2524. <p>=ex:hint_after_blank_line}</p>
  2525. <p>[foo]</p>
  2526. ````````````````````````````````
  2527. The title may be omitted:
  2528. ```````````````````````````````` example
  2529. [foo]:
  2530. /url
  2531. [foo]
  2532. .
  2533. <p><a href="/url">foo</a></p>
  2534. ````````````````````````````````
  2535. The link destination may not be omitted:
  2536. ```````````````````````````````` example
  2537. [foo]:
  2538. [foo]
  2539. .
  2540. <p>[foo]:</p>
  2541. <p>[foo]</p>
  2542. ````````````````````````````````
  2543. However, an empty link destination may be specified using
  2544. angle brackets:
  2545. ```````````````````````````````` example
  2546. [foo]: <>
  2547. [foo]
  2548. .
  2549. <p><a href="">foo</a></p>
  2550. ````````````````````````````````
  2551. The link destination may be omitted
  2552. when an annotation is included:
  2553. ```````````````````````````````` example
  2554. [foo]: {.ex:hint}
  2555. [foo]
  2556. .
  2557. <p><div typeof="ex:hint">foo</div></p>
  2558. ````````````````````````````````
  2559. An empty [annotation destination] may be specified
  2560. using angle brackets:
  2561. ```````````````````````````````` example
  2562. [foo]: {<>}
  2563. [foo]
  2564. .
  2565. <p><div resource="">foo</div></p>
  2566. ````````````````````````````````
  2567. An empty [annotation destination] as CURIE is not allowed, however:
  2568. ```````````````````````````````` example
  2569. [foo]: {.}
  2570. [foo]
  2571. .
  2572. <p>[foo]: {.}</p>
  2573. <p>[foo]</p>
  2574. ````````````````````````````````
  2575. The title must be separated from the link destination by
  2576. spaces or tabs:
  2577. ```````````````````````````````` example
  2578. [foo]: <bar>(baz)
  2579. [foo]
  2580. .
  2581. <p>[foo]: <bar>(baz)</p>
  2582. <p>[foo]</p>
  2583. ````````````````````````````````
  2584. The annotation must be separated from the link destination by
  2585. spaces or tabs
  2586. ```````````````````````````````` example
  2587. [foo]: <bar>{.ex:hint}
  2588. [foo]
  2589. .
  2590. <p>[foo]: <a typeof="ex:hint" href="bar">bar</a></p>
  2591. <p>[foo]</p>
  2592. ````````````````````````````````
  2593. ```````````````````````````````` example
  2594. [foo]: /bar{.ex:hint}
  2595. [foo]
  2596. .
  2597. <p>[foo]: /bar{.ex:hint}</p>
  2598. <p>[foo]</p>
  2599. ````````````````````````````````
  2600. Both title and destination can contain backslash escapes
  2601. and literal backslashes:
  2602. ```````````````````````````````` example
  2603. [foo]: /url\bar\*baz "foo\"bar\baz"
  2604. [foo]
  2605. .
  2606. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2607. ````````````````````````````````
  2608. Annotation can contain backslash escapes
  2609. in angle brackets:
  2610. ```````````````````````````````` example
  2611. [foo]: /url\bar\*baz {.<foo\"bar\baz>}
  2612. [foo]
  2613. .
  2614. <p><a href="/url%5Cbar*baz" typeof="foo&quot;bar\baz">foo</a></p>
  2615. ````````````````````````````````
  2616. Annotation cannot contain backslash escapes
  2617. in CURIE form, however:
  2618. ```````````````````````````````` example
  2619. [foo]: /url\bar\*baz {.ex:foo\"bar\baz}
  2620. [foo]
  2621. .
  2622. <p>[foo]: /url\bar\*baz {.ex:foo\"bar\baz}</p>
  2623. <p>[foo]</p>
  2624. ````````````````````````````````
  2625. A link can come before its corresponding definition:
  2626. ```````````````````````````````` example
  2627. [foo]
  2628. [foo]: url
  2629. .
  2630. <p><a href="url">foo</a></p>
  2631. ````````````````````````````````
  2632. ```````````````````````````````` example
  2633. [foo]
  2634. [foo]: url {.ex:hint}
  2635. .
  2636. <p><a href="url" typeof="ex:hint">foo</a></p>
  2637. ````````````````````````````````
  2638. If there are several matching definitions, the first one takes
  2639. precedence:
  2640. ```````````````````````````````` example
  2641. [foo]
  2642. [foo]: first
  2643. [foo]: second
  2644. .
  2645. <p><a href="first">foo</a></p>
  2646. ````````````````````````````````
  2647. ```````````````````````````````` example
  2648. [foo]
  2649. [foo]: url {.ex:first}
  2650. [foo]: url {.ex:second}
  2651. .
  2652. <p><a href="url" typeof"ex:first">foo</a></p>
  2653. ````````````````````````````````
  2654. ```````````````````````````````` example
  2655. [foo]
  2656. [foo]: url {.ex:first}
  2657. [foo]: url {=ex:second}
  2658. .
  2659. <p><a href="url" typeof"ex:first">foo</a></p>
  2660. ````````````````````````````````
  2661. ```````````````````````````````` example
  2662. [foo]: url {.ex:first}
  2663. [foo]
  2664. [foo]: url {=ex:second}
  2665. .
  2666. <p><a href="url" typeof"ex:first">foo</a></p>
  2667. ````````````````````````````````
  2668. ```````````````````````````````` example
  2669. [foo]
  2670. [foo]: url {}
  2671. [foo]: url {=ex:second}
  2672. .
  2673. <p><a href="url">foo</a></p>
  2674. ````````````````````````````````
  2675. ```````````````````````````````` example
  2676. [foo]
  2677. [foo]: url
  2678. [foo]: url {=ex:second}
  2679. .
  2680. <p><a href="url">foo</a></p>
  2681. ````````````````````````````````
  2682. As noted in the section on [Links], matching of labels is
  2683. case-insensitive (see [matches]).
  2684. ```````````````````````````````` example
  2685. [FOO]: /url
  2686. [Foo]
  2687. .
  2688. <p><a href="/url">Foo</a></p>
  2689. ````````````````````````````````
  2690. ```````````````````````````````` example
  2691. [FOO]: /url {.ex:hint}
  2692. [Foo]
  2693. .
  2694. <p><a href="/url" typeof="ex:hint">Foo</a></p>
  2695. ````````````````````````````````
  2696. ```````````````````````````````` example
  2697. [ΑΓΩ]: /φου
  2698. [αγω]
  2699. .
  2700. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2701. ````````````````````````````````
  2702. ```````````````````````````````` example
  2703. [ΑΓΩ]: /φου {.φου:φου φου:φου =φου:φου .<φου> <φου> =<φου>}
  2704. [αγω]
  2705. .
  2706. <p><a href="/%CF%86%CE%BF%CF%85" typeof="φου:φου φου" resource="φου:φου φου" property="φου:φου φου">αγω</a></p>
  2707. ````````````````````````````````
  2708. Here is a link reference definition with no corresponding link.
  2709. It contributes nothing to the document.
  2710. ```````````````````````````````` example
  2711. [foo]: /url
  2712. .
  2713. ````````````````````````````````
  2714. Here is another one:
  2715. ```````````````````````````````` example
  2716. [
  2717. foo
  2718. ]: /url
  2719. bar
  2720. .
  2721. <p>bar</p>
  2722. ````````````````````````````````
  2723. Here are some with annotation:
  2724. ```````````````````````````````` example
  2725. [foo]: /url {.ex:hint}
  2726. .
  2727. ````````````````````````````````
  2728. ```````````````````````````````` example
  2729. [
  2730. foo
  2731. ]: /url
  2732. {
  2733. .ex:hint
  2734. }
  2735. bar
  2736. .
  2737. <p>bar</p>
  2738. ````````````````````````````````
  2739. This is not a link reference definition, because there are
  2740. characters other than spaces or tabs after the title:
  2741. ```````````````````````````````` example
  2742. [foo]: /url "title" ok
  2743. .
  2744. <p>[foo]: /url &quot;title&quot; ok</p>
  2745. ````````````````````````````````
  2746. Here is a bogus annotated one with stuff after title:
  2747. ```````````````````````````````` example
  2748. [foo]: /url {.ex:hint} "title" ok
  2749. .
  2750. <p>[foo]: /url {.ex:hint} &quot;title&quot; ok</p>
  2751. ````````````````````````````````
  2752. This is not a link reference definition,
  2753. because there are characters other than spaces or tabs or title after annotation:
  2754. ```````````````````````````````` example
  2755. [foo]: /url {.ex:hint} ok
  2756. .
  2757. <p>[foo]: /url {.ex:hint} ok</p>
  2758. ````````````````````````````````
  2759. This is a link reference definition, but it has no title:
  2760. ```````````````````````````````` example
  2761. [foo]: /url
  2762. "title" ok
  2763. .
  2764. <p>&quot;title&quot; ok</p>
  2765. ````````````````````````````````
  2766. Here is an annotated link reference definition, without title:
  2767. ```````````````````````````````` example
  2768. [foo]: /url {.ex:hint}
  2769. "title" ok
  2770. .
  2771. <p>&quot;title&quot; ok</p>
  2772. ````````````````````````````````
  2773. This is not a link reference definition, because it is indented
  2774. four spaces:
  2775. ```````````````````````````````` example
  2776. [foo]: /url "title"
  2777. [foo]
  2778. .
  2779. <pre><code>[foo]: /url &quot;title&quot;
  2780. </code></pre>
  2781. <p>[foo]</p>
  2782. ````````````````````````````````
  2783. Here is a bogus one indented four spaces, with annotation:
  2784. ```````````````````````````````` example
  2785. [foo]: /url {.ex:hint} "title"
  2786. [foo]
  2787. .
  2788. <pre><code>[foo]: /url {.ex:hint} &quot;title&quot;
  2789. </code></pre>
  2790. <p>[foo]</p>
  2791. ````````````````````````````````
  2792. This is not a link reference definition, because it occurs inside
  2793. a code block:
  2794. ```````````````````````````````` example
  2795. ```
  2796. [foo]: /url
  2797. ```
  2798. [foo]
  2799. .
  2800. <pre><code>[foo]: /url
  2801. </code></pre>
  2802. <p>[foo]</p>
  2803. ````````````````````````````````
  2804. Here is an annotated bogus link reference definition in code block:
  2805. ```````````````````````````````` example
  2806. ```
  2807. [foo]: /url {.ex:hint}
  2808. ```
  2809. [foo]
  2810. .
  2811. <pre><code>[foo]: /url {.ex:hint}
  2812. </code></pre>
  2813. <p>[foo]</p>
  2814. ````````````````````````````````
  2815. A [link reference definition] cannot interrupt a paragraph.
  2816. ```````````````````````````````` example
  2817. Foo
  2818. [bar]: /baz
  2819. [bar]
  2820. .
  2821. <p>Foo
  2822. [bar]: /baz</p>
  2823. <p>[bar]</p>
  2824. ````````````````````````````````
  2825. ```````````````````````````````` example
  2826. Foo
  2827. [bar]: /baz {.ex:hint}
  2828. [bar]
  2829. .
  2830. <p>Foo
  2831. [bar]: /baz {.ex:hint}</p>
  2832. <p>[bar]</p>
  2833. ````````````````````````````````
  2834. However, it can directly follow other block elements, such as headings
  2835. and thematic breaks, and it need not be followed by a blank line.
  2836. ```````````````````````````````` example
  2837. # [Foo]
  2838. [foo]: /url
  2839. > bar
  2840. .
  2841. <h1><a href="/url">Foo</a></h1>
  2842. <blockquote>
  2843. <p>bar</p>
  2844. </blockquote>
  2845. ````````````````````````````````
  2846. ```````````````````````````````` example
  2847. # [Foo]
  2848. [foo]: /url {.ex:hint}
  2849. > bar
  2850. .
  2851. <h1><a href="/url" typeof="ex:hint">Foo</a></h1>
  2852. <blockquote>
  2853. <p>bar</p>
  2854. </blockquote>
  2855. ````````````````````````````````
  2856. ```````````````````````````````` example
  2857. [foo]: /url
  2858. bar
  2859. ===
  2860. [foo]
  2861. .
  2862. <h1>bar</h1>
  2863. <p><a href="/url">foo</a></p>
  2864. ````````````````````````````````
  2865. ```````````````````````````````` example
  2866. [foo]: /url {.ex:hint}
  2867. bar
  2868. ===
  2869. [foo]
  2870. .
  2871. <h1>bar</h1>
  2872. <p><a href="/url" typeof="ex:hint">foo</a></p>
  2873. ````````````````````````````````
  2874. ```````````````````````````````` example
  2875. [foo]: /url
  2876. ===
  2877. [foo]
  2878. .
  2879. <p>===
  2880. <a href="/url">foo</a></p>
  2881. ````````````````````````````````
  2882. ```````````````````````````````` example
  2883. [foo]: /url {.ex:hint}
  2884. ===
  2885. [foo]
  2886. .
  2887. <p>===
  2888. <a href="/url" typeof="ex:hint">foo</a></p>
  2889. ````````````````````````````````
  2890. Several [link reference definitions]
  2891. can occur one after another, without intervening blank lines.
  2892. ```````````````````````````````` example
  2893. [foo]: /foo-url "foo"
  2894. [bar]: /bar-url
  2895. "bar"
  2896. [baz]: /baz-url
  2897. [foo],
  2898. [bar],
  2899. [baz]
  2900. .
  2901. <p><a href="/foo-url" title="foo">foo</a>,
  2902. <a href="/bar-url" title="bar">bar</a>,
  2903. <a href="/baz-url">baz</a></p>
  2904. ````````````````````````````````
  2905. ```````````````````````````````` example
  2906. [foo]: /foo-url {.ex:first} "foo"
  2907. [bar]: /bar-url
  2908. {.ex:second}
  2909. "bar"
  2910. [baz]: /baz-url {.ex:third}
  2911. [foo],
  2912. [bar],
  2913. [baz]
  2914. .
  2915. <p><a href="/foo-url" typeof="ex:first" title="foo">foo</a>,
  2916. <a href="/bar-url" typeof="ex:second" title="bar">bar</a>,
  2917. <a href="/baz-url" typeof="ex:third">baz</a></p>
  2918. ````````````````````````````````
  2919. [Link reference definitions] can occur
  2920. inside block containers, like lists and block quotations. They
  2921. affect the entire document, not just the container in which they
  2922. are defined:
  2923. ```````````````````````````````` example
  2924. [foo]
  2925. > [foo]: /url
  2926. .
  2927. <p><a href="/url">foo</a></p>
  2928. <blockquote>
  2929. </blockquote>
  2930. ````````````````````````````````
  2931. ```````````````````````````````` example
  2932. [foo]
  2933. > [foo]: /url {.ex:hint}
  2934. .
  2935. <p><a href="/url" typeof="ex:hint">foo</a></p>
  2936. <blockquote>
  2937. </blockquote>
  2938. ````````````````````````````````
  2939. Whether something is a [link reference definition] is
  2940. independent of whether the link reference it defines is
  2941. used in the document. Thus, for example, the following
  2942. document contains just a link reference definition, and
  2943. no visible content:
  2944. ```````````````````````````````` example
  2945. [foo]: /url
  2946. .
  2947. ````````````````````````````````
  2948. ```````````````````````````````` example
  2949. [foo]: /url {.ex:hint}
  2950. .
  2951. ````````````````````````````````
  2952. ## Annotation reference definitions
  2953. An [annotation reference definition](@)
  2954. consists of an [annotation reference label],
  2955. optionally preceded by up to three spaces of indentation,
  2956. followed by a colon (`:`),
  2957. optional spaces or tabs (including up to one [line ending]),
  2958. an [annotation destination],
  2959. and an optional [annotation reference flag],
  2960. which if it is present must be separated
  2961. from the [annotation reference label]
  2962. and [annotation reference destination]
  2963. by spaces or tabs (including up to one [line ending]).
  2964. No further character may occur.
  2965. If [annotation reference flag] is present
  2966. then [annotation reference destination] is optional.
  2967. An [annotation reference definition]
  2968. does not correspond to a structural element of a document.
  2969. Instead, it defines a label
  2970. which can be used in [annotations] elsewhere in the document.
  2971. [Annotation reference definitions] can come
  2972. either before or after the links that use them.
  2973. FIXME: define [annotation reference label]
  2974. FIXME: define [annotation reference flag] "@default"
  2975. FIXME: define [annotation initial context]
  2976. from <https://www.w3.org/2011/rdfa-context/rdfa-1.1>.
  2977. ## Paragraphs
  2978. A sequence of non-blank lines that cannot be interpreted as other
  2979. kinds of blocks forms a [paragraph](@).
  2980. The contents of the paragraph are the result of parsing the
  2981. paragraph's raw content as inlines. The paragraph's raw content
  2982. is formed by concatenating the lines and removing initial and final
  2983. spaces or tabs.
  2984. A simple example with two paragraphs:
  2985. ```````````````````````````````` example
  2986. aaa
  2987. bbb
  2988. .
  2989. <p>aaa</p>
  2990. <p>bbb</p>
  2991. ````````````````````````````````
  2992. Paragraphs can contain multiple lines, but no blank lines:
  2993. ```````````````````````````````` example
  2994. aaa
  2995. bbb
  2996. ccc
  2997. ddd
  2998. .
  2999. <p>aaa
  3000. bbb</p>
  3001. <p>ccc
  3002. ddd</p>
  3003. ````````````````````````````````
  3004. Multiple blank lines between paragraphs have no effect:
  3005. ```````````````````````````````` example
  3006. aaa
  3007. bbb
  3008. .
  3009. <p>aaa</p>
  3010. <p>bbb</p>
  3011. ````````````````````````````````
  3012. Leading spaces or tabs are skipped:
  3013. ```````````````````````````````` example
  3014. aaa
  3015. bbb
  3016. .
  3017. <p>aaa
  3018. bbb</p>
  3019. ````````````````````````````````
  3020. Lines after the first may be indented any amount, since indented
  3021. code blocks cannot interrupt paragraphs.
  3022. ```````````````````````````````` example
  3023. aaa
  3024. bbb
  3025. ccc
  3026. .
  3027. <p>aaa
  3028. bbb
  3029. ccc</p>
  3030. ````````````````````````````````
  3031. However, the first line may be preceded by up to three spaces of indentation.
  3032. Four spaces of indentation is too many:
  3033. ```````````````````````````````` example
  3034. aaa
  3035. bbb
  3036. .
  3037. <p>aaa
  3038. bbb</p>
  3039. ````````````````````````````````
  3040. ```````````````````````````````` example
  3041. aaa
  3042. bbb
  3043. .
  3044. <pre><code>aaa
  3045. </code></pre>
  3046. <p>bbb</p>
  3047. ````````````````````````````````
  3048. Final spaces or tabs are stripped before inline parsing, so a paragraph
  3049. that ends with two or more spaces will not end with a [hard line
  3050. break]:
  3051. ```````````````````````````````` example
  3052. aaa
  3053. bbb
  3054. .
  3055. <p>aaa<br />
  3056. bbb</p>
  3057. ````````````````````````````````
  3058. ## Blank lines
  3059. [Blank lines] between block-level elements are ignored,
  3060. except for the role they play in determining whether a [list]
  3061. is [tight] or [loose].
  3062. Blank lines at the beginning and end of the document are also ignored.
  3063. ```````````````````````````````` example
  3064. aaa
  3065. # aaa
  3066. .
  3067. <p>aaa</p>
  3068. <h1>aaa</h1>
  3069. ````````````````````````````````
  3070. # Container blocks
  3071. A [container block](#container-blocks) is a block that has other
  3072. blocks as its contents. There are two basic kinds of container blocks:
  3073. [block quotes] and [list items].
  3074. [Lists] are meta-containers for [list items].
  3075. We define the syntax for container blocks recursively. The general
  3076. form of the definition is:
  3077. > If X is a sequence of blocks, then the result of
  3078. > transforming X in such-and-such a way is a container of type Y
  3079. > with these blocks as its content.
  3080. So, we explain what counts as a block quote or list item by explaining
  3081. how these can be *generated* from their contents. This should suffice
  3082. to define the syntax, although it does not give a recipe for *parsing*
  3083. these constructions. (A recipe is provided below in the section entitled
  3084. [A parsing strategy](#appendix-a-parsing-strategy).)
  3085. ## Block quotes
  3086. A [block quote marker](@),
  3087. optionally preceded by up to three spaces of indentation,
  3088. consists of (a) the character `>` together with a following space of
  3089. indentation, or (b) a single character `>` not followed by a space of
  3090. indentation.
  3091. The following rules define [block quotes]:
  3092. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  3093. of blocks *Bs*, then the result of prepending a [block quote
  3094. marker] to the beginning of each line in *Ls*
  3095. is a [block quote](#block-quotes) containing *Bs*.
  3096. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  3097. quote](#block-quotes) with contents *Bs*, then the result of deleting
  3098. the initial [block quote marker] from one or
  3099. more lines in which the next character other than a space or tab after the
  3100. [block quote marker] is [paragraph continuation
  3101. text] is a block quote with *Bs* as its content.
  3102. [Paragraph continuation text](@) is text
  3103. that will be parsed as part of the content of a paragraph, but does
  3104. not occur at the beginning of the paragraph.
  3105. 3. **Consecutiveness.** A document cannot contain two [block
  3106. quotes] in a row unless there is a [blank line] between them.
  3107. Nothing else counts as a [block quote](#block-quotes).
  3108. Here is a simple example:
  3109. ```````````````````````````````` example
  3110. > # Foo
  3111. > bar
  3112. > baz
  3113. .
  3114. <blockquote>
  3115. <h1>Foo</h1>
  3116. <p>bar
  3117. baz</p>
  3118. </blockquote>
  3119. ````````````````````````````````
  3120. The space or tab after the `>` characters can be omitted:
  3121. ```````````````````````````````` example
  3122. ># Foo
  3123. >bar
  3124. > baz
  3125. .
  3126. <blockquote>
  3127. <h1>Foo</h1>
  3128. <p>bar
  3129. baz</p>
  3130. </blockquote>
  3131. ````````````````````````````````
  3132. The `>` characters can be preceded by up to three spaces of indentation:
  3133. ```````````````````````````````` example
  3134. > # Foo
  3135. > bar
  3136. > baz
  3137. .
  3138. <blockquote>
  3139. <h1>Foo</h1>
  3140. <p>bar
  3141. baz</p>
  3142. </blockquote>
  3143. ````````````````````````````````
  3144. Four spaces of indentation is too many:
  3145. ```````````````````````````````` example
  3146. > # Foo
  3147. > bar
  3148. > baz
  3149. .
  3150. <pre><code>&gt; # Foo
  3151. &gt; bar
  3152. &gt; baz
  3153. </code></pre>
  3154. ````````````````````````````````
  3155. The Laziness clause allows us to omit the `>` before
  3156. [paragraph continuation text]:
  3157. ```````````````````````````````` example
  3158. > # Foo
  3159. > bar
  3160. baz
  3161. .
  3162. <blockquote>
  3163. <h1>Foo</h1>
  3164. <p>bar
  3165. baz</p>
  3166. </blockquote>
  3167. ````````````````````````````````
  3168. A block quote can contain some lazy and some non-lazy
  3169. continuation lines:
  3170. ```````````````````````````````` example
  3171. > bar
  3172. baz
  3173. > foo
  3174. .
  3175. <blockquote>
  3176. <p>bar
  3177. baz
  3178. foo</p>
  3179. </blockquote>
  3180. ````````````````````````````````
  3181. Laziness only applies to lines that would have been continuations of
  3182. paragraphs had they been prepended with [block quote markers].
  3183. For example, the `> ` cannot be omitted in the second line of
  3184. ``` markdown
  3185. > foo
  3186. > ---
  3187. ```
  3188. without changing the meaning:
  3189. ```````````````````````````````` example
  3190. > foo
  3191. ---
  3192. .
  3193. <blockquote>
  3194. <p>foo</p>
  3195. </blockquote>
  3196. <hr />
  3197. ````````````````````````````````
  3198. Similarly, if we omit the `> ` in the second line of
  3199. ``` markdown
  3200. > - foo
  3201. > - bar
  3202. ```
  3203. then the block quote ends after the first line:
  3204. ```````````````````````````````` example
  3205. > - foo
  3206. - bar
  3207. .
  3208. <blockquote>
  3209. <ul>
  3210. <li>foo</li>
  3211. </ul>
  3212. </blockquote>
  3213. <ul>
  3214. <li>bar</li>
  3215. </ul>
  3216. ````````````````````````````````
  3217. For the same reason, we can't omit the `> ` in front of
  3218. subsequent lines of an indented or fenced code block:
  3219. ```````````````````````````````` example
  3220. > foo
  3221. bar
  3222. .
  3223. <blockquote>
  3224. <pre><code>foo
  3225. </code></pre>
  3226. </blockquote>
  3227. <pre><code>bar
  3228. </code></pre>
  3229. ````````````````````````````````
  3230. ```````````````````````````````` example
  3231. > ```
  3232. foo
  3233. ```
  3234. .
  3235. <blockquote>
  3236. <pre><code></code></pre>
  3237. </blockquote>
  3238. <p>foo</p>
  3239. <pre><code></code></pre>
  3240. ````````````````````````````````
  3241. Note that in the following case, we have a [lazy
  3242. continuation line]:
  3243. ```````````````````````````````` example
  3244. > foo
  3245. - bar
  3246. .
  3247. <blockquote>
  3248. <p>foo
  3249. - bar</p>
  3250. </blockquote>
  3251. ````````````````````````````````
  3252. To see why, note that in
  3253. ```markdown
  3254. > foo
  3255. > - bar
  3256. ```
  3257. the `- bar` is indented too far to start a list, and can't
  3258. be an indented code block because indented code blocks cannot
  3259. interrupt paragraphs, so it is [paragraph continuation text].
  3260. A block quote can be empty:
  3261. ```````````````````````````````` example
  3262. >
  3263. .
  3264. <blockquote>
  3265. </blockquote>
  3266. ````````````````````````````````
  3267. ```````````````````````````````` example
  3268. >
  3269. >
  3270. >
  3271. .
  3272. <blockquote>
  3273. </blockquote>
  3274. ````````````````````````````````
  3275. A block quote can have initial or final blank lines:
  3276. ```````````````````````````````` example
  3277. >
  3278. > foo
  3279. >
  3280. .
  3281. <blockquote>
  3282. <p>foo</p>
  3283. </blockquote>
  3284. ````````````````````````````````
  3285. A blank line always separates block quotes:
  3286. ```````````````````````````````` example
  3287. > foo
  3288. > bar
  3289. .
  3290. <blockquote>
  3291. <p>foo</p>
  3292. </blockquote>
  3293. <blockquote>
  3294. <p>bar</p>
  3295. </blockquote>
  3296. ````````````````````````````````
  3297. (Most current Markdown implementations, including John Gruber's
  3298. original `Markdown.pl`, will parse this example as a single block quote
  3299. with two paragraphs. But it seems better to allow the author to decide
  3300. whether two block quotes or one are wanted.)
  3301. Consecutiveness means that if we put these block quotes together,
  3302. we get a single block quote:
  3303. ```````````````````````````````` example
  3304. > foo
  3305. > bar
  3306. .
  3307. <blockquote>
  3308. <p>foo
  3309. bar</p>
  3310. </blockquote>
  3311. ````````````````````````````````
  3312. To get a block quote with two paragraphs, use:
  3313. ```````````````````````````````` example
  3314. > foo
  3315. >
  3316. > bar
  3317. .
  3318. <blockquote>
  3319. <p>foo</p>
  3320. <p>bar</p>
  3321. </blockquote>
  3322. ````````````````````````````````
  3323. Block quotes can interrupt paragraphs:
  3324. ```````````````````````````````` example
  3325. foo
  3326. > bar
  3327. .
  3328. <p>foo</p>
  3329. <blockquote>
  3330. <p>bar</p>
  3331. </blockquote>
  3332. ````````````````````````````````
  3333. In general, blank lines are not needed before or after block
  3334. quotes:
  3335. ```````````````````````````````` example
  3336. > aaa
  3337. ***
  3338. > bbb
  3339. .
  3340. <blockquote>
  3341. <p>aaa</p>
  3342. </blockquote>
  3343. <hr />
  3344. <blockquote>
  3345. <p>bbb</p>
  3346. </blockquote>
  3347. ````````````````````````````````
  3348. However, because of laziness, a blank line is needed between
  3349. a block quote and a following paragraph:
  3350. ```````````````````````````````` example
  3351. > bar
  3352. baz
  3353. .
  3354. <blockquote>
  3355. <p>bar
  3356. baz</p>
  3357. </blockquote>
  3358. ````````````````````````````````
  3359. ```````````````````````````````` example
  3360. > bar
  3361. baz
  3362. .
  3363. <blockquote>
  3364. <p>bar</p>
  3365. </blockquote>
  3366. <p>baz</p>
  3367. ````````````````````````````````
  3368. ```````````````````````````````` example
  3369. > bar
  3370. >
  3371. baz
  3372. .
  3373. <blockquote>
  3374. <p>bar</p>
  3375. </blockquote>
  3376. <p>baz</p>
  3377. ````````````````````````````````
  3378. It is a consequence of the Laziness rule that any number
  3379. of initial `>`s may be omitted on a continuation line of a
  3380. nested block quote:
  3381. ```````````````````````````````` example
  3382. > > > foo
  3383. bar
  3384. .
  3385. <blockquote>
  3386. <blockquote>
  3387. <blockquote>
  3388. <p>foo
  3389. bar</p>
  3390. </blockquote>
  3391. </blockquote>
  3392. </blockquote>
  3393. ````````````````````````````````
  3394. ```````````````````````````````` example
  3395. >>> foo
  3396. > bar
  3397. >>baz
  3398. .
  3399. <blockquote>
  3400. <blockquote>
  3401. <blockquote>
  3402. <p>foo
  3403. bar
  3404. baz</p>
  3405. </blockquote>
  3406. </blockquote>
  3407. </blockquote>
  3408. ````````````````````````````````
  3409. When including an indented code block in a block quote,
  3410. remember that the [block quote marker] includes
  3411. both the `>` and a following space of indentation. So *five spaces* are needed
  3412. after the `>`:
  3413. ```````````````````````````````` example
  3414. > code
  3415. > not code
  3416. .
  3417. <blockquote>
  3418. <pre><code>code
  3419. </code></pre>
  3420. </blockquote>
  3421. <blockquote>
  3422. <p>not code</p>
  3423. </blockquote>
  3424. ````````````````````````````````
  3425. ## List items
  3426. A [list marker](@) is a
  3427. [bullet list marker] or an [ordered list marker].
  3428. A [bullet list marker](@)
  3429. is a `-`, `+`, or `*` character.
  3430. An [ordered list marker](@)
  3431. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  3432. `.` character or a `)` character. (The reason for the length
  3433. limit is that with 10 digits we start seeing integer overflows
  3434. in some browsers.)
  3435. The following rules define [list items]:
  3436. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  3437. blocks *Bs* starting with a character other than a space or tab, and *M* is
  3438. a list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces of indentation,
  3439. then the result of prepending *M* and the following spaces to the first line
  3440. of Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  3441. list item with *Bs* as its contents. The type of the list item
  3442. (bullet or ordered) is determined by the type of its list marker.
  3443. If the list item is ordered, then it is also assigned a start
  3444. number, based on the ordered list marker.
  3445. Exceptions:
  3446. 1. When the first list item in a [list] interrupts
  3447. a paragraph---that is, when it starts on a line that would
  3448. otherwise count as [paragraph continuation text]---then (a)
  3449. the lines *Ls* must not begin with a blank line, and (b) if
  3450. the list item is ordered, the start number must be 1.
  3451. 2. If any line is a [thematic break][thematic breaks] then
  3452. that line is not a list item.
  3453. For example, let *Ls* be the lines
  3454. ```````````````````````````````` example
  3455. A paragraph
  3456. with two lines.
  3457. indented code
  3458. > A block quote.
  3459. .
  3460. <p>A paragraph
  3461. with two lines.</p>
  3462. <pre><code>indented code
  3463. </code></pre>
  3464. <blockquote>
  3465. <p>A block quote.</p>
  3466. </blockquote>
  3467. ````````````````````````````````
  3468. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  3469. that the following is an ordered list item with start number 1,
  3470. and the same contents as *Ls*:
  3471. ```````````````````````````````` example
  3472. 1. A paragraph
  3473. with two lines.
  3474. indented code
  3475. > A block quote.
  3476. .
  3477. <ol>
  3478. <li>
  3479. <p>A paragraph
  3480. with two lines.</p>
  3481. <pre><code>indented code
  3482. </code></pre>
  3483. <blockquote>
  3484. <p>A block quote.</p>
  3485. </blockquote>
  3486. </li>
  3487. </ol>
  3488. ````````````````````````````````
  3489. The most important thing to notice is that the position of
  3490. the text after the list marker determines how much indentation
  3491. is needed in subsequent blocks in the list item. If the list
  3492. marker takes up two spaces of indentation, and there are three spaces between
  3493. the list marker and the next character other than a space or tab, then blocks
  3494. must be indented five spaces in order to fall under the list
  3495. item.
  3496. Here are some examples showing how far content must be indented to be
  3497. put under the list item:
  3498. ```````````````````````````````` example
  3499. - one
  3500. two
  3501. .
  3502. <ul>
  3503. <li>one</li>
  3504. </ul>
  3505. <p>two</p>
  3506. ````````````````````````````````
  3507. ```````````````````````````````` example
  3508. - one
  3509. two
  3510. .
  3511. <ul>
  3512. <li>
  3513. <p>one</p>
  3514. <p>two</p>
  3515. </li>
  3516. </ul>
  3517. ````````````````````````````````
  3518. ```````````````````````````````` example
  3519. - one
  3520. two
  3521. .
  3522. <ul>
  3523. <li>one</li>
  3524. </ul>
  3525. <pre><code> two
  3526. </code></pre>
  3527. ````````````````````````````````
  3528. ```````````````````````````````` example
  3529. - one
  3530. two
  3531. .
  3532. <ul>
  3533. <li>
  3534. <p>one</p>
  3535. <p>two</p>
  3536. </li>
  3537. </ul>
  3538. ````````````````````````````````
  3539. It is tempting to think of this in terms of columns: the continuation
  3540. blocks must be indented at least to the column of the first character other than
  3541. a space or tab after the list marker. However, that is not quite right.
  3542. The spaces of indentation after the list marker determine how much relative
  3543. indentation is needed. Which column this indentation reaches will depend on
  3544. how the list item is embedded in other constructions, as shown by
  3545. this example:
  3546. ```````````````````````````````` example
  3547. > > 1. one
  3548. >>
  3549. >> two
  3550. .
  3551. <blockquote>
  3552. <blockquote>
  3553. <ol>
  3554. <li>
  3555. <p>one</p>
  3556. <p>two</p>
  3557. </li>
  3558. </ol>
  3559. </blockquote>
  3560. </blockquote>
  3561. ````````````````````````````````
  3562. Here `two` occurs in the same column as the list marker `1.`,
  3563. but is actually contained in the list item, because there is
  3564. sufficient indentation after the last containing blockquote marker.
  3565. The converse is also possible. In the following example, the word `two`
  3566. occurs far to the right of the initial text of the list item, `one`, but
  3567. it is not considered part of the list item, because it is not indented
  3568. far enough past the blockquote marker:
  3569. ```````````````````````````````` example
  3570. >>- one
  3571. >>
  3572. > > two
  3573. .
  3574. <blockquote>
  3575. <blockquote>
  3576. <ul>
  3577. <li>one</li>
  3578. </ul>
  3579. <p>two</p>
  3580. </blockquote>
  3581. </blockquote>
  3582. ````````````````````````````````
  3583. Note that at least one space or tab is needed between the list marker and
  3584. any following content, so these are not list items:
  3585. ```````````````````````````````` example
  3586. -one
  3587. 2.two
  3588. .
  3589. <p>-one</p>
  3590. <p>2.two</p>
  3591. ````````````````````````````````
  3592. A list item may contain blocks that are separated by more than
  3593. one blank line.
  3594. ```````````````````````````````` example
  3595. - foo
  3596. bar
  3597. .
  3598. <ul>
  3599. <li>
  3600. <p>foo</p>
  3601. <p>bar</p>
  3602. </li>
  3603. </ul>
  3604. ````````````````````````````````
  3605. A list item may contain any kind of block:
  3606. ```````````````````````````````` example
  3607. 1. foo
  3608. ```
  3609. bar
  3610. ```
  3611. baz
  3612. > bam
  3613. .
  3614. <ol>
  3615. <li>
  3616. <p>foo</p>
  3617. <pre><code>bar
  3618. </code></pre>
  3619. <p>baz</p>
  3620. <blockquote>
  3621. <p>bam</p>
  3622. </blockquote>
  3623. </li>
  3624. </ol>
  3625. ````````````````````````````````
  3626. A list item that contains an indented code block will preserve
  3627. empty lines within the code block verbatim.
  3628. ```````````````````````````````` example
  3629. - Foo
  3630. bar
  3631. baz
  3632. .
  3633. <ul>
  3634. <li>
  3635. <p>Foo</p>
  3636. <pre><code>bar
  3637. baz
  3638. </code></pre>
  3639. </li>
  3640. </ul>
  3641. ````````````````````````````````
  3642. Note that ordered list start numbers must be nine digits or less:
  3643. ```````````````````````````````` example
  3644. 123456789. ok
  3645. .
  3646. <ol start="123456789">
  3647. <li>ok</li>
  3648. </ol>
  3649. ````````````````````````````````
  3650. ```````````````````````````````` example
  3651. 1234567890. not ok
  3652. .
  3653. <p>1234567890. not ok</p>
  3654. ````````````````````````````````
  3655. A start number may begin with 0s:
  3656. ```````````````````````````````` example
  3657. 0. ok
  3658. .
  3659. <ol start="0">
  3660. <li>ok</li>
  3661. </ol>
  3662. ````````````````````````````````
  3663. ```````````````````````````````` example
  3664. 003. ok
  3665. .
  3666. <ol start="3">
  3667. <li>ok</li>
  3668. </ol>
  3669. ````````````````````````````````
  3670. A start number may not be negative:
  3671. ```````````````````````````````` example
  3672. -1. not ok
  3673. .
  3674. <p>-1. not ok</p>
  3675. ````````````````````````````````
  3676. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3677. constitute a sequence of blocks *Bs* starting with an indented code
  3678. block, and *M* is a list marker of width *W* followed by
  3679. one space of indentation, then the result of prepending *M* and the
  3680. following space to the first line of *Ls*, and indenting subsequent lines
  3681. of *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3682. If a line is empty, then it need not be indented. The type of the
  3683. list item (bullet or ordered) is determined by the type of its list
  3684. marker. If the list item is ordered, then it is also assigned a
  3685. start number, based on the ordered list marker.
  3686. An indented code block will have to be preceded by four spaces of indentation
  3687. beyond the edge of the region where text will be included in the list item.
  3688. In the following case that is 6 spaces:
  3689. ```````````````````````````````` example
  3690. - foo
  3691. bar
  3692. .
  3693. <ul>
  3694. <li>
  3695. <p>foo</p>
  3696. <pre><code>bar
  3697. </code></pre>
  3698. </li>
  3699. </ul>
  3700. ````````````````````````````````
  3701. And in this case it is 11 spaces:
  3702. ```````````````````````````````` example
  3703. 10. foo
  3704. bar
  3705. .
  3706. <ol start="10">
  3707. <li>
  3708. <p>foo</p>
  3709. <pre><code>bar
  3710. </code></pre>
  3711. </li>
  3712. </ol>
  3713. ````````````````````````````````
  3714. If the *first* block in the list item is an indented code block,
  3715. then by rule #2, the contents must be preceded by *one* space of indentation
  3716. after the list marker:
  3717. ```````````````````````````````` example
  3718. indented code
  3719. paragraph
  3720. more code
  3721. .
  3722. <pre><code>indented code
  3723. </code></pre>
  3724. <p>paragraph</p>
  3725. <pre><code>more code
  3726. </code></pre>
  3727. ````````````````````````````````
  3728. ```````````````````````````````` example
  3729. 1. indented code
  3730. paragraph
  3731. more code
  3732. .
  3733. <ol>
  3734. <li>
  3735. <pre><code>indented code
  3736. </code></pre>
  3737. <p>paragraph</p>
  3738. <pre><code>more code
  3739. </code></pre>
  3740. </li>
  3741. </ol>
  3742. ````````````````````````````````
  3743. Note that an additional space of indentation is interpreted as space
  3744. inside the code block:
  3745. ```````````````````````````````` example
  3746. 1. indented code
  3747. paragraph
  3748. more code
  3749. .
  3750. <ol>
  3751. <li>
  3752. <pre><code> indented code
  3753. </code></pre>
  3754. <p>paragraph</p>
  3755. <pre><code>more code
  3756. </code></pre>
  3757. </li>
  3758. </ol>
  3759. ````````````````````````````````
  3760. Note that rules #1 and #2 only apply to two cases: (a) cases
  3761. in which the lines to be included in a list item begin with a
  3762. characer other than a space or tab, and (b) cases in which
  3763. they begin with an indented code
  3764. block. In a case like the following, where the first block begins with
  3765. three spaces of indentation, the rules do not allow us to form a list item by
  3766. indenting the whole thing and prepending a list marker:
  3767. ```````````````````````````````` example
  3768. foo
  3769. bar
  3770. .
  3771. <p>foo</p>
  3772. <p>bar</p>
  3773. ````````````````````````````````
  3774. ```````````````````````````````` example
  3775. - foo
  3776. bar
  3777. .
  3778. <ul>
  3779. <li>foo</li>
  3780. </ul>
  3781. <p>bar</p>
  3782. ````````````````````````````````
  3783. This is not a significant restriction, because when a block is preceded by up to
  3784. three spaces of indentation, the indentation can always be removed without
  3785. a change in interpretation, allowing rule #1 to be applied. So, in
  3786. the above case:
  3787. ```````````````````````````````` example
  3788. - foo
  3789. bar
  3790. .
  3791. <ul>
  3792. <li>
  3793. <p>foo</p>
  3794. <p>bar</p>
  3795. </li>
  3796. </ul>
  3797. ````````````````````````````````
  3798. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3799. starting with a single [blank line] constitute a (possibly empty)
  3800. sequence of blocks *Bs*, and *M* is a list marker of width *W*,
  3801. then the result of prepending *M* to the first line of *Ls*, and
  3802. preceding subsequent lines of *Ls* by *W + 1* spaces of indentation, is a
  3803. list item with *Bs* as its contents.
  3804. If a line is empty, then it need not be indented. The type of the
  3805. list item (bullet or ordered) is determined by the type of its list
  3806. marker. If the list item is ordered, then it is also assigned a
  3807. start number, based on the ordered list marker.
  3808. Here are some list items that start with a blank line but are not empty:
  3809. ```````````````````````````````` example
  3810. -
  3811. foo
  3812. -
  3813. ```
  3814. bar
  3815. ```
  3816. -
  3817. baz
  3818. .
  3819. <ul>
  3820. <li>foo</li>
  3821. <li>
  3822. <pre><code>bar
  3823. </code></pre>
  3824. </li>
  3825. <li>
  3826. <pre><code>baz
  3827. </code></pre>
  3828. </li>
  3829. </ul>
  3830. ````````````````````````````````
  3831. When the list item starts with a blank line, the number of spaces
  3832. following the list marker doesn't change the required indentation:
  3833. ```````````````````````````````` example
  3834. -
  3835. foo
  3836. .
  3837. <ul>
  3838. <li>foo</li>
  3839. </ul>
  3840. ````````````````````````````````
  3841. A list item can begin with at most one blank line.
  3842. In the following example, `foo` is not part of the list
  3843. item:
  3844. ```````````````````````````````` example
  3845. -
  3846. foo
  3847. .
  3848. <ul>
  3849. <li></li>
  3850. </ul>
  3851. <p>foo</p>
  3852. ````````````````````````````````
  3853. Here is an empty bullet list item:
  3854. ```````````````````````````````` example
  3855. - foo
  3856. -
  3857. - bar
  3858. .
  3859. <ul>
  3860. <li>foo</li>
  3861. <li></li>
  3862. <li>bar</li>
  3863. </ul>
  3864. ````````````````````````````````
  3865. It does not matter whether there are spaces or tabs following the [list marker]:
  3866. ```````````````````````````````` example
  3867. - foo
  3868. -
  3869. - bar
  3870. .
  3871. <ul>
  3872. <li>foo</li>
  3873. <li></li>
  3874. <li>bar</li>
  3875. </ul>
  3876. ````````````````````````````````
  3877. Here is an empty ordered list item:
  3878. ```````````````````````````````` example
  3879. 1. foo
  3880. 2.
  3881. 3. bar
  3882. .
  3883. <ol>
  3884. <li>foo</li>
  3885. <li></li>
  3886. <li>bar</li>
  3887. </ol>
  3888. ````````````````````````````````
  3889. A list may start or end with an empty list item:
  3890. ```````````````````````````````` example
  3891. *
  3892. .
  3893. <ul>
  3894. <li></li>
  3895. </ul>
  3896. ````````````````````````````````
  3897. However, an empty list item cannot interrupt a paragraph:
  3898. ```````````````````````````````` example
  3899. foo
  3900. *
  3901. foo
  3902. 1.
  3903. .
  3904. <p>foo
  3905. *</p>
  3906. <p>foo
  3907. 1.</p>
  3908. ````````````````````````````````
  3909. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3910. according to rule #1, #2, or #3, then the result of preceding each line
  3911. of *Ls* by up to three spaces of indentation (the same for each line) also
  3912. constitutes a list item with the same contents and attributes. If a line is
  3913. empty, then it need not be indented.
  3914. Indented one space:
  3915. ```````````````````````````````` example
  3916. 1. A paragraph
  3917. with two lines.
  3918. indented code
  3919. > A block quote.
  3920. .
  3921. <ol>
  3922. <li>
  3923. <p>A paragraph
  3924. with two lines.</p>
  3925. <pre><code>indented code
  3926. </code></pre>
  3927. <blockquote>
  3928. <p>A block quote.</p>
  3929. </blockquote>
  3930. </li>
  3931. </ol>
  3932. ````````````````````````````````
  3933. Indented two spaces:
  3934. ```````````````````````````````` example
  3935. 1. A paragraph
  3936. with two lines.
  3937. indented code
  3938. > A block quote.
  3939. .
  3940. <ol>
  3941. <li>
  3942. <p>A paragraph
  3943. with two lines.</p>
  3944. <pre><code>indented code
  3945. </code></pre>
  3946. <blockquote>
  3947. <p>A block quote.</p>
  3948. </blockquote>
  3949. </li>
  3950. </ol>
  3951. ````````````````````````````````
  3952. Indented three spaces:
  3953. ```````````````````````````````` example
  3954. 1. A paragraph
  3955. with two lines.
  3956. indented code
  3957. > A block quote.
  3958. .
  3959. <ol>
  3960. <li>
  3961. <p>A paragraph
  3962. with two lines.</p>
  3963. <pre><code>indented code
  3964. </code></pre>
  3965. <blockquote>
  3966. <p>A block quote.</p>
  3967. </blockquote>
  3968. </li>
  3969. </ol>
  3970. ````````````````````````````````
  3971. Four spaces indent gives a code block:
  3972. ```````````````````````````````` example
  3973. 1. A paragraph
  3974. with two lines.
  3975. indented code
  3976. > A block quote.
  3977. .
  3978. <pre><code>1. A paragraph
  3979. with two lines.
  3980. indented code
  3981. &gt; A block quote.
  3982. </code></pre>
  3983. ````````````````````````````````
  3984. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3985. item](#list-items) with contents *Bs*, then the result of deleting
  3986. some or all of the indentation from one or more lines in which the
  3987. next character other than a space or tab after the indentation is
  3988. [paragraph continuation text] is a
  3989. list item with the same contents and attributes. The unindented
  3990. lines are called
  3991. [lazy continuation line](@)s.
  3992. Here is an example with [lazy continuation lines]:
  3993. ```````````````````````````````` example
  3994. 1. A paragraph
  3995. with two lines.
  3996. indented code
  3997. > A block quote.
  3998. .
  3999. <ol>
  4000. <li>
  4001. <p>A paragraph
  4002. with two lines.</p>
  4003. <pre><code>indented code
  4004. </code></pre>
  4005. <blockquote>
  4006. <p>A block quote.</p>
  4007. </blockquote>
  4008. </li>
  4009. </ol>
  4010. ````````````````````````````````
  4011. Indentation can be partially deleted:
  4012. ```````````````````````````````` example
  4013. 1. A paragraph
  4014. with two lines.
  4015. .
  4016. <ol>
  4017. <li>A paragraph
  4018. with two lines.</li>
  4019. </ol>
  4020. ````````````````````````````````
  4021. These examples show how laziness can work in nested structures:
  4022. ```````````````````````````````` example
  4023. > 1. > Blockquote
  4024. continued here.
  4025. .
  4026. <blockquote>
  4027. <ol>
  4028. <li>
  4029. <blockquote>
  4030. <p>Blockquote
  4031. continued here.</p>
  4032. </blockquote>
  4033. </li>
  4034. </ol>
  4035. </blockquote>
  4036. ````````````````````````````````
  4037. ```````````````````````````````` example
  4038. > 1. > Blockquote
  4039. > continued here.
  4040. .
  4041. <blockquote>
  4042. <ol>
  4043. <li>
  4044. <blockquote>
  4045. <p>Blockquote
  4046. continued here.</p>
  4047. </blockquote>
  4048. </li>
  4049. </ol>
  4050. </blockquote>
  4051. ````````````````````````````````
  4052. 6. **That's all.** Nothing that is not counted as a list item by rules
  4053. #1--5 counts as a [list item](#list-items).
  4054. The rules for sublists follow from the general rules
  4055. [above][List items]. A sublist must be indented the same number
  4056. of spaces of indentation a paragraph would need to be in order to be included
  4057. in the list item.
  4058. So, in this case we need two spaces indent:
  4059. ```````````````````````````````` example
  4060. - foo
  4061. - bar
  4062. - baz
  4063. - boo
  4064. .
  4065. <ul>
  4066. <li>foo
  4067. <ul>
  4068. <li>bar
  4069. <ul>
  4070. <li>baz
  4071. <ul>
  4072. <li>boo</li>
  4073. </ul>
  4074. </li>
  4075. </ul>
  4076. </li>
  4077. </ul>
  4078. </li>
  4079. </ul>
  4080. ````````````````````````````````
  4081. One is not enough:
  4082. ```````````````````````````````` example
  4083. - foo
  4084. - bar
  4085. - baz
  4086. - boo
  4087. .
  4088. <ul>
  4089. <li>foo</li>
  4090. <li>bar</li>
  4091. <li>baz</li>
  4092. <li>boo</li>
  4093. </ul>
  4094. ````````````````````````````````
  4095. Here we need four, because the list marker is wider:
  4096. ```````````````````````````````` example
  4097. 10) foo
  4098. - bar
  4099. .
  4100. <ol start="10">
  4101. <li>foo
  4102. <ul>
  4103. <li>bar</li>
  4104. </ul>
  4105. </li>
  4106. </ol>
  4107. ````````````````````````````````
  4108. Three is not enough:
  4109. ```````````````````````````````` example
  4110. 10) foo
  4111. - bar
  4112. .
  4113. <ol start="10">
  4114. <li>foo</li>
  4115. </ol>
  4116. <ul>
  4117. <li>bar</li>
  4118. </ul>
  4119. ````````````````````````````````
  4120. A list may be the first block in a list item:
  4121. ```````````````````````````````` example
  4122. - - foo
  4123. .
  4124. <ul>
  4125. <li>
  4126. <ul>
  4127. <li>foo</li>
  4128. </ul>
  4129. </li>
  4130. </ul>
  4131. ````````````````````````````````
  4132. ```````````````````````````````` example
  4133. 1. - 2. foo
  4134. .
  4135. <ol>
  4136. <li>
  4137. <ul>
  4138. <li>
  4139. <ol start="2">
  4140. <li>foo</li>
  4141. </ol>
  4142. </li>
  4143. </ul>
  4144. </li>
  4145. </ol>
  4146. ````````````````````````````````
  4147. A list item can contain a heading:
  4148. ```````````````````````````````` example
  4149. - # Foo
  4150. - Bar
  4151. ---
  4152. baz
  4153. .
  4154. <ul>
  4155. <li>
  4156. <h1>Foo</h1>
  4157. </li>
  4158. <li>
  4159. <h2>Bar</h2>
  4160. baz</li>
  4161. </ul>
  4162. ````````````````````````````````
  4163. ### Motivation
  4164. John Gruber's Markdown spec says the following about list items:
  4165. 1. "List markers typically start at the left margin, but may be indented
  4166. by up to three spaces. List markers must be followed by one or more
  4167. spaces or a tab."
  4168. 2. "To make lists look nice, you can wrap items with hanging indents....
  4169. But if you don't want to, you don't have to."
  4170. 3. "List items may consist of multiple paragraphs. Each subsequent
  4171. paragraph in a list item must be indented by either 4 spaces or one
  4172. tab."
  4173. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  4174. but here again, Markdown will allow you to be lazy."
  4175. 5. "To put a blockquote within a list item, the blockquote's `>`
  4176. delimiters need to be indented."
  4177. 6. "To put a code block within a list item, the code block needs to be
  4178. indented twice — 8 spaces or two tabs."
  4179. These rules specify that a paragraph under a list item must be indented
  4180. four spaces (presumably, from the left margin, rather than the start of
  4181. the list marker, but this is not said), and that code under a list item
  4182. must be indented eight spaces instead of the usual four. They also say
  4183. that a block quote must be indented, but not by how much; however, the
  4184. example given has four spaces indentation. Although nothing is said
  4185. about other kinds of block-level content, it is certainly reasonable to
  4186. infer that *all* block elements under a list item, including other
  4187. lists, must be indented four spaces. This principle has been called the
  4188. *four-space rule*.
  4189. The four-space rule is clear and principled, and if the reference
  4190. implementation `Markdown.pl` had followed it, it probably would have
  4191. become the standard. However, `Markdown.pl` allowed paragraphs and
  4192. sublists to start with only two spaces indentation, at least on the
  4193. outer level. Worse, its behavior was inconsistent: a sublist of an
  4194. outer-level list needed two spaces indentation, but a sublist of this
  4195. sublist needed three spaces. It is not surprising, then, that different
  4196. implementations of Markdown have developed very different rules for
  4197. determining what comes under a list item. (Pandoc and python-Markdown,
  4198. for example, stuck with Gruber's syntax description and the four-space
  4199. rule, while discount, redcarpet, marked, PHP Markdown, and others
  4200. followed `Markdown.pl`'s behavior more closely.)
  4201. Unfortunately, given the divergences between implementations, there
  4202. is no way to give a spec for list items that will be guaranteed not
  4203. to break any existing documents. However, the spec given here should
  4204. correctly handle lists formatted with either the four-space rule or
  4205. the more forgiving `Markdown.pl` behavior, provided they are laid out
  4206. in a way that is natural for a human to read.
  4207. The strategy here is to let the width and indentation of the list marker
  4208. determine the indentation necessary for blocks to fall under the list
  4209. item, rather than having a fixed and arbitrary number. The writer can
  4210. think of the body of the list item as a unit which gets indented to the
  4211. right enough to fit the list marker (and any indentation on the list
  4212. marker). (The laziness rule, #5, then allows continuation lines to be
  4213. unindented if needed.)
  4214. This rule is superior, we claim, to any rule requiring a fixed level of
  4215. indentation from the margin. The four-space rule is clear but
  4216. unnatural. It is quite unintuitive that
  4217. ``` markdown
  4218. - foo
  4219. bar
  4220. - baz
  4221. ```
  4222. should be parsed as two lists with an intervening paragraph,
  4223. ``` html
  4224. <ul>
  4225. <li>foo</li>
  4226. </ul>
  4227. <p>bar</p>
  4228. <ul>
  4229. <li>baz</li>
  4230. </ul>
  4231. ```
  4232. as the four-space rule demands, rather than a single list,
  4233. ``` html
  4234. <ul>
  4235. <li>
  4236. <p>foo</p>
  4237. <p>bar</p>
  4238. <ul>
  4239. <li>baz</li>
  4240. </ul>
  4241. </li>
  4242. </ul>
  4243. ```
  4244. The choice of four spaces is arbitrary. It can be learned, but it is
  4245. not likely to be guessed, and it trips up beginners regularly.
  4246. Would it help to adopt a two-space rule? The problem is that such
  4247. a rule, together with the rule allowing up to three spaces of indentation for
  4248. the initial list marker, allows text that is indented *less than* the
  4249. original list marker to be included in the list item. For example,
  4250. `Markdown.pl` parses
  4251. ``` markdown
  4252. - one
  4253. two
  4254. ```
  4255. as a single list item, with `two` a continuation paragraph:
  4256. ``` html
  4257. <ul>
  4258. <li>
  4259. <p>one</p>
  4260. <p>two</p>
  4261. </li>
  4262. </ul>
  4263. ```
  4264. and similarly
  4265. ``` markdown
  4266. > - one
  4267. >
  4268. > two
  4269. ```
  4270. as
  4271. ``` html
  4272. <blockquote>
  4273. <ul>
  4274. <li>
  4275. <p>one</p>
  4276. <p>two</p>
  4277. </li>
  4278. </ul>
  4279. </blockquote>
  4280. ```
  4281. This is extremely unintuitive.
  4282. Rather than requiring a fixed indent from the margin, we could require
  4283. a fixed indent (say, two spaces, or even one space) from the list marker (which
  4284. may itself be indented). This proposal would remove the last anomaly
  4285. discussed. Unlike the spec presented above, it would count the following
  4286. as a list item with a subparagraph, even though the paragraph `bar`
  4287. is not indented as far as the first paragraph `foo`:
  4288. ``` markdown
  4289. 10. foo
  4290. bar
  4291. ```
  4292. Arguably this text does read like a list item with `bar` as a subparagraph,
  4293. which may count in favor of the proposal. However, on this proposal indented
  4294. code would have to be indented six spaces after the list marker. And this
  4295. would break a lot of existing Markdown, which has the pattern:
  4296. ``` markdown
  4297. 1. foo
  4298. indented code
  4299. ```
  4300. where the code is indented eight spaces. The spec above, by contrast, will
  4301. parse this text as expected, since the code block's indentation is measured
  4302. from the beginning of `foo`.
  4303. The one case that needs special treatment is a list item that *starts*
  4304. with indented code. How much indentation is required in that case, since
  4305. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  4306. that in such cases, we require one space indentation from the list marker
  4307. (and then the normal four spaces for the indented code). This will match the
  4308. four-space rule in cases where the list marker plus its initial indentation
  4309. takes four spaces (a common case), but diverge in other cases.
  4310. ## Lists
  4311. A [list](@) is a sequence of one or more
  4312. list items [of the same type]. The list items
  4313. may be separated by any number of blank lines.
  4314. Two list items are [of the same type](@)
  4315. if they begin with a [list marker] of the same type.
  4316. Two list markers are of the
  4317. same type if (a) they are bullet list markers using the same character
  4318. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  4319. delimiter (either `.` or `)`).
  4320. A list is an [ordered list](@)
  4321. if its constituent list items begin with
  4322. [ordered list markers], and a
  4323. [bullet list](@) if its constituent list
  4324. items begin with [bullet list markers].
  4325. The [start number](@)
  4326. of an [ordered list] is determined by the list number of
  4327. its initial list item. The numbers of subsequent list items are
  4328. disregarded.
  4329. A list is [loose](@) if any of its constituent
  4330. list items are separated by blank lines, or if any of its constituent
  4331. list items directly contain two block-level elements with a blank line
  4332. between them. Otherwise a list is [tight](@).
  4333. (The difference in HTML output is that paragraphs in a loose list are
  4334. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  4335. Changing the bullet or ordered list delimiter starts a new list:
  4336. ```````````````````````````````` example
  4337. - foo
  4338. - bar
  4339. + baz
  4340. .
  4341. <ul>
  4342. <li>foo</li>
  4343. <li>bar</li>
  4344. </ul>
  4345. <ul>
  4346. <li>baz</li>
  4347. </ul>
  4348. ````````````````````````````````
  4349. ```````````````````````````````` example
  4350. 1. foo
  4351. 2. bar
  4352. 3) baz
  4353. .
  4354. <ol>
  4355. <li>foo</li>
  4356. <li>bar</li>
  4357. </ol>
  4358. <ol start="3">
  4359. <li>baz</li>
  4360. </ol>
  4361. ````````````````````````````````
  4362. In CommonMark, a list can interrupt a paragraph. That is,
  4363. no blank line is needed to separate a paragraph from a following
  4364. list:
  4365. ```````````````````````````````` example
  4366. Foo
  4367. - bar
  4368. - baz
  4369. .
  4370. <p>Foo</p>
  4371. <ul>
  4372. <li>bar</li>
  4373. <li>baz</li>
  4374. </ul>
  4375. ````````````````````````````````
  4376. `Markdown.pl` does not allow this, through fear of triggering a list
  4377. via a numeral in a hard-wrapped line:
  4378. ``` markdown
  4379. The number of windows in my house is
  4380. 14. The number of doors is 6.
  4381. ```
  4382. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  4383. interrupt a paragraph, even though the same considerations might
  4384. apply.
  4385. In CommonMark, we do allow lists to interrupt paragraphs, for
  4386. two reasons. First, it is natural and not uncommon for people
  4387. to start lists without blank lines:
  4388. ``` markdown
  4389. I need to buy
  4390. - new shoes
  4391. - a coat
  4392. - a plane ticket
  4393. ```
  4394. Second, we are attracted to a
  4395. > [principle of uniformity](@):
  4396. > if a chunk of text has a certain
  4397. > meaning, it will continue to have the same meaning when put into a
  4398. > container block (such as a list item or blockquote).
  4399. (Indeed, the spec for [list items] and [block quotes] presupposes
  4400. this principle.) This principle implies that if
  4401. ``` markdown
  4402. * I need to buy
  4403. - new shoes
  4404. - a coat
  4405. - a plane ticket
  4406. ```
  4407. is a list item containing a paragraph followed by a nested sublist,
  4408. as all Markdown implementations agree it is (though the paragraph
  4409. may be rendered without `<p>` tags, since the list is "tight"),
  4410. then
  4411. ``` markdown
  4412. I need to buy
  4413. - new shoes
  4414. - a coat
  4415. - a plane ticket
  4416. ```
  4417. by itself should be a paragraph followed by a nested sublist.
  4418. Since it is well established Markdown practice to allow lists to
  4419. interrupt paragraphs inside list items, the [principle of
  4420. uniformity] requires us to allow this outside list items as
  4421. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  4422. takes a different approach, requiring blank lines before lists
  4423. even inside other list items.)
  4424. In order to solve of unwanted lists in paragraphs with
  4425. hard-wrapped numerals, we allow only lists starting with `1` to
  4426. interrupt paragraphs. Thus,
  4427. ```````````````````````````````` example
  4428. The number of windows in my house is
  4429. 14. The number of doors is 6.
  4430. .
  4431. <p>The number of windows in my house is
  4432. 14. The number of doors is 6.</p>
  4433. ````````````````````````````````
  4434. We may still get an unintended result in cases like
  4435. ```````````````````````````````` example
  4436. The number of windows in my house is
  4437. 1. The number of doors is 6.
  4438. .
  4439. <p>The number of windows in my house is</p>
  4440. <ol>
  4441. <li>The number of doors is 6.</li>
  4442. </ol>
  4443. ````````````````````````````````
  4444. but this rule should prevent most spurious list captures.
  4445. There can be any number of blank lines between items:
  4446. ```````````````````````````````` example
  4447. - foo
  4448. - bar
  4449. - baz
  4450. .
  4451. <ul>
  4452. <li>
  4453. <p>foo</p>
  4454. </li>
  4455. <li>
  4456. <p>bar</p>
  4457. </li>
  4458. <li>
  4459. <p>baz</p>
  4460. </li>
  4461. </ul>
  4462. ````````````````````````````````
  4463. ```````````````````````````````` example
  4464. - foo
  4465. - bar
  4466. - baz
  4467. bim
  4468. .
  4469. <ul>
  4470. <li>foo
  4471. <ul>
  4472. <li>bar
  4473. <ul>
  4474. <li>
  4475. <p>baz</p>
  4476. <p>bim</p>
  4477. </li>
  4478. </ul>
  4479. </li>
  4480. </ul>
  4481. </li>
  4482. </ul>
  4483. ````````````````````````````````
  4484. To separate consecutive lists of the same type, or to separate a
  4485. list from an indented code block that would otherwise be parsed
  4486. as a subparagraph of the final list item, you can insert a blank HTML
  4487. comment:
  4488. ```````````````````````````````` example
  4489. - foo
  4490. - bar
  4491. <!-- -->
  4492. - baz
  4493. - bim
  4494. .
  4495. <ul>
  4496. <li>foo</li>
  4497. <li>bar</li>
  4498. </ul>
  4499. <!-- -->
  4500. <ul>
  4501. <li>baz</li>
  4502. <li>bim</li>
  4503. </ul>
  4504. ````````````````````````````````
  4505. ```````````````````````````````` example
  4506. - foo
  4507. notcode
  4508. - foo
  4509. <!-- -->
  4510. code
  4511. .
  4512. <ul>
  4513. <li>
  4514. <p>foo</p>
  4515. <p>notcode</p>
  4516. </li>
  4517. <li>
  4518. <p>foo</p>
  4519. </li>
  4520. </ul>
  4521. <!-- -->
  4522. <pre><code>code
  4523. </code></pre>
  4524. ````````````````````````````````
  4525. List items need not be indented to the same level. The following
  4526. list items will be treated as items at the same list level,
  4527. since none is indented enough to belong to the previous list
  4528. item:
  4529. ```````````````````````````````` example
  4530. - a
  4531. - b
  4532. - c
  4533. - d
  4534. - e
  4535. - f
  4536. - g
  4537. .
  4538. <ul>
  4539. <li>a</li>
  4540. <li>b</li>
  4541. <li>c</li>
  4542. <li>d</li>
  4543. <li>e</li>
  4544. <li>f</li>
  4545. <li>g</li>
  4546. </ul>
  4547. ````````````````````````````````
  4548. ```````````````````````````````` example
  4549. 1. a
  4550. 2. b
  4551. 3. c
  4552. .
  4553. <ol>
  4554. <li>
  4555. <p>a</p>
  4556. </li>
  4557. <li>
  4558. <p>b</p>
  4559. </li>
  4560. <li>
  4561. <p>c</p>
  4562. </li>
  4563. </ol>
  4564. ````````````````````````````````
  4565. Note, however, that list items may not be preceded by more than
  4566. three spaces of indentation. Here `- e` is treated as a paragraph continuation
  4567. line, because it is indented more than three spaces:
  4568. ```````````````````````````````` example
  4569. - a
  4570. - b
  4571. - c
  4572. - d
  4573. - e
  4574. .
  4575. <ul>
  4576. <li>a</li>
  4577. <li>b</li>
  4578. <li>c</li>
  4579. <li>d
  4580. - e</li>
  4581. </ul>
  4582. ````````````````````````````````
  4583. And here, `3. c` is treated as in indented code block,
  4584. because it is indented four spaces and preceded by a
  4585. blank line.
  4586. ```````````````````````````````` example
  4587. 1. a
  4588. 2. b
  4589. 3. c
  4590. .
  4591. <ol>
  4592. <li>
  4593. <p>a</p>
  4594. </li>
  4595. <li>
  4596. <p>b</p>
  4597. </li>
  4598. </ol>
  4599. <pre><code>3. c
  4600. </code></pre>
  4601. ````````````````````````````````
  4602. This is a loose list, because there is a blank line between
  4603. two of the list items:
  4604. ```````````````````````````````` example
  4605. - a
  4606. - b
  4607. - c
  4608. .
  4609. <ul>
  4610. <li>
  4611. <p>a</p>
  4612. </li>
  4613. <li>
  4614. <p>b</p>
  4615. </li>
  4616. <li>
  4617. <p>c</p>
  4618. </li>
  4619. </ul>
  4620. ````````````````````````````````
  4621. So is this, with a empty second item:
  4622. ```````````````````````````````` example
  4623. * a
  4624. *
  4625. * c
  4626. .
  4627. <ul>
  4628. <li>
  4629. <p>a</p>
  4630. </li>
  4631. <li></li>
  4632. <li>
  4633. <p>c</p>
  4634. </li>
  4635. </ul>
  4636. ````````````````````````````````
  4637. These are loose lists, even though there are no blank lines between the items,
  4638. because one of the items directly contains two block-level elements
  4639. with a blank line between them:
  4640. ```````````````````````````````` example
  4641. - a
  4642. - b
  4643. c
  4644. - d
  4645. .
  4646. <ul>
  4647. <li>
  4648. <p>a</p>
  4649. </li>
  4650. <li>
  4651. <p>b</p>
  4652. <p>c</p>
  4653. </li>
  4654. <li>
  4655. <p>d</p>
  4656. </li>
  4657. </ul>
  4658. ````````````````````````````````
  4659. ```````````````````````````````` example
  4660. - a
  4661. - b
  4662. [ref]: /url
  4663. - d
  4664. .
  4665. <ul>
  4666. <li>
  4667. <p>a</p>
  4668. </li>
  4669. <li>
  4670. <p>b</p>
  4671. </li>
  4672. <li>
  4673. <p>d</p>
  4674. </li>
  4675. </ul>
  4676. ````````````````````````````````
  4677. This is a tight list, because the blank lines are in a code block:
  4678. ```````````````````````````````` example
  4679. - a
  4680. - ```
  4681. b
  4682. ```
  4683. - c
  4684. .
  4685. <ul>
  4686. <li>a</li>
  4687. <li>
  4688. <pre><code>b
  4689. </code></pre>
  4690. </li>
  4691. <li>c</li>
  4692. </ul>
  4693. ````````````````````````````````
  4694. This is a tight list, because the blank line is between two
  4695. paragraphs of a sublist. So the sublist is loose while
  4696. the outer list is tight:
  4697. ```````````````````````````````` example
  4698. - a
  4699. - b
  4700. c
  4701. - d
  4702. .
  4703. <ul>
  4704. <li>a
  4705. <ul>
  4706. <li>
  4707. <p>b</p>
  4708. <p>c</p>
  4709. </li>
  4710. </ul>
  4711. </li>
  4712. <li>d</li>
  4713. </ul>
  4714. ````````````````````````````````
  4715. This is a tight list, because the blank line is inside the
  4716. block quote:
  4717. ```````````````````````````````` example
  4718. * a
  4719. > b
  4720. >
  4721. * c
  4722. .
  4723. <ul>
  4724. <li>a
  4725. <blockquote>
  4726. <p>b</p>
  4727. </blockquote>
  4728. </li>
  4729. <li>c</li>
  4730. </ul>
  4731. ````````````````````````````````
  4732. This list is tight, because the consecutive block elements
  4733. are not separated by blank lines:
  4734. ```````````````````````````````` example
  4735. - a
  4736. > b
  4737. ```
  4738. c
  4739. ```
  4740. - d
  4741. .
  4742. <ul>
  4743. <li>a
  4744. <blockquote>
  4745. <p>b</p>
  4746. </blockquote>
  4747. <pre><code>c
  4748. </code></pre>
  4749. </li>
  4750. <li>d</li>
  4751. </ul>
  4752. ````````````````````````````````
  4753. A single-paragraph list is tight:
  4754. ```````````````````````````````` example
  4755. - a
  4756. .
  4757. <ul>
  4758. <li>a</li>
  4759. </ul>
  4760. ````````````````````````````````
  4761. ```````````````````````````````` example
  4762. - a
  4763. - b
  4764. .
  4765. <ul>
  4766. <li>a
  4767. <ul>
  4768. <li>b</li>
  4769. </ul>
  4770. </li>
  4771. </ul>
  4772. ````````````````````````````````
  4773. This list is loose, because of the blank line between the
  4774. two block elements in the list item:
  4775. ```````````````````````````````` example
  4776. 1. ```
  4777. foo
  4778. ```
  4779. bar
  4780. .
  4781. <ol>
  4782. <li>
  4783. <pre><code>foo
  4784. </code></pre>
  4785. <p>bar</p>
  4786. </li>
  4787. </ol>
  4788. ````````````````````````````````
  4789. Here the outer list is loose, the inner list tight:
  4790. ```````````````````````````````` example
  4791. * foo
  4792. * bar
  4793. baz
  4794. .
  4795. <ul>
  4796. <li>
  4797. <p>foo</p>
  4798. <ul>
  4799. <li>bar</li>
  4800. </ul>
  4801. <p>baz</p>
  4802. </li>
  4803. </ul>
  4804. ````````````````````````````````
  4805. ```````````````````````````````` example
  4806. - a
  4807. - b
  4808. - c
  4809. - d
  4810. - e
  4811. - f
  4812. .
  4813. <ul>
  4814. <li>
  4815. <p>a</p>
  4816. <ul>
  4817. <li>b</li>
  4818. <li>c</li>
  4819. </ul>
  4820. </li>
  4821. <li>
  4822. <p>d</p>
  4823. <ul>
  4824. <li>e</li>
  4825. <li>f</li>
  4826. </ul>
  4827. </li>
  4828. </ul>
  4829. ````````````````````````````````
  4830. # Inlines
  4831. Inlines are parsed sequentially from the beginning of the character
  4832. stream to the end (left to right, in left-to-right languages).
  4833. Thus, for example, in
  4834. ```````````````````````````````` example
  4835. `hi`lo`
  4836. .
  4837. <p><code>hi</code>lo`</p>
  4838. ````````````````````````````````
  4839. `hi` is parsed as code, leaving the backtick at the end as a literal
  4840. backtick.
  4841. ## Code spans
  4842. A [backtick string](@)
  4843. is a string of one or more backtick characters (`` ` ``) that is neither
  4844. preceded nor followed by a backtick.
  4845. A [code span](@) begins with a backtick string and ends with
  4846. a backtick string of equal length. The contents of the code span are
  4847. the characters between these two backtick strings, normalized in the
  4848. following ways:
  4849. - First, [line endings] are converted to [spaces].
  4850. - If the resulting string both begins *and* ends with a [space]
  4851. character, but does not consist entirely of [space]
  4852. characters, a single [space] character is removed from the
  4853. front and back. This allows you to include code that begins
  4854. or ends with backtick characters, which must be separated by
  4855. whitespace from the opening or closing backtick strings.
  4856. This is a simple code span:
  4857. ```````````````````````````````` example
  4858. `foo`
  4859. .
  4860. <p><code>foo</code></p>
  4861. ````````````````````````````````
  4862. Here two backticks are used, because the code contains a backtick.
  4863. This example also illustrates stripping of a single leading and
  4864. trailing space:
  4865. ```````````````````````````````` example
  4866. `` foo ` bar ``
  4867. .
  4868. <p><code>foo ` bar</code></p>
  4869. ````````````````````````````````
  4870. This example shows the motivation for stripping leading and trailing
  4871. spaces:
  4872. ```````````````````````````````` example
  4873. ` `` `
  4874. .
  4875. <p><code>``</code></p>
  4876. ````````````````````````````````
  4877. Note that only *one* space is stripped:
  4878. ```````````````````````````````` example
  4879. ` `` `
  4880. .
  4881. <p><code> `` </code></p>
  4882. ````````````````````````````````
  4883. The stripping only happens if the space is on both
  4884. sides of the string:
  4885. ```````````````````````````````` example
  4886. ` a`
  4887. .
  4888. <p><code> a</code></p>
  4889. ````````````````````````````````
  4890. Only [spaces], and not [unicode whitespace] in general, are
  4891. stripped in this way:
  4892. ```````````````````````````````` example
  4893. ` b `
  4894. .
  4895. <p><code> b </code></p>
  4896. ````````````````````````````````
  4897. No stripping occurs if the code span contains only spaces:
  4898. ```````````````````````````````` example
  4899. ` `
  4900. ` `
  4901. .
  4902. <p><code> </code>
  4903. <code> </code></p>
  4904. ````````````````````````````````
  4905. [Line endings] are treated like spaces:
  4906. ```````````````````````````````` example
  4907. ``
  4908. foo
  4909. bar
  4910. baz
  4911. ``
  4912. .
  4913. <p><code>foo bar baz</code></p>
  4914. ````````````````````````````````
  4915. ```````````````````````````````` example
  4916. ``
  4917. foo
  4918. ``
  4919. .
  4920. <p><code>foo </code></p>
  4921. ````````````````````````````````
  4922. Interior spaces are not collapsed:
  4923. ```````````````````````````````` example
  4924. `foo bar
  4925. baz`
  4926. .
  4927. <p><code>foo bar baz</code></p>
  4928. ````````````````````````````````
  4929. Note that browsers will typically collapse consecutive spaces
  4930. when rendering `<code>` elements, so it is recommended that
  4931. the following CSS be used:
  4932. code{white-space: pre-wrap;}
  4933. Note that backslash escapes do not work in code spans. All backslashes
  4934. are treated literally:
  4935. ```````````````````````````````` example
  4936. `foo\`bar`
  4937. .
  4938. <p><code>foo\</code>bar`</p>
  4939. ````````````````````````````````
  4940. Backslash escapes are never needed, because one can always choose a
  4941. string of *n* backtick characters as delimiters, where the code does
  4942. not contain any strings of exactly *n* backtick characters.
  4943. ```````````````````````````````` example
  4944. ``foo`bar``
  4945. .
  4946. <p><code>foo`bar</code></p>
  4947. ````````````````````````````````
  4948. ```````````````````````````````` example
  4949. ` foo `` bar `
  4950. .
  4951. <p><code>foo `` bar</code></p>
  4952. ````````````````````````````````
  4953. Code span backticks have higher precedence than any other inline
  4954. constructs except HTML tags and autolinks. Thus, for example, this is
  4955. not parsed as emphasized text, since the second `*` is part of a code
  4956. span:
  4957. ```````````````````````````````` example
  4958. *foo`*`
  4959. .
  4960. <p>*foo<code>*</code></p>
  4961. ````````````````````````````````
  4962. And this is not parsed as a link:
  4963. ```````````````````````````````` example
  4964. [not a `link](/foo`)
  4965. .
  4966. <p>[not a <code>link](/foo</code>)</p>
  4967. ````````````````````````````````
  4968. Code spans, HTML tags, and autolinks have the same precedence.
  4969. Thus, this is code:
  4970. ```````````````````````````````` example
  4971. `<a href="`">`
  4972. .
  4973. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4974. ````````````````````````````````
  4975. But this is an HTML tag:
  4976. ```````````````````````````````` example
  4977. <a href="`">`
  4978. .
  4979. <p><a href="`">`</p>
  4980. ````````````````````````````````
  4981. And this is code:
  4982. ```````````````````````````````` example
  4983. `<http://foo.bar.`baz>`
  4984. .
  4985. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4986. ````````````````````````````````
  4987. But this is an autolink:
  4988. ```````````````````````````````` example
  4989. <http://foo.bar.`baz>`
  4990. .
  4991. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4992. ````````````````````````````````
  4993. When a backtick string is not closed by a matching backtick string,
  4994. we just have literal backticks:
  4995. ```````````````````````````````` example
  4996. ```foo``
  4997. .
  4998. <p>```foo``</p>
  4999. ````````````````````````````````
  5000. ```````````````````````````````` example
  5001. `foo
  5002. .
  5003. <p>`foo</p>
  5004. ````````````````````````````````
  5005. The following case also illustrates the need for opening and
  5006. closing backtick strings to be equal in length:
  5007. ```````````````````````````````` example
  5008. `foo``bar``
  5009. .
  5010. <p>`foo<code>bar</code></p>
  5011. ````````````````````````````````
  5012. ## Emphasis and strong emphasis
  5013. John Gruber's original [Markdown syntax
  5014. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  5015. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  5016. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  5017. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  5018. > tag.
  5019. This is enough for most users, but these rules leave much undecided,
  5020. especially when it comes to nested emphasis. The original
  5021. `Markdown.pl` test suite makes it clear that triple `***` and
  5022. `___` delimiters can be used for strong emphasis, and most
  5023. implementations have also allowed the following patterns:
  5024. ``` markdown
  5025. ***strong emph***
  5026. ***strong** in emph*
  5027. ***emph* in strong**
  5028. **in strong *emph***
  5029. *in emph **strong***
  5030. ```
  5031. The following patterns are less widely supported, but the intent
  5032. is clear and they are useful (especially in contexts like bibliography
  5033. entries):
  5034. ``` markdown
  5035. *emph *with emph* in it*
  5036. **strong **with strong** in it**
  5037. ```
  5038. Many implementations have also restricted intraword emphasis to
  5039. the `*` forms, to avoid unwanted emphasis in words containing
  5040. internal underscores. (It is best practice to put these in code
  5041. spans, but users often do not.)
  5042. ``` markdown
  5043. internal emphasis: foo*bar*baz
  5044. no emphasis: foo_bar_baz
  5045. ```
  5046. The rules given below capture all of these patterns, while allowing
  5047. for efficient parsing strategies that do not backtrack.
  5048. First, some definitions. A [delimiter run](@) is either
  5049. a sequence of one or more `*` characters that is not preceded or
  5050. followed by a non-backslash-escaped `*` character, or a sequence
  5051. of one or more `_` characters that is not preceded or followed by
  5052. a non-backslash-escaped `_` character.
  5053. A [left-flanking delimiter run](@) is
  5054. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  5055. and either (2a) not followed by a [Unicode punctuation character], or
  5056. (2b) followed by a [Unicode punctuation character] and
  5057. preceded by [Unicode whitespace] or a [Unicode punctuation character].
  5058. For purposes of this definition, the beginning and the end of
  5059. the line count as Unicode whitespace.
  5060. A [right-flanking delimiter run](@) is
  5061. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  5062. and either (2a) not preceded by a [Unicode punctuation character], or
  5063. (2b) preceded by a [Unicode punctuation character] and
  5064. followed by [Unicode whitespace] or a [Unicode punctuation character].
  5065. For purposes of this definition, the beginning and the end of
  5066. the line count as Unicode whitespace.
  5067. Here are some examples of delimiter runs.
  5068. - left-flanking but not right-flanking:
  5069. ```
  5070. ***abc
  5071. _abc
  5072. **"abc"
  5073. _"abc"
  5074. ```
  5075. - right-flanking but not left-flanking:
  5076. ```
  5077. abc***
  5078. abc_
  5079. "abc"**
  5080. "abc"_
  5081. ```
  5082. - Both left and right-flanking:
  5083. ```
  5084. abc***def
  5085. "abc"_"def"
  5086. ```
  5087. - Neither left nor right-flanking:
  5088. ```
  5089. abc *** def
  5090. a _ b
  5091. ```
  5092. (The idea of distinguishing left-flanking and right-flanking
  5093. delimiter runs based on the character before and the character
  5094. after comes from Roopesh Chander's
  5095. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  5096. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  5097. run," and its rules for distinguishing left- and right-flanking runs
  5098. are a bit more complex than the ones given here.)
  5099. The following rules define emphasis and strong emphasis:
  5100. 1. A single `*` character [can open emphasis](@)
  5101. iff (if and only if) it is part of a [left-flanking delimiter run].
  5102. 2. A single `_` character [can open emphasis] iff
  5103. it is part of a [left-flanking delimiter run]
  5104. and either (a) not part of a [right-flanking delimiter run]
  5105. or (b) part of a [right-flanking delimiter run]
  5106. preceded by a [Unicode punctuation character].
  5107. 3. A single `*` character [can close emphasis](@)
  5108. iff it is part of a [right-flanking delimiter run].
  5109. 4. A single `_` character [can close emphasis] iff
  5110. it is part of a [right-flanking delimiter run]
  5111. and either (a) not part of a [left-flanking delimiter run]
  5112. or (b) part of a [left-flanking delimiter run]
  5113. followed by a [Unicode punctuation character].
  5114. 5. A double `**` [can open strong emphasis](@)
  5115. iff it is part of a [left-flanking delimiter run].
  5116. 6. A double `__` [can open strong emphasis] iff
  5117. it is part of a [left-flanking delimiter run]
  5118. and either (a) not part of a [right-flanking delimiter run]
  5119. or (b) part of a [right-flanking delimiter run]
  5120. preceded by a [Unicode punctuation character].
  5121. 7. A double `**` [can close strong emphasis](@)
  5122. iff it is part of a [right-flanking delimiter run].
  5123. 8. A double `__` [can close strong emphasis] iff
  5124. it is part of a [right-flanking delimiter run]
  5125. and either (a) not part of a [left-flanking delimiter run]
  5126. or (b) part of a [left-flanking delimiter run]
  5127. followed by a [Unicode punctuation character].
  5128. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  5129. with a delimiter that [can close emphasis], and that uses the same
  5130. character (`_` or `*`) as the opening delimiter. The
  5131. opening and closing delimiters must belong to separate
  5132. [delimiter runs]. If one of the delimiters can both
  5133. open and close emphasis, then the sum of the lengths of the
  5134. delimiter runs containing the opening and closing delimiters
  5135. must not be a multiple of 3 unless both lengths are
  5136. multiples of 3.
  5137. 10. Strong emphasis begins with a delimiter that
  5138. [can open strong emphasis] and ends with a delimiter that
  5139. [can close strong emphasis], and that uses the same character
  5140. (`_` or `*`) as the opening delimiter. The
  5141. opening and closing delimiters must belong to separate
  5142. [delimiter runs]. If one of the delimiters can both open
  5143. and close strong emphasis, then the sum of the lengths of
  5144. the delimiter runs containing the opening and closing
  5145. delimiters must not be a multiple of 3 unless both lengths
  5146. are multiples of 3.
  5147. 11. A literal `*` character cannot occur at the beginning or end of
  5148. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  5149. is backslash-escaped.
  5150. 12. A literal `_` character cannot occur at the beginning or end of
  5151. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  5152. is backslash-escaped.
  5153. Where rules 1--12 above are compatible with multiple parsings,
  5154. the following principles resolve ambiguity:
  5155. 13. The number of nestings should be minimized. Thus, for example,
  5156. an interpretation `<strong>...</strong>` is always preferred to
  5157. `<em><em>...</em></em>`.
  5158. 14. An interpretation `<em><strong>...</strong></em>` is always
  5159. preferred to `<strong><em>...</em></strong>`.
  5160. 15. When two potential emphasis or strong emphasis spans overlap,
  5161. so that the second begins before the first ends and ends after
  5162. the first ends, the first takes precedence. Thus, for example,
  5163. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  5164. than `*foo <em>bar* baz</em>`.
  5165. 16. When there are two potential emphasis or strong emphasis spans
  5166. with the same closing delimiter, the shorter one (the one that
  5167. opens later) takes precedence. Thus, for example,
  5168. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  5169. rather than `<strong>foo **bar baz</strong>`.
  5170. 17. Inline code spans, links, images, and HTML tags group more tightly
  5171. than emphasis. So, when there is a choice between an interpretation
  5172. that contains one of these elements and one that does not, the
  5173. former always wins. Thus, for example, `*[foo*](bar)` is
  5174. parsed as `*<a href="bar">foo*</a>` rather than as
  5175. `<em>[foo</em>](bar)`.
  5176. These rules can be illustrated through a series of examples.
  5177. Rule 1:
  5178. ```````````````````````````````` example
  5179. *foo bar*
  5180. .
  5181. <p><em>foo bar</em></p>
  5182. ````````````````````````````````
  5183. This is not emphasis, because the opening `*` is followed by
  5184. whitespace, and hence not part of a [left-flanking delimiter run]:
  5185. ```````````````````````````````` example
  5186. a * foo bar*
  5187. .
  5188. <p>a * foo bar*</p>
  5189. ````````````````````````````````
  5190. This is not emphasis, because the opening `*` is preceded
  5191. by an alphanumeric and followed by punctuation, and hence
  5192. not part of a [left-flanking delimiter run]:
  5193. ```````````````````````````````` example
  5194. a*"foo"*
  5195. .
  5196. <p>a*&quot;foo&quot;*</p>
  5197. ````````````````````````````````
  5198. Unicode nonbreaking spaces count as whitespace, too:
  5199. ```````````````````````````````` example
  5200. * a *
  5201. .
  5202. <p>* a *</p>
  5203. ````````````````````````````````
  5204. Intraword emphasis with `*` is permitted:
  5205. ```````````````````````````````` example
  5206. foo*bar*
  5207. .
  5208. <p>foo<em>bar</em></p>
  5209. ````````````````````````````````
  5210. ```````````````````````````````` example
  5211. 5*6*78
  5212. .
  5213. <p>5<em>6</em>78</p>
  5214. ````````````````````````````````
  5215. Rule 2:
  5216. ```````````````````````````````` example
  5217. _foo bar_
  5218. .
  5219. <p><em>foo bar</em></p>
  5220. ````````````````````````````````
  5221. This is not emphasis, because the opening `_` is followed by
  5222. whitespace:
  5223. ```````````````````````````````` example
  5224. _ foo bar_
  5225. .
  5226. <p>_ foo bar_</p>
  5227. ````````````````````````````````
  5228. This is not emphasis, because the opening `_` is preceded
  5229. by an alphanumeric and followed by punctuation:
  5230. ```````````````````````````````` example
  5231. a_"foo"_
  5232. .
  5233. <p>a_&quot;foo&quot;_</p>
  5234. ````````````````````````````````
  5235. Emphasis with `_` is not allowed inside words:
  5236. ```````````````````````````````` example
  5237. foo_bar_
  5238. .
  5239. <p>foo_bar_</p>
  5240. ````````````````````````````````
  5241. ```````````````````````````````` example
  5242. 5_6_78
  5243. .
  5244. <p>5_6_78</p>
  5245. ````````````````````````````````
  5246. ```````````````````````````````` example
  5247. пристаням_стремятся_
  5248. .
  5249. <p>пристаням_стремятся_</p>
  5250. ````````````````````````````````
  5251. Here `_` does not generate emphasis, because the first delimiter run
  5252. is right-flanking and the second left-flanking:
  5253. ```````````````````````````````` example
  5254. aa_"bb"_cc
  5255. .
  5256. <p>aa_&quot;bb&quot;_cc</p>
  5257. ````````````````````````````````
  5258. This is emphasis, even though the opening delimiter is
  5259. both left- and right-flanking, because it is preceded by
  5260. punctuation:
  5261. ```````````````````````````````` example
  5262. foo-_(bar)_
  5263. .
  5264. <p>foo-<em>(bar)</em></p>
  5265. ````````````````````````````````
  5266. Rule 3:
  5267. This is not emphasis, because the closing delimiter does
  5268. not match the opening delimiter:
  5269. ```````````````````````````````` example
  5270. _foo*
  5271. .
  5272. <p>_foo*</p>
  5273. ````````````````````````````````
  5274. This is not emphasis, because the closing `*` is preceded by
  5275. whitespace:
  5276. ```````````````````````````````` example
  5277. *foo bar *
  5278. .
  5279. <p>*foo bar *</p>
  5280. ````````````````````````````````
  5281. A line ending also counts as whitespace:
  5282. ```````````````````````````````` example
  5283. *foo bar
  5284. *
  5285. .
  5286. <p>*foo bar
  5287. *</p>
  5288. ````````````````````````````````
  5289. This is not emphasis, because the second `*` is
  5290. preceded by punctuation and followed by an alphanumeric
  5291. (hence it is not part of a [right-flanking delimiter run]:
  5292. ```````````````````````````````` example
  5293. *(*foo)
  5294. .
  5295. <p>*(*foo)</p>
  5296. ````````````````````````````````
  5297. The point of this restriction is more easily appreciated
  5298. with this example:
  5299. ```````````````````````````````` example
  5300. *(*foo*)*
  5301. .
  5302. <p><em>(<em>foo</em>)</em></p>
  5303. ````````````````````````````````
  5304. Intraword emphasis with `*` is allowed:
  5305. ```````````````````````````````` example
  5306. *foo*bar
  5307. .
  5308. <p><em>foo</em>bar</p>
  5309. ````````````````````````````````
  5310. Rule 4:
  5311. This is not emphasis, because the closing `_` is preceded by
  5312. whitespace:
  5313. ```````````````````````````````` example
  5314. _foo bar _
  5315. .
  5316. <p>_foo bar _</p>
  5317. ````````````````````````````````
  5318. This is not emphasis, because the second `_` is
  5319. preceded by punctuation and followed by an alphanumeric:
  5320. ```````````````````````````````` example
  5321. _(_foo)
  5322. .
  5323. <p>_(_foo)</p>
  5324. ````````````````````````````````
  5325. This is emphasis within emphasis:
  5326. ```````````````````````````````` example
  5327. _(_foo_)_
  5328. .
  5329. <p><em>(<em>foo</em>)</em></p>
  5330. ````````````````````````````````
  5331. Intraword emphasis is disallowed for `_`:
  5332. ```````````````````````````````` example
  5333. _foo_bar
  5334. .
  5335. <p>_foo_bar</p>
  5336. ````````````````````````````````
  5337. ```````````````````````````````` example
  5338. _пристаням_стремятся
  5339. .
  5340. <p>_пристаням_стремятся</p>
  5341. ````````````````````````````````
  5342. ```````````````````````````````` example
  5343. _foo_bar_baz_
  5344. .
  5345. <p><em>foo_bar_baz</em></p>
  5346. ````````````````````````````````
  5347. This is emphasis, even though the closing delimiter is
  5348. both left- and right-flanking, because it is followed by
  5349. punctuation:
  5350. ```````````````````````````````` example
  5351. _(bar)_.
  5352. .
  5353. <p><em>(bar)</em>.</p>
  5354. ````````````````````````````````
  5355. Rule 5:
  5356. ```````````````````````````````` example
  5357. **foo bar**
  5358. .
  5359. <p><strong>foo bar</strong></p>
  5360. ````````````````````````````````
  5361. This is not strong emphasis, because the opening delimiter is
  5362. followed by whitespace:
  5363. ```````````````````````````````` example
  5364. ** foo bar**
  5365. .
  5366. <p>** foo bar**</p>
  5367. ````````````````````````````````
  5368. This is not strong emphasis, because the opening `**` is preceded
  5369. by an alphanumeric and followed by punctuation, and hence
  5370. not part of a [left-flanking delimiter run]:
  5371. ```````````````````````````````` example
  5372. a**"foo"**
  5373. .
  5374. <p>a**&quot;foo&quot;**</p>
  5375. ````````````````````````````````
  5376. Intraword strong emphasis with `**` is permitted:
  5377. ```````````````````````````````` example
  5378. foo**bar**
  5379. .
  5380. <p>foo<strong>bar</strong></p>
  5381. ````````````````````````````````
  5382. Rule 6:
  5383. ```````````````````````````````` example
  5384. __foo bar__
  5385. .
  5386. <p><strong>foo bar</strong></p>
  5387. ````````````````````````````````
  5388. This is not strong emphasis, because the opening delimiter is
  5389. followed by whitespace:
  5390. ```````````````````````````````` example
  5391. __ foo bar__
  5392. .
  5393. <p>__ foo bar__</p>
  5394. ````````````````````````````````
  5395. A line ending counts as whitespace:
  5396. ```````````````````````````````` example
  5397. __
  5398. foo bar__
  5399. .
  5400. <p>__
  5401. foo bar__</p>
  5402. ````````````````````````````````
  5403. This is not strong emphasis, because the opening `__` is preceded
  5404. by an alphanumeric and followed by punctuation:
  5405. ```````````````````````````````` example
  5406. a__"foo"__
  5407. .
  5408. <p>a__&quot;foo&quot;__</p>
  5409. ````````````````````````````````
  5410. Intraword strong emphasis is forbidden with `__`:
  5411. ```````````````````````````````` example
  5412. foo__bar__
  5413. .
  5414. <p>foo__bar__</p>
  5415. ````````````````````````````````
  5416. ```````````````````````````````` example
  5417. 5__6__78
  5418. .
  5419. <p>5__6__78</p>
  5420. ````````````````````````````````
  5421. ```````````````````````````````` example
  5422. пристаням__стремятся__
  5423. .
  5424. <p>пристаням__стремятся__</p>
  5425. ````````````````````````````````
  5426. ```````````````````````````````` example
  5427. __foo, __bar__, baz__
  5428. .
  5429. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5430. ````````````````````````````````
  5431. This is strong emphasis, even though the opening delimiter is
  5432. both left- and right-flanking, because it is preceded by
  5433. punctuation:
  5434. ```````````````````````````````` example
  5435. foo-__(bar)__
  5436. .
  5437. <p>foo-<strong>(bar)</strong></p>
  5438. ````````````````````````````````
  5439. Rule 7:
  5440. This is not strong emphasis, because the closing delimiter is preceded
  5441. by whitespace:
  5442. ```````````````````````````````` example
  5443. **foo bar **
  5444. .
  5445. <p>**foo bar **</p>
  5446. ````````````````````````````````
  5447. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5448. Rule 11.)
  5449. This is not strong emphasis, because the second `**` is
  5450. preceded by punctuation and followed by an alphanumeric:
  5451. ```````````````````````````````` example
  5452. **(**foo)
  5453. .
  5454. <p>**(**foo)</p>
  5455. ````````````````````````````````
  5456. The point of this restriction is more easily appreciated
  5457. with these examples:
  5458. ```````````````````````````````` example
  5459. *(**foo**)*
  5460. .
  5461. <p><em>(<strong>foo</strong>)</em></p>
  5462. ````````````````````````````````
  5463. ```````````````````````````````` example
  5464. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5465. *Asclepias physocarpa*)**
  5466. .
  5467. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5468. <em>Asclepias physocarpa</em>)</strong></p>
  5469. ````````````````````````````````
  5470. ```````````````````````````````` example
  5471. **foo "*bar*" foo**
  5472. .
  5473. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5474. ````````````````````````````````
  5475. Intraword emphasis:
  5476. ```````````````````````````````` example
  5477. **foo**bar
  5478. .
  5479. <p><strong>foo</strong>bar</p>
  5480. ````````````````````````````````
  5481. Rule 8:
  5482. This is not strong emphasis, because the closing delimiter is
  5483. preceded by whitespace:
  5484. ```````````````````````````````` example
  5485. __foo bar __
  5486. .
  5487. <p>__foo bar __</p>
  5488. ````````````````````````````````
  5489. This is not strong emphasis, because the second `__` is
  5490. preceded by punctuation and followed by an alphanumeric:
  5491. ```````````````````````````````` example
  5492. __(__foo)
  5493. .
  5494. <p>__(__foo)</p>
  5495. ````````````````````````````````
  5496. The point of this restriction is more easily appreciated
  5497. with this example:
  5498. ```````````````````````````````` example
  5499. _(__foo__)_
  5500. .
  5501. <p><em>(<strong>foo</strong>)</em></p>
  5502. ````````````````````````````````
  5503. Intraword strong emphasis is forbidden with `__`:
  5504. ```````````````````````````````` example
  5505. __foo__bar
  5506. .
  5507. <p>__foo__bar</p>
  5508. ````````````````````````````````
  5509. ```````````````````````````````` example
  5510. __пристаням__стремятся
  5511. .
  5512. <p>__пристаням__стремятся</p>
  5513. ````````````````````````````````
  5514. ```````````````````````````````` example
  5515. __foo__bar__baz__
  5516. .
  5517. <p><strong>foo__bar__baz</strong></p>
  5518. ````````````````````````````````
  5519. This is strong emphasis, even though the closing delimiter is
  5520. both left- and right-flanking, because it is followed by
  5521. punctuation:
  5522. ```````````````````````````````` example
  5523. __(bar)__.
  5524. .
  5525. <p><strong>(bar)</strong>.</p>
  5526. ````````````````````````````````
  5527. Rule 9:
  5528. Any nonempty sequence of inline elements can be the contents of an
  5529. emphasized span.
  5530. ```````````````````````````````` example
  5531. *foo [bar](/url)*
  5532. .
  5533. <p><em>foo <a href="/url">bar</a></em></p>
  5534. ````````````````````````````````
  5535. ```````````````````````````````` example
  5536. *foo
  5537. bar*
  5538. .
  5539. <p><em>foo
  5540. bar</em></p>
  5541. ````````````````````````````````
  5542. In particular, emphasis and strong emphasis can be nested
  5543. inside emphasis:
  5544. ```````````````````````````````` example
  5545. _foo __bar__ baz_
  5546. .
  5547. <p><em>foo <strong>bar</strong> baz</em></p>
  5548. ````````````````````````````````
  5549. ```````````````````````````````` example
  5550. _foo _bar_ baz_
  5551. .
  5552. <p><em>foo <em>bar</em> baz</em></p>
  5553. ````````````````````````````````
  5554. ```````````````````````````````` example
  5555. __foo_ bar_
  5556. .
  5557. <p><em><em>foo</em> bar</em></p>
  5558. ````````````````````````````````
  5559. ```````````````````````````````` example
  5560. *foo *bar**
  5561. .
  5562. <p><em>foo <em>bar</em></em></p>
  5563. ````````````````````````````````
  5564. ```````````````````````````````` example
  5565. *foo **bar** baz*
  5566. .
  5567. <p><em>foo <strong>bar</strong> baz</em></p>
  5568. ````````````````````````````````
  5569. ```````````````````````````````` example
  5570. *foo**bar**baz*
  5571. .
  5572. <p><em>foo<strong>bar</strong>baz</em></p>
  5573. ````````````````````````````````
  5574. Note that in the preceding case, the interpretation
  5575. ``` markdown
  5576. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5577. ```
  5578. is precluded by the condition that a delimiter that
  5579. can both open and close (like the `*` after `foo`)
  5580. cannot form emphasis if the sum of the lengths of
  5581. the delimiter runs containing the opening and
  5582. closing delimiters is a multiple of 3 unless
  5583. both lengths are multiples of 3.
  5584. For the same reason, we don't get two consecutive
  5585. emphasis sections in this example:
  5586. ```````````````````````````````` example
  5587. *foo**bar*
  5588. .
  5589. <p><em>foo**bar</em></p>
  5590. ````````````````````````````````
  5591. The same condition ensures that the following
  5592. cases are all strong emphasis nested inside
  5593. emphasis, even when the interior whitespace is
  5594. omitted:
  5595. ```````````````````````````````` example
  5596. ***foo** bar*
  5597. .
  5598. <p><em><strong>foo</strong> bar</em></p>
  5599. ````````````````````````````````
  5600. ```````````````````````````````` example
  5601. *foo **bar***
  5602. .
  5603. <p><em>foo <strong>bar</strong></em></p>
  5604. ````````````````````````````````
  5605. ```````````````````````````````` example
  5606. *foo**bar***
  5607. .
  5608. <p><em>foo<strong>bar</strong></em></p>
  5609. ````````````````````````````````
  5610. When the lengths of the interior closing and opening
  5611. delimiter runs are *both* multiples of 3, though,
  5612. they can match to create emphasis:
  5613. ```````````````````````````````` example
  5614. foo***bar***baz
  5615. .
  5616. <p>foo<em><strong>bar</strong></em>baz</p>
  5617. ````````````````````````````````
  5618. ```````````````````````````````` example
  5619. foo******bar*********baz
  5620. .
  5621. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5622. ````````````````````````````````
  5623. Indefinite levels of nesting are possible:
  5624. ```````````````````````````````` example
  5625. *foo **bar *baz* bim** bop*
  5626. .
  5627. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5628. ````````````````````````````````
  5629. ```````````````````````````````` example
  5630. *foo [*bar*](/url)*
  5631. .
  5632. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5633. ````````````````````````````````
  5634. There can be no empty emphasis or strong emphasis:
  5635. ```````````````````````````````` example
  5636. ** is not an empty emphasis
  5637. .
  5638. <p>** is not an empty emphasis</p>
  5639. ````````````````````````````````
  5640. ```````````````````````````````` example
  5641. **** is not an empty strong emphasis
  5642. .
  5643. <p>**** is not an empty strong emphasis</p>
  5644. ````````````````````````````````
  5645. Rule 10:
  5646. Any nonempty sequence of inline elements can be the contents of an
  5647. strongly emphasized span.
  5648. ```````````````````````````````` example
  5649. **foo [bar](/url)**
  5650. .
  5651. <p><strong>foo <a href="/url">bar</a></strong></p>
  5652. ````````````````````````````````
  5653. ```````````````````````````````` example
  5654. **foo
  5655. bar**
  5656. .
  5657. <p><strong>foo
  5658. bar</strong></p>
  5659. ````````````````````````````````
  5660. In particular, emphasis and strong emphasis can be nested
  5661. inside strong emphasis:
  5662. ```````````````````````````````` example
  5663. __foo _bar_ baz__
  5664. .
  5665. <p><strong>foo <em>bar</em> baz</strong></p>
  5666. ````````````````````````````````
  5667. ```````````````````````````````` example
  5668. __foo __bar__ baz__
  5669. .
  5670. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5671. ````````````````````````````````
  5672. ```````````````````````````````` example
  5673. ____foo__ bar__
  5674. .
  5675. <p><strong><strong>foo</strong> bar</strong></p>
  5676. ````````````````````````````````
  5677. ```````````````````````````````` example
  5678. **foo **bar****
  5679. .
  5680. <p><strong>foo <strong>bar</strong></strong></p>
  5681. ````````````````````````````````
  5682. ```````````````````````````````` example
  5683. **foo *bar* baz**
  5684. .
  5685. <p><strong>foo <em>bar</em> baz</strong></p>
  5686. ````````````````````````````````
  5687. ```````````````````````````````` example
  5688. **foo*bar*baz**
  5689. .
  5690. <p><strong>foo<em>bar</em>baz</strong></p>
  5691. ````````````````````````````````
  5692. ```````````````````````````````` example
  5693. ***foo* bar**
  5694. .
  5695. <p><strong><em>foo</em> bar</strong></p>
  5696. ````````````````````````````````
  5697. ```````````````````````````````` example
  5698. **foo *bar***
  5699. .
  5700. <p><strong>foo <em>bar</em></strong></p>
  5701. ````````````````````````````````
  5702. Indefinite levels of nesting are possible:
  5703. ```````````````````````````````` example
  5704. **foo *bar **baz**
  5705. bim* bop**
  5706. .
  5707. <p><strong>foo <em>bar <strong>baz</strong>
  5708. bim</em> bop</strong></p>
  5709. ````````````````````````````````
  5710. ```````````````````````````````` example
  5711. **foo [*bar*](/url)**
  5712. .
  5713. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5714. ````````````````````````````````
  5715. There can be no empty emphasis or strong emphasis:
  5716. ```````````````````````````````` example
  5717. __ is not an empty emphasis
  5718. .
  5719. <p>__ is not an empty emphasis</p>
  5720. ````````````````````````````````
  5721. ```````````````````````````````` example
  5722. ____ is not an empty strong emphasis
  5723. .
  5724. <p>____ is not an empty strong emphasis</p>
  5725. ````````````````````````````````
  5726. Rule 11:
  5727. ```````````````````````````````` example
  5728. foo ***
  5729. .
  5730. <p>foo ***</p>
  5731. ````````````````````````````````
  5732. ```````````````````````````````` example
  5733. foo *\**
  5734. .
  5735. <p>foo <em>*</em></p>
  5736. ````````````````````````````````
  5737. ```````````````````````````````` example
  5738. foo *_*
  5739. .
  5740. <p>foo <em>_</em></p>
  5741. ````````````````````````````````
  5742. ```````````````````````````````` example
  5743. foo *****
  5744. .
  5745. <p>foo *****</p>
  5746. ````````````````````````````````
  5747. ```````````````````````````````` example
  5748. foo **\***
  5749. .
  5750. <p>foo <strong>*</strong></p>
  5751. ````````````````````````````````
  5752. ```````````````````````````````` example
  5753. foo **_**
  5754. .
  5755. <p>foo <strong>_</strong></p>
  5756. ````````````````````````````````
  5757. Note that when delimiters do not match evenly, Rule 11 determines
  5758. that the excess literal `*` characters will appear outside of the
  5759. emphasis, rather than inside it:
  5760. ```````````````````````````````` example
  5761. **foo*
  5762. .
  5763. <p>*<em>foo</em></p>
  5764. ````````````````````````````````
  5765. ```````````````````````````````` example
  5766. *foo**
  5767. .
  5768. <p><em>foo</em>*</p>
  5769. ````````````````````````````````
  5770. ```````````````````````````````` example
  5771. ***foo**
  5772. .
  5773. <p>*<strong>foo</strong></p>
  5774. ````````````````````````````````
  5775. ```````````````````````````````` example
  5776. ****foo*
  5777. .
  5778. <p>***<em>foo</em></p>
  5779. ````````````````````````````````
  5780. ```````````````````````````````` example
  5781. **foo***
  5782. .
  5783. <p><strong>foo</strong>*</p>
  5784. ````````````````````````````````
  5785. ```````````````````````````````` example
  5786. *foo****
  5787. .
  5788. <p><em>foo</em>***</p>
  5789. ````````````````````````````````
  5790. Rule 12:
  5791. ```````````````````````````````` example
  5792. foo ___
  5793. .
  5794. <p>foo ___</p>
  5795. ````````````````````````````````
  5796. ```````````````````````````````` example
  5797. foo _\__
  5798. .
  5799. <p>foo <em>_</em></p>
  5800. ````````````````````````````````
  5801. ```````````````````````````````` example
  5802. foo _*_
  5803. .
  5804. <p>foo <em>*</em></p>
  5805. ````````````````````````````````
  5806. ```````````````````````````````` example
  5807. foo _____
  5808. .
  5809. <p>foo _____</p>
  5810. ````````````````````````````````
  5811. ```````````````````````````````` example
  5812. foo __\___
  5813. .
  5814. <p>foo <strong>_</strong></p>
  5815. ````````````````````````````````
  5816. ```````````````````````````````` example
  5817. foo __*__
  5818. .
  5819. <p>foo <strong>*</strong></p>
  5820. ````````````````````````````````
  5821. ```````````````````````````````` example
  5822. __foo_
  5823. .
  5824. <p>_<em>foo</em></p>
  5825. ````````````````````````````````
  5826. Note that when delimiters do not match evenly, Rule 12 determines
  5827. that the excess literal `_` characters will appear outside of the
  5828. emphasis, rather than inside it:
  5829. ```````````````````````````````` example
  5830. _foo__
  5831. .
  5832. <p><em>foo</em>_</p>
  5833. ````````````````````````````````
  5834. ```````````````````````````````` example
  5835. ___foo__
  5836. .
  5837. <p>_<strong>foo</strong></p>
  5838. ````````````````````````````````
  5839. ```````````````````````````````` example
  5840. ____foo_
  5841. .
  5842. <p>___<em>foo</em></p>
  5843. ````````````````````````````````
  5844. ```````````````````````````````` example
  5845. __foo___
  5846. .
  5847. <p><strong>foo</strong>_</p>
  5848. ````````````````````````````````
  5849. ```````````````````````````````` example
  5850. _foo____
  5851. .
  5852. <p><em>foo</em>___</p>
  5853. ````````````````````````````````
  5854. Rule 13 implies that if you want emphasis nested directly inside
  5855. emphasis, you must use different delimiters:
  5856. ```````````````````````````````` example
  5857. **foo**
  5858. .
  5859. <p><strong>foo</strong></p>
  5860. ````````````````````````````````
  5861. ```````````````````````````````` example
  5862. *_foo_*
  5863. .
  5864. <p><em><em>foo</em></em></p>
  5865. ````````````````````````````````
  5866. ```````````````````````````````` example
  5867. __foo__
  5868. .
  5869. <p><strong>foo</strong></p>
  5870. ````````````````````````````````
  5871. ```````````````````````````````` example
  5872. _*foo*_
  5873. .
  5874. <p><em><em>foo</em></em></p>
  5875. ````````````````````````````````
  5876. However, strong emphasis within strong emphasis is possible without
  5877. switching delimiters:
  5878. ```````````````````````````````` example
  5879. ****foo****
  5880. .
  5881. <p><strong><strong>foo</strong></strong></p>
  5882. ````````````````````````````````
  5883. ```````````````````````````````` example
  5884. ____foo____
  5885. .
  5886. <p><strong><strong>foo</strong></strong></p>
  5887. ````````````````````````````````
  5888. Rule 13 can be applied to arbitrarily long sequences of
  5889. delimiters:
  5890. ```````````````````````````````` example
  5891. ******foo******
  5892. .
  5893. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5894. ````````````````````````````````
  5895. Rule 14:
  5896. ```````````````````````````````` example
  5897. ***foo***
  5898. .
  5899. <p><em><strong>foo</strong></em></p>
  5900. ````````````````````````````````
  5901. ```````````````````````````````` example
  5902. _____foo_____
  5903. .
  5904. <p><em><strong><strong>foo</strong></strong></em></p>
  5905. ````````````````````````````````
  5906. Rule 15:
  5907. ```````````````````````````````` example
  5908. *foo _bar* baz_
  5909. .
  5910. <p><em>foo _bar</em> baz_</p>
  5911. ````````````````````````````````
  5912. ```````````````````````````````` example
  5913. *foo __bar *baz bim__ bam*
  5914. .
  5915. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5916. ````````````````````````````````
  5917. Rule 16:
  5918. ```````````````````````````````` example
  5919. **foo **bar baz**
  5920. .
  5921. <p>**foo <strong>bar baz</strong></p>
  5922. ````````````````````````````````
  5923. ```````````````````````````````` example
  5924. *foo *bar baz*
  5925. .
  5926. <p>*foo <em>bar baz</em></p>
  5927. ````````````````````````````````
  5928. Rule 17:
  5929. ```````````````````````````````` example
  5930. *[bar*](/url)
  5931. .
  5932. <p>*<a href="/url">bar*</a></p>
  5933. ````````````````````````````````
  5934. ```````````````````````````````` example
  5935. _foo [bar_](/url)
  5936. .
  5937. <p>_foo <a href="/url">bar_</a></p>
  5938. ````````````````````````````````
  5939. ```````````````````````````````` example
  5940. *<img src="foo" title="*"/>
  5941. .
  5942. <p>*<img src="foo" title="*"/></p>
  5943. ````````````````````````````````
  5944. ```````````````````````````````` example
  5945. **<a href="**">
  5946. .
  5947. <p>**<a href="**"></p>
  5948. ````````````````````````````````
  5949. ```````````````````````````````` example
  5950. __<a href="__">
  5951. .
  5952. <p>__<a href="__"></p>
  5953. ````````````````````````````````
  5954. ```````````````````````````````` example
  5955. *a `*`*
  5956. .
  5957. <p><em>a <code>*</code></em></p>
  5958. ````````````````````````````````
  5959. ```````````````````````````````` example
  5960. _a `_`_
  5961. .
  5962. <p><em>a <code>_</code></em></p>
  5963. ````````````````````````````````
  5964. ```````````````````````````````` example
  5965. **a<http://foo.bar/?q=**>
  5966. .
  5967. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5968. ````````````````````````````````
  5969. ```````````````````````````````` example
  5970. __a<http://foo.bar/?q=__>
  5971. .
  5972. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5973. ````````````````````````````````
  5974. ## Links
  5975. A link contains [link text] (the visible text), a [link destination]
  5976. (the URI that is the link destination), and optionally a [link title].
  5977. There are two basic kinds of links in Markdown. In [inline links] the
  5978. destination and title are given immediately after the link text. In
  5979. [reference links] the destination and title are defined elsewhere in
  5980. the document.
  5981. A [link text](@) consists of a sequence of zero or more
  5982. inline elements enclosed by square brackets (`[` and `]`). The
  5983. following rules apply:
  5984. - Links may not contain other links, at any level of nesting. If
  5985. multiple otherwise valid link definitions appear nested inside each
  5986. other, the inner-most definition is used.
  5987. - Brackets are allowed in the [link text] only if (a) they
  5988. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5989. with an open bracket `[`, a sequence of zero or more inlines, and
  5990. a close bracket `]`.
  5991. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5992. than the brackets in link text. Thus, for example,
  5993. `` [foo`]` `` could not be a link text, since the second `]`
  5994. is part of a code span.
  5995. - The brackets in link text bind more tightly than markers for
  5996. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5997. A [link destination](@) consists of either
  5998. - a sequence of zero or more characters between an opening `<` and a
  5999. closing `>` that contains no line endings or unescaped
  6000. `<` or `>` characters, or
  6001. - a nonempty sequence of characters that does not start with `<`,
  6002. does not include [ASCII control characters][ASCII control character]
  6003. or [space] character, and includes parentheses only if (a) they are
  6004. backslash-escaped or (b) they are part of a balanced pair of
  6005. unescaped parentheses.
  6006. (Implementations may impose limits on parentheses nesting to
  6007. avoid performance issues, but at least three levels of nesting
  6008. should be supported.)
  6009. A [link title](@) consists of either
  6010. - a sequence of zero or more characters between straight double-quote
  6011. characters (`"`), including a `"` character only if it is
  6012. backslash-escaped, or
  6013. - a sequence of zero or more characters between straight single-quote
  6014. characters (`'`), including a `'` character only if it is
  6015. backslash-escaped, or
  6016. - a sequence of zero or more characters between matching parentheses
  6017. (`(...)`), including a `(` or `)` character only if it is
  6018. backslash-escaped.
  6019. Although [link titles] may span multiple lines, they may not contain
  6020. a [blank line].
  6021. An [inline link](@) consists of a [link text] followed immediately
  6022. by a left parenthesis `(`, an optional [link destination], an optional
  6023. [link title], and a right parenthesis `)`.
  6024. These four components may be separated by spaces, tabs, and up to one line
  6025. ending.
  6026. If both [link destination] and [link title] are present, they *must* be
  6027. separated by spaces, tabs, and up to one line ending.
  6028. The link's text consists of the inlines contained
  6029. in the [link text] (excluding the enclosing square brackets).
  6030. The link's URI consists of the link destination, excluding enclosing
  6031. `<...>` if present, with backslash-escapes in effect as described
  6032. above. The link's title consists of the link title, excluding its
  6033. enclosing delimiters, with backslash-escapes in effect as described
  6034. above.
  6035. Here is a simple inline link:
  6036. ```````````````````````````````` example
  6037. [link](/uri "title")
  6038. .
  6039. <p><a href="/uri" title="title">link</a></p>
  6040. ````````````````````````````````
  6041. The title, the link text and even
  6042. the destination may be omitted:
  6043. ```````````````````````````````` example
  6044. [link](/uri)
  6045. .
  6046. <p><a href="/uri">link</a></p>
  6047. ````````````````````````````````
  6048. ```````````````````````````````` example
  6049. [](./target.md)
  6050. .
  6051. <p><a href="./target.md"></a></p>
  6052. ````````````````````````````````
  6053. ```````````````````````````````` example
  6054. [link]()
  6055. .
  6056. <p><a href="">link</a></p>
  6057. ````````````````````````````````
  6058. ```````````````````````````````` example
  6059. [link](<>)
  6060. .
  6061. <p><a href="">link</a></p>
  6062. ````````````````````````````````
  6063. ```````````````````````````````` example
  6064. []()
  6065. .
  6066. <p><a href=""></a></p>
  6067. ````````````````````````````````
  6068. The destination can only contain spaces if it is
  6069. enclosed in pointy brackets:
  6070. ```````````````````````````````` example
  6071. [link](/my uri)
  6072. .
  6073. <p>[link](/my uri)</p>
  6074. ````````````````````````````````
  6075. ```````````````````````````````` example
  6076. [link](</my uri>)
  6077. .
  6078. <p><a href="/my%20uri">link</a></p>
  6079. ````````````````````````````````
  6080. The destination cannot contain line endings,
  6081. even if enclosed in pointy brackets:
  6082. ```````````````````````````````` example
  6083. [link](foo
  6084. bar)
  6085. .
  6086. <p>[link](foo
  6087. bar)</p>
  6088. ````````````````````````````````
  6089. ```````````````````````````````` example
  6090. [link](<foo
  6091. bar>)
  6092. .
  6093. <p>[link](<foo
  6094. bar>)</p>
  6095. ````````````````````````````````
  6096. The destination can contain `)` if it is enclosed
  6097. in pointy brackets:
  6098. ```````````````````````````````` example
  6099. [a](<b)c>)
  6100. .
  6101. <p><a href="b)c">a</a></p>
  6102. ````````````````````````````````
  6103. Pointy brackets that enclose links must be unescaped:
  6104. ```````````````````````````````` example
  6105. [link](<foo\>)
  6106. .
  6107. <p>[link](&lt;foo&gt;)</p>
  6108. ````````````````````````````````
  6109. These are not links, because the opening pointy bracket
  6110. is not matched properly:
  6111. ```````````````````````````````` example
  6112. [a](<b)c
  6113. [a](<b)c>
  6114. [a](<b>c)
  6115. .
  6116. <p>[a](&lt;b)c
  6117. [a](&lt;b)c&gt;
  6118. [a](<b>c)</p>
  6119. ````````````````````````````````
  6120. Parentheses inside the link destination may be escaped:
  6121. ```````````````````````````````` example
  6122. [link](\(foo\))
  6123. .
  6124. <p><a href="(foo)">link</a></p>
  6125. ````````````````````````````````
  6126. Any number of parentheses are allowed without escaping, as long as they are
  6127. balanced:
  6128. ```````````````````````````````` example
  6129. [link](foo(and(bar)))
  6130. .
  6131. <p><a href="foo(and(bar))">link</a></p>
  6132. ````````````````````````````````
  6133. However, if you have unbalanced parentheses, you need to escape or use the
  6134. `<...>` form:
  6135. ```````````````````````````````` example
  6136. [link](foo(and(bar))
  6137. .
  6138. <p>[link](foo(and(bar))</p>
  6139. ````````````````````````````````
  6140. ```````````````````````````````` example
  6141. [link](foo\(and\(bar\))
  6142. .
  6143. <p><a href="foo(and(bar)">link</a></p>
  6144. ````````````````````````````````
  6145. ```````````````````````````````` example
  6146. [link](<foo(and(bar)>)
  6147. .
  6148. <p><a href="foo(and(bar)">link</a></p>
  6149. ````````````````````````````````
  6150. Parentheses and other symbols can also be escaped, as usual
  6151. in Markdown:
  6152. ```````````````````````````````` example
  6153. [link](foo\)\:)
  6154. .
  6155. <p><a href="foo):">link</a></p>
  6156. ````````````````````````````````
  6157. A link can contain fragment identifiers and queries:
  6158. ```````````````````````````````` example
  6159. [link](#fragment)
  6160. [link](http://example.com#fragment)
  6161. [link](http://example.com?foo=3#frag)
  6162. .
  6163. <p><a href="#fragment">link</a></p>
  6164. <p><a href="http://example.com#fragment">link</a></p>
  6165. <p><a href="http://example.com?foo=3#frag">link</a></p>
  6166. ````````````````````````````````
  6167. Note that a backslash before a non-escapable character is
  6168. just a backslash:
  6169. ```````````````````````````````` example
  6170. [link](foo\bar)
  6171. .
  6172. <p><a href="foo%5Cbar">link</a></p>
  6173. ````````````````````````````````
  6174. URL-escaping should be left alone inside the destination, as all
  6175. URL-escaped characters are also valid URL characters. Entity and
  6176. numerical character references in the destination will be parsed
  6177. into the corresponding Unicode code points, as usual. These may
  6178. be optionally URL-escaped when written as HTML, but this spec
  6179. does not enforce any particular policy for rendering URLs in
  6180. HTML or other formats. Renderers may make different decisions
  6181. about how to escape or normalize URLs in the output.
  6182. ```````````````````````````````` example
  6183. [link](foo%20b&auml;)
  6184. .
  6185. <p><a href="foo%20b%C3%A4">link</a></p>
  6186. ````````````````````````````````
  6187. Note that, because titles can often be parsed as destinations,
  6188. if you try to omit the destination and keep the title, you'll
  6189. get unexpected results:
  6190. ```````````````````````````````` example
  6191. [link]("title")
  6192. .
  6193. <p><a href="%22title%22">link</a></p>
  6194. ````````````````````````````````
  6195. Titles may be in single quotes, double quotes, or parentheses:
  6196. ```````````````````````````````` example
  6197. [link](/url "title")
  6198. [link](/url 'title')
  6199. [link](/url (title))
  6200. .
  6201. <p><a href="/url" title="title">link</a>
  6202. <a href="/url" title="title">link</a>
  6203. <a href="/url" title="title">link</a></p>
  6204. ````````````````````````````````
  6205. Backslash escapes and entity and numeric character references
  6206. may be used in titles:
  6207. ```````````````````````````````` example
  6208. [link](/url "title \"&quot;")
  6209. .
  6210. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  6211. ````````````````````````````````
  6212. Titles must be separated from the link using spaces, tabs, and up to one line
  6213. ending.
  6214. Other [Unicode whitespace] like non-breaking space doesn't work.
  6215. ```````````````````````````````` example
  6216. [link](/url "title")
  6217. .
  6218. <p><a href="/url%C2%A0%22title%22">link</a></p>
  6219. ````````````````````````````````
  6220. Nested balanced quotes are not allowed without escaping:
  6221. ```````````````````````````````` example
  6222. [link](/url "title "and" title")
  6223. .
  6224. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  6225. ````````````````````````````````
  6226. But it is easy to work around this by using a different quote type:
  6227. ```````````````````````````````` example
  6228. [link](/url 'title "and" title')
  6229. .
  6230. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  6231. ````````````````````````````````
  6232. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  6233. title, and its test suite included a test demonstrating this.
  6234. But it is hard to see a good rationale for the extra complexity this
  6235. brings, since there are already many ways---backslash escaping,
  6236. entity and numeric character references, or using a different
  6237. quote type for the enclosing title---to write titles containing
  6238. double quotes. `Markdown.pl`'s handling of titles has a number
  6239. of other strange features. For example, it allows single-quoted
  6240. titles in inline links, but not reference links. And, in
  6241. reference links but not inline links, it allows a title to begin
  6242. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  6243. titles with no closing quotation mark, though 1.0.2b8 does not.
  6244. It seems preferable to adopt a simple, rational rule that works
  6245. the same way in inline links and link reference definitions.)
  6246. Spaces, tabs, and up to one line ending is allowed around the destination and
  6247. title:
  6248. ```````````````````````````````` example
  6249. [link]( /uri
  6250. "title" )
  6251. .
  6252. <p><a href="/uri" title="title">link</a></p>
  6253. ````````````````````````````````
  6254. But it is not allowed between the link text and the
  6255. following parenthesis:
  6256. ```````````````````````````````` example
  6257. [link] (/uri)
  6258. .
  6259. <p>[link] (/uri)</p>
  6260. ````````````````````````````````
  6261. The link text may contain balanced brackets, but not unbalanced ones,
  6262. unless they are escaped:
  6263. ```````````````````````````````` example
  6264. [link [foo [bar]]](/uri)
  6265. .
  6266. <p><a href="/uri">link [foo [bar]]</a></p>
  6267. ````````````````````````````````
  6268. ```````````````````````````````` example
  6269. [link] bar](/uri)
  6270. .
  6271. <p>[link] bar](/uri)</p>
  6272. ````````````````````````````````
  6273. ```````````````````````````````` example
  6274. [link [bar](/uri)
  6275. .
  6276. <p>[link <a href="/uri">bar</a></p>
  6277. ````````````````````````````````
  6278. ```````````````````````````````` example
  6279. [link \[bar](/uri)
  6280. .
  6281. <p><a href="/uri">link [bar</a></p>
  6282. ````````````````````````````````
  6283. The link text may contain inline content:
  6284. ```````````````````````````````` example
  6285. [link *foo **bar** `#`*](/uri)
  6286. .
  6287. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6288. ````````````````````````````````
  6289. ```````````````````````````````` example
  6290. [![moon](moon.jpg)](/uri)
  6291. .
  6292. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6293. ````````````````````````````````
  6294. However, links may not contain other links, at any level of nesting.
  6295. ```````````````````````````````` example
  6296. [foo [bar](/uri)](/uri)
  6297. .
  6298. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  6299. ````````````````````````````````
  6300. ```````````````````````````````` example
  6301. [foo *[bar [baz](/uri)](/uri)*](/uri)
  6302. .
  6303. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  6304. ````````````````````````````````
  6305. ```````````````````````````````` example
  6306. ![[[foo](uri1)](uri2)](uri3)
  6307. .
  6308. <p><img src="uri3" alt="[foo](uri2)" /></p>
  6309. ````````````````````````````````
  6310. These cases illustrate the precedence of link text grouping over
  6311. emphasis grouping:
  6312. ```````````````````````````````` example
  6313. *[foo*](/uri)
  6314. .
  6315. <p>*<a href="/uri">foo*</a></p>
  6316. ````````````````````````````````
  6317. ```````````````````````````````` example
  6318. [foo *bar](baz*)
  6319. .
  6320. <p><a href="baz*">foo *bar</a></p>
  6321. ````````````````````````````````
  6322. Note that brackets that *aren't* part of links do not take
  6323. precedence:
  6324. ```````````````````````````````` example
  6325. *foo [bar* baz]
  6326. .
  6327. <p><em>foo [bar</em> baz]</p>
  6328. ````````````````````````````````
  6329. These cases illustrate the precedence of HTML tags, code spans,
  6330. and autolinks over link grouping:
  6331. ```````````````````````````````` example
  6332. [foo <bar attr="](baz)">
  6333. .
  6334. <p>[foo <bar attr="](baz)"></p>
  6335. ````````````````````````````````
  6336. ```````````````````````````````` example
  6337. [foo`](/uri)`
  6338. .
  6339. <p>[foo<code>](/uri)</code></p>
  6340. ````````````````````````````````
  6341. ```````````````````````````````` example
  6342. [foo<http://example.com/?search=](uri)>
  6343. .
  6344. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  6345. ````````````````````````````````
  6346. There are three kinds of [reference link](@)s:
  6347. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  6348. and [shortcut](#shortcut-reference-link).
  6349. A [full reference link](@)
  6350. consists of a [link text] immediately followed by a [link label]
  6351. that [matches] a [link reference definition] elsewhere in the document.
  6352. A [link label](@) begins with a left bracket (`[`) and ends
  6353. with the first right bracket (`]`) that is not backslash-escaped.
  6354. Between these brackets there must be at least one character that is not a space,
  6355. tab, or line ending.
  6356. Unescaped square bracket characters are not allowed inside the
  6357. opening and closing square brackets of [link labels]. A link
  6358. label can have at most 999 characters inside the square
  6359. brackets.
  6360. One label [matches](@)
  6361. another just in case their normalized forms are equal. To normalize a
  6362. label, strip off the opening and closing brackets,
  6363. perform the *Unicode case fold*, strip leading and trailing
  6364. spaces, tabs, and line endings, and collapse consecutive internal
  6365. spaces, tabs, and line endings to a single space. If there are multiple
  6366. matching reference link definitions, the one that comes first in the
  6367. document is used. (It is desirable in such cases to emit a warning.)
  6368. The link's URI and title are provided by the matching [link
  6369. reference definition].
  6370. Here is a simple example:
  6371. ```````````````````````````````` example
  6372. [foo][bar]
  6373. [bar]: /url "title"
  6374. .
  6375. <p><a href="/url" title="title">foo</a></p>
  6376. ````````````````````````````````
  6377. The rules for the [link text] are the same as with
  6378. [inline links]. Thus:
  6379. The link text may contain balanced brackets, but not unbalanced ones,
  6380. unless they are escaped:
  6381. ```````````````````````````````` example
  6382. [link [foo [bar]]][ref]
  6383. [ref]: /uri
  6384. .
  6385. <p><a href="/uri">link [foo [bar]]</a></p>
  6386. ````````````````````````````````
  6387. ```````````````````````````````` example
  6388. [link \[bar][ref]
  6389. [ref]: /uri
  6390. .
  6391. <p><a href="/uri">link [bar</a></p>
  6392. ````````````````````````````````
  6393. The link text may contain inline content:
  6394. ```````````````````````````````` example
  6395. [link *foo **bar** `#`*][ref]
  6396. [ref]: /uri
  6397. .
  6398. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6399. ````````````````````````````````
  6400. ```````````````````````````````` example
  6401. [![moon](moon.jpg)][ref]
  6402. [ref]: /uri
  6403. .
  6404. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6405. ````````````````````````````````
  6406. However, links may not contain other links, at any level of nesting.
  6407. ```````````````````````````````` example
  6408. [foo [bar](/uri)][ref]
  6409. [ref]: /uri
  6410. .
  6411. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  6412. ````````````````````````````````
  6413. ```````````````````````````````` example
  6414. [foo *bar [baz][ref]*][ref]
  6415. [ref]: /uri
  6416. .
  6417. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  6418. ````````````````````````````````
  6419. (In the examples above, we have two [shortcut reference links]
  6420. instead of one [full reference link].)
  6421. The following cases illustrate the precedence of link text grouping over
  6422. emphasis grouping:
  6423. ```````````````````````````````` example
  6424. *[foo*][ref]
  6425. [ref]: /uri
  6426. .
  6427. <p>*<a href="/uri">foo*</a></p>
  6428. ````````````````````````````````
  6429. ```````````````````````````````` example
  6430. [foo *bar][ref]*
  6431. [ref]: /uri
  6432. .
  6433. <p><a href="/uri">foo *bar</a>*</p>
  6434. ````````````````````````````````
  6435. These cases illustrate the precedence of HTML tags, code spans,
  6436. and autolinks over link grouping:
  6437. ```````````````````````````````` example
  6438. [foo <bar attr="][ref]">
  6439. [ref]: /uri
  6440. .
  6441. <p>[foo <bar attr="][ref]"></p>
  6442. ````````````````````````````````
  6443. ```````````````````````````````` example
  6444. [foo`][ref]`
  6445. [ref]: /uri
  6446. .
  6447. <p>[foo<code>][ref]</code></p>
  6448. ````````````````````````````````
  6449. ```````````````````````````````` example
  6450. [foo<http://example.com/?search=][ref]>
  6451. [ref]: /uri
  6452. .
  6453. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6454. ````````````````````````````````
  6455. Matching is case-insensitive:
  6456. ```````````````````````````````` example
  6457. [foo][BaR]
  6458. [bar]: /url "title"
  6459. .
  6460. <p><a href="/url" title="title">foo</a></p>
  6461. ````````````````````````````````
  6462. Unicode case fold is used:
  6463. ```````````````````````````````` example
  6464. [ẞ]
  6465. [SS]: /url
  6466. .
  6467. <p><a href="/url">ẞ</a></p>
  6468. ````````````````````````````````
  6469. Consecutive internal spaces, tabs, and line endings are treated as one space for
  6470. purposes of determining matching:
  6471. ```````````````````````````````` example
  6472. [Foo
  6473. bar]: /url
  6474. [Baz][Foo bar]
  6475. .
  6476. <p><a href="/url">Baz</a></p>
  6477. ````````````````````````````````
  6478. No spaces, tabs, or line endings are allowed between the [link text] and the
  6479. [link label]:
  6480. ```````````````````````````````` example
  6481. [foo] [bar]
  6482. [bar]: /url "title"
  6483. .
  6484. <p>[foo] <a href="/url" title="title">bar</a></p>
  6485. ````````````````````````````````
  6486. ```````````````````````````````` example
  6487. [foo]
  6488. [bar]
  6489. [bar]: /url "title"
  6490. .
  6491. <p>[foo]
  6492. <a href="/url" title="title">bar</a></p>
  6493. ````````````````````````````````
  6494. This is a departure from John Gruber's original Markdown syntax
  6495. description, which explicitly allows whitespace between the link
  6496. text and the link label. It brings reference links in line with
  6497. [inline links], which (according to both original Markdown and
  6498. this spec) cannot have whitespace after the link text. More
  6499. importantly, it prevents inadvertent capture of consecutive
  6500. [shortcut reference links]. If whitespace is allowed between the
  6501. link text and the link label, then in the following we will have
  6502. a single reference link, not two shortcut reference links, as
  6503. intended:
  6504. ``` markdown
  6505. [foo]
  6506. [bar]
  6507. [foo]: /url1
  6508. [bar]: /url2
  6509. ```
  6510. (Note that [shortcut reference links] were introduced by Gruber
  6511. himself in a beta version of `Markdown.pl`, but never included
  6512. in the official syntax description. Without shortcut reference
  6513. links, it is harmless to allow space between the link text and
  6514. link label; but once shortcut references are introduced, it is
  6515. too dangerous to allow this, as it frequently leads to
  6516. unintended results.)
  6517. When there are multiple matching [link reference definitions],
  6518. the first is used:
  6519. ```````````````````````````````` example
  6520. [foo]: /url1
  6521. [foo]: /url2
  6522. [bar][foo]
  6523. .
  6524. <p><a href="/url1">bar</a></p>
  6525. ````````````````````````````````
  6526. Note that matching is performed on normalized strings, not parsed
  6527. inline content. So the following does not match, even though the
  6528. labels define equivalent inline content:
  6529. ```````````````````````````````` example
  6530. [bar][foo\!]
  6531. [foo!]: /url
  6532. .
  6533. <p>[bar][foo!]</p>
  6534. ````````````````````````````````
  6535. [Link labels] cannot contain brackets, unless they are
  6536. backslash-escaped:
  6537. ```````````````````````````````` example
  6538. [foo][ref[]
  6539. [ref[]: /uri
  6540. .
  6541. <p>[foo][ref[]</p>
  6542. <p>[ref[]: /uri</p>
  6543. ````````````````````````````````
  6544. ```````````````````````````````` example
  6545. [foo][ref[bar]]
  6546. [ref[bar]]: /uri
  6547. .
  6548. <p>[foo][ref[bar]]</p>
  6549. <p>[ref[bar]]: /uri</p>
  6550. ````````````````````````````````
  6551. ```````````````````````````````` example
  6552. [[[foo]]]
  6553. [[[foo]]]: /url
  6554. .
  6555. <p>[[[foo]]]</p>
  6556. <p>[[[foo]]]: /url</p>
  6557. ````````````````````````````````
  6558. ```````````````````````````````` example
  6559. [foo][ref\[]
  6560. [ref\[]: /uri
  6561. .
  6562. <p><a href="/uri">foo</a></p>
  6563. ````````````````````````````````
  6564. Note that in this example `]` is not backslash-escaped:
  6565. ```````````````````````````````` example
  6566. [bar\\]: /uri
  6567. [bar\\]
  6568. .
  6569. <p><a href="/uri">bar\</a></p>
  6570. ````````````````````````````````
  6571. A [link label] must contain at least one character that is not a space, tab, or
  6572. line ending:
  6573. ```````````````````````````````` example
  6574. []
  6575. []: /uri
  6576. .
  6577. <p>[]</p>
  6578. <p>[]: /uri</p>
  6579. ````````````````````````````````
  6580. ```````````````````````````````` example
  6581. [
  6582. ]
  6583. [
  6584. ]: /uri
  6585. .
  6586. <p>[
  6587. ]</p>
  6588. <p>[
  6589. ]: /uri</p>
  6590. ````````````````````````````````
  6591. A [collapsed reference link](@)
  6592. consists of a [link label] that [matches] a
  6593. [link reference definition] elsewhere in the
  6594. document, followed by the string `[]`.
  6595. The contents of the first link label are parsed as inlines,
  6596. which are used as the link's text. The link's URI and title are
  6597. provided by the matching reference link definition. Thus,
  6598. `[foo][]` is equivalent to `[foo][foo]`.
  6599. ```````````````````````````````` example
  6600. [foo][]
  6601. [foo]: /url "title"
  6602. .
  6603. <p><a href="/url" title="title">foo</a></p>
  6604. ````````````````````````````````
  6605. ```````````````````````````````` example
  6606. [*foo* bar][]
  6607. [*foo* bar]: /url "title"
  6608. .
  6609. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6610. ````````````````````````````````
  6611. The link labels are case-insensitive:
  6612. ```````````````````````````````` example
  6613. [Foo][]
  6614. [foo]: /url "title"
  6615. .
  6616. <p><a href="/url" title="title">Foo</a></p>
  6617. ````````````````````````````````
  6618. As with full reference links, spaces, tabs, or line endings are not
  6619. allowed between the two sets of brackets:
  6620. ```````````````````````````````` example
  6621. [foo]
  6622. []
  6623. [foo]: /url "title"
  6624. .
  6625. <p><a href="/url" title="title">foo</a>
  6626. []</p>
  6627. ````````````````````````````````
  6628. A [shortcut reference link](@)
  6629. consists of a [link label] that [matches] a
  6630. [link reference definition] elsewhere in the
  6631. document and is not followed by `[]` or a link label.
  6632. The contents of the first link label are parsed as inlines,
  6633. which are used as the link's text. The link's URI and title
  6634. are provided by the matching link reference definition.
  6635. Thus, `[foo]` is equivalent to `[foo][]`.
  6636. ```````````````````````````````` example
  6637. [foo]
  6638. [foo]: /url "title"
  6639. .
  6640. <p><a href="/url" title="title">foo</a></p>
  6641. ````````````````````````````````
  6642. ```````````````````````````````` example
  6643. [*foo* bar]
  6644. [*foo* bar]: /url "title"
  6645. .
  6646. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6647. ````````````````````````````````
  6648. ```````````````````````````````` example
  6649. [[*foo* bar]]
  6650. [*foo* bar]: /url "title"
  6651. .
  6652. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6653. ````````````````````````````````
  6654. ```````````````````````````````` example
  6655. [[bar [foo]
  6656. [foo]: /url
  6657. .
  6658. <p>[[bar <a href="/url">foo</a></p>
  6659. ````````````````````````````````
  6660. The link labels are case-insensitive:
  6661. ```````````````````````````````` example
  6662. [Foo]
  6663. [foo]: /url "title"
  6664. .
  6665. <p><a href="/url" title="title">Foo</a></p>
  6666. ````````````````````````````````
  6667. A space after the link text should be preserved:
  6668. ```````````````````````````````` example
  6669. [foo] bar
  6670. [foo]: /url
  6671. .
  6672. <p><a href="/url">foo</a> bar</p>
  6673. ````````````````````````````````
  6674. If you just want bracketed text, you can backslash-escape the
  6675. opening bracket to avoid links:
  6676. ```````````````````````````````` example
  6677. \[foo]
  6678. [foo]: /url "title"
  6679. .
  6680. <p>[foo]</p>
  6681. ````````````````````````````````
  6682. Note that this is a link, because a link label ends with the first
  6683. following closing bracket:
  6684. ```````````````````````````````` example
  6685. [foo*]: /url
  6686. *[foo*]
  6687. .
  6688. <p>*<a href="/url">foo*</a></p>
  6689. ````````````````````````````````
  6690. Full and compact references take precedence over shortcut
  6691. references:
  6692. ```````````````````````````````` example
  6693. [foo][bar]
  6694. [foo]: /url1
  6695. [bar]: /url2
  6696. .
  6697. <p><a href="/url2">foo</a></p>
  6698. ````````````````````````````````
  6699. ```````````````````````````````` example
  6700. [foo][]
  6701. [foo]: /url1
  6702. .
  6703. <p><a href="/url1">foo</a></p>
  6704. ````````````````````````````````
  6705. Inline links also take precedence:
  6706. ```````````````````````````````` example
  6707. [foo]()
  6708. [foo]: /url1
  6709. .
  6710. <p><a href="">foo</a></p>
  6711. ````````````````````````````````
  6712. ```````````````````````````````` example
  6713. [foo](not a link)
  6714. [foo]: /url1
  6715. .
  6716. <p><a href="/url1">foo</a>(not a link)</p>
  6717. ````````````````````````````````
  6718. In the following case `[bar][baz]` is parsed as a reference,
  6719. `[foo]` as normal text:
  6720. ```````````````````````````````` example
  6721. [foo][bar][baz]
  6722. [baz]: /url
  6723. .
  6724. <p>[foo]<a href="/url">bar</a></p>
  6725. ````````````````````````````````
  6726. Here, though, `[foo][bar]` is parsed as a reference, since
  6727. `[bar]` is defined:
  6728. ```````````````````````````````` example
  6729. [foo][bar][baz]
  6730. [baz]: /url1
  6731. [bar]: /url2
  6732. .
  6733. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6734. ````````````````````````````````
  6735. Here `[foo]` is not parsed as a shortcut reference, because it
  6736. is followed by a link label (even though `[bar]` is not defined):
  6737. ```````````````````````````````` example
  6738. [foo][bar][baz]
  6739. [baz]: /url1
  6740. [foo]: /url2
  6741. .
  6742. <p>[foo]<a href="/url1">bar</a></p>
  6743. ````````````````````````````````
  6744. ## Annotations
  6745. An annotation contains [annotation text] (the visible text), and an [annotation set]
  6746. (the hints to annotate the text with).
  6747. There are six basic kinds of annotations in Markdown.
  6748. In [block annotations] the hints are given at the end of the block.
  6749. In [block-tree annotations] the hints are given
  6750. at the beginning of the topmost block.
  6751. In [block-siblings annotations] the hints are given
  6752. as a prototype sibling block immediately before the first sibling block.
  6753. In [block-cluster annotations] the hints are given
  6754. as a prototype parent block immediately before the first block in the cluster.
  6755. In [inline annotations] the hints are given
  6756. immediately after the link text.
  6757. In referenced annotations the hints are defined
  6758. in a [reference link] elsewhere in the document.
  6759. An [annotation text](@) consists of either
  6760. - a single explicitly enclosed inline element,
  6761. i.e. a [code span],
  6762. [emphasis or strong emphasis][emphasis and strong emphasis],
  6763. [link], [image], or [autolink], or
  6764. - a sequence of zero or more inline elements
  6765. enclosed by square brackets (`[` and `]`).
  6766. The following rules apply:
  6767. - Annotations may not contain other annotations, at any level of nesting.
  6768. If multiple otherwise valid annotation definitions appear nested inside each
  6769. other, the inner-most definition is used.
  6770. - Brackets are allowed in the [annotation text] only if (a) they
  6771. are backslash-escaped or (b) they appear as a matched pair of brackets,
  6772. with an open bracket `[`, a sequence of zero or more inlines, and
  6773. a close bracket `]`.
  6774. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  6775. than the brackets in annotation text. Thus, for example,
  6776. `` [foo`]` `` could not be an annotation text, since the second `]`
  6777. is part of a code span.
  6778. - The brackets in annotation text bind more tightly than markers for
  6779. [emphasis and strong emphasis]. Thus, for example, `*[foo*]{hint}` is a link.
  6780. FIXME: define priority of link versus annotation text if necessary.
  6781. An [annotation set](@) consists of a an opening `{`,
  6782. followed by optional spaces or tabs (including up to one [line ending]),
  6783. zero or more [annotation hints]
  6784. delimited by spaces or tabs (including up to one [line ending]),
  6785. optional spaces or tabs (including up to one [line ending]),
  6786. and a closing `}`.
  6787. There are 3 types of [annotation hints][@] in Markdown.
  6788. [typeof hints], [property hints], and [resource hints].
  6789. A [typeof hint](@) consists of character `.`,
  6790. then an [annotation destination].
  6791. A [property hint](@) consists of an [annotation destination].
  6792. A [resource hint](@) consists of character `=`,
  6793. then an [annotation destination].
  6794. An [annotation destination](@) consists of either
  6795. - a sequence of zero or more characters between an opening `<` and a
  6796. closing `>` that contains no line endings or unescaped
  6797. `<` or `>` characters, or
  6798. - a nonempty sequence of characters that does not start with `<`,
  6799. does not include [ASCII control characters][ASCII control character]
  6800. or [space] character, and includes parentheses only if (a) they are
  6801. backslash-escaped or (b) they are part of a balanced pair of
  6802. unescaped parentheses.
  6803. (Implementations may impose limits on parentheses nesting to
  6804. avoid performance issues, but at least three levels of nesting
  6805. should be supported.)
  6806. FIXME: tighten [annotation destination] to cover only <...> and CURIEs.
  6807. FIXME: define [block annotation].
  6808. FIXME: define [block-tree annotation].
  6809. FIXME: define [block-siblings annotation].
  6810. FIXME: define [block-cluster annotation].
  6811. An [inline annotation](@) consists of an [annotation text]
  6812. followed immediately by an [annotation set].
  6813. The annotation consists of HTML tag attributes
  6814. with the type of [annotation hint] as key.
  6815. The annotation attribute's value is the [annotation destination],
  6816. excluding enclosing `<...>` if present.
  6817. FIXME: define addition of prefix attributes
  6818. when deviating from [annotation initial context].
  6819. FIXME: define addition of vocab attribute
  6820. when declared in [annotation reference definition].
  6821. FIXME: define normalization of [annotation destination] into CURIE
  6822. based on [annotation initial context]
  6823. and prefixes in [annotation reference definitions].
  6824. ## Images
  6825. Syntax for images is like the syntax for links, with one
  6826. difference. Instead of [link text], we have an
  6827. [image description](@). The rules for this are the
  6828. same as for [link text], except that (a) an
  6829. image description starts with `![` rather than `[`, and
  6830. (b) an image description may contain links.
  6831. An image description has inline elements
  6832. as its contents. When an image is rendered to HTML,
  6833. this is standardly used as the image's `alt` attribute.
  6834. ```````````````````````````````` example
  6835. ![foo](/url "title")
  6836. .
  6837. <p><img src="/url" alt="foo" title="title" /></p>
  6838. ````````````````````````````````
  6839. ```````````````````````````````` example
  6840. ![foo *bar*]
  6841. [foo *bar*]: train.jpg "train & tracks"
  6842. .
  6843. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6844. ````````````````````````````````
  6845. ```````````````````````````````` example
  6846. ![foo ![bar](/url)](/url2)
  6847. .
  6848. <p><img src="/url2" alt="foo bar" /></p>
  6849. ````````````````````````````````
  6850. ```````````````````````````````` example
  6851. ![foo [bar](/url)](/url2)
  6852. .
  6853. <p><img src="/url2" alt="foo bar" /></p>
  6854. ````````````````````````````````
  6855. Though this spec is concerned with parsing, not rendering, it is
  6856. recommended that in rendering to HTML, only the plain string content
  6857. of the [image description] be used. Note that in
  6858. the above example, the alt attribute's value is `foo bar`, not `foo
  6859. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6860. content is rendered, without formatting.
  6861. ```````````````````````````````` example
  6862. ![foo *bar*][]
  6863. [foo *bar*]: train.jpg "train & tracks"
  6864. .
  6865. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6866. ````````````````````````````````
  6867. ```````````````````````````````` example
  6868. ![foo *bar*][foobar]
  6869. [FOOBAR]: train.jpg "train & tracks"
  6870. .
  6871. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6872. ````````````````````````````````
  6873. ```````````````````````````````` example
  6874. ![foo](train.jpg)
  6875. .
  6876. <p><img src="train.jpg" alt="foo" /></p>
  6877. ````````````````````````````````
  6878. ```````````````````````````````` example
  6879. My ![foo bar](/path/to/train.jpg "title" )
  6880. .
  6881. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6882. ````````````````````````````````
  6883. ```````````````````````````````` example
  6884. ![foo](<url>)
  6885. .
  6886. <p><img src="url" alt="foo" /></p>
  6887. ````````````````````````````````
  6888. ```````````````````````````````` example
  6889. ![](/url)
  6890. .
  6891. <p><img src="/url" alt="" /></p>
  6892. ````````````````````````````````
  6893. Reference-style:
  6894. ```````````````````````````````` example
  6895. ![foo][bar]
  6896. [bar]: /url
  6897. .
  6898. <p><img src="/url" alt="foo" /></p>
  6899. ````````````````````````````````
  6900. ```````````````````````````````` example
  6901. ![foo][bar]
  6902. [BAR]: /url
  6903. .
  6904. <p><img src="/url" alt="foo" /></p>
  6905. ````````````````````````````````
  6906. Collapsed:
  6907. ```````````````````````````````` example
  6908. ![foo][]
  6909. [foo]: /url "title"
  6910. .
  6911. <p><img src="/url" alt="foo" title="title" /></p>
  6912. ````````````````````````````````
  6913. ```````````````````````````````` example
  6914. ![*foo* bar][]
  6915. [*foo* bar]: /url "title"
  6916. .
  6917. <p><img src="/url" alt="foo bar" title="title" /></p>
  6918. ````````````````````````````````
  6919. The labels are case-insensitive:
  6920. ```````````````````````````````` example
  6921. ![Foo][]
  6922. [foo]: /url "title"
  6923. .
  6924. <p><img src="/url" alt="Foo" title="title" /></p>
  6925. ````````````````````````````````
  6926. As with reference links, spaces, tabs, and line endings, are not allowed
  6927. between the two sets of brackets:
  6928. ```````````````````````````````` example
  6929. ![foo]
  6930. []
  6931. [foo]: /url "title"
  6932. .
  6933. <p><img src="/url" alt="foo" title="title" />
  6934. []</p>
  6935. ````````````````````````````````
  6936. Shortcut:
  6937. ```````````````````````````````` example
  6938. ![foo]
  6939. [foo]: /url "title"
  6940. .
  6941. <p><img src="/url" alt="foo" title="title" /></p>
  6942. ````````````````````````````````
  6943. ```````````````````````````````` example
  6944. ![*foo* bar]
  6945. [*foo* bar]: /url "title"
  6946. .
  6947. <p><img src="/url" alt="foo bar" title="title" /></p>
  6948. ````````````````````````````````
  6949. Note that link labels cannot contain unescaped brackets:
  6950. ```````````````````````````````` example
  6951. ![[foo]]
  6952. [[foo]]: /url "title"
  6953. .
  6954. <p>![[foo]]</p>
  6955. <p>[[foo]]: /url &quot;title&quot;</p>
  6956. ````````````````````````````````
  6957. The link labels are case-insensitive:
  6958. ```````````````````````````````` example
  6959. ![Foo]
  6960. [foo]: /url "title"
  6961. .
  6962. <p><img src="/url" alt="Foo" title="title" /></p>
  6963. ````````````````````````````````
  6964. If you just want a literal `!` followed by bracketed text, you can
  6965. backslash-escape the opening `[`:
  6966. ```````````````````````````````` example
  6967. !\[foo]
  6968. [foo]: /url "title"
  6969. .
  6970. <p>![foo]</p>
  6971. ````````````````````````````````
  6972. If you want a link after a literal `!`, backslash-escape the
  6973. `!`:
  6974. ```````````````````````````````` example
  6975. \![foo]
  6976. [foo]: /url "title"
  6977. .
  6978. <p>!<a href="/url" title="title">foo</a></p>
  6979. ````````````````````````````````
  6980. ## Autolinks
  6981. [Autolink](@)s are absolute URIs and email addresses inside
  6982. `<` and `>`. They are parsed as links, with the URL or email address
  6983. as the link label.
  6984. A [URI autolink](@) consists of `<`, followed by an
  6985. [absolute URI] followed by `>`. It is parsed as
  6986. a link to the URI, with the URI as the link's label.
  6987. An [absolute URI](@),
  6988. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6989. followed by zero or more characters other [ASCII control
  6990. characters][ASCII control character], [space], `<`, and `>`.
  6991. If the URI includes these characters, they must be percent-encoded
  6992. (e.g. `%20` for a space).
  6993. For purposes of this spec, a [scheme](@) is any sequence
  6994. of 2--32 characters beginning with an ASCII letter and followed
  6995. by any combination of ASCII letters, digits, or the symbols plus
  6996. ("+"), period ("."), or hyphen ("-").
  6997. Here are some valid autolinks:
  6998. ```````````````````````````````` example
  6999. <http://foo.bar.baz>
  7000. .
  7001. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  7002. ````````````````````````````````
  7003. ```````````````````````````````` example
  7004. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  7005. .
  7006. <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>
  7007. ````````````````````````````````
  7008. ```````````````````````````````` example
  7009. <irc://foo.bar:2233/baz>
  7010. .
  7011. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  7012. ````````````````````````````````
  7013. Uppercase is also fine:
  7014. ```````````````````````````````` example
  7015. <MAILTO:FOO@BAR.BAZ>
  7016. .
  7017. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  7018. ````````````````````````````````
  7019. Note that many strings that count as [absolute URIs] for
  7020. purposes of this spec are not valid URIs, because their
  7021. schemes are not registered or because of other problems
  7022. with their syntax:
  7023. ```````````````````````````````` example
  7024. <a+b+c:d>
  7025. .
  7026. <p><a href="a+b+c:d">a+b+c:d</a></p>
  7027. ````````````````````````````````
  7028. ```````````````````````````````` example
  7029. <made-up-scheme://foo,bar>
  7030. .
  7031. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  7032. ````````````````````````````````
  7033. ```````````````````````````````` example
  7034. <http://../>
  7035. .
  7036. <p><a href="http://../">http://../</a></p>
  7037. ````````````````````````````````
  7038. ```````````````````````````````` example
  7039. <localhost:5001/foo>
  7040. .
  7041. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  7042. ````````````````````````````````
  7043. Spaces are not allowed in autolinks:
  7044. ```````````````````````````````` example
  7045. <http://foo.bar/baz bim>
  7046. .
  7047. <p>&lt;http://foo.bar/baz bim&gt;</p>
  7048. ````````````````````````````````
  7049. Backslash-escapes do not work inside autolinks:
  7050. ```````````````````````````````` example
  7051. <http://example.com/\[\>
  7052. .
  7053. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  7054. ````````````````````````````````
  7055. An [email autolink](@)
  7056. consists of `<`, followed by an [email address],
  7057. followed by `>`. The link's label is the email address,
  7058. and the URL is `mailto:` followed by the email address.
  7059. An [email address](@),
  7060. for these purposes, is anything that matches
  7061. the [non-normative regex from the HTML5
  7062. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  7063. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  7064. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  7065. Examples of email autolinks:
  7066. ```````````````````````````````` example
  7067. <foo@bar.example.com>
  7068. .
  7069. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  7070. ````````````````````````````````
  7071. ```````````````````````````````` example
  7072. <foo+special@Bar.baz-bar0.com>
  7073. .
  7074. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  7075. ````````````````````````````````
  7076. Backslash-escapes do not work inside email autolinks:
  7077. ```````````````````````````````` example
  7078. <foo\+@bar.example.com>
  7079. .
  7080. <p>&lt;foo+@bar.example.com&gt;</p>
  7081. ````````````````````````````````
  7082. These are not autolinks:
  7083. ```````````````````````````````` example
  7084. <>
  7085. .
  7086. <p>&lt;&gt;</p>
  7087. ````````````````````````````````
  7088. ```````````````````````````````` example
  7089. < http://foo.bar >
  7090. .
  7091. <p>&lt; http://foo.bar &gt;</p>
  7092. ````````````````````````````````
  7093. ```````````````````````````````` example
  7094. <m:abc>
  7095. .
  7096. <p>&lt;m:abc&gt;</p>
  7097. ````````````````````````````````
  7098. ```````````````````````````````` example
  7099. <foo.bar.baz>
  7100. .
  7101. <p>&lt;foo.bar.baz&gt;</p>
  7102. ````````````````````````````````
  7103. ```````````````````````````````` example
  7104. http://example.com
  7105. .
  7106. <p>http://example.com</p>
  7107. ````````````````````````````````
  7108. ```````````````````````````````` example
  7109. foo@bar.example.com
  7110. .
  7111. <p>foo@bar.example.com</p>
  7112. ````````````````````````````````
  7113. ## Raw HTML
  7114. Text between `<` and `>` that looks like an HTML tag is parsed as a
  7115. raw HTML tag and will be rendered in HTML without escaping.
  7116. Tag and attribute names are not limited to current HTML tags,
  7117. so custom tags (and even, say, DocBook tags) may be used.
  7118. Here is the grammar for tags:
  7119. A [tag name](@) consists of an ASCII letter
  7120. followed by zero or more ASCII letters, digits, or
  7121. hyphens (`-`).
  7122. An [attribute](@) consists of spaces, tabs, and up to one line ending,
  7123. an [attribute name], and an optional
  7124. [attribute value specification].
  7125. An [attribute name](@)
  7126. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  7127. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  7128. specification restricted to ASCII. HTML5 is laxer.)
  7129. An [attribute value specification](@)
  7130. consists of optional spaces, tabs, and up to one line ending,
  7131. a `=` character, optional spaces, tabs, and up to one line ending,
  7132. and an [attribute value].
  7133. An [attribute value](@)
  7134. consists of an [unquoted attribute value],
  7135. a [single-quoted attribute value], or a [double-quoted attribute value].
  7136. An [unquoted attribute value](@)
  7137. is a nonempty string of characters not
  7138. including spaces, tabs, line endings, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  7139. A [single-quoted attribute value](@)
  7140. consists of `'`, zero or more
  7141. characters not including `'`, and a final `'`.
  7142. A [double-quoted attribute value](@)
  7143. consists of `"`, zero or more
  7144. characters not including `"`, and a final `"`.
  7145. An [open tag](@) consists of a `<` character, a [tag name],
  7146. zero or more [attributes], optional spaces, tabs, and up to one line ending,
  7147. an optional `/` character, and a `>` character.
  7148. A [closing tag](@) consists of the string `</`, a
  7149. [tag name], optional spaces, tabs, and up to one line ending, and the character
  7150. `>`.
  7151. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  7152. where *text* does not start with `>` or `->`, does not end with `-`,
  7153. and does not contain `--`. (See the
  7154. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  7155. A [processing instruction](@)
  7156. consists of the string `<?`, a string
  7157. of characters not including the string `?>`, and the string
  7158. `?>`.
  7159. A [declaration](@) consists of the string `<!`, an ASCII letter, zero or more
  7160. characters not including the character `>`, and the character `>`.
  7161. A [CDATA section](@) consists of
  7162. the string `<![CDATA[`, a string of characters not including the string
  7163. `]]>`, and the string `]]>`.
  7164. An [HTML tag](@) consists of an [open tag], a [closing tag],
  7165. an [HTML comment], a [processing instruction], a [declaration],
  7166. or a [CDATA section].
  7167. Here are some simple open tags:
  7168. ```````````````````````````````` example
  7169. <a><bab><c2c>
  7170. .
  7171. <p><a><bab><c2c></p>
  7172. ````````````````````````````````
  7173. Empty elements:
  7174. ```````````````````````````````` example
  7175. <a/><b2/>
  7176. .
  7177. <p><a/><b2/></p>
  7178. ````````````````````````````````
  7179. Whitespace is allowed:
  7180. ```````````````````````````````` example
  7181. <a /><b2
  7182. data="foo" >
  7183. .
  7184. <p><a /><b2
  7185. data="foo" ></p>
  7186. ````````````````````````````````
  7187. With attributes:
  7188. ```````````````````````````````` example
  7189. <a foo="bar" bam = 'baz <em>"</em>'
  7190. _boolean zoop:33=zoop:33 />
  7191. .
  7192. <p><a foo="bar" bam = 'baz <em>"</em>'
  7193. _boolean zoop:33=zoop:33 /></p>
  7194. ````````````````````````````````
  7195. Custom tag names can be used:
  7196. ```````````````````````````````` example
  7197. Foo <responsive-image src="foo.jpg" />
  7198. .
  7199. <p>Foo <responsive-image src="foo.jpg" /></p>
  7200. ````````````````````````````````
  7201. Illegal tag names, not parsed as HTML:
  7202. ```````````````````````````````` example
  7203. <33> <__>
  7204. .
  7205. <p>&lt;33&gt; &lt;__&gt;</p>
  7206. ````````````````````````````````
  7207. Illegal attribute names:
  7208. ```````````````````````````````` example
  7209. <a h*#ref="hi">
  7210. .
  7211. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  7212. ````````````````````````````````
  7213. Illegal attribute values:
  7214. ```````````````````````````````` example
  7215. <a href="hi'> <a href=hi'>
  7216. .
  7217. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  7218. ````````````````````````````````
  7219. Illegal whitespace:
  7220. ```````````````````````````````` example
  7221. < a><
  7222. foo><bar/ >
  7223. <foo bar=baz
  7224. bim!bop />
  7225. .
  7226. <p>&lt; a&gt;&lt;
  7227. foo&gt;&lt;bar/ &gt;
  7228. &lt;foo bar=baz
  7229. bim!bop /&gt;</p>
  7230. ````````````````````````````````
  7231. Missing whitespace:
  7232. ```````````````````````````````` example
  7233. <a href='bar'title=title>
  7234. .
  7235. <p>&lt;a href='bar'title=title&gt;</p>
  7236. ````````````````````````````````
  7237. Closing tags:
  7238. ```````````````````````````````` example
  7239. </a></foo >
  7240. .
  7241. <p></a></foo ></p>
  7242. ````````````````````````````````
  7243. Illegal attributes in closing tag:
  7244. ```````````````````````````````` example
  7245. </a href="foo">
  7246. .
  7247. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  7248. ````````````````````````````````
  7249. Comments:
  7250. ```````````````````````````````` example
  7251. foo <!-- this is a
  7252. comment - with hyphen -->
  7253. .
  7254. <p>foo <!-- this is a
  7255. comment - with hyphen --></p>
  7256. ````````````````````````````````
  7257. ```````````````````````````````` example
  7258. foo <!-- not a comment -- two hyphens -->
  7259. .
  7260. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  7261. ````````````````````````````````
  7262. Not comments:
  7263. ```````````````````````````````` example
  7264. foo <!--> foo -->
  7265. foo <!-- foo--->
  7266. .
  7267. <p>foo &lt;!--&gt; foo --&gt;</p>
  7268. <p>foo &lt;!-- foo---&gt;</p>
  7269. ````````````````````````````````
  7270. Processing instructions:
  7271. ```````````````````````````````` example
  7272. foo <?php echo $a; ?>
  7273. .
  7274. <p>foo <?php echo $a; ?></p>
  7275. ````````````````````````````````
  7276. Declarations:
  7277. ```````````````````````````````` example
  7278. foo <!ELEMENT br EMPTY>
  7279. .
  7280. <p>foo <!ELEMENT br EMPTY></p>
  7281. ````````````````````````````````
  7282. CDATA sections:
  7283. ```````````````````````````````` example
  7284. foo <![CDATA[>&<]]>
  7285. .
  7286. <p>foo <![CDATA[>&<]]></p>
  7287. ````````````````````````````````
  7288. Entity and numeric character references are preserved in HTML
  7289. attributes:
  7290. ```````````````````````````````` example
  7291. foo <a href="&ouml;">
  7292. .
  7293. <p>foo <a href="&ouml;"></p>
  7294. ````````````````````````````````
  7295. Backslash escapes do not work in HTML attributes:
  7296. ```````````````````````````````` example
  7297. foo <a href="\*">
  7298. .
  7299. <p>foo <a href="\*"></p>
  7300. ````````````````````````````````
  7301. ```````````````````````````````` example
  7302. <a href="\"">
  7303. .
  7304. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  7305. ````````````````````````````````
  7306. ## Hard line breaks
  7307. A line ending (not in a code span or HTML tag) that is preceded
  7308. by two or more spaces and does not occur at the end of a block
  7309. is parsed as a [hard line break](@) (rendered
  7310. in HTML as a `<br />` tag):
  7311. ```````````````````````````````` example
  7312. foo
  7313. baz
  7314. .
  7315. <p>foo<br />
  7316. baz</p>
  7317. ````````````````````````````````
  7318. For a more visible alternative, a backslash before the
  7319. [line ending] may be used instead of two or more spaces:
  7320. ```````````````````````````````` example
  7321. foo\
  7322. baz
  7323. .
  7324. <p>foo<br />
  7325. baz</p>
  7326. ````````````````````````````````
  7327. More than two spaces can be used:
  7328. ```````````````````````````````` example
  7329. foo
  7330. baz
  7331. .
  7332. <p>foo<br />
  7333. baz</p>
  7334. ````````````````````````````````
  7335. Leading spaces at the beginning of the next line are ignored:
  7336. ```````````````````````````````` example
  7337. foo
  7338. bar
  7339. .
  7340. <p>foo<br />
  7341. bar</p>
  7342. ````````````````````````````````
  7343. ```````````````````````````````` example
  7344. foo\
  7345. bar
  7346. .
  7347. <p>foo<br />
  7348. bar</p>
  7349. ````````````````````````````````
  7350. Hard line breaks can occur inside emphasis, links, and other constructs
  7351. that allow inline content:
  7352. ```````````````````````````````` example
  7353. *foo
  7354. bar*
  7355. .
  7356. <p><em>foo<br />
  7357. bar</em></p>
  7358. ````````````````````````````````
  7359. ```````````````````````````````` example
  7360. *foo\
  7361. bar*
  7362. .
  7363. <p><em>foo<br />
  7364. bar</em></p>
  7365. ````````````````````````````````
  7366. Hard line breaks do not occur inside code spans
  7367. ```````````````````````````````` example
  7368. `code
  7369. span`
  7370. .
  7371. <p><code>code span</code></p>
  7372. ````````````````````````````````
  7373. ```````````````````````````````` example
  7374. `code\
  7375. span`
  7376. .
  7377. <p><code>code\ span</code></p>
  7378. ````````````````````````````````
  7379. or HTML tags:
  7380. ```````````````````````````````` example
  7381. <a href="foo
  7382. bar">
  7383. .
  7384. <p><a href="foo
  7385. bar"></p>
  7386. ````````````````````````````````
  7387. ```````````````````````````````` example
  7388. <a href="foo\
  7389. bar">
  7390. .
  7391. <p><a href="foo\
  7392. bar"></p>
  7393. ````````````````````````````````
  7394. Hard line breaks are for separating inline content within a block.
  7395. Neither syntax for hard line breaks works at the end of a paragraph or
  7396. other block element:
  7397. ```````````````````````````````` example
  7398. foo\
  7399. .
  7400. <p>foo\</p>
  7401. ````````````````````````````````
  7402. ```````````````````````````````` example
  7403. foo
  7404. .
  7405. <p>foo</p>
  7406. ````````````````````````````````
  7407. ```````````````````````````````` example
  7408. ### foo\
  7409. .
  7410. <h3>foo\</h3>
  7411. ````````````````````````````````
  7412. ```````````````````````````````` example
  7413. ### foo
  7414. .
  7415. <h3>foo</h3>
  7416. ````````````````````````````````
  7417. ## Soft line breaks
  7418. A regular line ending (not in a code span or HTML tag) that is not
  7419. preceded by two or more spaces or a backslash is parsed as a
  7420. [softbreak](@). (A soft line break may be rendered in HTML either as a
  7421. [line ending] or as a space. The result will be the same in
  7422. browsers. In the examples here, a [line ending] will be used.)
  7423. ```````````````````````````````` example
  7424. foo
  7425. baz
  7426. .
  7427. <p>foo
  7428. baz</p>
  7429. ````````````````````````````````
  7430. Spaces at the end of the line and beginning of the next line are
  7431. removed:
  7432. ```````````````````````````````` example
  7433. foo
  7434. baz
  7435. .
  7436. <p>foo
  7437. baz</p>
  7438. ````````````````````````````````
  7439. A conforming parser may render a soft line break in HTML either as a
  7440. line ending or as a space.
  7441. A renderer may also provide an option to render soft line breaks
  7442. as hard line breaks.
  7443. ## Textual content
  7444. Any characters not given an interpretation by the above rules will
  7445. be parsed as plain textual content.
  7446. ```````````````````````````````` example
  7447. hello $.;'there
  7448. .
  7449. <p>hello $.;'there</p>
  7450. ````````````````````````````````
  7451. ```````````````````````````````` example
  7452. Foo χρῆν
  7453. .
  7454. <p>Foo χρῆν</p>
  7455. ````````````````````````````````
  7456. Internal spaces are preserved verbatim:
  7457. ```````````````````````````````` example
  7458. Multiple spaces
  7459. .
  7460. <p>Multiple spaces</p>
  7461. ````````````````````````````````
  7462. <!-- END TESTS -->
  7463. # Appendix: A parsing strategy
  7464. In this appendix we describe some features of the parsing strategy
  7465. used in the CommonMark reference implementations.
  7466. ## Overview
  7467. Parsing has two phases:
  7468. 1. In the first phase, lines of input are consumed and the block
  7469. structure of the document---its division into paragraphs, block quotes,
  7470. list items, and so on---is constructed. Text is assigned to these
  7471. blocks but not parsed. Link reference definitions are parsed and a
  7472. map of links is constructed.
  7473. 2. In the second phase, the raw text contents of paragraphs and headings
  7474. are parsed into sequences of Markdown inline elements (strings,
  7475. code spans, links, emphasis, and so on), using the map of link
  7476. references constructed in phase 1.
  7477. At each point in processing, the document is represented as a tree of
  7478. **blocks**. The root of the tree is a `document` block. The `document`
  7479. may have any number of other blocks as **children**. These children
  7480. may, in turn, have other blocks as children. The last child of a block
  7481. is normally considered **open**, meaning that subsequent lines of input
  7482. can alter its contents. (Blocks that are not open are **closed**.)
  7483. Here, for example, is a possible document tree, with the open blocks
  7484. marked by arrows:
  7485. ``` tree
  7486. -> document
  7487. -> block_quote
  7488. paragraph
  7489. "Lorem ipsum dolor\nsit amet."
  7490. -> list (type=bullet tight=true bullet_char=-)
  7491. list_item
  7492. paragraph
  7493. "Qui *quodsi iracundia*"
  7494. -> list_item
  7495. -> paragraph
  7496. "aliquando id"
  7497. ```
  7498. ## Phase 1: block structure
  7499. Each line that is processed has an effect on this tree. The line is
  7500. analyzed and, depending on its contents, the document may be altered
  7501. in one or more of the following ways:
  7502. 1. One or more open blocks may be closed.
  7503. 2. One or more new blocks may be created as children of the
  7504. last open block.
  7505. 3. Text may be added to the last (deepest) open block remaining
  7506. on the tree.
  7507. Once a line has been incorporated into the tree in this way,
  7508. it can be discarded, so input can be read in a stream.
  7509. For each line, we follow this procedure:
  7510. 1. First we iterate through the open blocks, starting with the
  7511. root document, and descending through last children down to the last
  7512. open block. Each block imposes a condition that the line must satisfy
  7513. if the block is to remain open. For example, a block quote requires a
  7514. `>` character. A paragraph requires a non-blank line.
  7515. In this phase we may match all or just some of the open
  7516. blocks. But we cannot close unmatched blocks yet, because we may have a
  7517. [lazy continuation line].
  7518. 2. Next, after consuming the continuation markers for existing
  7519. blocks, we look for new block starts (e.g. `>` for a block quote).
  7520. If we encounter a new block start, we close any blocks unmatched
  7521. in step 1 before creating the new block as a child of the last
  7522. matched container block.
  7523. 3. Finally, we look at the remainder of the line (after block
  7524. markers like `>`, list markers, and indentation have been consumed).
  7525. This is text that can be incorporated into the last open
  7526. block (a paragraph, code block, heading, or raw HTML).
  7527. Setext headings are formed when we see a line of a paragraph
  7528. that is a [setext heading underline].
  7529. Reference link definitions are detected when a paragraph is closed;
  7530. the accumulated text lines are parsed to see if they begin with
  7531. one or more reference link definitions. Any remainder becomes a
  7532. normal paragraph.
  7533. We can see how this works by considering how the tree above is
  7534. generated by four lines of Markdown:
  7535. ``` markdown
  7536. > Lorem ipsum dolor
  7537. sit amet.
  7538. > - Qui *quodsi iracundia*
  7539. > - aliquando id
  7540. ```
  7541. At the outset, our document model is just
  7542. ``` tree
  7543. -> document
  7544. ```
  7545. The first line of our text,
  7546. ``` markdown
  7547. > Lorem ipsum dolor
  7548. ```
  7549. causes a `block_quote` block to be created as a child of our
  7550. open `document` block, and a `paragraph` block as a child of
  7551. the `block_quote`. Then the text is added to the last open
  7552. block, the `paragraph`:
  7553. ``` tree
  7554. -> document
  7555. -> block_quote
  7556. -> paragraph
  7557. "Lorem ipsum dolor"
  7558. ```
  7559. The next line,
  7560. ``` markdown
  7561. sit amet.
  7562. ```
  7563. is a "lazy continuation" of the open `paragraph`, so it gets added
  7564. to the paragraph's text:
  7565. ``` tree
  7566. -> document
  7567. -> block_quote
  7568. -> paragraph
  7569. "Lorem ipsum dolor\nsit amet."
  7570. ```
  7571. The third line,
  7572. ``` markdown
  7573. > - Qui *quodsi iracundia*
  7574. ```
  7575. causes the `paragraph` block to be closed, and a new `list` block
  7576. opened as a child of the `block_quote`. A `list_item` is also
  7577. added as a child of the `list`, and a `paragraph` as a child of
  7578. the `list_item`. The text is then added to the new `paragraph`:
  7579. ``` tree
  7580. -> document
  7581. -> block_quote
  7582. paragraph
  7583. "Lorem ipsum dolor\nsit amet."
  7584. -> list (type=bullet tight=true bullet_char=-)
  7585. -> list_item
  7586. -> paragraph
  7587. "Qui *quodsi iracundia*"
  7588. ```
  7589. The fourth line,
  7590. ``` markdown
  7591. > - aliquando id
  7592. ```
  7593. causes the `list_item` (and its child the `paragraph`) to be closed,
  7594. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7595. is added as a child of the new `list_item`, to contain the text.
  7596. We thus obtain the final tree:
  7597. ``` tree
  7598. -> document
  7599. -> block_quote
  7600. paragraph
  7601. "Lorem ipsum dolor\nsit amet."
  7602. -> list (type=bullet tight=true bullet_char=-)
  7603. list_item
  7604. paragraph
  7605. "Qui *quodsi iracundia*"
  7606. -> list_item
  7607. -> paragraph
  7608. "aliquando id"
  7609. ```
  7610. ## Phase 2: inline structure
  7611. Once all of the input has been parsed, all open blocks are closed.
  7612. We then "walk the tree," visiting every node, and parse raw
  7613. string contents of paragraphs and headings as inlines. At this
  7614. point we have seen all the link reference definitions, so we can
  7615. resolve reference links as we go.
  7616. ``` tree
  7617. document
  7618. block_quote
  7619. paragraph
  7620. str "Lorem ipsum dolor"
  7621. softbreak
  7622. str "sit amet."
  7623. list (type=bullet tight=true bullet_char=-)
  7624. list_item
  7625. paragraph
  7626. str "Qui "
  7627. emph
  7628. str "quodsi iracundia"
  7629. list_item
  7630. paragraph
  7631. str "aliquando id"
  7632. ```
  7633. Notice how the [line ending] in the first paragraph has
  7634. been parsed as a `softbreak`, and the asterisks in the first list item
  7635. have become an `emph`.
  7636. ### An algorithm for parsing nested emphasis and links
  7637. By far the trickiest part of inline parsing is handling emphasis,
  7638. strong emphasis, links, and images. This is done using the following
  7639. algorithm.
  7640. When we're parsing inlines and we hit either
  7641. - a run of `*` or `_` characters, or
  7642. - a `[` or `![`
  7643. we insert a text node with these symbols as its literal content, and we
  7644. add a pointer to this text node to the [delimiter stack](@).
  7645. The [delimiter stack] is a doubly linked list. Each
  7646. element contains a pointer to a text node, plus information about
  7647. - the type of delimiter (`[`, `![`, `*`, `_`)
  7648. - the number of delimiters,
  7649. - whether the delimiter is "active" (all are active to start), and
  7650. - whether the delimiter is a potential opener, a potential closer,
  7651. or both (which depends on what sort of characters precede
  7652. and follow the delimiters).
  7653. When we hit a `]` character, we call the *look for link or image*
  7654. procedure (see below).
  7655. When we hit the end of the input, we call the *process emphasis*
  7656. procedure (see below), with `stack_bottom` = NULL.
  7657. #### *look for link or image*
  7658. Starting at the top of the delimiter stack, we look backwards
  7659. through the stack for an opening `[` or `![` delimiter.
  7660. - If we don't find one, we return a literal text node `]`.
  7661. - If we do find one, but it's not *active*, we remove the inactive
  7662. delimiter from the stack, and return a literal text node `]`.
  7663. - If we find one and it's active, then we parse ahead to see if
  7664. we have an inline link/image, reference link/image, compact reference
  7665. link/image, or shortcut reference link/image.
  7666. + If we don't, then we remove the opening delimiter from the
  7667. delimiter stack and return a literal text node `]`.
  7668. + If we do, then
  7669. * We return a link or image node whose children are the inlines
  7670. after the text node pointed to by the opening delimiter.
  7671. * We run *process emphasis* on these inlines, with the `[` opener
  7672. as `stack_bottom`.
  7673. * We remove the opening delimiter.
  7674. * If we have a link (and not an image), we also set all
  7675. `[` delimiters before the opening delimiter to *inactive*. (This
  7676. will prevent us from getting links within links.)
  7677. #### *process emphasis*
  7678. Parameter `stack_bottom` sets a lower bound to how far we
  7679. descend in the [delimiter stack]. If it is NULL, we can
  7680. go all the way to the bottom. Otherwise, we stop before
  7681. visiting `stack_bottom`.
  7682. Let `current_position` point to the element on the [delimiter stack]
  7683. just above `stack_bottom` (or the first element if `stack_bottom`
  7684. is NULL).
  7685. We keep track of the `openers_bottom` for each delimiter
  7686. type (`*`, `_`) and each length of the closing delimiter run
  7687. (modulo 3). Initialize this to `stack_bottom`.
  7688. Then we repeat the following until we run out of potential
  7689. closers:
  7690. - Move `current_position` forward in the delimiter stack (if needed)
  7691. until we find the first potential closer with delimiter `*` or `_`.
  7692. (This will be the potential closer closest
  7693. to the beginning of the input -- the first one in parse order.)
  7694. - Now, look back in the stack (staying above `stack_bottom` and
  7695. the `openers_bottom` for this delimiter type) for the
  7696. first matching potential opener ("matching" means same delimiter).
  7697. - If one is found:
  7698. + Figure out whether we have emphasis or strong emphasis:
  7699. if both closer and opener spans have length >= 2, we have
  7700. strong, otherwise regular.
  7701. + Insert an emph or strong emph node accordingly, after
  7702. the text node corresponding to the opener.
  7703. + Remove any delimiters between the opener and closer from
  7704. the delimiter stack.
  7705. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7706. from the opening and closing text nodes. If they become empty
  7707. as a result, remove them and remove the corresponding element
  7708. of the delimiter stack. If the closing node is removed, reset
  7709. `current_position` to the next element in the stack.
  7710. - If none is found:
  7711. + Set `openers_bottom` to the element before `current_position`.
  7712. (We know that there are no openers for this kind of closer up to and
  7713. including this point, so this puts a lower bound on future searches.)
  7714. + If the closer at `current_position` is not a potential opener,
  7715. remove it from the delimiter stack (since we know it can't
  7716. be a closer either).
  7717. + Advance `current_position` to the next element in the stack.
  7718. After we're done, we remove all delimiters above `stack_bottom` from the
  7719. delimiter stack.