aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 5e782f35b349549054034c6b250508ed71e228e7 (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%20url>
  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--8 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; &#98765432; &#0;
  4341. .
  4342. <p># Ӓ Ϡ � �</p>
  4343. ````````````````````````````````
  4344. [Hexadecimal numeric character
  4345. references](@) consist of `&#` +
  4346. either `X` or `x` + a string of 1-8 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. &ThisIsNotDefined; &hi?;
  4358. .
  4359. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4360. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4361. ````````````````````````````````
  4362. Although HTML5 does accept some entity references
  4363. without a trailing semicolon (such as `&copy`), these are not
  4364. recognized here, because it makes the grammar too ambiguous:
  4365. ```````````````````````````````` example
  4366. &copy
  4367. .
  4368. <p>&amp;copy</p>
  4369. ````````````````````````````````
  4370. Strings that are not on the list of HTML5 named entities are not
  4371. recognized as entity references either:
  4372. ```````````````````````````````` example
  4373. &MadeUpEntity;
  4374. .
  4375. <p>&amp;MadeUpEntity;</p>
  4376. ````````````````````````````````
  4377. Entity and numeric character references are recognized in any
  4378. context besides code spans or code blocks, including
  4379. URLs, [link titles], and [fenced code block][] [info strings]:
  4380. ```````````````````````````````` example
  4381. <a href="&ouml;&ouml;.html">
  4382. .
  4383. <a href="&ouml;&ouml;.html">
  4384. ````````````````````````````````
  4385. ```````````````````````````````` example
  4386. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4387. .
  4388. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4389. ````````````````````````````````
  4390. ```````````````````````````````` example
  4391. [foo]
  4392. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4393. .
  4394. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4395. ````````````````````````````````
  4396. ```````````````````````````````` example
  4397. ``` f&ouml;&ouml;
  4398. foo
  4399. ```
  4400. .
  4401. <pre><code class="language-föö">foo
  4402. </code></pre>
  4403. ````````````````````````````````
  4404. Entity and numeric character references are treated as literal
  4405. text in code spans and code blocks:
  4406. ```````````````````````````````` example
  4407. `f&ouml;&ouml;`
  4408. .
  4409. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4410. ````````````````````````````````
  4411. ```````````````````````````````` example
  4412. f&ouml;f&ouml;
  4413. .
  4414. <pre><code>f&amp;ouml;f&amp;ouml;
  4415. </code></pre>
  4416. ````````````````````````````````
  4417. ## Code spans
  4418. A [backtick string](@)
  4419. is a string of one or more backtick characters (`` ` ``) that is neither
  4420. preceded nor followed by a backtick.
  4421. A [code span](@) begins with a backtick string and ends with
  4422. a backtick string of equal length. The contents of the code span are
  4423. the characters between the two backtick strings, with leading and
  4424. trailing spaces and [line endings] removed, and
  4425. [whitespace] collapsed to single spaces.
  4426. This is a simple code span:
  4427. ```````````````````````````````` example
  4428. `foo`
  4429. .
  4430. <p><code>foo</code></p>
  4431. ````````````````````````````````
  4432. Here two backticks are used, because the code contains a backtick.
  4433. This example also illustrates stripping of leading and trailing spaces:
  4434. ```````````````````````````````` example
  4435. `` foo ` bar ``
  4436. .
  4437. <p><code>foo ` bar</code></p>
  4438. ````````````````````````````````
  4439. This example shows the motivation for stripping leading and trailing
  4440. spaces:
  4441. ```````````````````````````````` example
  4442. ` `` `
  4443. .
  4444. <p><code>``</code></p>
  4445. ````````````````````````````````
  4446. [Line endings] are treated like spaces:
  4447. ```````````````````````````````` example
  4448. ``
  4449. foo
  4450. ``
  4451. .
  4452. <p><code>foo</code></p>
  4453. ````````````````````````````````
  4454. Interior spaces and [line endings] are collapsed into
  4455. single spaces, just as they would be by a browser:
  4456. ```````````````````````````````` example
  4457. `foo bar
  4458. baz`
  4459. .
  4460. <p><code>foo bar baz</code></p>
  4461. ````````````````````````````````
  4462. Not all [Unicode whitespace] (for instance, non-breaking space) is
  4463. collapsed, however:
  4464. ```````````````````````````````` example
  4465. `a  b`
  4466. .
  4467. <p><code>a  b</code></p>
  4468. ````````````````````````````````
  4469. Q: Why not just leave the spaces, since browsers will collapse them
  4470. anyway? A: Because we might be targeting a non-HTML format, and we
  4471. shouldn't rely on HTML-specific rendering assumptions.
  4472. (Existing implementations differ in their treatment of internal
  4473. spaces and [line endings]. Some, including `Markdown.pl` and
  4474. `showdown`, convert an internal [line ending] into a
  4475. `<br />` tag. But this makes things difficult for those who like to
  4476. hard-wrap their paragraphs, since a line break in the midst of a code
  4477. span will cause an unintended line break in the output. Others just
  4478. leave internal spaces as they are, which is fine if only HTML is being
  4479. targeted.)
  4480. ```````````````````````````````` example
  4481. `foo `` bar`
  4482. .
  4483. <p><code>foo `` bar</code></p>
  4484. ````````````````````````````````
  4485. Note that backslash escapes do not work in code spans. All backslashes
  4486. are treated literally:
  4487. ```````````````````````````````` example
  4488. `foo\`bar`
  4489. .
  4490. <p><code>foo\</code>bar`</p>
  4491. ````````````````````````````````
  4492. Backslash escapes are never needed, because one can always choose a
  4493. string of *n* backtick characters as delimiters, where the code does
  4494. not contain any strings of exactly *n* backtick characters.
  4495. Code span backticks have higher precedence than any other inline
  4496. constructs except HTML tags and autolinks. Thus, for example, this is
  4497. not parsed as emphasized text, since the second `*` is part of a code
  4498. span:
  4499. ```````````````````````````````` example
  4500. *foo`*`
  4501. .
  4502. <p>*foo<code>*</code></p>
  4503. ````````````````````````````````
  4504. And this is not parsed as a link:
  4505. ```````````````````````````````` example
  4506. [not a `link](/foo`)
  4507. .
  4508. <p>[not a <code>link](/foo</code>)</p>
  4509. ````````````````````````````````
  4510. Code spans, HTML tags, and autolinks have the same precedence.
  4511. Thus, this is code:
  4512. ```````````````````````````````` example
  4513. `<a href="`">`
  4514. .
  4515. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4516. ````````````````````````````````
  4517. But this is an HTML tag:
  4518. ```````````````````````````````` example
  4519. <a href="`">`
  4520. .
  4521. <p><a href="`">`</p>
  4522. ````````````````````````````````
  4523. And this is code:
  4524. ```````````````````````````````` example
  4525. `<http://foo.bar.`baz>`
  4526. .
  4527. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4528. ````````````````````````````````
  4529. But this is an autolink:
  4530. ```````````````````````````````` example
  4531. <http://foo.bar.`baz>`
  4532. .
  4533. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4534. ````````````````````````````````
  4535. When a backtick string is not closed by a matching backtick string,
  4536. we just have literal backticks:
  4537. ```````````````````````````````` example
  4538. ```foo``
  4539. .
  4540. <p>```foo``</p>
  4541. ````````````````````````````````
  4542. ```````````````````````````````` example
  4543. `foo
  4544. .
  4545. <p>`foo</p>
  4546. ````````````````````````````````
  4547. The following case also illustrates the need for opening and
  4548. closing backtick strings to be equal in length:
  4549. ```````````````````````````````` example
  4550. `foo``bar``
  4551. .
  4552. <p>`foo<code>bar</code></p>
  4553. ````````````````````````````````
  4554. ## Emphasis and strong emphasis
  4555. John Gruber's original [Markdown syntax
  4556. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4557. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4558. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4559. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4560. > tag.
  4561. This is enough for most users, but these rules leave much undecided,
  4562. especially when it comes to nested emphasis. The original
  4563. `Markdown.pl` test suite makes it clear that triple `***` and
  4564. `___` delimiters can be used for strong emphasis, and most
  4565. implementations have also allowed the following patterns:
  4566. ``` markdown
  4567. ***strong emph***
  4568. ***strong** in emph*
  4569. ***emph* in strong**
  4570. **in strong *emph***
  4571. *in emph **strong***
  4572. ```
  4573. The following patterns are less widely supported, but the intent
  4574. is clear and they are useful (especially in contexts like bibliography
  4575. entries):
  4576. ``` markdown
  4577. *emph *with emph* in it*
  4578. **strong **with strong** in it**
  4579. ```
  4580. Many implementations have also restricted intraword emphasis to
  4581. the `*` forms, to avoid unwanted emphasis in words containing
  4582. internal underscores. (It is best practice to put these in code
  4583. spans, but users often do not.)
  4584. ``` markdown
  4585. internal emphasis: foo*bar*baz
  4586. no emphasis: foo_bar_baz
  4587. ```
  4588. The rules given below capture all of these patterns, while allowing
  4589. for efficient parsing strategies that do not backtrack.
  4590. First, some definitions. A [delimiter run](@) is either
  4591. a sequence of one or more `*` characters that is not preceded or
  4592. followed by a non-backslash-escaped `*` character, or a sequence
  4593. of one or more `_` characters that is not preceded or followed by
  4594. a non-backslash-escaped `_` character.
  4595. A [left-flanking delimiter run](@) is
  4596. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4597. and (b) not followed by a [punctuation character], or
  4598. preceded by [Unicode whitespace] or a [punctuation character].
  4599. For purposes of this definition, the beginning and the end of
  4600. the line count as Unicode whitespace.
  4601. A [right-flanking delimiter run](@) is
  4602. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4603. and (b) not preceded by a [punctuation character], or
  4604. followed by [Unicode whitespace] or a [punctuation character].
  4605. For purposes of this definition, the beginning and the end of
  4606. the line count as Unicode whitespace.
  4607. Here are some examples of delimiter runs.
  4608. - left-flanking but not right-flanking:
  4609. ```
  4610. ***abc
  4611. _abc
  4612. **"abc"
  4613. _"abc"
  4614. ```
  4615. - right-flanking but not left-flanking:
  4616. ```
  4617. abc***
  4618. abc_
  4619. "abc"**
  4620. "abc"_
  4621. ```
  4622. - Both left and right-flanking:
  4623. ```
  4624. abc***def
  4625. "abc"_"def"
  4626. ```
  4627. - Neither left nor right-flanking:
  4628. ```
  4629. abc *** def
  4630. a _ b
  4631. ```
  4632. (The idea of distinguishing left-flanking and right-flanking
  4633. delimiter runs based on the character before and the character
  4634. after comes from Roopesh Chander's
  4635. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4636. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4637. run," and its rules for distinguishing left- and right-flanking runs
  4638. are a bit more complex than the ones given here.)
  4639. The following rules define emphasis and strong emphasis:
  4640. 1. A single `*` character [can open emphasis](@)
  4641. iff (if and only if) it is part of a [left-flanking delimiter run].
  4642. 2. A single `_` character [can open emphasis] iff
  4643. it is part of a [left-flanking delimiter run]
  4644. and either (a) not part of a [right-flanking delimiter run]
  4645. or (b) part of a [right-flanking delimiter run]
  4646. preceded by punctuation.
  4647. 3. A single `*` character [can close emphasis](@)
  4648. iff it is part of a [right-flanking delimiter run].
  4649. 4. A single `_` character [can close emphasis] iff
  4650. it is part of a [right-flanking delimiter run]
  4651. and either (a) not part of a [left-flanking delimiter run]
  4652. or (b) part of a [left-flanking delimiter run]
  4653. followed by punctuation.
  4654. 5. A double `**` [can open strong emphasis](@)
  4655. iff it is part of a [left-flanking delimiter run].
  4656. 6. A double `__` [can open strong emphasis] iff
  4657. it is part of a [left-flanking delimiter run]
  4658. and either (a) not part of a [right-flanking delimiter run]
  4659. or (b) part of a [right-flanking delimiter run]
  4660. preceded by punctuation.
  4661. 7. A double `**` [can close strong emphasis](@)
  4662. iff it is part of a [right-flanking delimiter run].
  4663. 8. A double `__` [can close strong emphasis] iff
  4664. it is part of a [right-flanking delimiter run]
  4665. and either (a) not part of a [left-flanking delimiter run]
  4666. or (b) part of a [left-flanking delimiter run]
  4667. followed by punctuation.
  4668. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4669. with a delimiter that [can close emphasis], and that uses the same
  4670. character (`_` or `*`) as the opening delimiter. The
  4671. opening and closing delimiters must belong to separate
  4672. [delimiter runs]. If one of the delimiters can both
  4673. open and close emphasis, then the sum of the lengths of the
  4674. delimiter runs containing the opening and closing delimiters
  4675. must not be a multiple of 3.
  4676. 10. Strong emphasis begins with a delimiter that
  4677. [can open strong emphasis] and ends with a delimiter that
  4678. [can close strong emphasis], and that uses the same character
  4679. (`_` or `*`) as the opening delimiter. The
  4680. opening and closing delimiters must belong to separate
  4681. [delimiter runs]. If one of the delimiters can both open
  4682. and close strong emphasis, then the sum of the lengths of
  4683. the delimiter runs containing the opening and closing
  4684. delimiters must not be a multiple of 3.
  4685. 11. A literal `*` character cannot occur at the beginning or end of
  4686. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4687. is backslash-escaped.
  4688. 12. A literal `_` character cannot occur at the beginning or end of
  4689. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4690. is backslash-escaped.
  4691. Where rules 1--12 above are compatible with multiple parsings,
  4692. the following principles resolve ambiguity:
  4693. 13. The number of nestings should be minimized. Thus, for example,
  4694. an interpretation `<strong>...</strong>` is always preferred to
  4695. `<em><em>...</em></em>`.
  4696. 14. An interpretation `<em><strong>...</strong></em>` is always
  4697. preferred to `<strong><em>...</em></strong>`.
  4698. 15. When two potential emphasis or strong emphasis spans overlap,
  4699. so that the second begins before the first ends and ends after
  4700. the first ends, the first takes precedence. Thus, for example,
  4701. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4702. than `*foo <em>bar* baz</em>`.
  4703. 16. When there are two potential emphasis or strong emphasis spans
  4704. with the same closing delimiter, the shorter one (the one that
  4705. opens later) takes precedence. Thus, for example,
  4706. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4707. rather than `<strong>foo **bar baz</strong>`.
  4708. 17. Inline code spans, links, images, and HTML tags group more tightly
  4709. than emphasis. So, when there is a choice between an interpretation
  4710. that contains one of these elements and one that does not, the
  4711. former always wins. Thus, for example, `*[foo*](bar)` is
  4712. parsed as `*<a href="bar">foo*</a>` rather than as
  4713. `<em>[foo</em>](bar)`.
  4714. These rules can be illustrated through a series of examples.
  4715. Rule 1:
  4716. ```````````````````````````````` example
  4717. *foo bar*
  4718. .
  4719. <p><em>foo bar</em></p>
  4720. ````````````````````````````````
  4721. This is not emphasis, because the opening `*` is followed by
  4722. whitespace, and hence not part of a [left-flanking delimiter run]:
  4723. ```````````````````````````````` example
  4724. a * foo bar*
  4725. .
  4726. <p>a * foo bar*</p>
  4727. ````````````````````````````````
  4728. This is not emphasis, because the opening `*` is preceded
  4729. by an alphanumeric and followed by punctuation, and hence
  4730. not part of a [left-flanking delimiter run]:
  4731. ```````````````````````````````` example
  4732. a*"foo"*
  4733. .
  4734. <p>a*&quot;foo&quot;*</p>
  4735. ````````````````````````````````
  4736. Unicode nonbreaking spaces count as whitespace, too:
  4737. ```````````````````````````````` example
  4738. * a *
  4739. .
  4740. <p>* a *</p>
  4741. ````````````````````````````````
  4742. Intraword emphasis with `*` is permitted:
  4743. ```````````````````````````````` example
  4744. foo*bar*
  4745. .
  4746. <p>foo<em>bar</em></p>
  4747. ````````````````````````````````
  4748. ```````````````````````````````` example
  4749. 5*6*78
  4750. .
  4751. <p>5<em>6</em>78</p>
  4752. ````````````````````````````````
  4753. Rule 2:
  4754. ```````````````````````````````` example
  4755. _foo bar_
  4756. .
  4757. <p><em>foo bar</em></p>
  4758. ````````````````````````````````
  4759. This is not emphasis, because the opening `_` is followed by
  4760. whitespace:
  4761. ```````````````````````````````` example
  4762. _ foo bar_
  4763. .
  4764. <p>_ foo bar_</p>
  4765. ````````````````````````````````
  4766. This is not emphasis, because the opening `_` is preceded
  4767. by an alphanumeric and followed by punctuation:
  4768. ```````````````````````````````` example
  4769. a_"foo"_
  4770. .
  4771. <p>a_&quot;foo&quot;_</p>
  4772. ````````````````````````````````
  4773. Emphasis with `_` is not allowed inside words:
  4774. ```````````````````````````````` example
  4775. foo_bar_
  4776. .
  4777. <p>foo_bar_</p>
  4778. ````````````````````````````````
  4779. ```````````````````````````````` example
  4780. 5_6_78
  4781. .
  4782. <p>5_6_78</p>
  4783. ````````````````````````````````
  4784. ```````````````````````````````` example
  4785. пристаням_стремятся_
  4786. .
  4787. <p>пристаням_стремятся_</p>
  4788. ````````````````````````````````
  4789. Here `_` does not generate emphasis, because the first delimiter run
  4790. is right-flanking and the second left-flanking:
  4791. ```````````````````````````````` example
  4792. aa_"bb"_cc
  4793. .
  4794. <p>aa_&quot;bb&quot;_cc</p>
  4795. ````````````````````````````````
  4796. This is emphasis, even though the opening delimiter is
  4797. both left- and right-flanking, because it is preceded by
  4798. punctuation:
  4799. ```````````````````````````````` example
  4800. foo-_(bar)_
  4801. .
  4802. <p>foo-<em>(bar)</em></p>
  4803. ````````````````````````````````
  4804. Rule 3:
  4805. This is not emphasis, because the closing delimiter does
  4806. not match the opening delimiter:
  4807. ```````````````````````````````` example
  4808. _foo*
  4809. .
  4810. <p>_foo*</p>
  4811. ````````````````````````````````
  4812. This is not emphasis, because the closing `*` is preceded by
  4813. whitespace:
  4814. ```````````````````````````````` example
  4815. *foo bar *
  4816. .
  4817. <p>*foo bar *</p>
  4818. ````````````````````````````````
  4819. A newline also counts as whitespace:
  4820. ```````````````````````````````` example
  4821. *foo bar
  4822. *
  4823. .
  4824. <p>*foo bar
  4825. *</p>
  4826. ````````````````````````````````
  4827. This is not emphasis, because the second `*` is
  4828. preceded by punctuation and followed by an alphanumeric
  4829. (hence it is not part of a [right-flanking delimiter run]:
  4830. ```````````````````````````````` example
  4831. *(*foo)
  4832. .
  4833. <p>*(*foo)</p>
  4834. ````````````````````````````````
  4835. The point of this restriction is more easily appreciated
  4836. with this example:
  4837. ```````````````````````````````` example
  4838. *(*foo*)*
  4839. .
  4840. <p><em>(<em>foo</em>)</em></p>
  4841. ````````````````````````````````
  4842. Intraword emphasis with `*` is allowed:
  4843. ```````````````````````````````` example
  4844. *foo*bar
  4845. .
  4846. <p><em>foo</em>bar</p>
  4847. ````````````````````````````````
  4848. Rule 4:
  4849. This is not emphasis, because the closing `_` is preceded by
  4850. whitespace:
  4851. ```````````````````````````````` example
  4852. _foo bar _
  4853. .
  4854. <p>_foo bar _</p>
  4855. ````````````````````````````````
  4856. This is not emphasis, because the second `_` is
  4857. preceded by punctuation and followed by an alphanumeric:
  4858. ```````````````````````````````` example
  4859. _(_foo)
  4860. .
  4861. <p>_(_foo)</p>
  4862. ````````````````````````````````
  4863. This is emphasis within emphasis:
  4864. ```````````````````````````````` example
  4865. _(_foo_)_
  4866. .
  4867. <p><em>(<em>foo</em>)</em></p>
  4868. ````````````````````````````````
  4869. Intraword emphasis is disallowed for `_`:
  4870. ```````````````````````````````` example
  4871. _foo_bar
  4872. .
  4873. <p>_foo_bar</p>
  4874. ````````````````````````````````
  4875. ```````````````````````````````` example
  4876. _пристаням_стремятся
  4877. .
  4878. <p>_пристаням_стремятся</p>
  4879. ````````````````````````````````
  4880. ```````````````````````````````` example
  4881. _foo_bar_baz_
  4882. .
  4883. <p><em>foo_bar_baz</em></p>
  4884. ````````````````````````````````
  4885. This is emphasis, even though the closing delimiter is
  4886. both left- and right-flanking, because it is followed by
  4887. punctuation:
  4888. ```````````````````````````````` example
  4889. _(bar)_.
  4890. .
  4891. <p><em>(bar)</em>.</p>
  4892. ````````````````````````````````
  4893. Rule 5:
  4894. ```````````````````````````````` example
  4895. **foo bar**
  4896. .
  4897. <p><strong>foo bar</strong></p>
  4898. ````````````````````````````````
  4899. This is not strong emphasis, because the opening delimiter is
  4900. followed by whitespace:
  4901. ```````````````````````````````` example
  4902. ** foo bar**
  4903. .
  4904. <p>** foo bar**</p>
  4905. ````````````````````````````````
  4906. This is not strong emphasis, because the opening `**` is preceded
  4907. by an alphanumeric and followed by punctuation, and hence
  4908. not part of a [left-flanking delimiter run]:
  4909. ```````````````````````````````` example
  4910. a**"foo"**
  4911. .
  4912. <p>a**&quot;foo&quot;**</p>
  4913. ````````````````````````````````
  4914. Intraword strong emphasis with `**` is permitted:
  4915. ```````````````````````````````` example
  4916. foo**bar**
  4917. .
  4918. <p>foo<strong>bar</strong></p>
  4919. ````````````````````````````````
  4920. Rule 6:
  4921. ```````````````````````````````` example
  4922. __foo bar__
  4923. .
  4924. <p><strong>foo bar</strong></p>
  4925. ````````````````````````````````
  4926. This is not strong emphasis, because the opening delimiter is
  4927. followed by whitespace:
  4928. ```````````````````````````````` example
  4929. __ foo bar__
  4930. .
  4931. <p>__ foo bar__</p>
  4932. ````````````````````````````````
  4933. A newline counts as whitespace:
  4934. ```````````````````````````````` example
  4935. __
  4936. foo bar__
  4937. .
  4938. <p>__
  4939. foo bar__</p>
  4940. ````````````````````````````````
  4941. This is not strong emphasis, because the opening `__` is preceded
  4942. by an alphanumeric and followed by punctuation:
  4943. ```````````````````````````````` example
  4944. a__"foo"__
  4945. .
  4946. <p>a__&quot;foo&quot;__</p>
  4947. ````````````````````````````````
  4948. Intraword strong emphasis is forbidden with `__`:
  4949. ```````````````````````````````` example
  4950. foo__bar__
  4951. .
  4952. <p>foo__bar__</p>
  4953. ````````````````````````````````
  4954. ```````````````````````````````` example
  4955. 5__6__78
  4956. .
  4957. <p>5__6__78</p>
  4958. ````````````````````````````````
  4959. ```````````````````````````````` example
  4960. пристаням__стремятся__
  4961. .
  4962. <p>пристаням__стремятся__</p>
  4963. ````````````````````````````````
  4964. ```````````````````````````````` example
  4965. __foo, __bar__, baz__
  4966. .
  4967. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4968. ````````````````````````````````
  4969. This is strong emphasis, even though the opening delimiter is
  4970. both left- and right-flanking, because it is preceded by
  4971. punctuation:
  4972. ```````````````````````````````` example
  4973. foo-__(bar)__
  4974. .
  4975. <p>foo-<strong>(bar)</strong></p>
  4976. ````````````````````````````````
  4977. Rule 7:
  4978. This is not strong emphasis, because the closing delimiter is preceded
  4979. by whitespace:
  4980. ```````````````````````````````` example
  4981. **foo bar **
  4982. .
  4983. <p>**foo bar **</p>
  4984. ````````````````````````````````
  4985. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4986. Rule 11.)
  4987. This is not strong emphasis, because the second `**` is
  4988. preceded by punctuation and followed by an alphanumeric:
  4989. ```````````````````````````````` example
  4990. **(**foo)
  4991. .
  4992. <p>**(**foo)</p>
  4993. ````````````````````````````````
  4994. The point of this restriction is more easily appreciated
  4995. with these examples:
  4996. ```````````````````````````````` example
  4997. *(**foo**)*
  4998. .
  4999. <p><em>(<strong>foo</strong>)</em></p>
  5000. ````````````````````````````````
  5001. ```````````````````````````````` example
  5002. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5003. *Asclepias physocarpa*)**
  5004. .
  5005. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5006. <em>Asclepias physocarpa</em>)</strong></p>
  5007. ````````````````````````````````
  5008. ```````````````````````````````` example
  5009. **foo "*bar*" foo**
  5010. .
  5011. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5012. ````````````````````````````````
  5013. Intraword emphasis:
  5014. ```````````````````````````````` example
  5015. **foo**bar
  5016. .
  5017. <p><strong>foo</strong>bar</p>
  5018. ````````````````````````````````
  5019. Rule 8:
  5020. This is not strong emphasis, because the closing delimiter is
  5021. preceded by whitespace:
  5022. ```````````````````````````````` example
  5023. __foo bar __
  5024. .
  5025. <p>__foo bar __</p>
  5026. ````````````````````````````````
  5027. This is not strong emphasis, because the second `__` is
  5028. preceded by punctuation and followed by an alphanumeric:
  5029. ```````````````````````````````` example
  5030. __(__foo)
  5031. .
  5032. <p>__(__foo)</p>
  5033. ````````````````````````````````
  5034. The point of this restriction is more easily appreciated
  5035. with this example:
  5036. ```````````````````````````````` example
  5037. _(__foo__)_
  5038. .
  5039. <p><em>(<strong>foo</strong>)</em></p>
  5040. ````````````````````````````````
  5041. Intraword strong emphasis is forbidden with `__`:
  5042. ```````````````````````````````` example
  5043. __foo__bar
  5044. .
  5045. <p>__foo__bar</p>
  5046. ````````````````````````````````
  5047. ```````````````````````````````` example
  5048. __пристаням__стремятся
  5049. .
  5050. <p>__пристаням__стремятся</p>
  5051. ````````````````````````````````
  5052. ```````````````````````````````` example
  5053. __foo__bar__baz__
  5054. .
  5055. <p><strong>foo__bar__baz</strong></p>
  5056. ````````````````````````````````
  5057. This is strong emphasis, even though the closing delimiter is
  5058. both left- and right-flanking, because it is followed by
  5059. punctuation:
  5060. ```````````````````````````````` example
  5061. __(bar)__.
  5062. .
  5063. <p><strong>(bar)</strong>.</p>
  5064. ````````````````````````````````
  5065. Rule 9:
  5066. Any nonempty sequence of inline elements can be the contents of an
  5067. emphasized span.
  5068. ```````````````````````````````` example
  5069. *foo [bar](/url)*
  5070. .
  5071. <p><em>foo <a href="/url">bar</a></em></p>
  5072. ````````````````````````````````
  5073. ```````````````````````````````` example
  5074. *foo
  5075. bar*
  5076. .
  5077. <p><em>foo
  5078. bar</em></p>
  5079. ````````````````````````````````
  5080. In particular, emphasis and strong emphasis can be nested
  5081. inside emphasis:
  5082. ```````````````````````````````` example
  5083. _foo __bar__ baz_
  5084. .
  5085. <p><em>foo <strong>bar</strong> baz</em></p>
  5086. ````````````````````````````````
  5087. ```````````````````````````````` example
  5088. _foo _bar_ baz_
  5089. .
  5090. <p><em>foo <em>bar</em> baz</em></p>
  5091. ````````````````````````````````
  5092. ```````````````````````````````` example
  5093. __foo_ bar_
  5094. .
  5095. <p><em><em>foo</em> bar</em></p>
  5096. ````````````````````````````````
  5097. ```````````````````````````````` example
  5098. *foo *bar**
  5099. .
  5100. <p><em>foo <em>bar</em></em></p>
  5101. ````````````````````````````````
  5102. ```````````````````````````````` example
  5103. *foo **bar** baz*
  5104. .
  5105. <p><em>foo <strong>bar</strong> baz</em></p>
  5106. ````````````````````````````````
  5107. ```````````````````````````````` example
  5108. *foo**bar**baz*
  5109. .
  5110. <p><em>foo<strong>bar</strong>baz</em></p>
  5111. ````````````````````````````````
  5112. Note that in the preceding case, the interpretation
  5113. ``` markdown
  5114. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5115. ```
  5116. is precluded by the condition that a delimiter that
  5117. can both open and close (like the `*` after `foo`)
  5118. cannot form emphasis if the sum of the lengths of
  5119. the delimiter runs containing the opening and
  5120. closing delimiters is a multiple of 3.
  5121. The same condition ensures that the following
  5122. cases are all strong emphasis nested inside
  5123. emphasis, even when the interior spaces are
  5124. omitted:
  5125. ```````````````````````````````` example
  5126. ***foo** bar*
  5127. .
  5128. <p><em><strong>foo</strong> bar</em></p>
  5129. ````````````````````````````````
  5130. ```````````````````````````````` example
  5131. *foo **bar***
  5132. .
  5133. <p><em>foo <strong>bar</strong></em></p>
  5134. ````````````````````````````````
  5135. ```````````````````````````````` example
  5136. *foo**bar***
  5137. .
  5138. <p><em>foo<strong>bar</strong></em></p>
  5139. ````````````````````````````````
  5140. Indefinite levels of nesting are possible:
  5141. ```````````````````````````````` example
  5142. *foo **bar *baz* bim** bop*
  5143. .
  5144. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5145. ````````````````````````````````
  5146. ```````````````````````````````` example
  5147. *foo [*bar*](/url)*
  5148. .
  5149. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5150. ````````````````````````````````
  5151. There can be no empty emphasis or strong emphasis:
  5152. ```````````````````````````````` example
  5153. ** is not an empty emphasis
  5154. .
  5155. <p>** is not an empty emphasis</p>
  5156. ````````````````````````````````
  5157. ```````````````````````````````` example
  5158. **** is not an empty strong emphasis
  5159. .
  5160. <p>**** is not an empty strong emphasis</p>
  5161. ````````````````````````````````
  5162. Rule 10:
  5163. Any nonempty sequence of inline elements can be the contents of an
  5164. strongly emphasized span.
  5165. ```````````````````````````````` example
  5166. **foo [bar](/url)**
  5167. .
  5168. <p><strong>foo <a href="/url">bar</a></strong></p>
  5169. ````````````````````````````````
  5170. ```````````````````````````````` example
  5171. **foo
  5172. bar**
  5173. .
  5174. <p><strong>foo
  5175. bar</strong></p>
  5176. ````````````````````````````````
  5177. In particular, emphasis and strong emphasis can be nested
  5178. inside strong emphasis:
  5179. ```````````````````````````````` example
  5180. __foo _bar_ baz__
  5181. .
  5182. <p><strong>foo <em>bar</em> baz</strong></p>
  5183. ````````````````````````````````
  5184. ```````````````````````````````` example
  5185. __foo __bar__ baz__
  5186. .
  5187. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5188. ````````````````````````````````
  5189. ```````````````````````````````` example
  5190. ____foo__ bar__
  5191. .
  5192. <p><strong><strong>foo</strong> bar</strong></p>
  5193. ````````````````````````````````
  5194. ```````````````````````````````` example
  5195. **foo **bar****
  5196. .
  5197. <p><strong>foo <strong>bar</strong></strong></p>
  5198. ````````````````````````````````
  5199. ```````````````````````````````` example
  5200. **foo *bar* baz**
  5201. .
  5202. <p><strong>foo <em>bar</em> baz</strong></p>
  5203. ````````````````````````````````
  5204. ```````````````````````````````` example
  5205. **foo*bar*baz**
  5206. .
  5207. <p><strong>foo<em>bar</em>baz</strong></p>
  5208. ````````````````````````````````
  5209. ```````````````````````````````` example
  5210. ***foo* bar**
  5211. .
  5212. <p><strong><em>foo</em> bar</strong></p>
  5213. ````````````````````````````````
  5214. ```````````````````````````````` example
  5215. **foo *bar***
  5216. .
  5217. <p><strong>foo <em>bar</em></strong></p>
  5218. ````````````````````````````````
  5219. Indefinite levels of nesting are possible:
  5220. ```````````````````````````````` example
  5221. **foo *bar **baz**
  5222. bim* bop**
  5223. .
  5224. <p><strong>foo <em>bar <strong>baz</strong>
  5225. bim</em> bop</strong></p>
  5226. ````````````````````````````````
  5227. ```````````````````````````````` example
  5228. **foo [*bar*](/url)**
  5229. .
  5230. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5231. ````````````````````````````````
  5232. There can be no empty emphasis or strong emphasis:
  5233. ```````````````````````````````` example
  5234. __ is not an empty emphasis
  5235. .
  5236. <p>__ is not an empty emphasis</p>
  5237. ````````````````````````````````
  5238. ```````````````````````````````` example
  5239. ____ is not an empty strong emphasis
  5240. .
  5241. <p>____ is not an empty strong emphasis</p>
  5242. ````````````````````````````````
  5243. Rule 11:
  5244. ```````````````````````````````` example
  5245. foo ***
  5246. .
  5247. <p>foo ***</p>
  5248. ````````````````````````````````
  5249. ```````````````````````````````` example
  5250. foo *\**
  5251. .
  5252. <p>foo <em>*</em></p>
  5253. ````````````````````````````````
  5254. ```````````````````````````````` example
  5255. foo *_*
  5256. .
  5257. <p>foo <em>_</em></p>
  5258. ````````````````````````````````
  5259. ```````````````````````````````` example
  5260. foo *****
  5261. .
  5262. <p>foo *****</p>
  5263. ````````````````````````````````
  5264. ```````````````````````````````` example
  5265. foo **\***
  5266. .
  5267. <p>foo <strong>*</strong></p>
  5268. ````````````````````````````````
  5269. ```````````````````````````````` example
  5270. foo **_**
  5271. .
  5272. <p>foo <strong>_</strong></p>
  5273. ````````````````````````````````
  5274. Note that when delimiters do not match evenly, Rule 11 determines
  5275. that the excess literal `*` characters will appear outside of the
  5276. emphasis, rather than inside it:
  5277. ```````````````````````````````` example
  5278. **foo*
  5279. .
  5280. <p>*<em>foo</em></p>
  5281. ````````````````````````````````
  5282. ```````````````````````````````` example
  5283. *foo**
  5284. .
  5285. <p><em>foo</em>*</p>
  5286. ````````````````````````````````
  5287. ```````````````````````````````` example
  5288. ***foo**
  5289. .
  5290. <p>*<strong>foo</strong></p>
  5291. ````````````````````````````````
  5292. ```````````````````````````````` example
  5293. ****foo*
  5294. .
  5295. <p>***<em>foo</em></p>
  5296. ````````````````````````````````
  5297. ```````````````````````````````` example
  5298. **foo***
  5299. .
  5300. <p><strong>foo</strong>*</p>
  5301. ````````````````````````````````
  5302. ```````````````````````````````` example
  5303. *foo****
  5304. .
  5305. <p><em>foo</em>***</p>
  5306. ````````````````````````````````
  5307. Rule 12:
  5308. ```````````````````````````````` example
  5309. foo ___
  5310. .
  5311. <p>foo ___</p>
  5312. ````````````````````````````````
  5313. ```````````````````````````````` example
  5314. foo _\__
  5315. .
  5316. <p>foo <em>_</em></p>
  5317. ````````````````````````````````
  5318. ```````````````````````````````` example
  5319. foo _*_
  5320. .
  5321. <p>foo <em>*</em></p>
  5322. ````````````````````````````````
  5323. ```````````````````````````````` example
  5324. foo _____
  5325. .
  5326. <p>foo _____</p>
  5327. ````````````````````````````````
  5328. ```````````````````````````````` example
  5329. foo __\___
  5330. .
  5331. <p>foo <strong>_</strong></p>
  5332. ````````````````````````````````
  5333. ```````````````````````````````` example
  5334. foo __*__
  5335. .
  5336. <p>foo <strong>*</strong></p>
  5337. ````````````````````````````````
  5338. ```````````````````````````````` example
  5339. __foo_
  5340. .
  5341. <p>_<em>foo</em></p>
  5342. ````````````````````````````````
  5343. Note that when delimiters do not match evenly, Rule 12 determines
  5344. that the excess literal `_` characters will appear outside of the
  5345. emphasis, rather than inside it:
  5346. ```````````````````````````````` example
  5347. _foo__
  5348. .
  5349. <p><em>foo</em>_</p>
  5350. ````````````````````````````````
  5351. ```````````````````````````````` example
  5352. ___foo__
  5353. .
  5354. <p>_<strong>foo</strong></p>
  5355. ````````````````````````````````
  5356. ```````````````````````````````` example
  5357. ____foo_
  5358. .
  5359. <p>___<em>foo</em></p>
  5360. ````````````````````````````````
  5361. ```````````````````````````````` example
  5362. __foo___
  5363. .
  5364. <p><strong>foo</strong>_</p>
  5365. ````````````````````````````````
  5366. ```````````````````````````````` example
  5367. _foo____
  5368. .
  5369. <p><em>foo</em>___</p>
  5370. ````````````````````````````````
  5371. Rule 13 implies that if you want emphasis nested directly inside
  5372. emphasis, you must use different delimiters:
  5373. ```````````````````````````````` example
  5374. **foo**
  5375. .
  5376. <p><strong>foo</strong></p>
  5377. ````````````````````````````````
  5378. ```````````````````````````````` example
  5379. *_foo_*
  5380. .
  5381. <p><em><em>foo</em></em></p>
  5382. ````````````````````````````````
  5383. ```````````````````````````````` example
  5384. __foo__
  5385. .
  5386. <p><strong>foo</strong></p>
  5387. ````````````````````````````````
  5388. ```````````````````````````````` example
  5389. _*foo*_
  5390. .
  5391. <p><em><em>foo</em></em></p>
  5392. ````````````````````````````````
  5393. However, strong emphasis within strong emphasis is possible without
  5394. switching delimiters:
  5395. ```````````````````````````````` example
  5396. ****foo****
  5397. .
  5398. <p><strong><strong>foo</strong></strong></p>
  5399. ````````````````````````````````
  5400. ```````````````````````````````` example
  5401. ____foo____
  5402. .
  5403. <p><strong><strong>foo</strong></strong></p>
  5404. ````````````````````````````````
  5405. Rule 13 can be applied to arbitrarily long sequences of
  5406. delimiters:
  5407. ```````````````````````````````` example
  5408. ******foo******
  5409. .
  5410. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5411. ````````````````````````````````
  5412. Rule 14:
  5413. ```````````````````````````````` example
  5414. ***foo***
  5415. .
  5416. <p><em><strong>foo</strong></em></p>
  5417. ````````````````````````````````
  5418. ```````````````````````````````` example
  5419. _____foo_____
  5420. .
  5421. <p><em><strong><strong>foo</strong></strong></em></p>
  5422. ````````````````````````````````
  5423. Rule 15:
  5424. ```````````````````````````````` example
  5425. *foo _bar* baz_
  5426. .
  5427. <p><em>foo _bar</em> baz_</p>
  5428. ````````````````````````````````
  5429. ```````````````````````````````` example
  5430. *foo __bar *baz bim__ bam*
  5431. .
  5432. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5433. ````````````````````````````````
  5434. Rule 16:
  5435. ```````````````````````````````` example
  5436. **foo **bar baz**
  5437. .
  5438. <p>**foo <strong>bar baz</strong></p>
  5439. ````````````````````````````````
  5440. ```````````````````````````````` example
  5441. *foo *bar baz*
  5442. .
  5443. <p>*foo <em>bar baz</em></p>
  5444. ````````````````````````````````
  5445. Rule 17:
  5446. ```````````````````````````````` example
  5447. *[bar*](/url)
  5448. .
  5449. <p>*<a href="/url">bar*</a></p>
  5450. ````````````````````````````````
  5451. ```````````````````````````````` example
  5452. _foo [bar_](/url)
  5453. .
  5454. <p>_foo <a href="/url">bar_</a></p>
  5455. ````````````````````````````````
  5456. ```````````````````````````````` example
  5457. *<img src="foo" title="*"/>
  5458. .
  5459. <p>*<img src="foo" title="*"/></p>
  5460. ````````````````````````````````
  5461. ```````````````````````````````` example
  5462. **<a href="**">
  5463. .
  5464. <p>**<a href="**"></p>
  5465. ````````````````````````````````
  5466. ```````````````````````````````` example
  5467. __<a href="__">
  5468. .
  5469. <p>__<a href="__"></p>
  5470. ````````````````````````````````
  5471. ```````````````````````````````` example
  5472. *a `*`*
  5473. .
  5474. <p><em>a <code>*</code></em></p>
  5475. ````````````````````````````````
  5476. ```````````````````````````````` example
  5477. _a `_`_
  5478. .
  5479. <p><em>a <code>_</code></em></p>
  5480. ````````````````````````````````
  5481. ```````````````````````````````` example
  5482. **a<http://foo.bar/?q=**>
  5483. .
  5484. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5485. ````````````````````````````````
  5486. ```````````````````````````````` example
  5487. __a<http://foo.bar/?q=__>
  5488. .
  5489. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5490. ````````````````````````````````
  5491. ## Links
  5492. A link contains [link text] (the visible text), a [link destination]
  5493. (the URI that is the link destination), and optionally a [link title].
  5494. There are two basic kinds of links in Markdown. In [inline links] the
  5495. destination and title are given immediately after the link text. In
  5496. [reference links] the destination and title are defined elsewhere in
  5497. the document.
  5498. A [link text](@) consists of a sequence of zero or more
  5499. inline elements enclosed by square brackets (`[` and `]`). The
  5500. following rules apply:
  5501. - Links may not contain other links, at any level of nesting. If
  5502. multiple otherwise valid link definitions appear nested inside each
  5503. other, the inner-most definition is used.
  5504. - Brackets are allowed in the [link text] only if (a) they
  5505. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5506. with an open bracket `[`, a sequence of zero or more inlines, and
  5507. a close bracket `]`.
  5508. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5509. than the brackets in link text. Thus, for example,
  5510. `` [foo`]` `` could not be a link text, since the second `]`
  5511. is part of a code span.
  5512. - The brackets in link text bind more tightly than markers for
  5513. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5514. A [link destination](@) consists of either
  5515. - a sequence of zero or more characters between an opening `<` and a
  5516. closing `>` that contains no spaces, line breaks, or unescaped
  5517. `<` or `>` characters, or
  5518. - a nonempty sequence of characters that does not include
  5519. ASCII space or control characters, and includes parentheses
  5520. only if (a) they are backslash-escaped or (b) they are part of
  5521. a balanced pair of unescaped parentheses. (Implementations
  5522. may impose limits on parentheses nesting to avoid performance
  5523. issues, but at least three levels of nesting should be supported.)
  5524. A [link title](@) consists of either
  5525. - a sequence of zero or more characters between straight double-quote
  5526. characters (`"`), including a `"` character only if it is
  5527. backslash-escaped, or
  5528. - a sequence of zero or more characters between straight single-quote
  5529. characters (`'`), including a `'` character only if it is
  5530. backslash-escaped, or
  5531. - a sequence of zero or more characters between matching parentheses
  5532. (`(...)`), including a `)` character only if it is backslash-escaped.
  5533. Although [link titles] may span multiple lines, they may not contain
  5534. a [blank line].
  5535. An [inline link](@) consists of a [link text] followed immediately
  5536. by a left parenthesis `(`, optional [whitespace], an optional
  5537. [link destination], an optional [link title] separated from the link
  5538. destination by [whitespace], optional [whitespace], and a right
  5539. parenthesis `)`. The link's text consists of the inlines contained
  5540. in the [link text] (excluding the enclosing square brackets).
  5541. The link's URI consists of the link destination, excluding enclosing
  5542. `<...>` if present, with backslash-escapes in effect as described
  5543. above. The link's title consists of the link title, excluding its
  5544. enclosing delimiters, with backslash-escapes in effect as described
  5545. above.
  5546. Here is a simple inline link:
  5547. ```````````````````````````````` example
  5548. [link](/uri "title")
  5549. .
  5550. <p><a href="/uri" title="title">link</a></p>
  5551. ````````````````````````````````
  5552. The title may be omitted:
  5553. ```````````````````````````````` example
  5554. [link](/uri)
  5555. .
  5556. <p><a href="/uri">link</a></p>
  5557. ````````````````````````````````
  5558. Both the title and the destination may be omitted:
  5559. ```````````````````````````````` example
  5560. [link]()
  5561. .
  5562. <p><a href="">link</a></p>
  5563. ````````````````````````````````
  5564. ```````````````````````````````` example
  5565. [link](<>)
  5566. .
  5567. <p><a href="">link</a></p>
  5568. ````````````````````````````````
  5569. The destination cannot contain spaces or line breaks,
  5570. even if enclosed in pointy brackets:
  5571. ```````````````````````````````` example
  5572. [link](/my uri)
  5573. .
  5574. <p>[link](/my uri)</p>
  5575. ````````````````````````````````
  5576. ```````````````````````````````` example
  5577. [link](</my uri>)
  5578. .
  5579. <p>[link](&lt;/my uri&gt;)</p>
  5580. ````````````````````````````````
  5581. ```````````````````````````````` example
  5582. [link](foo
  5583. bar)
  5584. .
  5585. <p>[link](foo
  5586. bar)</p>
  5587. ````````````````````````````````
  5588. ```````````````````````````````` example
  5589. [link](<foo
  5590. bar>)
  5591. .
  5592. <p>[link](<foo
  5593. bar>)</p>
  5594. ````````````````````````````````
  5595. Parentheses inside the link destination may be escaped:
  5596. ```````````````````````````````` example
  5597. [link](\(foo\))
  5598. .
  5599. <p><a href="(foo)">link</a></p>
  5600. ````````````````````````````````
  5601. Any number of parentheses are allowed without escaping, as long as they are
  5602. balanced:
  5603. ```````````````````````````````` example
  5604. [link](foo(and(bar)))
  5605. .
  5606. <p><a href="foo(and(bar))">link</a></p>
  5607. ````````````````````````````````
  5608. However, if you have unbalanced parentheses, you need to escape or use the
  5609. `<...>` form:
  5610. ```````````````````````````````` example
  5611. [link](foo\(and\(bar\))
  5612. .
  5613. <p><a href="foo(and(bar)">link</a></p>
  5614. ````````````````````````````````
  5615. ```````````````````````````````` example
  5616. [link](<foo(and(bar)>)
  5617. .
  5618. <p><a href="foo(and(bar)">link</a></p>
  5619. ````````````````````````````````
  5620. Parentheses and other symbols can also be escaped, as usual
  5621. in Markdown:
  5622. ```````````````````````````````` example
  5623. [link](foo\)\:)
  5624. .
  5625. <p><a href="foo):">link</a></p>
  5626. ````````````````````````````````
  5627. A link can contain fragment identifiers and queries:
  5628. ```````````````````````````````` example
  5629. [link](#fragment)
  5630. [link](http://example.com#fragment)
  5631. [link](http://example.com?foo=3#frag)
  5632. .
  5633. <p><a href="#fragment">link</a></p>
  5634. <p><a href="http://example.com#fragment">link</a></p>
  5635. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5636. ````````````````````````````````
  5637. Note that a backslash before a non-escapable character is
  5638. just a backslash:
  5639. ```````````````````````````````` example
  5640. [link](foo\bar)
  5641. .
  5642. <p><a href="foo%5Cbar">link</a></p>
  5643. ````````````````````````````````
  5644. URL-escaping should be left alone inside the destination, as all
  5645. URL-escaped characters are also valid URL characters. Entity and
  5646. numerical character references in the destination will be parsed
  5647. into the corresponding Unicode code points, as usual. These may
  5648. be optionally URL-escaped when written as HTML, but this spec
  5649. does not enforce any particular policy for rendering URLs in
  5650. HTML or other formats. Renderers may make different decisions
  5651. about how to escape or normalize URLs in the output.
  5652. ```````````````````````````````` example
  5653. [link](foo%20b&auml;)
  5654. .
  5655. <p><a href="foo%20b%C3%A4">link</a></p>
  5656. ````````````````````````````````
  5657. Note that, because titles can often be parsed as destinations,
  5658. if you try to omit the destination and keep the title, you'll
  5659. get unexpected results:
  5660. ```````````````````````````````` example
  5661. [link]("title")
  5662. .
  5663. <p><a href="%22title%22">link</a></p>
  5664. ````````````````````````````````
  5665. Titles may be in single quotes, double quotes, or parentheses:
  5666. ```````````````````````````````` example
  5667. [link](/url "title")
  5668. [link](/url 'title')
  5669. [link](/url (title))
  5670. .
  5671. <p><a href="/url" title="title">link</a>
  5672. <a href="/url" title="title">link</a>
  5673. <a href="/url" title="title">link</a></p>
  5674. ````````````````````````````````
  5675. Backslash escapes and entity and numeric character references
  5676. may be used in titles:
  5677. ```````````````````````````````` example
  5678. [link](/url "title \"&quot;")
  5679. .
  5680. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5681. ````````````````````````````````
  5682. Titles must be separated from the link using a [whitespace].
  5683. Other [Unicode whitespace] like non-breaking space doesn't work.
  5684. ```````````````````````````````` example
  5685. [link](/url "title")
  5686. .
  5687. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5688. ````````````````````````````````
  5689. Nested balanced quotes are not allowed without escaping:
  5690. ```````````````````````````````` example
  5691. [link](/url "title "and" title")
  5692. .
  5693. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5694. ````````````````````````````````
  5695. But it is easy to work around this by using a different quote type:
  5696. ```````````````````````````````` example
  5697. [link](/url 'title "and" title')
  5698. .
  5699. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5700. ````````````````````````````````
  5701. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5702. title, and its test suite included a test demonstrating this.
  5703. But it is hard to see a good rationale for the extra complexity this
  5704. brings, since there are already many ways---backslash escaping,
  5705. entity and numeric character references, or using a different
  5706. quote type for the enclosing title---to write titles containing
  5707. double quotes. `Markdown.pl`'s handling of titles has a number
  5708. of other strange features. For example, it allows single-quoted
  5709. titles in inline links, but not reference links. And, in
  5710. reference links but not inline links, it allows a title to begin
  5711. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5712. titles with no closing quotation mark, though 1.0.2b8 does not.
  5713. It seems preferable to adopt a simple, rational rule that works
  5714. the same way in inline links and link reference definitions.)
  5715. [Whitespace] is allowed around the destination and title:
  5716. ```````````````````````````````` example
  5717. [link]( /uri
  5718. "title" )
  5719. .
  5720. <p><a href="/uri" title="title">link</a></p>
  5721. ````````````````````````````````
  5722. But it is not allowed between the link text and the
  5723. following parenthesis:
  5724. ```````````````````````````````` example
  5725. [link] (/uri)
  5726. .
  5727. <p>[link] (/uri)</p>
  5728. ````````````````````````````````
  5729. The link text may contain balanced brackets, but not unbalanced ones,
  5730. unless they are escaped:
  5731. ```````````````````````````````` example
  5732. [link [foo [bar]]](/uri)
  5733. .
  5734. <p><a href="/uri">link [foo [bar]]</a></p>
  5735. ````````````````````````````````
  5736. ```````````````````````````````` example
  5737. [link] bar](/uri)
  5738. .
  5739. <p>[link] bar](/uri)</p>
  5740. ````````````````````````````````
  5741. ```````````````````````````````` example
  5742. [link [bar](/uri)
  5743. .
  5744. <p>[link <a href="/uri">bar</a></p>
  5745. ````````````````````````````````
  5746. ```````````````````````````````` example
  5747. [link \[bar](/uri)
  5748. .
  5749. <p><a href="/uri">link [bar</a></p>
  5750. ````````````````````````````````
  5751. The link text may contain inline content:
  5752. ```````````````````````````````` example
  5753. [link *foo **bar** `#`*](/uri)
  5754. .
  5755. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5756. ````````````````````````````````
  5757. ```````````````````````````````` example
  5758. [![moon](moon.jpg)](/uri)
  5759. .
  5760. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5761. ````````````````````````````````
  5762. However, links may not contain other links, at any level of nesting.
  5763. ```````````````````````````````` example
  5764. [foo [bar](/uri)](/uri)
  5765. .
  5766. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5767. ````````````````````````````````
  5768. ```````````````````````````````` example
  5769. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5770. .
  5771. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5772. ````````````````````````````````
  5773. ```````````````````````````````` example
  5774. ![[[foo](uri1)](uri2)](uri3)
  5775. .
  5776. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5777. ````````````````````````````````
  5778. These cases illustrate the precedence of link text grouping over
  5779. emphasis grouping:
  5780. ```````````````````````````````` example
  5781. *[foo*](/uri)
  5782. .
  5783. <p>*<a href="/uri">foo*</a></p>
  5784. ````````````````````````````````
  5785. ```````````````````````````````` example
  5786. [foo *bar](baz*)
  5787. .
  5788. <p><a href="baz*">foo *bar</a></p>
  5789. ````````````````````````````````
  5790. Note that brackets that *aren't* part of links do not take
  5791. precedence:
  5792. ```````````````````````````````` example
  5793. *foo [bar* baz]
  5794. .
  5795. <p><em>foo [bar</em> baz]</p>
  5796. ````````````````````````````````
  5797. These cases illustrate the precedence of HTML tags, code spans,
  5798. and autolinks over link grouping:
  5799. ```````````````````````````````` example
  5800. [foo <bar attr="](baz)">
  5801. .
  5802. <p>[foo <bar attr="](baz)"></p>
  5803. ````````````````````````````````
  5804. ```````````````````````````````` example
  5805. [foo`](/uri)`
  5806. .
  5807. <p>[foo<code>](/uri)</code></p>
  5808. ````````````````````````````````
  5809. ```````````````````````````````` example
  5810. [foo<http://example.com/?search=](uri)>
  5811. .
  5812. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5813. ````````````````````````````````
  5814. There are three kinds of [reference link](@)s:
  5815. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5816. and [shortcut](#shortcut-reference-link).
  5817. A [full reference link](@)
  5818. consists of a [link text] immediately followed by a [link label]
  5819. that [matches] a [link reference definition] elsewhere in the document.
  5820. A [link label](@) begins with a left bracket (`[`) and ends
  5821. with the first right bracket (`]`) that is not backslash-escaped.
  5822. Between these brackets there must be at least one [non-whitespace character].
  5823. Unescaped square bracket characters are not allowed inside the
  5824. opening and closing square brackets of [link labels]. A link
  5825. label can have at most 999 characters inside the square
  5826. brackets.
  5827. One label [matches](@)
  5828. another just in case their normalized forms are equal. To normalize a
  5829. label, strip off the opening and closing brackets,
  5830. perform the *Unicode case fold*, strip leading and trailing
  5831. [whitespace] and collapse consecutive internal
  5832. [whitespace] to a single space. If there are multiple
  5833. matching reference link definitions, the one that comes first in the
  5834. document is used. (It is desirable in such cases to emit a warning.)
  5835. The contents of the first link label are parsed as inlines, which are
  5836. used as the link's text. The link's URI and title are provided by the
  5837. matching [link reference definition].
  5838. Here is a simple example:
  5839. ```````````````````````````````` example
  5840. [foo][bar]
  5841. [bar]: /url "title"
  5842. .
  5843. <p><a href="/url" title="title">foo</a></p>
  5844. ````````````````````````````````
  5845. The rules for the [link text] are the same as with
  5846. [inline links]. Thus:
  5847. The link text may contain balanced brackets, but not unbalanced ones,
  5848. unless they are escaped:
  5849. ```````````````````````````````` example
  5850. [link [foo [bar]]][ref]
  5851. [ref]: /uri
  5852. .
  5853. <p><a href="/uri">link [foo [bar]]</a></p>
  5854. ````````````````````````````````
  5855. ```````````````````````````````` example
  5856. [link \[bar][ref]
  5857. [ref]: /uri
  5858. .
  5859. <p><a href="/uri">link [bar</a></p>
  5860. ````````````````````````````````
  5861. The link text may contain inline content:
  5862. ```````````````````````````````` example
  5863. [link *foo **bar** `#`*][ref]
  5864. [ref]: /uri
  5865. .
  5866. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5867. ````````````````````````````````
  5868. ```````````````````````````````` example
  5869. [![moon](moon.jpg)][ref]
  5870. [ref]: /uri
  5871. .
  5872. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5873. ````````````````````````````````
  5874. However, links may not contain other links, at any level of nesting.
  5875. ```````````````````````````````` example
  5876. [foo [bar](/uri)][ref]
  5877. [ref]: /uri
  5878. .
  5879. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5880. ````````````````````````````````
  5881. ```````````````````````````````` example
  5882. [foo *bar [baz][ref]*][ref]
  5883. [ref]: /uri
  5884. .
  5885. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5886. ````````````````````````````````
  5887. (In the examples above, we have two [shortcut reference links]
  5888. instead of one [full reference link].)
  5889. The following cases illustrate the precedence of link text grouping over
  5890. emphasis grouping:
  5891. ```````````````````````````````` example
  5892. *[foo*][ref]
  5893. [ref]: /uri
  5894. .
  5895. <p>*<a href="/uri">foo*</a></p>
  5896. ````````````````````````````````
  5897. ```````````````````````````````` example
  5898. [foo *bar][ref]
  5899. [ref]: /uri
  5900. .
  5901. <p><a href="/uri">foo *bar</a></p>
  5902. ````````````````````````````````
  5903. These cases illustrate the precedence of HTML tags, code spans,
  5904. and autolinks over link grouping:
  5905. ```````````````````````````````` example
  5906. [foo <bar attr="][ref]">
  5907. [ref]: /uri
  5908. .
  5909. <p>[foo <bar attr="][ref]"></p>
  5910. ````````````````````````````````
  5911. ```````````````````````````````` example
  5912. [foo`][ref]`
  5913. [ref]: /uri
  5914. .
  5915. <p>[foo<code>][ref]</code></p>
  5916. ````````````````````````````````
  5917. ```````````````````````````````` example
  5918. [foo<http://example.com/?search=][ref]>
  5919. [ref]: /uri
  5920. .
  5921. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5922. ````````````````````````````````
  5923. Matching is case-insensitive:
  5924. ```````````````````````````````` example
  5925. [foo][BaR]
  5926. [bar]: /url "title"
  5927. .
  5928. <p><a href="/url" title="title">foo</a></p>
  5929. ````````````````````````````````
  5930. Unicode case fold is used:
  5931. ```````````````````````````````` example
  5932. [Толпой][Толпой] is a Russian word.
  5933. [ТОЛПОЙ]: /url
  5934. .
  5935. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5936. ````````````````````````````````
  5937. Consecutive internal [whitespace] is treated as one space for
  5938. purposes of determining matching:
  5939. ```````````````````````````````` example
  5940. [Foo
  5941. bar]: /url
  5942. [Baz][Foo bar]
  5943. .
  5944. <p><a href="/url">Baz</a></p>
  5945. ````````````````````````````````
  5946. No [whitespace] is allowed between the [link text] and the
  5947. [link label]:
  5948. ```````````````````````````````` example
  5949. [foo] [bar]
  5950. [bar]: /url "title"
  5951. .
  5952. <p>[foo] <a href="/url" title="title">bar</a></p>
  5953. ````````````````````````````````
  5954. ```````````````````````````````` example
  5955. [foo]
  5956. [bar]
  5957. [bar]: /url "title"
  5958. .
  5959. <p>[foo]
  5960. <a href="/url" title="title">bar</a></p>
  5961. ````````````````````````````````
  5962. This is a departure from John Gruber's original Markdown syntax
  5963. description, which explicitly allows whitespace between the link
  5964. text and the link label. It brings reference links in line with
  5965. [inline links], which (according to both original Markdown and
  5966. this spec) cannot have whitespace after the link text. More
  5967. importantly, it prevents inadvertent capture of consecutive
  5968. [shortcut reference links]. If whitespace is allowed between the
  5969. link text and the link label, then in the following we will have
  5970. a single reference link, not two shortcut reference links, as
  5971. intended:
  5972. ``` markdown
  5973. [foo]
  5974. [bar]
  5975. [foo]: /url1
  5976. [bar]: /url2
  5977. ```
  5978. (Note that [shortcut reference links] were introduced by Gruber
  5979. himself in a beta version of `Markdown.pl`, but never included
  5980. in the official syntax description. Without shortcut reference
  5981. links, it is harmless to allow space between the link text and
  5982. link label; but once shortcut references are introduced, it is
  5983. too dangerous to allow this, as it frequently leads to
  5984. unintended results.)
  5985. When there are multiple matching [link reference definitions],
  5986. the first is used:
  5987. ```````````````````````````````` example
  5988. [foo]: /url1
  5989. [foo]: /url2
  5990. [bar][foo]
  5991. .
  5992. <p><a href="/url1">bar</a></p>
  5993. ````````````````````````````````
  5994. Note that matching is performed on normalized strings, not parsed
  5995. inline content. So the following does not match, even though the
  5996. labels define equivalent inline content:
  5997. ```````````````````````````````` example
  5998. [bar][foo\!]
  5999. [foo!]: /url
  6000. .
  6001. <p>[bar][foo!]</p>
  6002. ````````````````````````````````
  6003. [Link labels] cannot contain brackets, unless they are
  6004. backslash-escaped:
  6005. ```````````````````````````````` example
  6006. [foo][ref[]
  6007. [ref[]: /uri
  6008. .
  6009. <p>[foo][ref[]</p>
  6010. <p>[ref[]: /uri</p>
  6011. ````````````````````````````````
  6012. ```````````````````````````````` example
  6013. [foo][ref[bar]]
  6014. [ref[bar]]: /uri
  6015. .
  6016. <p>[foo][ref[bar]]</p>
  6017. <p>[ref[bar]]: /uri</p>
  6018. ````````````````````````````````
  6019. ```````````````````````````````` example
  6020. [[[foo]]]
  6021. [[[foo]]]: /url
  6022. .
  6023. <p>[[[foo]]]</p>
  6024. <p>[[[foo]]]: /url</p>
  6025. ````````````````````````````````
  6026. ```````````````````````````````` example
  6027. [foo][ref\[]
  6028. [ref\[]: /uri
  6029. .
  6030. <p><a href="/uri">foo</a></p>
  6031. ````````````````````````````````
  6032. Note that in this example `]` is not backslash-escaped:
  6033. ```````````````````````````````` example
  6034. [bar\\]: /uri
  6035. [bar\\]
  6036. .
  6037. <p><a href="/uri">bar\</a></p>
  6038. ````````````````````````````````
  6039. A [link label] must contain at least one [non-whitespace character]:
  6040. ```````````````````````````````` example
  6041. []
  6042. []: /uri
  6043. .
  6044. <p>[]</p>
  6045. <p>[]: /uri</p>
  6046. ````````````````````````````````
  6047. ```````````````````````````````` example
  6048. [
  6049. ]
  6050. [
  6051. ]: /uri
  6052. .
  6053. <p>[
  6054. ]</p>
  6055. <p>[
  6056. ]: /uri</p>
  6057. ````````````````````````````````
  6058. A [collapsed reference link](@)
  6059. consists of a [link label] that [matches] a
  6060. [link reference definition] elsewhere in the
  6061. document, followed by the string `[]`.
  6062. The contents of the first link label are parsed as inlines,
  6063. which are used as the link's text. The link's URI and title are
  6064. provided by the matching reference link definition. Thus,
  6065. `[foo][]` is equivalent to `[foo][foo]`.
  6066. ```````````````````````````````` example
  6067. [foo][]
  6068. [foo]: /url "title"
  6069. .
  6070. <p><a href="/url" title="title">foo</a></p>
  6071. ````````````````````````````````
  6072. ```````````````````````````````` example
  6073. [*foo* bar][]
  6074. [*foo* bar]: /url "title"
  6075. .
  6076. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6077. ````````````````````````````````
  6078. The link labels are case-insensitive:
  6079. ```````````````````````````````` example
  6080. [Foo][]
  6081. [foo]: /url "title"
  6082. .
  6083. <p><a href="/url" title="title">Foo</a></p>
  6084. ````````````````````````````````
  6085. As with full reference links, [whitespace] is not
  6086. allowed between the two sets of brackets:
  6087. ```````````````````````````````` example
  6088. [foo]
  6089. []
  6090. [foo]: /url "title"
  6091. .
  6092. <p><a href="/url" title="title">foo</a>
  6093. []</p>
  6094. ````````````````````````````````
  6095. A [shortcut reference link](@)
  6096. consists of a [link label] that [matches] a
  6097. [link reference definition] elsewhere in the
  6098. document and is not followed by `[]` or a link label.
  6099. The contents of the first link label are parsed as inlines,
  6100. which are used as the link's text. The link's URI and title
  6101. are provided by the matching link reference definition.
  6102. Thus, `[foo]` is equivalent to `[foo][]`.
  6103. ```````````````````````````````` example
  6104. [foo]
  6105. [foo]: /url "title"
  6106. .
  6107. <p><a href="/url" title="title">foo</a></p>
  6108. ````````````````````````````````
  6109. ```````````````````````````````` example
  6110. [*foo* bar]
  6111. [*foo* bar]: /url "title"
  6112. .
  6113. <p><a href="/url" title="title"><em>foo</em> bar</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. [[bar [foo]
  6123. [foo]: /url
  6124. .
  6125. <p>[[bar <a href="/url">foo</a></p>
  6126. ````````````````````````````````
  6127. The link labels are case-insensitive:
  6128. ```````````````````````````````` example
  6129. [Foo]
  6130. [foo]: /url "title"
  6131. .
  6132. <p><a href="/url" title="title">Foo</a></p>
  6133. ````````````````````````````````
  6134. A space after the link text should be preserved:
  6135. ```````````````````````````````` example
  6136. [foo] bar
  6137. [foo]: /url
  6138. .
  6139. <p><a href="/url">foo</a> bar</p>
  6140. ````````````````````````````````
  6141. If you just want bracketed text, you can backslash-escape the
  6142. opening bracket to avoid links:
  6143. ```````````````````````````````` example
  6144. \[foo]
  6145. [foo]: /url "title"
  6146. .
  6147. <p>[foo]</p>
  6148. ````````````````````````````````
  6149. Note that this is a link, because a link label ends with the first
  6150. following closing bracket:
  6151. ```````````````````````````````` example
  6152. [foo*]: /url
  6153. *[foo*]
  6154. .
  6155. <p>*<a href="/url">foo*</a></p>
  6156. ````````````````````````````````
  6157. Full and compact references take precedence over shortcut
  6158. references:
  6159. ```````````````````````````````` example
  6160. [foo][bar]
  6161. [foo]: /url1
  6162. [bar]: /url2
  6163. .
  6164. <p><a href="/url2">foo</a></p>
  6165. ````````````````````````````````
  6166. ```````````````````````````````` example
  6167. [foo][]
  6168. [foo]: /url1
  6169. .
  6170. <p><a href="/url1">foo</a></p>
  6171. ````````````````````````````````
  6172. Inline links also take precedence:
  6173. ```````````````````````````````` example
  6174. [foo]()
  6175. [foo]: /url1
  6176. .
  6177. <p><a href="">foo</a></p>
  6178. ````````````````````````````````
  6179. ```````````````````````````````` example
  6180. [foo](not a link)
  6181. [foo]: /url1
  6182. .
  6183. <p><a href="/url1">foo</a>(not a link)</p>
  6184. ````````````````````````````````
  6185. In the following case `[bar][baz]` is parsed as a reference,
  6186. `[foo]` as normal text:
  6187. ```````````````````````````````` example
  6188. [foo][bar][baz]
  6189. [baz]: /url
  6190. .
  6191. <p>[foo]<a href="/url">bar</a></p>
  6192. ````````````````````````````````
  6193. Here, though, `[foo][bar]` is parsed as a reference, since
  6194. `[bar]` is defined:
  6195. ```````````````````````````````` example
  6196. [foo][bar][baz]
  6197. [baz]: /url1
  6198. [bar]: /url2
  6199. .
  6200. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6201. ````````````````````````````````
  6202. Here `[foo]` is not parsed as a shortcut reference, because it
  6203. is followed by a link label (even though `[bar]` is not defined):
  6204. ```````````````````````````````` example
  6205. [foo][bar][baz]
  6206. [baz]: /url1
  6207. [foo]: /url2
  6208. .
  6209. <p>[foo]<a href="/url1">bar</a></p>
  6210. ````````````````````````````````
  6211. ## Images
  6212. Syntax for images is like the syntax for links, with one
  6213. difference. Instead of [link text], we have an
  6214. [image description](@). The rules for this are the
  6215. same as for [link text], except that (a) an
  6216. image description starts with `![` rather than `[`, and
  6217. (b) an image description may contain links.
  6218. An image description has inline elements
  6219. as its contents. When an image is rendered to HTML,
  6220. this is standardly used as the image's `alt` attribute.
  6221. ```````````````````````````````` example
  6222. ![foo](/url "title")
  6223. .
  6224. <p><img src="/url" alt="foo" title="title" /></p>
  6225. ````````````````````````````````
  6226. ```````````````````````````````` example
  6227. ![foo *bar*]
  6228. [foo *bar*]: train.jpg "train & tracks"
  6229. .
  6230. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6231. ````````````````````````````````
  6232. ```````````````````````````````` example
  6233. ![foo ![bar](/url)](/url2)
  6234. .
  6235. <p><img src="/url2" alt="foo bar" /></p>
  6236. ````````````````````````````````
  6237. ```````````````````````````````` example
  6238. ![foo [bar](/url)](/url2)
  6239. .
  6240. <p><img src="/url2" alt="foo bar" /></p>
  6241. ````````````````````````````````
  6242. Though this spec is concerned with parsing, not rendering, it is
  6243. recommended that in rendering to HTML, only the plain string content
  6244. of the [image description] be used. Note that in
  6245. the above example, the alt attribute's value is `foo bar`, not `foo
  6246. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6247. content is rendered, without formatting.
  6248. ```````````````````````````````` example
  6249. ![foo *bar*][]
  6250. [foo *bar*]: train.jpg "train & tracks"
  6251. .
  6252. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6253. ````````````````````````````````
  6254. ```````````````````````````````` example
  6255. ![foo *bar*][foobar]
  6256. [FOOBAR]: train.jpg "train & tracks"
  6257. .
  6258. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6259. ````````````````````````````````
  6260. ```````````````````````````````` example
  6261. ![foo](train.jpg)
  6262. .
  6263. <p><img src="train.jpg" alt="foo" /></p>
  6264. ````````````````````````````````
  6265. ```````````````````````````````` example
  6266. My ![foo bar](/path/to/train.jpg "title" )
  6267. .
  6268. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6269. ````````````````````````````````
  6270. ```````````````````````````````` example
  6271. ![foo](<url>)
  6272. .
  6273. <p><img src="url" alt="foo" /></p>
  6274. ````````````````````````````````
  6275. ```````````````````````````````` example
  6276. ![](/url)
  6277. .
  6278. <p><img src="/url" alt="" /></p>
  6279. ````````````````````````````````
  6280. Reference-style:
  6281. ```````````````````````````````` example
  6282. ![foo][bar]
  6283. [bar]: /url
  6284. .
  6285. <p><img src="/url" alt="foo" /></p>
  6286. ````````````````````````````````
  6287. ```````````````````````````````` example
  6288. ![foo][bar]
  6289. [BAR]: /url
  6290. .
  6291. <p><img src="/url" alt="foo" /></p>
  6292. ````````````````````````````````
  6293. Collapsed:
  6294. ```````````````````````````````` example
  6295. ![foo][]
  6296. [foo]: /url "title"
  6297. .
  6298. <p><img src="/url" alt="foo" title="title" /></p>
  6299. ````````````````````````````````
  6300. ```````````````````````````````` example
  6301. ![*foo* bar][]
  6302. [*foo* bar]: /url "title"
  6303. .
  6304. <p><img src="/url" alt="foo bar" title="title" /></p>
  6305. ````````````````````````````````
  6306. The labels are case-insensitive:
  6307. ```````````````````````````````` example
  6308. ![Foo][]
  6309. [foo]: /url "title"
  6310. .
  6311. <p><img src="/url" alt="Foo" title="title" /></p>
  6312. ````````````````````````````````
  6313. As with reference links, [whitespace] is not allowed
  6314. between the two sets of brackets:
  6315. ```````````````````````````````` example
  6316. ![foo]
  6317. []
  6318. [foo]: /url "title"
  6319. .
  6320. <p><img src="/url" alt="foo" title="title" />
  6321. []</p>
  6322. ````````````````````````````````
  6323. Shortcut:
  6324. ```````````````````````````````` example
  6325. ![foo]
  6326. [foo]: /url "title"
  6327. .
  6328. <p><img src="/url" alt="foo" title="title" /></p>
  6329. ````````````````````````````````
  6330. ```````````````````````````````` example
  6331. ![*foo* bar]
  6332. [*foo* bar]: /url "title"
  6333. .
  6334. <p><img src="/url" alt="foo bar" title="title" /></p>
  6335. ````````````````````````````````
  6336. Note that link labels cannot contain unescaped brackets:
  6337. ```````````````````````````````` example
  6338. ![[foo]]
  6339. [[foo]]: /url "title"
  6340. .
  6341. <p>![[foo]]</p>
  6342. <p>[[foo]]: /url &quot;title&quot;</p>
  6343. ````````````````````````````````
  6344. The link labels are case-insensitive:
  6345. ```````````````````````````````` example
  6346. ![Foo]
  6347. [foo]: /url "title"
  6348. .
  6349. <p><img src="/url" alt="Foo" title="title" /></p>
  6350. ````````````````````````````````
  6351. If you just want a literal `!` followed by bracketed text, you can
  6352. backslash-escape the opening `[`:
  6353. ```````````````````````````````` example
  6354. !\[foo]
  6355. [foo]: /url "title"
  6356. .
  6357. <p>![foo]</p>
  6358. ````````````````````````````````
  6359. If you want a link after a literal `!`, backslash-escape the
  6360. `!`:
  6361. ```````````````````````````````` example
  6362. \![foo]
  6363. [foo]: /url "title"
  6364. .
  6365. <p>!<a href="/url" title="title">foo</a></p>
  6366. ````````````````````````````````
  6367. ## Autolinks
  6368. [Autolink](@)s are absolute URIs and email addresses inside
  6369. `<` and `>`. They are parsed as links, with the URL or email address
  6370. as the link label.
  6371. A [URI autolink](@) consists of `<`, followed by an
  6372. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6373. a link to the URI, with the URI as the link's label.
  6374. An [absolute URI](@),
  6375. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6376. followed by zero or more characters other than ASCII
  6377. [whitespace] and control characters, `<`, and `>`. If
  6378. the URI includes these characters, they must be percent-encoded
  6379. (e.g. `%20` for a space).
  6380. For purposes of this spec, a [scheme](@) is any sequence
  6381. of 2--32 characters beginning with an ASCII letter and followed
  6382. by any combination of ASCII letters, digits, or the symbols plus
  6383. ("+"), period ("."), or hyphen ("-").
  6384. Here are some valid autolinks:
  6385. ```````````````````````````````` example
  6386. <http://foo.bar.baz>
  6387. .
  6388. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6389. ````````````````````````````````
  6390. ```````````````````````````````` example
  6391. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6392. .
  6393. <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>
  6394. ````````````````````````````````
  6395. ```````````````````````````````` example
  6396. <irc://foo.bar:2233/baz>
  6397. .
  6398. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6399. ````````````````````````````````
  6400. Uppercase is also fine:
  6401. ```````````````````````````````` example
  6402. <MAILTO:FOO@BAR.BAZ>
  6403. .
  6404. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6405. ````````````````````````````````
  6406. Note that many strings that count as [absolute URIs] for
  6407. purposes of this spec are not valid URIs, because their
  6408. schemes are not registered or because of other problems
  6409. with their syntax:
  6410. ```````````````````````````````` example
  6411. <a+b+c:d>
  6412. .
  6413. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6414. ````````````````````````````````
  6415. ```````````````````````````````` example
  6416. <made-up-scheme://foo,bar>
  6417. .
  6418. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6419. ````````````````````````````````
  6420. ```````````````````````````````` example
  6421. <http://../>
  6422. .
  6423. <p><a href="http://../">http://../</a></p>
  6424. ````````````````````````````````
  6425. ```````````````````````````````` example
  6426. <localhost:5001/foo>
  6427. .
  6428. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6429. ````````````````````````````````
  6430. Spaces are not allowed in autolinks:
  6431. ```````````````````````````````` example
  6432. <http://foo.bar/baz bim>
  6433. .
  6434. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6435. ````````````````````````````````
  6436. Backslash-escapes do not work inside autolinks:
  6437. ```````````````````````````````` example
  6438. <http://example.com/\[\>
  6439. .
  6440. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6441. ````````````````````````````````
  6442. An [email autolink](@)
  6443. consists of `<`, followed by an [email address],
  6444. followed by `>`. The link's label is the email address,
  6445. and the URL is `mailto:` followed by the email address.
  6446. An [email address](@),
  6447. for these purposes, is anything that matches
  6448. the [non-normative regex from the HTML5
  6449. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6450. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6451. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6452. Examples of email autolinks:
  6453. ```````````````````````````````` example
  6454. <foo@bar.example.com>
  6455. .
  6456. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6457. ````````````````````````````````
  6458. ```````````````````````````````` example
  6459. <foo+special@Bar.baz-bar0.com>
  6460. .
  6461. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6462. ````````````````````````````````
  6463. Backslash-escapes do not work inside email autolinks:
  6464. ```````````````````````````````` example
  6465. <foo\+@bar.example.com>
  6466. .
  6467. <p>&lt;foo+@bar.example.com&gt;</p>
  6468. ````````````````````````````````
  6469. These are not autolinks:
  6470. ```````````````````````````````` example
  6471. <>
  6472. .
  6473. <p>&lt;&gt;</p>
  6474. ````````````````````````````````
  6475. ```````````````````````````````` example
  6476. < http://foo.bar >
  6477. .
  6478. <p>&lt; http://foo.bar &gt;</p>
  6479. ````````````````````````````````
  6480. ```````````````````````````````` example
  6481. <m:abc>
  6482. .
  6483. <p>&lt;m:abc&gt;</p>
  6484. ````````````````````````````````
  6485. ```````````````````````````````` example
  6486. <foo.bar.baz>
  6487. .
  6488. <p>&lt;foo.bar.baz&gt;</p>
  6489. ````````````````````````````````
  6490. ```````````````````````````````` example
  6491. http://example.com
  6492. .
  6493. <p>http://example.com</p>
  6494. ````````````````````````````````
  6495. ```````````````````````````````` example
  6496. foo@bar.example.com
  6497. .
  6498. <p>foo@bar.example.com</p>
  6499. ````````````````````````````````
  6500. ## Raw HTML
  6501. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6502. raw HTML tag and will be rendered in HTML without escaping.
  6503. Tag and attribute names are not limited to current HTML tags,
  6504. so custom tags (and even, say, DocBook tags) may be used.
  6505. Here is the grammar for tags:
  6506. A [tag name](@) consists of an ASCII letter
  6507. followed by zero or more ASCII letters, digits, or
  6508. hyphens (`-`).
  6509. An [attribute](@) consists of [whitespace],
  6510. an [attribute name], and an optional
  6511. [attribute value specification].
  6512. An [attribute name](@)
  6513. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6514. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6515. specification restricted to ASCII. HTML5 is laxer.)
  6516. An [attribute value specification](@)
  6517. consists of optional [whitespace],
  6518. a `=` character, optional [whitespace], and an [attribute
  6519. value].
  6520. An [attribute value](@)
  6521. consists of an [unquoted attribute value],
  6522. a [single-quoted attribute value], or a [double-quoted attribute value].
  6523. An [unquoted attribute value](@)
  6524. is a nonempty string of characters not
  6525. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6526. A [single-quoted attribute value](@)
  6527. consists of `'`, zero or more
  6528. characters not including `'`, and a final `'`.
  6529. A [double-quoted attribute value](@)
  6530. consists of `"`, zero or more
  6531. characters not including `"`, and a final `"`.
  6532. An [open tag](@) consists of a `<` character, a [tag name],
  6533. zero or more [attributes], optional [whitespace], an optional `/`
  6534. character, and a `>` character.
  6535. A [closing tag](@) consists of the string `</`, a
  6536. [tag name], optional [whitespace], and the character `>`.
  6537. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6538. where *text* does not start with `>` or `->`, does not end with `-`,
  6539. and does not contain `--`. (See the
  6540. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6541. A [processing instruction](@)
  6542. consists of the string `<?`, a string
  6543. of characters not including the string `?>`, and the string
  6544. `?>`.
  6545. A [declaration](@) consists of the
  6546. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6547. [whitespace], a string of characters not including the
  6548. character `>`, and the character `>`.
  6549. A [CDATA section](@) consists of
  6550. the string `<![CDATA[`, a string of characters not including the string
  6551. `]]>`, and the string `]]>`.
  6552. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6553. an [HTML comment], a [processing instruction], a [declaration],
  6554. or a [CDATA section].
  6555. Here are some simple open tags:
  6556. ```````````````````````````````` example
  6557. <a><bab><c2c>
  6558. .
  6559. <p><a><bab><c2c></p>
  6560. ````````````````````````````````
  6561. Empty elements:
  6562. ```````````````````````````````` example
  6563. <a/><b2/>
  6564. .
  6565. <p><a/><b2/></p>
  6566. ````````````````````````````````
  6567. [Whitespace] is allowed:
  6568. ```````````````````````````````` example
  6569. <a /><b2
  6570. data="foo" >
  6571. .
  6572. <p><a /><b2
  6573. data="foo" ></p>
  6574. ````````````````````````````````
  6575. With attributes:
  6576. ```````````````````````````````` example
  6577. <a foo="bar" bam = 'baz <em>"</em>'
  6578. _boolean zoop:33=zoop:33 />
  6579. .
  6580. <p><a foo="bar" bam = 'baz <em>"</em>'
  6581. _boolean zoop:33=zoop:33 /></p>
  6582. ````````````````````````````````
  6583. Custom tag names can be used:
  6584. ```````````````````````````````` example
  6585. Foo <responsive-image src="foo.jpg" />
  6586. .
  6587. <p>Foo <responsive-image src="foo.jpg" /></p>
  6588. ````````````````````````````````
  6589. Illegal tag names, not parsed as HTML:
  6590. ```````````````````````````````` example
  6591. <33> <__>
  6592. .
  6593. <p>&lt;33&gt; &lt;__&gt;</p>
  6594. ````````````````````````````````
  6595. Illegal attribute names:
  6596. ```````````````````````````````` example
  6597. <a h*#ref="hi">
  6598. .
  6599. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6600. ````````````````````````````````
  6601. Illegal attribute values:
  6602. ```````````````````````````````` example
  6603. <a href="hi'> <a href=hi'>
  6604. .
  6605. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6606. ````````````````````````````````
  6607. Illegal [whitespace]:
  6608. ```````````````````````````````` example
  6609. < a><
  6610. foo><bar/ >
  6611. <foo bar=baz
  6612. bim!bop />
  6613. .
  6614. <p>&lt; a&gt;&lt;
  6615. foo&gt;&lt;bar/ &gt;
  6616. &lt;foo bar=baz
  6617. bim!bop /&gt;</p>
  6618. ````````````````````````````````
  6619. Missing [whitespace]:
  6620. ```````````````````````````````` example
  6621. <a href='bar'title=title>
  6622. .
  6623. <p>&lt;a href='bar'title=title&gt;</p>
  6624. ````````````````````````````````
  6625. Closing tags:
  6626. ```````````````````````````````` example
  6627. </a></foo >
  6628. .
  6629. <p></a></foo ></p>
  6630. ````````````````````````````````
  6631. Illegal attributes in closing tag:
  6632. ```````````````````````````````` example
  6633. </a href="foo">
  6634. .
  6635. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6636. ````````````````````````````````
  6637. Comments:
  6638. ```````````````````````````````` example
  6639. foo <!-- this is a
  6640. comment - with hyphen -->
  6641. .
  6642. <p>foo <!-- this is a
  6643. comment - with hyphen --></p>
  6644. ````````````````````````````````
  6645. ```````````````````````````````` example
  6646. foo <!-- not a comment -- two hyphens -->
  6647. .
  6648. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6649. ````````````````````````````````
  6650. Not comments:
  6651. ```````````````````````````````` example
  6652. foo <!--> foo -->
  6653. foo <!-- foo--->
  6654. .
  6655. <p>foo &lt;!--&gt; foo --&gt;</p>
  6656. <p>foo &lt;!-- foo---&gt;</p>
  6657. ````````````````````````````````
  6658. Processing instructions:
  6659. ```````````````````````````````` example
  6660. foo <?php echo $a; ?>
  6661. .
  6662. <p>foo <?php echo $a; ?></p>
  6663. ````````````````````````````````
  6664. Declarations:
  6665. ```````````````````````````````` example
  6666. foo <!ELEMENT br EMPTY>
  6667. .
  6668. <p>foo <!ELEMENT br EMPTY></p>
  6669. ````````````````````````````````
  6670. CDATA sections:
  6671. ```````````````````````````````` example
  6672. foo <![CDATA[>&<]]>
  6673. .
  6674. <p>foo <![CDATA[>&<]]></p>
  6675. ````````````````````````````````
  6676. Entity and numeric character references are preserved in HTML
  6677. attributes:
  6678. ```````````````````````````````` example
  6679. foo <a href="&ouml;">
  6680. .
  6681. <p>foo <a href="&ouml;"></p>
  6682. ````````````````````````````````
  6683. Backslash escapes do not work in HTML attributes:
  6684. ```````````````````````````````` example
  6685. foo <a href="\*">
  6686. .
  6687. <p>foo <a href="\*"></p>
  6688. ````````````````````````````````
  6689. ```````````````````````````````` example
  6690. <a href="\"">
  6691. .
  6692. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6693. ````````````````````````````````
  6694. ## Hard line breaks
  6695. A line break (not in a code span or HTML tag) that is preceded
  6696. by two or more spaces and does not occur at the end of a block
  6697. is parsed as a [hard line break](@) (rendered
  6698. in HTML as a `<br />` tag):
  6699. ```````````````````````````````` example
  6700. foo
  6701. baz
  6702. .
  6703. <p>foo<br />
  6704. baz</p>
  6705. ````````````````````````````````
  6706. For a more visible alternative, a backslash before the
  6707. [line ending] may be used instead of two spaces:
  6708. ```````````````````````````````` example
  6709. foo\
  6710. baz
  6711. .
  6712. <p>foo<br />
  6713. baz</p>
  6714. ````````````````````````````````
  6715. More than two spaces can be used:
  6716. ```````````````````````````````` example
  6717. foo
  6718. baz
  6719. .
  6720. <p>foo<br />
  6721. baz</p>
  6722. ````````````````````````````````
  6723. Leading spaces at the beginning of the next line are ignored:
  6724. ```````````````````````````````` example
  6725. foo
  6726. bar
  6727. .
  6728. <p>foo<br />
  6729. bar</p>
  6730. ````````````````````````````````
  6731. ```````````````````````````````` example
  6732. foo\
  6733. bar
  6734. .
  6735. <p>foo<br />
  6736. bar</p>
  6737. ````````````````````````````````
  6738. Line breaks can occur inside emphasis, links, and other constructs
  6739. that allow inline content:
  6740. ```````````````````````````````` example
  6741. *foo
  6742. bar*
  6743. .
  6744. <p><em>foo<br />
  6745. bar</em></p>
  6746. ````````````````````````````````
  6747. ```````````````````````````````` example
  6748. *foo\
  6749. bar*
  6750. .
  6751. <p><em>foo<br />
  6752. bar</em></p>
  6753. ````````````````````````````````
  6754. Line breaks do not occur inside code spans
  6755. ```````````````````````````````` example
  6756. `code
  6757. span`
  6758. .
  6759. <p><code>code span</code></p>
  6760. ````````````````````````````````
  6761. ```````````````````````````````` example
  6762. `code\
  6763. span`
  6764. .
  6765. <p><code>code\ span</code></p>
  6766. ````````````````````````````````
  6767. or HTML tags:
  6768. ```````````````````````````````` example
  6769. <a href="foo
  6770. bar">
  6771. .
  6772. <p><a href="foo
  6773. bar"></p>
  6774. ````````````````````````````````
  6775. ```````````````````````````````` example
  6776. <a href="foo\
  6777. bar">
  6778. .
  6779. <p><a href="foo\
  6780. bar"></p>
  6781. ````````````````````````````````
  6782. Hard line breaks are for separating inline content within a block.
  6783. Neither syntax for hard line breaks works at the end of a paragraph or
  6784. other block element:
  6785. ```````````````````````````````` example
  6786. foo\
  6787. .
  6788. <p>foo\</p>
  6789. ````````````````````````````````
  6790. ```````````````````````````````` example
  6791. foo
  6792. .
  6793. <p>foo</p>
  6794. ````````````````````````````````
  6795. ```````````````````````````````` example
  6796. ### foo\
  6797. .
  6798. <h3>foo\</h3>
  6799. ````````````````````````````````
  6800. ```````````````````````````````` example
  6801. ### foo
  6802. .
  6803. <h3>foo</h3>
  6804. ````````````````````````````````
  6805. ## Soft line breaks
  6806. A regular line break (not in a code span or HTML tag) that is not
  6807. preceded by two or more spaces or a backslash is parsed as a
  6808. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6809. [line ending] or as a space. The result will be the same in
  6810. browsers. In the examples here, a [line ending] will be used.)
  6811. ```````````````````````````````` example
  6812. foo
  6813. baz
  6814. .
  6815. <p>foo
  6816. baz</p>
  6817. ````````````````````````````````
  6818. Spaces at the end of the line and beginning of the next line are
  6819. removed:
  6820. ```````````````````````````````` example
  6821. foo
  6822. baz
  6823. .
  6824. <p>foo
  6825. baz</p>
  6826. ````````````````````````````````
  6827. A conforming parser may render a soft line break in HTML either as a
  6828. line break or as a space.
  6829. A renderer may also provide an option to render soft line breaks
  6830. as hard line breaks.
  6831. ## Textual content
  6832. Any characters not given an interpretation by the above rules will
  6833. be parsed as plain textual content.
  6834. ```````````````````````````````` example
  6835. hello $.;'there
  6836. .
  6837. <p>hello $.;'there</p>
  6838. ````````````````````````````````
  6839. ```````````````````````````````` example
  6840. Foo χρῆν
  6841. .
  6842. <p>Foo χρῆν</p>
  6843. ````````````````````````````````
  6844. Internal spaces are preserved verbatim:
  6845. ```````````````````````````````` example
  6846. Multiple spaces
  6847. .
  6848. <p>Multiple spaces</p>
  6849. ````````````````````````````````
  6850. <!-- END TESTS -->
  6851. # Appendix: A parsing strategy
  6852. In this appendix we describe some features of the parsing strategy
  6853. used in the CommonMark reference implementations.
  6854. ## Overview
  6855. Parsing has two phases:
  6856. 1. In the first phase, lines of input are consumed and the block
  6857. structure of the document---its division into paragraphs, block quotes,
  6858. list items, and so on---is constructed. Text is assigned to these
  6859. blocks but not parsed. Link reference definitions are parsed and a
  6860. map of links is constructed.
  6861. 2. In the second phase, the raw text contents of paragraphs and headings
  6862. are parsed into sequences of Markdown inline elements (strings,
  6863. code spans, links, emphasis, and so on), using the map of link
  6864. references constructed in phase 1.
  6865. At each point in processing, the document is represented as a tree of
  6866. **blocks**. The root of the tree is a `document` block. The `document`
  6867. may have any number of other blocks as **children**. These children
  6868. may, in turn, have other blocks as children. The last child of a block
  6869. is normally considered **open**, meaning that subsequent lines of input
  6870. can alter its contents. (Blocks that are not open are **closed**.)
  6871. Here, for example, is a possible document tree, with the open blocks
  6872. marked by arrows:
  6873. ``` tree
  6874. -> document
  6875. -> block_quote
  6876. paragraph
  6877. "Lorem ipsum dolor\nsit amet."
  6878. -> list (type=bullet tight=true bullet_char=-)
  6879. list_item
  6880. paragraph
  6881. "Qui *quodsi iracundia*"
  6882. -> list_item
  6883. -> paragraph
  6884. "aliquando id"
  6885. ```
  6886. ## Phase 1: block structure
  6887. Each line that is processed has an effect on this tree. The line is
  6888. analyzed and, depending on its contents, the document may be altered
  6889. in one or more of the following ways:
  6890. 1. One or more open blocks may be closed.
  6891. 2. One or more new blocks may be created as children of the
  6892. last open block.
  6893. 3. Text may be added to the last (deepest) open block remaining
  6894. on the tree.
  6895. Once a line has been incorporated into the tree in this way,
  6896. it can be discarded, so input can be read in a stream.
  6897. For each line, we follow this procedure:
  6898. 1. First we iterate through the open blocks, starting with the
  6899. root document, and descending through last children down to the last
  6900. open block. Each block imposes a condition that the line must satisfy
  6901. if the block is to remain open. For example, a block quote requires a
  6902. `>` character. A paragraph requires a non-blank line.
  6903. In this phase we may match all or just some of the open
  6904. blocks. But we cannot close unmatched blocks yet, because we may have a
  6905. [lazy continuation line].
  6906. 2. Next, after consuming the continuation markers for existing
  6907. blocks, we look for new block starts (e.g. `>` for a block quote).
  6908. If we encounter a new block start, we close any blocks unmatched
  6909. in step 1 before creating the new block as a child of the last
  6910. matched block.
  6911. 3. Finally, we look at the remainder of the line (after block
  6912. markers like `>`, list markers, and indentation have been consumed).
  6913. This is text that can be incorporated into the last open
  6914. block (a paragraph, code block, heading, or raw HTML).
  6915. Setext headings are formed when we see a line of a paragraph
  6916. that is a [setext heading underline].
  6917. Reference link definitions are detected when a paragraph is closed;
  6918. the accumulated text lines are parsed to see if they begin with
  6919. one or more reference link definitions. Any remainder becomes a
  6920. normal paragraph.
  6921. We can see how this works by considering how the tree above is
  6922. generated by four lines of Markdown:
  6923. ``` markdown
  6924. > Lorem ipsum dolor
  6925. sit amet.
  6926. > - Qui *quodsi iracundia*
  6927. > - aliquando id
  6928. ```
  6929. At the outset, our document model is just
  6930. ``` tree
  6931. -> document
  6932. ```
  6933. The first line of our text,
  6934. ``` markdown
  6935. > Lorem ipsum dolor
  6936. ```
  6937. causes a `block_quote` block to be created as a child of our
  6938. open `document` block, and a `paragraph` block as a child of
  6939. the `block_quote`. Then the text is added to the last open
  6940. block, the `paragraph`:
  6941. ``` tree
  6942. -> document
  6943. -> block_quote
  6944. -> paragraph
  6945. "Lorem ipsum dolor"
  6946. ```
  6947. The next line,
  6948. ``` markdown
  6949. sit amet.
  6950. ```
  6951. is a "lazy continuation" of the open `paragraph`, so it gets added
  6952. to the paragraph's text:
  6953. ``` tree
  6954. -> document
  6955. -> block_quote
  6956. -> paragraph
  6957. "Lorem ipsum dolor\nsit amet."
  6958. ```
  6959. The third line,
  6960. ``` markdown
  6961. > - Qui *quodsi iracundia*
  6962. ```
  6963. causes the `paragraph` block to be closed, and a new `list` block
  6964. opened as a child of the `block_quote`. A `list_item` is also
  6965. added as a child of the `list`, and a `paragraph` as a child of
  6966. the `list_item`. The text is then added to the new `paragraph`:
  6967. ``` tree
  6968. -> document
  6969. -> block_quote
  6970. paragraph
  6971. "Lorem ipsum dolor\nsit amet."
  6972. -> list (type=bullet tight=true bullet_char=-)
  6973. -> list_item
  6974. -> paragraph
  6975. "Qui *quodsi iracundia*"
  6976. ```
  6977. The fourth line,
  6978. ``` markdown
  6979. > - aliquando id
  6980. ```
  6981. causes the `list_item` (and its child the `paragraph`) to be closed,
  6982. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6983. is added as a child of the new `list_item`, to contain the text.
  6984. We thus obtain the final tree:
  6985. ``` tree
  6986. -> document
  6987. -> block_quote
  6988. paragraph
  6989. "Lorem ipsum dolor\nsit amet."
  6990. -> list (type=bullet tight=true bullet_char=-)
  6991. list_item
  6992. paragraph
  6993. "Qui *quodsi iracundia*"
  6994. -> list_item
  6995. -> paragraph
  6996. "aliquando id"
  6997. ```
  6998. ## Phase 2: inline structure
  6999. Once all of the input has been parsed, all open blocks are closed.
  7000. We then "walk the tree," visiting every node, and parse raw
  7001. string contents of paragraphs and headings as inlines. At this
  7002. point we have seen all the link reference definitions, so we can
  7003. resolve reference links as we go.
  7004. ``` tree
  7005. document
  7006. block_quote
  7007. paragraph
  7008. str "Lorem ipsum dolor"
  7009. softbreak
  7010. str "sit amet."
  7011. list (type=bullet tight=true bullet_char=-)
  7012. list_item
  7013. paragraph
  7014. str "Qui "
  7015. emph
  7016. str "quodsi iracundia"
  7017. list_item
  7018. paragraph
  7019. str "aliquando id"
  7020. ```
  7021. Notice how the [line ending] in the first paragraph has
  7022. been parsed as a `softbreak`, and the asterisks in the first list item
  7023. have become an `emph`.
  7024. ### An algorithm for parsing nested emphasis and links
  7025. By far the trickiest part of inline parsing is handling emphasis,
  7026. strong emphasis, links, and images. This is done using the following
  7027. algorithm.
  7028. When we're parsing inlines and we hit either
  7029. - a run of `*` or `_` characters, or
  7030. - a `[` or `![`
  7031. we insert a text node with these symbols as its literal content, and we
  7032. add a pointer to this text node to the [delimiter stack](@).
  7033. The [delimiter stack] is a doubly linked list. Each
  7034. element contains a pointer to a text node, plus information about
  7035. - the type of delimiter (`[`, `![`, `*`, `_`)
  7036. - the number of delimiters,
  7037. - whether the delimiter is "active" (all are active to start), and
  7038. - whether the delimiter is a potential opener, a potential closer,
  7039. or both (which depends on what sort of characters precede
  7040. and follow the delimiters).
  7041. When we hit a `]` character, we call the *look for link or image*
  7042. procedure (see below).
  7043. When we hit the end of the input, we call the *process emphasis*
  7044. procedure (see below), with `stack_bottom` = NULL.
  7045. #### *look for link or image*
  7046. Starting at the top of the delimiter stack, we look backwards
  7047. through the stack for an opening `[` or `![` delimiter.
  7048. - If we don't find one, we return a literal text node `]`.
  7049. - If we do find one, but it's not *active*, we remove the inactive
  7050. delimiter from the stack, and return a literal text node `]`.
  7051. - If we find one and it's active, then we parse ahead to see if
  7052. we have an inline link/image, reference link/image, compact reference
  7053. link/image, or shortcut reference link/image.
  7054. + If we don't, then we remove the opening delimiter from the
  7055. delimiter stack and return a literal text node `]`.
  7056. + If we do, then
  7057. * We return a link or image node whose children are the inlines
  7058. after the text node pointed to by the opening delimiter.
  7059. * We run *process emphasis* on these inlines, with the `[` opener
  7060. as `stack_bottom`.
  7061. * We remove the opening delimiter.
  7062. * If we have a link (and not an image), we also set all
  7063. `[` delimiters before the opening delimiter to *inactive*. (This
  7064. will prevent us from getting links within links.)
  7065. #### *process emphasis*
  7066. Parameter `stack_bottom` sets a lower bound to how far we
  7067. descend in the [delimiter stack]. If it is NULL, we can
  7068. go all the way to the bottom. Otherwise, we stop before
  7069. visiting `stack_bottom`.
  7070. Let `current_position` point to the element on the [delimiter stack]
  7071. just above `stack_bottom` (or the first element if `stack_bottom`
  7072. is NULL).
  7073. We keep track of the `openers_bottom` for each delimiter
  7074. type (`*`, `_`). Initialize this to `stack_bottom`.
  7075. Then we repeat the following until we run out of potential
  7076. closers:
  7077. - Move `current_position` forward in the delimiter stack (if needed)
  7078. until we find the first potential closer with delimiter `*` or `_`.
  7079. (This will be the potential closer closest
  7080. to the beginning of the input -- the first one in parse order.)
  7081. - Now, look back in the stack (staying above `stack_bottom` and
  7082. the `openers_bottom` for this delimiter type) for the
  7083. first matching potential opener ("matching" means same delimiter).
  7084. - If one is found:
  7085. + Figure out whether we have emphasis or strong emphasis:
  7086. if both closer and opener spans have length >= 2, we have
  7087. strong, otherwise regular.
  7088. + Insert an emph or strong emph node accordingly, after
  7089. the text node corresponding to the opener.
  7090. + Remove any delimiters between the opener and closer from
  7091. the delimiter stack.
  7092. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7093. from the opening and closing text nodes. If they become empty
  7094. as a result, remove them and remove the corresponding element
  7095. of the delimiter stack. If the closing node is removed, reset
  7096. `current_position` to the next element in the stack.
  7097. - If none in found:
  7098. + Set `openers_bottom` to the element before `current_position`.
  7099. (We know that there are no openers for this kind of closer up to and
  7100. including this point, so this puts a lower bound on future searches.)
  7101. + If the closer at `current_position` is not a potential opener,
  7102. remove it from the delimiter stack (since we know it can't
  7103. be a closer either).
  7104. + Advance `current_position` to the next element in the stack.
  7105. After we're done, we remove all delimiters above `stack_bottom` from the
  7106. delimiter stack.