aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 14c8d65232ba76787bcf4ee876257779a11f177d (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. The title may extend over multiple lines:
  2452. ```````````````````````````````` example
  2453. [foo]: /url '
  2454. title
  2455. line1
  2456. line2
  2457. '
  2458. [foo]
  2459. .
  2460. <p><a href="/url" title="
  2461. title
  2462. line1
  2463. line2
  2464. ">foo</a></p>
  2465. ````````````````````````````````
  2466. However, it may not contain a [blank line]:
  2467. ```````````````````````````````` example
  2468. [foo]: /url 'title
  2469. with blank line'
  2470. [foo]
  2471. .
  2472. <p>[foo]: /url 'title</p>
  2473. <p>with blank line'</p>
  2474. <p>[foo]</p>
  2475. ````````````````````````````````
  2476. The title may be omitted:
  2477. ```````````````````````````````` example
  2478. [foo]:
  2479. /url
  2480. [foo]
  2481. .
  2482. <p><a href="/url">foo</a></p>
  2483. ````````````````````````````````
  2484. The link destination may not be omitted:
  2485. ```````````````````````````````` example
  2486. [foo]:
  2487. [foo]
  2488. .
  2489. <p>[foo]:</p>
  2490. <p>[foo]</p>
  2491. ````````````````````````````````
  2492. However, an empty link destination may be specified using
  2493. angle brackets:
  2494. ```````````````````````````````` example
  2495. [foo]: <>
  2496. [foo]
  2497. .
  2498. <p><a href="">foo</a></p>
  2499. ````````````````````````````````
  2500. The title must be separated from the link destination by
  2501. spaces or tabs:
  2502. ```````````````````````````````` example
  2503. [foo]: <bar>(baz)
  2504. [foo]
  2505. .
  2506. <p>[foo]: <bar>(baz)</p>
  2507. <p>[foo]</p>
  2508. ````````````````````````````````
  2509. Both title and destination can contain backslash escapes
  2510. and literal backslashes:
  2511. ```````````````````````````````` example
  2512. [foo]: /url\bar\*baz "foo\"bar\baz"
  2513. [foo]
  2514. .
  2515. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2516. ````````````````````````````````
  2517. A link can come before its corresponding definition:
  2518. ```````````````````````````````` example
  2519. [foo]
  2520. [foo]: url
  2521. .
  2522. <p><a href="url">foo</a></p>
  2523. ````````````````````````````````
  2524. If there are several matching definitions, the first one takes
  2525. precedence:
  2526. ```````````````````````````````` example
  2527. [foo]
  2528. [foo]: first
  2529. [foo]: second
  2530. .
  2531. <p><a href="first">foo</a></p>
  2532. ````````````````````````````````
  2533. As noted in the section on [Links], matching of labels is
  2534. case-insensitive (see [matches]).
  2535. ```````````````````````````````` example
  2536. [FOO]: /url
  2537. [Foo]
  2538. .
  2539. <p><a href="/url">Foo</a></p>
  2540. ````````````````````````````````
  2541. ```````````````````````````````` example
  2542. [ΑΓΩ]: /φου
  2543. [αγω]
  2544. .
  2545. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2546. ````````````````````````````````
  2547. Here is a link reference definition with no corresponding link.
  2548. It contributes nothing to the document.
  2549. ```````````````````````````````` example
  2550. [foo]: /url
  2551. .
  2552. ````````````````````````````````
  2553. Here is another one:
  2554. ```````````````````````````````` example
  2555. [
  2556. foo
  2557. ]: /url
  2558. bar
  2559. .
  2560. <p>bar</p>
  2561. ````````````````````````````````
  2562. This is not a link reference definition, because there are
  2563. characters other than spaces or tabs after the title:
  2564. ```````````````````````````````` example
  2565. [foo]: /url "title" ok
  2566. .
  2567. <p>[foo]: /url &quot;title&quot; ok</p>
  2568. ````````````````````````````````
  2569. This is a link reference definition, but it has no title:
  2570. ```````````````````````````````` example
  2571. [foo]: /url
  2572. "title" ok
  2573. .
  2574. <p>&quot;title&quot; ok</p>
  2575. ````````````````````````````````
  2576. This is not a link reference definition, because it is indented
  2577. four spaces:
  2578. ```````````````````````````````` example
  2579. [foo]: /url "title"
  2580. [foo]
  2581. .
  2582. <pre><code>[foo]: /url &quot;title&quot;
  2583. </code></pre>
  2584. <p>[foo]</p>
  2585. ````````````````````````````````
  2586. This is not a link reference definition, because it occurs inside
  2587. a code block:
  2588. ```````````````````````````````` example
  2589. ```
  2590. [foo]: /url
  2591. ```
  2592. [foo]
  2593. .
  2594. <pre><code>[foo]: /url
  2595. </code></pre>
  2596. <p>[foo]</p>
  2597. ````````````````````````````````
  2598. A [link reference definition] cannot interrupt a paragraph.
  2599. ```````````````````````````````` example
  2600. Foo
  2601. [bar]: /baz
  2602. [bar]
  2603. .
  2604. <p>Foo
  2605. [bar]: /baz</p>
  2606. <p>[bar]</p>
  2607. ````````````````````````````````
  2608. However, it can directly follow other block elements, such as headings
  2609. and thematic breaks, and it need not be followed by a blank line.
  2610. ```````````````````````````````` example
  2611. # [Foo]
  2612. [foo]: /url
  2613. > bar
  2614. .
  2615. <h1><a href="/url">Foo</a></h1>
  2616. <blockquote>
  2617. <p>bar</p>
  2618. </blockquote>
  2619. ````````````````````````````````
  2620. ```````````````````````````````` example
  2621. [foo]: /url
  2622. bar
  2623. ===
  2624. [foo]
  2625. .
  2626. <h1>bar</h1>
  2627. <p><a href="/url">foo</a></p>
  2628. ````````````````````````````````
  2629. ```````````````````````````````` example
  2630. [foo]: /url
  2631. ===
  2632. [foo]
  2633. .
  2634. <p>===
  2635. <a href="/url">foo</a></p>
  2636. ````````````````````````````````
  2637. Several [link reference definitions]
  2638. can occur one after another, without intervening blank lines.
  2639. ```````````````````````````````` example
  2640. [foo]: /foo-url "foo"
  2641. [bar]: /bar-url
  2642. "bar"
  2643. [baz]: /baz-url
  2644. [foo],
  2645. [bar],
  2646. [baz]
  2647. .
  2648. <p><a href="/foo-url" title="foo">foo</a>,
  2649. <a href="/bar-url" title="bar">bar</a>,
  2650. <a href="/baz-url">baz</a></p>
  2651. ````````````````````````````````
  2652. [Link reference definitions] can occur
  2653. inside block containers, like lists and block quotations. They
  2654. affect the entire document, not just the container in which they
  2655. are defined:
  2656. ```````````````````````````````` example
  2657. [foo]
  2658. > [foo]: /url
  2659. .
  2660. <p><a href="/url">foo</a></p>
  2661. <blockquote>
  2662. </blockquote>
  2663. ````````````````````````````````
  2664. Whether something is a [link reference definition] is
  2665. independent of whether the link reference it defines is
  2666. used in the document. Thus, for example, the following
  2667. document contains just a link reference definition, and
  2668. no visible content:
  2669. ```````````````````````````````` example
  2670. [foo]: /url
  2671. .
  2672. ````````````````````````````````
  2673. ## Annotation reference definitions
  2674. An [annotation reference definition](@)
  2675. consists of an [annotation reference label],
  2676. optionally preceded by up to three spaces of indentation,
  2677. followed by a colon (`:`),
  2678. optional spaces or tabs (including up to one [line ending]),
  2679. an [annotation destination],
  2680. and an optional [annotation reference flag],
  2681. which if it is present must be separated
  2682. from the [annotation reference label]
  2683. and [annotation reference destination]
  2684. by spaces or tabs (including up to one [line ending]).
  2685. No further character may occur.
  2686. If [annotation reference flag] is present
  2687. then [annotation reference destination] is optional.
  2688. An [annotation reference definition]
  2689. does not correspond to a structural element of a document.
  2690. Instead, it defines a label
  2691. which can be used in [annotations] elsewhere in the document.
  2692. [Annotation reference definitions] can come
  2693. either before or after the links that use them.
  2694. FIXME: define [annotation reference label]
  2695. FIXME: define [annotation reference flag] "@default"
  2696. FIXME: define [annotation initial context]
  2697. from <https://www.w3.org/2011/rdfa-context/rdfa-1.1>.
  2698. ## Paragraphs
  2699. A sequence of non-blank lines that cannot be interpreted as other
  2700. kinds of blocks forms a [paragraph](@).
  2701. The contents of the paragraph are the result of parsing the
  2702. paragraph's raw content as inlines. The paragraph's raw content
  2703. is formed by concatenating the lines and removing initial and final
  2704. spaces or tabs.
  2705. A simple example with two paragraphs:
  2706. ```````````````````````````````` example
  2707. aaa
  2708. bbb
  2709. .
  2710. <p>aaa</p>
  2711. <p>bbb</p>
  2712. ````````````````````````````````
  2713. Paragraphs can contain multiple lines, but no blank lines:
  2714. ```````````````````````````````` example
  2715. aaa
  2716. bbb
  2717. ccc
  2718. ddd
  2719. .
  2720. <p>aaa
  2721. bbb</p>
  2722. <p>ccc
  2723. ddd</p>
  2724. ````````````````````````````````
  2725. Multiple blank lines between paragraphs have no effect:
  2726. ```````````````````````````````` example
  2727. aaa
  2728. bbb
  2729. .
  2730. <p>aaa</p>
  2731. <p>bbb</p>
  2732. ````````````````````````````````
  2733. Leading spaces or tabs are skipped:
  2734. ```````````````````````````````` example
  2735. aaa
  2736. bbb
  2737. .
  2738. <p>aaa
  2739. bbb</p>
  2740. ````````````````````````````````
  2741. Lines after the first may be indented any amount, since indented
  2742. code blocks cannot interrupt paragraphs.
  2743. ```````````````````````````````` example
  2744. aaa
  2745. bbb
  2746. ccc
  2747. .
  2748. <p>aaa
  2749. bbb
  2750. ccc</p>
  2751. ````````````````````````````````
  2752. However, the first line may be preceded by up to three spaces of indentation.
  2753. Four spaces of indentation is too many:
  2754. ```````````````````````````````` example
  2755. aaa
  2756. bbb
  2757. .
  2758. <p>aaa
  2759. bbb</p>
  2760. ````````````````````````````````
  2761. ```````````````````````````````` example
  2762. aaa
  2763. bbb
  2764. .
  2765. <pre><code>aaa
  2766. </code></pre>
  2767. <p>bbb</p>
  2768. ````````````````````````````````
  2769. Final spaces or tabs are stripped before inline parsing, so a paragraph
  2770. that ends with two or more spaces will not end with a [hard line
  2771. break]:
  2772. ```````````````````````````````` example
  2773. aaa
  2774. bbb
  2775. .
  2776. <p>aaa<br />
  2777. bbb</p>
  2778. ````````````````````````````````
  2779. ## Blank lines
  2780. [Blank lines] between block-level elements are ignored,
  2781. except for the role they play in determining whether a [list]
  2782. is [tight] or [loose].
  2783. Blank lines at the beginning and end of the document are also ignored.
  2784. ```````````````````````````````` example
  2785. aaa
  2786. # aaa
  2787. .
  2788. <p>aaa</p>
  2789. <h1>aaa</h1>
  2790. ````````````````````````````````
  2791. # Container blocks
  2792. A [container block](#container-blocks) is a block that has other
  2793. blocks as its contents. There are two basic kinds of container blocks:
  2794. [block quotes] and [list items].
  2795. [Lists] are meta-containers for [list items].
  2796. We define the syntax for container blocks recursively. The general
  2797. form of the definition is:
  2798. > If X is a sequence of blocks, then the result of
  2799. > transforming X in such-and-such a way is a container of type Y
  2800. > with these blocks as its content.
  2801. So, we explain what counts as a block quote or list item by explaining
  2802. how these can be *generated* from their contents. This should suffice
  2803. to define the syntax, although it does not give a recipe for *parsing*
  2804. these constructions. (A recipe is provided below in the section entitled
  2805. [A parsing strategy](#appendix-a-parsing-strategy).)
  2806. ## Block quotes
  2807. A [block quote marker](@),
  2808. optionally preceded by up to three spaces of indentation,
  2809. consists of (a) the character `>` together with a following space of
  2810. indentation, or (b) a single character `>` not followed by a space of
  2811. indentation.
  2812. The following rules define [block quotes]:
  2813. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2814. of blocks *Bs*, then the result of prepending a [block quote
  2815. marker] to the beginning of each line in *Ls*
  2816. is a [block quote](#block-quotes) containing *Bs*.
  2817. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2818. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2819. the initial [block quote marker] from one or
  2820. more lines in which the next character other than a space or tab after the
  2821. [block quote marker] is [paragraph continuation
  2822. text] is a block quote with *Bs* as its content.
  2823. [Paragraph continuation text](@) is text
  2824. that will be parsed as part of the content of a paragraph, but does
  2825. not occur at the beginning of the paragraph.
  2826. 3. **Consecutiveness.** A document cannot contain two [block
  2827. quotes] in a row unless there is a [blank line] between them.
  2828. Nothing else counts as a [block quote](#block-quotes).
  2829. Here is a simple example:
  2830. ```````````````````````````````` example
  2831. > # Foo
  2832. > bar
  2833. > baz
  2834. .
  2835. <blockquote>
  2836. <h1>Foo</h1>
  2837. <p>bar
  2838. baz</p>
  2839. </blockquote>
  2840. ````````````````````````````````
  2841. The space or tab after the `>` characters can be omitted:
  2842. ```````````````````````````````` example
  2843. ># Foo
  2844. >bar
  2845. > baz
  2846. .
  2847. <blockquote>
  2848. <h1>Foo</h1>
  2849. <p>bar
  2850. baz</p>
  2851. </blockquote>
  2852. ````````````````````````````````
  2853. The `>` characters can be preceded by up to three spaces of indentation:
  2854. ```````````````````````````````` example
  2855. > # Foo
  2856. > bar
  2857. > baz
  2858. .
  2859. <blockquote>
  2860. <h1>Foo</h1>
  2861. <p>bar
  2862. baz</p>
  2863. </blockquote>
  2864. ````````````````````````````````
  2865. Four spaces of indentation is too many:
  2866. ```````````````````````````````` example
  2867. > # Foo
  2868. > bar
  2869. > baz
  2870. .
  2871. <pre><code>&gt; # Foo
  2872. &gt; bar
  2873. &gt; baz
  2874. </code></pre>
  2875. ````````````````````````````````
  2876. The Laziness clause allows us to omit the `>` before
  2877. [paragraph continuation text]:
  2878. ```````````````````````````````` example
  2879. > # Foo
  2880. > bar
  2881. baz
  2882. .
  2883. <blockquote>
  2884. <h1>Foo</h1>
  2885. <p>bar
  2886. baz</p>
  2887. </blockquote>
  2888. ````````````````````````````````
  2889. A block quote can contain some lazy and some non-lazy
  2890. continuation lines:
  2891. ```````````````````````````````` example
  2892. > bar
  2893. baz
  2894. > foo
  2895. .
  2896. <blockquote>
  2897. <p>bar
  2898. baz
  2899. foo</p>
  2900. </blockquote>
  2901. ````````````````````````````````
  2902. Laziness only applies to lines that would have been continuations of
  2903. paragraphs had they been prepended with [block quote markers].
  2904. For example, the `> ` cannot be omitted in the second line of
  2905. ``` markdown
  2906. > foo
  2907. > ---
  2908. ```
  2909. without changing the meaning:
  2910. ```````````````````````````````` example
  2911. > foo
  2912. ---
  2913. .
  2914. <blockquote>
  2915. <p>foo</p>
  2916. </blockquote>
  2917. <hr />
  2918. ````````````````````````````````
  2919. Similarly, if we omit the `> ` in the second line of
  2920. ``` markdown
  2921. > - foo
  2922. > - bar
  2923. ```
  2924. then the block quote ends after the first line:
  2925. ```````````````````````````````` example
  2926. > - foo
  2927. - bar
  2928. .
  2929. <blockquote>
  2930. <ul>
  2931. <li>foo</li>
  2932. </ul>
  2933. </blockquote>
  2934. <ul>
  2935. <li>bar</li>
  2936. </ul>
  2937. ````````````````````````````````
  2938. For the same reason, we can't omit the `> ` in front of
  2939. subsequent lines of an indented or fenced code block:
  2940. ```````````````````````````````` example
  2941. > foo
  2942. bar
  2943. .
  2944. <blockquote>
  2945. <pre><code>foo
  2946. </code></pre>
  2947. </blockquote>
  2948. <pre><code>bar
  2949. </code></pre>
  2950. ````````````````````````````````
  2951. ```````````````````````````````` example
  2952. > ```
  2953. foo
  2954. ```
  2955. .
  2956. <blockquote>
  2957. <pre><code></code></pre>
  2958. </blockquote>
  2959. <p>foo</p>
  2960. <pre><code></code></pre>
  2961. ````````````````````````````````
  2962. Note that in the following case, we have a [lazy
  2963. continuation line]:
  2964. ```````````````````````````````` example
  2965. > foo
  2966. - bar
  2967. .
  2968. <blockquote>
  2969. <p>foo
  2970. - bar</p>
  2971. </blockquote>
  2972. ````````````````````````````````
  2973. To see why, note that in
  2974. ```markdown
  2975. > foo
  2976. > - bar
  2977. ```
  2978. the `- bar` is indented too far to start a list, and can't
  2979. be an indented code block because indented code blocks cannot
  2980. interrupt paragraphs, so it is [paragraph continuation text].
  2981. A block quote can be empty:
  2982. ```````````````````````````````` example
  2983. >
  2984. .
  2985. <blockquote>
  2986. </blockquote>
  2987. ````````````````````````````````
  2988. ```````````````````````````````` example
  2989. >
  2990. >
  2991. >
  2992. .
  2993. <blockquote>
  2994. </blockquote>
  2995. ````````````````````````````````
  2996. A block quote can have initial or final blank lines:
  2997. ```````````````````````````````` example
  2998. >
  2999. > foo
  3000. >
  3001. .
  3002. <blockquote>
  3003. <p>foo</p>
  3004. </blockquote>
  3005. ````````````````````````````````
  3006. A blank line always separates block quotes:
  3007. ```````````````````````````````` example
  3008. > foo
  3009. > bar
  3010. .
  3011. <blockquote>
  3012. <p>foo</p>
  3013. </blockquote>
  3014. <blockquote>
  3015. <p>bar</p>
  3016. </blockquote>
  3017. ````````````````````````````````
  3018. (Most current Markdown implementations, including John Gruber's
  3019. original `Markdown.pl`, will parse this example as a single block quote
  3020. with two paragraphs. But it seems better to allow the author to decide
  3021. whether two block quotes or one are wanted.)
  3022. Consecutiveness means that if we put these block quotes together,
  3023. we get a single block quote:
  3024. ```````````````````````````````` example
  3025. > foo
  3026. > bar
  3027. .
  3028. <blockquote>
  3029. <p>foo
  3030. bar</p>
  3031. </blockquote>
  3032. ````````````````````````````````
  3033. To get a block quote with two paragraphs, use:
  3034. ```````````````````````````````` example
  3035. > foo
  3036. >
  3037. > bar
  3038. .
  3039. <blockquote>
  3040. <p>foo</p>
  3041. <p>bar</p>
  3042. </blockquote>
  3043. ````````````````````````````````
  3044. Block quotes can interrupt paragraphs:
  3045. ```````````````````````````````` example
  3046. foo
  3047. > bar
  3048. .
  3049. <p>foo</p>
  3050. <blockquote>
  3051. <p>bar</p>
  3052. </blockquote>
  3053. ````````````````````````````````
  3054. In general, blank lines are not needed before or after block
  3055. quotes:
  3056. ```````````````````````````````` example
  3057. > aaa
  3058. ***
  3059. > bbb
  3060. .
  3061. <blockquote>
  3062. <p>aaa</p>
  3063. </blockquote>
  3064. <hr />
  3065. <blockquote>
  3066. <p>bbb</p>
  3067. </blockquote>
  3068. ````````````````````````````````
  3069. However, because of laziness, a blank line is needed between
  3070. a block quote and a following paragraph:
  3071. ```````````````````````````````` example
  3072. > bar
  3073. baz
  3074. .
  3075. <blockquote>
  3076. <p>bar
  3077. baz</p>
  3078. </blockquote>
  3079. ````````````````````````````````
  3080. ```````````````````````````````` example
  3081. > bar
  3082. baz
  3083. .
  3084. <blockquote>
  3085. <p>bar</p>
  3086. </blockquote>
  3087. <p>baz</p>
  3088. ````````````````````````````````
  3089. ```````````````````````````````` example
  3090. > bar
  3091. >
  3092. baz
  3093. .
  3094. <blockquote>
  3095. <p>bar</p>
  3096. </blockquote>
  3097. <p>baz</p>
  3098. ````````````````````````````````
  3099. It is a consequence of the Laziness rule that any number
  3100. of initial `>`s may be omitted on a continuation line of a
  3101. nested block quote:
  3102. ```````````````````````````````` example
  3103. > > > foo
  3104. bar
  3105. .
  3106. <blockquote>
  3107. <blockquote>
  3108. <blockquote>
  3109. <p>foo
  3110. bar</p>
  3111. </blockquote>
  3112. </blockquote>
  3113. </blockquote>
  3114. ````````````````````````````````
  3115. ```````````````````````````````` example
  3116. >>> foo
  3117. > bar
  3118. >>baz
  3119. .
  3120. <blockquote>
  3121. <blockquote>
  3122. <blockquote>
  3123. <p>foo
  3124. bar
  3125. baz</p>
  3126. </blockquote>
  3127. </blockquote>
  3128. </blockquote>
  3129. ````````````````````````````````
  3130. When including an indented code block in a block quote,
  3131. remember that the [block quote marker] includes
  3132. both the `>` and a following space of indentation. So *five spaces* are needed
  3133. after the `>`:
  3134. ```````````````````````````````` example
  3135. > code
  3136. > not code
  3137. .
  3138. <blockquote>
  3139. <pre><code>code
  3140. </code></pre>
  3141. </blockquote>
  3142. <blockquote>
  3143. <p>not code</p>
  3144. </blockquote>
  3145. ````````````````````````````````
  3146. ## List items
  3147. A [list marker](@) is a
  3148. [bullet list marker] or an [ordered list marker].
  3149. A [bullet list marker](@)
  3150. is a `-`, `+`, or `*` character.
  3151. An [ordered list marker](@)
  3152. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  3153. `.` character or a `)` character. (The reason for the length
  3154. limit is that with 10 digits we start seeing integer overflows
  3155. in some browsers.)
  3156. The following rules define [list items]:
  3157. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  3158. blocks *Bs* starting with a character other than a space or tab, and *M* is
  3159. a list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces of indentation,
  3160. then the result of prepending *M* and the following spaces to the first line
  3161. of Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  3162. list item with *Bs* as its contents. The type of the list item
  3163. (bullet or ordered) is determined by the type of its list marker.
  3164. If the list item is ordered, then it is also assigned a start
  3165. number, based on the ordered list marker.
  3166. Exceptions:
  3167. 1. When the first list item in a [list] interrupts
  3168. a paragraph---that is, when it starts on a line that would
  3169. otherwise count as [paragraph continuation text]---then (a)
  3170. the lines *Ls* must not begin with a blank line, and (b) if
  3171. the list item is ordered, the start number must be 1.
  3172. 2. If any line is a [thematic break][thematic breaks] then
  3173. that line is not a list item.
  3174. For example, let *Ls* be the lines
  3175. ```````````````````````````````` example
  3176. A paragraph
  3177. with two lines.
  3178. indented code
  3179. > A block quote.
  3180. .
  3181. <p>A paragraph
  3182. with two lines.</p>
  3183. <pre><code>indented code
  3184. </code></pre>
  3185. <blockquote>
  3186. <p>A block quote.</p>
  3187. </blockquote>
  3188. ````````````````````````````````
  3189. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  3190. that the following is an ordered list item with start number 1,
  3191. and the same contents as *Ls*:
  3192. ```````````````````````````````` example
  3193. 1. A paragraph
  3194. with two lines.
  3195. indented code
  3196. > A block quote.
  3197. .
  3198. <ol>
  3199. <li>
  3200. <p>A paragraph
  3201. with two lines.</p>
  3202. <pre><code>indented code
  3203. </code></pre>
  3204. <blockquote>
  3205. <p>A block quote.</p>
  3206. </blockquote>
  3207. </li>
  3208. </ol>
  3209. ````````````````````````````````
  3210. The most important thing to notice is that the position of
  3211. the text after the list marker determines how much indentation
  3212. is needed in subsequent blocks in the list item. If the list
  3213. marker takes up two spaces of indentation, and there are three spaces between
  3214. the list marker and the next character other than a space or tab, then blocks
  3215. must be indented five spaces in order to fall under the list
  3216. item.
  3217. Here are some examples showing how far content must be indented to be
  3218. put under the list item:
  3219. ```````````````````````````````` example
  3220. - one
  3221. two
  3222. .
  3223. <ul>
  3224. <li>one</li>
  3225. </ul>
  3226. <p>two</p>
  3227. ````````````````````````````````
  3228. ```````````````````````````````` example
  3229. - one
  3230. two
  3231. .
  3232. <ul>
  3233. <li>
  3234. <p>one</p>
  3235. <p>two</p>
  3236. </li>
  3237. </ul>
  3238. ````````````````````````````````
  3239. ```````````````````````````````` example
  3240. - one
  3241. two
  3242. .
  3243. <ul>
  3244. <li>one</li>
  3245. </ul>
  3246. <pre><code> two
  3247. </code></pre>
  3248. ````````````````````````````````
  3249. ```````````````````````````````` example
  3250. - one
  3251. two
  3252. .
  3253. <ul>
  3254. <li>
  3255. <p>one</p>
  3256. <p>two</p>
  3257. </li>
  3258. </ul>
  3259. ````````````````````````````````
  3260. It is tempting to think of this in terms of columns: the continuation
  3261. blocks must be indented at least to the column of the first character other than
  3262. a space or tab after the list marker. However, that is not quite right.
  3263. The spaces of indentation after the list marker determine how much relative
  3264. indentation is needed. Which column this indentation reaches will depend on
  3265. how the list item is embedded in other constructions, as shown by
  3266. this example:
  3267. ```````````````````````````````` example
  3268. > > 1. one
  3269. >>
  3270. >> two
  3271. .
  3272. <blockquote>
  3273. <blockquote>
  3274. <ol>
  3275. <li>
  3276. <p>one</p>
  3277. <p>two</p>
  3278. </li>
  3279. </ol>
  3280. </blockquote>
  3281. </blockquote>
  3282. ````````````````````````````````
  3283. Here `two` occurs in the same column as the list marker `1.`,
  3284. but is actually contained in the list item, because there is
  3285. sufficient indentation after the last containing blockquote marker.
  3286. The converse is also possible. In the following example, the word `two`
  3287. occurs far to the right of the initial text of the list item, `one`, but
  3288. it is not considered part of the list item, because it is not indented
  3289. far enough past the blockquote marker:
  3290. ```````````````````````````````` example
  3291. >>- one
  3292. >>
  3293. > > two
  3294. .
  3295. <blockquote>
  3296. <blockquote>
  3297. <ul>
  3298. <li>one</li>
  3299. </ul>
  3300. <p>two</p>
  3301. </blockquote>
  3302. </blockquote>
  3303. ````````````````````````````````
  3304. Note that at least one space or tab is needed between the list marker and
  3305. any following content, so these are not list items:
  3306. ```````````````````````````````` example
  3307. -one
  3308. 2.two
  3309. .
  3310. <p>-one</p>
  3311. <p>2.two</p>
  3312. ````````````````````````````````
  3313. A list item may contain blocks that are separated by more than
  3314. one blank line.
  3315. ```````````````````````````````` example
  3316. - foo
  3317. bar
  3318. .
  3319. <ul>
  3320. <li>
  3321. <p>foo</p>
  3322. <p>bar</p>
  3323. </li>
  3324. </ul>
  3325. ````````````````````````````````
  3326. A list item may contain any kind of block:
  3327. ```````````````````````````````` example
  3328. 1. foo
  3329. ```
  3330. bar
  3331. ```
  3332. baz
  3333. > bam
  3334. .
  3335. <ol>
  3336. <li>
  3337. <p>foo</p>
  3338. <pre><code>bar
  3339. </code></pre>
  3340. <p>baz</p>
  3341. <blockquote>
  3342. <p>bam</p>
  3343. </blockquote>
  3344. </li>
  3345. </ol>
  3346. ````````````````````````````````
  3347. A list item that contains an indented code block will preserve
  3348. empty lines within the code block verbatim.
  3349. ```````````````````````````````` example
  3350. - Foo
  3351. bar
  3352. baz
  3353. .
  3354. <ul>
  3355. <li>
  3356. <p>Foo</p>
  3357. <pre><code>bar
  3358. baz
  3359. </code></pre>
  3360. </li>
  3361. </ul>
  3362. ````````````````````````````````
  3363. Note that ordered list start numbers must be nine digits or less:
  3364. ```````````````````````````````` example
  3365. 123456789. ok
  3366. .
  3367. <ol start="123456789">
  3368. <li>ok</li>
  3369. </ol>
  3370. ````````````````````````````````
  3371. ```````````````````````````````` example
  3372. 1234567890. not ok
  3373. .
  3374. <p>1234567890. not ok</p>
  3375. ````````````````````````````````
  3376. A start number may begin with 0s:
  3377. ```````````````````````````````` example
  3378. 0. ok
  3379. .
  3380. <ol start="0">
  3381. <li>ok</li>
  3382. </ol>
  3383. ````````````````````````````````
  3384. ```````````````````````````````` example
  3385. 003. ok
  3386. .
  3387. <ol start="3">
  3388. <li>ok</li>
  3389. </ol>
  3390. ````````````````````````````````
  3391. A start number may not be negative:
  3392. ```````````````````````````````` example
  3393. -1. not ok
  3394. .
  3395. <p>-1. not ok</p>
  3396. ````````````````````````````````
  3397. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3398. constitute a sequence of blocks *Bs* starting with an indented code
  3399. block, and *M* is a list marker of width *W* followed by
  3400. one space of indentation, then the result of prepending *M* and the
  3401. following space to the first line of *Ls*, and indenting subsequent lines
  3402. of *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3403. If a line is empty, then it need not be indented. The type of the
  3404. list item (bullet or ordered) is determined by the type of its list
  3405. marker. If the list item is ordered, then it is also assigned a
  3406. start number, based on the ordered list marker.
  3407. An indented code block will have to be preceded by four spaces of indentation
  3408. beyond the edge of the region where text will be included in the list item.
  3409. In the following case that is 6 spaces:
  3410. ```````````````````````````````` example
  3411. - foo
  3412. bar
  3413. .
  3414. <ul>
  3415. <li>
  3416. <p>foo</p>
  3417. <pre><code>bar
  3418. </code></pre>
  3419. </li>
  3420. </ul>
  3421. ````````````````````````````````
  3422. And in this case it is 11 spaces:
  3423. ```````````````````````````````` example
  3424. 10. foo
  3425. bar
  3426. .
  3427. <ol start="10">
  3428. <li>
  3429. <p>foo</p>
  3430. <pre><code>bar
  3431. </code></pre>
  3432. </li>
  3433. </ol>
  3434. ````````````````````````````````
  3435. If the *first* block in the list item is an indented code block,
  3436. then by rule #2, the contents must be preceded by *one* space of indentation
  3437. after the list marker:
  3438. ```````````````````````````````` example
  3439. indented code
  3440. paragraph
  3441. more code
  3442. .
  3443. <pre><code>indented code
  3444. </code></pre>
  3445. <p>paragraph</p>
  3446. <pre><code>more code
  3447. </code></pre>
  3448. ````````````````````````````````
  3449. ```````````````````````````````` example
  3450. 1. indented code
  3451. paragraph
  3452. more code
  3453. .
  3454. <ol>
  3455. <li>
  3456. <pre><code>indented code
  3457. </code></pre>
  3458. <p>paragraph</p>
  3459. <pre><code>more code
  3460. </code></pre>
  3461. </li>
  3462. </ol>
  3463. ````````````````````````````````
  3464. Note that an additional space of indentation is interpreted as space
  3465. inside the code block:
  3466. ```````````````````````````````` example
  3467. 1. indented code
  3468. paragraph
  3469. more code
  3470. .
  3471. <ol>
  3472. <li>
  3473. <pre><code> indented code
  3474. </code></pre>
  3475. <p>paragraph</p>
  3476. <pre><code>more code
  3477. </code></pre>
  3478. </li>
  3479. </ol>
  3480. ````````````````````````````````
  3481. Note that rules #1 and #2 only apply to two cases: (a) cases
  3482. in which the lines to be included in a list item begin with a
  3483. characer other than a space or tab, and (b) cases in which
  3484. they begin with an indented code
  3485. block. In a case like the following, where the first block begins with
  3486. three spaces of indentation, the rules do not allow us to form a list item by
  3487. indenting the whole thing and prepending a list marker:
  3488. ```````````````````````````````` example
  3489. foo
  3490. bar
  3491. .
  3492. <p>foo</p>
  3493. <p>bar</p>
  3494. ````````````````````````````````
  3495. ```````````````````````````````` example
  3496. - foo
  3497. bar
  3498. .
  3499. <ul>
  3500. <li>foo</li>
  3501. </ul>
  3502. <p>bar</p>
  3503. ````````````````````````````````
  3504. This is not a significant restriction, because when a block is preceded by up to
  3505. three spaces of indentation, the indentation can always be removed without
  3506. a change in interpretation, allowing rule #1 to be applied. So, in
  3507. the above case:
  3508. ```````````````````````````````` example
  3509. - foo
  3510. bar
  3511. .
  3512. <ul>
  3513. <li>
  3514. <p>foo</p>
  3515. <p>bar</p>
  3516. </li>
  3517. </ul>
  3518. ````````````````````````````````
  3519. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3520. starting with a single [blank line] constitute a (possibly empty)
  3521. sequence of blocks *Bs*, and *M* is a list marker of width *W*,
  3522. then the result of prepending *M* to the first line of *Ls*, and
  3523. preceding subsequent lines of *Ls* by *W + 1* spaces of indentation, is a
  3524. list item with *Bs* as its contents.
  3525. If a line is empty, then it need not be indented. The type of the
  3526. list item (bullet or ordered) is determined by the type of its list
  3527. marker. If the list item is ordered, then it is also assigned a
  3528. start number, based on the ordered list marker.
  3529. Here are some list items that start with a blank line but are not empty:
  3530. ```````````````````````````````` example
  3531. -
  3532. foo
  3533. -
  3534. ```
  3535. bar
  3536. ```
  3537. -
  3538. baz
  3539. .
  3540. <ul>
  3541. <li>foo</li>
  3542. <li>
  3543. <pre><code>bar
  3544. </code></pre>
  3545. </li>
  3546. <li>
  3547. <pre><code>baz
  3548. </code></pre>
  3549. </li>
  3550. </ul>
  3551. ````````````````````````````````
  3552. When the list item starts with a blank line, the number of spaces
  3553. following the list marker doesn't change the required indentation:
  3554. ```````````````````````````````` example
  3555. -
  3556. foo
  3557. .
  3558. <ul>
  3559. <li>foo</li>
  3560. </ul>
  3561. ````````````````````````````````
  3562. A list item can begin with at most one blank line.
  3563. In the following example, `foo` is not part of the list
  3564. item:
  3565. ```````````````````````````````` example
  3566. -
  3567. foo
  3568. .
  3569. <ul>
  3570. <li></li>
  3571. </ul>
  3572. <p>foo</p>
  3573. ````````````````````````````````
  3574. Here is an empty bullet list item:
  3575. ```````````````````````````````` example
  3576. - foo
  3577. -
  3578. - bar
  3579. .
  3580. <ul>
  3581. <li>foo</li>
  3582. <li></li>
  3583. <li>bar</li>
  3584. </ul>
  3585. ````````````````````````````````
  3586. It does not matter whether there are spaces or tabs following the [list marker]:
  3587. ```````````````````````````````` example
  3588. - foo
  3589. -
  3590. - bar
  3591. .
  3592. <ul>
  3593. <li>foo</li>
  3594. <li></li>
  3595. <li>bar</li>
  3596. </ul>
  3597. ````````````````````````````````
  3598. Here is an empty ordered list item:
  3599. ```````````````````````````````` example
  3600. 1. foo
  3601. 2.
  3602. 3. bar
  3603. .
  3604. <ol>
  3605. <li>foo</li>
  3606. <li></li>
  3607. <li>bar</li>
  3608. </ol>
  3609. ````````````````````````````````
  3610. A list may start or end with an empty list item:
  3611. ```````````````````````````````` example
  3612. *
  3613. .
  3614. <ul>
  3615. <li></li>
  3616. </ul>
  3617. ````````````````````````````````
  3618. However, an empty list item cannot interrupt a paragraph:
  3619. ```````````````````````````````` example
  3620. foo
  3621. *
  3622. foo
  3623. 1.
  3624. .
  3625. <p>foo
  3626. *</p>
  3627. <p>foo
  3628. 1.</p>
  3629. ````````````````````````````````
  3630. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3631. according to rule #1, #2, or #3, then the result of preceding each line
  3632. of *Ls* by up to three spaces of indentation (the same for each line) also
  3633. constitutes a list item with the same contents and attributes. If a line is
  3634. empty, then it need not be indented.
  3635. Indented one space:
  3636. ```````````````````````````````` example
  3637. 1. A paragraph
  3638. with two lines.
  3639. indented code
  3640. > A block quote.
  3641. .
  3642. <ol>
  3643. <li>
  3644. <p>A paragraph
  3645. with two lines.</p>
  3646. <pre><code>indented code
  3647. </code></pre>
  3648. <blockquote>
  3649. <p>A block quote.</p>
  3650. </blockquote>
  3651. </li>
  3652. </ol>
  3653. ````````````````````````````````
  3654. Indented two spaces:
  3655. ```````````````````````````````` example
  3656. 1. A paragraph
  3657. with two lines.
  3658. indented code
  3659. > A block quote.
  3660. .
  3661. <ol>
  3662. <li>
  3663. <p>A paragraph
  3664. with two lines.</p>
  3665. <pre><code>indented code
  3666. </code></pre>
  3667. <blockquote>
  3668. <p>A block quote.</p>
  3669. </blockquote>
  3670. </li>
  3671. </ol>
  3672. ````````````````````````````````
  3673. Indented three spaces:
  3674. ```````````````````````````````` example
  3675. 1. A paragraph
  3676. with two lines.
  3677. indented code
  3678. > A block quote.
  3679. .
  3680. <ol>
  3681. <li>
  3682. <p>A paragraph
  3683. with two lines.</p>
  3684. <pre><code>indented code
  3685. </code></pre>
  3686. <blockquote>
  3687. <p>A block quote.</p>
  3688. </blockquote>
  3689. </li>
  3690. </ol>
  3691. ````````````````````````````````
  3692. Four spaces indent gives a code block:
  3693. ```````````````````````````````` example
  3694. 1. A paragraph
  3695. with two lines.
  3696. indented code
  3697. > A block quote.
  3698. .
  3699. <pre><code>1. A paragraph
  3700. with two lines.
  3701. indented code
  3702. &gt; A block quote.
  3703. </code></pre>
  3704. ````````````````````````````````
  3705. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3706. item](#list-items) with contents *Bs*, then the result of deleting
  3707. some or all of the indentation from one or more lines in which the
  3708. next character other than a space or tab after the indentation is
  3709. [paragraph continuation text] is a
  3710. list item with the same contents and attributes. The unindented
  3711. lines are called
  3712. [lazy continuation line](@)s.
  3713. Here is an example with [lazy continuation lines]:
  3714. ```````````````````````````````` example
  3715. 1. A paragraph
  3716. with two lines.
  3717. indented code
  3718. > A block quote.
  3719. .
  3720. <ol>
  3721. <li>
  3722. <p>A paragraph
  3723. with two lines.</p>
  3724. <pre><code>indented code
  3725. </code></pre>
  3726. <blockquote>
  3727. <p>A block quote.</p>
  3728. </blockquote>
  3729. </li>
  3730. </ol>
  3731. ````````````````````````````````
  3732. Indentation can be partially deleted:
  3733. ```````````````````````````````` example
  3734. 1. A paragraph
  3735. with two lines.
  3736. .
  3737. <ol>
  3738. <li>A paragraph
  3739. with two lines.</li>
  3740. </ol>
  3741. ````````````````````````````````
  3742. These examples show how laziness can work in nested structures:
  3743. ```````````````````````````````` example
  3744. > 1. > Blockquote
  3745. continued here.
  3746. .
  3747. <blockquote>
  3748. <ol>
  3749. <li>
  3750. <blockquote>
  3751. <p>Blockquote
  3752. continued here.</p>
  3753. </blockquote>
  3754. </li>
  3755. </ol>
  3756. </blockquote>
  3757. ````````````````````````````````
  3758. ```````````````````````````````` example
  3759. > 1. > Blockquote
  3760. > continued here.
  3761. .
  3762. <blockquote>
  3763. <ol>
  3764. <li>
  3765. <blockquote>
  3766. <p>Blockquote
  3767. continued here.</p>
  3768. </blockquote>
  3769. </li>
  3770. </ol>
  3771. </blockquote>
  3772. ````````````````````````````````
  3773. 6. **That's all.** Nothing that is not counted as a list item by rules
  3774. #1--5 counts as a [list item](#list-items).
  3775. The rules for sublists follow from the general rules
  3776. [above][List items]. A sublist must be indented the same number
  3777. of spaces of indentation a paragraph would need to be in order to be included
  3778. in the list item.
  3779. So, in this case we need two spaces indent:
  3780. ```````````````````````````````` example
  3781. - foo
  3782. - bar
  3783. - baz
  3784. - boo
  3785. .
  3786. <ul>
  3787. <li>foo
  3788. <ul>
  3789. <li>bar
  3790. <ul>
  3791. <li>baz
  3792. <ul>
  3793. <li>boo</li>
  3794. </ul>
  3795. </li>
  3796. </ul>
  3797. </li>
  3798. </ul>
  3799. </li>
  3800. </ul>
  3801. ````````````````````````````````
  3802. One is not enough:
  3803. ```````````````````````````````` example
  3804. - foo
  3805. - bar
  3806. - baz
  3807. - boo
  3808. .
  3809. <ul>
  3810. <li>foo</li>
  3811. <li>bar</li>
  3812. <li>baz</li>
  3813. <li>boo</li>
  3814. </ul>
  3815. ````````````````````````````````
  3816. Here we need four, because the list marker is wider:
  3817. ```````````````````````````````` example
  3818. 10) foo
  3819. - bar
  3820. .
  3821. <ol start="10">
  3822. <li>foo
  3823. <ul>
  3824. <li>bar</li>
  3825. </ul>
  3826. </li>
  3827. </ol>
  3828. ````````````````````````````````
  3829. Three is not enough:
  3830. ```````````````````````````````` example
  3831. 10) foo
  3832. - bar
  3833. .
  3834. <ol start="10">
  3835. <li>foo</li>
  3836. </ol>
  3837. <ul>
  3838. <li>bar</li>
  3839. </ul>
  3840. ````````````````````````````````
  3841. A list may be the first block in a list item:
  3842. ```````````````````````````````` example
  3843. - - foo
  3844. .
  3845. <ul>
  3846. <li>
  3847. <ul>
  3848. <li>foo</li>
  3849. </ul>
  3850. </li>
  3851. </ul>
  3852. ````````````````````````````````
  3853. ```````````````````````````````` example
  3854. 1. - 2. foo
  3855. .
  3856. <ol>
  3857. <li>
  3858. <ul>
  3859. <li>
  3860. <ol start="2">
  3861. <li>foo</li>
  3862. </ol>
  3863. </li>
  3864. </ul>
  3865. </li>
  3866. </ol>
  3867. ````````````````````````````````
  3868. A list item can contain a heading:
  3869. ```````````````````````````````` example
  3870. - # Foo
  3871. - Bar
  3872. ---
  3873. baz
  3874. .
  3875. <ul>
  3876. <li>
  3877. <h1>Foo</h1>
  3878. </li>
  3879. <li>
  3880. <h2>Bar</h2>
  3881. baz</li>
  3882. </ul>
  3883. ````````````````````````````````
  3884. ### Motivation
  3885. John Gruber's Markdown spec says the following about list items:
  3886. 1. "List markers typically start at the left margin, but may be indented
  3887. by up to three spaces. List markers must be followed by one or more
  3888. spaces or a tab."
  3889. 2. "To make lists look nice, you can wrap items with hanging indents....
  3890. But if you don't want to, you don't have to."
  3891. 3. "List items may consist of multiple paragraphs. Each subsequent
  3892. paragraph in a list item must be indented by either 4 spaces or one
  3893. tab."
  3894. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3895. but here again, Markdown will allow you to be lazy."
  3896. 5. "To put a blockquote within a list item, the blockquote's `>`
  3897. delimiters need to be indented."
  3898. 6. "To put a code block within a list item, the code block needs to be
  3899. indented twice — 8 spaces or two tabs."
  3900. These rules specify that a paragraph under a list item must be indented
  3901. four spaces (presumably, from the left margin, rather than the start of
  3902. the list marker, but this is not said), and that code under a list item
  3903. must be indented eight spaces instead of the usual four. They also say
  3904. that a block quote must be indented, but not by how much; however, the
  3905. example given has four spaces indentation. Although nothing is said
  3906. about other kinds of block-level content, it is certainly reasonable to
  3907. infer that *all* block elements under a list item, including other
  3908. lists, must be indented four spaces. This principle has been called the
  3909. *four-space rule*.
  3910. The four-space rule is clear and principled, and if the reference
  3911. implementation `Markdown.pl` had followed it, it probably would have
  3912. become the standard. However, `Markdown.pl` allowed paragraphs and
  3913. sublists to start with only two spaces indentation, at least on the
  3914. outer level. Worse, its behavior was inconsistent: a sublist of an
  3915. outer-level list needed two spaces indentation, but a sublist of this
  3916. sublist needed three spaces. It is not surprising, then, that different
  3917. implementations of Markdown have developed very different rules for
  3918. determining what comes under a list item. (Pandoc and python-Markdown,
  3919. for example, stuck with Gruber's syntax description and the four-space
  3920. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3921. followed `Markdown.pl`'s behavior more closely.)
  3922. Unfortunately, given the divergences between implementations, there
  3923. is no way to give a spec for list items that will be guaranteed not
  3924. to break any existing documents. However, the spec given here should
  3925. correctly handle lists formatted with either the four-space rule or
  3926. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3927. in a way that is natural for a human to read.
  3928. The strategy here is to let the width and indentation of the list marker
  3929. determine the indentation necessary for blocks to fall under the list
  3930. item, rather than having a fixed and arbitrary number. The writer can
  3931. think of the body of the list item as a unit which gets indented to the
  3932. right enough to fit the list marker (and any indentation on the list
  3933. marker). (The laziness rule, #5, then allows continuation lines to be
  3934. unindented if needed.)
  3935. This rule is superior, we claim, to any rule requiring a fixed level of
  3936. indentation from the margin. The four-space rule is clear but
  3937. unnatural. It is quite unintuitive that
  3938. ``` markdown
  3939. - foo
  3940. bar
  3941. - baz
  3942. ```
  3943. should be parsed as two lists with an intervening paragraph,
  3944. ``` html
  3945. <ul>
  3946. <li>foo</li>
  3947. </ul>
  3948. <p>bar</p>
  3949. <ul>
  3950. <li>baz</li>
  3951. </ul>
  3952. ```
  3953. as the four-space rule demands, rather than a single list,
  3954. ``` html
  3955. <ul>
  3956. <li>
  3957. <p>foo</p>
  3958. <p>bar</p>
  3959. <ul>
  3960. <li>baz</li>
  3961. </ul>
  3962. </li>
  3963. </ul>
  3964. ```
  3965. The choice of four spaces is arbitrary. It can be learned, but it is
  3966. not likely to be guessed, and it trips up beginners regularly.
  3967. Would it help to adopt a two-space rule? The problem is that such
  3968. a rule, together with the rule allowing up to three spaces of indentation for
  3969. the initial list marker, allows text that is indented *less than* the
  3970. original list marker to be included in the list item. For example,
  3971. `Markdown.pl` parses
  3972. ``` markdown
  3973. - one
  3974. two
  3975. ```
  3976. as a single list item, with `two` a continuation paragraph:
  3977. ``` html
  3978. <ul>
  3979. <li>
  3980. <p>one</p>
  3981. <p>two</p>
  3982. </li>
  3983. </ul>
  3984. ```
  3985. and similarly
  3986. ``` markdown
  3987. > - one
  3988. >
  3989. > two
  3990. ```
  3991. as
  3992. ``` html
  3993. <blockquote>
  3994. <ul>
  3995. <li>
  3996. <p>one</p>
  3997. <p>two</p>
  3998. </li>
  3999. </ul>
  4000. </blockquote>
  4001. ```
  4002. This is extremely unintuitive.
  4003. Rather than requiring a fixed indent from the margin, we could require
  4004. a fixed indent (say, two spaces, or even one space) from the list marker (which
  4005. may itself be indented). This proposal would remove the last anomaly
  4006. discussed. Unlike the spec presented above, it would count the following
  4007. as a list item with a subparagraph, even though the paragraph `bar`
  4008. is not indented as far as the first paragraph `foo`:
  4009. ``` markdown
  4010. 10. foo
  4011. bar
  4012. ```
  4013. Arguably this text does read like a list item with `bar` as a subparagraph,
  4014. which may count in favor of the proposal. However, on this proposal indented
  4015. code would have to be indented six spaces after the list marker. And this
  4016. would break a lot of existing Markdown, which has the pattern:
  4017. ``` markdown
  4018. 1. foo
  4019. indented code
  4020. ```
  4021. where the code is indented eight spaces. The spec above, by contrast, will
  4022. parse this text as expected, since the code block's indentation is measured
  4023. from the beginning of `foo`.
  4024. The one case that needs special treatment is a list item that *starts*
  4025. with indented code. How much indentation is required in that case, since
  4026. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  4027. that in such cases, we require one space indentation from the list marker
  4028. (and then the normal four spaces for the indented code). This will match the
  4029. four-space rule in cases where the list marker plus its initial indentation
  4030. takes four spaces (a common case), but diverge in other cases.
  4031. ## Lists
  4032. A [list](@) is a sequence of one or more
  4033. list items [of the same type]. The list items
  4034. may be separated by any number of blank lines.
  4035. Two list items are [of the same type](@)
  4036. if they begin with a [list marker] of the same type.
  4037. Two list markers are of the
  4038. same type if (a) they are bullet list markers using the same character
  4039. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  4040. delimiter (either `.` or `)`).
  4041. A list is an [ordered list](@)
  4042. if its constituent list items begin with
  4043. [ordered list markers], and a
  4044. [bullet list](@) if its constituent list
  4045. items begin with [bullet list markers].
  4046. The [start number](@)
  4047. of an [ordered list] is determined by the list number of
  4048. its initial list item. The numbers of subsequent list items are
  4049. disregarded.
  4050. A list is [loose](@) if any of its constituent
  4051. list items are separated by blank lines, or if any of its constituent
  4052. list items directly contain two block-level elements with a blank line
  4053. between them. Otherwise a list is [tight](@).
  4054. (The difference in HTML output is that paragraphs in a loose list are
  4055. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  4056. Changing the bullet or ordered list delimiter starts a new list:
  4057. ```````````````````````````````` example
  4058. - foo
  4059. - bar
  4060. + baz
  4061. .
  4062. <ul>
  4063. <li>foo</li>
  4064. <li>bar</li>
  4065. </ul>
  4066. <ul>
  4067. <li>baz</li>
  4068. </ul>
  4069. ````````````````````````````````
  4070. ```````````````````````````````` example
  4071. 1. foo
  4072. 2. bar
  4073. 3) baz
  4074. .
  4075. <ol>
  4076. <li>foo</li>
  4077. <li>bar</li>
  4078. </ol>
  4079. <ol start="3">
  4080. <li>baz</li>
  4081. </ol>
  4082. ````````````````````````````````
  4083. In CommonMark, a list can interrupt a paragraph. That is,
  4084. no blank line is needed to separate a paragraph from a following
  4085. list:
  4086. ```````````````````````````````` example
  4087. Foo
  4088. - bar
  4089. - baz
  4090. .
  4091. <p>Foo</p>
  4092. <ul>
  4093. <li>bar</li>
  4094. <li>baz</li>
  4095. </ul>
  4096. ````````````````````````````````
  4097. `Markdown.pl` does not allow this, through fear of triggering a list
  4098. via a numeral in a hard-wrapped line:
  4099. ``` markdown
  4100. The number of windows in my house is
  4101. 14. The number of doors is 6.
  4102. ```
  4103. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  4104. interrupt a paragraph, even though the same considerations might
  4105. apply.
  4106. In CommonMark, we do allow lists to interrupt paragraphs, for
  4107. two reasons. First, it is natural and not uncommon for people
  4108. to start lists without blank lines:
  4109. ``` markdown
  4110. I need to buy
  4111. - new shoes
  4112. - a coat
  4113. - a plane ticket
  4114. ```
  4115. Second, we are attracted to a
  4116. > [principle of uniformity](@):
  4117. > if a chunk of text has a certain
  4118. > meaning, it will continue to have the same meaning when put into a
  4119. > container block (such as a list item or blockquote).
  4120. (Indeed, the spec for [list items] and [block quotes] presupposes
  4121. this principle.) This principle implies that if
  4122. ``` markdown
  4123. * I need to buy
  4124. - new shoes
  4125. - a coat
  4126. - a plane ticket
  4127. ```
  4128. is a list item containing a paragraph followed by a nested sublist,
  4129. as all Markdown implementations agree it is (though the paragraph
  4130. may be rendered without `<p>` tags, since the list is "tight"),
  4131. then
  4132. ``` markdown
  4133. I need to buy
  4134. - new shoes
  4135. - a coat
  4136. - a plane ticket
  4137. ```
  4138. by itself should be a paragraph followed by a nested sublist.
  4139. Since it is well established Markdown practice to allow lists to
  4140. interrupt paragraphs inside list items, the [principle of
  4141. uniformity] requires us to allow this outside list items as
  4142. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  4143. takes a different approach, requiring blank lines before lists
  4144. even inside other list items.)
  4145. In order to solve of unwanted lists in paragraphs with
  4146. hard-wrapped numerals, we allow only lists starting with `1` to
  4147. interrupt paragraphs. Thus,
  4148. ```````````````````````````````` example
  4149. The number of windows in my house is
  4150. 14. The number of doors is 6.
  4151. .
  4152. <p>The number of windows in my house is
  4153. 14. The number of doors is 6.</p>
  4154. ````````````````````````````````
  4155. We may still get an unintended result in cases like
  4156. ```````````````````````````````` example
  4157. The number of windows in my house is
  4158. 1. The number of doors is 6.
  4159. .
  4160. <p>The number of windows in my house is</p>
  4161. <ol>
  4162. <li>The number of doors is 6.</li>
  4163. </ol>
  4164. ````````````````````````````````
  4165. but this rule should prevent most spurious list captures.
  4166. There can be any number of blank lines between items:
  4167. ```````````````````````````````` example
  4168. - foo
  4169. - bar
  4170. - baz
  4171. .
  4172. <ul>
  4173. <li>
  4174. <p>foo</p>
  4175. </li>
  4176. <li>
  4177. <p>bar</p>
  4178. </li>
  4179. <li>
  4180. <p>baz</p>
  4181. </li>
  4182. </ul>
  4183. ````````````````````````````````
  4184. ```````````````````````````````` example
  4185. - foo
  4186. - bar
  4187. - baz
  4188. bim
  4189. .
  4190. <ul>
  4191. <li>foo
  4192. <ul>
  4193. <li>bar
  4194. <ul>
  4195. <li>
  4196. <p>baz</p>
  4197. <p>bim</p>
  4198. </li>
  4199. </ul>
  4200. </li>
  4201. </ul>
  4202. </li>
  4203. </ul>
  4204. ````````````````````````````````
  4205. To separate consecutive lists of the same type, or to separate a
  4206. list from an indented code block that would otherwise be parsed
  4207. as a subparagraph of the final list item, you can insert a blank HTML
  4208. comment:
  4209. ```````````````````````````````` example
  4210. - foo
  4211. - bar
  4212. <!-- -->
  4213. - baz
  4214. - bim
  4215. .
  4216. <ul>
  4217. <li>foo</li>
  4218. <li>bar</li>
  4219. </ul>
  4220. <!-- -->
  4221. <ul>
  4222. <li>baz</li>
  4223. <li>bim</li>
  4224. </ul>
  4225. ````````````````````````````````
  4226. ```````````````````````````````` example
  4227. - foo
  4228. notcode
  4229. - foo
  4230. <!-- -->
  4231. code
  4232. .
  4233. <ul>
  4234. <li>
  4235. <p>foo</p>
  4236. <p>notcode</p>
  4237. </li>
  4238. <li>
  4239. <p>foo</p>
  4240. </li>
  4241. </ul>
  4242. <!-- -->
  4243. <pre><code>code
  4244. </code></pre>
  4245. ````````````````````````````````
  4246. List items need not be indented to the same level. The following
  4247. list items will be treated as items at the same list level,
  4248. since none is indented enough to belong to the previous list
  4249. item:
  4250. ```````````````````````````````` example
  4251. - a
  4252. - b
  4253. - c
  4254. - d
  4255. - e
  4256. - f
  4257. - g
  4258. .
  4259. <ul>
  4260. <li>a</li>
  4261. <li>b</li>
  4262. <li>c</li>
  4263. <li>d</li>
  4264. <li>e</li>
  4265. <li>f</li>
  4266. <li>g</li>
  4267. </ul>
  4268. ````````````````````````````````
  4269. ```````````````````````````````` example
  4270. 1. a
  4271. 2. b
  4272. 3. c
  4273. .
  4274. <ol>
  4275. <li>
  4276. <p>a</p>
  4277. </li>
  4278. <li>
  4279. <p>b</p>
  4280. </li>
  4281. <li>
  4282. <p>c</p>
  4283. </li>
  4284. </ol>
  4285. ````````````````````````````````
  4286. Note, however, that list items may not be preceded by more than
  4287. three spaces of indentation. Here `- e` is treated as a paragraph continuation
  4288. line, because it is indented more than three spaces:
  4289. ```````````````````````````````` example
  4290. - a
  4291. - b
  4292. - c
  4293. - d
  4294. - e
  4295. .
  4296. <ul>
  4297. <li>a</li>
  4298. <li>b</li>
  4299. <li>c</li>
  4300. <li>d
  4301. - e</li>
  4302. </ul>
  4303. ````````````````````````````````
  4304. And here, `3. c` is treated as in indented code block,
  4305. because it is indented four spaces and preceded by a
  4306. blank line.
  4307. ```````````````````````````````` example
  4308. 1. a
  4309. 2. b
  4310. 3. c
  4311. .
  4312. <ol>
  4313. <li>
  4314. <p>a</p>
  4315. </li>
  4316. <li>
  4317. <p>b</p>
  4318. </li>
  4319. </ol>
  4320. <pre><code>3. c
  4321. </code></pre>
  4322. ````````````````````````````````
  4323. This is a loose list, because there is a blank line between
  4324. two of the list items:
  4325. ```````````````````````````````` example
  4326. - a
  4327. - b
  4328. - c
  4329. .
  4330. <ul>
  4331. <li>
  4332. <p>a</p>
  4333. </li>
  4334. <li>
  4335. <p>b</p>
  4336. </li>
  4337. <li>
  4338. <p>c</p>
  4339. </li>
  4340. </ul>
  4341. ````````````````````````````````
  4342. So is this, with a empty second item:
  4343. ```````````````````````````````` example
  4344. * a
  4345. *
  4346. * c
  4347. .
  4348. <ul>
  4349. <li>
  4350. <p>a</p>
  4351. </li>
  4352. <li></li>
  4353. <li>
  4354. <p>c</p>
  4355. </li>
  4356. </ul>
  4357. ````````````````````````````````
  4358. These are loose lists, even though there are no blank lines between the items,
  4359. because one of the items directly contains two block-level elements
  4360. with a blank line between them:
  4361. ```````````````````````````````` example
  4362. - a
  4363. - b
  4364. c
  4365. - d
  4366. .
  4367. <ul>
  4368. <li>
  4369. <p>a</p>
  4370. </li>
  4371. <li>
  4372. <p>b</p>
  4373. <p>c</p>
  4374. </li>
  4375. <li>
  4376. <p>d</p>
  4377. </li>
  4378. </ul>
  4379. ````````````````````````````````
  4380. ```````````````````````````````` example
  4381. - a
  4382. - b
  4383. [ref]: /url
  4384. - d
  4385. .
  4386. <ul>
  4387. <li>
  4388. <p>a</p>
  4389. </li>
  4390. <li>
  4391. <p>b</p>
  4392. </li>
  4393. <li>
  4394. <p>d</p>
  4395. </li>
  4396. </ul>
  4397. ````````````````````````````````
  4398. This is a tight list, because the blank lines are in a code block:
  4399. ```````````````````````````````` example
  4400. - a
  4401. - ```
  4402. b
  4403. ```
  4404. - c
  4405. .
  4406. <ul>
  4407. <li>a</li>
  4408. <li>
  4409. <pre><code>b
  4410. </code></pre>
  4411. </li>
  4412. <li>c</li>
  4413. </ul>
  4414. ````````````````````````````````
  4415. This is a tight list, because the blank line is between two
  4416. paragraphs of a sublist. So the sublist is loose while
  4417. the outer list is tight:
  4418. ```````````````````````````````` example
  4419. - a
  4420. - b
  4421. c
  4422. - d
  4423. .
  4424. <ul>
  4425. <li>a
  4426. <ul>
  4427. <li>
  4428. <p>b</p>
  4429. <p>c</p>
  4430. </li>
  4431. </ul>
  4432. </li>
  4433. <li>d</li>
  4434. </ul>
  4435. ````````````````````````````````
  4436. This is a tight list, because the blank line is inside the
  4437. block quote:
  4438. ```````````````````````````````` example
  4439. * a
  4440. > b
  4441. >
  4442. * c
  4443. .
  4444. <ul>
  4445. <li>a
  4446. <blockquote>
  4447. <p>b</p>
  4448. </blockquote>
  4449. </li>
  4450. <li>c</li>
  4451. </ul>
  4452. ````````````````````````````````
  4453. This list is tight, because the consecutive block elements
  4454. are not separated by blank lines:
  4455. ```````````````````````````````` example
  4456. - a
  4457. > b
  4458. ```
  4459. c
  4460. ```
  4461. - d
  4462. .
  4463. <ul>
  4464. <li>a
  4465. <blockquote>
  4466. <p>b</p>
  4467. </blockquote>
  4468. <pre><code>c
  4469. </code></pre>
  4470. </li>
  4471. <li>d</li>
  4472. </ul>
  4473. ````````````````````````````````
  4474. A single-paragraph list is tight:
  4475. ```````````````````````````````` example
  4476. - a
  4477. .
  4478. <ul>
  4479. <li>a</li>
  4480. </ul>
  4481. ````````````````````````````````
  4482. ```````````````````````````````` example
  4483. - a
  4484. - b
  4485. .
  4486. <ul>
  4487. <li>a
  4488. <ul>
  4489. <li>b</li>
  4490. </ul>
  4491. </li>
  4492. </ul>
  4493. ````````````````````````````````
  4494. This list is loose, because of the blank line between the
  4495. two block elements in the list item:
  4496. ```````````````````````````````` example
  4497. 1. ```
  4498. foo
  4499. ```
  4500. bar
  4501. .
  4502. <ol>
  4503. <li>
  4504. <pre><code>foo
  4505. </code></pre>
  4506. <p>bar</p>
  4507. </li>
  4508. </ol>
  4509. ````````````````````````````````
  4510. Here the outer list is loose, the inner list tight:
  4511. ```````````````````````````````` example
  4512. * foo
  4513. * bar
  4514. baz
  4515. .
  4516. <ul>
  4517. <li>
  4518. <p>foo</p>
  4519. <ul>
  4520. <li>bar</li>
  4521. </ul>
  4522. <p>baz</p>
  4523. </li>
  4524. </ul>
  4525. ````````````````````````````````
  4526. ```````````````````````````````` example
  4527. - a
  4528. - b
  4529. - c
  4530. - d
  4531. - e
  4532. - f
  4533. .
  4534. <ul>
  4535. <li>
  4536. <p>a</p>
  4537. <ul>
  4538. <li>b</li>
  4539. <li>c</li>
  4540. </ul>
  4541. </li>
  4542. <li>
  4543. <p>d</p>
  4544. <ul>
  4545. <li>e</li>
  4546. <li>f</li>
  4547. </ul>
  4548. </li>
  4549. </ul>
  4550. ````````````````````````````````
  4551. # Inlines
  4552. Inlines are parsed sequentially from the beginning of the character
  4553. stream to the end (left to right, in left-to-right languages).
  4554. Thus, for example, in
  4555. ```````````````````````````````` example
  4556. `hi`lo`
  4557. .
  4558. <p><code>hi</code>lo`</p>
  4559. ````````````````````````````````
  4560. `hi` is parsed as code, leaving the backtick at the end as a literal
  4561. backtick.
  4562. ## Code spans
  4563. A [backtick string](@)
  4564. is a string of one or more backtick characters (`` ` ``) that is neither
  4565. preceded nor followed by a backtick.
  4566. A [code span](@) begins with a backtick string and ends with
  4567. a backtick string of equal length. The contents of the code span are
  4568. the characters between these two backtick strings, normalized in the
  4569. following ways:
  4570. - First, [line endings] are converted to [spaces].
  4571. - If the resulting string both begins *and* ends with a [space]
  4572. character, but does not consist entirely of [space]
  4573. characters, a single [space] character is removed from the
  4574. front and back. This allows you to include code that begins
  4575. or ends with backtick characters, which must be separated by
  4576. whitespace from the opening or closing backtick strings.
  4577. This is a simple code span:
  4578. ```````````````````````````````` example
  4579. `foo`
  4580. .
  4581. <p><code>foo</code></p>
  4582. ````````````````````````````````
  4583. Here two backticks are used, because the code contains a backtick.
  4584. This example also illustrates stripping of a single leading and
  4585. trailing space:
  4586. ```````````````````````````````` example
  4587. `` foo ` bar ``
  4588. .
  4589. <p><code>foo ` bar</code></p>
  4590. ````````````````````````````````
  4591. This example shows the motivation for stripping leading and trailing
  4592. spaces:
  4593. ```````````````````````````````` example
  4594. ` `` `
  4595. .
  4596. <p><code>``</code></p>
  4597. ````````````````````````````````
  4598. Note that only *one* space is stripped:
  4599. ```````````````````````````````` example
  4600. ` `` `
  4601. .
  4602. <p><code> `` </code></p>
  4603. ````````````````````````````````
  4604. The stripping only happens if the space is on both
  4605. sides of the string:
  4606. ```````````````````````````````` example
  4607. ` a`
  4608. .
  4609. <p><code> a</code></p>
  4610. ````````````````````````````````
  4611. Only [spaces], and not [unicode whitespace] in general, are
  4612. stripped in this way:
  4613. ```````````````````````````````` example
  4614. ` b `
  4615. .
  4616. <p><code> b </code></p>
  4617. ````````````````````````````````
  4618. No stripping occurs if the code span contains only spaces:
  4619. ```````````````````````````````` example
  4620. ` `
  4621. ` `
  4622. .
  4623. <p><code> </code>
  4624. <code> </code></p>
  4625. ````````````````````````````````
  4626. [Line endings] are treated like spaces:
  4627. ```````````````````````````````` example
  4628. ``
  4629. foo
  4630. bar
  4631. baz
  4632. ``
  4633. .
  4634. <p><code>foo bar baz</code></p>
  4635. ````````````````````````````````
  4636. ```````````````````````````````` example
  4637. ``
  4638. foo
  4639. ``
  4640. .
  4641. <p><code>foo </code></p>
  4642. ````````````````````````````````
  4643. Interior spaces are not collapsed:
  4644. ```````````````````````````````` example
  4645. `foo bar
  4646. baz`
  4647. .
  4648. <p><code>foo bar baz</code></p>
  4649. ````````````````````````````````
  4650. Note that browsers will typically collapse consecutive spaces
  4651. when rendering `<code>` elements, so it is recommended that
  4652. the following CSS be used:
  4653. code{white-space: pre-wrap;}
  4654. Note that backslash escapes do not work in code spans. All backslashes
  4655. are treated literally:
  4656. ```````````````````````````````` example
  4657. `foo\`bar`
  4658. .
  4659. <p><code>foo\</code>bar`</p>
  4660. ````````````````````````````````
  4661. Backslash escapes are never needed, because one can always choose a
  4662. string of *n* backtick characters as delimiters, where the code does
  4663. not contain any strings of exactly *n* backtick characters.
  4664. ```````````````````````````````` example
  4665. ``foo`bar``
  4666. .
  4667. <p><code>foo`bar</code></p>
  4668. ````````````````````````````````
  4669. ```````````````````````````````` example
  4670. ` foo `` bar `
  4671. .
  4672. <p><code>foo `` bar</code></p>
  4673. ````````````````````````````````
  4674. Code span backticks have higher precedence than any other inline
  4675. constructs except HTML tags and autolinks. Thus, for example, this is
  4676. not parsed as emphasized text, since the second `*` is part of a code
  4677. span:
  4678. ```````````````````````````````` example
  4679. *foo`*`
  4680. .
  4681. <p>*foo<code>*</code></p>
  4682. ````````````````````````````````
  4683. And this is not parsed as a link:
  4684. ```````````````````````````````` example
  4685. [not a `link](/foo`)
  4686. .
  4687. <p>[not a <code>link](/foo</code>)</p>
  4688. ````````````````````````````````
  4689. Code spans, HTML tags, and autolinks have the same precedence.
  4690. Thus, this is code:
  4691. ```````````````````````````````` example
  4692. `<a href="`">`
  4693. .
  4694. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4695. ````````````````````````````````
  4696. But this is an HTML tag:
  4697. ```````````````````````````````` example
  4698. <a href="`">`
  4699. .
  4700. <p><a href="`">`</p>
  4701. ````````````````````````````````
  4702. And this is code:
  4703. ```````````````````````````````` example
  4704. `<http://foo.bar.`baz>`
  4705. .
  4706. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4707. ````````````````````````````````
  4708. But this is an autolink:
  4709. ```````````````````````````````` example
  4710. <http://foo.bar.`baz>`
  4711. .
  4712. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4713. ````````````````````````````````
  4714. When a backtick string is not closed by a matching backtick string,
  4715. we just have literal backticks:
  4716. ```````````````````````````````` example
  4717. ```foo``
  4718. .
  4719. <p>```foo``</p>
  4720. ````````````````````````````````
  4721. ```````````````````````````````` example
  4722. `foo
  4723. .
  4724. <p>`foo</p>
  4725. ````````````````````````````````
  4726. The following case also illustrates the need for opening and
  4727. closing backtick strings to be equal in length:
  4728. ```````````````````````````````` example
  4729. `foo``bar``
  4730. .
  4731. <p>`foo<code>bar</code></p>
  4732. ````````````````````````````````
  4733. ## Emphasis and strong emphasis
  4734. John Gruber's original [Markdown syntax
  4735. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4736. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4737. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4738. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4739. > tag.
  4740. This is enough for most users, but these rules leave much undecided,
  4741. especially when it comes to nested emphasis. The original
  4742. `Markdown.pl` test suite makes it clear that triple `***` and
  4743. `___` delimiters can be used for strong emphasis, and most
  4744. implementations have also allowed the following patterns:
  4745. ``` markdown
  4746. ***strong emph***
  4747. ***strong** in emph*
  4748. ***emph* in strong**
  4749. **in strong *emph***
  4750. *in emph **strong***
  4751. ```
  4752. The following patterns are less widely supported, but the intent
  4753. is clear and they are useful (especially in contexts like bibliography
  4754. entries):
  4755. ``` markdown
  4756. *emph *with emph* in it*
  4757. **strong **with strong** in it**
  4758. ```
  4759. Many implementations have also restricted intraword emphasis to
  4760. the `*` forms, to avoid unwanted emphasis in words containing
  4761. internal underscores. (It is best practice to put these in code
  4762. spans, but users often do not.)
  4763. ``` markdown
  4764. internal emphasis: foo*bar*baz
  4765. no emphasis: foo_bar_baz
  4766. ```
  4767. The rules given below capture all of these patterns, while allowing
  4768. for efficient parsing strategies that do not backtrack.
  4769. First, some definitions. A [delimiter run](@) is either
  4770. a sequence of one or more `*` characters that is not preceded or
  4771. followed by a non-backslash-escaped `*` character, or a sequence
  4772. of one or more `_` characters that is not preceded or followed by
  4773. a non-backslash-escaped `_` character.
  4774. A [left-flanking delimiter run](@) is
  4775. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4776. and either (2a) not followed by a [Unicode punctuation character], or
  4777. (2b) followed by a [Unicode punctuation character] and
  4778. preceded by [Unicode whitespace] or a [Unicode punctuation character].
  4779. For purposes of this definition, the beginning and the end of
  4780. the line count as Unicode whitespace.
  4781. A [right-flanking delimiter run](@) is
  4782. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4783. and either (2a) not preceded by a [Unicode punctuation character], or
  4784. (2b) preceded by a [Unicode punctuation character] and
  4785. followed by [Unicode whitespace] or a [Unicode punctuation character].
  4786. For purposes of this definition, the beginning and the end of
  4787. the line count as Unicode whitespace.
  4788. Here are some examples of delimiter runs.
  4789. - left-flanking but not right-flanking:
  4790. ```
  4791. ***abc
  4792. _abc
  4793. **"abc"
  4794. _"abc"
  4795. ```
  4796. - right-flanking but not left-flanking:
  4797. ```
  4798. abc***
  4799. abc_
  4800. "abc"**
  4801. "abc"_
  4802. ```
  4803. - Both left and right-flanking:
  4804. ```
  4805. abc***def
  4806. "abc"_"def"
  4807. ```
  4808. - Neither left nor right-flanking:
  4809. ```
  4810. abc *** def
  4811. a _ b
  4812. ```
  4813. (The idea of distinguishing left-flanking and right-flanking
  4814. delimiter runs based on the character before and the character
  4815. after comes from Roopesh Chander's
  4816. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4817. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4818. run," and its rules for distinguishing left- and right-flanking runs
  4819. are a bit more complex than the ones given here.)
  4820. The following rules define emphasis and strong emphasis:
  4821. 1. A single `*` character [can open emphasis](@)
  4822. iff (if and only if) it is part of a [left-flanking delimiter run].
  4823. 2. A single `_` character [can open emphasis] iff
  4824. it is part of a [left-flanking delimiter run]
  4825. and either (a) not part of a [right-flanking delimiter run]
  4826. or (b) part of a [right-flanking delimiter run]
  4827. preceded by a [Unicode punctuation character].
  4828. 3. A single `*` character [can close emphasis](@)
  4829. iff it is part of a [right-flanking delimiter run].
  4830. 4. A single `_` character [can close emphasis] iff
  4831. it is part of a [right-flanking delimiter run]
  4832. and either (a) not part of a [left-flanking delimiter run]
  4833. or (b) part of a [left-flanking delimiter run]
  4834. followed by a [Unicode punctuation character].
  4835. 5. A double `**` [can open strong emphasis](@)
  4836. iff it is part of a [left-flanking delimiter run].
  4837. 6. A double `__` [can open strong emphasis] iff
  4838. it is part of a [left-flanking delimiter run]
  4839. and either (a) not part of a [right-flanking delimiter run]
  4840. or (b) part of a [right-flanking delimiter run]
  4841. preceded by a [Unicode punctuation character].
  4842. 7. A double `**` [can close strong emphasis](@)
  4843. iff it is part of a [right-flanking delimiter run].
  4844. 8. A double `__` [can close strong emphasis] iff
  4845. it is part of a [right-flanking delimiter run]
  4846. and either (a) not part of a [left-flanking delimiter run]
  4847. or (b) part of a [left-flanking delimiter run]
  4848. followed by a [Unicode punctuation character].
  4849. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4850. with a delimiter that [can close emphasis], and that uses the same
  4851. character (`_` or `*`) as the opening delimiter. The
  4852. opening and closing delimiters must belong to separate
  4853. [delimiter runs]. If one of the delimiters can both
  4854. open and close emphasis, then the sum of the lengths of the
  4855. delimiter runs containing the opening and closing delimiters
  4856. must not be a multiple of 3 unless both lengths are
  4857. multiples of 3.
  4858. 10. Strong emphasis begins with a delimiter that
  4859. [can open strong emphasis] and ends with a delimiter that
  4860. [can close strong emphasis], and that uses the same character
  4861. (`_` or `*`) as the opening delimiter. The
  4862. opening and closing delimiters must belong to separate
  4863. [delimiter runs]. If one of the delimiters can both open
  4864. and close strong emphasis, then the sum of the lengths of
  4865. the delimiter runs containing the opening and closing
  4866. delimiters must not be a multiple of 3 unless both lengths
  4867. are multiples of 3.
  4868. 11. A literal `*` character cannot occur at the beginning or end of
  4869. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4870. is backslash-escaped.
  4871. 12. A literal `_` character cannot occur at the beginning or end of
  4872. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4873. is backslash-escaped.
  4874. Where rules 1--12 above are compatible with multiple parsings,
  4875. the following principles resolve ambiguity:
  4876. 13. The number of nestings should be minimized. Thus, for example,
  4877. an interpretation `<strong>...</strong>` is always preferred to
  4878. `<em><em>...</em></em>`.
  4879. 14. An interpretation `<em><strong>...</strong></em>` is always
  4880. preferred to `<strong><em>...</em></strong>`.
  4881. 15. When two potential emphasis or strong emphasis spans overlap,
  4882. so that the second begins before the first ends and ends after
  4883. the first ends, the first takes precedence. Thus, for example,
  4884. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4885. than `*foo <em>bar* baz</em>`.
  4886. 16. When there are two potential emphasis or strong emphasis spans
  4887. with the same closing delimiter, the shorter one (the one that
  4888. opens later) takes precedence. Thus, for example,
  4889. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4890. rather than `<strong>foo **bar baz</strong>`.
  4891. 17. Inline code spans, links, images, and HTML tags group more tightly
  4892. than emphasis. So, when there is a choice between an interpretation
  4893. that contains one of these elements and one that does not, the
  4894. former always wins. Thus, for example, `*[foo*](bar)` is
  4895. parsed as `*<a href="bar">foo*</a>` rather than as
  4896. `<em>[foo</em>](bar)`.
  4897. These rules can be illustrated through a series of examples.
  4898. Rule 1:
  4899. ```````````````````````````````` example
  4900. *foo bar*
  4901. .
  4902. <p><em>foo bar</em></p>
  4903. ````````````````````````````````
  4904. This is not emphasis, because the opening `*` is followed by
  4905. whitespace, and hence not part of a [left-flanking delimiter run]:
  4906. ```````````````````````````````` example
  4907. a * foo bar*
  4908. .
  4909. <p>a * foo bar*</p>
  4910. ````````````````````````````````
  4911. This is not emphasis, because the opening `*` is preceded
  4912. by an alphanumeric and followed by punctuation, and hence
  4913. not part of a [left-flanking delimiter run]:
  4914. ```````````````````````````````` example
  4915. a*"foo"*
  4916. .
  4917. <p>a*&quot;foo&quot;*</p>
  4918. ````````````````````````````````
  4919. Unicode nonbreaking spaces count as whitespace, too:
  4920. ```````````````````````````````` example
  4921. * a *
  4922. .
  4923. <p>* a *</p>
  4924. ````````````````````````````````
  4925. Intraword emphasis with `*` is permitted:
  4926. ```````````````````````````````` example
  4927. foo*bar*
  4928. .
  4929. <p>foo<em>bar</em></p>
  4930. ````````````````````````````````
  4931. ```````````````````````````````` example
  4932. 5*6*78
  4933. .
  4934. <p>5<em>6</em>78</p>
  4935. ````````````````````````````````
  4936. Rule 2:
  4937. ```````````````````````````````` example
  4938. _foo bar_
  4939. .
  4940. <p><em>foo bar</em></p>
  4941. ````````````````````````````````
  4942. This is not emphasis, because the opening `_` is followed by
  4943. whitespace:
  4944. ```````````````````````````````` example
  4945. _ foo bar_
  4946. .
  4947. <p>_ foo bar_</p>
  4948. ````````````````````````````````
  4949. This is not emphasis, because the opening `_` is preceded
  4950. by an alphanumeric and followed by punctuation:
  4951. ```````````````````````````````` example
  4952. a_"foo"_
  4953. .
  4954. <p>a_&quot;foo&quot;_</p>
  4955. ````````````````````````````````
  4956. Emphasis with `_` is not allowed inside words:
  4957. ```````````````````````````````` example
  4958. foo_bar_
  4959. .
  4960. <p>foo_bar_</p>
  4961. ````````````````````````````````
  4962. ```````````````````````````````` example
  4963. 5_6_78
  4964. .
  4965. <p>5_6_78</p>
  4966. ````````````````````````````````
  4967. ```````````````````````````````` example
  4968. пристаням_стремятся_
  4969. .
  4970. <p>пристаням_стремятся_</p>
  4971. ````````````````````````````````
  4972. Here `_` does not generate emphasis, because the first delimiter run
  4973. is right-flanking and the second left-flanking:
  4974. ```````````````````````````````` example
  4975. aa_"bb"_cc
  4976. .
  4977. <p>aa_&quot;bb&quot;_cc</p>
  4978. ````````````````````````````````
  4979. This is emphasis, even though the opening delimiter is
  4980. both left- and right-flanking, because it is preceded by
  4981. punctuation:
  4982. ```````````````````````````````` example
  4983. foo-_(bar)_
  4984. .
  4985. <p>foo-<em>(bar)</em></p>
  4986. ````````````````````````````````
  4987. Rule 3:
  4988. This is not emphasis, because the closing delimiter does
  4989. not match the opening delimiter:
  4990. ```````````````````````````````` example
  4991. _foo*
  4992. .
  4993. <p>_foo*</p>
  4994. ````````````````````````````````
  4995. This is not emphasis, because the closing `*` is preceded by
  4996. whitespace:
  4997. ```````````````````````````````` example
  4998. *foo bar *
  4999. .
  5000. <p>*foo bar *</p>
  5001. ````````````````````````````````
  5002. A line ending also counts as whitespace:
  5003. ```````````````````````````````` example
  5004. *foo bar
  5005. *
  5006. .
  5007. <p>*foo bar
  5008. *</p>
  5009. ````````````````````````````````
  5010. This is not emphasis, because the second `*` is
  5011. preceded by punctuation and followed by an alphanumeric
  5012. (hence it is not part of a [right-flanking delimiter run]:
  5013. ```````````````````````````````` example
  5014. *(*foo)
  5015. .
  5016. <p>*(*foo)</p>
  5017. ````````````````````````````````
  5018. The point of this restriction is more easily appreciated
  5019. with this example:
  5020. ```````````````````````````````` example
  5021. *(*foo*)*
  5022. .
  5023. <p><em>(<em>foo</em>)</em></p>
  5024. ````````````````````````````````
  5025. Intraword emphasis with `*` is allowed:
  5026. ```````````````````````````````` example
  5027. *foo*bar
  5028. .
  5029. <p><em>foo</em>bar</p>
  5030. ````````````````````````````````
  5031. Rule 4:
  5032. This is not emphasis, because the closing `_` is preceded by
  5033. whitespace:
  5034. ```````````````````````````````` example
  5035. _foo bar _
  5036. .
  5037. <p>_foo bar _</p>
  5038. ````````````````````````````````
  5039. This is not emphasis, because the second `_` is
  5040. preceded by punctuation and followed by an alphanumeric:
  5041. ```````````````````````````````` example
  5042. _(_foo)
  5043. .
  5044. <p>_(_foo)</p>
  5045. ````````````````````````````````
  5046. This is emphasis within emphasis:
  5047. ```````````````````````````````` example
  5048. _(_foo_)_
  5049. .
  5050. <p><em>(<em>foo</em>)</em></p>
  5051. ````````````````````````````````
  5052. Intraword emphasis is disallowed for `_`:
  5053. ```````````````````````````````` example
  5054. _foo_bar
  5055. .
  5056. <p>_foo_bar</p>
  5057. ````````````````````````````````
  5058. ```````````````````````````````` example
  5059. _пристаням_стремятся
  5060. .
  5061. <p>_пристаням_стремятся</p>
  5062. ````````````````````````````````
  5063. ```````````````````````````````` example
  5064. _foo_bar_baz_
  5065. .
  5066. <p><em>foo_bar_baz</em></p>
  5067. ````````````````````````````````
  5068. This is emphasis, even though the closing delimiter is
  5069. both left- and right-flanking, because it is followed by
  5070. punctuation:
  5071. ```````````````````````````````` example
  5072. _(bar)_.
  5073. .
  5074. <p><em>(bar)</em>.</p>
  5075. ````````````````````````````````
  5076. Rule 5:
  5077. ```````````````````````````````` example
  5078. **foo bar**
  5079. .
  5080. <p><strong>foo bar</strong></p>
  5081. ````````````````````````````````
  5082. This is not strong emphasis, because the opening delimiter is
  5083. followed by whitespace:
  5084. ```````````````````````````````` example
  5085. ** foo bar**
  5086. .
  5087. <p>** foo bar**</p>
  5088. ````````````````````````````````
  5089. This is not strong emphasis, because the opening `**` is preceded
  5090. by an alphanumeric and followed by punctuation, and hence
  5091. not part of a [left-flanking delimiter run]:
  5092. ```````````````````````````````` example
  5093. a**"foo"**
  5094. .
  5095. <p>a**&quot;foo&quot;**</p>
  5096. ````````````````````````````````
  5097. Intraword strong emphasis with `**` is permitted:
  5098. ```````````````````````````````` example
  5099. foo**bar**
  5100. .
  5101. <p>foo<strong>bar</strong></p>
  5102. ````````````````````````````````
  5103. Rule 6:
  5104. ```````````````````````````````` example
  5105. __foo bar__
  5106. .
  5107. <p><strong>foo bar</strong></p>
  5108. ````````````````````````````````
  5109. This is not strong emphasis, because the opening delimiter is
  5110. followed by whitespace:
  5111. ```````````````````````````````` example
  5112. __ foo bar__
  5113. .
  5114. <p>__ foo bar__</p>
  5115. ````````````````````````````````
  5116. A line ending counts as whitespace:
  5117. ```````````````````````````````` example
  5118. __
  5119. foo bar__
  5120. .
  5121. <p>__
  5122. foo bar__</p>
  5123. ````````````````````````````````
  5124. This is not strong emphasis, because the opening `__` is preceded
  5125. by an alphanumeric and followed by punctuation:
  5126. ```````````````````````````````` example
  5127. a__"foo"__
  5128. .
  5129. <p>a__&quot;foo&quot;__</p>
  5130. ````````````````````````````````
  5131. Intraword strong emphasis is forbidden with `__`:
  5132. ```````````````````````````````` example
  5133. foo__bar__
  5134. .
  5135. <p>foo__bar__</p>
  5136. ````````````````````````````````
  5137. ```````````````````````````````` example
  5138. 5__6__78
  5139. .
  5140. <p>5__6__78</p>
  5141. ````````````````````````````````
  5142. ```````````````````````````````` example
  5143. пристаням__стремятся__
  5144. .
  5145. <p>пристаням__стремятся__</p>
  5146. ````````````````````````````````
  5147. ```````````````````````````````` example
  5148. __foo, __bar__, baz__
  5149. .
  5150. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5151. ````````````````````````````````
  5152. This is strong emphasis, even though the opening delimiter is
  5153. both left- and right-flanking, because it is preceded by
  5154. punctuation:
  5155. ```````````````````````````````` example
  5156. foo-__(bar)__
  5157. .
  5158. <p>foo-<strong>(bar)</strong></p>
  5159. ````````````````````````````````
  5160. Rule 7:
  5161. This is not strong emphasis, because the closing delimiter is preceded
  5162. by whitespace:
  5163. ```````````````````````````````` example
  5164. **foo bar **
  5165. .
  5166. <p>**foo bar **</p>
  5167. ````````````````````````````````
  5168. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5169. Rule 11.)
  5170. This is not strong emphasis, because the second `**` is
  5171. preceded by punctuation and followed by an alphanumeric:
  5172. ```````````````````````````````` example
  5173. **(**foo)
  5174. .
  5175. <p>**(**foo)</p>
  5176. ````````````````````````````````
  5177. The point of this restriction is more easily appreciated
  5178. with these examples:
  5179. ```````````````````````````````` example
  5180. *(**foo**)*
  5181. .
  5182. <p><em>(<strong>foo</strong>)</em></p>
  5183. ````````````````````````````````
  5184. ```````````````````````````````` example
  5185. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5186. *Asclepias physocarpa*)**
  5187. .
  5188. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5189. <em>Asclepias physocarpa</em>)</strong></p>
  5190. ````````````````````````````````
  5191. ```````````````````````````````` example
  5192. **foo "*bar*" foo**
  5193. .
  5194. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5195. ````````````````````````````````
  5196. Intraword emphasis:
  5197. ```````````````````````````````` example
  5198. **foo**bar
  5199. .
  5200. <p><strong>foo</strong>bar</p>
  5201. ````````````````````````````````
  5202. Rule 8:
  5203. This is not strong emphasis, because the closing delimiter is
  5204. preceded by whitespace:
  5205. ```````````````````````````````` example
  5206. __foo bar __
  5207. .
  5208. <p>__foo bar __</p>
  5209. ````````````````````````````````
  5210. This is not strong emphasis, because the second `__` is
  5211. preceded by punctuation and followed by an alphanumeric:
  5212. ```````````````````````````````` example
  5213. __(__foo)
  5214. .
  5215. <p>__(__foo)</p>
  5216. ````````````````````````````````
  5217. The point of this restriction is more easily appreciated
  5218. with this example:
  5219. ```````````````````````````````` example
  5220. _(__foo__)_
  5221. .
  5222. <p><em>(<strong>foo</strong>)</em></p>
  5223. ````````````````````````````````
  5224. Intraword strong emphasis is forbidden with `__`:
  5225. ```````````````````````````````` example
  5226. __foo__bar
  5227. .
  5228. <p>__foo__bar</p>
  5229. ````````````````````````````````
  5230. ```````````````````````````````` example
  5231. __пристаням__стремятся
  5232. .
  5233. <p>__пристаням__стремятся</p>
  5234. ````````````````````````````````
  5235. ```````````````````````````````` example
  5236. __foo__bar__baz__
  5237. .
  5238. <p><strong>foo__bar__baz</strong></p>
  5239. ````````````````````````````````
  5240. This is strong emphasis, even though the closing delimiter is
  5241. both left- and right-flanking, because it is followed by
  5242. punctuation:
  5243. ```````````````````````````````` example
  5244. __(bar)__.
  5245. .
  5246. <p><strong>(bar)</strong>.</p>
  5247. ````````````````````````````````
  5248. Rule 9:
  5249. Any nonempty sequence of inline elements can be the contents of an
  5250. emphasized span.
  5251. ```````````````````````````````` example
  5252. *foo [bar](/url)*
  5253. .
  5254. <p><em>foo <a href="/url">bar</a></em></p>
  5255. ````````````````````````````````
  5256. ```````````````````````````````` example
  5257. *foo
  5258. bar*
  5259. .
  5260. <p><em>foo
  5261. bar</em></p>
  5262. ````````````````````````````````
  5263. In particular, emphasis and strong emphasis can be nested
  5264. inside emphasis:
  5265. ```````````````````````````````` example
  5266. _foo __bar__ baz_
  5267. .
  5268. <p><em>foo <strong>bar</strong> baz</em></p>
  5269. ````````````````````````````````
  5270. ```````````````````````````````` example
  5271. _foo _bar_ baz_
  5272. .
  5273. <p><em>foo <em>bar</em> baz</em></p>
  5274. ````````````````````````````````
  5275. ```````````````````````````````` example
  5276. __foo_ bar_
  5277. .
  5278. <p><em><em>foo</em> bar</em></p>
  5279. ````````````````````````````````
  5280. ```````````````````````````````` example
  5281. *foo *bar**
  5282. .
  5283. <p><em>foo <em>bar</em></em></p>
  5284. ````````````````````````````````
  5285. ```````````````````````````````` example
  5286. *foo **bar** baz*
  5287. .
  5288. <p><em>foo <strong>bar</strong> baz</em></p>
  5289. ````````````````````````````````
  5290. ```````````````````````````````` example
  5291. *foo**bar**baz*
  5292. .
  5293. <p><em>foo<strong>bar</strong>baz</em></p>
  5294. ````````````````````````````````
  5295. Note that in the preceding case, the interpretation
  5296. ``` markdown
  5297. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5298. ```
  5299. is precluded by the condition that a delimiter that
  5300. can both open and close (like the `*` after `foo`)
  5301. cannot form emphasis if the sum of the lengths of
  5302. the delimiter runs containing the opening and
  5303. closing delimiters is a multiple of 3 unless
  5304. both lengths are multiples of 3.
  5305. For the same reason, we don't get two consecutive
  5306. emphasis sections in this example:
  5307. ```````````````````````````````` example
  5308. *foo**bar*
  5309. .
  5310. <p><em>foo**bar</em></p>
  5311. ````````````````````````````````
  5312. The same condition ensures that the following
  5313. cases are all strong emphasis nested inside
  5314. emphasis, even when the interior whitespace is
  5315. omitted:
  5316. ```````````````````````````````` example
  5317. ***foo** bar*
  5318. .
  5319. <p><em><strong>foo</strong> bar</em></p>
  5320. ````````````````````````````````
  5321. ```````````````````````````````` example
  5322. *foo **bar***
  5323. .
  5324. <p><em>foo <strong>bar</strong></em></p>
  5325. ````````````````````````````````
  5326. ```````````````````````````````` example
  5327. *foo**bar***
  5328. .
  5329. <p><em>foo<strong>bar</strong></em></p>
  5330. ````````````````````````````````
  5331. When the lengths of the interior closing and opening
  5332. delimiter runs are *both* multiples of 3, though,
  5333. they can match to create emphasis:
  5334. ```````````````````````````````` example
  5335. foo***bar***baz
  5336. .
  5337. <p>foo<em><strong>bar</strong></em>baz</p>
  5338. ````````````````````````````````
  5339. ```````````````````````````````` example
  5340. foo******bar*********baz
  5341. .
  5342. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5343. ````````````````````````````````
  5344. Indefinite levels of nesting are possible:
  5345. ```````````````````````````````` example
  5346. *foo **bar *baz* bim** bop*
  5347. .
  5348. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5349. ````````````````````````````````
  5350. ```````````````````````````````` example
  5351. *foo [*bar*](/url)*
  5352. .
  5353. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5354. ````````````````````````````````
  5355. There can be no empty emphasis or strong emphasis:
  5356. ```````````````````````````````` example
  5357. ** is not an empty emphasis
  5358. .
  5359. <p>** is not an empty emphasis</p>
  5360. ````````````````````````````````
  5361. ```````````````````````````````` example
  5362. **** is not an empty strong emphasis
  5363. .
  5364. <p>**** is not an empty strong emphasis</p>
  5365. ````````````````````````````````
  5366. Rule 10:
  5367. Any nonempty sequence of inline elements can be the contents of an
  5368. strongly emphasized span.
  5369. ```````````````````````````````` example
  5370. **foo [bar](/url)**
  5371. .
  5372. <p><strong>foo <a href="/url">bar</a></strong></p>
  5373. ````````````````````````````````
  5374. ```````````````````````````````` example
  5375. **foo
  5376. bar**
  5377. .
  5378. <p><strong>foo
  5379. bar</strong></p>
  5380. ````````````````````````````````
  5381. In particular, emphasis and strong emphasis can be nested
  5382. inside strong emphasis:
  5383. ```````````````````````````````` example
  5384. __foo _bar_ baz__
  5385. .
  5386. <p><strong>foo <em>bar</em> baz</strong></p>
  5387. ````````````````````````````````
  5388. ```````````````````````````````` example
  5389. __foo __bar__ baz__
  5390. .
  5391. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5392. ````````````````````````````````
  5393. ```````````````````````````````` example
  5394. ____foo__ bar__
  5395. .
  5396. <p><strong><strong>foo</strong> bar</strong></p>
  5397. ````````````````````````````````
  5398. ```````````````````````````````` example
  5399. **foo **bar****
  5400. .
  5401. <p><strong>foo <strong>bar</strong></strong></p>
  5402. ````````````````````````````````
  5403. ```````````````````````````````` example
  5404. **foo *bar* baz**
  5405. .
  5406. <p><strong>foo <em>bar</em> baz</strong></p>
  5407. ````````````````````````````````
  5408. ```````````````````````````````` example
  5409. **foo*bar*baz**
  5410. .
  5411. <p><strong>foo<em>bar</em>baz</strong></p>
  5412. ````````````````````````````````
  5413. ```````````````````````````````` example
  5414. ***foo* bar**
  5415. .
  5416. <p><strong><em>foo</em> bar</strong></p>
  5417. ````````````````````````````````
  5418. ```````````````````````````````` example
  5419. **foo *bar***
  5420. .
  5421. <p><strong>foo <em>bar</em></strong></p>
  5422. ````````````````````````````````
  5423. Indefinite levels of nesting are possible:
  5424. ```````````````````````````````` example
  5425. **foo *bar **baz**
  5426. bim* bop**
  5427. .
  5428. <p><strong>foo <em>bar <strong>baz</strong>
  5429. bim</em> bop</strong></p>
  5430. ````````````````````````````````
  5431. ```````````````````````````````` example
  5432. **foo [*bar*](/url)**
  5433. .
  5434. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5435. ````````````````````````````````
  5436. There can be no empty emphasis or strong emphasis:
  5437. ```````````````````````````````` example
  5438. __ is not an empty emphasis
  5439. .
  5440. <p>__ is not an empty emphasis</p>
  5441. ````````````````````````````````
  5442. ```````````````````````````````` example
  5443. ____ is not an empty strong emphasis
  5444. .
  5445. <p>____ is not an empty strong emphasis</p>
  5446. ````````````````````````````````
  5447. Rule 11:
  5448. ```````````````````````````````` example
  5449. foo ***
  5450. .
  5451. <p>foo ***</p>
  5452. ````````````````````````````````
  5453. ```````````````````````````````` example
  5454. foo *\**
  5455. .
  5456. <p>foo <em>*</em></p>
  5457. ````````````````````````````````
  5458. ```````````````````````````````` example
  5459. foo *_*
  5460. .
  5461. <p>foo <em>_</em></p>
  5462. ````````````````````````````````
  5463. ```````````````````````````````` example
  5464. foo *****
  5465. .
  5466. <p>foo *****</p>
  5467. ````````````````````````````````
  5468. ```````````````````````````````` example
  5469. foo **\***
  5470. .
  5471. <p>foo <strong>*</strong></p>
  5472. ````````````````````````````````
  5473. ```````````````````````````````` example
  5474. foo **_**
  5475. .
  5476. <p>foo <strong>_</strong></p>
  5477. ````````````````````````````````
  5478. Note that when delimiters do not match evenly, Rule 11 determines
  5479. that the excess literal `*` characters will appear outside of the
  5480. emphasis, rather than inside it:
  5481. ```````````````````````````````` example
  5482. **foo*
  5483. .
  5484. <p>*<em>foo</em></p>
  5485. ````````````````````````````````
  5486. ```````````````````````````````` example
  5487. *foo**
  5488. .
  5489. <p><em>foo</em>*</p>
  5490. ````````````````````````````````
  5491. ```````````````````````````````` example
  5492. ***foo**
  5493. .
  5494. <p>*<strong>foo</strong></p>
  5495. ````````````````````````````````
  5496. ```````````````````````````````` example
  5497. ****foo*
  5498. .
  5499. <p>***<em>foo</em></p>
  5500. ````````````````````````````````
  5501. ```````````````````````````````` example
  5502. **foo***
  5503. .
  5504. <p><strong>foo</strong>*</p>
  5505. ````````````````````````````````
  5506. ```````````````````````````````` example
  5507. *foo****
  5508. .
  5509. <p><em>foo</em>***</p>
  5510. ````````````````````````````````
  5511. Rule 12:
  5512. ```````````````````````````````` example
  5513. foo ___
  5514. .
  5515. <p>foo ___</p>
  5516. ````````````````````````````````
  5517. ```````````````````````````````` example
  5518. foo _\__
  5519. .
  5520. <p>foo <em>_</em></p>
  5521. ````````````````````````````````
  5522. ```````````````````````````````` example
  5523. foo _*_
  5524. .
  5525. <p>foo <em>*</em></p>
  5526. ````````````````````````````````
  5527. ```````````````````````````````` example
  5528. foo _____
  5529. .
  5530. <p>foo _____</p>
  5531. ````````````````````````````````
  5532. ```````````````````````````````` example
  5533. foo __\___
  5534. .
  5535. <p>foo <strong>_</strong></p>
  5536. ````````````````````````````````
  5537. ```````````````````````````````` example
  5538. foo __*__
  5539. .
  5540. <p>foo <strong>*</strong></p>
  5541. ````````````````````````````````
  5542. ```````````````````````````````` example
  5543. __foo_
  5544. .
  5545. <p>_<em>foo</em></p>
  5546. ````````````````````````````````
  5547. Note that when delimiters do not match evenly, Rule 12 determines
  5548. that the excess literal `_` characters will appear outside of the
  5549. emphasis, rather than inside it:
  5550. ```````````````````````````````` example
  5551. _foo__
  5552. .
  5553. <p><em>foo</em>_</p>
  5554. ````````````````````````````````
  5555. ```````````````````````````````` example
  5556. ___foo__
  5557. .
  5558. <p>_<strong>foo</strong></p>
  5559. ````````````````````````````````
  5560. ```````````````````````````````` example
  5561. ____foo_
  5562. .
  5563. <p>___<em>foo</em></p>
  5564. ````````````````````````````````
  5565. ```````````````````````````````` example
  5566. __foo___
  5567. .
  5568. <p><strong>foo</strong>_</p>
  5569. ````````````````````````````````
  5570. ```````````````````````````````` example
  5571. _foo____
  5572. .
  5573. <p><em>foo</em>___</p>
  5574. ````````````````````````````````
  5575. Rule 13 implies that if you want emphasis nested directly inside
  5576. emphasis, you must use different delimiters:
  5577. ```````````````````````````````` example
  5578. **foo**
  5579. .
  5580. <p><strong>foo</strong></p>
  5581. ````````````````````````````````
  5582. ```````````````````````````````` example
  5583. *_foo_*
  5584. .
  5585. <p><em><em>foo</em></em></p>
  5586. ````````````````````````````````
  5587. ```````````````````````````````` example
  5588. __foo__
  5589. .
  5590. <p><strong>foo</strong></p>
  5591. ````````````````````````````````
  5592. ```````````````````````````````` example
  5593. _*foo*_
  5594. .
  5595. <p><em><em>foo</em></em></p>
  5596. ````````````````````````````````
  5597. However, strong emphasis within strong emphasis is possible without
  5598. switching delimiters:
  5599. ```````````````````````````````` example
  5600. ****foo****
  5601. .
  5602. <p><strong><strong>foo</strong></strong></p>
  5603. ````````````````````````````````
  5604. ```````````````````````````````` example
  5605. ____foo____
  5606. .
  5607. <p><strong><strong>foo</strong></strong></p>
  5608. ````````````````````````````````
  5609. Rule 13 can be applied to arbitrarily long sequences of
  5610. delimiters:
  5611. ```````````````````````````````` example
  5612. ******foo******
  5613. .
  5614. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5615. ````````````````````````````````
  5616. Rule 14:
  5617. ```````````````````````````````` example
  5618. ***foo***
  5619. .
  5620. <p><em><strong>foo</strong></em></p>
  5621. ````````````````````````````````
  5622. ```````````````````````````````` example
  5623. _____foo_____
  5624. .
  5625. <p><em><strong><strong>foo</strong></strong></em></p>
  5626. ````````````````````````````````
  5627. Rule 15:
  5628. ```````````````````````````````` example
  5629. *foo _bar* baz_
  5630. .
  5631. <p><em>foo _bar</em> baz_</p>
  5632. ````````````````````````````````
  5633. ```````````````````````````````` example
  5634. *foo __bar *baz bim__ bam*
  5635. .
  5636. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5637. ````````````````````````````````
  5638. Rule 16:
  5639. ```````````````````````````````` example
  5640. **foo **bar baz**
  5641. .
  5642. <p>**foo <strong>bar baz</strong></p>
  5643. ````````````````````````````````
  5644. ```````````````````````````````` example
  5645. *foo *bar baz*
  5646. .
  5647. <p>*foo <em>bar baz</em></p>
  5648. ````````````````````````````````
  5649. Rule 17:
  5650. ```````````````````````````````` example
  5651. *[bar*](/url)
  5652. .
  5653. <p>*<a href="/url">bar*</a></p>
  5654. ````````````````````````````````
  5655. ```````````````````````````````` example
  5656. _foo [bar_](/url)
  5657. .
  5658. <p>_foo <a href="/url">bar_</a></p>
  5659. ````````````````````````````````
  5660. ```````````````````````````````` example
  5661. *<img src="foo" title="*"/>
  5662. .
  5663. <p>*<img src="foo" title="*"/></p>
  5664. ````````````````````````````````
  5665. ```````````````````````````````` example
  5666. **<a href="**">
  5667. .
  5668. <p>**<a href="**"></p>
  5669. ````````````````````````````````
  5670. ```````````````````````````````` example
  5671. __<a href="__">
  5672. .
  5673. <p>__<a href="__"></p>
  5674. ````````````````````````````````
  5675. ```````````````````````````````` example
  5676. *a `*`*
  5677. .
  5678. <p><em>a <code>*</code></em></p>
  5679. ````````````````````````````````
  5680. ```````````````````````````````` example
  5681. _a `_`_
  5682. .
  5683. <p><em>a <code>_</code></em></p>
  5684. ````````````````````````````````
  5685. ```````````````````````````````` example
  5686. **a<http://foo.bar/?q=**>
  5687. .
  5688. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5689. ````````````````````````````````
  5690. ```````````````````````````````` example
  5691. __a<http://foo.bar/?q=__>
  5692. .
  5693. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5694. ````````````````````````````````
  5695. ## Links
  5696. A link contains [link text] (the visible text), a [link destination]
  5697. (the URI that is the link destination), and optionally a [link title].
  5698. There are two basic kinds of links in Markdown. In [inline links] the
  5699. destination and title are given immediately after the link text. In
  5700. [reference links] the destination and title are defined elsewhere in
  5701. the document.
  5702. A [link text](@) consists of a sequence of zero or more
  5703. inline elements enclosed by square brackets (`[` and `]`). The
  5704. following rules apply:
  5705. - Links may not contain other links, at any level of nesting. If
  5706. multiple otherwise valid link definitions appear nested inside each
  5707. other, the inner-most definition is used.
  5708. - Brackets are allowed in the [link text] only if (a) they
  5709. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5710. with an open bracket `[`, a sequence of zero or more inlines, and
  5711. a close bracket `]`.
  5712. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5713. than the brackets in link text. Thus, for example,
  5714. `` [foo`]` `` could not be a link text, since the second `]`
  5715. is part of a code span.
  5716. - The brackets in link text bind more tightly than markers for
  5717. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5718. A [link destination](@) consists of either
  5719. - a sequence of zero or more characters between an opening `<` and a
  5720. closing `>` that contains no line endings or unescaped
  5721. `<` or `>` characters, or
  5722. - a nonempty sequence of characters that does not start with `<`,
  5723. does not include [ASCII control characters][ASCII control character]
  5724. or [space] character, and includes parentheses only if (a) they are
  5725. backslash-escaped or (b) they are part of a balanced pair of
  5726. unescaped parentheses.
  5727. (Implementations may impose limits on parentheses nesting to
  5728. avoid performance issues, but at least three levels of nesting
  5729. should be supported.)
  5730. A [link title](@) consists of either
  5731. - a sequence of zero or more characters between straight double-quote
  5732. characters (`"`), including a `"` character only if it is
  5733. backslash-escaped, or
  5734. - a sequence of zero or more characters between straight single-quote
  5735. characters (`'`), including a `'` character only if it is
  5736. backslash-escaped, or
  5737. - a sequence of zero or more characters between matching parentheses
  5738. (`(...)`), including a `(` or `)` character only if it is
  5739. backslash-escaped.
  5740. Although [link titles] may span multiple lines, they may not contain
  5741. a [blank line].
  5742. An [inline link](@) consists of a [link text] followed immediately
  5743. by a left parenthesis `(`, an optional [link destination], an optional
  5744. [link title], and a right parenthesis `)`.
  5745. These four components may be separated by spaces, tabs, and up to one line
  5746. ending.
  5747. If both [link destination] and [link title] are present, they *must* be
  5748. separated by spaces, tabs, and up to one line ending.
  5749. The link's text consists of the inlines contained
  5750. in the [link text] (excluding the enclosing square brackets).
  5751. The link's URI consists of the link destination, excluding enclosing
  5752. `<...>` if present, with backslash-escapes in effect as described
  5753. above. The link's title consists of the link title, excluding its
  5754. enclosing delimiters, with backslash-escapes in effect as described
  5755. above.
  5756. Here is a simple inline link:
  5757. ```````````````````````````````` example
  5758. [link](/uri "title")
  5759. .
  5760. <p><a href="/uri" title="title">link</a></p>
  5761. ````````````````````````````````
  5762. The title, the link text and even
  5763. the destination may be omitted:
  5764. ```````````````````````````````` example
  5765. [link](/uri)
  5766. .
  5767. <p><a href="/uri">link</a></p>
  5768. ````````````````````````````````
  5769. ```````````````````````````````` example
  5770. [](./target.md)
  5771. .
  5772. <p><a href="./target.md"></a></p>
  5773. ````````````````````````````````
  5774. ```````````````````````````````` example
  5775. [link]()
  5776. .
  5777. <p><a href="">link</a></p>
  5778. ````````````````````````````````
  5779. ```````````````````````````````` example
  5780. [link](<>)
  5781. .
  5782. <p><a href="">link</a></p>
  5783. ````````````````````````````````
  5784. ```````````````````````````````` example
  5785. []()
  5786. .
  5787. <p><a href=""></a></p>
  5788. ````````````````````````````````
  5789. The destination can only contain spaces if it is
  5790. enclosed in pointy brackets:
  5791. ```````````````````````````````` example
  5792. [link](/my uri)
  5793. .
  5794. <p>[link](/my uri)</p>
  5795. ````````````````````````````````
  5796. ```````````````````````````````` example
  5797. [link](</my uri>)
  5798. .
  5799. <p><a href="/my%20uri">link</a></p>
  5800. ````````````````````````````````
  5801. The destination cannot contain line endings,
  5802. even if enclosed in pointy brackets:
  5803. ```````````````````````````````` example
  5804. [link](foo
  5805. bar)
  5806. .
  5807. <p>[link](foo
  5808. bar)</p>
  5809. ````````````````````````````````
  5810. ```````````````````````````````` example
  5811. [link](<foo
  5812. bar>)
  5813. .
  5814. <p>[link](<foo
  5815. bar>)</p>
  5816. ````````````````````````````````
  5817. The destination can contain `)` if it is enclosed
  5818. in pointy brackets:
  5819. ```````````````````````````````` example
  5820. [a](<b)c>)
  5821. .
  5822. <p><a href="b)c">a</a></p>
  5823. ````````````````````````````````
  5824. Pointy brackets that enclose links must be unescaped:
  5825. ```````````````````````````````` example
  5826. [link](<foo\>)
  5827. .
  5828. <p>[link](&lt;foo&gt;)</p>
  5829. ````````````````````````````````
  5830. These are not links, because the opening pointy bracket
  5831. is not matched properly:
  5832. ```````````````````````````````` example
  5833. [a](<b)c
  5834. [a](<b)c>
  5835. [a](<b>c)
  5836. .
  5837. <p>[a](&lt;b)c
  5838. [a](&lt;b)c&gt;
  5839. [a](<b>c)</p>
  5840. ````````````````````````````````
  5841. Parentheses inside the link destination may be escaped:
  5842. ```````````````````````````````` example
  5843. [link](\(foo\))
  5844. .
  5845. <p><a href="(foo)">link</a></p>
  5846. ````````````````````````````````
  5847. Any number of parentheses are allowed without escaping, as long as they are
  5848. balanced:
  5849. ```````````````````````````````` example
  5850. [link](foo(and(bar)))
  5851. .
  5852. <p><a href="foo(and(bar))">link</a></p>
  5853. ````````````````````````````````
  5854. However, if you have unbalanced parentheses, you need to escape or use the
  5855. `<...>` form:
  5856. ```````````````````````````````` example
  5857. [link](foo(and(bar))
  5858. .
  5859. <p>[link](foo(and(bar))</p>
  5860. ````````````````````````````````
  5861. ```````````````````````````````` example
  5862. [link](foo\(and\(bar\))
  5863. .
  5864. <p><a href="foo(and(bar)">link</a></p>
  5865. ````````````````````````````````
  5866. ```````````````````````````````` example
  5867. [link](<foo(and(bar)>)
  5868. .
  5869. <p><a href="foo(and(bar)">link</a></p>
  5870. ````````````````````````````````
  5871. Parentheses and other symbols can also be escaped, as usual
  5872. in Markdown:
  5873. ```````````````````````````````` example
  5874. [link](foo\)\:)
  5875. .
  5876. <p><a href="foo):">link</a></p>
  5877. ````````````````````````````````
  5878. A link can contain fragment identifiers and queries:
  5879. ```````````````````````````````` example
  5880. [link](#fragment)
  5881. [link](http://example.com#fragment)
  5882. [link](http://example.com?foo=3#frag)
  5883. .
  5884. <p><a href="#fragment">link</a></p>
  5885. <p><a href="http://example.com#fragment">link</a></p>
  5886. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5887. ````````````````````````````````
  5888. Note that a backslash before a non-escapable character is
  5889. just a backslash:
  5890. ```````````````````````````````` example
  5891. [link](foo\bar)
  5892. .
  5893. <p><a href="foo%5Cbar">link</a></p>
  5894. ````````````````````````````````
  5895. URL-escaping should be left alone inside the destination, as all
  5896. URL-escaped characters are also valid URL characters. Entity and
  5897. numerical character references in the destination will be parsed
  5898. into the corresponding Unicode code points, as usual. These may
  5899. be optionally URL-escaped when written as HTML, but this spec
  5900. does not enforce any particular policy for rendering URLs in
  5901. HTML or other formats. Renderers may make different decisions
  5902. about how to escape or normalize URLs in the output.
  5903. ```````````````````````````````` example
  5904. [link](foo%20b&auml;)
  5905. .
  5906. <p><a href="foo%20b%C3%A4">link</a></p>
  5907. ````````````````````````````````
  5908. Note that, because titles can often be parsed as destinations,
  5909. if you try to omit the destination and keep the title, you'll
  5910. get unexpected results:
  5911. ```````````````````````````````` example
  5912. [link]("title")
  5913. .
  5914. <p><a href="%22title%22">link</a></p>
  5915. ````````````````````````````````
  5916. Titles may be in single quotes, double quotes, or parentheses:
  5917. ```````````````````````````````` example
  5918. [link](/url "title")
  5919. [link](/url 'title')
  5920. [link](/url (title))
  5921. .
  5922. <p><a href="/url" title="title">link</a>
  5923. <a href="/url" title="title">link</a>
  5924. <a href="/url" title="title">link</a></p>
  5925. ````````````````````````````````
  5926. Backslash escapes and entity and numeric character references
  5927. may be used in titles:
  5928. ```````````````````````````````` example
  5929. [link](/url "title \"&quot;")
  5930. .
  5931. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5932. ````````````````````````````````
  5933. Titles must be separated from the link using spaces, tabs, and up to one line
  5934. ending.
  5935. Other [Unicode whitespace] like non-breaking space doesn't work.
  5936. ```````````````````````````````` example
  5937. [link](/url "title")
  5938. .
  5939. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5940. ````````````````````````````````
  5941. Nested balanced quotes are not allowed without escaping:
  5942. ```````````````````````````````` example
  5943. [link](/url "title "and" title")
  5944. .
  5945. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5946. ````````````````````````````````
  5947. But it is easy to work around this by using a different quote type:
  5948. ```````````````````````````````` example
  5949. [link](/url 'title "and" title')
  5950. .
  5951. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5952. ````````````````````````````````
  5953. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5954. title, and its test suite included a test demonstrating this.
  5955. But it is hard to see a good rationale for the extra complexity this
  5956. brings, since there are already many ways---backslash escaping,
  5957. entity and numeric character references, or using a different
  5958. quote type for the enclosing title---to write titles containing
  5959. double quotes. `Markdown.pl`'s handling of titles has a number
  5960. of other strange features. For example, it allows single-quoted
  5961. titles in inline links, but not reference links. And, in
  5962. reference links but not inline links, it allows a title to begin
  5963. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5964. titles with no closing quotation mark, though 1.0.2b8 does not.
  5965. It seems preferable to adopt a simple, rational rule that works
  5966. the same way in inline links and link reference definitions.)
  5967. Spaces, tabs, and up to one line ending is allowed around the destination and
  5968. title:
  5969. ```````````````````````````````` example
  5970. [link]( /uri
  5971. "title" )
  5972. .
  5973. <p><a href="/uri" title="title">link</a></p>
  5974. ````````````````````````````````
  5975. But it is not allowed between the link text and the
  5976. following parenthesis:
  5977. ```````````````````````````````` example
  5978. [link] (/uri)
  5979. .
  5980. <p>[link] (/uri)</p>
  5981. ````````````````````````````````
  5982. The link text may contain balanced brackets, but not unbalanced ones,
  5983. unless they are escaped:
  5984. ```````````````````````````````` example
  5985. [link [foo [bar]]](/uri)
  5986. .
  5987. <p><a href="/uri">link [foo [bar]]</a></p>
  5988. ````````````````````````````````
  5989. ```````````````````````````````` example
  5990. [link] bar](/uri)
  5991. .
  5992. <p>[link] bar](/uri)</p>
  5993. ````````````````````````````````
  5994. ```````````````````````````````` example
  5995. [link [bar](/uri)
  5996. .
  5997. <p>[link <a href="/uri">bar</a></p>
  5998. ````````````````````````````````
  5999. ```````````````````````````````` example
  6000. [link \[bar](/uri)
  6001. .
  6002. <p><a href="/uri">link [bar</a></p>
  6003. ````````````````````````````````
  6004. The link text may contain inline content:
  6005. ```````````````````````````````` example
  6006. [link *foo **bar** `#`*](/uri)
  6007. .
  6008. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6009. ````````````````````````````````
  6010. ```````````````````````````````` example
  6011. [![moon](moon.jpg)](/uri)
  6012. .
  6013. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6014. ````````````````````````````````
  6015. However, links may not contain other links, at any level of nesting.
  6016. ```````````````````````````````` example
  6017. [foo [bar](/uri)](/uri)
  6018. .
  6019. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  6020. ````````````````````````````````
  6021. ```````````````````````````````` example
  6022. [foo *[bar [baz](/uri)](/uri)*](/uri)
  6023. .
  6024. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  6025. ````````````````````````````````
  6026. ```````````````````````````````` example
  6027. ![[[foo](uri1)](uri2)](uri3)
  6028. .
  6029. <p><img src="uri3" alt="[foo](uri2)" /></p>
  6030. ````````````````````````````````
  6031. These cases illustrate the precedence of link text grouping over
  6032. emphasis grouping:
  6033. ```````````````````````````````` example
  6034. *[foo*](/uri)
  6035. .
  6036. <p>*<a href="/uri">foo*</a></p>
  6037. ````````````````````````````````
  6038. ```````````````````````````````` example
  6039. [foo *bar](baz*)
  6040. .
  6041. <p><a href="baz*">foo *bar</a></p>
  6042. ````````````````````````````````
  6043. Note that brackets that *aren't* part of links do not take
  6044. precedence:
  6045. ```````````````````````````````` example
  6046. *foo [bar* baz]
  6047. .
  6048. <p><em>foo [bar</em> baz]</p>
  6049. ````````````````````````````````
  6050. These cases illustrate the precedence of HTML tags, code spans,
  6051. and autolinks over link grouping:
  6052. ```````````````````````````````` example
  6053. [foo <bar attr="](baz)">
  6054. .
  6055. <p>[foo <bar attr="](baz)"></p>
  6056. ````````````````````````````````
  6057. ```````````````````````````````` example
  6058. [foo`](/uri)`
  6059. .
  6060. <p>[foo<code>](/uri)</code></p>
  6061. ````````````````````````````````
  6062. ```````````````````````````````` example
  6063. [foo<http://example.com/?search=](uri)>
  6064. .
  6065. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  6066. ````````````````````````````````
  6067. There are three kinds of [reference link](@)s:
  6068. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  6069. and [shortcut](#shortcut-reference-link).
  6070. A [full reference link](@)
  6071. consists of a [link text] immediately followed by a [link label]
  6072. that [matches] a [link reference definition] elsewhere in the document.
  6073. A [link label](@) begins with a left bracket (`[`) and ends
  6074. with the first right bracket (`]`) that is not backslash-escaped.
  6075. Between these brackets there must be at least one character that is not a space,
  6076. tab, or line ending.
  6077. Unescaped square bracket characters are not allowed inside the
  6078. opening and closing square brackets of [link labels]. A link
  6079. label can have at most 999 characters inside the square
  6080. brackets.
  6081. One label [matches](@)
  6082. another just in case their normalized forms are equal. To normalize a
  6083. label, strip off the opening and closing brackets,
  6084. perform the *Unicode case fold*, strip leading and trailing
  6085. spaces, tabs, and line endings, and collapse consecutive internal
  6086. spaces, tabs, and line endings to a single space. If there are multiple
  6087. matching reference link definitions, the one that comes first in the
  6088. document is used. (It is desirable in such cases to emit a warning.)
  6089. The link's URI and title are provided by the matching [link
  6090. reference definition].
  6091. Here is a simple example:
  6092. ```````````````````````````````` example
  6093. [foo][bar]
  6094. [bar]: /url "title"
  6095. .
  6096. <p><a href="/url" title="title">foo</a></p>
  6097. ````````````````````````````````
  6098. The rules for the [link text] are the same as with
  6099. [inline links]. Thus:
  6100. The link text may contain balanced brackets, but not unbalanced ones,
  6101. unless they are escaped:
  6102. ```````````````````````````````` example
  6103. [link [foo [bar]]][ref]
  6104. [ref]: /uri
  6105. .
  6106. <p><a href="/uri">link [foo [bar]]</a></p>
  6107. ````````````````````````````````
  6108. ```````````````````````````````` example
  6109. [link \[bar][ref]
  6110. [ref]: /uri
  6111. .
  6112. <p><a href="/uri">link [bar</a></p>
  6113. ````````````````````````````````
  6114. The link text may contain inline content:
  6115. ```````````````````````````````` example
  6116. [link *foo **bar** `#`*][ref]
  6117. [ref]: /uri
  6118. .
  6119. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  6120. ````````````````````````````````
  6121. ```````````````````````````````` example
  6122. [![moon](moon.jpg)][ref]
  6123. [ref]: /uri
  6124. .
  6125. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  6126. ````````````````````````````````
  6127. However, links may not contain other links, at any level of nesting.
  6128. ```````````````````````````````` example
  6129. [foo [bar](/uri)][ref]
  6130. [ref]: /uri
  6131. .
  6132. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  6133. ````````````````````````````````
  6134. ```````````````````````````````` example
  6135. [foo *bar [baz][ref]*][ref]
  6136. [ref]: /uri
  6137. .
  6138. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  6139. ````````````````````````````````
  6140. (In the examples above, we have two [shortcut reference links]
  6141. instead of one [full reference link].)
  6142. The following cases illustrate the precedence of link text grouping over
  6143. emphasis grouping:
  6144. ```````````````````````````````` example
  6145. *[foo*][ref]
  6146. [ref]: /uri
  6147. .
  6148. <p>*<a href="/uri">foo*</a></p>
  6149. ````````````````````````````````
  6150. ```````````````````````````````` example
  6151. [foo *bar][ref]*
  6152. [ref]: /uri
  6153. .
  6154. <p><a href="/uri">foo *bar</a>*</p>
  6155. ````````````````````````````````
  6156. These cases illustrate the precedence of HTML tags, code spans,
  6157. and autolinks over link grouping:
  6158. ```````````````````````````````` example
  6159. [foo <bar attr="][ref]">
  6160. [ref]: /uri
  6161. .
  6162. <p>[foo <bar attr="][ref]"></p>
  6163. ````````````````````````````````
  6164. ```````````````````````````````` example
  6165. [foo`][ref]`
  6166. [ref]: /uri
  6167. .
  6168. <p>[foo<code>][ref]</code></p>
  6169. ````````````````````````````````
  6170. ```````````````````````````````` example
  6171. [foo<http://example.com/?search=][ref]>
  6172. [ref]: /uri
  6173. .
  6174. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6175. ````````````````````````````````
  6176. Matching is case-insensitive:
  6177. ```````````````````````````````` example
  6178. [foo][BaR]
  6179. [bar]: /url "title"
  6180. .
  6181. <p><a href="/url" title="title">foo</a></p>
  6182. ````````````````````````````````
  6183. Unicode case fold is used:
  6184. ```````````````````````````````` example
  6185. [ẞ]
  6186. [SS]: /url
  6187. .
  6188. <p><a href="/url">ẞ</a></p>
  6189. ````````````````````````````````
  6190. Consecutive internal spaces, tabs, and line endings are treated as one space for
  6191. purposes of determining matching:
  6192. ```````````````````````````````` example
  6193. [Foo
  6194. bar]: /url
  6195. [Baz][Foo bar]
  6196. .
  6197. <p><a href="/url">Baz</a></p>
  6198. ````````````````````````````````
  6199. No spaces, tabs, or line endings are allowed between the [link text] and the
  6200. [link label]:
  6201. ```````````````````````````````` example
  6202. [foo] [bar]
  6203. [bar]: /url "title"
  6204. .
  6205. <p>[foo] <a href="/url" title="title">bar</a></p>
  6206. ````````````````````````````````
  6207. ```````````````````````````````` example
  6208. [foo]
  6209. [bar]
  6210. [bar]: /url "title"
  6211. .
  6212. <p>[foo]
  6213. <a href="/url" title="title">bar</a></p>
  6214. ````````````````````````````````
  6215. This is a departure from John Gruber's original Markdown syntax
  6216. description, which explicitly allows whitespace between the link
  6217. text and the link label. It brings reference links in line with
  6218. [inline links], which (according to both original Markdown and
  6219. this spec) cannot have whitespace after the link text. More
  6220. importantly, it prevents inadvertent capture of consecutive
  6221. [shortcut reference links]. If whitespace is allowed between the
  6222. link text and the link label, then in the following we will have
  6223. a single reference link, not two shortcut reference links, as
  6224. intended:
  6225. ``` markdown
  6226. [foo]
  6227. [bar]
  6228. [foo]: /url1
  6229. [bar]: /url2
  6230. ```
  6231. (Note that [shortcut reference links] were introduced by Gruber
  6232. himself in a beta version of `Markdown.pl`, but never included
  6233. in the official syntax description. Without shortcut reference
  6234. links, it is harmless to allow space between the link text and
  6235. link label; but once shortcut references are introduced, it is
  6236. too dangerous to allow this, as it frequently leads to
  6237. unintended results.)
  6238. When there are multiple matching [link reference definitions],
  6239. the first is used:
  6240. ```````````````````````````````` example
  6241. [foo]: /url1
  6242. [foo]: /url2
  6243. [bar][foo]
  6244. .
  6245. <p><a href="/url1">bar</a></p>
  6246. ````````````````````````````````
  6247. Note that matching is performed on normalized strings, not parsed
  6248. inline content. So the following does not match, even though the
  6249. labels define equivalent inline content:
  6250. ```````````````````````````````` example
  6251. [bar][foo\!]
  6252. [foo!]: /url
  6253. .
  6254. <p>[bar][foo!]</p>
  6255. ````````````````````````````````
  6256. [Link labels] cannot contain brackets, unless they are
  6257. backslash-escaped:
  6258. ```````````````````````````````` example
  6259. [foo][ref[]
  6260. [ref[]: /uri
  6261. .
  6262. <p>[foo][ref[]</p>
  6263. <p>[ref[]: /uri</p>
  6264. ````````````````````````````````
  6265. ```````````````````````````````` example
  6266. [foo][ref[bar]]
  6267. [ref[bar]]: /uri
  6268. .
  6269. <p>[foo][ref[bar]]</p>
  6270. <p>[ref[bar]]: /uri</p>
  6271. ````````````````````````````````
  6272. ```````````````````````````````` example
  6273. [[[foo]]]
  6274. [[[foo]]]: /url
  6275. .
  6276. <p>[[[foo]]]</p>
  6277. <p>[[[foo]]]: /url</p>
  6278. ````````````````````````````````
  6279. ```````````````````````````````` example
  6280. [foo][ref\[]
  6281. [ref\[]: /uri
  6282. .
  6283. <p><a href="/uri">foo</a></p>
  6284. ````````````````````````````````
  6285. Note that in this example `]` is not backslash-escaped:
  6286. ```````````````````````````````` example
  6287. [bar\\]: /uri
  6288. [bar\\]
  6289. .
  6290. <p><a href="/uri">bar\</a></p>
  6291. ````````````````````````````````
  6292. A [link label] must contain at least one character that is not a space, tab, or
  6293. line ending:
  6294. ```````````````````````````````` example
  6295. []
  6296. []: /uri
  6297. .
  6298. <p>[]</p>
  6299. <p>[]: /uri</p>
  6300. ````````````````````````````````
  6301. ```````````````````````````````` example
  6302. [
  6303. ]
  6304. [
  6305. ]: /uri
  6306. .
  6307. <p>[
  6308. ]</p>
  6309. <p>[
  6310. ]: /uri</p>
  6311. ````````````````````````````````
  6312. A [collapsed reference link](@)
  6313. consists of a [link label] that [matches] a
  6314. [link reference definition] elsewhere in the
  6315. document, followed by the string `[]`.
  6316. The contents of the first link label are parsed as inlines,
  6317. which are used as the link's text. The link's URI and title are
  6318. provided by the matching reference link definition. Thus,
  6319. `[foo][]` is equivalent to `[foo][foo]`.
  6320. ```````````````````````````````` example
  6321. [foo][]
  6322. [foo]: /url "title"
  6323. .
  6324. <p><a href="/url" title="title">foo</a></p>
  6325. ````````````````````````````````
  6326. ```````````````````````````````` example
  6327. [*foo* bar][]
  6328. [*foo* bar]: /url "title"
  6329. .
  6330. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6331. ````````````````````````````````
  6332. The link labels are case-insensitive:
  6333. ```````````````````````````````` example
  6334. [Foo][]
  6335. [foo]: /url "title"
  6336. .
  6337. <p><a href="/url" title="title">Foo</a></p>
  6338. ````````````````````````````````
  6339. As with full reference links, spaces, tabs, or line endings are not
  6340. allowed between the two sets of brackets:
  6341. ```````````````````````````````` example
  6342. [foo]
  6343. []
  6344. [foo]: /url "title"
  6345. .
  6346. <p><a href="/url" title="title">foo</a>
  6347. []</p>
  6348. ````````````````````````````````
  6349. A [shortcut reference link](@)
  6350. consists of a [link label] that [matches] a
  6351. [link reference definition] elsewhere in the
  6352. document and is not followed by `[]` or a link label.
  6353. The contents of the first link label are parsed as inlines,
  6354. which are used as the link's text. The link's URI and title
  6355. are provided by the matching link reference definition.
  6356. Thus, `[foo]` is equivalent to `[foo][]`.
  6357. ```````````````````````````````` example
  6358. [foo]
  6359. [foo]: /url "title"
  6360. .
  6361. <p><a href="/url" title="title">foo</a></p>
  6362. ````````````````````````````````
  6363. ```````````````````````````````` example
  6364. [*foo* bar]
  6365. [*foo* bar]: /url "title"
  6366. .
  6367. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6368. ````````````````````````````````
  6369. ```````````````````````````````` example
  6370. [[*foo* bar]]
  6371. [*foo* bar]: /url "title"
  6372. .
  6373. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6374. ````````````````````````````````
  6375. ```````````````````````````````` example
  6376. [[bar [foo]
  6377. [foo]: /url
  6378. .
  6379. <p>[[bar <a href="/url">foo</a></p>
  6380. ````````````````````````````````
  6381. The link labels are case-insensitive:
  6382. ```````````````````````````````` example
  6383. [Foo]
  6384. [foo]: /url "title"
  6385. .
  6386. <p><a href="/url" title="title">Foo</a></p>
  6387. ````````````````````````````````
  6388. A space after the link text should be preserved:
  6389. ```````````````````````````````` example
  6390. [foo] bar
  6391. [foo]: /url
  6392. .
  6393. <p><a href="/url">foo</a> bar</p>
  6394. ````````````````````````````````
  6395. If you just want bracketed text, you can backslash-escape the
  6396. opening bracket to avoid links:
  6397. ```````````````````````````````` example
  6398. \[foo]
  6399. [foo]: /url "title"
  6400. .
  6401. <p>[foo]</p>
  6402. ````````````````````````````````
  6403. Note that this is a link, because a link label ends with the first
  6404. following closing bracket:
  6405. ```````````````````````````````` example
  6406. [foo*]: /url
  6407. *[foo*]
  6408. .
  6409. <p>*<a href="/url">foo*</a></p>
  6410. ````````````````````````````````
  6411. Full and compact references take precedence over shortcut
  6412. references:
  6413. ```````````````````````````````` example
  6414. [foo][bar]
  6415. [foo]: /url1
  6416. [bar]: /url2
  6417. .
  6418. <p><a href="/url2">foo</a></p>
  6419. ````````````````````````````````
  6420. ```````````````````````````````` example
  6421. [foo][]
  6422. [foo]: /url1
  6423. .
  6424. <p><a href="/url1">foo</a></p>
  6425. ````````````````````````````````
  6426. Inline links also take precedence:
  6427. ```````````````````````````````` example
  6428. [foo]()
  6429. [foo]: /url1
  6430. .
  6431. <p><a href="">foo</a></p>
  6432. ````````````````````````````````
  6433. ```````````````````````````````` example
  6434. [foo](not a link)
  6435. [foo]: /url1
  6436. .
  6437. <p><a href="/url1">foo</a>(not a link)</p>
  6438. ````````````````````````````````
  6439. In the following case `[bar][baz]` is parsed as a reference,
  6440. `[foo]` as normal text:
  6441. ```````````````````````````````` example
  6442. [foo][bar][baz]
  6443. [baz]: /url
  6444. .
  6445. <p>[foo]<a href="/url">bar</a></p>
  6446. ````````````````````````````````
  6447. Here, though, `[foo][bar]` is parsed as a reference, since
  6448. `[bar]` is defined:
  6449. ```````````````````````````````` example
  6450. [foo][bar][baz]
  6451. [baz]: /url1
  6452. [bar]: /url2
  6453. .
  6454. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6455. ````````````````````````````````
  6456. Here `[foo]` is not parsed as a shortcut reference, because it
  6457. is followed by a link label (even though `[bar]` is not defined):
  6458. ```````````````````````````````` example
  6459. [foo][bar][baz]
  6460. [baz]: /url1
  6461. [foo]: /url2
  6462. .
  6463. <p>[foo]<a href="/url1">bar</a></p>
  6464. ````````````````````````````````
  6465. ## Annotations
  6466. An annotation contains [annotation text] (the visible text), and an [annotation set]
  6467. (the hints to annotate the text with).
  6468. There are six basic kinds of annotations in Markdown.
  6469. In [block annotations] the hints are given at the end of the block.
  6470. In [block-tree annotations] the hints are given
  6471. at the beginning of the topmost block.
  6472. In [block-siblings annotations] the hints are given
  6473. as a prototype sibling block immediately before the first sibling block.
  6474. In [block-cluster annotations] the hints are given
  6475. as a prototype parent block immediately before the first block in the cluster.
  6476. In [inline annotations] the hints are given
  6477. immediately after the link text.
  6478. In referenced annotations the hints are defined
  6479. in a [reference link] elsewhere in the document.
  6480. An [annotation text](@) consists of either
  6481. - a single explicitly enclosed inline element,
  6482. i.e. a [code span],
  6483. [emphasis or strong emphasis][emphasis and strong emphasis],
  6484. [link], [image], or [autolink], or
  6485. - a sequence of zero or more inline elements
  6486. enclosed by square brackets (`[` and `]`).
  6487. The following rules apply:
  6488. - Annotations may not contain other annotations, at any level of nesting.
  6489. If multiple otherwise valid annotation definitions appear nested inside each
  6490. other, the inner-most definition is used.
  6491. - Brackets are allowed in the [annotation text] only if (a) they
  6492. are backslash-escaped or (b) they appear as a matched pair of brackets,
  6493. with an open bracket `[`, a sequence of zero or more inlines, and
  6494. a close bracket `]`.
  6495. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  6496. than the brackets in annotation text. Thus, for example,
  6497. `` [foo`]` `` could not be an annotation text, since the second `]`
  6498. is part of a code span.
  6499. - The brackets in annotation text bind more tightly than markers for
  6500. [emphasis and strong emphasis]. Thus, for example, `*[foo*]{hint}` is a link.
  6501. FIXME: define priority of link versus annotation text if necessary.
  6502. An [annotation set](@) consists of a an opening `{`,
  6503. followed by optional spaces or tabs (including up to one [line ending]),
  6504. zero or more [annotation hints]
  6505. delimited by spaces or tabs (including up to one [line ending]),
  6506. optional spaces or tabs (including up to one [line ending]),
  6507. and a closing `}`.
  6508. There are 3 types of [annotation hints][@] in Markdown.
  6509. [typeof hints], [property hints], and [resource hints].
  6510. A [typeof hint](@) consists of character `.`,
  6511. then an [annotation destination].
  6512. A [property hint](@) consists of an [annotation destination].
  6513. A [resource hint](@) consists of character `=`,
  6514. then an [annotation destination].
  6515. An [annotation destination](@) consists of either
  6516. - a sequence of zero or more characters between an opening `<` and a
  6517. closing `>` that contains no line endings or unescaped
  6518. `<` or `>` characters, or
  6519. - a nonempty sequence of characters that does not start with `<`,
  6520. does not include [ASCII control characters][ASCII control character]
  6521. or [space] character, and includes parentheses only if (a) they are
  6522. backslash-escaped or (b) they are part of a balanced pair of
  6523. unescaped parentheses.
  6524. (Implementations may impose limits on parentheses nesting to
  6525. avoid performance issues, but at least three levels of nesting
  6526. should be supported.)
  6527. FIXME: tighten [annotation destination] to cover only <...> and CURIEs.
  6528. FIXME: define [block annotation].
  6529. FIXME: define [block-tree annotation].
  6530. FIXME: define [block-siblings annotation].
  6531. FIXME: define [block-cluster annotation].
  6532. An [inline annotation](@) consists of an [annotation text]
  6533. followed immediately by an [annotation set].
  6534. The annotation consists of HTML tag attributes
  6535. with the type of [annotation hint] as key.
  6536. The annotation attribute's value is the [annotation destination],
  6537. excluding enclosing `<...>` if present.
  6538. FIXME: define addition of prefix attributes
  6539. when deviating from [annotation initial context].
  6540. FIXME: define addition of vocab attribute
  6541. when declared in [annotation reference definition].
  6542. FIXME: define normalization of [annotation destination] into CURIE
  6543. based on [annotation initial context]
  6544. and prefixes in [annotation reference definitions].
  6545. ## Images
  6546. Syntax for images is like the syntax for links, with one
  6547. difference. Instead of [link text], we have an
  6548. [image description](@). The rules for this are the
  6549. same as for [link text], except that (a) an
  6550. image description starts with `![` rather than `[`, and
  6551. (b) an image description may contain links.
  6552. An image description has inline elements
  6553. as its contents. When an image is rendered to HTML,
  6554. this is standardly used as the image's `alt` attribute.
  6555. ```````````````````````````````` example
  6556. ![foo](/url "title")
  6557. .
  6558. <p><img src="/url" alt="foo" title="title" /></p>
  6559. ````````````````````````````````
  6560. ```````````````````````````````` example
  6561. ![foo *bar*]
  6562. [foo *bar*]: train.jpg "train & tracks"
  6563. .
  6564. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6565. ````````````````````````````````
  6566. ```````````````````````````````` example
  6567. ![foo ![bar](/url)](/url2)
  6568. .
  6569. <p><img src="/url2" alt="foo bar" /></p>
  6570. ````````````````````````````````
  6571. ```````````````````````````````` example
  6572. ![foo [bar](/url)](/url2)
  6573. .
  6574. <p><img src="/url2" alt="foo bar" /></p>
  6575. ````````````````````````````````
  6576. Though this spec is concerned with parsing, not rendering, it is
  6577. recommended that in rendering to HTML, only the plain string content
  6578. of the [image description] be used. Note that in
  6579. the above example, the alt attribute's value is `foo bar`, not `foo
  6580. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6581. content is rendered, without formatting.
  6582. ```````````````````````````````` example
  6583. ![foo *bar*][]
  6584. [foo *bar*]: train.jpg "train & tracks"
  6585. .
  6586. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6587. ````````````````````````````````
  6588. ```````````````````````````````` example
  6589. ![foo *bar*][foobar]
  6590. [FOOBAR]: train.jpg "train & tracks"
  6591. .
  6592. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6593. ````````````````````````````````
  6594. ```````````````````````````````` example
  6595. ![foo](train.jpg)
  6596. .
  6597. <p><img src="train.jpg" alt="foo" /></p>
  6598. ````````````````````````````````
  6599. ```````````````````````````````` example
  6600. My ![foo bar](/path/to/train.jpg "title" )
  6601. .
  6602. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6603. ````````````````````````````````
  6604. ```````````````````````````````` example
  6605. ![foo](<url>)
  6606. .
  6607. <p><img src="url" alt="foo" /></p>
  6608. ````````````````````````````````
  6609. ```````````````````````````````` example
  6610. ![](/url)
  6611. .
  6612. <p><img src="/url" alt="" /></p>
  6613. ````````````````````````````````
  6614. Reference-style:
  6615. ```````````````````````````````` example
  6616. ![foo][bar]
  6617. [bar]: /url
  6618. .
  6619. <p><img src="/url" alt="foo" /></p>
  6620. ````````````````````````````````
  6621. ```````````````````````````````` example
  6622. ![foo][bar]
  6623. [BAR]: /url
  6624. .
  6625. <p><img src="/url" alt="foo" /></p>
  6626. ````````````````````````````````
  6627. Collapsed:
  6628. ```````````````````````````````` example
  6629. ![foo][]
  6630. [foo]: /url "title"
  6631. .
  6632. <p><img src="/url" alt="foo" title="title" /></p>
  6633. ````````````````````````````````
  6634. ```````````````````````````````` example
  6635. ![*foo* bar][]
  6636. [*foo* bar]: /url "title"
  6637. .
  6638. <p><img src="/url" alt="foo bar" title="title" /></p>
  6639. ````````````````````````````````
  6640. The labels are case-insensitive:
  6641. ```````````````````````````````` example
  6642. ![Foo][]
  6643. [foo]: /url "title"
  6644. .
  6645. <p><img src="/url" alt="Foo" title="title" /></p>
  6646. ````````````````````````````````
  6647. As with reference links, spaces, tabs, and line endings, are not allowed
  6648. between the two sets of brackets:
  6649. ```````````````````````````````` example
  6650. ![foo]
  6651. []
  6652. [foo]: /url "title"
  6653. .
  6654. <p><img src="/url" alt="foo" title="title" />
  6655. []</p>
  6656. ````````````````````````````````
  6657. Shortcut:
  6658. ```````````````````````````````` example
  6659. ![foo]
  6660. [foo]: /url "title"
  6661. .
  6662. <p><img src="/url" alt="foo" title="title" /></p>
  6663. ````````````````````````````````
  6664. ```````````````````````````````` example
  6665. ![*foo* bar]
  6666. [*foo* bar]: /url "title"
  6667. .
  6668. <p><img src="/url" alt="foo bar" title="title" /></p>
  6669. ````````````````````````````````
  6670. Note that link labels cannot contain unescaped brackets:
  6671. ```````````````````````````````` example
  6672. ![[foo]]
  6673. [[foo]]: /url "title"
  6674. .
  6675. <p>![[foo]]</p>
  6676. <p>[[foo]]: /url &quot;title&quot;</p>
  6677. ````````````````````````````````
  6678. The link labels are case-insensitive:
  6679. ```````````````````````````````` example
  6680. ![Foo]
  6681. [foo]: /url "title"
  6682. .
  6683. <p><img src="/url" alt="Foo" title="title" /></p>
  6684. ````````````````````````````````
  6685. If you just want a literal `!` followed by bracketed text, you can
  6686. backslash-escape the opening `[`:
  6687. ```````````````````````````````` example
  6688. !\[foo]
  6689. [foo]: /url "title"
  6690. .
  6691. <p>![foo]</p>
  6692. ````````````````````````````````
  6693. If you want a link after a literal `!`, backslash-escape the
  6694. `!`:
  6695. ```````````````````````````````` example
  6696. \![foo]
  6697. [foo]: /url "title"
  6698. .
  6699. <p>!<a href="/url" title="title">foo</a></p>
  6700. ````````````````````````````````
  6701. ## Autolinks
  6702. [Autolink](@)s are absolute URIs and email addresses inside
  6703. `<` and `>`. They are parsed as links, with the URL or email address
  6704. as the link label.
  6705. A [URI autolink](@) consists of `<`, followed by an
  6706. [absolute URI] followed by `>`. It is parsed as
  6707. a link to the URI, with the URI as the link's label.
  6708. An [absolute URI](@),
  6709. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6710. followed by zero or more characters other [ASCII control
  6711. characters][ASCII control character], [space], `<`, and `>`.
  6712. If the URI includes these characters, they must be percent-encoded
  6713. (e.g. `%20` for a space).
  6714. For purposes of this spec, a [scheme](@) is any sequence
  6715. of 2--32 characters beginning with an ASCII letter and followed
  6716. by any combination of ASCII letters, digits, or the symbols plus
  6717. ("+"), period ("."), or hyphen ("-").
  6718. Here are some valid autolinks:
  6719. ```````````````````````````````` example
  6720. <http://foo.bar.baz>
  6721. .
  6722. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6723. ````````````````````````````````
  6724. ```````````````````````````````` example
  6725. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6726. .
  6727. <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>
  6728. ````````````````````````````````
  6729. ```````````````````````````````` example
  6730. <irc://foo.bar:2233/baz>
  6731. .
  6732. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6733. ````````````````````````````````
  6734. Uppercase is also fine:
  6735. ```````````````````````````````` example
  6736. <MAILTO:FOO@BAR.BAZ>
  6737. .
  6738. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6739. ````````````````````````````````
  6740. Note that many strings that count as [absolute URIs] for
  6741. purposes of this spec are not valid URIs, because their
  6742. schemes are not registered or because of other problems
  6743. with their syntax:
  6744. ```````````````````````````````` example
  6745. <a+b+c:d>
  6746. .
  6747. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6748. ````````````````````````````````
  6749. ```````````````````````````````` example
  6750. <made-up-scheme://foo,bar>
  6751. .
  6752. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6753. ````````````````````````````````
  6754. ```````````````````````````````` example
  6755. <http://../>
  6756. .
  6757. <p><a href="http://../">http://../</a></p>
  6758. ````````````````````````````````
  6759. ```````````````````````````````` example
  6760. <localhost:5001/foo>
  6761. .
  6762. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6763. ````````````````````````````````
  6764. Spaces are not allowed in autolinks:
  6765. ```````````````````````````````` example
  6766. <http://foo.bar/baz bim>
  6767. .
  6768. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6769. ````````````````````````````````
  6770. Backslash-escapes do not work inside autolinks:
  6771. ```````````````````````````````` example
  6772. <http://example.com/\[\>
  6773. .
  6774. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6775. ````````````````````````````````
  6776. An [email autolink](@)
  6777. consists of `<`, followed by an [email address],
  6778. followed by `>`. The link's label is the email address,
  6779. and the URL is `mailto:` followed by the email address.
  6780. An [email address](@),
  6781. for these purposes, is anything that matches
  6782. the [non-normative regex from the HTML5
  6783. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6784. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6785. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6786. Examples of email autolinks:
  6787. ```````````````````````````````` example
  6788. <foo@bar.example.com>
  6789. .
  6790. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6791. ````````````````````````````````
  6792. ```````````````````````````````` example
  6793. <foo+special@Bar.baz-bar0.com>
  6794. .
  6795. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6796. ````````````````````````````````
  6797. Backslash-escapes do not work inside email autolinks:
  6798. ```````````````````````````````` example
  6799. <foo\+@bar.example.com>
  6800. .
  6801. <p>&lt;foo+@bar.example.com&gt;</p>
  6802. ````````````````````````````````
  6803. These are not autolinks:
  6804. ```````````````````````````````` example
  6805. <>
  6806. .
  6807. <p>&lt;&gt;</p>
  6808. ````````````````````````````````
  6809. ```````````````````````````````` example
  6810. < http://foo.bar >
  6811. .
  6812. <p>&lt; http://foo.bar &gt;</p>
  6813. ````````````````````````````````
  6814. ```````````````````````````````` example
  6815. <m:abc>
  6816. .
  6817. <p>&lt;m:abc&gt;</p>
  6818. ````````````````````````````````
  6819. ```````````````````````````````` example
  6820. <foo.bar.baz>
  6821. .
  6822. <p>&lt;foo.bar.baz&gt;</p>
  6823. ````````````````````````````````
  6824. ```````````````````````````````` example
  6825. http://example.com
  6826. .
  6827. <p>http://example.com</p>
  6828. ````````````````````````````````
  6829. ```````````````````````````````` example
  6830. foo@bar.example.com
  6831. .
  6832. <p>foo@bar.example.com</p>
  6833. ````````````````````````````````
  6834. ## Raw HTML
  6835. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6836. raw HTML tag and will be rendered in HTML without escaping.
  6837. Tag and attribute names are not limited to current HTML tags,
  6838. so custom tags (and even, say, DocBook tags) may be used.
  6839. Here is the grammar for tags:
  6840. A [tag name](@) consists of an ASCII letter
  6841. followed by zero or more ASCII letters, digits, or
  6842. hyphens (`-`).
  6843. An [attribute](@) consists of spaces, tabs, and up to one line ending,
  6844. an [attribute name], and an optional
  6845. [attribute value specification].
  6846. An [attribute name](@)
  6847. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6848. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6849. specification restricted to ASCII. HTML5 is laxer.)
  6850. An [attribute value specification](@)
  6851. consists of optional spaces, tabs, and up to one line ending,
  6852. a `=` character, optional spaces, tabs, and up to one line ending,
  6853. and an [attribute value].
  6854. An [attribute value](@)
  6855. consists of an [unquoted attribute value],
  6856. a [single-quoted attribute value], or a [double-quoted attribute value].
  6857. An [unquoted attribute value](@)
  6858. is a nonempty string of characters not
  6859. including spaces, tabs, line endings, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6860. A [single-quoted attribute value](@)
  6861. consists of `'`, zero or more
  6862. characters not including `'`, and a final `'`.
  6863. A [double-quoted attribute value](@)
  6864. consists of `"`, zero or more
  6865. characters not including `"`, and a final `"`.
  6866. An [open tag](@) consists of a `<` character, a [tag name],
  6867. zero or more [attributes], optional spaces, tabs, and up to one line ending,
  6868. an optional `/` character, and a `>` character.
  6869. A [closing tag](@) consists of the string `</`, a
  6870. [tag name], optional spaces, tabs, and up to one line ending, and the character
  6871. `>`.
  6872. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6873. where *text* does not start with `>` or `->`, does not end with `-`,
  6874. and does not contain `--`. (See the
  6875. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6876. A [processing instruction](@)
  6877. consists of the string `<?`, a string
  6878. of characters not including the string `?>`, and the string
  6879. `?>`.
  6880. A [declaration](@) consists of the string `<!`, an ASCII letter, zero or more
  6881. characters not including the character `>`, and the character `>`.
  6882. A [CDATA section](@) consists of
  6883. the string `<![CDATA[`, a string of characters not including the string
  6884. `]]>`, and the string `]]>`.
  6885. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6886. an [HTML comment], a [processing instruction], a [declaration],
  6887. or a [CDATA section].
  6888. Here are some simple open tags:
  6889. ```````````````````````````````` example
  6890. <a><bab><c2c>
  6891. .
  6892. <p><a><bab><c2c></p>
  6893. ````````````````````````````````
  6894. Empty elements:
  6895. ```````````````````````````````` example
  6896. <a/><b2/>
  6897. .
  6898. <p><a/><b2/></p>
  6899. ````````````````````````````````
  6900. Whitespace is allowed:
  6901. ```````````````````````````````` example
  6902. <a /><b2
  6903. data="foo" >
  6904. .
  6905. <p><a /><b2
  6906. data="foo" ></p>
  6907. ````````````````````````````````
  6908. With attributes:
  6909. ```````````````````````````````` example
  6910. <a foo="bar" bam = 'baz <em>"</em>'
  6911. _boolean zoop:33=zoop:33 />
  6912. .
  6913. <p><a foo="bar" bam = 'baz <em>"</em>'
  6914. _boolean zoop:33=zoop:33 /></p>
  6915. ````````````````````````````````
  6916. Custom tag names can be used:
  6917. ```````````````````````````````` example
  6918. Foo <responsive-image src="foo.jpg" />
  6919. .
  6920. <p>Foo <responsive-image src="foo.jpg" /></p>
  6921. ````````````````````````````````
  6922. Illegal tag names, not parsed as HTML:
  6923. ```````````````````````````````` example
  6924. <33> <__>
  6925. .
  6926. <p>&lt;33&gt; &lt;__&gt;</p>
  6927. ````````````````````````````````
  6928. Illegal attribute names:
  6929. ```````````````````````````````` example
  6930. <a h*#ref="hi">
  6931. .
  6932. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6933. ````````````````````````````````
  6934. Illegal attribute values:
  6935. ```````````````````````````````` example
  6936. <a href="hi'> <a href=hi'>
  6937. .
  6938. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6939. ````````````````````````````````
  6940. Illegal whitespace:
  6941. ```````````````````````````````` example
  6942. < a><
  6943. foo><bar/ >
  6944. <foo bar=baz
  6945. bim!bop />
  6946. .
  6947. <p>&lt; a&gt;&lt;
  6948. foo&gt;&lt;bar/ &gt;
  6949. &lt;foo bar=baz
  6950. bim!bop /&gt;</p>
  6951. ````````````````````````````````
  6952. Missing whitespace:
  6953. ```````````````````````````````` example
  6954. <a href='bar'title=title>
  6955. .
  6956. <p>&lt;a href='bar'title=title&gt;</p>
  6957. ````````````````````````````````
  6958. Closing tags:
  6959. ```````````````````````````````` example
  6960. </a></foo >
  6961. .
  6962. <p></a></foo ></p>
  6963. ````````````````````````````````
  6964. Illegal attributes in closing tag:
  6965. ```````````````````````````````` example
  6966. </a href="foo">
  6967. .
  6968. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6969. ````````````````````````````````
  6970. Comments:
  6971. ```````````````````````````````` example
  6972. foo <!-- this is a
  6973. comment - with hyphen -->
  6974. .
  6975. <p>foo <!-- this is a
  6976. comment - with hyphen --></p>
  6977. ````````````````````````````````
  6978. ```````````````````````````````` example
  6979. foo <!-- not a comment -- two hyphens -->
  6980. .
  6981. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6982. ````````````````````````````````
  6983. Not comments:
  6984. ```````````````````````````````` example
  6985. foo <!--> foo -->
  6986. foo <!-- foo--->
  6987. .
  6988. <p>foo &lt;!--&gt; foo --&gt;</p>
  6989. <p>foo &lt;!-- foo---&gt;</p>
  6990. ````````````````````````````````
  6991. Processing instructions:
  6992. ```````````````````````````````` example
  6993. foo <?php echo $a; ?>
  6994. .
  6995. <p>foo <?php echo $a; ?></p>
  6996. ````````````````````````````````
  6997. Declarations:
  6998. ```````````````````````````````` example
  6999. foo <!ELEMENT br EMPTY>
  7000. .
  7001. <p>foo <!ELEMENT br EMPTY></p>
  7002. ````````````````````````````````
  7003. CDATA sections:
  7004. ```````````````````````````````` example
  7005. foo <![CDATA[>&<]]>
  7006. .
  7007. <p>foo <![CDATA[>&<]]></p>
  7008. ````````````````````````````````
  7009. Entity and numeric character references are preserved in HTML
  7010. attributes:
  7011. ```````````````````````````````` example
  7012. foo <a href="&ouml;">
  7013. .
  7014. <p>foo <a href="&ouml;"></p>
  7015. ````````````````````````````````
  7016. Backslash escapes do not work in HTML attributes:
  7017. ```````````````````````````````` example
  7018. foo <a href="\*">
  7019. .
  7020. <p>foo <a href="\*"></p>
  7021. ````````````````````````````````
  7022. ```````````````````````````````` example
  7023. <a href="\"">
  7024. .
  7025. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  7026. ````````````````````````````````
  7027. ## Hard line breaks
  7028. A line ending (not in a code span or HTML tag) that is preceded
  7029. by two or more spaces and does not occur at the end of a block
  7030. is parsed as a [hard line break](@) (rendered
  7031. in HTML as a `<br />` tag):
  7032. ```````````````````````````````` example
  7033. foo
  7034. baz
  7035. .
  7036. <p>foo<br />
  7037. baz</p>
  7038. ````````````````````````````````
  7039. For a more visible alternative, a backslash before the
  7040. [line ending] may be used instead of two or more spaces:
  7041. ```````````````````````````````` example
  7042. foo\
  7043. baz
  7044. .
  7045. <p>foo<br />
  7046. baz</p>
  7047. ````````````````````````````````
  7048. More than two spaces can be used:
  7049. ```````````````````````````````` example
  7050. foo
  7051. baz
  7052. .
  7053. <p>foo<br />
  7054. baz</p>
  7055. ````````````````````````````````
  7056. Leading spaces at the beginning of the next line are ignored:
  7057. ```````````````````````````````` example
  7058. foo
  7059. bar
  7060. .
  7061. <p>foo<br />
  7062. bar</p>
  7063. ````````````````````````````````
  7064. ```````````````````````````````` example
  7065. foo\
  7066. bar
  7067. .
  7068. <p>foo<br />
  7069. bar</p>
  7070. ````````````````````````````````
  7071. Hard line breaks can occur inside emphasis, links, and other constructs
  7072. that allow inline content:
  7073. ```````````````````````````````` example
  7074. *foo
  7075. bar*
  7076. .
  7077. <p><em>foo<br />
  7078. bar</em></p>
  7079. ````````````````````````````````
  7080. ```````````````````````````````` example
  7081. *foo\
  7082. bar*
  7083. .
  7084. <p><em>foo<br />
  7085. bar</em></p>
  7086. ````````````````````````````````
  7087. Hard line breaks do not occur inside code spans
  7088. ```````````````````````````````` example
  7089. `code
  7090. span`
  7091. .
  7092. <p><code>code span</code></p>
  7093. ````````````````````````````````
  7094. ```````````````````````````````` example
  7095. `code\
  7096. span`
  7097. .
  7098. <p><code>code\ span</code></p>
  7099. ````````````````````````````````
  7100. or HTML tags:
  7101. ```````````````````````````````` example
  7102. <a href="foo
  7103. bar">
  7104. .
  7105. <p><a href="foo
  7106. bar"></p>
  7107. ````````````````````````````````
  7108. ```````````````````````````````` example
  7109. <a href="foo\
  7110. bar">
  7111. .
  7112. <p><a href="foo\
  7113. bar"></p>
  7114. ````````````````````````````````
  7115. Hard line breaks are for separating inline content within a block.
  7116. Neither syntax for hard line breaks works at the end of a paragraph or
  7117. other block element:
  7118. ```````````````````````````````` example
  7119. foo\
  7120. .
  7121. <p>foo\</p>
  7122. ````````````````````````````````
  7123. ```````````````````````````````` example
  7124. foo
  7125. .
  7126. <p>foo</p>
  7127. ````````````````````````````````
  7128. ```````````````````````````````` example
  7129. ### foo\
  7130. .
  7131. <h3>foo\</h3>
  7132. ````````````````````````````````
  7133. ```````````````````````````````` example
  7134. ### foo
  7135. .
  7136. <h3>foo</h3>
  7137. ````````````````````````````````
  7138. ## Soft line breaks
  7139. A regular line ending (not in a code span or HTML tag) that is not
  7140. preceded by two or more spaces or a backslash is parsed as a
  7141. [softbreak](@). (A soft line break may be rendered in HTML either as a
  7142. [line ending] or as a space. The result will be the same in
  7143. browsers. In the examples here, a [line ending] will be used.)
  7144. ```````````````````````````````` example
  7145. foo
  7146. baz
  7147. .
  7148. <p>foo
  7149. baz</p>
  7150. ````````````````````````````````
  7151. Spaces at the end of the line and beginning of the next line are
  7152. removed:
  7153. ```````````````````````````````` example
  7154. foo
  7155. baz
  7156. .
  7157. <p>foo
  7158. baz</p>
  7159. ````````````````````````````````
  7160. A conforming parser may render a soft line break in HTML either as a
  7161. line ending or as a space.
  7162. A renderer may also provide an option to render soft line breaks
  7163. as hard line breaks.
  7164. ## Textual content
  7165. Any characters not given an interpretation by the above rules will
  7166. be parsed as plain textual content.
  7167. ```````````````````````````````` example
  7168. hello $.;'there
  7169. .
  7170. <p>hello $.;'there</p>
  7171. ````````````````````````````````
  7172. ```````````````````````````````` example
  7173. Foo χρῆν
  7174. .
  7175. <p>Foo χρῆν</p>
  7176. ````````````````````````````````
  7177. Internal spaces are preserved verbatim:
  7178. ```````````````````````````````` example
  7179. Multiple spaces
  7180. .
  7181. <p>Multiple spaces</p>
  7182. ````````````````````````````````
  7183. <!-- END TESTS -->
  7184. # Appendix: A parsing strategy
  7185. In this appendix we describe some features of the parsing strategy
  7186. used in the CommonMark reference implementations.
  7187. ## Overview
  7188. Parsing has two phases:
  7189. 1. In the first phase, lines of input are consumed and the block
  7190. structure of the document---its division into paragraphs, block quotes,
  7191. list items, and so on---is constructed. Text is assigned to these
  7192. blocks but not parsed. Link reference definitions are parsed and a
  7193. map of links is constructed.
  7194. 2. In the second phase, the raw text contents of paragraphs and headings
  7195. are parsed into sequences of Markdown inline elements (strings,
  7196. code spans, links, emphasis, and so on), using the map of link
  7197. references constructed in phase 1.
  7198. At each point in processing, the document is represented as a tree of
  7199. **blocks**. The root of the tree is a `document` block. The `document`
  7200. may have any number of other blocks as **children**. These children
  7201. may, in turn, have other blocks as children. The last child of a block
  7202. is normally considered **open**, meaning that subsequent lines of input
  7203. can alter its contents. (Blocks that are not open are **closed**.)
  7204. Here, for example, is a possible document tree, with the open blocks
  7205. marked by arrows:
  7206. ``` tree
  7207. -> document
  7208. -> block_quote
  7209. paragraph
  7210. "Lorem ipsum dolor\nsit amet."
  7211. -> list (type=bullet tight=true bullet_char=-)
  7212. list_item
  7213. paragraph
  7214. "Qui *quodsi iracundia*"
  7215. -> list_item
  7216. -> paragraph
  7217. "aliquando id"
  7218. ```
  7219. ## Phase 1: block structure
  7220. Each line that is processed has an effect on this tree. The line is
  7221. analyzed and, depending on its contents, the document may be altered
  7222. in one or more of the following ways:
  7223. 1. One or more open blocks may be closed.
  7224. 2. One or more new blocks may be created as children of the
  7225. last open block.
  7226. 3. Text may be added to the last (deepest) open block remaining
  7227. on the tree.
  7228. Once a line has been incorporated into the tree in this way,
  7229. it can be discarded, so input can be read in a stream.
  7230. For each line, we follow this procedure:
  7231. 1. First we iterate through the open blocks, starting with the
  7232. root document, and descending through last children down to the last
  7233. open block. Each block imposes a condition that the line must satisfy
  7234. if the block is to remain open. For example, a block quote requires a
  7235. `>` character. A paragraph requires a non-blank line.
  7236. In this phase we may match all or just some of the open
  7237. blocks. But we cannot close unmatched blocks yet, because we may have a
  7238. [lazy continuation line].
  7239. 2. Next, after consuming the continuation markers for existing
  7240. blocks, we look for new block starts (e.g. `>` for a block quote).
  7241. If we encounter a new block start, we close any blocks unmatched
  7242. in step 1 before creating the new block as a child of the last
  7243. matched container block.
  7244. 3. Finally, we look at the remainder of the line (after block
  7245. markers like `>`, list markers, and indentation have been consumed).
  7246. This is text that can be incorporated into the last open
  7247. block (a paragraph, code block, heading, or raw HTML).
  7248. Setext headings are formed when we see a line of a paragraph
  7249. that is a [setext heading underline].
  7250. Reference link definitions are detected when a paragraph is closed;
  7251. the accumulated text lines are parsed to see if they begin with
  7252. one or more reference link definitions. Any remainder becomes a
  7253. normal paragraph.
  7254. We can see how this works by considering how the tree above is
  7255. generated by four lines of Markdown:
  7256. ``` markdown
  7257. > Lorem ipsum dolor
  7258. sit amet.
  7259. > - Qui *quodsi iracundia*
  7260. > - aliquando id
  7261. ```
  7262. At the outset, our document model is just
  7263. ``` tree
  7264. -> document
  7265. ```
  7266. The first line of our text,
  7267. ``` markdown
  7268. > Lorem ipsum dolor
  7269. ```
  7270. causes a `block_quote` block to be created as a child of our
  7271. open `document` block, and a `paragraph` block as a child of
  7272. the `block_quote`. Then the text is added to the last open
  7273. block, the `paragraph`:
  7274. ``` tree
  7275. -> document
  7276. -> block_quote
  7277. -> paragraph
  7278. "Lorem ipsum dolor"
  7279. ```
  7280. The next line,
  7281. ``` markdown
  7282. sit amet.
  7283. ```
  7284. is a "lazy continuation" of the open `paragraph`, so it gets added
  7285. to the paragraph's text:
  7286. ``` tree
  7287. -> document
  7288. -> block_quote
  7289. -> paragraph
  7290. "Lorem ipsum dolor\nsit amet."
  7291. ```
  7292. The third line,
  7293. ``` markdown
  7294. > - Qui *quodsi iracundia*
  7295. ```
  7296. causes the `paragraph` block to be closed, and a new `list` block
  7297. opened as a child of the `block_quote`. A `list_item` is also
  7298. added as a child of the `list`, and a `paragraph` as a child of
  7299. the `list_item`. The text is then added to the new `paragraph`:
  7300. ``` tree
  7301. -> document
  7302. -> block_quote
  7303. paragraph
  7304. "Lorem ipsum dolor\nsit amet."
  7305. -> list (type=bullet tight=true bullet_char=-)
  7306. -> list_item
  7307. -> paragraph
  7308. "Qui *quodsi iracundia*"
  7309. ```
  7310. The fourth line,
  7311. ``` markdown
  7312. > - aliquando id
  7313. ```
  7314. causes the `list_item` (and its child the `paragraph`) to be closed,
  7315. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7316. is added as a child of the new `list_item`, to contain the text.
  7317. We thus obtain the final tree:
  7318. ``` tree
  7319. -> document
  7320. -> block_quote
  7321. paragraph
  7322. "Lorem ipsum dolor\nsit amet."
  7323. -> list (type=bullet tight=true bullet_char=-)
  7324. list_item
  7325. paragraph
  7326. "Qui *quodsi iracundia*"
  7327. -> list_item
  7328. -> paragraph
  7329. "aliquando id"
  7330. ```
  7331. ## Phase 2: inline structure
  7332. Once all of the input has been parsed, all open blocks are closed.
  7333. We then "walk the tree," visiting every node, and parse raw
  7334. string contents of paragraphs and headings as inlines. At this
  7335. point we have seen all the link reference definitions, so we can
  7336. resolve reference links as we go.
  7337. ``` tree
  7338. document
  7339. block_quote
  7340. paragraph
  7341. str "Lorem ipsum dolor"
  7342. softbreak
  7343. str "sit amet."
  7344. list (type=bullet tight=true bullet_char=-)
  7345. list_item
  7346. paragraph
  7347. str "Qui "
  7348. emph
  7349. str "quodsi iracundia"
  7350. list_item
  7351. paragraph
  7352. str "aliquando id"
  7353. ```
  7354. Notice how the [line ending] in the first paragraph has
  7355. been parsed as a `softbreak`, and the asterisks in the first list item
  7356. have become an `emph`.
  7357. ### An algorithm for parsing nested emphasis and links
  7358. By far the trickiest part of inline parsing is handling emphasis,
  7359. strong emphasis, links, and images. This is done using the following
  7360. algorithm.
  7361. When we're parsing inlines and we hit either
  7362. - a run of `*` or `_` characters, or
  7363. - a `[` or `![`
  7364. we insert a text node with these symbols as its literal content, and we
  7365. add a pointer to this text node to the [delimiter stack](@).
  7366. The [delimiter stack] is a doubly linked list. Each
  7367. element contains a pointer to a text node, plus information about
  7368. - the type of delimiter (`[`, `![`, `*`, `_`)
  7369. - the number of delimiters,
  7370. - whether the delimiter is "active" (all are active to start), and
  7371. - whether the delimiter is a potential opener, a potential closer,
  7372. or both (which depends on what sort of characters precede
  7373. and follow the delimiters).
  7374. When we hit a `]` character, we call the *look for link or image*
  7375. procedure (see below).
  7376. When we hit the end of the input, we call the *process emphasis*
  7377. procedure (see below), with `stack_bottom` = NULL.
  7378. #### *look for link or image*
  7379. Starting at the top of the delimiter stack, we look backwards
  7380. through the stack for an opening `[` or `![` delimiter.
  7381. - If we don't find one, we return a literal text node `]`.
  7382. - If we do find one, but it's not *active*, we remove the inactive
  7383. delimiter from the stack, and return a literal text node `]`.
  7384. - If we find one and it's active, then we parse ahead to see if
  7385. we have an inline link/image, reference link/image, compact reference
  7386. link/image, or shortcut reference link/image.
  7387. + If we don't, then we remove the opening delimiter from the
  7388. delimiter stack and return a literal text node `]`.
  7389. + If we do, then
  7390. * We return a link or image node whose children are the inlines
  7391. after the text node pointed to by the opening delimiter.
  7392. * We run *process emphasis* on these inlines, with the `[` opener
  7393. as `stack_bottom`.
  7394. * We remove the opening delimiter.
  7395. * If we have a link (and not an image), we also set all
  7396. `[` delimiters before the opening delimiter to *inactive*. (This
  7397. will prevent us from getting links within links.)
  7398. #### *process emphasis*
  7399. Parameter `stack_bottom` sets a lower bound to how far we
  7400. descend in the [delimiter stack]. If it is NULL, we can
  7401. go all the way to the bottom. Otherwise, we stop before
  7402. visiting `stack_bottom`.
  7403. Let `current_position` point to the element on the [delimiter stack]
  7404. just above `stack_bottom` (or the first element if `stack_bottom`
  7405. is NULL).
  7406. We keep track of the `openers_bottom` for each delimiter
  7407. type (`*`, `_`) and each length of the closing delimiter run
  7408. (modulo 3). Initialize this to `stack_bottom`.
  7409. Then we repeat the following until we run out of potential
  7410. closers:
  7411. - Move `current_position` forward in the delimiter stack (if needed)
  7412. until we find the first potential closer with delimiter `*` or `_`.
  7413. (This will be the potential closer closest
  7414. to the beginning of the input -- the first one in parse order.)
  7415. - Now, look back in the stack (staying above `stack_bottom` and
  7416. the `openers_bottom` for this delimiter type) for the
  7417. first matching potential opener ("matching" means same delimiter).
  7418. - If one is found:
  7419. + Figure out whether we have emphasis or strong emphasis:
  7420. if both closer and opener spans have length >= 2, we have
  7421. strong, otherwise regular.
  7422. + Insert an emph or strong emph node accordingly, after
  7423. the text node corresponding to the opener.
  7424. + Remove any delimiters between the opener and closer from
  7425. the delimiter stack.
  7426. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7427. from the opening and closing text nodes. If they become empty
  7428. as a result, remove them and remove the corresponding element
  7429. of the delimiter stack. If the closing node is removed, reset
  7430. `current_position` to the next element in the stack.
  7431. - If none is found:
  7432. + Set `openers_bottom` to the element before `current_position`.
  7433. (We know that there are no openers for this kind of closer up to and
  7434. including this point, so this puts a lower bound on future searches.)
  7435. + If the closer at `current_position` is not a potential opener,
  7436. remove it from the delimiter stack (since we know it can't
  7437. be a closer either).
  7438. + Advance `current_position` to the next element in the stack.
  7439. After we're done, we remove all delimiters above `stack_bottom` from the
  7440. delimiter stack.