aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 09442264842eee17810920c9d64e0817e81c5b56 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.28
  5. date: '2017-08-01'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a github wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  252. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  253. A [punctuation character](@) is an [ASCII
  254. punctuation character] or anything in
  255. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  256. ## Tabs
  257. Tabs in lines are not expanded to [spaces]. However,
  258. in contexts where whitespace helps to define block structure,
  259. tabs behave as if they were replaced by spaces with a tab stop
  260. of 4 characters.
  261. Thus, for example, a tab can be used instead of four spaces
  262. in an indented code block. (Note, however, that internal
  263. tabs are passed through as literal tabs, not expanded to
  264. spaces.)
  265. ```````````````````````````````` example
  266. →foo→baz→→bim
  267. .
  268. <pre><code>foo→baz→→bim
  269. </code></pre>
  270. ````````````````````````````````
  271. ```````````````````````````````` example
  272. →foo→baz→→bim
  273. .
  274. <pre><code>foo→baz→→bim
  275. </code></pre>
  276. ````````````````````````````````
  277. ```````````````````````````````` example
  278. a→a
  279. ὐ→a
  280. .
  281. <pre><code>a→a
  282. ὐ→a
  283. </code></pre>
  284. ````````````````````````````````
  285. In the following example, a continuation paragraph of a list
  286. item is indented with a tab; this has exactly the same effect
  287. as indentation with four spaces would:
  288. ```````````````````````````````` example
  289. - foo
  290. →bar
  291. .
  292. <ul>
  293. <li>
  294. <p>foo</p>
  295. <p>bar</p>
  296. </li>
  297. </ul>
  298. ````````````````````````````````
  299. ```````````````````````````````` example
  300. - foo
  301. →→bar
  302. .
  303. <ul>
  304. <li>
  305. <p>foo</p>
  306. <pre><code> bar
  307. </code></pre>
  308. </li>
  309. </ul>
  310. ````````````````````````````````
  311. Normally the `>` that begins a block quote may be followed
  312. optionally by a space, which is not considered part of the
  313. content. In the following case `>` is followed by a tab,
  314. which is treated as if it were expanded into three spaces.
  315. Since one of these spaces is considered part of the
  316. delimiter, `foo` is considered to be indented six spaces
  317. inside the block quote context, so we get an indented
  318. code block starting with two spaces.
  319. ```````````````````````````````` example
  320. >→→foo
  321. .
  322. <blockquote>
  323. <pre><code> foo
  324. </code></pre>
  325. </blockquote>
  326. ````````````````````````````````
  327. ```````````````````````````````` example
  328. -→→foo
  329. .
  330. <ul>
  331. <li>
  332. <pre><code> foo
  333. </code></pre>
  334. </li>
  335. </ul>
  336. ````````````````````````````````
  337. ```````````````````````````````` example
  338. foo
  339. →bar
  340. .
  341. <pre><code>foo
  342. bar
  343. </code></pre>
  344. ````````````````````````````````
  345. ```````````````````````````````` example
  346. - foo
  347. - bar
  348. → - baz
  349. .
  350. <ul>
  351. <li>foo
  352. <ul>
  353. <li>bar
  354. <ul>
  355. <li>baz</li>
  356. </ul>
  357. </li>
  358. </ul>
  359. </li>
  360. </ul>
  361. ````````````````````````````````
  362. ```````````````````````````````` example
  363. #→Foo
  364. .
  365. <h1>Foo</h1>
  366. ````````````````````````````````
  367. ```````````````````````````````` example
  368. *→*→*→
  369. .
  370. <hr />
  371. ````````````````````````````````
  372. ## Insecure characters
  373. For security reasons, the Unicode character `U+0000` must be replaced
  374. with the REPLACEMENT CHARACTER (`U+FFFD`).
  375. # Blocks and inlines
  376. We can think of a document as a sequence of
  377. [blocks](@)---structural elements like paragraphs, block
  378. quotations, lists, headings, rules, and code blocks. Some blocks (like
  379. block quotes and list items) contain other blocks; others (like
  380. headings and paragraphs) contain [inline](@) content---text,
  381. links, emphasized text, images, code spans, and so on.
  382. ## Precedence
  383. Indicators of block structure always take precedence over indicators
  384. of inline structure. So, for example, the following is a list with
  385. two items, not a list with one item containing a code span:
  386. ```````````````````````````````` example
  387. - `one
  388. - two`
  389. .
  390. <ul>
  391. <li>`one</li>
  392. <li>two`</li>
  393. </ul>
  394. ````````````````````````````````
  395. This means that parsing can proceed in two steps: first, the block
  396. structure of the document can be discerned; second, text lines inside
  397. paragraphs, headings, and other block constructs can be parsed for inline
  398. structure. The second step requires information about link reference
  399. definitions that will be available only at the end of the first
  400. step. Note that the first step requires processing lines in sequence,
  401. but the second can be parallelized, since the inline parsing of
  402. one block element does not affect the inline parsing of any other.
  403. ## Container blocks and leaf blocks
  404. We can divide blocks into two types:
  405. [container block](@)s,
  406. which can contain other blocks, and [leaf block](@)s,
  407. which cannot.
  408. # Leaf blocks
  409. This section describes the different kinds of leaf block that make up a
  410. Markdown document.
  411. ## Thematic breaks
  412. A line consisting of 0-3 spaces of indentation, followed by a sequence
  413. of three or more matching `-`, `_`, or `*` characters, each followed
  414. optionally by any number of spaces or tabs, forms a
  415. [thematic break](@).
  416. ```````````````````````````````` example
  417. ***
  418. ---
  419. ___
  420. .
  421. <hr />
  422. <hr />
  423. <hr />
  424. ````````````````````````````````
  425. Wrong characters:
  426. ```````````````````````````````` example
  427. +++
  428. .
  429. <p>+++</p>
  430. ````````````````````````````````
  431. ```````````````````````````````` example
  432. ===
  433. .
  434. <p>===</p>
  435. ````````````````````````````````
  436. Not enough characters:
  437. ```````````````````````````````` example
  438. --
  439. **
  440. __
  441. .
  442. <p>--
  443. **
  444. __</p>
  445. ````````````````````````````````
  446. One to three spaces indent are allowed:
  447. ```````````````````````````````` example
  448. ***
  449. ***
  450. ***
  451. .
  452. <hr />
  453. <hr />
  454. <hr />
  455. ````````````````````````````````
  456. Four spaces is too many:
  457. ```````````````````````````````` example
  458. ***
  459. .
  460. <pre><code>***
  461. </code></pre>
  462. ````````````````````````````````
  463. ```````````````````````````````` example
  464. Foo
  465. ***
  466. .
  467. <p>Foo
  468. ***</p>
  469. ````````````````````````````````
  470. More than three characters may be used:
  471. ```````````````````````````````` example
  472. _____________________________________
  473. .
  474. <hr />
  475. ````````````````````````````````
  476. Spaces are allowed between the characters:
  477. ```````````````````````````````` example
  478. - - -
  479. .
  480. <hr />
  481. ````````````````````````````````
  482. ```````````````````````````````` example
  483. ** * ** * ** * **
  484. .
  485. <hr />
  486. ````````````````````````````````
  487. ```````````````````````````````` example
  488. - - - -
  489. .
  490. <hr />
  491. ````````````````````````````````
  492. Spaces are allowed at the end:
  493. ```````````````````````````````` example
  494. - - - -
  495. .
  496. <hr />
  497. ````````````````````````````````
  498. However, no other characters may occur in the line:
  499. ```````````````````````````````` example
  500. _ _ _ _ a
  501. a------
  502. ---a---
  503. .
  504. <p>_ _ _ _ a</p>
  505. <p>a------</p>
  506. <p>---a---</p>
  507. ````````````````````````````````
  508. It is required that all of the [non-whitespace characters] be the same.
  509. So, this is not a thematic break:
  510. ```````````````````````````````` example
  511. *-*
  512. .
  513. <p><em>-</em></p>
  514. ````````````````````````````````
  515. Thematic breaks do not need blank lines before or after:
  516. ```````````````````````````````` example
  517. - foo
  518. ***
  519. - bar
  520. .
  521. <ul>
  522. <li>foo</li>
  523. </ul>
  524. <hr />
  525. <ul>
  526. <li>bar</li>
  527. </ul>
  528. ````````````````````````````````
  529. Thematic breaks can interrupt a paragraph:
  530. ```````````````````````````````` example
  531. Foo
  532. ***
  533. bar
  534. .
  535. <p>Foo</p>
  536. <hr />
  537. <p>bar</p>
  538. ````````````````````````````````
  539. If a line of dashes that meets the above conditions for being a
  540. thematic break could also be interpreted as the underline of a [setext
  541. heading], the interpretation as a
  542. [setext heading] takes precedence. Thus, for example,
  543. this is a setext heading, not a paragraph followed by a thematic break:
  544. ```````````````````````````````` example
  545. Foo
  546. ---
  547. bar
  548. .
  549. <h2>Foo</h2>
  550. <p>bar</p>
  551. ````````````````````````````````
  552. When both a thematic break and a list item are possible
  553. interpretations of a line, the thematic break takes precedence:
  554. ```````````````````````````````` example
  555. * Foo
  556. * * *
  557. * Bar
  558. .
  559. <ul>
  560. <li>Foo</li>
  561. </ul>
  562. <hr />
  563. <ul>
  564. <li>Bar</li>
  565. </ul>
  566. ````````````````````````````````
  567. If you want a thematic break in a list item, use a different bullet:
  568. ```````````````````````````````` example
  569. - Foo
  570. - * * *
  571. .
  572. <ul>
  573. <li>Foo</li>
  574. <li>
  575. <hr />
  576. </li>
  577. </ul>
  578. ````````````````````````````````
  579. ## ATX headings
  580. An [ATX heading](@)
  581. consists of a string of characters, parsed as inline content, between an
  582. opening sequence of 1--6 unescaped `#` characters and an optional
  583. closing sequence of any number of unescaped `#` characters.
  584. The opening sequence of `#` characters must be followed by a
  585. [space] or by the end of line. The optional closing sequence of `#`s must be
  586. preceded by a [space] and may be followed by spaces only. The opening
  587. `#` character may be indented 0-3 spaces. The raw contents of the
  588. heading are stripped of leading and trailing spaces before being parsed
  589. as inline content. The heading level is equal to the number of `#`
  590. characters in the opening sequence.
  591. Simple headings:
  592. ```````````````````````````````` example
  593. # foo
  594. ## foo
  595. ### foo
  596. #### foo
  597. ##### foo
  598. ###### foo
  599. .
  600. <h1>foo</h1>
  601. <h2>foo</h2>
  602. <h3>foo</h3>
  603. <h4>foo</h4>
  604. <h5>foo</h5>
  605. <h6>foo</h6>
  606. ````````````````````````````````
  607. More than six `#` characters is not a heading:
  608. ```````````````````````````````` example
  609. ####### foo
  610. .
  611. <p>####### foo</p>
  612. ````````````````````````````````
  613. At least one space is required between the `#` characters and the
  614. heading's contents, unless the heading is empty. Note that many
  615. implementations currently do not require the space. However, the
  616. space was required by the
  617. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  618. and it helps prevent things like the following from being parsed as
  619. headings:
  620. ```````````````````````````````` example
  621. #5 bolt
  622. #hashtag
  623. .
  624. <p>#5 bolt</p>
  625. <p>#hashtag</p>
  626. ````````````````````````````````
  627. This is not a heading, because the first `#` is escaped:
  628. ```````````````````````````````` example
  629. \## foo
  630. .
  631. <p>## foo</p>
  632. ````````````````````````````````
  633. Contents are parsed as inlines:
  634. ```````````````````````````````` example
  635. # foo *bar* \*baz\*
  636. .
  637. <h1>foo <em>bar</em> *baz*</h1>
  638. ````````````````````````````````
  639. Leading and trailing blanks are ignored in parsing inline content:
  640. ```````````````````````````````` example
  641. # foo
  642. .
  643. <h1>foo</h1>
  644. ````````````````````````````````
  645. One to three spaces indentation are allowed:
  646. ```````````````````````````````` example
  647. ### foo
  648. ## foo
  649. # foo
  650. .
  651. <h3>foo</h3>
  652. <h2>foo</h2>
  653. <h1>foo</h1>
  654. ````````````````````````````````
  655. Four spaces are too much:
  656. ```````````````````````````````` example
  657. # foo
  658. .
  659. <pre><code># foo
  660. </code></pre>
  661. ````````````````````````````````
  662. ```````````````````````````````` example
  663. foo
  664. # bar
  665. .
  666. <p>foo
  667. # bar</p>
  668. ````````````````````````````````
  669. A closing sequence of `#` characters is optional:
  670. ```````````````````````````````` example
  671. ## foo ##
  672. ### bar ###
  673. .
  674. <h2>foo</h2>
  675. <h3>bar</h3>
  676. ````````````````````````````````
  677. It need not be the same length as the opening sequence:
  678. ```````````````````````````````` example
  679. # foo ##################################
  680. ##### foo ##
  681. .
  682. <h1>foo</h1>
  683. <h5>foo</h5>
  684. ````````````````````````````````
  685. Spaces are allowed after the closing sequence:
  686. ```````````````````````````````` example
  687. ### foo ###
  688. .
  689. <h3>foo</h3>
  690. ````````````````````````````````
  691. A sequence of `#` characters with anything but [spaces] following it
  692. is not a closing sequence, but counts as part of the contents of the
  693. heading:
  694. ```````````````````````````````` example
  695. ### foo ### b
  696. .
  697. <h3>foo ### b</h3>
  698. ````````````````````````````````
  699. The closing sequence must be preceded by a space:
  700. ```````````````````````````````` example
  701. # foo#
  702. .
  703. <h1>foo#</h1>
  704. ````````````````````````````````
  705. Backslash-escaped `#` characters do not count as part
  706. of the closing sequence:
  707. ```````````````````````````````` example
  708. ### foo \###
  709. ## foo #\##
  710. # foo \#
  711. .
  712. <h3>foo ###</h3>
  713. <h2>foo ###</h2>
  714. <h1>foo #</h1>
  715. ````````````````````````````````
  716. ATX headings need not be separated from surrounding content by blank
  717. lines, and they can interrupt paragraphs:
  718. ```````````````````````````````` example
  719. ****
  720. ## foo
  721. ****
  722. .
  723. <hr />
  724. <h2>foo</h2>
  725. <hr />
  726. ````````````````````````````````
  727. ```````````````````````````````` example
  728. Foo bar
  729. # baz
  730. Bar foo
  731. .
  732. <p>Foo bar</p>
  733. <h1>baz</h1>
  734. <p>Bar foo</p>
  735. ````````````````````````````````
  736. ATX headings can be empty:
  737. ```````````````````````````````` example
  738. ##
  739. #
  740. ### ###
  741. .
  742. <h2></h2>
  743. <h1></h1>
  744. <h3></h3>
  745. ````````````````````````````````
  746. ## Setext headings
  747. A [setext heading](@) consists of one or more
  748. lines of text, each containing at least one [non-whitespace
  749. character], with no more than 3 spaces indentation, followed by
  750. a [setext heading underline]. The lines of text must be such
  751. that, were they not followed by the setext heading underline,
  752. they would be interpreted as a paragraph: they cannot be
  753. interpretable as a [code fence], [ATX heading][ATX headings],
  754. [block quote][block quotes], [thematic break][thematic breaks],
  755. [list item][list items], or [HTML block][HTML blocks].
  756. A [setext heading underline](@) is a sequence of
  757. `=` characters or a sequence of `-` characters, with no more than 3
  758. spaces indentation and any number of trailing spaces. If a line
  759. containing a single `-` can be interpreted as an
  760. empty [list items], it should be interpreted this way
  761. and not as a [setext heading underline].
  762. The heading is a level 1 heading if `=` characters are used in
  763. the [setext heading underline], and a level 2 heading if `-`
  764. characters are used. The contents of the heading are the result
  765. of parsing the preceding lines of text as CommonMark inline
  766. content.
  767. In general, a setext heading need not be preceded or followed by a
  768. blank line. However, it cannot interrupt a paragraph, so when a
  769. setext heading comes after a paragraph, a blank line is needed between
  770. them.
  771. Simple examples:
  772. ```````````````````````````````` example
  773. Foo *bar*
  774. =========
  775. Foo *bar*
  776. ---------
  777. .
  778. <h1>Foo <em>bar</em></h1>
  779. <h2>Foo <em>bar</em></h2>
  780. ````````````````````````````````
  781. The content of the header may span more than one line:
  782. ```````````````````````````````` example
  783. Foo *bar
  784. baz*
  785. ====
  786. .
  787. <h1>Foo <em>bar
  788. baz</em></h1>
  789. ````````````````````````````````
  790. The underlining can be any length:
  791. ```````````````````````````````` example
  792. Foo
  793. -------------------------
  794. Foo
  795. =
  796. .
  797. <h2>Foo</h2>
  798. <h1>Foo</h1>
  799. ````````````````````````````````
  800. The heading content can be indented up to three spaces, and need
  801. not line up with the underlining:
  802. ```````````````````````````````` example
  803. Foo
  804. ---
  805. Foo
  806. -----
  807. Foo
  808. ===
  809. .
  810. <h2>Foo</h2>
  811. <h2>Foo</h2>
  812. <h1>Foo</h1>
  813. ````````````````````````````````
  814. Four spaces indent is too much:
  815. ```````````````````````````````` example
  816. Foo
  817. ---
  818. Foo
  819. ---
  820. .
  821. <pre><code>Foo
  822. ---
  823. Foo
  824. </code></pre>
  825. <hr />
  826. ````````````````````````````````
  827. The setext heading underline can be indented up to three spaces, and
  828. may have trailing spaces:
  829. ```````````````````````````````` example
  830. Foo
  831. ----
  832. .
  833. <h2>Foo</h2>
  834. ````````````````````````````````
  835. Four spaces is too much:
  836. ```````````````````````````````` example
  837. Foo
  838. ---
  839. .
  840. <p>Foo
  841. ---</p>
  842. ````````````````````````````````
  843. The setext heading underline cannot contain internal spaces:
  844. ```````````````````````````````` example
  845. Foo
  846. = =
  847. Foo
  848. --- -
  849. .
  850. <p>Foo
  851. = =</p>
  852. <p>Foo</p>
  853. <hr />
  854. ````````````````````````````````
  855. Trailing spaces in the content line do not cause a line break:
  856. ```````````````````````````````` example
  857. Foo
  858. -----
  859. .
  860. <h2>Foo</h2>
  861. ````````````````````````````````
  862. Nor does a backslash at the end:
  863. ```````````````````````````````` example
  864. Foo\
  865. ----
  866. .
  867. <h2>Foo\</h2>
  868. ````````````````````````````````
  869. Since indicators of block structure take precedence over
  870. indicators of inline structure, the following are setext headings:
  871. ```````````````````````````````` example
  872. `Foo
  873. ----
  874. `
  875. <a title="a lot
  876. ---
  877. of dashes"/>
  878. .
  879. <h2>`Foo</h2>
  880. <p>`</p>
  881. <h2>&lt;a title=&quot;a lot</h2>
  882. <p>of dashes&quot;/&gt;</p>
  883. ````````````````````````````````
  884. The setext heading underline cannot be a [lazy continuation
  885. line] in a list item or block quote:
  886. ```````````````````````````````` example
  887. > Foo
  888. ---
  889. .
  890. <blockquote>
  891. <p>Foo</p>
  892. </blockquote>
  893. <hr />
  894. ````````````````````````````````
  895. ```````````````````````````````` example
  896. > foo
  897. bar
  898. ===
  899. .
  900. <blockquote>
  901. <p>foo
  902. bar
  903. ===</p>
  904. </blockquote>
  905. ````````````````````````````````
  906. ```````````````````````````````` example
  907. - Foo
  908. ---
  909. .
  910. <ul>
  911. <li>Foo</li>
  912. </ul>
  913. <hr />
  914. ````````````````````````````````
  915. A blank line is needed between a paragraph and a following
  916. setext heading, since otherwise the paragraph becomes part
  917. of the heading's content:
  918. ```````````````````````````````` example
  919. Foo
  920. Bar
  921. ---
  922. .
  923. <h2>Foo
  924. Bar</h2>
  925. ````````````````````````````````
  926. But in general a blank line is not required before or after
  927. setext headings:
  928. ```````````````````````````````` example
  929. ---
  930. Foo
  931. ---
  932. Bar
  933. ---
  934. Baz
  935. .
  936. <hr />
  937. <h2>Foo</h2>
  938. <h2>Bar</h2>
  939. <p>Baz</p>
  940. ````````````````````````````````
  941. Setext headings cannot be empty:
  942. ```````````````````````````````` example
  943. ====
  944. .
  945. <p>====</p>
  946. ````````````````````````````````
  947. Setext heading text lines must not be interpretable as block
  948. constructs other than paragraphs. So, the line of dashes
  949. in these examples gets interpreted as a thematic break:
  950. ```````````````````````````````` example
  951. ---
  952. ---
  953. .
  954. <hr />
  955. <hr />
  956. ````````````````````````````````
  957. ```````````````````````````````` example
  958. - foo
  959. -----
  960. .
  961. <ul>
  962. <li>foo</li>
  963. </ul>
  964. <hr />
  965. ````````````````````````````````
  966. ```````````````````````````````` example
  967. foo
  968. ---
  969. .
  970. <pre><code>foo
  971. </code></pre>
  972. <hr />
  973. ````````````````````````````````
  974. ```````````````````````````````` example
  975. > foo
  976. -----
  977. .
  978. <blockquote>
  979. <p>foo</p>
  980. </blockquote>
  981. <hr />
  982. ````````````````````````````````
  983. If you want a heading with `> foo` as its literal text, you can
  984. use backslash escapes:
  985. ```````````````````````````````` example
  986. \> foo
  987. ------
  988. .
  989. <h2>&gt; foo</h2>
  990. ````````````````````````````````
  991. **Compatibility note:** Most existing Markdown implementations
  992. do not allow the text of setext headings to span multiple lines.
  993. But there is no consensus about how to interpret
  994. ``` markdown
  995. Foo
  996. bar
  997. ---
  998. baz
  999. ```
  1000. One can find four different interpretations:
  1001. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1002. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1003. 3. paragraph "Foo bar --- baz"
  1004. 4. heading "Foo bar", paragraph "baz"
  1005. We find interpretation 4 most natural, and interpretation 4
  1006. increases the expressive power of CommonMark, by allowing
  1007. multiline headings. Authors who want interpretation 1 can
  1008. put a blank line after the first paragraph:
  1009. ```````````````````````````````` example
  1010. Foo
  1011. bar
  1012. ---
  1013. baz
  1014. .
  1015. <p>Foo</p>
  1016. <h2>bar</h2>
  1017. <p>baz</p>
  1018. ````````````````````````````````
  1019. Authors who want interpretation 2 can put blank lines around
  1020. the thematic break,
  1021. ```````````````````````````````` example
  1022. Foo
  1023. bar
  1024. ---
  1025. baz
  1026. .
  1027. <p>Foo
  1028. bar</p>
  1029. <hr />
  1030. <p>baz</p>
  1031. ````````````````````````````````
  1032. or use a thematic break that cannot count as a [setext heading
  1033. underline], such as
  1034. ```````````````````````````````` example
  1035. Foo
  1036. bar
  1037. * * *
  1038. baz
  1039. .
  1040. <p>Foo
  1041. bar</p>
  1042. <hr />
  1043. <p>baz</p>
  1044. ````````````````````````````````
  1045. Authors who want interpretation 3 can use backslash escapes:
  1046. ```````````````````````````````` example
  1047. Foo
  1048. bar
  1049. \---
  1050. baz
  1051. .
  1052. <p>Foo
  1053. bar
  1054. ---
  1055. baz</p>
  1056. ````````````````````````````````
  1057. ## Indented code blocks
  1058. An [indented code block](@) is composed of one or more
  1059. [indented chunks] separated by blank lines.
  1060. An [indented chunk](@) is a sequence of non-blank lines,
  1061. each indented four or more spaces. The contents of the code block are
  1062. the literal contents of the lines, including trailing
  1063. [line endings], minus four spaces of indentation.
  1064. An indented code block has no [info string].
  1065. An indented code block cannot interrupt a paragraph, so there must be
  1066. a blank line between a paragraph and a following indented code block.
  1067. (A blank line is not needed, however, between a code block and a following
  1068. paragraph.)
  1069. ```````````````````````````````` example
  1070. a simple
  1071. indented code block
  1072. .
  1073. <pre><code>a simple
  1074. indented code block
  1075. </code></pre>
  1076. ````````````````````````````````
  1077. If there is any ambiguity between an interpretation of indentation
  1078. as a code block and as indicating that material belongs to a [list
  1079. item][list items], the list item interpretation takes precedence:
  1080. ```````````````````````````````` example
  1081. - foo
  1082. bar
  1083. .
  1084. <ul>
  1085. <li>
  1086. <p>foo</p>
  1087. <p>bar</p>
  1088. </li>
  1089. </ul>
  1090. ````````````````````````````````
  1091. ```````````````````````````````` example
  1092. 1. foo
  1093. - bar
  1094. .
  1095. <ol>
  1096. <li>
  1097. <p>foo</p>
  1098. <ul>
  1099. <li>bar</li>
  1100. </ul>
  1101. </li>
  1102. </ol>
  1103. ````````````````````````````````
  1104. The contents of a code block are literal text, and do not get parsed
  1105. as Markdown:
  1106. ```````````````````````````````` example
  1107. <a/>
  1108. *hi*
  1109. - one
  1110. .
  1111. <pre><code>&lt;a/&gt;
  1112. *hi*
  1113. - one
  1114. </code></pre>
  1115. ````````````````````````````````
  1116. Here we have three chunks separated by blank lines:
  1117. ```````````````````````````````` example
  1118. chunk1
  1119. chunk2
  1120. chunk3
  1121. .
  1122. <pre><code>chunk1
  1123. chunk2
  1124. chunk3
  1125. </code></pre>
  1126. ````````````````````````````````
  1127. Any initial spaces beyond four will be included in the content, even
  1128. in interior blank lines:
  1129. ```````````````````````````````` example
  1130. chunk1
  1131. chunk2
  1132. .
  1133. <pre><code>chunk1
  1134. chunk2
  1135. </code></pre>
  1136. ````````````````````````````````
  1137. An indented code block cannot interrupt a paragraph. (This
  1138. allows hanging indents and the like.)
  1139. ```````````````````````````````` example
  1140. Foo
  1141. bar
  1142. .
  1143. <p>Foo
  1144. bar</p>
  1145. ````````````````````````````````
  1146. However, any non-blank line with fewer than four leading spaces ends
  1147. the code block immediately. So a paragraph may occur immediately
  1148. after indented code:
  1149. ```````````````````````````````` example
  1150. foo
  1151. bar
  1152. .
  1153. <pre><code>foo
  1154. </code></pre>
  1155. <p>bar</p>
  1156. ````````````````````````````````
  1157. And indented code can occur immediately before and after other kinds of
  1158. blocks:
  1159. ```````````````````````````````` example
  1160. # Heading
  1161. foo
  1162. Heading
  1163. ------
  1164. foo
  1165. ----
  1166. .
  1167. <h1>Heading</h1>
  1168. <pre><code>foo
  1169. </code></pre>
  1170. <h2>Heading</h2>
  1171. <pre><code>foo
  1172. </code></pre>
  1173. <hr />
  1174. ````````````````````````````````
  1175. The first line can be indented more than four spaces:
  1176. ```````````````````````````````` example
  1177. foo
  1178. bar
  1179. .
  1180. <pre><code> foo
  1181. bar
  1182. </code></pre>
  1183. ````````````````````````````````
  1184. Blank lines preceding or following an indented code block
  1185. are not included in it:
  1186. ```````````````````````````````` example
  1187. foo
  1188. .
  1189. <pre><code>foo
  1190. </code></pre>
  1191. ````````````````````````````````
  1192. Trailing spaces are included in the code block's content:
  1193. ```````````````````````````````` example
  1194. foo
  1195. .
  1196. <pre><code>foo
  1197. </code></pre>
  1198. ````````````````````````````````
  1199. ## Fenced code blocks
  1200. A [code fence](@) is a sequence
  1201. of at least three consecutive backtick characters (`` ` ``) or
  1202. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1203. A [fenced code block](@)
  1204. begins with a code fence, indented no more than three spaces.
  1205. The line with the opening code fence may optionally contain some text
  1206. following the code fence; this is trimmed of leading and trailing
  1207. whitespace and called the [info string](@).
  1208. The [info string] may not contain any backtick
  1209. characters. (The reason for this restriction is that otherwise
  1210. some inline code would be incorrectly interpreted as the
  1211. beginning of a fenced code block.)
  1212. The content of the code block consists of all subsequent lines, until
  1213. a closing [code fence] of the same type as the code block
  1214. began with (backticks or tildes), and with at least as many backticks
  1215. or tildes as the opening code fence. If the leading code fence is
  1216. indented N spaces, then up to N spaces of indentation are removed from
  1217. each line of the content (if present). (If a content line is not
  1218. indented, it is preserved unchanged. If it is indented less than N
  1219. spaces, all of the indentation is removed.)
  1220. The closing code fence may be indented up to three spaces, and may be
  1221. followed only by spaces, which are ignored. If the end of the
  1222. containing block (or document) is reached and no closing code fence
  1223. has been found, the code block contains all of the lines after the
  1224. opening code fence until the end of the containing block (or
  1225. document). (An alternative spec would require backtracking in the
  1226. event that a closing code fence is not found. But this makes parsing
  1227. much less efficient, and there seems to be no real down side to the
  1228. behavior described here.)
  1229. A fenced code block may interrupt a paragraph, and does not require
  1230. a blank line either before or after.
  1231. The content of a code fence is treated as literal text, not parsed
  1232. as inlines. The first word of the [info string] is typically used to
  1233. specify the language of the code sample, and rendered in the `class`
  1234. attribute of the `code` tag. However, this spec does not mandate any
  1235. particular treatment of the [info string].
  1236. Here is a simple example with backticks:
  1237. ```````````````````````````````` example
  1238. ```
  1239. <
  1240. >
  1241. ```
  1242. .
  1243. <pre><code>&lt;
  1244. &gt;
  1245. </code></pre>
  1246. ````````````````````````````````
  1247. With tildes:
  1248. ```````````````````````````````` example
  1249. ~~~
  1250. <
  1251. >
  1252. ~~~
  1253. .
  1254. <pre><code>&lt;
  1255. &gt;
  1256. </code></pre>
  1257. ````````````````````````````````
  1258. Fewer than three backticks is not enough:
  1259. ```````````````````````````````` example
  1260. ``
  1261. foo
  1262. ``
  1263. .
  1264. <p><code>foo</code></p>
  1265. ````````````````````````````````
  1266. The closing code fence must use the same character as the opening
  1267. fence:
  1268. ```````````````````````````````` example
  1269. ```
  1270. aaa
  1271. ~~~
  1272. ```
  1273. .
  1274. <pre><code>aaa
  1275. ~~~
  1276. </code></pre>
  1277. ````````````````````````````````
  1278. ```````````````````````````````` example
  1279. ~~~
  1280. aaa
  1281. ```
  1282. ~~~
  1283. .
  1284. <pre><code>aaa
  1285. ```
  1286. </code></pre>
  1287. ````````````````````````````````
  1288. The closing code fence must be at least as long as the opening fence:
  1289. ```````````````````````````````` example
  1290. ````
  1291. aaa
  1292. ```
  1293. ``````
  1294. .
  1295. <pre><code>aaa
  1296. ```
  1297. </code></pre>
  1298. ````````````````````````````````
  1299. ```````````````````````````````` example
  1300. ~~~~
  1301. aaa
  1302. ~~~
  1303. ~~~~
  1304. .
  1305. <pre><code>aaa
  1306. ~~~
  1307. </code></pre>
  1308. ````````````````````````````````
  1309. Unclosed code blocks are closed by the end of the document
  1310. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1311. ```````````````````````````````` example
  1312. ```
  1313. .
  1314. <pre><code></code></pre>
  1315. ````````````````````````````````
  1316. ```````````````````````````````` example
  1317. `````
  1318. ```
  1319. aaa
  1320. .
  1321. <pre><code>
  1322. ```
  1323. aaa
  1324. </code></pre>
  1325. ````````````````````````````````
  1326. ```````````````````````````````` example
  1327. > ```
  1328. > aaa
  1329. bbb
  1330. .
  1331. <blockquote>
  1332. <pre><code>aaa
  1333. </code></pre>
  1334. </blockquote>
  1335. <p>bbb</p>
  1336. ````````````````````````````````
  1337. A code block can have all empty lines as its content:
  1338. ```````````````````````````````` example
  1339. ```
  1340. ```
  1341. .
  1342. <pre><code>
  1343. </code></pre>
  1344. ````````````````````````````````
  1345. A code block can be empty:
  1346. ```````````````````````````````` example
  1347. ```
  1348. ```
  1349. .
  1350. <pre><code></code></pre>
  1351. ````````````````````````````````
  1352. Fences can be indented. If the opening fence is indented,
  1353. content lines will have equivalent opening indentation removed,
  1354. if present:
  1355. ```````````````````````````````` example
  1356. ```
  1357. aaa
  1358. aaa
  1359. ```
  1360. .
  1361. <pre><code>aaa
  1362. aaa
  1363. </code></pre>
  1364. ````````````````````````````````
  1365. ```````````````````````````````` example
  1366. ```
  1367. aaa
  1368. aaa
  1369. aaa
  1370. ```
  1371. .
  1372. <pre><code>aaa
  1373. aaa
  1374. aaa
  1375. </code></pre>
  1376. ````````````````````````````````
  1377. ```````````````````````````````` example
  1378. ```
  1379. aaa
  1380. aaa
  1381. aaa
  1382. ```
  1383. .
  1384. <pre><code>aaa
  1385. aaa
  1386. aaa
  1387. </code></pre>
  1388. ````````````````````````````````
  1389. Four spaces indentation produces an indented code block:
  1390. ```````````````````````````````` example
  1391. ```
  1392. aaa
  1393. ```
  1394. .
  1395. <pre><code>```
  1396. aaa
  1397. ```
  1398. </code></pre>
  1399. ````````````````````````````````
  1400. Closing fences may be indented by 0-3 spaces, and their indentation
  1401. need not match that of the opening fence:
  1402. ```````````````````````````````` example
  1403. ```
  1404. aaa
  1405. ```
  1406. .
  1407. <pre><code>aaa
  1408. </code></pre>
  1409. ````````````````````````````````
  1410. ```````````````````````````````` example
  1411. ```
  1412. aaa
  1413. ```
  1414. .
  1415. <pre><code>aaa
  1416. </code></pre>
  1417. ````````````````````````````````
  1418. This is not a closing fence, because it is indented 4 spaces:
  1419. ```````````````````````````````` example
  1420. ```
  1421. aaa
  1422. ```
  1423. .
  1424. <pre><code>aaa
  1425. ```
  1426. </code></pre>
  1427. ````````````````````````````````
  1428. Code fences (opening and closing) cannot contain internal spaces:
  1429. ```````````````````````````````` example
  1430. ``` ```
  1431. aaa
  1432. .
  1433. <p><code></code>
  1434. aaa</p>
  1435. ````````````````````````````````
  1436. ```````````````````````````````` example
  1437. ~~~~~~
  1438. aaa
  1439. ~~~ ~~
  1440. .
  1441. <pre><code>aaa
  1442. ~~~ ~~
  1443. </code></pre>
  1444. ````````````````````````````````
  1445. Fenced code blocks can interrupt paragraphs, and can be followed
  1446. directly by paragraphs, without a blank line between:
  1447. ```````````````````````````````` example
  1448. foo
  1449. ```
  1450. bar
  1451. ```
  1452. baz
  1453. .
  1454. <p>foo</p>
  1455. <pre><code>bar
  1456. </code></pre>
  1457. <p>baz</p>
  1458. ````````````````````````````````
  1459. Other blocks can also occur before and after fenced code blocks
  1460. without an intervening blank line:
  1461. ```````````````````````````````` example
  1462. foo
  1463. ---
  1464. ~~~
  1465. bar
  1466. ~~~
  1467. # baz
  1468. .
  1469. <h2>foo</h2>
  1470. <pre><code>bar
  1471. </code></pre>
  1472. <h1>baz</h1>
  1473. ````````````````````````````````
  1474. An [info string] can be provided after the opening code fence.
  1475. Opening and closing spaces will be stripped, and the first word, prefixed
  1476. with `language-`, is used as the value for the `class` attribute of the
  1477. `code` element within the enclosing `pre` element.
  1478. ```````````````````````````````` example
  1479. ```ruby
  1480. def foo(x)
  1481. return 3
  1482. end
  1483. ```
  1484. .
  1485. <pre><code class="language-ruby">def foo(x)
  1486. return 3
  1487. end
  1488. </code></pre>
  1489. ````````````````````````````````
  1490. ```````````````````````````````` example
  1491. ~~~~ ruby startline=3 $%@#$
  1492. def foo(x)
  1493. return 3
  1494. end
  1495. ~~~~~~~
  1496. .
  1497. <pre><code class="language-ruby">def foo(x)
  1498. return 3
  1499. end
  1500. </code></pre>
  1501. ````````````````````````````````
  1502. ```````````````````````````````` example
  1503. ````;
  1504. ````
  1505. .
  1506. <pre><code class="language-;"></code></pre>
  1507. ````````````````````````````````
  1508. [Info strings] for backtick code blocks cannot contain backticks:
  1509. ```````````````````````````````` example
  1510. ``` aa ```
  1511. foo
  1512. .
  1513. <p><code>aa</code>
  1514. foo</p>
  1515. ````````````````````````````````
  1516. Closing code fences cannot have [info strings]:
  1517. ```````````````````````````````` example
  1518. ```
  1519. ``` aaa
  1520. ```
  1521. .
  1522. <pre><code>``` aaa
  1523. </code></pre>
  1524. ````````````````````````````````
  1525. ## HTML blocks
  1526. An [HTML block](@) is a group of lines that is treated
  1527. as raw HTML (and will not be escaped in HTML output).
  1528. There are seven kinds of [HTML block], which can be defined
  1529. by their start and end conditions. The block begins with a line that
  1530. meets a [start condition](@) (after up to three spaces
  1531. optional indentation). It ends with the first subsequent line that
  1532. meets a matching [end condition](@), or the last line of
  1533. the document or other [container block]), if no line is encountered that meets the
  1534. [end condition]. If the first line meets both the [start condition]
  1535. and the [end condition], the block will contain just that line.
  1536. 1. **Start condition:** line begins with the string `<script`,
  1537. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1538. the string `>`, or the end of the line.\
  1539. **End condition:** line contains an end tag
  1540. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1541. need not match the start tag).
  1542. 2. **Start condition:** line begins with the string `<!--`.\
  1543. **End condition:** line contains the string `-->`.
  1544. 3. **Start condition:** line begins with the string `<?`.\
  1545. **End condition:** line contains the string `?>`.
  1546. 4. **Start condition:** line begins with the string `<!`
  1547. followed by an uppercase ASCII letter.\
  1548. **End condition:** line contains the character `>`.
  1549. 5. **Start condition:** line begins with the string
  1550. `<![CDATA[`.\
  1551. **End condition:** line contains the string `]]>`.
  1552. 6. **Start condition:** line begins the string `<` or `</`
  1553. followed by one of the strings (case-insensitive) `address`,
  1554. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1555. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1556. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1557. `footer`, `form`, `frame`, `frameset`,
  1558. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1559. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1560. `meta`, `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1561. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1562. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1563. by [whitespace], the end of the line, the string `>`, or
  1564. the string `/>`.\
  1565. **End condition:** line is followed by a [blank line].
  1566. 7. **Start condition:** line begins with a complete [open tag]
  1567. or [closing tag] (with any [tag name] other than `script`,
  1568. `style`, or `pre`) followed only by [whitespace]
  1569. or the end of the line.\
  1570. **End condition:** line is followed by a [blank line].
  1571. HTML blocks continue until they are closed by their appropriate
  1572. [end condition], or the last line of the document or other [container block].
  1573. This means any HTML **within an HTML block** that might otherwise be recognised
  1574. as a start condition will be ignored by the parser and passed through as-is,
  1575. without changing the parser's state.
  1576. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1577. the parser state; as the HTML block was started in by start condition 6, it
  1578. will end at any blank line. This can be surprising:
  1579. ```````````````````````````````` example
  1580. <table><tr><td>
  1581. <pre>
  1582. **Hello**,
  1583. _world_.
  1584. </pre>
  1585. </td></tr></table>
  1586. .
  1587. <table><tr><td>
  1588. <pre>
  1589. **Hello**,
  1590. <p><em>world</em>.
  1591. </pre></p>
  1592. </td></tr></table>
  1593. ````````````````````````````````
  1594. In this case, the HTML block is terminated by the newline — the `**Hello**`
  1595. text remains verbatim — and regular parsing resumes, with a paragraph,
  1596. emphasised `world` and inline and block HTML following.
  1597. All types of [HTML blocks] except type 7 may interrupt
  1598. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1599. (This restriction is intended to prevent unwanted interpretation
  1600. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1601. Some simple examples follow. Here are some basic HTML blocks
  1602. of type 6:
  1603. ```````````````````````````````` example
  1604. <table>
  1605. <tr>
  1606. <td>
  1607. hi
  1608. </td>
  1609. </tr>
  1610. </table>
  1611. okay.
  1612. .
  1613. <table>
  1614. <tr>
  1615. <td>
  1616. hi
  1617. </td>
  1618. </tr>
  1619. </table>
  1620. <p>okay.</p>
  1621. ````````````````````````````````
  1622. ```````````````````````````````` example
  1623. <div>
  1624. *hello*
  1625. <foo><a>
  1626. .
  1627. <div>
  1628. *hello*
  1629. <foo><a>
  1630. ````````````````````````````````
  1631. A block can also start with a closing tag:
  1632. ```````````````````````````````` example
  1633. </div>
  1634. *foo*
  1635. .
  1636. </div>
  1637. *foo*
  1638. ````````````````````````````````
  1639. Here we have two HTML blocks with a Markdown paragraph between them:
  1640. ```````````````````````````````` example
  1641. <DIV CLASS="foo">
  1642. *Markdown*
  1643. </DIV>
  1644. .
  1645. <DIV CLASS="foo">
  1646. <p><em>Markdown</em></p>
  1647. </DIV>
  1648. ````````````````````````````````
  1649. The tag on the first line can be partial, as long
  1650. as it is split where there would be whitespace:
  1651. ```````````````````````````````` example
  1652. <div id="foo"
  1653. class="bar">
  1654. </div>
  1655. .
  1656. <div id="foo"
  1657. class="bar">
  1658. </div>
  1659. ````````````````````````````````
  1660. ```````````````````````````````` example
  1661. <div id="foo" class="bar
  1662. baz">
  1663. </div>
  1664. .
  1665. <div id="foo" class="bar
  1666. baz">
  1667. </div>
  1668. ````````````````````````````````
  1669. An open tag need not be closed:
  1670. ```````````````````````````````` example
  1671. <div>
  1672. *foo*
  1673. *bar*
  1674. .
  1675. <div>
  1676. *foo*
  1677. <p><em>bar</em></p>
  1678. ````````````````````````````````
  1679. A partial tag need not even be completed (garbage
  1680. in, garbage out):
  1681. ```````````````````````````````` example
  1682. <div id="foo"
  1683. *hi*
  1684. .
  1685. <div id="foo"
  1686. *hi*
  1687. ````````````````````````````````
  1688. ```````````````````````````````` example
  1689. <div class
  1690. foo
  1691. .
  1692. <div class
  1693. foo
  1694. ````````````````````````````````
  1695. The initial tag doesn't even need to be a valid
  1696. tag, as long as it starts like one:
  1697. ```````````````````````````````` example
  1698. <div *???-&&&-<---
  1699. *foo*
  1700. .
  1701. <div *???-&&&-<---
  1702. *foo*
  1703. ````````````````````````````````
  1704. In type 6 blocks, the initial tag need not be on a line by
  1705. itself:
  1706. ```````````````````````````````` example
  1707. <div><a href="bar">*foo*</a></div>
  1708. .
  1709. <div><a href="bar">*foo*</a></div>
  1710. ````````````````````````````````
  1711. ```````````````````````````````` example
  1712. <table><tr><td>
  1713. foo
  1714. </td></tr></table>
  1715. .
  1716. <table><tr><td>
  1717. foo
  1718. </td></tr></table>
  1719. ````````````````````````````````
  1720. Everything until the next blank line or end of document
  1721. gets included in the HTML block. So, in the following
  1722. example, what looks like a Markdown code block
  1723. is actually part of the HTML block, which continues until a blank
  1724. line or the end of the document is reached:
  1725. ```````````````````````````````` example
  1726. <div></div>
  1727. ``` c
  1728. int x = 33;
  1729. ```
  1730. .
  1731. <div></div>
  1732. ``` c
  1733. int x = 33;
  1734. ```
  1735. ````````````````````````````````
  1736. To start an [HTML block] with a tag that is *not* in the
  1737. list of block-level tags in (6), you must put the tag by
  1738. itself on the first line (and it must be complete):
  1739. ```````````````````````````````` example
  1740. <a href="foo">
  1741. *bar*
  1742. </a>
  1743. .
  1744. <a href="foo">
  1745. *bar*
  1746. </a>
  1747. ````````````````````````````````
  1748. In type 7 blocks, the [tag name] can be anything:
  1749. ```````````````````````````````` example
  1750. <Warning>
  1751. *bar*
  1752. </Warning>
  1753. .
  1754. <Warning>
  1755. *bar*
  1756. </Warning>
  1757. ````````````````````````````````
  1758. ```````````````````````````````` example
  1759. <i class="foo">
  1760. *bar*
  1761. </i>
  1762. .
  1763. <i class="foo">
  1764. *bar*
  1765. </i>
  1766. ````````````````````````````````
  1767. ```````````````````````````````` example
  1768. </ins>
  1769. *bar*
  1770. .
  1771. </ins>
  1772. *bar*
  1773. ````````````````````````````````
  1774. These rules are designed to allow us to work with tags that
  1775. can function as either block-level or inline-level tags.
  1776. The `<del>` tag is a nice example. We can surround content with
  1777. `<del>` tags in three different ways. In this case, we get a raw
  1778. HTML block, because the `<del>` tag is on a line by itself:
  1779. ```````````````````````````````` example
  1780. <del>
  1781. *foo*
  1782. </del>
  1783. .
  1784. <del>
  1785. *foo*
  1786. </del>
  1787. ````````````````````````````````
  1788. In this case, we get a raw HTML block that just includes
  1789. the `<del>` tag (because it ends with the following blank
  1790. line). So the contents get interpreted as CommonMark:
  1791. ```````````````````````````````` example
  1792. <del>
  1793. *foo*
  1794. </del>
  1795. .
  1796. <del>
  1797. <p><em>foo</em></p>
  1798. </del>
  1799. ````````````````````````````````
  1800. Finally, in this case, the `<del>` tags are interpreted
  1801. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1802. the tag is not on a line by itself, we get inline HTML
  1803. rather than an [HTML block].)
  1804. ```````````````````````````````` example
  1805. <del>*foo*</del>
  1806. .
  1807. <p><del><em>foo</em></del></p>
  1808. ````````````````````````````````
  1809. HTML tags designed to contain literal content
  1810. (`script`, `style`, `pre`), comments, processing instructions,
  1811. and declarations are treated somewhat differently.
  1812. Instead of ending at the first blank line, these blocks
  1813. end at the first line containing a corresponding end tag.
  1814. As a result, these blocks can contain blank lines:
  1815. A pre tag (type 1):
  1816. ```````````````````````````````` example
  1817. <pre language="haskell"><code>
  1818. import Text.HTML.TagSoup
  1819. main :: IO ()
  1820. main = print $ parseTags tags
  1821. </code></pre>
  1822. okay
  1823. .
  1824. <pre language="haskell"><code>
  1825. import Text.HTML.TagSoup
  1826. main :: IO ()
  1827. main = print $ parseTags tags
  1828. </code></pre>
  1829. <p>okay</p>
  1830. ````````````````````````````````
  1831. A script tag (type 1):
  1832. ```````````````````````````````` example
  1833. <script type="text/javascript">
  1834. // JavaScript example
  1835. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1836. </script>
  1837. okay
  1838. .
  1839. <script type="text/javascript">
  1840. // JavaScript example
  1841. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1842. </script>
  1843. <p>okay</p>
  1844. ````````````````````````````````
  1845. A style tag (type 1):
  1846. ```````````````````````````````` example
  1847. <style
  1848. type="text/css">
  1849. h1 {color:red;}
  1850. p {color:blue;}
  1851. </style>
  1852. okay
  1853. .
  1854. <style
  1855. type="text/css">
  1856. h1 {color:red;}
  1857. p {color:blue;}
  1858. </style>
  1859. <p>okay</p>
  1860. ````````````````````````````````
  1861. If there is no matching end tag, the block will end at the
  1862. end of the document (or the enclosing [block quote][block quotes]
  1863. or [list item][list items]):
  1864. ```````````````````````````````` example
  1865. <style
  1866. type="text/css">
  1867. foo
  1868. .
  1869. <style
  1870. type="text/css">
  1871. foo
  1872. ````````````````````````````````
  1873. ```````````````````````````````` example
  1874. > <div>
  1875. > foo
  1876. bar
  1877. .
  1878. <blockquote>
  1879. <div>
  1880. foo
  1881. </blockquote>
  1882. <p>bar</p>
  1883. ````````````````````````````````
  1884. ```````````````````````````````` example
  1885. - <div>
  1886. - foo
  1887. .
  1888. <ul>
  1889. <li>
  1890. <div>
  1891. </li>
  1892. <li>foo</li>
  1893. </ul>
  1894. ````````````````````````````````
  1895. The end tag can occur on the same line as the start tag:
  1896. ```````````````````````````````` example
  1897. <style>p{color:red;}</style>
  1898. *foo*
  1899. .
  1900. <style>p{color:red;}</style>
  1901. <p><em>foo</em></p>
  1902. ````````````````````````````````
  1903. ```````````````````````````````` example
  1904. <!-- foo -->*bar*
  1905. *baz*
  1906. .
  1907. <!-- foo -->*bar*
  1908. <p><em>baz</em></p>
  1909. ````````````````````````````````
  1910. Note that anything on the last line after the
  1911. end tag will be included in the [HTML block]:
  1912. ```````````````````````````````` example
  1913. <script>
  1914. foo
  1915. </script>1. *bar*
  1916. .
  1917. <script>
  1918. foo
  1919. </script>1. *bar*
  1920. ````````````````````````````````
  1921. A comment (type 2):
  1922. ```````````````````````````````` example
  1923. <!-- Foo
  1924. bar
  1925. baz -->
  1926. okay
  1927. .
  1928. <!-- Foo
  1929. bar
  1930. baz -->
  1931. <p>okay</p>
  1932. ````````````````````````````````
  1933. A processing instruction (type 3):
  1934. ```````````````````````````````` example
  1935. <?php
  1936. echo '>';
  1937. ?>
  1938. okay
  1939. .
  1940. <?php
  1941. echo '>';
  1942. ?>
  1943. <p>okay</p>
  1944. ````````````````````````````````
  1945. A declaration (type 4):
  1946. ```````````````````````````````` example
  1947. <!DOCTYPE html>
  1948. .
  1949. <!DOCTYPE html>
  1950. ````````````````````````````````
  1951. CDATA (type 5):
  1952. ```````````````````````````````` example
  1953. <![CDATA[
  1954. function matchwo(a,b)
  1955. {
  1956. if (a < b && a < 0) then {
  1957. return 1;
  1958. } else {
  1959. return 0;
  1960. }
  1961. }
  1962. ]]>
  1963. okay
  1964. .
  1965. <![CDATA[
  1966. function matchwo(a,b)
  1967. {
  1968. if (a < b && a < 0) then {
  1969. return 1;
  1970. } else {
  1971. return 0;
  1972. }
  1973. }
  1974. ]]>
  1975. <p>okay</p>
  1976. ````````````````````````````````
  1977. The opening tag can be indented 1-3 spaces, but not 4:
  1978. ```````````````````````````````` example
  1979. <!-- foo -->
  1980. <!-- foo -->
  1981. .
  1982. <!-- foo -->
  1983. <pre><code>&lt;!-- foo --&gt;
  1984. </code></pre>
  1985. ````````````````````````````````
  1986. ```````````````````````````````` example
  1987. <div>
  1988. <div>
  1989. .
  1990. <div>
  1991. <pre><code>&lt;div&gt;
  1992. </code></pre>
  1993. ````````````````````````````````
  1994. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  1995. preceded by a blank line.
  1996. ```````````````````````````````` example
  1997. Foo
  1998. <div>
  1999. bar
  2000. </div>
  2001. .
  2002. <p>Foo</p>
  2003. <div>
  2004. bar
  2005. </div>
  2006. ````````````````````````````````
  2007. However, a following blank line is needed, except at the end of
  2008. a document, and except for blocks of types 1--5, above:
  2009. ```````````````````````````````` example
  2010. <div>
  2011. bar
  2012. </div>
  2013. *foo*
  2014. .
  2015. <div>
  2016. bar
  2017. </div>
  2018. *foo*
  2019. ````````````````````````````````
  2020. HTML blocks of type 7 cannot interrupt a paragraph:
  2021. ```````````````````````````````` example
  2022. Foo
  2023. <a href="bar">
  2024. baz
  2025. .
  2026. <p>Foo
  2027. <a href="bar">
  2028. baz</p>
  2029. ````````````````````````````````
  2030. This rule differs from John Gruber's original Markdown syntax
  2031. specification, which says:
  2032. > The only restrictions are that block-level HTML elements —
  2033. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2034. > surrounding content by blank lines, and the start and end tags of the
  2035. > block should not be indented with tabs or spaces.
  2036. In some ways Gruber's rule is more restrictive than the one given
  2037. here:
  2038. - It requires that an HTML block be preceded by a blank line.
  2039. - It does not allow the start tag to be indented.
  2040. - It requires a matching end tag, which it also does not allow to
  2041. be indented.
  2042. Most Markdown implementations (including some of Gruber's own) do not
  2043. respect all of these restrictions.
  2044. There is one respect, however, in which Gruber's rule is more liberal
  2045. than the one given here, since it allows blank lines to occur inside
  2046. an HTML block. There are two reasons for disallowing them here.
  2047. First, it removes the need to parse balanced tags, which is
  2048. expensive and can require backtracking from the end of the document
  2049. if no matching end tag is found. Second, it provides a very simple
  2050. and flexible way of including Markdown content inside HTML tags:
  2051. simply separate the Markdown from the HTML using blank lines:
  2052. Compare:
  2053. ```````````````````````````````` example
  2054. <div>
  2055. *Emphasized* text.
  2056. </div>
  2057. .
  2058. <div>
  2059. <p><em>Emphasized</em> text.</p>
  2060. </div>
  2061. ````````````````````````````````
  2062. ```````````````````````````````` example
  2063. <div>
  2064. *Emphasized* text.
  2065. </div>
  2066. .
  2067. <div>
  2068. *Emphasized* text.
  2069. </div>
  2070. ````````````````````````````````
  2071. Some Markdown implementations have adopted a convention of
  2072. interpreting content inside tags as text if the open tag has
  2073. the attribute `markdown=1`. The rule given above seems a simpler and
  2074. more elegant way of achieving the same expressive power, which is also
  2075. much simpler to parse.
  2076. The main potential drawback is that one can no longer paste HTML
  2077. blocks into Markdown documents with 100% reliability. However,
  2078. *in most cases* this will work fine, because the blank lines in
  2079. HTML are usually followed by HTML block tags. For example:
  2080. ```````````````````````````````` example
  2081. <table>
  2082. <tr>
  2083. <td>
  2084. Hi
  2085. </td>
  2086. </tr>
  2087. </table>
  2088. .
  2089. <table>
  2090. <tr>
  2091. <td>
  2092. Hi
  2093. </td>
  2094. </tr>
  2095. </table>
  2096. ````````````````````````````````
  2097. There are problems, however, if the inner tags are indented
  2098. *and* separated by spaces, as then they will be interpreted as
  2099. an indented code block:
  2100. ```````````````````````````````` example
  2101. <table>
  2102. <tr>
  2103. <td>
  2104. Hi
  2105. </td>
  2106. </tr>
  2107. </table>
  2108. .
  2109. <table>
  2110. <tr>
  2111. <pre><code>&lt;td&gt;
  2112. Hi
  2113. &lt;/td&gt;
  2114. </code></pre>
  2115. </tr>
  2116. </table>
  2117. ````````````````````````````````
  2118. Fortunately, blank lines are usually not necessary and can be
  2119. deleted. The exception is inside `<pre>` tags, but as described
  2120. above, raw HTML blocks starting with `<pre>` *can* contain blank
  2121. lines.
  2122. ## Link reference definitions
  2123. A [link reference definition](@)
  2124. consists of a [link label], indented up to three spaces, followed
  2125. by a colon (`:`), optional [whitespace] (including up to one
  2126. [line ending]), a [link destination],
  2127. optional [whitespace] (including up to one
  2128. [line ending]), and an optional [link
  2129. title], which if it is present must be separated
  2130. from the [link destination] by [whitespace].
  2131. No further [non-whitespace characters] may occur on the line.
  2132. A [link reference definition]
  2133. does not correspond to a structural element of a document. Instead, it
  2134. defines a label which can be used in [reference links]
  2135. and reference-style [images] elsewhere in the document. [Link
  2136. reference definitions] can come either before or after the links that use
  2137. them.
  2138. ```````````````````````````````` example
  2139. [foo]: /url "title"
  2140. [foo]
  2141. .
  2142. <p><a href="/url" title="title">foo</a></p>
  2143. ````````````````````````````````
  2144. ```````````````````````````````` example
  2145. [foo]:
  2146. /url
  2147. 'the title'
  2148. [foo]
  2149. .
  2150. <p><a href="/url" title="the title">foo</a></p>
  2151. ````````````````````````````````
  2152. ```````````````````````````````` example
  2153. [Foo*bar\]]:my_(url) 'title (with parens)'
  2154. [Foo*bar\]]
  2155. .
  2156. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2157. ````````````````````````````````
  2158. ```````````````````````````````` example
  2159. [Foo bar]:
  2160. <my url>
  2161. 'title'
  2162. [Foo bar]
  2163. .
  2164. <p><a href="my%20url" title="title">Foo bar</a></p>
  2165. ````````````````````````````````
  2166. The title may extend over multiple lines:
  2167. ```````````````````````````````` example
  2168. [foo]: /url '
  2169. title
  2170. line1
  2171. line2
  2172. '
  2173. [foo]
  2174. .
  2175. <p><a href="/url" title="
  2176. title
  2177. line1
  2178. line2
  2179. ">foo</a></p>
  2180. ````````````````````````````````
  2181. However, it may not contain a [blank line]:
  2182. ```````````````````````````````` example
  2183. [foo]: /url 'title
  2184. with blank line'
  2185. [foo]
  2186. .
  2187. <p>[foo]: /url 'title</p>
  2188. <p>with blank line'</p>
  2189. <p>[foo]</p>
  2190. ````````````````````````````````
  2191. The title may be omitted:
  2192. ```````````````````````````````` example
  2193. [foo]:
  2194. /url
  2195. [foo]
  2196. .
  2197. <p><a href="/url">foo</a></p>
  2198. ````````````````````````````````
  2199. The link destination may not be omitted:
  2200. ```````````````````````````````` example
  2201. [foo]:
  2202. [foo]
  2203. .
  2204. <p>[foo]:</p>
  2205. <p>[foo]</p>
  2206. ````````````````````````````````
  2207. Both title and destination can contain backslash escapes
  2208. and literal backslashes:
  2209. ```````````````````````````````` example
  2210. [foo]: /url\bar\*baz "foo\"bar\baz"
  2211. [foo]
  2212. .
  2213. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2214. ````````````````````````````````
  2215. A link can come before its corresponding definition:
  2216. ```````````````````````````````` example
  2217. [foo]
  2218. [foo]: url
  2219. .
  2220. <p><a href="url">foo</a></p>
  2221. ````````````````````````````````
  2222. If there are several matching definitions, the first one takes
  2223. precedence:
  2224. ```````````````````````````````` example
  2225. [foo]
  2226. [foo]: first
  2227. [foo]: second
  2228. .
  2229. <p><a href="first">foo</a></p>
  2230. ````````````````````````````````
  2231. As noted in the section on [Links], matching of labels is
  2232. case-insensitive (see [matches]).
  2233. ```````````````````````````````` example
  2234. [FOO]: /url
  2235. [Foo]
  2236. .
  2237. <p><a href="/url">Foo</a></p>
  2238. ````````````````````````````````
  2239. ```````````````````````````````` example
  2240. [ΑΓΩ]: /φου
  2241. [αγω]
  2242. .
  2243. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2244. ````````````````````````````````
  2245. Here is a link reference definition with no corresponding link.
  2246. It contributes nothing to the document.
  2247. ```````````````````````````````` example
  2248. [foo]: /url
  2249. .
  2250. ````````````````````````````````
  2251. Here is another one:
  2252. ```````````````````````````````` example
  2253. [
  2254. foo
  2255. ]: /url
  2256. bar
  2257. .
  2258. <p>bar</p>
  2259. ````````````````````````````````
  2260. This is not a link reference definition, because there are
  2261. [non-whitespace characters] after the title:
  2262. ```````````````````````````````` example
  2263. [foo]: /url "title" ok
  2264. .
  2265. <p>[foo]: /url &quot;title&quot; ok</p>
  2266. ````````````````````````````````
  2267. This is a link reference definition, but it has no title:
  2268. ```````````````````````````````` example
  2269. [foo]: /url
  2270. "title" ok
  2271. .
  2272. <p>&quot;title&quot; ok</p>
  2273. ````````````````````````````````
  2274. This is not a link reference definition, because it is indented
  2275. four spaces:
  2276. ```````````````````````````````` example
  2277. [foo]: /url "title"
  2278. [foo]
  2279. .
  2280. <pre><code>[foo]: /url &quot;title&quot;
  2281. </code></pre>
  2282. <p>[foo]</p>
  2283. ````````````````````````````````
  2284. This is not a link reference definition, because it occurs inside
  2285. a code block:
  2286. ```````````````````````````````` example
  2287. ```
  2288. [foo]: /url
  2289. ```
  2290. [foo]
  2291. .
  2292. <pre><code>[foo]: /url
  2293. </code></pre>
  2294. <p>[foo]</p>
  2295. ````````````````````````````````
  2296. A [link reference definition] cannot interrupt a paragraph.
  2297. ```````````````````````````````` example
  2298. Foo
  2299. [bar]: /baz
  2300. [bar]
  2301. .
  2302. <p>Foo
  2303. [bar]: /baz</p>
  2304. <p>[bar]</p>
  2305. ````````````````````````````````
  2306. However, it can directly follow other block elements, such as headings
  2307. and thematic breaks, and it need not be followed by a blank line.
  2308. ```````````````````````````````` example
  2309. # [Foo]
  2310. [foo]: /url
  2311. > bar
  2312. .
  2313. <h1><a href="/url">Foo</a></h1>
  2314. <blockquote>
  2315. <p>bar</p>
  2316. </blockquote>
  2317. ````````````````````````````````
  2318. Several [link reference definitions]
  2319. can occur one after another, without intervening blank lines.
  2320. ```````````````````````````````` example
  2321. [foo]: /foo-url "foo"
  2322. [bar]: /bar-url
  2323. "bar"
  2324. [baz]: /baz-url
  2325. [foo],
  2326. [bar],
  2327. [baz]
  2328. .
  2329. <p><a href="/foo-url" title="foo">foo</a>,
  2330. <a href="/bar-url" title="bar">bar</a>,
  2331. <a href="/baz-url">baz</a></p>
  2332. ````````````````````````````````
  2333. [Link reference definitions] can occur
  2334. inside block containers, like lists and block quotations. They
  2335. affect the entire document, not just the container in which they
  2336. are defined:
  2337. ```````````````````````````````` example
  2338. [foo]
  2339. > [foo]: /url
  2340. .
  2341. <p><a href="/url">foo</a></p>
  2342. <blockquote>
  2343. </blockquote>
  2344. ````````````````````````````````
  2345. ## Paragraphs
  2346. A sequence of non-blank lines that cannot be interpreted as other
  2347. kinds of blocks forms a [paragraph](@).
  2348. The contents of the paragraph are the result of parsing the
  2349. paragraph's raw content as inlines. The paragraph's raw content
  2350. is formed by concatenating the lines and removing initial and final
  2351. [whitespace].
  2352. A simple example with two paragraphs:
  2353. ```````````````````````````````` example
  2354. aaa
  2355. bbb
  2356. .
  2357. <p>aaa</p>
  2358. <p>bbb</p>
  2359. ````````````````````````````````
  2360. Paragraphs can contain multiple lines, but no blank lines:
  2361. ```````````````````````````````` example
  2362. aaa
  2363. bbb
  2364. ccc
  2365. ddd
  2366. .
  2367. <p>aaa
  2368. bbb</p>
  2369. <p>ccc
  2370. ddd</p>
  2371. ````````````````````````````````
  2372. Multiple blank lines between paragraph have no effect:
  2373. ```````````````````````````````` example
  2374. aaa
  2375. bbb
  2376. .
  2377. <p>aaa</p>
  2378. <p>bbb</p>
  2379. ````````````````````````````````
  2380. Leading spaces are skipped:
  2381. ```````````````````````````````` example
  2382. aaa
  2383. bbb
  2384. .
  2385. <p>aaa
  2386. bbb</p>
  2387. ````````````````````````````````
  2388. Lines after the first may be indented any amount, since indented
  2389. code blocks cannot interrupt paragraphs.
  2390. ```````````````````````````````` example
  2391. aaa
  2392. bbb
  2393. ccc
  2394. .
  2395. <p>aaa
  2396. bbb
  2397. ccc</p>
  2398. ````````````````````````````````
  2399. However, the first line may be indented at most three spaces,
  2400. or an indented code block will be triggered:
  2401. ```````````````````````````````` example
  2402. aaa
  2403. bbb
  2404. .
  2405. <p>aaa
  2406. bbb</p>
  2407. ````````````````````````````````
  2408. ```````````````````````````````` example
  2409. aaa
  2410. bbb
  2411. .
  2412. <pre><code>aaa
  2413. </code></pre>
  2414. <p>bbb</p>
  2415. ````````````````````````````````
  2416. Final spaces are stripped before inline parsing, so a paragraph
  2417. that ends with two or more spaces will not end with a [hard line
  2418. break]:
  2419. ```````````````````````````````` example
  2420. aaa
  2421. bbb
  2422. .
  2423. <p>aaa<br />
  2424. bbb</p>
  2425. ````````````````````````````````
  2426. ## Blank lines
  2427. [Blank lines] between block-level elements are ignored,
  2428. except for the role they play in determining whether a [list]
  2429. is [tight] or [loose].
  2430. Blank lines at the beginning and end of the document are also ignored.
  2431. ```````````````````````````````` example
  2432. aaa
  2433. # aaa
  2434. .
  2435. <p>aaa</p>
  2436. <h1>aaa</h1>
  2437. ````````````````````````````````
  2438. # Container blocks
  2439. A [container block] is a block that has other
  2440. blocks as its contents. There are two basic kinds of container blocks:
  2441. [block quotes] and [list items].
  2442. [Lists] are meta-containers for [list items].
  2443. We define the syntax for container blocks recursively. The general
  2444. form of the definition is:
  2445. > If X is a sequence of blocks, then the result of
  2446. > transforming X in such-and-such a way is a container of type Y
  2447. > with these blocks as its content.
  2448. So, we explain what counts as a block quote or list item by explaining
  2449. how these can be *generated* from their contents. This should suffice
  2450. to define the syntax, although it does not give a recipe for *parsing*
  2451. these constructions. (A recipe is provided below in the section entitled
  2452. [A parsing strategy](#appendix-a-parsing-strategy).)
  2453. ## Block quotes
  2454. A [block quote marker](@)
  2455. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2456. with a following space, or (b) a single character `>` not followed by a space.
  2457. The following rules define [block quotes]:
  2458. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2459. of blocks *Bs*, then the result of prepending a [block quote
  2460. marker] to the beginning of each line in *Ls*
  2461. is a [block quote](#block-quotes) containing *Bs*.
  2462. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2463. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2464. the initial [block quote marker] from one or
  2465. more lines in which the next [non-whitespace character] after the [block
  2466. quote marker] is [paragraph continuation
  2467. text] is a block quote with *Bs* as its content.
  2468. [Paragraph continuation text](@) is text
  2469. that will be parsed as part of the content of a paragraph, but does
  2470. not occur at the beginning of the paragraph.
  2471. 3. **Consecutiveness.** A document cannot contain two [block
  2472. quotes] in a row unless there is a [blank line] between them.
  2473. Nothing else counts as a [block quote](#block-quotes).
  2474. Here is a simple example:
  2475. ```````````````````````````````` example
  2476. > # Foo
  2477. > bar
  2478. > baz
  2479. .
  2480. <blockquote>
  2481. <h1>Foo</h1>
  2482. <p>bar
  2483. baz</p>
  2484. </blockquote>
  2485. ````````````````````````````````
  2486. The spaces after the `>` characters can be omitted:
  2487. ```````````````````````````````` example
  2488. ># Foo
  2489. >bar
  2490. > baz
  2491. .
  2492. <blockquote>
  2493. <h1>Foo</h1>
  2494. <p>bar
  2495. baz</p>
  2496. </blockquote>
  2497. ````````````````````````````````
  2498. The `>` characters can be indented 1-3 spaces:
  2499. ```````````````````````````````` example
  2500. > # Foo
  2501. > bar
  2502. > baz
  2503. .
  2504. <blockquote>
  2505. <h1>Foo</h1>
  2506. <p>bar
  2507. baz</p>
  2508. </blockquote>
  2509. ````````````````````````````````
  2510. Four spaces gives us a code block:
  2511. ```````````````````````````````` example
  2512. > # Foo
  2513. > bar
  2514. > baz
  2515. .
  2516. <pre><code>&gt; # Foo
  2517. &gt; bar
  2518. &gt; baz
  2519. </code></pre>
  2520. ````````````````````````````````
  2521. The Laziness clause allows us to omit the `>` before
  2522. [paragraph continuation text]:
  2523. ```````````````````````````````` example
  2524. > # Foo
  2525. > bar
  2526. baz
  2527. .
  2528. <blockquote>
  2529. <h1>Foo</h1>
  2530. <p>bar
  2531. baz</p>
  2532. </blockquote>
  2533. ````````````````````````````````
  2534. A block quote can contain some lazy and some non-lazy
  2535. continuation lines:
  2536. ```````````````````````````````` example
  2537. > bar
  2538. baz
  2539. > foo
  2540. .
  2541. <blockquote>
  2542. <p>bar
  2543. baz
  2544. foo</p>
  2545. </blockquote>
  2546. ````````````````````````````````
  2547. Laziness only applies to lines that would have been continuations of
  2548. paragraphs had they been prepended with [block quote markers].
  2549. For example, the `> ` cannot be omitted in the second line of
  2550. ``` markdown
  2551. > foo
  2552. > ---
  2553. ```
  2554. without changing the meaning:
  2555. ```````````````````````````````` example
  2556. > foo
  2557. ---
  2558. .
  2559. <blockquote>
  2560. <p>foo</p>
  2561. </blockquote>
  2562. <hr />
  2563. ````````````````````````````````
  2564. Similarly, if we omit the `> ` in the second line of
  2565. ``` markdown
  2566. > - foo
  2567. > - bar
  2568. ```
  2569. then the block quote ends after the first line:
  2570. ```````````````````````````````` example
  2571. > - foo
  2572. - bar
  2573. .
  2574. <blockquote>
  2575. <ul>
  2576. <li>foo</li>
  2577. </ul>
  2578. </blockquote>
  2579. <ul>
  2580. <li>bar</li>
  2581. </ul>
  2582. ````````````````````````````````
  2583. For the same reason, we can't omit the `> ` in front of
  2584. subsequent lines of an indented or fenced code block:
  2585. ```````````````````````````````` example
  2586. > foo
  2587. bar
  2588. .
  2589. <blockquote>
  2590. <pre><code>foo
  2591. </code></pre>
  2592. </blockquote>
  2593. <pre><code>bar
  2594. </code></pre>
  2595. ````````````````````````````````
  2596. ```````````````````````````````` example
  2597. > ```
  2598. foo
  2599. ```
  2600. .
  2601. <blockquote>
  2602. <pre><code></code></pre>
  2603. </blockquote>
  2604. <p>foo</p>
  2605. <pre><code></code></pre>
  2606. ````````````````````````````````
  2607. Note that in the following case, we have a [lazy
  2608. continuation line]:
  2609. ```````````````````````````````` example
  2610. > foo
  2611. - bar
  2612. .
  2613. <blockquote>
  2614. <p>foo
  2615. - bar</p>
  2616. </blockquote>
  2617. ````````````````````````````````
  2618. To see why, note that in
  2619. ```markdown
  2620. > foo
  2621. > - bar
  2622. ```
  2623. the `- bar` is indented too far to start a list, and can't
  2624. be an indented code block because indented code blocks cannot
  2625. interrupt paragraphs, so it is [paragraph continuation text].
  2626. A block quote can be empty:
  2627. ```````````````````````````````` example
  2628. >
  2629. .
  2630. <blockquote>
  2631. </blockquote>
  2632. ````````````````````````````````
  2633. ```````````````````````````````` example
  2634. >
  2635. >
  2636. >
  2637. .
  2638. <blockquote>
  2639. </blockquote>
  2640. ````````````````````````````````
  2641. A block quote can have initial or final blank lines:
  2642. ```````````````````````````````` example
  2643. >
  2644. > foo
  2645. >
  2646. .
  2647. <blockquote>
  2648. <p>foo</p>
  2649. </blockquote>
  2650. ````````````````````````````````
  2651. A blank line always separates block quotes:
  2652. ```````````````````````````````` example
  2653. > foo
  2654. > bar
  2655. .
  2656. <blockquote>
  2657. <p>foo</p>
  2658. </blockquote>
  2659. <blockquote>
  2660. <p>bar</p>
  2661. </blockquote>
  2662. ````````````````````````````````
  2663. (Most current Markdown implementations, including John Gruber's
  2664. original `Markdown.pl`, will parse this example as a single block quote
  2665. with two paragraphs. But it seems better to allow the author to decide
  2666. whether two block quotes or one are wanted.)
  2667. Consecutiveness means that if we put these block quotes together,
  2668. we get a single block quote:
  2669. ```````````````````````````````` example
  2670. > foo
  2671. > bar
  2672. .
  2673. <blockquote>
  2674. <p>foo
  2675. bar</p>
  2676. </blockquote>
  2677. ````````````````````````````````
  2678. To get a block quote with two paragraphs, use:
  2679. ```````````````````````````````` example
  2680. > foo
  2681. >
  2682. > bar
  2683. .
  2684. <blockquote>
  2685. <p>foo</p>
  2686. <p>bar</p>
  2687. </blockquote>
  2688. ````````````````````````````````
  2689. Block quotes can interrupt paragraphs:
  2690. ```````````````````````````````` example
  2691. foo
  2692. > bar
  2693. .
  2694. <p>foo</p>
  2695. <blockquote>
  2696. <p>bar</p>
  2697. </blockquote>
  2698. ````````````````````````````````
  2699. In general, blank lines are not needed before or after block
  2700. quotes:
  2701. ```````````````````````````````` example
  2702. > aaa
  2703. ***
  2704. > bbb
  2705. .
  2706. <blockquote>
  2707. <p>aaa</p>
  2708. </blockquote>
  2709. <hr />
  2710. <blockquote>
  2711. <p>bbb</p>
  2712. </blockquote>
  2713. ````````````````````````````````
  2714. However, because of laziness, a blank line is needed between
  2715. a block quote and a following paragraph:
  2716. ```````````````````````````````` example
  2717. > bar
  2718. baz
  2719. .
  2720. <blockquote>
  2721. <p>bar
  2722. baz</p>
  2723. </blockquote>
  2724. ````````````````````````````````
  2725. ```````````````````````````````` example
  2726. > bar
  2727. baz
  2728. .
  2729. <blockquote>
  2730. <p>bar</p>
  2731. </blockquote>
  2732. <p>baz</p>
  2733. ````````````````````````````````
  2734. ```````````````````````````````` example
  2735. > bar
  2736. >
  2737. baz
  2738. .
  2739. <blockquote>
  2740. <p>bar</p>
  2741. </blockquote>
  2742. <p>baz</p>
  2743. ````````````````````````````````
  2744. It is a consequence of the Laziness rule that any number
  2745. of initial `>`s may be omitted on a continuation line of a
  2746. nested block quote:
  2747. ```````````````````````````````` example
  2748. > > > foo
  2749. bar
  2750. .
  2751. <blockquote>
  2752. <blockquote>
  2753. <blockquote>
  2754. <p>foo
  2755. bar</p>
  2756. </blockquote>
  2757. </blockquote>
  2758. </blockquote>
  2759. ````````````````````````````````
  2760. ```````````````````````````````` example
  2761. >>> foo
  2762. > bar
  2763. >>baz
  2764. .
  2765. <blockquote>
  2766. <blockquote>
  2767. <blockquote>
  2768. <p>foo
  2769. bar
  2770. baz</p>
  2771. </blockquote>
  2772. </blockquote>
  2773. </blockquote>
  2774. ````````````````````````````````
  2775. When including an indented code block in a block quote,
  2776. remember that the [block quote marker] includes
  2777. both the `>` and a following space. So *five spaces* are needed after
  2778. the `>`:
  2779. ```````````````````````````````` example
  2780. > code
  2781. > not code
  2782. .
  2783. <blockquote>
  2784. <pre><code>code
  2785. </code></pre>
  2786. </blockquote>
  2787. <blockquote>
  2788. <p>not code</p>
  2789. </blockquote>
  2790. ````````````````````````````````
  2791. ## List items
  2792. A [list marker](@) is a
  2793. [bullet list marker] or an [ordered list marker].
  2794. A [bullet list marker](@)
  2795. is a `-`, `+`, or `*` character.
  2796. An [ordered list marker](@)
  2797. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2798. `.` character or a `)` character. (The reason for the length
  2799. limit is that with 10 digits we start seeing integer overflows
  2800. in some browsers.)
  2801. The following rules define [list items]:
  2802. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2803. blocks *Bs* starting with a [non-whitespace character] and not separated
  2804. from each other by more than one blank line, and *M* is a list
  2805. marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2806. of prepending *M* and the following spaces to the first line of
  2807. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2808. list item with *Bs* as its contents. The type of the list item
  2809. (bullet or ordered) is determined by the type of its list marker.
  2810. If the list item is ordered, then it is also assigned a start
  2811. number, based on the ordered list marker.
  2812. Exceptions:
  2813. 1. When the first list item in a [list] interrupts
  2814. a paragraph---that is, when it starts on a line that would
  2815. otherwise count as [paragraph continuation text]---then (a)
  2816. the lines *Ls* must not begin with a blank line, and (b) if
  2817. the list item is ordered, the start number must be 1.
  2818. 2. If any line is a [thematic break][thematic breaks] then
  2819. that line is not a list item.
  2820. For example, let *Ls* be the lines
  2821. ```````````````````````````````` example
  2822. A paragraph
  2823. with two lines.
  2824. indented code
  2825. > A block quote.
  2826. .
  2827. <p>A paragraph
  2828. with two lines.</p>
  2829. <pre><code>indented code
  2830. </code></pre>
  2831. <blockquote>
  2832. <p>A block quote.</p>
  2833. </blockquote>
  2834. ````````````````````````````````
  2835. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2836. that the following is an ordered list item with start number 1,
  2837. and the same contents as *Ls*:
  2838. ```````````````````````````````` example
  2839. 1. A paragraph
  2840. with two lines.
  2841. indented code
  2842. > A block quote.
  2843. .
  2844. <ol>
  2845. <li>
  2846. <p>A paragraph
  2847. with two lines.</p>
  2848. <pre><code>indented code
  2849. </code></pre>
  2850. <blockquote>
  2851. <p>A block quote.</p>
  2852. </blockquote>
  2853. </li>
  2854. </ol>
  2855. ````````````````````````````````
  2856. The most important thing to notice is that the position of
  2857. the text after the list marker determines how much indentation
  2858. is needed in subsequent blocks in the list item. If the list
  2859. marker takes up two spaces, and there are three spaces between
  2860. the list marker and the next [non-whitespace character], then blocks
  2861. must be indented five spaces in order to fall under the list
  2862. item.
  2863. Here are some examples showing how far content must be indented to be
  2864. put under the list item:
  2865. ```````````````````````````````` example
  2866. - one
  2867. two
  2868. .
  2869. <ul>
  2870. <li>one</li>
  2871. </ul>
  2872. <p>two</p>
  2873. ````````````````````````````````
  2874. ```````````````````````````````` example
  2875. - one
  2876. two
  2877. .
  2878. <ul>
  2879. <li>
  2880. <p>one</p>
  2881. <p>two</p>
  2882. </li>
  2883. </ul>
  2884. ````````````````````````````````
  2885. ```````````````````````````````` example
  2886. - one
  2887. two
  2888. .
  2889. <ul>
  2890. <li>one</li>
  2891. </ul>
  2892. <pre><code> two
  2893. </code></pre>
  2894. ````````````````````````````````
  2895. ```````````````````````````````` example
  2896. - one
  2897. two
  2898. .
  2899. <ul>
  2900. <li>
  2901. <p>one</p>
  2902. <p>two</p>
  2903. </li>
  2904. </ul>
  2905. ````````````````````````````````
  2906. It is tempting to think of this in terms of columns: the continuation
  2907. blocks must be indented at least to the column of the first
  2908. [non-whitespace character] after the list marker. However, that is not quite right.
  2909. The spaces after the list marker determine how much relative indentation
  2910. is needed. Which column this indentation reaches will depend on
  2911. how the list item is embedded in other constructions, as shown by
  2912. this example:
  2913. ```````````````````````````````` example
  2914. > > 1. one
  2915. >>
  2916. >> two
  2917. .
  2918. <blockquote>
  2919. <blockquote>
  2920. <ol>
  2921. <li>
  2922. <p>one</p>
  2923. <p>two</p>
  2924. </li>
  2925. </ol>
  2926. </blockquote>
  2927. </blockquote>
  2928. ````````````````````````````````
  2929. Here `two` occurs in the same column as the list marker `1.`,
  2930. but is actually contained in the list item, because there is
  2931. sufficient indentation after the last containing blockquote marker.
  2932. The converse is also possible. In the following example, the word `two`
  2933. occurs far to the right of the initial text of the list item, `one`, but
  2934. it is not considered part of the list item, because it is not indented
  2935. far enough past the blockquote marker:
  2936. ```````````````````````````````` example
  2937. >>- one
  2938. >>
  2939. > > two
  2940. .
  2941. <blockquote>
  2942. <blockquote>
  2943. <ul>
  2944. <li>one</li>
  2945. </ul>
  2946. <p>two</p>
  2947. </blockquote>
  2948. </blockquote>
  2949. ````````````````````````````````
  2950. Note that at least one space is needed between the list marker and
  2951. any following content, so these are not list items:
  2952. ```````````````````````````````` example
  2953. -one
  2954. 2.two
  2955. .
  2956. <p>-one</p>
  2957. <p>2.two</p>
  2958. ````````````````````````````````
  2959. A list item may contain blocks that are separated by more than
  2960. one blank line.
  2961. ```````````````````````````````` example
  2962. - foo
  2963. bar
  2964. .
  2965. <ul>
  2966. <li>
  2967. <p>foo</p>
  2968. <p>bar</p>
  2969. </li>
  2970. </ul>
  2971. ````````````````````````````````
  2972. A list item may contain any kind of block:
  2973. ```````````````````````````````` example
  2974. 1. foo
  2975. ```
  2976. bar
  2977. ```
  2978. baz
  2979. > bam
  2980. .
  2981. <ol>
  2982. <li>
  2983. <p>foo</p>
  2984. <pre><code>bar
  2985. </code></pre>
  2986. <p>baz</p>
  2987. <blockquote>
  2988. <p>bam</p>
  2989. </blockquote>
  2990. </li>
  2991. </ol>
  2992. ````````````````````````````````
  2993. A list item that contains an indented code block will preserve
  2994. empty lines within the code block verbatim.
  2995. ```````````````````````````````` example
  2996. - Foo
  2997. bar
  2998. baz
  2999. .
  3000. <ul>
  3001. <li>
  3002. <p>Foo</p>
  3003. <pre><code>bar
  3004. baz
  3005. </code></pre>
  3006. </li>
  3007. </ul>
  3008. ````````````````````````````````
  3009. Note that ordered list start numbers must be nine digits or less:
  3010. ```````````````````````````````` example
  3011. 123456789. ok
  3012. .
  3013. <ol start="123456789">
  3014. <li>ok</li>
  3015. </ol>
  3016. ````````````````````````````````
  3017. ```````````````````````````````` example
  3018. 1234567890. not ok
  3019. .
  3020. <p>1234567890. not ok</p>
  3021. ````````````````````````````````
  3022. A start number may begin with 0s:
  3023. ```````````````````````````````` example
  3024. 0. ok
  3025. .
  3026. <ol start="0">
  3027. <li>ok</li>
  3028. </ol>
  3029. ````````````````````````````````
  3030. ```````````````````````````````` example
  3031. 003. ok
  3032. .
  3033. <ol start="3">
  3034. <li>ok</li>
  3035. </ol>
  3036. ````````````````````````````````
  3037. A start number may not be negative:
  3038. ```````````````````````````````` example
  3039. -1. not ok
  3040. .
  3041. <p>-1. not ok</p>
  3042. ````````````````````````````````
  3043. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3044. constitute a sequence of blocks *Bs* starting with an indented code
  3045. block and not separated from each other by more than one blank line,
  3046. and *M* is a list marker of width *W* followed by
  3047. one space, then the result of prepending *M* and the following
  3048. space to the first line of *Ls*, and indenting subsequent lines of
  3049. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3050. If a line is empty, then it need not be indented. The type of the
  3051. list item (bullet or ordered) is determined by the type of its list
  3052. marker. If the list item is ordered, then it is also assigned a
  3053. start number, based on the ordered list marker.
  3054. An indented code block will have to be indented four spaces beyond
  3055. the edge of the region where text will be included in the list item.
  3056. In the following case that is 6 spaces:
  3057. ```````````````````````````````` example
  3058. - foo
  3059. bar
  3060. .
  3061. <ul>
  3062. <li>
  3063. <p>foo</p>
  3064. <pre><code>bar
  3065. </code></pre>
  3066. </li>
  3067. </ul>
  3068. ````````````````````````````````
  3069. And in this case it is 11 spaces:
  3070. ```````````````````````````````` example
  3071. 10. foo
  3072. bar
  3073. .
  3074. <ol start="10">
  3075. <li>
  3076. <p>foo</p>
  3077. <pre><code>bar
  3078. </code></pre>
  3079. </li>
  3080. </ol>
  3081. ````````````````````````````````
  3082. If the *first* block in the list item is an indented code block,
  3083. then by rule #2, the contents must be indented *one* space after the
  3084. list marker:
  3085. ```````````````````````````````` example
  3086. indented code
  3087. paragraph
  3088. more code
  3089. .
  3090. <pre><code>indented code
  3091. </code></pre>
  3092. <p>paragraph</p>
  3093. <pre><code>more code
  3094. </code></pre>
  3095. ````````````````````````````````
  3096. ```````````````````````````````` example
  3097. 1. indented code
  3098. paragraph
  3099. more code
  3100. .
  3101. <ol>
  3102. <li>
  3103. <pre><code>indented code
  3104. </code></pre>
  3105. <p>paragraph</p>
  3106. <pre><code>more code
  3107. </code></pre>
  3108. </li>
  3109. </ol>
  3110. ````````````````````````````````
  3111. Note that an additional space indent is interpreted as space
  3112. inside the code block:
  3113. ```````````````````````````````` example
  3114. 1. indented code
  3115. paragraph
  3116. more code
  3117. .
  3118. <ol>
  3119. <li>
  3120. <pre><code> indented code
  3121. </code></pre>
  3122. <p>paragraph</p>
  3123. <pre><code>more code
  3124. </code></pre>
  3125. </li>
  3126. </ol>
  3127. ````````````````````````````````
  3128. Note that rules #1 and #2 only apply to two cases: (a) cases
  3129. in which the lines to be included in a list item begin with a
  3130. [non-whitespace character], and (b) cases in which
  3131. they begin with an indented code
  3132. block. In a case like the following, where the first block begins with
  3133. a three-space indent, the rules do not allow us to form a list item by
  3134. indenting the whole thing and prepending a list marker:
  3135. ```````````````````````````````` example
  3136. foo
  3137. bar
  3138. .
  3139. <p>foo</p>
  3140. <p>bar</p>
  3141. ````````````````````````````````
  3142. ```````````````````````````````` example
  3143. - foo
  3144. bar
  3145. .
  3146. <ul>
  3147. <li>foo</li>
  3148. </ul>
  3149. <p>bar</p>
  3150. ````````````````````````````````
  3151. This is not a significant restriction, because when a block begins
  3152. with 1-3 spaces indent, the indentation can always be removed without
  3153. a change in interpretation, allowing rule #1 to be applied. So, in
  3154. the above case:
  3155. ```````````````````````````````` example
  3156. - foo
  3157. bar
  3158. .
  3159. <ul>
  3160. <li>
  3161. <p>foo</p>
  3162. <p>bar</p>
  3163. </li>
  3164. </ul>
  3165. ````````````````````````````````
  3166. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3167. starting with a single [blank line] constitute a (possibly empty)
  3168. sequence of blocks *Bs*, not separated from each other by more than
  3169. one blank line, and *M* is a list marker of width *W*,
  3170. then the result of prepending *M* to the first line of *Ls*, and
  3171. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3172. item with *Bs* as its contents.
  3173. If a line is empty, then it need not be indented. The type of the
  3174. list item (bullet or ordered) is determined by the type of its list
  3175. marker. If the list item is ordered, then it is also assigned a
  3176. start number, based on the ordered list marker.
  3177. Here are some list items that start with a blank line but are not empty:
  3178. ```````````````````````````````` example
  3179. -
  3180. foo
  3181. -
  3182. ```
  3183. bar
  3184. ```
  3185. -
  3186. baz
  3187. .
  3188. <ul>
  3189. <li>foo</li>
  3190. <li>
  3191. <pre><code>bar
  3192. </code></pre>
  3193. </li>
  3194. <li>
  3195. <pre><code>baz
  3196. </code></pre>
  3197. </li>
  3198. </ul>
  3199. ````````````````````````````````
  3200. When the list item starts with a blank line, the number of spaces
  3201. following the list marker doesn't change the required indentation:
  3202. ```````````````````````````````` example
  3203. -
  3204. foo
  3205. .
  3206. <ul>
  3207. <li>foo</li>
  3208. </ul>
  3209. ````````````````````````````````
  3210. A list item can begin with at most one blank line.
  3211. In the following example, `foo` is not part of the list
  3212. item:
  3213. ```````````````````````````````` example
  3214. -
  3215. foo
  3216. .
  3217. <ul>
  3218. <li></li>
  3219. </ul>
  3220. <p>foo</p>
  3221. ````````````````````````````````
  3222. Here is an empty bullet list item:
  3223. ```````````````````````````````` example
  3224. - foo
  3225. -
  3226. - bar
  3227. .
  3228. <ul>
  3229. <li>foo</li>
  3230. <li></li>
  3231. <li>bar</li>
  3232. </ul>
  3233. ````````````````````````````````
  3234. It does not matter whether there are spaces following the [list marker]:
  3235. ```````````````````````````````` example
  3236. - foo
  3237. -
  3238. - bar
  3239. .
  3240. <ul>
  3241. <li>foo</li>
  3242. <li></li>
  3243. <li>bar</li>
  3244. </ul>
  3245. ````````````````````````````````
  3246. Here is an empty ordered list item:
  3247. ```````````````````````````````` example
  3248. 1. foo
  3249. 2.
  3250. 3. bar
  3251. .
  3252. <ol>
  3253. <li>foo</li>
  3254. <li></li>
  3255. <li>bar</li>
  3256. </ol>
  3257. ````````````````````````````````
  3258. A list may start or end with an empty list item:
  3259. ```````````````````````````````` example
  3260. *
  3261. .
  3262. <ul>
  3263. <li></li>
  3264. </ul>
  3265. ````````````````````````````````
  3266. However, an empty list item cannot interrupt a paragraph:
  3267. ```````````````````````````````` example
  3268. foo
  3269. *
  3270. foo
  3271. 1.
  3272. .
  3273. <p>foo
  3274. *</p>
  3275. <p>foo
  3276. 1.</p>
  3277. ````````````````````````````````
  3278. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3279. according to rule #1, #2, or #3, then the result of indenting each line
  3280. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3281. list item with the same contents and attributes. If a line is
  3282. empty, then it need not be indented.
  3283. Indented one space:
  3284. ```````````````````````````````` example
  3285. 1. A paragraph
  3286. with two lines.
  3287. indented code
  3288. > A block quote.
  3289. .
  3290. <ol>
  3291. <li>
  3292. <p>A paragraph
  3293. with two lines.</p>
  3294. <pre><code>indented code
  3295. </code></pre>
  3296. <blockquote>
  3297. <p>A block quote.</p>
  3298. </blockquote>
  3299. </li>
  3300. </ol>
  3301. ````````````````````````````````
  3302. Indented two spaces:
  3303. ```````````````````````````````` example
  3304. 1. A paragraph
  3305. with two lines.
  3306. indented code
  3307. > A block quote.
  3308. .
  3309. <ol>
  3310. <li>
  3311. <p>A paragraph
  3312. with two lines.</p>
  3313. <pre><code>indented code
  3314. </code></pre>
  3315. <blockquote>
  3316. <p>A block quote.</p>
  3317. </blockquote>
  3318. </li>
  3319. </ol>
  3320. ````````````````````````````````
  3321. Indented three spaces:
  3322. ```````````````````````````````` example
  3323. 1. A paragraph
  3324. with two lines.
  3325. indented code
  3326. > A block quote.
  3327. .
  3328. <ol>
  3329. <li>
  3330. <p>A paragraph
  3331. with two lines.</p>
  3332. <pre><code>indented code
  3333. </code></pre>
  3334. <blockquote>
  3335. <p>A block quote.</p>
  3336. </blockquote>
  3337. </li>
  3338. </ol>
  3339. ````````````````````````````````
  3340. Four spaces indent gives a code block:
  3341. ```````````````````````````````` example
  3342. 1. A paragraph
  3343. with two lines.
  3344. indented code
  3345. > A block quote.
  3346. .
  3347. <pre><code>1. A paragraph
  3348. with two lines.
  3349. indented code
  3350. &gt; A block quote.
  3351. </code></pre>
  3352. ````````````````````````````````
  3353. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3354. item](#list-items) with contents *Bs*, then the result of deleting
  3355. some or all of the indentation from one or more lines in which the
  3356. next [non-whitespace character] after the indentation is
  3357. [paragraph continuation text] is a
  3358. list item with the same contents and attributes. The unindented
  3359. lines are called
  3360. [lazy continuation line](@)s.
  3361. Here is an example with [lazy continuation lines]:
  3362. ```````````````````````````````` example
  3363. 1. A paragraph
  3364. with two lines.
  3365. indented code
  3366. > A block quote.
  3367. .
  3368. <ol>
  3369. <li>
  3370. <p>A paragraph
  3371. with two lines.</p>
  3372. <pre><code>indented code
  3373. </code></pre>
  3374. <blockquote>
  3375. <p>A block quote.</p>
  3376. </blockquote>
  3377. </li>
  3378. </ol>
  3379. ````````````````````````````````
  3380. Indentation can be partially deleted:
  3381. ```````````````````````````````` example
  3382. 1. A paragraph
  3383. with two lines.
  3384. .
  3385. <ol>
  3386. <li>A paragraph
  3387. with two lines.</li>
  3388. </ol>
  3389. ````````````````````````````````
  3390. These examples show how laziness can work in nested structures:
  3391. ```````````````````````````````` example
  3392. > 1. > Blockquote
  3393. continued here.
  3394. .
  3395. <blockquote>
  3396. <ol>
  3397. <li>
  3398. <blockquote>
  3399. <p>Blockquote
  3400. continued here.</p>
  3401. </blockquote>
  3402. </li>
  3403. </ol>
  3404. </blockquote>
  3405. ````````````````````````````````
  3406. ```````````````````````````````` example
  3407. > 1. > Blockquote
  3408. > continued here.
  3409. .
  3410. <blockquote>
  3411. <ol>
  3412. <li>
  3413. <blockquote>
  3414. <p>Blockquote
  3415. continued here.</p>
  3416. </blockquote>
  3417. </li>
  3418. </ol>
  3419. </blockquote>
  3420. ````````````````````````````````
  3421. 6. **That's all.** Nothing that is not counted as a list item by rules
  3422. #1--5 counts as a [list item](#list-items).
  3423. The rules for sublists follow from the general rules above. A sublist
  3424. must be indented the same number of spaces a paragraph would need to be
  3425. in order to be included in the list item.
  3426. So, in this case we need two spaces indent:
  3427. ```````````````````````````````` example
  3428. - foo
  3429. - bar
  3430. - baz
  3431. - boo
  3432. .
  3433. <ul>
  3434. <li>foo
  3435. <ul>
  3436. <li>bar
  3437. <ul>
  3438. <li>baz
  3439. <ul>
  3440. <li>boo</li>
  3441. </ul>
  3442. </li>
  3443. </ul>
  3444. </li>
  3445. </ul>
  3446. </li>
  3447. </ul>
  3448. ````````````````````````````````
  3449. One is not enough:
  3450. ```````````````````````````````` example
  3451. - foo
  3452. - bar
  3453. - baz
  3454. - boo
  3455. .
  3456. <ul>
  3457. <li>foo</li>
  3458. <li>bar</li>
  3459. <li>baz</li>
  3460. <li>boo</li>
  3461. </ul>
  3462. ````````````````````````````````
  3463. Here we need four, because the list marker is wider:
  3464. ```````````````````````````````` example
  3465. 10) foo
  3466. - bar
  3467. .
  3468. <ol start="10">
  3469. <li>foo
  3470. <ul>
  3471. <li>bar</li>
  3472. </ul>
  3473. </li>
  3474. </ol>
  3475. ````````````````````````````````
  3476. Three is not enough:
  3477. ```````````````````````````````` example
  3478. 10) foo
  3479. - bar
  3480. .
  3481. <ol start="10">
  3482. <li>foo</li>
  3483. </ol>
  3484. <ul>
  3485. <li>bar</li>
  3486. </ul>
  3487. ````````````````````````````````
  3488. A list may be the first block in a list item:
  3489. ```````````````````````````````` example
  3490. - - foo
  3491. .
  3492. <ul>
  3493. <li>
  3494. <ul>
  3495. <li>foo</li>
  3496. </ul>
  3497. </li>
  3498. </ul>
  3499. ````````````````````````````````
  3500. ```````````````````````````````` example
  3501. 1. - 2. foo
  3502. .
  3503. <ol>
  3504. <li>
  3505. <ul>
  3506. <li>
  3507. <ol start="2">
  3508. <li>foo</li>
  3509. </ol>
  3510. </li>
  3511. </ul>
  3512. </li>
  3513. </ol>
  3514. ````````````````````````````````
  3515. A list item can contain a heading:
  3516. ```````````````````````````````` example
  3517. - # Foo
  3518. - Bar
  3519. ---
  3520. baz
  3521. .
  3522. <ul>
  3523. <li>
  3524. <h1>Foo</h1>
  3525. </li>
  3526. <li>
  3527. <h2>Bar</h2>
  3528. baz</li>
  3529. </ul>
  3530. ````````````````````````````````
  3531. ### Motivation
  3532. John Gruber's Markdown spec says the following about list items:
  3533. 1. "List markers typically start at the left margin, but may be indented
  3534. by up to three spaces. List markers must be followed by one or more
  3535. spaces or a tab."
  3536. 2. "To make lists look nice, you can wrap items with hanging indents....
  3537. But if you don't want to, you don't have to."
  3538. 3. "List items may consist of multiple paragraphs. Each subsequent
  3539. paragraph in a list item must be indented by either 4 spaces or one
  3540. tab."
  3541. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3542. but here again, Markdown will allow you to be lazy."
  3543. 5. "To put a blockquote within a list item, the blockquote's `>`
  3544. delimiters need to be indented."
  3545. 6. "To put a code block within a list item, the code block needs to be
  3546. indented twice — 8 spaces or two tabs."
  3547. These rules specify that a paragraph under a list item must be indented
  3548. four spaces (presumably, from the left margin, rather than the start of
  3549. the list marker, but this is not said), and that code under a list item
  3550. must be indented eight spaces instead of the usual four. They also say
  3551. that a block quote must be indented, but not by how much; however, the
  3552. example given has four spaces indentation. Although nothing is said
  3553. about other kinds of block-level content, it is certainly reasonable to
  3554. infer that *all* block elements under a list item, including other
  3555. lists, must be indented four spaces. This principle has been called the
  3556. *four-space rule*.
  3557. The four-space rule is clear and principled, and if the reference
  3558. implementation `Markdown.pl` had followed it, it probably would have
  3559. become the standard. However, `Markdown.pl` allowed paragraphs and
  3560. sublists to start with only two spaces indentation, at least on the
  3561. outer level. Worse, its behavior was inconsistent: a sublist of an
  3562. outer-level list needed two spaces indentation, but a sublist of this
  3563. sublist needed three spaces. It is not surprising, then, that different
  3564. implementations of Markdown have developed very different rules for
  3565. determining what comes under a list item. (Pandoc and python-Markdown,
  3566. for example, stuck with Gruber's syntax description and the four-space
  3567. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3568. followed `Markdown.pl`'s behavior more closely.)
  3569. Unfortunately, given the divergences between implementations, there
  3570. is no way to give a spec for list items that will be guaranteed not
  3571. to break any existing documents. However, the spec given here should
  3572. correctly handle lists formatted with either the four-space rule or
  3573. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3574. in a way that is natural for a human to read.
  3575. The strategy here is to let the width and indentation of the list marker
  3576. determine the indentation necessary for blocks to fall under the list
  3577. item, rather than having a fixed and arbitrary number. The writer can
  3578. think of the body of the list item as a unit which gets indented to the
  3579. right enough to fit the list marker (and any indentation on the list
  3580. marker). (The laziness rule, #5, then allows continuation lines to be
  3581. unindented if needed.)
  3582. This rule is superior, we claim, to any rule requiring a fixed level of
  3583. indentation from the margin. The four-space rule is clear but
  3584. unnatural. It is quite unintuitive that
  3585. ``` markdown
  3586. - foo
  3587. bar
  3588. - baz
  3589. ```
  3590. should be parsed as two lists with an intervening paragraph,
  3591. ``` html
  3592. <ul>
  3593. <li>foo</li>
  3594. </ul>
  3595. <p>bar</p>
  3596. <ul>
  3597. <li>baz</li>
  3598. </ul>
  3599. ```
  3600. as the four-space rule demands, rather than a single list,
  3601. ``` html
  3602. <ul>
  3603. <li>
  3604. <p>foo</p>
  3605. <p>bar</p>
  3606. <ul>
  3607. <li>baz</li>
  3608. </ul>
  3609. </li>
  3610. </ul>
  3611. ```
  3612. The choice of four spaces is arbitrary. It can be learned, but it is
  3613. not likely to be guessed, and it trips up beginners regularly.
  3614. Would it help to adopt a two-space rule? The problem is that such
  3615. a rule, together with the rule allowing 1--3 spaces indentation of the
  3616. initial list marker, allows text that is indented *less than* the
  3617. original list marker to be included in the list item. For example,
  3618. `Markdown.pl` parses
  3619. ``` markdown
  3620. - one
  3621. two
  3622. ```
  3623. as a single list item, with `two` a continuation paragraph:
  3624. ``` html
  3625. <ul>
  3626. <li>
  3627. <p>one</p>
  3628. <p>two</p>
  3629. </li>
  3630. </ul>
  3631. ```
  3632. and similarly
  3633. ``` markdown
  3634. > - one
  3635. >
  3636. > two
  3637. ```
  3638. as
  3639. ``` html
  3640. <blockquote>
  3641. <ul>
  3642. <li>
  3643. <p>one</p>
  3644. <p>two</p>
  3645. </li>
  3646. </ul>
  3647. </blockquote>
  3648. ```
  3649. This is extremely unintuitive.
  3650. Rather than requiring a fixed indent from the margin, we could require
  3651. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3652. may itself be indented). This proposal would remove the last anomaly
  3653. discussed. Unlike the spec presented above, it would count the following
  3654. as a list item with a subparagraph, even though the paragraph `bar`
  3655. is not indented as far as the first paragraph `foo`:
  3656. ``` markdown
  3657. 10. foo
  3658. bar
  3659. ```
  3660. Arguably this text does read like a list item with `bar` as a subparagraph,
  3661. which may count in favor of the proposal. However, on this proposal indented
  3662. code would have to be indented six spaces after the list marker. And this
  3663. would break a lot of existing Markdown, which has the pattern:
  3664. ``` markdown
  3665. 1. foo
  3666. indented code
  3667. ```
  3668. where the code is indented eight spaces. The spec above, by contrast, will
  3669. parse this text as expected, since the code block's indentation is measured
  3670. from the beginning of `foo`.
  3671. The one case that needs special treatment is a list item that *starts*
  3672. with indented code. How much indentation is required in that case, since
  3673. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3674. that in such cases, we require one space indentation from the list marker
  3675. (and then the normal four spaces for the indented code). This will match the
  3676. four-space rule in cases where the list marker plus its initial indentation
  3677. takes four spaces (a common case), but diverge in other cases.
  3678. ## Lists
  3679. A [list](@) is a sequence of one or more
  3680. list items [of the same type]. The list items
  3681. may be separated by any number of blank lines.
  3682. Two list items are [of the same type](@)
  3683. if they begin with a [list marker] of the same type.
  3684. Two list markers are of the
  3685. same type if (a) they are bullet list markers using the same character
  3686. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3687. delimiter (either `.` or `)`).
  3688. A list is an [ordered list](@)
  3689. if its constituent list items begin with
  3690. [ordered list markers], and a
  3691. [bullet list](@) if its constituent list
  3692. items begin with [bullet list markers].
  3693. The [start number](@)
  3694. of an [ordered list] is determined by the list number of
  3695. its initial list item. The numbers of subsequent list items are
  3696. disregarded.
  3697. A list is [loose](@) if any of its constituent
  3698. list items are separated by blank lines, or if any of its constituent
  3699. list items directly contain two block-level elements with a blank line
  3700. between them. Otherwise a list is [tight](@).
  3701. (The difference in HTML output is that paragraphs in a loose list are
  3702. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3703. Changing the bullet or ordered list delimiter starts a new list:
  3704. ```````````````````````````````` example
  3705. - foo
  3706. - bar
  3707. + baz
  3708. .
  3709. <ul>
  3710. <li>foo</li>
  3711. <li>bar</li>
  3712. </ul>
  3713. <ul>
  3714. <li>baz</li>
  3715. </ul>
  3716. ````````````````````````````````
  3717. ```````````````````````````````` example
  3718. 1. foo
  3719. 2. bar
  3720. 3) baz
  3721. .
  3722. <ol>
  3723. <li>foo</li>
  3724. <li>bar</li>
  3725. </ol>
  3726. <ol start="3">
  3727. <li>baz</li>
  3728. </ol>
  3729. ````````````````````````````````
  3730. In CommonMark, a list can interrupt a paragraph. That is,
  3731. no blank line is needed to separate a paragraph from a following
  3732. list:
  3733. ```````````````````````````````` example
  3734. Foo
  3735. - bar
  3736. - baz
  3737. .
  3738. <p>Foo</p>
  3739. <ul>
  3740. <li>bar</li>
  3741. <li>baz</li>
  3742. </ul>
  3743. ````````````````````````````````
  3744. `Markdown.pl` does not allow this, through fear of triggering a list
  3745. via a numeral in a hard-wrapped line:
  3746. ``` markdown
  3747. The number of windows in my house is
  3748. 14. The number of doors is 6.
  3749. ```
  3750. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3751. interrupt a paragraph, even though the same considerations might
  3752. apply.
  3753. In CommonMark, we do allow lists to interrupt paragraphs, for
  3754. two reasons. First, it is natural and not uncommon for people
  3755. to start lists without blank lines:
  3756. ``` markdown
  3757. I need to buy
  3758. - new shoes
  3759. - a coat
  3760. - a plane ticket
  3761. ```
  3762. Second, we are attracted to a
  3763. > [principle of uniformity](@):
  3764. > if a chunk of text has a certain
  3765. > meaning, it will continue to have the same meaning when put into a
  3766. > container block (such as a list item or blockquote).
  3767. (Indeed, the spec for [list items] and [block quotes] presupposes
  3768. this principle.) This principle implies that if
  3769. ``` markdown
  3770. * I need to buy
  3771. - new shoes
  3772. - a coat
  3773. - a plane ticket
  3774. ```
  3775. is a list item containing a paragraph followed by a nested sublist,
  3776. as all Markdown implementations agree it is (though the paragraph
  3777. may be rendered without `<p>` tags, since the list is "tight"),
  3778. then
  3779. ``` markdown
  3780. I need to buy
  3781. - new shoes
  3782. - a coat
  3783. - a plane ticket
  3784. ```
  3785. by itself should be a paragraph followed by a nested sublist.
  3786. Since it is well established Markdown practice to allow lists to
  3787. interrupt paragraphs inside list items, the [principle of
  3788. uniformity] requires us to allow this outside list items as
  3789. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3790. takes a different approach, requiring blank lines before lists
  3791. even inside other list items.)
  3792. In order to solve of unwanted lists in paragraphs with
  3793. hard-wrapped numerals, we allow only lists starting with `1` to
  3794. interrupt paragraphs. Thus,
  3795. ```````````````````````````````` example
  3796. The number of windows in my house is
  3797. 14. The number of doors is 6.
  3798. .
  3799. <p>The number of windows in my house is
  3800. 14. The number of doors is 6.</p>
  3801. ````````````````````````````````
  3802. We may still get an unintended result in cases like
  3803. ```````````````````````````````` example
  3804. The number of windows in my house is
  3805. 1. The number of doors is 6.
  3806. .
  3807. <p>The number of windows in my house is</p>
  3808. <ol>
  3809. <li>The number of doors is 6.</li>
  3810. </ol>
  3811. ````````````````````````````````
  3812. but this rule should prevent most spurious list captures.
  3813. There can be any number of blank lines between items:
  3814. ```````````````````````````````` example
  3815. - foo
  3816. - bar
  3817. - baz
  3818. .
  3819. <ul>
  3820. <li>
  3821. <p>foo</p>
  3822. </li>
  3823. <li>
  3824. <p>bar</p>
  3825. </li>
  3826. <li>
  3827. <p>baz</p>
  3828. </li>
  3829. </ul>
  3830. ````````````````````````````````
  3831. ```````````````````````````````` example
  3832. - foo
  3833. - bar
  3834. - baz
  3835. bim
  3836. .
  3837. <ul>
  3838. <li>foo
  3839. <ul>
  3840. <li>bar
  3841. <ul>
  3842. <li>
  3843. <p>baz</p>
  3844. <p>bim</p>
  3845. </li>
  3846. </ul>
  3847. </li>
  3848. </ul>
  3849. </li>
  3850. </ul>
  3851. ````````````````````````````````
  3852. To separate consecutive lists of the same type, or to separate a
  3853. list from an indented code block that would otherwise be parsed
  3854. as a subparagraph of the final list item, you can insert a blank HTML
  3855. comment:
  3856. ```````````````````````````````` example
  3857. - foo
  3858. - bar
  3859. <!-- -->
  3860. - baz
  3861. - bim
  3862. .
  3863. <ul>
  3864. <li>foo</li>
  3865. <li>bar</li>
  3866. </ul>
  3867. <!-- -->
  3868. <ul>
  3869. <li>baz</li>
  3870. <li>bim</li>
  3871. </ul>
  3872. ````````````````````````````````
  3873. ```````````````````````````````` example
  3874. - foo
  3875. notcode
  3876. - foo
  3877. <!-- -->
  3878. code
  3879. .
  3880. <ul>
  3881. <li>
  3882. <p>foo</p>
  3883. <p>notcode</p>
  3884. </li>
  3885. <li>
  3886. <p>foo</p>
  3887. </li>
  3888. </ul>
  3889. <!-- -->
  3890. <pre><code>code
  3891. </code></pre>
  3892. ````````````````````````````````
  3893. List items need not be indented to the same level. The following
  3894. list items will be treated as items at the same list level,
  3895. since none is indented enough to belong to the previous list
  3896. item:
  3897. ```````````````````````````````` example
  3898. - a
  3899. - b
  3900. - c
  3901. - d
  3902. - e
  3903. - f
  3904. - g
  3905. .
  3906. <ul>
  3907. <li>a</li>
  3908. <li>b</li>
  3909. <li>c</li>
  3910. <li>d</li>
  3911. <li>e</li>
  3912. <li>f</li>
  3913. <li>g</li>
  3914. </ul>
  3915. ````````````````````````````````
  3916. ```````````````````````````````` example
  3917. 1. a
  3918. 2. b
  3919. 3. c
  3920. .
  3921. <ol>
  3922. <li>
  3923. <p>a</p>
  3924. </li>
  3925. <li>
  3926. <p>b</p>
  3927. </li>
  3928. <li>
  3929. <p>c</p>
  3930. </li>
  3931. </ol>
  3932. ````````````````````````````````
  3933. Note, however, that list items may not be indented more than
  3934. three spaces. Here `- e` is treated as a paragraph continuation
  3935. line, because it is indented more than three spaces:
  3936. ```````````````````````````````` example
  3937. - a
  3938. - b
  3939. - c
  3940. - d
  3941. - e
  3942. .
  3943. <ul>
  3944. <li>a</li>
  3945. <li>b</li>
  3946. <li>c</li>
  3947. <li>d
  3948. - e</li>
  3949. </ul>
  3950. ````````````````````````````````
  3951. And here, `3. c` is treated as in indented code block,
  3952. because it is indented four spaces and preceded by a
  3953. blank line.
  3954. ```````````````````````````````` example
  3955. 1. a
  3956. 2. b
  3957. 3. c
  3958. .
  3959. <ol>
  3960. <li>
  3961. <p>a</p>
  3962. </li>
  3963. <li>
  3964. <p>b</p>
  3965. </li>
  3966. </ol>
  3967. <pre><code>3. c
  3968. </code></pre>
  3969. ````````````````````````````````
  3970. This is a loose list, because there is a blank line between
  3971. two of the list items:
  3972. ```````````````````````````````` example
  3973. - a
  3974. - b
  3975. - c
  3976. .
  3977. <ul>
  3978. <li>
  3979. <p>a</p>
  3980. </li>
  3981. <li>
  3982. <p>b</p>
  3983. </li>
  3984. <li>
  3985. <p>c</p>
  3986. </li>
  3987. </ul>
  3988. ````````````````````````````````
  3989. So is this, with a empty second item:
  3990. ```````````````````````````````` example
  3991. * a
  3992. *
  3993. * c
  3994. .
  3995. <ul>
  3996. <li>
  3997. <p>a</p>
  3998. </li>
  3999. <li></li>
  4000. <li>
  4001. <p>c</p>
  4002. </li>
  4003. </ul>
  4004. ````````````````````````````````
  4005. These are loose lists, even though there is no space between the items,
  4006. because one of the items directly contains two block-level elements
  4007. with a blank line between them:
  4008. ```````````````````````````````` example
  4009. - a
  4010. - b
  4011. c
  4012. - d
  4013. .
  4014. <ul>
  4015. <li>
  4016. <p>a</p>
  4017. </li>
  4018. <li>
  4019. <p>b</p>
  4020. <p>c</p>
  4021. </li>
  4022. <li>
  4023. <p>d</p>
  4024. </li>
  4025. </ul>
  4026. ````````````````````````````````
  4027. ```````````````````````````````` example
  4028. - a
  4029. - b
  4030. [ref]: /url
  4031. - d
  4032. .
  4033. <ul>
  4034. <li>
  4035. <p>a</p>
  4036. </li>
  4037. <li>
  4038. <p>b</p>
  4039. </li>
  4040. <li>
  4041. <p>d</p>
  4042. </li>
  4043. </ul>
  4044. ````````````````````````````````
  4045. This is a tight list, because the blank lines are in a code block:
  4046. ```````````````````````````````` example
  4047. - a
  4048. - ```
  4049. b
  4050. ```
  4051. - c
  4052. .
  4053. <ul>
  4054. <li>a</li>
  4055. <li>
  4056. <pre><code>b
  4057. </code></pre>
  4058. </li>
  4059. <li>c</li>
  4060. </ul>
  4061. ````````````````````````````````
  4062. This is a tight list, because the blank line is between two
  4063. paragraphs of a sublist. So the sublist is loose while
  4064. the outer list is tight:
  4065. ```````````````````````````````` example
  4066. - a
  4067. - b
  4068. c
  4069. - d
  4070. .
  4071. <ul>
  4072. <li>a
  4073. <ul>
  4074. <li>
  4075. <p>b</p>
  4076. <p>c</p>
  4077. </li>
  4078. </ul>
  4079. </li>
  4080. <li>d</li>
  4081. </ul>
  4082. ````````````````````````````````
  4083. This is a tight list, because the blank line is inside the
  4084. block quote:
  4085. ```````````````````````````````` example
  4086. * a
  4087. > b
  4088. >
  4089. * c
  4090. .
  4091. <ul>
  4092. <li>a
  4093. <blockquote>
  4094. <p>b</p>
  4095. </blockquote>
  4096. </li>
  4097. <li>c</li>
  4098. </ul>
  4099. ````````````````````````````````
  4100. This list is tight, because the consecutive block elements
  4101. are not separated by blank lines:
  4102. ```````````````````````````````` example
  4103. - a
  4104. > b
  4105. ```
  4106. c
  4107. ```
  4108. - d
  4109. .
  4110. <ul>
  4111. <li>a
  4112. <blockquote>
  4113. <p>b</p>
  4114. </blockquote>
  4115. <pre><code>c
  4116. </code></pre>
  4117. </li>
  4118. <li>d</li>
  4119. </ul>
  4120. ````````````````````````````````
  4121. A single-paragraph list is tight:
  4122. ```````````````````````````````` example
  4123. - a
  4124. .
  4125. <ul>
  4126. <li>a</li>
  4127. </ul>
  4128. ````````````````````````````````
  4129. ```````````````````````````````` example
  4130. - a
  4131. - b
  4132. .
  4133. <ul>
  4134. <li>a
  4135. <ul>
  4136. <li>b</li>
  4137. </ul>
  4138. </li>
  4139. </ul>
  4140. ````````````````````````````````
  4141. This list is loose, because of the blank line between the
  4142. two block elements in the list item:
  4143. ```````````````````````````````` example
  4144. 1. ```
  4145. foo
  4146. ```
  4147. bar
  4148. .
  4149. <ol>
  4150. <li>
  4151. <pre><code>foo
  4152. </code></pre>
  4153. <p>bar</p>
  4154. </li>
  4155. </ol>
  4156. ````````````````````````````````
  4157. Here the outer list is loose, the inner list tight:
  4158. ```````````````````````````````` example
  4159. * foo
  4160. * bar
  4161. baz
  4162. .
  4163. <ul>
  4164. <li>
  4165. <p>foo</p>
  4166. <ul>
  4167. <li>bar</li>
  4168. </ul>
  4169. <p>baz</p>
  4170. </li>
  4171. </ul>
  4172. ````````````````````````````````
  4173. ```````````````````````````````` example
  4174. - a
  4175. - b
  4176. - c
  4177. - d
  4178. - e
  4179. - f
  4180. .
  4181. <ul>
  4182. <li>
  4183. <p>a</p>
  4184. <ul>
  4185. <li>b</li>
  4186. <li>c</li>
  4187. </ul>
  4188. </li>
  4189. <li>
  4190. <p>d</p>
  4191. <ul>
  4192. <li>e</li>
  4193. <li>f</li>
  4194. </ul>
  4195. </li>
  4196. </ul>
  4197. ````````````````````````````````
  4198. # Inlines
  4199. Inlines are parsed sequentially from the beginning of the character
  4200. stream to the end (left to right, in left-to-right languages).
  4201. Thus, for example, in
  4202. ```````````````````````````````` example
  4203. `hi`lo`
  4204. .
  4205. <p><code>hi</code>lo`</p>
  4206. ````````````````````````````````
  4207. `hi` is parsed as code, leaving the backtick at the end as a literal
  4208. backtick.
  4209. ## Backslash escapes
  4210. Any ASCII punctuation character may be backslash-escaped:
  4211. ```````````````````````````````` example
  4212. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4213. .
  4214. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4215. ````````````````````````````````
  4216. Backslashes before other characters are treated as literal
  4217. backslashes:
  4218. ```````````````````````````````` example
  4219. \→\A\a\ \3\φ\«
  4220. .
  4221. <p>\→\A\a\ \3\φ\«</p>
  4222. ````````````````````````````````
  4223. Escaped characters are treated as regular characters and do
  4224. not have their usual Markdown meanings:
  4225. ```````````````````````````````` example
  4226. \*not emphasized*
  4227. \<br/> not a tag
  4228. \[not a link](/foo)
  4229. \`not code`
  4230. 1\. not a list
  4231. \* not a list
  4232. \# not a heading
  4233. \[foo]: /url "not a reference"
  4234. .
  4235. <p>*not emphasized*
  4236. &lt;br/&gt; not a tag
  4237. [not a link](/foo)
  4238. `not code`
  4239. 1. not a list
  4240. * not a list
  4241. # not a heading
  4242. [foo]: /url &quot;not a reference&quot;</p>
  4243. ````````````````````````````````
  4244. If a backslash is itself escaped, the following character is not:
  4245. ```````````````````````````````` example
  4246. \\*emphasis*
  4247. .
  4248. <p>\<em>emphasis</em></p>
  4249. ````````````````````````````````
  4250. A backslash at the end of the line is a [hard line break]:
  4251. ```````````````````````````````` example
  4252. foo\
  4253. bar
  4254. .
  4255. <p>foo<br />
  4256. bar</p>
  4257. ````````````````````````````````
  4258. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4259. raw HTML:
  4260. ```````````````````````````````` example
  4261. `` \[\` ``
  4262. .
  4263. <p><code>\[\`</code></p>
  4264. ````````````````````````````````
  4265. ```````````````````````````````` example
  4266. \[\]
  4267. .
  4268. <pre><code>\[\]
  4269. </code></pre>
  4270. ````````````````````````````````
  4271. ```````````````````````````````` example
  4272. ~~~
  4273. \[\]
  4274. ~~~
  4275. .
  4276. <pre><code>\[\]
  4277. </code></pre>
  4278. ````````````````````````````````
  4279. ```````````````````````````````` example
  4280. <http://example.com?find=\*>
  4281. .
  4282. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4283. ````````````````````````````````
  4284. ```````````````````````````````` example
  4285. <a href="/bar\/)">
  4286. .
  4287. <a href="/bar\/)">
  4288. ````````````````````````````````
  4289. But they work in all other contexts, including URLs and link titles,
  4290. link references, and [info strings] in [fenced code blocks]:
  4291. ```````````````````````````````` example
  4292. [foo](/bar\* "ti\*tle")
  4293. .
  4294. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4295. ````````````````````````````````
  4296. ```````````````````````````````` example
  4297. [foo]
  4298. [foo]: /bar\* "ti\*tle"
  4299. .
  4300. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4301. ````````````````````````````````
  4302. ```````````````````````````````` example
  4303. ``` foo\+bar
  4304. foo
  4305. ```
  4306. .
  4307. <pre><code class="language-foo+bar">foo
  4308. </code></pre>
  4309. ````````````````````````````````
  4310. ## Entity and numeric character references
  4311. All valid HTML entity references and numeric character
  4312. references, except those occuring in code blocks and code spans,
  4313. are recognized as such and treated as equivalent to the
  4314. corresponding Unicode characters. Conforming CommonMark parsers
  4315. need not store information about whether a particular character
  4316. was represented in the source using a Unicode character or
  4317. an entity reference.
  4318. [Entity references](@) consist of `&` + any of the valid
  4319. HTML5 entity names + `;`. The
  4320. document <https://html.spec.whatwg.org/multipage/entities.json>
  4321. is used as an authoritative source for the valid entity
  4322. references and their corresponding code points.
  4323. ```````````````````````````````` example
  4324. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4325. &frac34; &HilbertSpace; &DifferentialD;
  4326. &ClockwiseContourIntegral; &ngE;
  4327. .
  4328. <p>  &amp; © Æ Ď
  4329. ¾ ℋ ⅆ
  4330. ∲ ≧̸</p>
  4331. ````````````````````````````````
  4332. [Decimal numeric character
  4333. references](@)
  4334. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  4335. numeric character reference is parsed as the corresponding
  4336. Unicode character. Invalid Unicode code points will be replaced by
  4337. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4338. the code point `U+0000` will also be replaced by `U+FFFD`.
  4339. ```````````````````````````````` example
  4340. &#35; &#1234; &#992; &#0;
  4341. .
  4342. <p># Ӓ Ϡ � �</p>
  4343. ````````````````````````````````
  4344. [Hexadecimal numeric character
  4345. references](@) consist of `&#` +
  4346. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  4347. They too are parsed as the corresponding Unicode character (this
  4348. time specified with a hexadecimal numeral instead of decimal).
  4349. ```````````````````````````````` example
  4350. &#X22; &#XD06; &#xcab;
  4351. .
  4352. <p>&quot; ആ ಫ</p>
  4353. ````````````````````````````````
  4354. Here are some nonentities:
  4355. ```````````````````````````````` example
  4356. &nbsp &x; &#; &#x;
  4357. &#987654321;
  4358. &#abcdef0;
  4359. &ThisIsNotDefined; &hi?;
  4360. .
  4361. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4362. &amp;#987654321;
  4363. &amp;#abcdef0;
  4364. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4365. ````````````````````````````````
  4366. Although HTML5 does accept some entity references
  4367. without a trailing semicolon (such as `&copy`), these are not
  4368. recognized here, because it makes the grammar too ambiguous:
  4369. ```````````````````````````````` example
  4370. &copy
  4371. .
  4372. <p>&amp;copy</p>
  4373. ````````````````````````````````
  4374. Strings that are not on the list of HTML5 named entities are not
  4375. recognized as entity references either:
  4376. ```````````````````````````````` example
  4377. &MadeUpEntity;
  4378. .
  4379. <p>&amp;MadeUpEntity;</p>
  4380. ````````````````````````````````
  4381. Entity and numeric character references are recognized in any
  4382. context besides code spans or code blocks, including
  4383. URLs, [link titles], and [fenced code block][] [info strings]:
  4384. ```````````````````````````````` example
  4385. <a href="&ouml;&ouml;.html">
  4386. .
  4387. <a href="&ouml;&ouml;.html">
  4388. ````````````````````````````````
  4389. ```````````````````````````````` example
  4390. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4391. .
  4392. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4393. ````````````````````````````````
  4394. ```````````````````````````````` example
  4395. [foo]
  4396. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4397. .
  4398. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4399. ````````````````````````````````
  4400. ```````````````````````````````` example
  4401. ``` f&ouml;&ouml;
  4402. foo
  4403. ```
  4404. .
  4405. <pre><code class="language-föö">foo
  4406. </code></pre>
  4407. ````````````````````````````````
  4408. Entity and numeric character references are treated as literal
  4409. text in code spans and code blocks:
  4410. ```````````````````````````````` example
  4411. `f&ouml;&ouml;`
  4412. .
  4413. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4414. ````````````````````````````````
  4415. ```````````````````````````````` example
  4416. f&ouml;f&ouml;
  4417. .
  4418. <pre><code>f&amp;ouml;f&amp;ouml;
  4419. </code></pre>
  4420. ````````````````````````````````
  4421. ## Code spans
  4422. A [backtick string](@)
  4423. is a string of one or more backtick characters (`` ` ``) that is neither
  4424. preceded nor followed by a backtick.
  4425. A [code span](@) begins with a backtick string and ends with
  4426. a backtick string of equal length. The contents of the code span are
  4427. the characters between the two backtick strings, with leading and
  4428. trailing spaces and [line endings] removed, and
  4429. [whitespace] collapsed to single spaces.
  4430. This is a simple code span:
  4431. ```````````````````````````````` example
  4432. `foo`
  4433. .
  4434. <p><code>foo</code></p>
  4435. ````````````````````````````````
  4436. Here two backticks are used, because the code contains a backtick.
  4437. This example also illustrates stripping of leading and trailing spaces:
  4438. ```````````````````````````````` example
  4439. `` foo ` bar ``
  4440. .
  4441. <p><code>foo ` bar</code></p>
  4442. ````````````````````````````````
  4443. This example shows the motivation for stripping leading and trailing
  4444. spaces:
  4445. ```````````````````````````````` example
  4446. ` `` `
  4447. .
  4448. <p><code>``</code></p>
  4449. ````````````````````````````````
  4450. [Line endings] are treated like spaces:
  4451. ```````````````````````````````` example
  4452. ``
  4453. foo
  4454. ``
  4455. .
  4456. <p><code>foo</code></p>
  4457. ````````````````````````````````
  4458. Interior spaces and [line endings] are collapsed into
  4459. single spaces, just as they would be by a browser:
  4460. ```````````````````````````````` example
  4461. `foo bar
  4462. baz`
  4463. .
  4464. <p><code>foo bar baz</code></p>
  4465. ````````````````````````````````
  4466. Not all [Unicode whitespace] (for instance, non-breaking space) is
  4467. collapsed, however:
  4468. ```````````````````````````````` example
  4469. `a  b`
  4470. .
  4471. <p><code>a  b</code></p>
  4472. ````````````````````````````````
  4473. Q: Why not just leave the spaces, since browsers will collapse them
  4474. anyway? A: Because we might be targeting a non-HTML format, and we
  4475. shouldn't rely on HTML-specific rendering assumptions.
  4476. (Existing implementations differ in their treatment of internal
  4477. spaces and [line endings]. Some, including `Markdown.pl` and
  4478. `showdown`, convert an internal [line ending] into a
  4479. `<br />` tag. But this makes things difficult for those who like to
  4480. hard-wrap their paragraphs, since a line break in the midst of a code
  4481. span will cause an unintended line break in the output. Others just
  4482. leave internal spaces as they are, which is fine if only HTML is being
  4483. targeted.)
  4484. ```````````````````````````````` example
  4485. `foo `` bar`
  4486. .
  4487. <p><code>foo `` bar</code></p>
  4488. ````````````````````````````````
  4489. Note that backslash escapes do not work in code spans. All backslashes
  4490. are treated literally:
  4491. ```````````````````````````````` example
  4492. `foo\`bar`
  4493. .
  4494. <p><code>foo\</code>bar`</p>
  4495. ````````````````````````````````
  4496. Backslash escapes are never needed, because one can always choose a
  4497. string of *n* backtick characters as delimiters, where the code does
  4498. not contain any strings of exactly *n* backtick characters.
  4499. Code span backticks have higher precedence than any other inline
  4500. constructs except HTML tags and autolinks. Thus, for example, this is
  4501. not parsed as emphasized text, since the second `*` is part of a code
  4502. span:
  4503. ```````````````````````````````` example
  4504. *foo`*`
  4505. .
  4506. <p>*foo<code>*</code></p>
  4507. ````````````````````````````````
  4508. And this is not parsed as a link:
  4509. ```````````````````````````````` example
  4510. [not a `link](/foo`)
  4511. .
  4512. <p>[not a <code>link](/foo</code>)</p>
  4513. ````````````````````````````````
  4514. Code spans, HTML tags, and autolinks have the same precedence.
  4515. Thus, this is code:
  4516. ```````````````````````````````` example
  4517. `<a href="`">`
  4518. .
  4519. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4520. ````````````````````````````````
  4521. But this is an HTML tag:
  4522. ```````````````````````````````` example
  4523. <a href="`">`
  4524. .
  4525. <p><a href="`">`</p>
  4526. ````````````````````````````````
  4527. And this is code:
  4528. ```````````````````````````````` example
  4529. `<http://foo.bar.`baz>`
  4530. .
  4531. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4532. ````````````````````````````````
  4533. But this is an autolink:
  4534. ```````````````````````````````` example
  4535. <http://foo.bar.`baz>`
  4536. .
  4537. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4538. ````````````````````````````````
  4539. When a backtick string is not closed by a matching backtick string,
  4540. we just have literal backticks:
  4541. ```````````````````````````````` example
  4542. ```foo``
  4543. .
  4544. <p>```foo``</p>
  4545. ````````````````````````````````
  4546. ```````````````````````````````` example
  4547. `foo
  4548. .
  4549. <p>`foo</p>
  4550. ````````````````````````````````
  4551. The following case also illustrates the need for opening and
  4552. closing backtick strings to be equal in length:
  4553. ```````````````````````````````` example
  4554. `foo``bar``
  4555. .
  4556. <p>`foo<code>bar</code></p>
  4557. ````````````````````````````````
  4558. ## Emphasis and strong emphasis
  4559. John Gruber's original [Markdown syntax
  4560. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4561. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4562. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4563. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4564. > tag.
  4565. This is enough for most users, but these rules leave much undecided,
  4566. especially when it comes to nested emphasis. The original
  4567. `Markdown.pl` test suite makes it clear that triple `***` and
  4568. `___` delimiters can be used for strong emphasis, and most
  4569. implementations have also allowed the following patterns:
  4570. ``` markdown
  4571. ***strong emph***
  4572. ***strong** in emph*
  4573. ***emph* in strong**
  4574. **in strong *emph***
  4575. *in emph **strong***
  4576. ```
  4577. The following patterns are less widely supported, but the intent
  4578. is clear and they are useful (especially in contexts like bibliography
  4579. entries):
  4580. ``` markdown
  4581. *emph *with emph* in it*
  4582. **strong **with strong** in it**
  4583. ```
  4584. Many implementations have also restricted intraword emphasis to
  4585. the `*` forms, to avoid unwanted emphasis in words containing
  4586. internal underscores. (It is best practice to put these in code
  4587. spans, but users often do not.)
  4588. ``` markdown
  4589. internal emphasis: foo*bar*baz
  4590. no emphasis: foo_bar_baz
  4591. ```
  4592. The rules given below capture all of these patterns, while allowing
  4593. for efficient parsing strategies that do not backtrack.
  4594. First, some definitions. A [delimiter run](@) is either
  4595. a sequence of one or more `*` characters that is not preceded or
  4596. followed by a non-backslash-escaped `*` character, or a sequence
  4597. of one or more `_` characters that is not preceded or followed by
  4598. a non-backslash-escaped `_` character.
  4599. A [left-flanking delimiter run](@) is
  4600. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4601. and (b) not followed by a [punctuation character], or
  4602. preceded by [Unicode whitespace] or a [punctuation character].
  4603. For purposes of this definition, the beginning and the end of
  4604. the line count as Unicode whitespace.
  4605. A [right-flanking delimiter run](@) is
  4606. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4607. and (b) not preceded by a [punctuation character], or
  4608. followed by [Unicode whitespace] or a [punctuation character].
  4609. For purposes of this definition, the beginning and the end of
  4610. the line count as Unicode whitespace.
  4611. Here are some examples of delimiter runs.
  4612. - left-flanking but not right-flanking:
  4613. ```
  4614. ***abc
  4615. _abc
  4616. **"abc"
  4617. _"abc"
  4618. ```
  4619. - right-flanking but not left-flanking:
  4620. ```
  4621. abc***
  4622. abc_
  4623. "abc"**
  4624. "abc"_
  4625. ```
  4626. - Both left and right-flanking:
  4627. ```
  4628. abc***def
  4629. "abc"_"def"
  4630. ```
  4631. - Neither left nor right-flanking:
  4632. ```
  4633. abc *** def
  4634. a _ b
  4635. ```
  4636. (The idea of distinguishing left-flanking and right-flanking
  4637. delimiter runs based on the character before and the character
  4638. after comes from Roopesh Chander's
  4639. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4640. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4641. run," and its rules for distinguishing left- and right-flanking runs
  4642. are a bit more complex than the ones given here.)
  4643. The following rules define emphasis and strong emphasis:
  4644. 1. A single `*` character [can open emphasis](@)
  4645. iff (if and only if) it is part of a [left-flanking delimiter run].
  4646. 2. A single `_` character [can open emphasis] iff
  4647. it is part of a [left-flanking delimiter run]
  4648. and either (a) not part of a [right-flanking delimiter run]
  4649. or (b) part of a [right-flanking delimiter run]
  4650. preceded by punctuation.
  4651. 3. A single `*` character [can close emphasis](@)
  4652. iff it is part of a [right-flanking delimiter run].
  4653. 4. A single `_` character [can close emphasis] iff
  4654. it is part of a [right-flanking delimiter run]
  4655. and either (a) not part of a [left-flanking delimiter run]
  4656. or (b) part of a [left-flanking delimiter run]
  4657. followed by punctuation.
  4658. 5. A double `**` [can open strong emphasis](@)
  4659. iff it is part of a [left-flanking delimiter run].
  4660. 6. A double `__` [can open strong emphasis] iff
  4661. it is part of a [left-flanking delimiter run]
  4662. and either (a) not part of a [right-flanking delimiter run]
  4663. or (b) part of a [right-flanking delimiter run]
  4664. preceded by punctuation.
  4665. 7. A double `**` [can close strong emphasis](@)
  4666. iff it is part of a [right-flanking delimiter run].
  4667. 8. A double `__` [can close strong emphasis] iff
  4668. it is part of a [right-flanking delimiter run]
  4669. and either (a) not part of a [left-flanking delimiter run]
  4670. or (b) part of a [left-flanking delimiter run]
  4671. followed by punctuation.
  4672. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4673. with a delimiter that [can close emphasis], and that uses the same
  4674. character (`_` or `*`) as the opening delimiter. The
  4675. opening and closing delimiters must belong to separate
  4676. [delimiter runs]. If one of the delimiters can both
  4677. open and close emphasis, then the sum of the lengths of the
  4678. delimiter runs containing the opening and closing delimiters
  4679. must not be a multiple of 3.
  4680. 10. Strong emphasis begins with a delimiter that
  4681. [can open strong emphasis] and ends with a delimiter that
  4682. [can close strong emphasis], and that uses the same character
  4683. (`_` or `*`) as the opening delimiter. The
  4684. opening and closing delimiters must belong to separate
  4685. [delimiter runs]. If one of the delimiters can both open
  4686. and close strong emphasis, then the sum of the lengths of
  4687. the delimiter runs containing the opening and closing
  4688. delimiters must not be a multiple of 3.
  4689. 11. A literal `*` character cannot occur at the beginning or end of
  4690. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4691. is backslash-escaped.
  4692. 12. A literal `_` character cannot occur at the beginning or end of
  4693. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4694. is backslash-escaped.
  4695. Where rules 1--12 above are compatible with multiple parsings,
  4696. the following principles resolve ambiguity:
  4697. 13. The number of nestings should be minimized. Thus, for example,
  4698. an interpretation `<strong>...</strong>` is always preferred to
  4699. `<em><em>...</em></em>`.
  4700. 14. An interpretation `<em><strong>...</strong></em>` is always
  4701. preferred to `<strong><em>...</em></strong>`.
  4702. 15. When two potential emphasis or strong emphasis spans overlap,
  4703. so that the second begins before the first ends and ends after
  4704. the first ends, the first takes precedence. Thus, for example,
  4705. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4706. than `*foo <em>bar* baz</em>`.
  4707. 16. When there are two potential emphasis or strong emphasis spans
  4708. with the same closing delimiter, the shorter one (the one that
  4709. opens later) takes precedence. Thus, for example,
  4710. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4711. rather than `<strong>foo **bar baz</strong>`.
  4712. 17. Inline code spans, links, images, and HTML tags group more tightly
  4713. than emphasis. So, when there is a choice between an interpretation
  4714. that contains one of these elements and one that does not, the
  4715. former always wins. Thus, for example, `*[foo*](bar)` is
  4716. parsed as `*<a href="bar">foo*</a>` rather than as
  4717. `<em>[foo</em>](bar)`.
  4718. These rules can be illustrated through a series of examples.
  4719. Rule 1:
  4720. ```````````````````````````````` example
  4721. *foo bar*
  4722. .
  4723. <p><em>foo bar</em></p>
  4724. ````````````````````````````````
  4725. This is not emphasis, because the opening `*` is followed by
  4726. whitespace, and hence not part of a [left-flanking delimiter run]:
  4727. ```````````````````````````````` example
  4728. a * foo bar*
  4729. .
  4730. <p>a * foo bar*</p>
  4731. ````````````````````````````````
  4732. This is not emphasis, because the opening `*` is preceded
  4733. by an alphanumeric and followed by punctuation, and hence
  4734. not part of a [left-flanking delimiter run]:
  4735. ```````````````````````````````` example
  4736. a*"foo"*
  4737. .
  4738. <p>a*&quot;foo&quot;*</p>
  4739. ````````````````````````````````
  4740. Unicode nonbreaking spaces count as whitespace, too:
  4741. ```````````````````````````````` example
  4742. * a *
  4743. .
  4744. <p>* a *</p>
  4745. ````````````````````````````````
  4746. Intraword emphasis with `*` is permitted:
  4747. ```````````````````````````````` example
  4748. foo*bar*
  4749. .
  4750. <p>foo<em>bar</em></p>
  4751. ````````````````````````````````
  4752. ```````````````````````````````` example
  4753. 5*6*78
  4754. .
  4755. <p>5<em>6</em>78</p>
  4756. ````````````````````````````````
  4757. Rule 2:
  4758. ```````````````````````````````` example
  4759. _foo bar_
  4760. .
  4761. <p><em>foo bar</em></p>
  4762. ````````````````````````````````
  4763. This is not emphasis, because the opening `_` is followed by
  4764. whitespace:
  4765. ```````````````````````````````` example
  4766. _ foo bar_
  4767. .
  4768. <p>_ foo bar_</p>
  4769. ````````````````````````````````
  4770. This is not emphasis, because the opening `_` is preceded
  4771. by an alphanumeric and followed by punctuation:
  4772. ```````````````````````````````` example
  4773. a_"foo"_
  4774. .
  4775. <p>a_&quot;foo&quot;_</p>
  4776. ````````````````````````````````
  4777. Emphasis with `_` is not allowed inside words:
  4778. ```````````````````````````````` example
  4779. foo_bar_
  4780. .
  4781. <p>foo_bar_</p>
  4782. ````````````````````````````````
  4783. ```````````````````````````````` example
  4784. 5_6_78
  4785. .
  4786. <p>5_6_78</p>
  4787. ````````````````````````````````
  4788. ```````````````````````````````` example
  4789. пристаням_стремятся_
  4790. .
  4791. <p>пристаням_стремятся_</p>
  4792. ````````````````````````````````
  4793. Here `_` does not generate emphasis, because the first delimiter run
  4794. is right-flanking and the second left-flanking:
  4795. ```````````````````````````````` example
  4796. aa_"bb"_cc
  4797. .
  4798. <p>aa_&quot;bb&quot;_cc</p>
  4799. ````````````````````````````````
  4800. This is emphasis, even though the opening delimiter is
  4801. both left- and right-flanking, because it is preceded by
  4802. punctuation:
  4803. ```````````````````````````````` example
  4804. foo-_(bar)_
  4805. .
  4806. <p>foo-<em>(bar)</em></p>
  4807. ````````````````````````````````
  4808. Rule 3:
  4809. This is not emphasis, because the closing delimiter does
  4810. not match the opening delimiter:
  4811. ```````````````````````````````` example
  4812. _foo*
  4813. .
  4814. <p>_foo*</p>
  4815. ````````````````````````````````
  4816. This is not emphasis, because the closing `*` is preceded by
  4817. whitespace:
  4818. ```````````````````````````````` example
  4819. *foo bar *
  4820. .
  4821. <p>*foo bar *</p>
  4822. ````````````````````````````````
  4823. A newline also counts as whitespace:
  4824. ```````````````````````````````` example
  4825. *foo bar
  4826. *
  4827. .
  4828. <p>*foo bar
  4829. *</p>
  4830. ````````````````````````````````
  4831. This is not emphasis, because the second `*` is
  4832. preceded by punctuation and followed by an alphanumeric
  4833. (hence it is not part of a [right-flanking delimiter run]:
  4834. ```````````````````````````````` example
  4835. *(*foo)
  4836. .
  4837. <p>*(*foo)</p>
  4838. ````````````````````````````````
  4839. The point of this restriction is more easily appreciated
  4840. with this example:
  4841. ```````````````````````````````` example
  4842. *(*foo*)*
  4843. .
  4844. <p><em>(<em>foo</em>)</em></p>
  4845. ````````````````````````````````
  4846. Intraword emphasis with `*` is allowed:
  4847. ```````````````````````````````` example
  4848. *foo*bar
  4849. .
  4850. <p><em>foo</em>bar</p>
  4851. ````````````````````````````````
  4852. Rule 4:
  4853. This is not emphasis, because the closing `_` is preceded by
  4854. whitespace:
  4855. ```````````````````````````````` example
  4856. _foo bar _
  4857. .
  4858. <p>_foo bar _</p>
  4859. ````````````````````````````````
  4860. This is not emphasis, because the second `_` is
  4861. preceded by punctuation and followed by an alphanumeric:
  4862. ```````````````````````````````` example
  4863. _(_foo)
  4864. .
  4865. <p>_(_foo)</p>
  4866. ````````````````````````````````
  4867. This is emphasis within emphasis:
  4868. ```````````````````````````````` example
  4869. _(_foo_)_
  4870. .
  4871. <p><em>(<em>foo</em>)</em></p>
  4872. ````````````````````````````````
  4873. Intraword emphasis is disallowed for `_`:
  4874. ```````````````````````````````` example
  4875. _foo_bar
  4876. .
  4877. <p>_foo_bar</p>
  4878. ````````````````````````````````
  4879. ```````````````````````````````` example
  4880. _пристаням_стремятся
  4881. .
  4882. <p>_пристаням_стремятся</p>
  4883. ````````````````````````````````
  4884. ```````````````````````````````` example
  4885. _foo_bar_baz_
  4886. .
  4887. <p><em>foo_bar_baz</em></p>
  4888. ````````````````````````````````
  4889. This is emphasis, even though the closing delimiter is
  4890. both left- and right-flanking, because it is followed by
  4891. punctuation:
  4892. ```````````````````````````````` example
  4893. _(bar)_.
  4894. .
  4895. <p><em>(bar)</em>.</p>
  4896. ````````````````````````````````
  4897. Rule 5:
  4898. ```````````````````````````````` example
  4899. **foo bar**
  4900. .
  4901. <p><strong>foo bar</strong></p>
  4902. ````````````````````````````````
  4903. This is not strong emphasis, because the opening delimiter is
  4904. followed by whitespace:
  4905. ```````````````````````````````` example
  4906. ** foo bar**
  4907. .
  4908. <p>** foo bar**</p>
  4909. ````````````````````````````````
  4910. This is not strong emphasis, because the opening `**` is preceded
  4911. by an alphanumeric and followed by punctuation, and hence
  4912. not part of a [left-flanking delimiter run]:
  4913. ```````````````````````````````` example
  4914. a**"foo"**
  4915. .
  4916. <p>a**&quot;foo&quot;**</p>
  4917. ````````````````````````````````
  4918. Intraword strong emphasis with `**` is permitted:
  4919. ```````````````````````````````` example
  4920. foo**bar**
  4921. .
  4922. <p>foo<strong>bar</strong></p>
  4923. ````````````````````````````````
  4924. Rule 6:
  4925. ```````````````````````````````` example
  4926. __foo bar__
  4927. .
  4928. <p><strong>foo bar</strong></p>
  4929. ````````````````````````````````
  4930. This is not strong emphasis, because the opening delimiter is
  4931. followed by whitespace:
  4932. ```````````````````````````````` example
  4933. __ foo bar__
  4934. .
  4935. <p>__ foo bar__</p>
  4936. ````````````````````````````````
  4937. A newline counts as whitespace:
  4938. ```````````````````````````````` example
  4939. __
  4940. foo bar__
  4941. .
  4942. <p>__
  4943. foo bar__</p>
  4944. ````````````````````````````````
  4945. This is not strong emphasis, because the opening `__` is preceded
  4946. by an alphanumeric and followed by punctuation:
  4947. ```````````````````````````````` example
  4948. a__"foo"__
  4949. .
  4950. <p>a__&quot;foo&quot;__</p>
  4951. ````````````````````````````````
  4952. Intraword strong emphasis is forbidden with `__`:
  4953. ```````````````````````````````` example
  4954. foo__bar__
  4955. .
  4956. <p>foo__bar__</p>
  4957. ````````````````````````````````
  4958. ```````````````````````````````` example
  4959. 5__6__78
  4960. .
  4961. <p>5__6__78</p>
  4962. ````````````````````````````````
  4963. ```````````````````````````````` example
  4964. пристаням__стремятся__
  4965. .
  4966. <p>пристаням__стремятся__</p>
  4967. ````````````````````````````````
  4968. ```````````````````````````````` example
  4969. __foo, __bar__, baz__
  4970. .
  4971. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4972. ````````````````````````````````
  4973. This is strong emphasis, even though the opening delimiter is
  4974. both left- and right-flanking, because it is preceded by
  4975. punctuation:
  4976. ```````````````````````````````` example
  4977. foo-__(bar)__
  4978. .
  4979. <p>foo-<strong>(bar)</strong></p>
  4980. ````````````````````````````````
  4981. Rule 7:
  4982. This is not strong emphasis, because the closing delimiter is preceded
  4983. by whitespace:
  4984. ```````````````````````````````` example
  4985. **foo bar **
  4986. .
  4987. <p>**foo bar **</p>
  4988. ````````````````````````````````
  4989. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4990. Rule 11.)
  4991. This is not strong emphasis, because the second `**` is
  4992. preceded by punctuation and followed by an alphanumeric:
  4993. ```````````````````````````````` example
  4994. **(**foo)
  4995. .
  4996. <p>**(**foo)</p>
  4997. ````````````````````````````````
  4998. The point of this restriction is more easily appreciated
  4999. with these examples:
  5000. ```````````````````````````````` example
  5001. *(**foo**)*
  5002. .
  5003. <p><em>(<strong>foo</strong>)</em></p>
  5004. ````````````````````````````````
  5005. ```````````````````````````````` example
  5006. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5007. *Asclepias physocarpa*)**
  5008. .
  5009. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5010. <em>Asclepias physocarpa</em>)</strong></p>
  5011. ````````````````````````````````
  5012. ```````````````````````````````` example
  5013. **foo "*bar*" foo**
  5014. .
  5015. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5016. ````````````````````````````````
  5017. Intraword emphasis:
  5018. ```````````````````````````````` example
  5019. **foo**bar
  5020. .
  5021. <p><strong>foo</strong>bar</p>
  5022. ````````````````````````````````
  5023. Rule 8:
  5024. This is not strong emphasis, because the closing delimiter is
  5025. preceded by whitespace:
  5026. ```````````````````````````````` example
  5027. __foo bar __
  5028. .
  5029. <p>__foo bar __</p>
  5030. ````````````````````````````````
  5031. This is not strong emphasis, because the second `__` is
  5032. preceded by punctuation and followed by an alphanumeric:
  5033. ```````````````````````````````` example
  5034. __(__foo)
  5035. .
  5036. <p>__(__foo)</p>
  5037. ````````````````````````````````
  5038. The point of this restriction is more easily appreciated
  5039. with this example:
  5040. ```````````````````````````````` example
  5041. _(__foo__)_
  5042. .
  5043. <p><em>(<strong>foo</strong>)</em></p>
  5044. ````````````````````````````````
  5045. Intraword strong emphasis is forbidden with `__`:
  5046. ```````````````````````````````` example
  5047. __foo__bar
  5048. .
  5049. <p>__foo__bar</p>
  5050. ````````````````````````````````
  5051. ```````````````````````````````` example
  5052. __пристаням__стремятся
  5053. .
  5054. <p>__пристаням__стремятся</p>
  5055. ````````````````````````````````
  5056. ```````````````````````````````` example
  5057. __foo__bar__baz__
  5058. .
  5059. <p><strong>foo__bar__baz</strong></p>
  5060. ````````````````````````````````
  5061. This is strong emphasis, even though the closing delimiter is
  5062. both left- and right-flanking, because it is followed by
  5063. punctuation:
  5064. ```````````````````````````````` example
  5065. __(bar)__.
  5066. .
  5067. <p><strong>(bar)</strong>.</p>
  5068. ````````````````````````````````
  5069. Rule 9:
  5070. Any nonempty sequence of inline elements can be the contents of an
  5071. emphasized span.
  5072. ```````````````````````````````` example
  5073. *foo [bar](/url)*
  5074. .
  5075. <p><em>foo <a href="/url">bar</a></em></p>
  5076. ````````````````````````````````
  5077. ```````````````````````````````` example
  5078. *foo
  5079. bar*
  5080. .
  5081. <p><em>foo
  5082. bar</em></p>
  5083. ````````````````````````````````
  5084. In particular, emphasis and strong emphasis can be nested
  5085. inside emphasis:
  5086. ```````````````````````````````` example
  5087. _foo __bar__ baz_
  5088. .
  5089. <p><em>foo <strong>bar</strong> baz</em></p>
  5090. ````````````````````````````````
  5091. ```````````````````````````````` example
  5092. _foo _bar_ baz_
  5093. .
  5094. <p><em>foo <em>bar</em> baz</em></p>
  5095. ````````````````````````````````
  5096. ```````````````````````````````` example
  5097. __foo_ bar_
  5098. .
  5099. <p><em><em>foo</em> bar</em></p>
  5100. ````````````````````````````````
  5101. ```````````````````````````````` example
  5102. *foo *bar**
  5103. .
  5104. <p><em>foo <em>bar</em></em></p>
  5105. ````````````````````````````````
  5106. ```````````````````````````````` example
  5107. *foo **bar** baz*
  5108. .
  5109. <p><em>foo <strong>bar</strong> baz</em></p>
  5110. ````````````````````````````````
  5111. ```````````````````````````````` example
  5112. *foo**bar**baz*
  5113. .
  5114. <p><em>foo<strong>bar</strong>baz</em></p>
  5115. ````````````````````````````````
  5116. Note that in the preceding case, the interpretation
  5117. ``` markdown
  5118. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5119. ```
  5120. is precluded by the condition that a delimiter that
  5121. can both open and close (like the `*` after `foo`)
  5122. cannot form emphasis if the sum of the lengths of
  5123. the delimiter runs containing the opening and
  5124. closing delimiters is a multiple of 3.
  5125. The same condition ensures that the following
  5126. cases are all strong emphasis nested inside
  5127. emphasis, even when the interior spaces are
  5128. omitted:
  5129. ```````````````````````````````` example
  5130. ***foo** bar*
  5131. .
  5132. <p><em><strong>foo</strong> bar</em></p>
  5133. ````````````````````````````````
  5134. ```````````````````````````````` example
  5135. *foo **bar***
  5136. .
  5137. <p><em>foo <strong>bar</strong></em></p>
  5138. ````````````````````````````````
  5139. ```````````````````````````````` example
  5140. *foo**bar***
  5141. .
  5142. <p><em>foo<strong>bar</strong></em></p>
  5143. ````````````````````````````````
  5144. Indefinite levels of nesting are possible:
  5145. ```````````````````````````````` example
  5146. *foo **bar *baz* bim** bop*
  5147. .
  5148. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5149. ````````````````````````````````
  5150. ```````````````````````````````` example
  5151. *foo [*bar*](/url)*
  5152. .
  5153. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5154. ````````````````````````````````
  5155. There can be no empty emphasis or strong emphasis:
  5156. ```````````````````````````````` example
  5157. ** is not an empty emphasis
  5158. .
  5159. <p>** is not an empty emphasis</p>
  5160. ````````````````````````````````
  5161. ```````````````````````````````` example
  5162. **** is not an empty strong emphasis
  5163. .
  5164. <p>**** is not an empty strong emphasis</p>
  5165. ````````````````````````````````
  5166. Rule 10:
  5167. Any nonempty sequence of inline elements can be the contents of an
  5168. strongly emphasized span.
  5169. ```````````````````````````````` example
  5170. **foo [bar](/url)**
  5171. .
  5172. <p><strong>foo <a href="/url">bar</a></strong></p>
  5173. ````````````````````````````````
  5174. ```````````````````````````````` example
  5175. **foo
  5176. bar**
  5177. .
  5178. <p><strong>foo
  5179. bar</strong></p>
  5180. ````````````````````````````````
  5181. In particular, emphasis and strong emphasis can be nested
  5182. inside strong emphasis:
  5183. ```````````````````````````````` example
  5184. __foo _bar_ baz__
  5185. .
  5186. <p><strong>foo <em>bar</em> baz</strong></p>
  5187. ````````````````````````````````
  5188. ```````````````````````````````` example
  5189. __foo __bar__ baz__
  5190. .
  5191. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5192. ````````````````````````````````
  5193. ```````````````````````````````` example
  5194. ____foo__ bar__
  5195. .
  5196. <p><strong><strong>foo</strong> bar</strong></p>
  5197. ````````````````````````````````
  5198. ```````````````````````````````` example
  5199. **foo **bar****
  5200. .
  5201. <p><strong>foo <strong>bar</strong></strong></p>
  5202. ````````````````````````````````
  5203. ```````````````````````````````` example
  5204. **foo *bar* baz**
  5205. .
  5206. <p><strong>foo <em>bar</em> baz</strong></p>
  5207. ````````````````````````````````
  5208. ```````````````````````````````` example
  5209. **foo*bar*baz**
  5210. .
  5211. <p><strong>foo<em>bar</em>baz</strong></p>
  5212. ````````````````````````````````
  5213. ```````````````````````````````` example
  5214. ***foo* bar**
  5215. .
  5216. <p><strong><em>foo</em> bar</strong></p>
  5217. ````````````````````````````````
  5218. ```````````````````````````````` example
  5219. **foo *bar***
  5220. .
  5221. <p><strong>foo <em>bar</em></strong></p>
  5222. ````````````````````````````````
  5223. Indefinite levels of nesting are possible:
  5224. ```````````````````````````````` example
  5225. **foo *bar **baz**
  5226. bim* bop**
  5227. .
  5228. <p><strong>foo <em>bar <strong>baz</strong>
  5229. bim</em> bop</strong></p>
  5230. ````````````````````````````````
  5231. ```````````````````````````````` example
  5232. **foo [*bar*](/url)**
  5233. .
  5234. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5235. ````````````````````````````````
  5236. There can be no empty emphasis or strong emphasis:
  5237. ```````````````````````````````` example
  5238. __ is not an empty emphasis
  5239. .
  5240. <p>__ is not an empty emphasis</p>
  5241. ````````````````````````````````
  5242. ```````````````````````````````` example
  5243. ____ is not an empty strong emphasis
  5244. .
  5245. <p>____ is not an empty strong emphasis</p>
  5246. ````````````````````````````````
  5247. Rule 11:
  5248. ```````````````````````````````` example
  5249. foo ***
  5250. .
  5251. <p>foo ***</p>
  5252. ````````````````````````````````
  5253. ```````````````````````````````` example
  5254. foo *\**
  5255. .
  5256. <p>foo <em>*</em></p>
  5257. ````````````````````````````````
  5258. ```````````````````````````````` example
  5259. foo *_*
  5260. .
  5261. <p>foo <em>_</em></p>
  5262. ````````````````````````````````
  5263. ```````````````````````````````` example
  5264. foo *****
  5265. .
  5266. <p>foo *****</p>
  5267. ````````````````````````````````
  5268. ```````````````````````````````` example
  5269. foo **\***
  5270. .
  5271. <p>foo <strong>*</strong></p>
  5272. ````````````````````````````````
  5273. ```````````````````````````````` example
  5274. foo **_**
  5275. .
  5276. <p>foo <strong>_</strong></p>
  5277. ````````````````````````````````
  5278. Note that when delimiters do not match evenly, Rule 11 determines
  5279. that the excess literal `*` characters will appear outside of the
  5280. emphasis, rather than inside it:
  5281. ```````````````````````````````` example
  5282. **foo*
  5283. .
  5284. <p>*<em>foo</em></p>
  5285. ````````````````````````````````
  5286. ```````````````````````````````` example
  5287. *foo**
  5288. .
  5289. <p><em>foo</em>*</p>
  5290. ````````````````````````````````
  5291. ```````````````````````````````` example
  5292. ***foo**
  5293. .
  5294. <p>*<strong>foo</strong></p>
  5295. ````````````````````````````````
  5296. ```````````````````````````````` example
  5297. ****foo*
  5298. .
  5299. <p>***<em>foo</em></p>
  5300. ````````````````````````````````
  5301. ```````````````````````````````` example
  5302. **foo***
  5303. .
  5304. <p><strong>foo</strong>*</p>
  5305. ````````````````````````````````
  5306. ```````````````````````````````` example
  5307. *foo****
  5308. .
  5309. <p><em>foo</em>***</p>
  5310. ````````````````````````````````
  5311. Rule 12:
  5312. ```````````````````````````````` example
  5313. foo ___
  5314. .
  5315. <p>foo ___</p>
  5316. ````````````````````````````````
  5317. ```````````````````````````````` example
  5318. foo _\__
  5319. .
  5320. <p>foo <em>_</em></p>
  5321. ````````````````````````````````
  5322. ```````````````````````````````` example
  5323. foo _*_
  5324. .
  5325. <p>foo <em>*</em></p>
  5326. ````````````````````````````````
  5327. ```````````````````````````````` example
  5328. foo _____
  5329. .
  5330. <p>foo _____</p>
  5331. ````````````````````````````````
  5332. ```````````````````````````````` example
  5333. foo __\___
  5334. .
  5335. <p>foo <strong>_</strong></p>
  5336. ````````````````````````````````
  5337. ```````````````````````````````` example
  5338. foo __*__
  5339. .
  5340. <p>foo <strong>*</strong></p>
  5341. ````````````````````````````````
  5342. ```````````````````````````````` example
  5343. __foo_
  5344. .
  5345. <p>_<em>foo</em></p>
  5346. ````````````````````````````````
  5347. Note that when delimiters do not match evenly, Rule 12 determines
  5348. that the excess literal `_` characters will appear outside of the
  5349. emphasis, rather than inside it:
  5350. ```````````````````````````````` example
  5351. _foo__
  5352. .
  5353. <p><em>foo</em>_</p>
  5354. ````````````````````````````````
  5355. ```````````````````````````````` example
  5356. ___foo__
  5357. .
  5358. <p>_<strong>foo</strong></p>
  5359. ````````````````````````````````
  5360. ```````````````````````````````` example
  5361. ____foo_
  5362. .
  5363. <p>___<em>foo</em></p>
  5364. ````````````````````````````````
  5365. ```````````````````````````````` example
  5366. __foo___
  5367. .
  5368. <p><strong>foo</strong>_</p>
  5369. ````````````````````````````````
  5370. ```````````````````````````````` example
  5371. _foo____
  5372. .
  5373. <p><em>foo</em>___</p>
  5374. ````````````````````````````````
  5375. Rule 13 implies that if you want emphasis nested directly inside
  5376. emphasis, you must use different delimiters:
  5377. ```````````````````````````````` example
  5378. **foo**
  5379. .
  5380. <p><strong>foo</strong></p>
  5381. ````````````````````````````````
  5382. ```````````````````````````````` example
  5383. *_foo_*
  5384. .
  5385. <p><em><em>foo</em></em></p>
  5386. ````````````````````````````````
  5387. ```````````````````````````````` example
  5388. __foo__
  5389. .
  5390. <p><strong>foo</strong></p>
  5391. ````````````````````````````````
  5392. ```````````````````````````````` example
  5393. _*foo*_
  5394. .
  5395. <p><em><em>foo</em></em></p>
  5396. ````````````````````````````````
  5397. However, strong emphasis within strong emphasis is possible without
  5398. switching delimiters:
  5399. ```````````````````````````````` example
  5400. ****foo****
  5401. .
  5402. <p><strong><strong>foo</strong></strong></p>
  5403. ````````````````````````````````
  5404. ```````````````````````````````` example
  5405. ____foo____
  5406. .
  5407. <p><strong><strong>foo</strong></strong></p>
  5408. ````````````````````````````````
  5409. Rule 13 can be applied to arbitrarily long sequences of
  5410. delimiters:
  5411. ```````````````````````````````` example
  5412. ******foo******
  5413. .
  5414. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5415. ````````````````````````````````
  5416. Rule 14:
  5417. ```````````````````````````````` example
  5418. ***foo***
  5419. .
  5420. <p><em><strong>foo</strong></em></p>
  5421. ````````````````````````````````
  5422. ```````````````````````````````` example
  5423. _____foo_____
  5424. .
  5425. <p><em><strong><strong>foo</strong></strong></em></p>
  5426. ````````````````````````````````
  5427. Rule 15:
  5428. ```````````````````````````````` example
  5429. *foo _bar* baz_
  5430. .
  5431. <p><em>foo _bar</em> baz_</p>
  5432. ````````````````````````````````
  5433. ```````````````````````````````` example
  5434. *foo __bar *baz bim__ bam*
  5435. .
  5436. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5437. ````````````````````````````````
  5438. Rule 16:
  5439. ```````````````````````````````` example
  5440. **foo **bar baz**
  5441. .
  5442. <p>**foo <strong>bar baz</strong></p>
  5443. ````````````````````````````````
  5444. ```````````````````````````````` example
  5445. *foo *bar baz*
  5446. .
  5447. <p>*foo <em>bar baz</em></p>
  5448. ````````````````````````````````
  5449. Rule 17:
  5450. ```````````````````````````````` example
  5451. *[bar*](/url)
  5452. .
  5453. <p>*<a href="/url">bar*</a></p>
  5454. ````````````````````````````````
  5455. ```````````````````````````````` example
  5456. _foo [bar_](/url)
  5457. .
  5458. <p>_foo <a href="/url">bar_</a></p>
  5459. ````````````````````````````````
  5460. ```````````````````````````````` example
  5461. *<img src="foo" title="*"/>
  5462. .
  5463. <p>*<img src="foo" title="*"/></p>
  5464. ````````````````````````````````
  5465. ```````````````````````````````` example
  5466. **<a href="**">
  5467. .
  5468. <p>**<a href="**"></p>
  5469. ````````````````````````````````
  5470. ```````````````````````````````` example
  5471. __<a href="__">
  5472. .
  5473. <p>__<a href="__"></p>
  5474. ````````````````````````````````
  5475. ```````````````````````````````` example
  5476. *a `*`*
  5477. .
  5478. <p><em>a <code>*</code></em></p>
  5479. ````````````````````````````````
  5480. ```````````````````````````````` example
  5481. _a `_`_
  5482. .
  5483. <p><em>a <code>_</code></em></p>
  5484. ````````````````````````````````
  5485. ```````````````````````````````` example
  5486. **a<http://foo.bar/?q=**>
  5487. .
  5488. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5489. ````````````````````````````````
  5490. ```````````````````````````````` example
  5491. __a<http://foo.bar/?q=__>
  5492. .
  5493. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5494. ````````````````````````````````
  5495. ## Links
  5496. A link contains [link text] (the visible text), a [link destination]
  5497. (the URI that is the link destination), and optionally a [link title].
  5498. There are two basic kinds of links in Markdown. In [inline links] the
  5499. destination and title are given immediately after the link text. In
  5500. [reference links] the destination and title are defined elsewhere in
  5501. the document.
  5502. A [link text](@) consists of a sequence of zero or more
  5503. inline elements enclosed by square brackets (`[` and `]`). The
  5504. following rules apply:
  5505. - Links may not contain other links, at any level of nesting. If
  5506. multiple otherwise valid link definitions appear nested inside each
  5507. other, the inner-most definition is used.
  5508. - Brackets are allowed in the [link text] only if (a) they
  5509. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5510. with an open bracket `[`, a sequence of zero or more inlines, and
  5511. a close bracket `]`.
  5512. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5513. than the brackets in link text. Thus, for example,
  5514. `` [foo`]` `` could not be a link text, since the second `]`
  5515. is part of a code span.
  5516. - The brackets in link text bind more tightly than markers for
  5517. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5518. A [link destination](@) consists of either
  5519. - a sequence of zero or more characters between an opening `<` and a
  5520. closing `>` that contains no line breaks or unescaped
  5521. `<` or `>` characters, or
  5522. - a nonempty sequence of characters that does not include
  5523. ASCII space or control characters, and includes parentheses
  5524. only if (a) they are backslash-escaped or (b) they are part of
  5525. a balanced pair of unescaped parentheses. (Implementations
  5526. may impose limits on parentheses nesting to avoid performance
  5527. issues, but at least three levels of nesting should be supported.)
  5528. A [link title](@) consists of either
  5529. - a sequence of zero or more characters between straight double-quote
  5530. characters (`"`), including a `"` character only if it is
  5531. backslash-escaped, or
  5532. - a sequence of zero or more characters between straight single-quote
  5533. characters (`'`), including a `'` character only if it is
  5534. backslash-escaped, or
  5535. - a sequence of zero or more characters between matching parentheses
  5536. (`(...)`), including a `)` character only if it is backslash-escaped.
  5537. Although [link titles] may span multiple lines, they may not contain
  5538. a [blank line].
  5539. An [inline link](@) consists of a [link text] followed immediately
  5540. by a left parenthesis `(`, optional [whitespace], an optional
  5541. [link destination], an optional [link title] separated from the link
  5542. destination by [whitespace], optional [whitespace], and a right
  5543. parenthesis `)`. The link's text consists of the inlines contained
  5544. in the [link text] (excluding the enclosing square brackets).
  5545. The link's URI consists of the link destination, excluding enclosing
  5546. `<...>` if present, with backslash-escapes in effect as described
  5547. above. The link's title consists of the link title, excluding its
  5548. enclosing delimiters, with backslash-escapes in effect as described
  5549. above.
  5550. Here is a simple inline link:
  5551. ```````````````````````````````` example
  5552. [link](/uri "title")
  5553. .
  5554. <p><a href="/uri" title="title">link</a></p>
  5555. ````````````````````````````````
  5556. The title may be omitted:
  5557. ```````````````````````````````` example
  5558. [link](/uri)
  5559. .
  5560. <p><a href="/uri">link</a></p>
  5561. ````````````````````````````````
  5562. Both the title and the destination may be omitted:
  5563. ```````````````````````````````` example
  5564. [link]()
  5565. .
  5566. <p><a href="">link</a></p>
  5567. ````````````````````````````````
  5568. ```````````````````````````````` example
  5569. [link](<>)
  5570. .
  5571. <p><a href="">link</a></p>
  5572. ````````````````````````````````
  5573. The destination can only contain spaces if it is
  5574. enclosed in pointy brackets:
  5575. ```````````````````````````````` example
  5576. [link](/my uri)
  5577. .
  5578. <p>[link](/my uri)</p>
  5579. ````````````````````````````````
  5580. ```````````````````````````````` example
  5581. [link](</my uri>)
  5582. .
  5583. <p><a href="my%20uri">link</a></p>
  5584. ````````````````````````````````
  5585. The destination cannot contain line breaks,
  5586. even if enclosed in pointy brackets:
  5587. ```````````````````````````````` example
  5588. [link](foo
  5589. bar)
  5590. .
  5591. <p>[link](foo
  5592. bar)</p>
  5593. ````````````````````````````````
  5594. ```````````````````````````````` example
  5595. [link](<foo
  5596. bar>)
  5597. .
  5598. <p>[link](<foo
  5599. bar>)</p>
  5600. ````````````````````````````````
  5601. Parentheses inside the link destination may be escaped:
  5602. ```````````````````````````````` example
  5603. [link](\(foo\))
  5604. .
  5605. <p><a href="(foo)">link</a></p>
  5606. ````````````````````````````````
  5607. Any number of parentheses are allowed without escaping, as long as they are
  5608. balanced:
  5609. ```````````````````````````````` example
  5610. [link](foo(and(bar)))
  5611. .
  5612. <p><a href="foo(and(bar))">link</a></p>
  5613. ````````````````````````````````
  5614. However, if you have unbalanced parentheses, you need to escape or use the
  5615. `<...>` form:
  5616. ```````````````````````````````` example
  5617. [link](foo\(and\(bar\))
  5618. .
  5619. <p><a href="foo(and(bar)">link</a></p>
  5620. ````````````````````````````````
  5621. ```````````````````````````````` example
  5622. [link](<foo(and(bar)>)
  5623. .
  5624. <p><a href="foo(and(bar)">link</a></p>
  5625. ````````````````````````````````
  5626. Parentheses and other symbols can also be escaped, as usual
  5627. in Markdown:
  5628. ```````````````````````````````` example
  5629. [link](foo\)\:)
  5630. .
  5631. <p><a href="foo):">link</a></p>
  5632. ````````````````````````````````
  5633. A link can contain fragment identifiers and queries:
  5634. ```````````````````````````````` example
  5635. [link](#fragment)
  5636. [link](http://example.com#fragment)
  5637. [link](http://example.com?foo=3#frag)
  5638. .
  5639. <p><a href="#fragment">link</a></p>
  5640. <p><a href="http://example.com#fragment">link</a></p>
  5641. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5642. ````````````````````````````````
  5643. Note that a backslash before a non-escapable character is
  5644. just a backslash:
  5645. ```````````````````````````````` example
  5646. [link](foo\bar)
  5647. .
  5648. <p><a href="foo%5Cbar">link</a></p>
  5649. ````````````````````````````````
  5650. URL-escaping should be left alone inside the destination, as all
  5651. URL-escaped characters are also valid URL characters. Entity and
  5652. numerical character references in the destination will be parsed
  5653. into the corresponding Unicode code points, as usual. These may
  5654. be optionally URL-escaped when written as HTML, but this spec
  5655. does not enforce any particular policy for rendering URLs in
  5656. HTML or other formats. Renderers may make different decisions
  5657. about how to escape or normalize URLs in the output.
  5658. ```````````````````````````````` example
  5659. [link](foo%20b&auml;)
  5660. .
  5661. <p><a href="foo%20b%C3%A4">link</a></p>
  5662. ````````````````````````````````
  5663. Note that, because titles can often be parsed as destinations,
  5664. if you try to omit the destination and keep the title, you'll
  5665. get unexpected results:
  5666. ```````````````````````````````` example
  5667. [link]("title")
  5668. .
  5669. <p><a href="%22title%22">link</a></p>
  5670. ````````````````````````````````
  5671. Titles may be in single quotes, double quotes, or parentheses:
  5672. ```````````````````````````````` example
  5673. [link](/url "title")
  5674. [link](/url 'title')
  5675. [link](/url (title))
  5676. .
  5677. <p><a href="/url" title="title">link</a>
  5678. <a href="/url" title="title">link</a>
  5679. <a href="/url" title="title">link</a></p>
  5680. ````````````````````````````````
  5681. Backslash escapes and entity and numeric character references
  5682. may be used in titles:
  5683. ```````````````````````````````` example
  5684. [link](/url "title \"&quot;")
  5685. .
  5686. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5687. ````````````````````````````````
  5688. Titles must be separated from the link using a [whitespace].
  5689. Other [Unicode whitespace] like non-breaking space doesn't work.
  5690. ```````````````````````````````` example
  5691. [link](/url "title")
  5692. .
  5693. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5694. ````````````````````````````````
  5695. Nested balanced quotes are not allowed without escaping:
  5696. ```````````````````````````````` example
  5697. [link](/url "title "and" title")
  5698. .
  5699. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5700. ````````````````````````````````
  5701. But it is easy to work around this by using a different quote type:
  5702. ```````````````````````````````` example
  5703. [link](/url 'title "and" title')
  5704. .
  5705. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5706. ````````````````````````````````
  5707. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5708. title, and its test suite included a test demonstrating this.
  5709. But it is hard to see a good rationale for the extra complexity this
  5710. brings, since there are already many ways---backslash escaping,
  5711. entity and numeric character references, or using a different
  5712. quote type for the enclosing title---to write titles containing
  5713. double quotes. `Markdown.pl`'s handling of titles has a number
  5714. of other strange features. For example, it allows single-quoted
  5715. titles in inline links, but not reference links. And, in
  5716. reference links but not inline links, it allows a title to begin
  5717. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5718. titles with no closing quotation mark, though 1.0.2b8 does not.
  5719. It seems preferable to adopt a simple, rational rule that works
  5720. the same way in inline links and link reference definitions.)
  5721. [Whitespace] is allowed around the destination and title:
  5722. ```````````````````````````````` example
  5723. [link]( /uri
  5724. "title" )
  5725. .
  5726. <p><a href="/uri" title="title">link</a></p>
  5727. ````````````````````````````````
  5728. But it is not allowed between the link text and the
  5729. following parenthesis:
  5730. ```````````````````````````````` example
  5731. [link] (/uri)
  5732. .
  5733. <p>[link] (/uri)</p>
  5734. ````````````````````````````````
  5735. The link text may contain balanced brackets, but not unbalanced ones,
  5736. unless they are escaped:
  5737. ```````````````````````````````` example
  5738. [link [foo [bar]]](/uri)
  5739. .
  5740. <p><a href="/uri">link [foo [bar]]</a></p>
  5741. ````````````````````````````````
  5742. ```````````````````````````````` example
  5743. [link] bar](/uri)
  5744. .
  5745. <p>[link] bar](/uri)</p>
  5746. ````````````````````````````````
  5747. ```````````````````````````````` example
  5748. [link [bar](/uri)
  5749. .
  5750. <p>[link <a href="/uri">bar</a></p>
  5751. ````````````````````````````````
  5752. ```````````````````````````````` example
  5753. [link \[bar](/uri)
  5754. .
  5755. <p><a href="/uri">link [bar</a></p>
  5756. ````````````````````````````````
  5757. The link text may contain inline content:
  5758. ```````````````````````````````` example
  5759. [link *foo **bar** `#`*](/uri)
  5760. .
  5761. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5762. ````````````````````````````````
  5763. ```````````````````````````````` example
  5764. [![moon](moon.jpg)](/uri)
  5765. .
  5766. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5767. ````````````````````````````````
  5768. However, links may not contain other links, at any level of nesting.
  5769. ```````````````````````````````` example
  5770. [foo [bar](/uri)](/uri)
  5771. .
  5772. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5773. ````````````````````````````````
  5774. ```````````````````````````````` example
  5775. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5776. .
  5777. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5778. ````````````````````````````````
  5779. ```````````````````````````````` example
  5780. ![[[foo](uri1)](uri2)](uri3)
  5781. .
  5782. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5783. ````````````````````````````````
  5784. These cases illustrate the precedence of link text grouping over
  5785. emphasis grouping:
  5786. ```````````````````````````````` example
  5787. *[foo*](/uri)
  5788. .
  5789. <p>*<a href="/uri">foo*</a></p>
  5790. ````````````````````````````````
  5791. ```````````````````````````````` example
  5792. [foo *bar](baz*)
  5793. .
  5794. <p><a href="baz*">foo *bar</a></p>
  5795. ````````````````````````````````
  5796. Note that brackets that *aren't* part of links do not take
  5797. precedence:
  5798. ```````````````````````````````` example
  5799. *foo [bar* baz]
  5800. .
  5801. <p><em>foo [bar</em> baz]</p>
  5802. ````````````````````````````````
  5803. These cases illustrate the precedence of HTML tags, code spans,
  5804. and autolinks over link grouping:
  5805. ```````````````````````````````` example
  5806. [foo <bar attr="](baz)">
  5807. .
  5808. <p>[foo <bar attr="](baz)"></p>
  5809. ````````````````````````````````
  5810. ```````````````````````````````` example
  5811. [foo`](/uri)`
  5812. .
  5813. <p>[foo<code>](/uri)</code></p>
  5814. ````````````````````````````````
  5815. ```````````````````````````````` example
  5816. [foo<http://example.com/?search=](uri)>
  5817. .
  5818. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5819. ````````````````````````````````
  5820. There are three kinds of [reference link](@)s:
  5821. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5822. and [shortcut](#shortcut-reference-link).
  5823. A [full reference link](@)
  5824. consists of a [link text] immediately followed by a [link label]
  5825. that [matches] a [link reference definition] elsewhere in the document.
  5826. A [link label](@) begins with a left bracket (`[`) and ends
  5827. with the first right bracket (`]`) that is not backslash-escaped.
  5828. Between these brackets there must be at least one [non-whitespace character].
  5829. Unescaped square bracket characters are not allowed inside the
  5830. opening and closing square brackets of [link labels]. A link
  5831. label can have at most 999 characters inside the square
  5832. brackets.
  5833. One label [matches](@)
  5834. another just in case their normalized forms are equal. To normalize a
  5835. label, strip off the opening and closing brackets,
  5836. perform the *Unicode case fold*, strip leading and trailing
  5837. [whitespace] and collapse consecutive internal
  5838. [whitespace] to a single space. If there are multiple
  5839. matching reference link definitions, the one that comes first in the
  5840. document is used. (It is desirable in such cases to emit a warning.)
  5841. The contents of the first link label are parsed as inlines, which are
  5842. used as the link's text. The link's URI and title are provided by the
  5843. matching [link reference definition].
  5844. Here is a simple example:
  5845. ```````````````````````````````` example
  5846. [foo][bar]
  5847. [bar]: /url "title"
  5848. .
  5849. <p><a href="/url" title="title">foo</a></p>
  5850. ````````````````````````````````
  5851. The rules for the [link text] are the same as with
  5852. [inline links]. Thus:
  5853. The link text may contain balanced brackets, but not unbalanced ones,
  5854. unless they are escaped:
  5855. ```````````````````````````````` example
  5856. [link [foo [bar]]][ref]
  5857. [ref]: /uri
  5858. .
  5859. <p><a href="/uri">link [foo [bar]]</a></p>
  5860. ````````````````````````````````
  5861. ```````````````````````````````` example
  5862. [link \[bar][ref]
  5863. [ref]: /uri
  5864. .
  5865. <p><a href="/uri">link [bar</a></p>
  5866. ````````````````````````````````
  5867. The link text may contain inline content:
  5868. ```````````````````````````````` example
  5869. [link *foo **bar** `#`*][ref]
  5870. [ref]: /uri
  5871. .
  5872. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5873. ````````````````````````````````
  5874. ```````````````````````````````` example
  5875. [![moon](moon.jpg)][ref]
  5876. [ref]: /uri
  5877. .
  5878. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5879. ````````````````````````````````
  5880. However, links may not contain other links, at any level of nesting.
  5881. ```````````````````````````````` example
  5882. [foo [bar](/uri)][ref]
  5883. [ref]: /uri
  5884. .
  5885. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5886. ````````````````````````````````
  5887. ```````````````````````````````` example
  5888. [foo *bar [baz][ref]*][ref]
  5889. [ref]: /uri
  5890. .
  5891. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5892. ````````````````````````````````
  5893. (In the examples above, we have two [shortcut reference links]
  5894. instead of one [full reference link].)
  5895. The following cases illustrate the precedence of link text grouping over
  5896. emphasis grouping:
  5897. ```````````````````````````````` example
  5898. *[foo*][ref]
  5899. [ref]: /uri
  5900. .
  5901. <p>*<a href="/uri">foo*</a></p>
  5902. ````````````````````````````````
  5903. ```````````````````````````````` example
  5904. [foo *bar][ref]
  5905. [ref]: /uri
  5906. .
  5907. <p><a href="/uri">foo *bar</a></p>
  5908. ````````````````````````````````
  5909. These cases illustrate the precedence of HTML tags, code spans,
  5910. and autolinks over link grouping:
  5911. ```````````````````````````````` example
  5912. [foo <bar attr="][ref]">
  5913. [ref]: /uri
  5914. .
  5915. <p>[foo <bar attr="][ref]"></p>
  5916. ````````````````````````````````
  5917. ```````````````````````````````` example
  5918. [foo`][ref]`
  5919. [ref]: /uri
  5920. .
  5921. <p>[foo<code>][ref]</code></p>
  5922. ````````````````````````````````
  5923. ```````````````````````````````` example
  5924. [foo<http://example.com/?search=][ref]>
  5925. [ref]: /uri
  5926. .
  5927. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5928. ````````````````````````````````
  5929. Matching is case-insensitive:
  5930. ```````````````````````````````` example
  5931. [foo][BaR]
  5932. [bar]: /url "title"
  5933. .
  5934. <p><a href="/url" title="title">foo</a></p>
  5935. ````````````````````````````````
  5936. Unicode case fold is used:
  5937. ```````````````````````````````` example
  5938. [Толпой][Толпой] is a Russian word.
  5939. [ТОЛПОЙ]: /url
  5940. .
  5941. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5942. ````````````````````````````````
  5943. Consecutive internal [whitespace] is treated as one space for
  5944. purposes of determining matching:
  5945. ```````````````````````````````` example
  5946. [Foo
  5947. bar]: /url
  5948. [Baz][Foo bar]
  5949. .
  5950. <p><a href="/url">Baz</a></p>
  5951. ````````````````````````````````
  5952. No [whitespace] is allowed between the [link text] and the
  5953. [link label]:
  5954. ```````````````````````````````` example
  5955. [foo] [bar]
  5956. [bar]: /url "title"
  5957. .
  5958. <p>[foo] <a href="/url" title="title">bar</a></p>
  5959. ````````````````````````````````
  5960. ```````````````````````````````` example
  5961. [foo]
  5962. [bar]
  5963. [bar]: /url "title"
  5964. .
  5965. <p>[foo]
  5966. <a href="/url" title="title">bar</a></p>
  5967. ````````````````````````````````
  5968. This is a departure from John Gruber's original Markdown syntax
  5969. description, which explicitly allows whitespace between the link
  5970. text and the link label. It brings reference links in line with
  5971. [inline links], which (according to both original Markdown and
  5972. this spec) cannot have whitespace after the link text. More
  5973. importantly, it prevents inadvertent capture of consecutive
  5974. [shortcut reference links]. If whitespace is allowed between the
  5975. link text and the link label, then in the following we will have
  5976. a single reference link, not two shortcut reference links, as
  5977. intended:
  5978. ``` markdown
  5979. [foo]
  5980. [bar]
  5981. [foo]: /url1
  5982. [bar]: /url2
  5983. ```
  5984. (Note that [shortcut reference links] were introduced by Gruber
  5985. himself in a beta version of `Markdown.pl`, but never included
  5986. in the official syntax description. Without shortcut reference
  5987. links, it is harmless to allow space between the link text and
  5988. link label; but once shortcut references are introduced, it is
  5989. too dangerous to allow this, as it frequently leads to
  5990. unintended results.)
  5991. When there are multiple matching [link reference definitions],
  5992. the first is used:
  5993. ```````````````````````````````` example
  5994. [foo]: /url1
  5995. [foo]: /url2
  5996. [bar][foo]
  5997. .
  5998. <p><a href="/url1">bar</a></p>
  5999. ````````````````````````````````
  6000. Note that matching is performed on normalized strings, not parsed
  6001. inline content. So the following does not match, even though the
  6002. labels define equivalent inline content:
  6003. ```````````````````````````````` example
  6004. [bar][foo\!]
  6005. [foo!]: /url
  6006. .
  6007. <p>[bar][foo!]</p>
  6008. ````````````````````````````````
  6009. [Link labels] cannot contain brackets, unless they are
  6010. backslash-escaped:
  6011. ```````````````````````````````` example
  6012. [foo][ref[]
  6013. [ref[]: /uri
  6014. .
  6015. <p>[foo][ref[]</p>
  6016. <p>[ref[]: /uri</p>
  6017. ````````````````````````````````
  6018. ```````````````````````````````` example
  6019. [foo][ref[bar]]
  6020. [ref[bar]]: /uri
  6021. .
  6022. <p>[foo][ref[bar]]</p>
  6023. <p>[ref[bar]]: /uri</p>
  6024. ````````````````````````````````
  6025. ```````````````````````````````` example
  6026. [[[foo]]]
  6027. [[[foo]]]: /url
  6028. .
  6029. <p>[[[foo]]]</p>
  6030. <p>[[[foo]]]: /url</p>
  6031. ````````````````````````````````
  6032. ```````````````````````````````` example
  6033. [foo][ref\[]
  6034. [ref\[]: /uri
  6035. .
  6036. <p><a href="/uri">foo</a></p>
  6037. ````````````````````````````````
  6038. Note that in this example `]` is not backslash-escaped:
  6039. ```````````````````````````````` example
  6040. [bar\\]: /uri
  6041. [bar\\]
  6042. .
  6043. <p><a href="/uri">bar\</a></p>
  6044. ````````````````````````````````
  6045. A [link label] must contain at least one [non-whitespace character]:
  6046. ```````````````````````````````` example
  6047. []
  6048. []: /uri
  6049. .
  6050. <p>[]</p>
  6051. <p>[]: /uri</p>
  6052. ````````````````````````````````
  6053. ```````````````````````````````` example
  6054. [
  6055. ]
  6056. [
  6057. ]: /uri
  6058. .
  6059. <p>[
  6060. ]</p>
  6061. <p>[
  6062. ]: /uri</p>
  6063. ````````````````````````````````
  6064. A [collapsed reference link](@)
  6065. consists of a [link label] that [matches] a
  6066. [link reference definition] elsewhere in the
  6067. document, followed by the string `[]`.
  6068. The contents of the first link label are parsed as inlines,
  6069. which are used as the link's text. The link's URI and title are
  6070. provided by the matching reference link definition. Thus,
  6071. `[foo][]` is equivalent to `[foo][foo]`.
  6072. ```````````````````````````````` example
  6073. [foo][]
  6074. [foo]: /url "title"
  6075. .
  6076. <p><a href="/url" title="title">foo</a></p>
  6077. ````````````````````````````````
  6078. ```````````````````````````````` example
  6079. [*foo* bar][]
  6080. [*foo* bar]: /url "title"
  6081. .
  6082. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6083. ````````````````````````````````
  6084. The link labels are case-insensitive:
  6085. ```````````````````````````````` example
  6086. [Foo][]
  6087. [foo]: /url "title"
  6088. .
  6089. <p><a href="/url" title="title">Foo</a></p>
  6090. ````````````````````````````````
  6091. As with full reference links, [whitespace] is not
  6092. allowed between the two sets of brackets:
  6093. ```````````````````````````````` example
  6094. [foo]
  6095. []
  6096. [foo]: /url "title"
  6097. .
  6098. <p><a href="/url" title="title">foo</a>
  6099. []</p>
  6100. ````````````````````````````````
  6101. A [shortcut reference link](@)
  6102. consists of a [link label] that [matches] a
  6103. [link reference definition] elsewhere in the
  6104. document and is not followed by `[]` or a link label.
  6105. The contents of the first link label are parsed as inlines,
  6106. which are used as the link's text. The link's URI and title
  6107. are provided by the matching link reference definition.
  6108. Thus, `[foo]` is equivalent to `[foo][]`.
  6109. ```````````````````````````````` example
  6110. [foo]
  6111. [foo]: /url "title"
  6112. .
  6113. <p><a href="/url" title="title">foo</a></p>
  6114. ````````````````````````````````
  6115. ```````````````````````````````` example
  6116. [*foo* bar]
  6117. [*foo* bar]: /url "title"
  6118. .
  6119. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6120. ````````````````````````````````
  6121. ```````````````````````````````` example
  6122. [[*foo* bar]]
  6123. [*foo* bar]: /url "title"
  6124. .
  6125. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6126. ````````````````````````````````
  6127. ```````````````````````````````` example
  6128. [[bar [foo]
  6129. [foo]: /url
  6130. .
  6131. <p>[[bar <a href="/url">foo</a></p>
  6132. ````````````````````````````````
  6133. The link labels are case-insensitive:
  6134. ```````````````````````````````` example
  6135. [Foo]
  6136. [foo]: /url "title"
  6137. .
  6138. <p><a href="/url" title="title">Foo</a></p>
  6139. ````````````````````````````````
  6140. A space after the link text should be preserved:
  6141. ```````````````````````````````` example
  6142. [foo] bar
  6143. [foo]: /url
  6144. .
  6145. <p><a href="/url">foo</a> bar</p>
  6146. ````````````````````````````````
  6147. If you just want bracketed text, you can backslash-escape the
  6148. opening bracket to avoid links:
  6149. ```````````````````````````````` example
  6150. \[foo]
  6151. [foo]: /url "title"
  6152. .
  6153. <p>[foo]</p>
  6154. ````````````````````````````````
  6155. Note that this is a link, because a link label ends with the first
  6156. following closing bracket:
  6157. ```````````````````````````````` example
  6158. [foo*]: /url
  6159. *[foo*]
  6160. .
  6161. <p>*<a href="/url">foo*</a></p>
  6162. ````````````````````````````````
  6163. Full and compact references take precedence over shortcut
  6164. references:
  6165. ```````````````````````````````` example
  6166. [foo][bar]
  6167. [foo]: /url1
  6168. [bar]: /url2
  6169. .
  6170. <p><a href="/url2">foo</a></p>
  6171. ````````````````````````````````
  6172. ```````````````````````````````` example
  6173. [foo][]
  6174. [foo]: /url1
  6175. .
  6176. <p><a href="/url1">foo</a></p>
  6177. ````````````````````````````````
  6178. Inline links also take precedence:
  6179. ```````````````````````````````` example
  6180. [foo]()
  6181. [foo]: /url1
  6182. .
  6183. <p><a href="">foo</a></p>
  6184. ````````````````````````````````
  6185. ```````````````````````````````` example
  6186. [foo](not a link)
  6187. [foo]: /url1
  6188. .
  6189. <p><a href="/url1">foo</a>(not a link)</p>
  6190. ````````````````````````````````
  6191. In the following case `[bar][baz]` is parsed as a reference,
  6192. `[foo]` as normal text:
  6193. ```````````````````````````````` example
  6194. [foo][bar][baz]
  6195. [baz]: /url
  6196. .
  6197. <p>[foo]<a href="/url">bar</a></p>
  6198. ````````````````````````````````
  6199. Here, though, `[foo][bar]` is parsed as a reference, since
  6200. `[bar]` is defined:
  6201. ```````````````````````````````` example
  6202. [foo][bar][baz]
  6203. [baz]: /url1
  6204. [bar]: /url2
  6205. .
  6206. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6207. ````````````````````````````````
  6208. Here `[foo]` is not parsed as a shortcut reference, because it
  6209. is followed by a link label (even though `[bar]` is not defined):
  6210. ```````````````````````````````` example
  6211. [foo][bar][baz]
  6212. [baz]: /url1
  6213. [foo]: /url2
  6214. .
  6215. <p>[foo]<a href="/url1">bar</a></p>
  6216. ````````````````````````````````
  6217. ## Images
  6218. Syntax for images is like the syntax for links, with one
  6219. difference. Instead of [link text], we have an
  6220. [image description](@). The rules for this are the
  6221. same as for [link text], except that (a) an
  6222. image description starts with `![` rather than `[`, and
  6223. (b) an image description may contain links.
  6224. An image description has inline elements
  6225. as its contents. When an image is rendered to HTML,
  6226. this is standardly used as the image's `alt` attribute.
  6227. ```````````````````````````````` example
  6228. ![foo](/url "title")
  6229. .
  6230. <p><img src="/url" alt="foo" title="title" /></p>
  6231. ````````````````````````````````
  6232. ```````````````````````````````` example
  6233. ![foo *bar*]
  6234. [foo *bar*]: train.jpg "train & tracks"
  6235. .
  6236. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6237. ````````````````````````````````
  6238. ```````````````````````````````` example
  6239. ![foo ![bar](/url)](/url2)
  6240. .
  6241. <p><img src="/url2" alt="foo bar" /></p>
  6242. ````````````````````````````````
  6243. ```````````````````````````````` example
  6244. ![foo [bar](/url)](/url2)
  6245. .
  6246. <p><img src="/url2" alt="foo bar" /></p>
  6247. ````````````````````````````````
  6248. Though this spec is concerned with parsing, not rendering, it is
  6249. recommended that in rendering to HTML, only the plain string content
  6250. of the [image description] be used. Note that in
  6251. the above example, the alt attribute's value is `foo bar`, not `foo
  6252. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6253. content is rendered, without formatting.
  6254. ```````````````````````````````` example
  6255. ![foo *bar*][]
  6256. [foo *bar*]: train.jpg "train & tracks"
  6257. .
  6258. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6259. ````````````````````````````````
  6260. ```````````````````````````````` example
  6261. ![foo *bar*][foobar]
  6262. [FOOBAR]: train.jpg "train & tracks"
  6263. .
  6264. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6265. ````````````````````````````````
  6266. ```````````````````````````````` example
  6267. ![foo](train.jpg)
  6268. .
  6269. <p><img src="train.jpg" alt="foo" /></p>
  6270. ````````````````````````````````
  6271. ```````````````````````````````` example
  6272. My ![foo bar](/path/to/train.jpg "title" )
  6273. .
  6274. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6275. ````````````````````````````````
  6276. ```````````````````````````````` example
  6277. ![foo](<url>)
  6278. .
  6279. <p><img src="url" alt="foo" /></p>
  6280. ````````````````````````````````
  6281. ```````````````````````````````` example
  6282. ![](/url)
  6283. .
  6284. <p><img src="/url" alt="" /></p>
  6285. ````````````````````````````````
  6286. Reference-style:
  6287. ```````````````````````````````` example
  6288. ![foo][bar]
  6289. [bar]: /url
  6290. .
  6291. <p><img src="/url" alt="foo" /></p>
  6292. ````````````````````````````````
  6293. ```````````````````````````````` example
  6294. ![foo][bar]
  6295. [BAR]: /url
  6296. .
  6297. <p><img src="/url" alt="foo" /></p>
  6298. ````````````````````````````````
  6299. Collapsed:
  6300. ```````````````````````````````` example
  6301. ![foo][]
  6302. [foo]: /url "title"
  6303. .
  6304. <p><img src="/url" alt="foo" title="title" /></p>
  6305. ````````````````````````````````
  6306. ```````````````````````````````` example
  6307. ![*foo* bar][]
  6308. [*foo* bar]: /url "title"
  6309. .
  6310. <p><img src="/url" alt="foo bar" title="title" /></p>
  6311. ````````````````````````````````
  6312. The labels are case-insensitive:
  6313. ```````````````````````````````` example
  6314. ![Foo][]
  6315. [foo]: /url "title"
  6316. .
  6317. <p><img src="/url" alt="Foo" title="title" /></p>
  6318. ````````````````````````````````
  6319. As with reference links, [whitespace] is not allowed
  6320. between the two sets of brackets:
  6321. ```````````````````````````````` example
  6322. ![foo]
  6323. []
  6324. [foo]: /url "title"
  6325. .
  6326. <p><img src="/url" alt="foo" title="title" />
  6327. []</p>
  6328. ````````````````````````````````
  6329. Shortcut:
  6330. ```````````````````````````````` example
  6331. ![foo]
  6332. [foo]: /url "title"
  6333. .
  6334. <p><img src="/url" alt="foo" title="title" /></p>
  6335. ````````````````````````````````
  6336. ```````````````````````````````` example
  6337. ![*foo* bar]
  6338. [*foo* bar]: /url "title"
  6339. .
  6340. <p><img src="/url" alt="foo bar" title="title" /></p>
  6341. ````````````````````````````````
  6342. Note that link labels cannot contain unescaped brackets:
  6343. ```````````````````````````````` example
  6344. ![[foo]]
  6345. [[foo]]: /url "title"
  6346. .
  6347. <p>![[foo]]</p>
  6348. <p>[[foo]]: /url &quot;title&quot;</p>
  6349. ````````````````````````````````
  6350. The link labels are case-insensitive:
  6351. ```````````````````````````````` example
  6352. ![Foo]
  6353. [foo]: /url "title"
  6354. .
  6355. <p><img src="/url" alt="Foo" title="title" /></p>
  6356. ````````````````````````````````
  6357. If you just want a literal `!` followed by bracketed text, you can
  6358. backslash-escape the opening `[`:
  6359. ```````````````````````````````` example
  6360. !\[foo]
  6361. [foo]: /url "title"
  6362. .
  6363. <p>![foo]</p>
  6364. ````````````````````````````````
  6365. If you want a link after a literal `!`, backslash-escape the
  6366. `!`:
  6367. ```````````````````````````````` example
  6368. \![foo]
  6369. [foo]: /url "title"
  6370. .
  6371. <p>!<a href="/url" title="title">foo</a></p>
  6372. ````````````````````````````````
  6373. ## Autolinks
  6374. [Autolink](@)s are absolute URIs and email addresses inside
  6375. `<` and `>`. They are parsed as links, with the URL or email address
  6376. as the link label.
  6377. A [URI autolink](@) consists of `<`, followed by an
  6378. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6379. a link to the URI, with the URI as the link's label.
  6380. An [absolute URI](@),
  6381. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6382. followed by zero or more characters other than ASCII
  6383. [whitespace] and control characters, `<`, and `>`. If
  6384. the URI includes these characters, they must be percent-encoded
  6385. (e.g. `%20` for a space).
  6386. For purposes of this spec, a [scheme](@) is any sequence
  6387. of 2--32 characters beginning with an ASCII letter and followed
  6388. by any combination of ASCII letters, digits, or the symbols plus
  6389. ("+"), period ("."), or hyphen ("-").
  6390. Here are some valid autolinks:
  6391. ```````````````````````````````` example
  6392. <http://foo.bar.baz>
  6393. .
  6394. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6395. ````````````````````````````````
  6396. ```````````````````````````````` example
  6397. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6398. .
  6399. <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>
  6400. ````````````````````````````````
  6401. ```````````````````````````````` example
  6402. <irc://foo.bar:2233/baz>
  6403. .
  6404. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6405. ````````````````````````````````
  6406. Uppercase is also fine:
  6407. ```````````````````````````````` example
  6408. <MAILTO:FOO@BAR.BAZ>
  6409. .
  6410. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6411. ````````````````````````````````
  6412. Note that many strings that count as [absolute URIs] for
  6413. purposes of this spec are not valid URIs, because their
  6414. schemes are not registered or because of other problems
  6415. with their syntax:
  6416. ```````````````````````````````` example
  6417. <a+b+c:d>
  6418. .
  6419. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6420. ````````````````````````````````
  6421. ```````````````````````````````` example
  6422. <made-up-scheme://foo,bar>
  6423. .
  6424. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6425. ````````````````````````````````
  6426. ```````````````````````````````` example
  6427. <http://../>
  6428. .
  6429. <p><a href="http://../">http://../</a></p>
  6430. ````````````````````````````````
  6431. ```````````````````````````````` example
  6432. <localhost:5001/foo>
  6433. .
  6434. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6435. ````````````````````````````````
  6436. Spaces are not allowed in autolinks:
  6437. ```````````````````````````````` example
  6438. <http://foo.bar/baz bim>
  6439. .
  6440. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6441. ````````````````````````````````
  6442. Backslash-escapes do not work inside autolinks:
  6443. ```````````````````````````````` example
  6444. <http://example.com/\[\>
  6445. .
  6446. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6447. ````````````````````````````````
  6448. An [email autolink](@)
  6449. consists of `<`, followed by an [email address],
  6450. followed by `>`. The link's label is the email address,
  6451. and the URL is `mailto:` followed by the email address.
  6452. An [email address](@),
  6453. for these purposes, is anything that matches
  6454. the [non-normative regex from the HTML5
  6455. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6456. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6457. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6458. Examples of email autolinks:
  6459. ```````````````````````````````` example
  6460. <foo@bar.example.com>
  6461. .
  6462. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6463. ````````````````````````````````
  6464. ```````````````````````````````` example
  6465. <foo+special@Bar.baz-bar0.com>
  6466. .
  6467. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6468. ````````````````````````````````
  6469. Backslash-escapes do not work inside email autolinks:
  6470. ```````````````````````````````` example
  6471. <foo\+@bar.example.com>
  6472. .
  6473. <p>&lt;foo+@bar.example.com&gt;</p>
  6474. ````````````````````````````````
  6475. These are not autolinks:
  6476. ```````````````````````````````` example
  6477. <>
  6478. .
  6479. <p>&lt;&gt;</p>
  6480. ````````````````````````````````
  6481. ```````````````````````````````` example
  6482. < http://foo.bar >
  6483. .
  6484. <p>&lt; http://foo.bar &gt;</p>
  6485. ````````````````````````````````
  6486. ```````````````````````````````` example
  6487. <m:abc>
  6488. .
  6489. <p>&lt;m:abc&gt;</p>
  6490. ````````````````````````````````
  6491. ```````````````````````````````` example
  6492. <foo.bar.baz>
  6493. .
  6494. <p>&lt;foo.bar.baz&gt;</p>
  6495. ````````````````````````````````
  6496. ```````````````````````````````` example
  6497. http://example.com
  6498. .
  6499. <p>http://example.com</p>
  6500. ````````````````````````````````
  6501. ```````````````````````````````` example
  6502. foo@bar.example.com
  6503. .
  6504. <p>foo@bar.example.com</p>
  6505. ````````````````````````````````
  6506. ## Raw HTML
  6507. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6508. raw HTML tag and will be rendered in HTML without escaping.
  6509. Tag and attribute names are not limited to current HTML tags,
  6510. so custom tags (and even, say, DocBook tags) may be used.
  6511. Here is the grammar for tags:
  6512. A [tag name](@) consists of an ASCII letter
  6513. followed by zero or more ASCII letters, digits, or
  6514. hyphens (`-`).
  6515. An [attribute](@) consists of [whitespace],
  6516. an [attribute name], and an optional
  6517. [attribute value specification].
  6518. An [attribute name](@)
  6519. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6520. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6521. specification restricted to ASCII. HTML5 is laxer.)
  6522. An [attribute value specification](@)
  6523. consists of optional [whitespace],
  6524. a `=` character, optional [whitespace], and an [attribute
  6525. value].
  6526. An [attribute value](@)
  6527. consists of an [unquoted attribute value],
  6528. a [single-quoted attribute value], or a [double-quoted attribute value].
  6529. An [unquoted attribute value](@)
  6530. is a nonempty string of characters not
  6531. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6532. A [single-quoted attribute value](@)
  6533. consists of `'`, zero or more
  6534. characters not including `'`, and a final `'`.
  6535. A [double-quoted attribute value](@)
  6536. consists of `"`, zero or more
  6537. characters not including `"`, and a final `"`.
  6538. An [open tag](@) consists of a `<` character, a [tag name],
  6539. zero or more [attributes], optional [whitespace], an optional `/`
  6540. character, and a `>` character.
  6541. A [closing tag](@) consists of the string `</`, a
  6542. [tag name], optional [whitespace], and the character `>`.
  6543. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6544. where *text* does not start with `>` or `->`, does not end with `-`,
  6545. and does not contain `--`. (See the
  6546. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6547. A [processing instruction](@)
  6548. consists of the string `<?`, a string
  6549. of characters not including the string `?>`, and the string
  6550. `?>`.
  6551. A [declaration](@) consists of the
  6552. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6553. [whitespace], a string of characters not including the
  6554. character `>`, and the character `>`.
  6555. A [CDATA section](@) consists of
  6556. the string `<![CDATA[`, a string of characters not including the string
  6557. `]]>`, and the string `]]>`.
  6558. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6559. an [HTML comment], a [processing instruction], a [declaration],
  6560. or a [CDATA section].
  6561. Here are some simple open tags:
  6562. ```````````````````````````````` example
  6563. <a><bab><c2c>
  6564. .
  6565. <p><a><bab><c2c></p>
  6566. ````````````````````````````````
  6567. Empty elements:
  6568. ```````````````````````````````` example
  6569. <a/><b2/>
  6570. .
  6571. <p><a/><b2/></p>
  6572. ````````````````````````````````
  6573. [Whitespace] is allowed:
  6574. ```````````````````````````````` example
  6575. <a /><b2
  6576. data="foo" >
  6577. .
  6578. <p><a /><b2
  6579. data="foo" ></p>
  6580. ````````````````````````````````
  6581. With attributes:
  6582. ```````````````````````````````` example
  6583. <a foo="bar" bam = 'baz <em>"</em>'
  6584. _boolean zoop:33=zoop:33 />
  6585. .
  6586. <p><a foo="bar" bam = 'baz <em>"</em>'
  6587. _boolean zoop:33=zoop:33 /></p>
  6588. ````````````````````````````````
  6589. Custom tag names can be used:
  6590. ```````````````````````````````` example
  6591. Foo <responsive-image src="foo.jpg" />
  6592. .
  6593. <p>Foo <responsive-image src="foo.jpg" /></p>
  6594. ````````````````````````````````
  6595. Illegal tag names, not parsed as HTML:
  6596. ```````````````````````````````` example
  6597. <33> <__>
  6598. .
  6599. <p>&lt;33&gt; &lt;__&gt;</p>
  6600. ````````````````````````````````
  6601. Illegal attribute names:
  6602. ```````````````````````````````` example
  6603. <a h*#ref="hi">
  6604. .
  6605. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6606. ````````````````````````````````
  6607. Illegal attribute values:
  6608. ```````````````````````````````` example
  6609. <a href="hi'> <a href=hi'>
  6610. .
  6611. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6612. ````````````````````````````````
  6613. Illegal [whitespace]:
  6614. ```````````````````````````````` example
  6615. < a><
  6616. foo><bar/ >
  6617. <foo bar=baz
  6618. bim!bop />
  6619. .
  6620. <p>&lt; a&gt;&lt;
  6621. foo&gt;&lt;bar/ &gt;
  6622. &lt;foo bar=baz
  6623. bim!bop /&gt;</p>
  6624. ````````````````````````````````
  6625. Missing [whitespace]:
  6626. ```````````````````````````````` example
  6627. <a href='bar'title=title>
  6628. .
  6629. <p>&lt;a href='bar'title=title&gt;</p>
  6630. ````````````````````````````````
  6631. Closing tags:
  6632. ```````````````````````````````` example
  6633. </a></foo >
  6634. .
  6635. <p></a></foo ></p>
  6636. ````````````````````````````````
  6637. Illegal attributes in closing tag:
  6638. ```````````````````````````````` example
  6639. </a href="foo">
  6640. .
  6641. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6642. ````````````````````````````````
  6643. Comments:
  6644. ```````````````````````````````` example
  6645. foo <!-- this is a
  6646. comment - with hyphen -->
  6647. .
  6648. <p>foo <!-- this is a
  6649. comment - with hyphen --></p>
  6650. ````````````````````````````````
  6651. ```````````````````````````````` example
  6652. foo <!-- not a comment -- two hyphens -->
  6653. .
  6654. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6655. ````````````````````````````````
  6656. Not comments:
  6657. ```````````````````````````````` example
  6658. foo <!--> foo -->
  6659. foo <!-- foo--->
  6660. .
  6661. <p>foo &lt;!--&gt; foo --&gt;</p>
  6662. <p>foo &lt;!-- foo---&gt;</p>
  6663. ````````````````````````````````
  6664. Processing instructions:
  6665. ```````````````````````````````` example
  6666. foo <?php echo $a; ?>
  6667. .
  6668. <p>foo <?php echo $a; ?></p>
  6669. ````````````````````````````````
  6670. Declarations:
  6671. ```````````````````````````````` example
  6672. foo <!ELEMENT br EMPTY>
  6673. .
  6674. <p>foo <!ELEMENT br EMPTY></p>
  6675. ````````````````````````````````
  6676. CDATA sections:
  6677. ```````````````````````````````` example
  6678. foo <![CDATA[>&<]]>
  6679. .
  6680. <p>foo <![CDATA[>&<]]></p>
  6681. ````````````````````````````````
  6682. Entity and numeric character references are preserved in HTML
  6683. attributes:
  6684. ```````````````````````````````` example
  6685. foo <a href="&ouml;">
  6686. .
  6687. <p>foo <a href="&ouml;"></p>
  6688. ````````````````````````````````
  6689. Backslash escapes do not work in HTML attributes:
  6690. ```````````````````````````````` example
  6691. foo <a href="\*">
  6692. .
  6693. <p>foo <a href="\*"></p>
  6694. ````````````````````````````````
  6695. ```````````````````````````````` example
  6696. <a href="\"">
  6697. .
  6698. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6699. ````````````````````````````````
  6700. ## Hard line breaks
  6701. A line break (not in a code span or HTML tag) that is preceded
  6702. by two or more spaces and does not occur at the end of a block
  6703. is parsed as a [hard line break](@) (rendered
  6704. in HTML as a `<br />` tag):
  6705. ```````````````````````````````` example
  6706. foo
  6707. baz
  6708. .
  6709. <p>foo<br />
  6710. baz</p>
  6711. ````````````````````````````````
  6712. For a more visible alternative, a backslash before the
  6713. [line ending] may be used instead of two spaces:
  6714. ```````````````````````````````` example
  6715. foo\
  6716. baz
  6717. .
  6718. <p>foo<br />
  6719. baz</p>
  6720. ````````````````````````````````
  6721. More than two spaces can be used:
  6722. ```````````````````````````````` example
  6723. foo
  6724. baz
  6725. .
  6726. <p>foo<br />
  6727. baz</p>
  6728. ````````````````````````````````
  6729. Leading spaces at the beginning of the next line are ignored:
  6730. ```````````````````````````````` example
  6731. foo
  6732. bar
  6733. .
  6734. <p>foo<br />
  6735. bar</p>
  6736. ````````````````````````````````
  6737. ```````````````````````````````` example
  6738. foo\
  6739. bar
  6740. .
  6741. <p>foo<br />
  6742. bar</p>
  6743. ````````````````````````````````
  6744. Line breaks can occur inside emphasis, links, and other constructs
  6745. that allow inline content:
  6746. ```````````````````````````````` example
  6747. *foo
  6748. bar*
  6749. .
  6750. <p><em>foo<br />
  6751. bar</em></p>
  6752. ````````````````````````````````
  6753. ```````````````````````````````` example
  6754. *foo\
  6755. bar*
  6756. .
  6757. <p><em>foo<br />
  6758. bar</em></p>
  6759. ````````````````````````````````
  6760. Line breaks do not occur inside code spans
  6761. ```````````````````````````````` example
  6762. `code
  6763. span`
  6764. .
  6765. <p><code>code span</code></p>
  6766. ````````````````````````````````
  6767. ```````````````````````````````` example
  6768. `code\
  6769. span`
  6770. .
  6771. <p><code>code\ span</code></p>
  6772. ````````````````````````````````
  6773. or HTML tags:
  6774. ```````````````````````````````` example
  6775. <a href="foo
  6776. bar">
  6777. .
  6778. <p><a href="foo
  6779. bar"></p>
  6780. ````````````````````````````````
  6781. ```````````````````````````````` example
  6782. <a href="foo\
  6783. bar">
  6784. .
  6785. <p><a href="foo\
  6786. bar"></p>
  6787. ````````````````````````````````
  6788. Hard line breaks are for separating inline content within a block.
  6789. Neither syntax for hard line breaks works at the end of a paragraph or
  6790. other block element:
  6791. ```````````````````````````````` example
  6792. foo\
  6793. .
  6794. <p>foo\</p>
  6795. ````````````````````````````````
  6796. ```````````````````````````````` example
  6797. foo
  6798. .
  6799. <p>foo</p>
  6800. ````````````````````````````````
  6801. ```````````````````````````````` example
  6802. ### foo\
  6803. .
  6804. <h3>foo\</h3>
  6805. ````````````````````````````````
  6806. ```````````````````````````````` example
  6807. ### foo
  6808. .
  6809. <h3>foo</h3>
  6810. ````````````````````````````````
  6811. ## Soft line breaks
  6812. A regular line break (not in a code span or HTML tag) that is not
  6813. preceded by two or more spaces or a backslash is parsed as a
  6814. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6815. [line ending] or as a space. The result will be the same in
  6816. browsers. In the examples here, a [line ending] will be used.)
  6817. ```````````````````````````````` example
  6818. foo
  6819. baz
  6820. .
  6821. <p>foo
  6822. baz</p>
  6823. ````````````````````````````````
  6824. Spaces at the end of the line and beginning of the next line are
  6825. removed:
  6826. ```````````````````````````````` example
  6827. foo
  6828. baz
  6829. .
  6830. <p>foo
  6831. baz</p>
  6832. ````````````````````````````````
  6833. A conforming parser may render a soft line break in HTML either as a
  6834. line break or as a space.
  6835. A renderer may also provide an option to render soft line breaks
  6836. as hard line breaks.
  6837. ## Textual content
  6838. Any characters not given an interpretation by the above rules will
  6839. be parsed as plain textual content.
  6840. ```````````````````````````````` example
  6841. hello $.;'there
  6842. .
  6843. <p>hello $.;'there</p>
  6844. ````````````````````````````````
  6845. ```````````````````````````````` example
  6846. Foo χρῆν
  6847. .
  6848. <p>Foo χρῆν</p>
  6849. ````````````````````````````````
  6850. Internal spaces are preserved verbatim:
  6851. ```````````````````````````````` example
  6852. Multiple spaces
  6853. .
  6854. <p>Multiple spaces</p>
  6855. ````````````````````````````````
  6856. <!-- END TESTS -->
  6857. # Appendix: A parsing strategy
  6858. In this appendix we describe some features of the parsing strategy
  6859. used in the CommonMark reference implementations.
  6860. ## Overview
  6861. Parsing has two phases:
  6862. 1. In the first phase, lines of input are consumed and the block
  6863. structure of the document---its division into paragraphs, block quotes,
  6864. list items, and so on---is constructed. Text is assigned to these
  6865. blocks but not parsed. Link reference definitions are parsed and a
  6866. map of links is constructed.
  6867. 2. In the second phase, the raw text contents of paragraphs and headings
  6868. are parsed into sequences of Markdown inline elements (strings,
  6869. code spans, links, emphasis, and so on), using the map of link
  6870. references constructed in phase 1.
  6871. At each point in processing, the document is represented as a tree of
  6872. **blocks**. The root of the tree is a `document` block. The `document`
  6873. may have any number of other blocks as **children**. These children
  6874. may, in turn, have other blocks as children. The last child of a block
  6875. is normally considered **open**, meaning that subsequent lines of input
  6876. can alter its contents. (Blocks that are not open are **closed**.)
  6877. Here, for example, is a possible document tree, with the open blocks
  6878. marked by arrows:
  6879. ``` tree
  6880. -> document
  6881. -> block_quote
  6882. paragraph
  6883. "Lorem ipsum dolor\nsit amet."
  6884. -> list (type=bullet tight=true bullet_char=-)
  6885. list_item
  6886. paragraph
  6887. "Qui *quodsi iracundia*"
  6888. -> list_item
  6889. -> paragraph
  6890. "aliquando id"
  6891. ```
  6892. ## Phase 1: block structure
  6893. Each line that is processed has an effect on this tree. The line is
  6894. analyzed and, depending on its contents, the document may be altered
  6895. in one or more of the following ways:
  6896. 1. One or more open blocks may be closed.
  6897. 2. One or more new blocks may be created as children of the
  6898. last open block.
  6899. 3. Text may be added to the last (deepest) open block remaining
  6900. on the tree.
  6901. Once a line has been incorporated into the tree in this way,
  6902. it can be discarded, so input can be read in a stream.
  6903. For each line, we follow this procedure:
  6904. 1. First we iterate through the open blocks, starting with the
  6905. root document, and descending through last children down to the last
  6906. open block. Each block imposes a condition that the line must satisfy
  6907. if the block is to remain open. For example, a block quote requires a
  6908. `>` character. A paragraph requires a non-blank line.
  6909. In this phase we may match all or just some of the open
  6910. blocks. But we cannot close unmatched blocks yet, because we may have a
  6911. [lazy continuation line].
  6912. 2. Next, after consuming the continuation markers for existing
  6913. blocks, we look for new block starts (e.g. `>` for a block quote).
  6914. If we encounter a new block start, we close any blocks unmatched
  6915. in step 1 before creating the new block as a child of the last
  6916. matched block.
  6917. 3. Finally, we look at the remainder of the line (after block
  6918. markers like `>`, list markers, and indentation have been consumed).
  6919. This is text that can be incorporated into the last open
  6920. block (a paragraph, code block, heading, or raw HTML).
  6921. Setext headings are formed when we see a line of a paragraph
  6922. that is a [setext heading underline].
  6923. Reference link definitions are detected when a paragraph is closed;
  6924. the accumulated text lines are parsed to see if they begin with
  6925. one or more reference link definitions. Any remainder becomes a
  6926. normal paragraph.
  6927. We can see how this works by considering how the tree above is
  6928. generated by four lines of Markdown:
  6929. ``` markdown
  6930. > Lorem ipsum dolor
  6931. sit amet.
  6932. > - Qui *quodsi iracundia*
  6933. > - aliquando id
  6934. ```
  6935. At the outset, our document model is just
  6936. ``` tree
  6937. -> document
  6938. ```
  6939. The first line of our text,
  6940. ``` markdown
  6941. > Lorem ipsum dolor
  6942. ```
  6943. causes a `block_quote` block to be created as a child of our
  6944. open `document` block, and a `paragraph` block as a child of
  6945. the `block_quote`. Then the text is added to the last open
  6946. block, the `paragraph`:
  6947. ``` tree
  6948. -> document
  6949. -> block_quote
  6950. -> paragraph
  6951. "Lorem ipsum dolor"
  6952. ```
  6953. The next line,
  6954. ``` markdown
  6955. sit amet.
  6956. ```
  6957. is a "lazy continuation" of the open `paragraph`, so it gets added
  6958. to the paragraph's text:
  6959. ``` tree
  6960. -> document
  6961. -> block_quote
  6962. -> paragraph
  6963. "Lorem ipsum dolor\nsit amet."
  6964. ```
  6965. The third line,
  6966. ``` markdown
  6967. > - Qui *quodsi iracundia*
  6968. ```
  6969. causes the `paragraph` block to be closed, and a new `list` block
  6970. opened as a child of the `block_quote`. A `list_item` is also
  6971. added as a child of the `list`, and a `paragraph` as a child of
  6972. the `list_item`. The text is then added to the new `paragraph`:
  6973. ``` tree
  6974. -> document
  6975. -> block_quote
  6976. paragraph
  6977. "Lorem ipsum dolor\nsit amet."
  6978. -> list (type=bullet tight=true bullet_char=-)
  6979. -> list_item
  6980. -> paragraph
  6981. "Qui *quodsi iracundia*"
  6982. ```
  6983. The fourth line,
  6984. ``` markdown
  6985. > - aliquando id
  6986. ```
  6987. causes the `list_item` (and its child the `paragraph`) to be closed,
  6988. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6989. is added as a child of the new `list_item`, to contain the text.
  6990. We thus obtain the final tree:
  6991. ``` tree
  6992. -> document
  6993. -> block_quote
  6994. paragraph
  6995. "Lorem ipsum dolor\nsit amet."
  6996. -> list (type=bullet tight=true bullet_char=-)
  6997. list_item
  6998. paragraph
  6999. "Qui *quodsi iracundia*"
  7000. -> list_item
  7001. -> paragraph
  7002. "aliquando id"
  7003. ```
  7004. ## Phase 2: inline structure
  7005. Once all of the input has been parsed, all open blocks are closed.
  7006. We then "walk the tree," visiting every node, and parse raw
  7007. string contents of paragraphs and headings as inlines. At this
  7008. point we have seen all the link reference definitions, so we can
  7009. resolve reference links as we go.
  7010. ``` tree
  7011. document
  7012. block_quote
  7013. paragraph
  7014. str "Lorem ipsum dolor"
  7015. softbreak
  7016. str "sit amet."
  7017. list (type=bullet tight=true bullet_char=-)
  7018. list_item
  7019. paragraph
  7020. str "Qui "
  7021. emph
  7022. str "quodsi iracundia"
  7023. list_item
  7024. paragraph
  7025. str "aliquando id"
  7026. ```
  7027. Notice how the [line ending] in the first paragraph has
  7028. been parsed as a `softbreak`, and the asterisks in the first list item
  7029. have become an `emph`.
  7030. ### An algorithm for parsing nested emphasis and links
  7031. By far the trickiest part of inline parsing is handling emphasis,
  7032. strong emphasis, links, and images. This is done using the following
  7033. algorithm.
  7034. When we're parsing inlines and we hit either
  7035. - a run of `*` or `_` characters, or
  7036. - a `[` or `![`
  7037. we insert a text node with these symbols as its literal content, and we
  7038. add a pointer to this text node to the [delimiter stack](@).
  7039. The [delimiter stack] is a doubly linked list. Each
  7040. element contains a pointer to a text node, plus information about
  7041. - the type of delimiter (`[`, `![`, `*`, `_`)
  7042. - the number of delimiters,
  7043. - whether the delimiter is "active" (all are active to start), and
  7044. - whether the delimiter is a potential opener, a potential closer,
  7045. or both (which depends on what sort of characters precede
  7046. and follow the delimiters).
  7047. When we hit a `]` character, we call the *look for link or image*
  7048. procedure (see below).
  7049. When we hit the end of the input, we call the *process emphasis*
  7050. procedure (see below), with `stack_bottom` = NULL.
  7051. #### *look for link or image*
  7052. Starting at the top of the delimiter stack, we look backwards
  7053. through the stack for an opening `[` or `![` delimiter.
  7054. - If we don't find one, we return a literal text node `]`.
  7055. - If we do find one, but it's not *active*, we remove the inactive
  7056. delimiter from the stack, and return a literal text node `]`.
  7057. - If we find one and it's active, then we parse ahead to see if
  7058. we have an inline link/image, reference link/image, compact reference
  7059. link/image, or shortcut reference link/image.
  7060. + If we don't, then we remove the opening delimiter from the
  7061. delimiter stack and return a literal text node `]`.
  7062. + If we do, then
  7063. * We return a link or image node whose children are the inlines
  7064. after the text node pointed to by the opening delimiter.
  7065. * We run *process emphasis* on these inlines, with the `[` opener
  7066. as `stack_bottom`.
  7067. * We remove the opening delimiter.
  7068. * If we have a link (and not an image), we also set all
  7069. `[` delimiters before the opening delimiter to *inactive*. (This
  7070. will prevent us from getting links within links.)
  7071. #### *process emphasis*
  7072. Parameter `stack_bottom` sets a lower bound to how far we
  7073. descend in the [delimiter stack]. If it is NULL, we can
  7074. go all the way to the bottom. Otherwise, we stop before
  7075. visiting `stack_bottom`.
  7076. Let `current_position` point to the element on the [delimiter stack]
  7077. just above `stack_bottom` (or the first element if `stack_bottom`
  7078. is NULL).
  7079. We keep track of the `openers_bottom` for each delimiter
  7080. type (`*`, `_`). Initialize this to `stack_bottom`.
  7081. Then we repeat the following until we run out of potential
  7082. closers:
  7083. - Move `current_position` forward in the delimiter stack (if needed)
  7084. until we find the first potential closer with delimiter `*` or `_`.
  7085. (This will be the potential closer closest
  7086. to the beginning of the input -- the first one in parse order.)
  7087. - Now, look back in the stack (staying above `stack_bottom` and
  7088. the `openers_bottom` for this delimiter type) for the
  7089. first matching potential opener ("matching" means same delimiter).
  7090. - If one is found:
  7091. + Figure out whether we have emphasis or strong emphasis:
  7092. if both closer and opener spans have length >= 2, we have
  7093. strong, otherwise regular.
  7094. + Insert an emph or strong emph node accordingly, after
  7095. the text node corresponding to the opener.
  7096. + Remove any delimiters between the opener and closer from
  7097. the delimiter stack.
  7098. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7099. from the opening and closing text nodes. If they become empty
  7100. as a result, remove them and remove the corresponding element
  7101. of the delimiter stack. If the closing node is removed, reset
  7102. `current_position` to the next element in the stack.
  7103. - If none in found:
  7104. + Set `openers_bottom` to the element before `current_position`.
  7105. (We know that there are no openers for this kind of closer up to and
  7106. including this point, so this puts a lower bound on future searches.)
  7107. + If the closer at `current_position` is not a potential opener,
  7108. remove it from the delimiter stack (since we know it can't
  7109. be a closer either).
  7110. + Advance `current_position` to the next element in the stack.
  7111. After we're done, we remove all delimiters above `stack_bottom` from the
  7112. delimiter stack.