aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 02d1c7f6afb7294230471a2faa42ec99adb0f8e1 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.26
  5. date: '2016-07-15'
  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 used for indicating formatting in email and
  12. usenet posts. It was developed in 2004 by John Gruber, who wrote
  13. the first Markdown-to-HTML converter in Perl, and it soon became
  14. ubiquitous. In the next decade, dozens of implementations were
  15. developed in many languages. Some extended the original
  16. Markdown syntax with conventions for footnotes, tables, and
  17. other document elements. Some allowed Markdown documents to be
  18. rendered in formats other than HTML. Websites like Reddit,
  19. StackOverflow, and GitHub had millions of people using Markdown.
  20. And Markdown started to be used beyond the web, to author books,
  21. articles, slide shows, letters, and lecture notes.
  22. What distinguishes Markdown from many other lightweight markup
  23. syntaxes, which are often easier to write, is its readability.
  24. As Gruber writes:
  25. > The overriding design goal for Markdown's formatting syntax is
  26. > to make it as readable as possible. The idea is that a
  27. > Markdown-formatted document should be publishable as-is, as
  28. > plain text, without looking like it's been marked up with tags
  29. > or formatting instructions.
  30. > (<http://daringfireball.net/projects/markdown/>)
  31. The point can be illustrated by comparing a sample of
  32. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  33. an equivalent sample of Markdown. Here is a sample of
  34. AsciiDoc from the AsciiDoc manual:
  35. ```
  36. 1. List item one.
  37. +
  38. List item one continued with a second paragraph followed by an
  39. Indented block.
  40. +
  41. .................
  42. $ ls *.sh
  43. $ mv *.sh ~/tmp
  44. .................
  45. +
  46. List item continued with a third paragraph.
  47. 2. List item two continued with an open block.
  48. +
  49. --
  50. This paragraph is part of the preceding list item.
  51. a. This list is nested and does not require explicit item
  52. continuation.
  53. +
  54. This paragraph is part of the preceding list item.
  55. b. List item b.
  56. This paragraph belongs to item two of the outer list.
  57. --
  58. ```
  59. And here is the equivalent in Markdown:
  60. ```
  61. 1. List item one.
  62. List item one continued with a second paragraph followed by an
  63. Indented block.
  64. $ ls *.sh
  65. $ mv *.sh ~/tmp
  66. List item continued with a third paragraph.
  67. 2. List item two continued with an open block.
  68. This paragraph is part of the preceding list item.
  69. 1. This list is nested and does not require explicit item continuation.
  70. This paragraph is part of the preceding list item.
  71. 2. List item b.
  72. This paragraph belongs to item two of the outer list.
  73. ```
  74. The AsciiDoc version is, arguably, easier to write. You don't need
  75. to worry about indentation. But the Markdown version is much easier
  76. to read. The nesting of list items is apparent to the eye in the
  77. source, not just in the processed document.
  78. ## Why is a spec needed?
  79. John Gruber's [canonical description of Markdown's
  80. syntax](http://daringfireball.net/projects/markdown/syntax)
  81. does not specify the syntax unambiguously. Here are some examples of
  82. questions it does not answer:
  83. 1. How much indentation is needed for a sublist? The spec says that
  84. continuation paragraphs need to be indented four spaces, but is
  85. not fully explicit about sublists. It is natural to think that
  86. they, too, must be indented four spaces, but `Markdown.pl` does
  87. not require that. This is hardly a "corner case," and divergences
  88. between implementations on this issue often lead to surprises for
  89. users in real documents. (See [this comment by John
  90. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  91. 2. Is a blank line needed before a block quote or heading?
  92. Most implementations do not require the blank line. However,
  93. this can lead to unexpected results in hard-wrapped text, and
  94. also to ambiguities in parsing (note that some implementations
  95. put the heading inside the blockquote, while others do not).
  96. (John Gruber has also spoken [in favor of requiring the blank
  97. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  98. 3. Is a blank line needed before an indented code block?
  99. (`Markdown.pl` requires it, but this is not mentioned in the
  100. documentation, and some implementations do not require it.)
  101. ``` markdown
  102. paragraph
  103. code?
  104. ```
  105. 4. What is the exact rule for determining when list items get
  106. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  107. "tight"? What should we do with a list like this?
  108. ``` markdown
  109. 1. one
  110. 2. two
  111. 3. three
  112. ```
  113. Or this?
  114. ``` markdown
  115. 1. one
  116. - a
  117. - b
  118. 2. two
  119. ```
  120. (There are some relevant comments by John Gruber
  121. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  122. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  123. ``` markdown
  124. 8. item 1
  125. 9. item 2
  126. 10. item 2a
  127. ```
  128. 6. Is this one list with a thematic break in its second item,
  129. or two lists separated by a thematic break?
  130. ``` markdown
  131. * a
  132. * * * * *
  133. * b
  134. ```
  135. 7. When list markers change from numbers to bullets, do we have
  136. two lists or one? (The Markdown syntax description suggests two,
  137. but the perl scripts and many other implementations produce one.)
  138. ``` markdown
  139. 1. fee
  140. 2. fie
  141. - foe
  142. - fum
  143. ```
  144. 8. What are the precedence rules for the markers of inline structure?
  145. For example, is the following a valid link, or does the code span
  146. take precedence ?
  147. ``` markdown
  148. [a backtick (`)](/url) and [another backtick (`)](/url).
  149. ```
  150. 9. What are the precedence rules for markers of emphasis and strong
  151. emphasis? For example, how should the following be parsed?
  152. ``` markdown
  153. *foo *bar* baz*
  154. ```
  155. 10. What are the precedence rules between block-level and inline-level
  156. structure? For example, how should the following be parsed?
  157. ``` markdown
  158. - `a long code span can contain a hyphen like this
  159. - and it can screw things up`
  160. ```
  161. 11. Can list items include section headings? (`Markdown.pl` does not
  162. allow this, but does allow blockquotes to include headings.)
  163. ``` markdown
  164. - # Heading
  165. ```
  166. 12. Can list items be empty?
  167. ``` markdown
  168. * a
  169. *
  170. * b
  171. ```
  172. 13. Can link references be defined inside block quotes or list items?
  173. ``` markdown
  174. > Blockquote [foo].
  175. >
  176. > [foo]: /url
  177. ```
  178. 14. If there are multiple definitions for the same reference, which takes
  179. precedence?
  180. ``` markdown
  181. [foo]: /url1
  182. [foo]: /url2
  183. [foo][]
  184. ```
  185. In the absence of a spec, early implementers consulted `Markdown.pl`
  186. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  187. gave manifestly bad results in many cases, so it was not a
  188. satisfactory replacement for a spec.
  189. Because there is no unambiguous spec, implementations have diverged
  190. considerably. As a result, users are often surprised to find that
  191. a document that renders one way on one system (say, a github wiki)
  192. renders differently on another (say, converting to docbook using
  193. pandoc). To make matters worse, because nothing in Markdown counts
  194. as a "syntax error," the divergence often isn't discovered right away.
  195. ## About this document
  196. This document attempts to specify Markdown syntax unambiguously.
  197. It contains many examples with side-by-side Markdown and
  198. HTML. These are intended to double as conformance tests. An
  199. accompanying script `spec_tests.py` can be used to run the tests
  200. against any Markdown program:
  201. python test/spec_tests.py --spec spec.txt --program PROGRAM
  202. Since this document describes how Markdown is to be parsed into
  203. an abstract syntax tree, it would have made sense to use an abstract
  204. representation of the syntax tree instead of HTML. But HTML is capable
  205. of representing the structural distinctions we need to make, and the
  206. choice of HTML for the tests makes it possible to run the tests against
  207. an implementation without writing an abstract syntax tree renderer.
  208. This document is generated from a text file, `spec.txt`, written
  209. in Markdown with a small extension for the side-by-side tests.
  210. The script `tools/makespec.py` can be used to convert `spec.txt` into
  211. HTML or CommonMark (which can then be converted into other formats).
  212. In the examples, the `→` character is used to represent tabs.
  213. # Preliminaries
  214. ## Characters and lines
  215. Any sequence of [characters] is a valid CommonMark
  216. document.
  217. A [character](@) is a Unicode code point. Although some
  218. code points (for example, combining accents) do not correspond to
  219. characters in an intuitive sense, all code points count as characters
  220. for purposes of this spec.
  221. This spec does not specify an encoding; it thinks of lines as composed
  222. of [characters] rather than bytes. A conforming parser may be limited
  223. to a certain encoding.
  224. A [line](@) is a sequence of zero or more [characters]
  225. other than newline (`U+000A`) or carriage return (`U+000D`),
  226. followed by a [line ending] or by the end of file.
  227. A [line ending](@) is a newline (`U+000A`), a carriage return
  228. (`U+000D`) not followed by a newline, or a carriage return and a
  229. following newline.
  230. A line containing no characters, or a line containing only spaces
  231. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  232. The following definitions of character classes will be used in this spec:
  233. A [whitespace character](@) is a space
  234. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  235. form feed (`U+000C`), or carriage return (`U+000D`).
  236. [Whitespace](@) is a sequence of one or more [whitespace
  237. characters].
  238. A [Unicode whitespace character](@) is
  239. any code point in the Unicode `Zs` class, or a tab (`U+0009`),
  240. carriage return (`U+000D`), newline (`U+000A`), or form feed
  241. (`U+000C`).
  242. [Unicode whitespace](@) is a sequence of one
  243. or more [Unicode whitespace characters].
  244. A [space](@) is `U+0020`.
  245. A [non-whitespace character](@) is any character
  246. that is not a [whitespace character].
  247. An [ASCII punctuation character](@)
  248. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  249. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  250. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  251. A [punctuation character](@) is an [ASCII
  252. punctuation character] or anything in
  253. the Unicode classes `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  254. ## Tabs
  255. Tabs in lines are not expanded to [spaces]. However,
  256. in contexts where whitespace helps to define block structure,
  257. tabs behave as if they were replaced by spaces with a tab stop
  258. of 4 characters.
  259. Thus, for example, a tab can be used instead of four spaces
  260. in an indented code block. (Note, however, that internal
  261. tabs are passed through as literal tabs, not expanded to
  262. spaces.)
  263. ```````````````````````````````` example
  264. →foo→baz→→bim
  265. .
  266. <pre><code>foo→baz→→bim
  267. </code></pre>
  268. ````````````````````````````````
  269. ```````````````````````````````` example
  270. →foo→baz→→bim
  271. .
  272. <pre><code>foo→baz→→bim
  273. </code></pre>
  274. ````````````````````````````````
  275. ```````````````````````````````` example
  276. a→a
  277. ὐ→a
  278. .
  279. <pre><code>a→a
  280. ὐ→a
  281. </code></pre>
  282. ````````````````````````````````
  283. In the following example, a continuation paragraph of a list
  284. item is indented with a tab; this has exactly the same effect
  285. as indentation with four spaces would:
  286. ```````````````````````````````` example
  287. - foo
  288. →bar
  289. .
  290. <ul>
  291. <li>
  292. <p>foo</p>
  293. <p>bar</p>
  294. </li>
  295. </ul>
  296. ````````````````````````````````
  297. ```````````````````````````````` example
  298. - foo
  299. →→bar
  300. .
  301. <ul>
  302. <li>
  303. <p>foo</p>
  304. <pre><code> bar
  305. </code></pre>
  306. </li>
  307. </ul>
  308. ````````````````````````````````
  309. Normally the `>` that begins a block quote may be followed
  310. optionally by a space, which is not considered part of the
  311. content. In the following case `>` is followed by a tab,
  312. which is treated as if it were expanded into spaces.
  313. Since one of theses spaces is considered part of the
  314. delimiter, `foo` is considered to be indented six spaces
  315. inside the block quote context, so we get an indented
  316. code block starting with two spaces.
  317. ```````````````````````````````` example
  318. >→→foo
  319. .
  320. <blockquote>
  321. <pre><code> foo
  322. </code></pre>
  323. </blockquote>
  324. ````````````````````````````````
  325. ```````````````````````````````` example
  326. -→→foo
  327. .
  328. <ul>
  329. <li>
  330. <pre><code> foo
  331. </code></pre>
  332. </li>
  333. </ul>
  334. ````````````````````````````````
  335. ```````````````````````````````` example
  336. foo
  337. →bar
  338. .
  339. <pre><code>foo
  340. bar
  341. </code></pre>
  342. ````````````````````````````````
  343. ```````````````````````````````` example
  344. - foo
  345. - bar
  346. → - baz
  347. .
  348. <ul>
  349. <li>foo
  350. <ul>
  351. <li>bar
  352. <ul>
  353. <li>baz</li>
  354. </ul>
  355. </li>
  356. </ul>
  357. </li>
  358. </ul>
  359. ````````````````````````````````
  360. ```````````````````````````````` example
  361. #→Foo
  362. .
  363. <h1>Foo</h1>
  364. ````````````````````````````````
  365. ```````````````````````````````` example
  366. *→*→*→
  367. .
  368. <hr />
  369. ````````````````````````````````
  370. ## Insecure characters
  371. For security reasons, the Unicode character `U+0000` must be replaced
  372. with the REPLACEMENT CHARACTER (`U+FFFD`).
  373. # Blocks and inlines
  374. We can think of a document as a sequence of
  375. [blocks](@)---structural elements like paragraphs, block
  376. quotations, lists, headings, rules, and code blocks. Some blocks (like
  377. block quotes and list items) contain other blocks; others (like
  378. headings and paragraphs) contain [inline](@) content---text,
  379. links, emphasized text, images, code, and so on.
  380. ## Precedence
  381. Indicators of block structure always take precedence over indicators
  382. of inline structure. So, for example, the following is a list with
  383. two items, not a list with one item containing a code span:
  384. ```````````````````````````````` example
  385. - `one
  386. - two`
  387. .
  388. <ul>
  389. <li>`one</li>
  390. <li>two`</li>
  391. </ul>
  392. ````````````````````````````````
  393. This means that parsing can proceed in two steps: first, the block
  394. structure of the document can be discerned; second, text lines inside
  395. paragraphs, headings, and other block constructs can be parsed for inline
  396. structure. The second step requires information about link reference
  397. definitions that will be available only at the end of the first
  398. step. Note that the first step requires processing lines in sequence,
  399. but the second can be parallelized, since the inline parsing of
  400. one block element does not affect the inline parsing of any other.
  401. ## Container blocks and leaf blocks
  402. We can divide blocks into two types:
  403. [container block](@)s,
  404. which can contain other blocks, and [leaf block](@)s,
  405. which cannot.
  406. # Leaf blocks
  407. This section describes the different kinds of leaf block that make up a
  408. Markdown document.
  409. ## Thematic breaks
  410. A line consisting of 0-3 spaces of indentation, followed by a sequence
  411. of three or more matching `-`, `_`, or `*` characters, each followed
  412. optionally by any number of spaces, forms a
  413. [thematic break](@).
  414. ```````````````````````````````` example
  415. ***
  416. ---
  417. ___
  418. .
  419. <hr />
  420. <hr />
  421. <hr />
  422. ````````````````````````````````
  423. Wrong characters:
  424. ```````````````````````````````` example
  425. +++
  426. .
  427. <p>+++</p>
  428. ````````````````````````````````
  429. ```````````````````````````````` example
  430. ===
  431. .
  432. <p>===</p>
  433. ````````````````````````````````
  434. Not enough characters:
  435. ```````````````````````````````` example
  436. --
  437. **
  438. __
  439. .
  440. <p>--
  441. **
  442. __</p>
  443. ````````````````````````````````
  444. One to three spaces indent are allowed:
  445. ```````````````````````````````` example
  446. ***
  447. ***
  448. ***
  449. .
  450. <hr />
  451. <hr />
  452. <hr />
  453. ````````````````````````````````
  454. Four spaces is too many:
  455. ```````````````````````````````` example
  456. ***
  457. .
  458. <pre><code>***
  459. </code></pre>
  460. ````````````````````````````````
  461. ```````````````````````````````` example
  462. Foo
  463. ***
  464. .
  465. <p>Foo
  466. ***</p>
  467. ````````````````````````````````
  468. More than three characters may be used:
  469. ```````````````````````````````` example
  470. _____________________________________
  471. .
  472. <hr />
  473. ````````````````````````````````
  474. Spaces are allowed between the characters:
  475. ```````````````````````````````` example
  476. - - -
  477. .
  478. <hr />
  479. ````````````````````````````````
  480. ```````````````````````````````` example
  481. ** * ** * ** * **
  482. .
  483. <hr />
  484. ````````````````````````````````
  485. ```````````````````````````````` example
  486. - - - -
  487. .
  488. <hr />
  489. ````````````````````````````````
  490. Spaces are allowed at the end:
  491. ```````````````````````````````` example
  492. - - - -
  493. .
  494. <hr />
  495. ````````````````````````````````
  496. However, no other characters may occur in the line:
  497. ```````````````````````````````` example
  498. _ _ _ _ a
  499. a------
  500. ---a---
  501. .
  502. <p>_ _ _ _ a</p>
  503. <p>a------</p>
  504. <p>---a---</p>
  505. ````````````````````````````````
  506. It is required that all of the [non-whitespace characters] be the same.
  507. So, this is not a thematic break:
  508. ```````````````````````````````` example
  509. *-*
  510. .
  511. <p><em>-</em></p>
  512. ````````````````````````````````
  513. Thematic breaks do not need blank lines before or after:
  514. ```````````````````````````````` example
  515. - foo
  516. ***
  517. - bar
  518. .
  519. <ul>
  520. <li>foo</li>
  521. </ul>
  522. <hr />
  523. <ul>
  524. <li>bar</li>
  525. </ul>
  526. ````````````````````````````````
  527. Thematic breaks can interrupt a paragraph:
  528. ```````````````````````````````` example
  529. Foo
  530. ***
  531. bar
  532. .
  533. <p>Foo</p>
  534. <hr />
  535. <p>bar</p>
  536. ````````````````````````````````
  537. If a line of dashes that meets the above conditions for being a
  538. thematic break could also be interpreted as the underline of a [setext
  539. heading], the interpretation as a
  540. [setext heading] takes precedence. Thus, for example,
  541. this is a setext heading, not a paragraph followed by a thematic break:
  542. ```````````````````````````````` example
  543. Foo
  544. ---
  545. bar
  546. .
  547. <h2>Foo</h2>
  548. <p>bar</p>
  549. ````````````````````````````````
  550. When both a thematic break and a list item are possible
  551. interpretations of a line, the thematic break takes precedence:
  552. ```````````````````````````````` example
  553. * Foo
  554. * * *
  555. * Bar
  556. .
  557. <ul>
  558. <li>Foo</li>
  559. </ul>
  560. <hr />
  561. <ul>
  562. <li>Bar</li>
  563. </ul>
  564. ````````````````````````````````
  565. If you want a thematic break in a list item, use a different bullet:
  566. ```````````````````````````````` example
  567. - Foo
  568. - * * *
  569. .
  570. <ul>
  571. <li>Foo</li>
  572. <li>
  573. <hr />
  574. </li>
  575. </ul>
  576. ````````````````````````````````
  577. ## ATX headings
  578. An [ATX heading](@)
  579. consists of a string of characters, parsed as inline content, between an
  580. opening sequence of 1--6 unescaped `#` characters and an optional
  581. closing sequence of any number of unescaped `#` characters.
  582. The opening sequence of `#` characters must be followed by a
  583. [space] or by the end of line. The optional closing sequence of `#`s must be
  584. preceded by a [space] and may be followed by spaces only. The opening
  585. `#` character may be indented 0-3 spaces. The raw contents of the
  586. heading are stripped of leading and trailing spaces before being parsed
  587. as inline content. The heading level is equal to the number of `#`
  588. characters in the opening sequence.
  589. Simple headings:
  590. ```````````````````````````````` example
  591. # foo
  592. ## foo
  593. ### foo
  594. #### foo
  595. ##### foo
  596. ###### foo
  597. .
  598. <h1>foo</h1>
  599. <h2>foo</h2>
  600. <h3>foo</h3>
  601. <h4>foo</h4>
  602. <h5>foo</h5>
  603. <h6>foo</h6>
  604. ````````````````````````````````
  605. More than six `#` characters is not a heading:
  606. ```````````````````````````````` example
  607. ####### foo
  608. .
  609. <p>####### foo</p>
  610. ````````````````````````````````
  611. At least one space is required between the `#` characters and the
  612. heading's contents, unless the heading is empty. Note that many
  613. implementations currently do not require the space. However, the
  614. space was required by the
  615. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  616. and it helps prevent things like the following from being parsed as
  617. headings:
  618. ```````````````````````````````` example
  619. #5 bolt
  620. #hashtag
  621. .
  622. <p>#5 bolt</p>
  623. <p>#hashtag</p>
  624. ````````````````````````````````
  625. This is not a heading, because the first `#` is escaped:
  626. ```````````````````````````````` example
  627. \## foo
  628. .
  629. <p>## foo</p>
  630. ````````````````````````````````
  631. Contents are parsed as inlines:
  632. ```````````````````````````````` example
  633. # foo *bar* \*baz\*
  634. .
  635. <h1>foo <em>bar</em> *baz*</h1>
  636. ````````````````````````````````
  637. Leading and trailing blanks are ignored in parsing inline content:
  638. ```````````````````````````````` example
  639. # foo
  640. .
  641. <h1>foo</h1>
  642. ````````````````````````````````
  643. One to three spaces indentation are allowed:
  644. ```````````````````````````````` example
  645. ### foo
  646. ## foo
  647. # foo
  648. .
  649. <h3>foo</h3>
  650. <h2>foo</h2>
  651. <h1>foo</h1>
  652. ````````````````````````````````
  653. Four spaces are too much:
  654. ```````````````````````````````` example
  655. # foo
  656. .
  657. <pre><code># foo
  658. </code></pre>
  659. ````````````````````````````````
  660. ```````````````````````````````` example
  661. foo
  662. # bar
  663. .
  664. <p>foo
  665. # bar</p>
  666. ````````````````````````````````
  667. A closing sequence of `#` characters is optional:
  668. ```````````````````````````````` example
  669. ## foo ##
  670. ### bar ###
  671. .
  672. <h2>foo</h2>
  673. <h3>bar</h3>
  674. ````````````````````````````````
  675. It need not be the same length as the opening sequence:
  676. ```````````````````````````````` example
  677. # foo ##################################
  678. ##### foo ##
  679. .
  680. <h1>foo</h1>
  681. <h5>foo</h5>
  682. ````````````````````````````````
  683. Spaces are allowed after the closing sequence:
  684. ```````````````````````````````` example
  685. ### foo ###
  686. .
  687. <h3>foo</h3>
  688. ````````````````````````````````
  689. A sequence of `#` characters with anything but [spaces] following it
  690. is not a closing sequence, but counts as part of the contents of the
  691. heading:
  692. ```````````````````````````````` example
  693. ### foo ### b
  694. .
  695. <h3>foo ### b</h3>
  696. ````````````````````````````````
  697. The closing sequence must be preceded by a space:
  698. ```````````````````````````````` example
  699. # foo#
  700. .
  701. <h1>foo#</h1>
  702. ````````````````````````````````
  703. Backslash-escaped `#` characters do not count as part
  704. of the closing sequence:
  705. ```````````````````````````````` example
  706. ### foo \###
  707. ## foo #\##
  708. # foo \#
  709. .
  710. <h3>foo ###</h3>
  711. <h2>foo ###</h2>
  712. <h1>foo #</h1>
  713. ````````````````````````````````
  714. ATX headings need not be separated from surrounding content by blank
  715. lines, and they can interrupt paragraphs:
  716. ```````````````````````````````` example
  717. ****
  718. ## foo
  719. ****
  720. .
  721. <hr />
  722. <h2>foo</h2>
  723. <hr />
  724. ````````````````````````````````
  725. ```````````````````````````````` example
  726. Foo bar
  727. # baz
  728. Bar foo
  729. .
  730. <p>Foo bar</p>
  731. <h1>baz</h1>
  732. <p>Bar foo</p>
  733. ````````````````````````````````
  734. ATX headings can be empty:
  735. ```````````````````````````````` example
  736. ##
  737. #
  738. ### ###
  739. .
  740. <h2></h2>
  741. <h1></h1>
  742. <h3></h3>
  743. ````````````````````````````````
  744. ## Setext headings
  745. A [setext heading](@) consists of one or more
  746. lines of text, each containing at least one [non-whitespace
  747. character], with no more than 3 spaces indentation, followed by
  748. a [setext heading underline]. The lines of text must be such
  749. that, were they not followed by the setext heading underline,
  750. they would be interpreted as a paragraph: they cannot be
  751. interpretable as a [code fence], [ATX heading][ATX headings],
  752. [block quote][block quotes], [thematic break][thematic breaks],
  753. [list item][list items], or [HTML block][HTML blocks].
  754. A [setext heading underline](@) is a sequence of
  755. `=` characters or a sequence of `-` characters, with no more than 3
  756. spaces indentation and any number of trailing spaces. If a line
  757. containing a single `-` can be interpreted as an
  758. empty [list items], it should be interpreted this way
  759. and not as a [setext heading underline].
  760. The heading is a level 1 heading if `=` characters are used in
  761. the [setext heading underline], and a level 2 heading if `-`
  762. characters are used. The contents of the heading are the result
  763. of parsing the preceding lines of text as CommonMark inline
  764. content.
  765. In general, a setext heading need not be preceded or followed by a
  766. blank line. However, it cannot interrupt a paragraph, so when a
  767. setext heading comes after a paragraph, a blank line is needed between
  768. them.
  769. Simple examples:
  770. ```````````````````````````````` example
  771. Foo *bar*
  772. =========
  773. Foo *bar*
  774. ---------
  775. .
  776. <h1>Foo <em>bar</em></h1>
  777. <h2>Foo <em>bar</em></h2>
  778. ````````````````````````````````
  779. The content of the header may span more than one line:
  780. ```````````````````````````````` example
  781. Foo *bar
  782. baz*
  783. ====
  784. .
  785. <h1>Foo <em>bar
  786. baz</em></h1>
  787. ````````````````````````````````
  788. The underlining can be any length:
  789. ```````````````````````````````` example
  790. Foo
  791. -------------------------
  792. Foo
  793. =
  794. .
  795. <h2>Foo</h2>
  796. <h1>Foo</h1>
  797. ````````````````````````````````
  798. The heading content can be indented up to three spaces, and need
  799. not line up with the underlining:
  800. ```````````````````````````````` example
  801. Foo
  802. ---
  803. Foo
  804. -----
  805. Foo
  806. ===
  807. .
  808. <h2>Foo</h2>
  809. <h2>Foo</h2>
  810. <h1>Foo</h1>
  811. ````````````````````````````````
  812. Four spaces indent is too much:
  813. ```````````````````````````````` example
  814. Foo
  815. ---
  816. Foo
  817. ---
  818. .
  819. <pre><code>Foo
  820. ---
  821. Foo
  822. </code></pre>
  823. <hr />
  824. ````````````````````````````````
  825. The setext heading underline can be indented up to three spaces, and
  826. may have trailing spaces:
  827. ```````````````````````````````` example
  828. Foo
  829. ----
  830. .
  831. <h2>Foo</h2>
  832. ````````````````````````````````
  833. Four spaces is too much:
  834. ```````````````````````````````` example
  835. Foo
  836. ---
  837. .
  838. <p>Foo
  839. ---</p>
  840. ````````````````````````````````
  841. The setext heading underline cannot contain internal spaces:
  842. ```````````````````````````````` example
  843. Foo
  844. = =
  845. Foo
  846. --- -
  847. .
  848. <p>Foo
  849. = =</p>
  850. <p>Foo</p>
  851. <hr />
  852. ````````````````````````````````
  853. Trailing spaces in the content line do not cause a line break:
  854. ```````````````````````````````` example
  855. Foo
  856. -----
  857. .
  858. <h2>Foo</h2>
  859. ````````````````````````````````
  860. Nor does a backslash at the end:
  861. ```````````````````````````````` example
  862. Foo\
  863. ----
  864. .
  865. <h2>Foo\</h2>
  866. ````````````````````````````````
  867. Since indicators of block structure take precedence over
  868. indicators of inline structure, the following are setext headings:
  869. ```````````````````````````````` example
  870. `Foo
  871. ----
  872. `
  873. <a title="a lot
  874. ---
  875. of dashes"/>
  876. .
  877. <h2>`Foo</h2>
  878. <p>`</p>
  879. <h2>&lt;a title=&quot;a lot</h2>
  880. <p>of dashes&quot;/&gt;</p>
  881. ````````````````````````````````
  882. The setext heading underline cannot be a [lazy continuation
  883. line] in a list item or block quote:
  884. ```````````````````````````````` example
  885. > Foo
  886. ---
  887. .
  888. <blockquote>
  889. <p>Foo</p>
  890. </blockquote>
  891. <hr />
  892. ````````````````````````````````
  893. ```````````````````````````````` example
  894. > foo
  895. bar
  896. ===
  897. .
  898. <blockquote>
  899. <p>foo
  900. bar
  901. ===</p>
  902. </blockquote>
  903. ````````````````````````````````
  904. ```````````````````````````````` example
  905. - Foo
  906. ---
  907. .
  908. <ul>
  909. <li>Foo</li>
  910. </ul>
  911. <hr />
  912. ````````````````````````````````
  913. A blank line is needed between a paragraph and a following
  914. setext heading, since otherwise the paragraph becomes part
  915. of the heading's content:
  916. ```````````````````````````````` example
  917. Foo
  918. Bar
  919. ---
  920. .
  921. <h2>Foo
  922. Bar</h2>
  923. ````````````````````````````````
  924. But in general a blank line is not required before or after
  925. setext headings:
  926. ```````````````````````````````` example
  927. ---
  928. Foo
  929. ---
  930. Bar
  931. ---
  932. Baz
  933. .
  934. <hr />
  935. <h2>Foo</h2>
  936. <h2>Bar</h2>
  937. <p>Baz</p>
  938. ````````````````````````````````
  939. Setext headings cannot be empty:
  940. ```````````````````````````````` example
  941. ====
  942. .
  943. <p>====</p>
  944. ````````````````````````````````
  945. Setext heading text lines must not be interpretable as block
  946. constructs other than paragraphs. So, the line of dashes
  947. in these examples gets interpreted as a thematic break:
  948. ```````````````````````````````` example
  949. ---
  950. ---
  951. .
  952. <hr />
  953. <hr />
  954. ````````````````````````````````
  955. ```````````````````````````````` example
  956. - foo
  957. -----
  958. .
  959. <ul>
  960. <li>foo</li>
  961. </ul>
  962. <hr />
  963. ````````````````````````````````
  964. ```````````````````````````````` example
  965. foo
  966. ---
  967. .
  968. <pre><code>foo
  969. </code></pre>
  970. <hr />
  971. ````````````````````````````````
  972. ```````````````````````````````` example
  973. > foo
  974. -----
  975. .
  976. <blockquote>
  977. <p>foo</p>
  978. </blockquote>
  979. <hr />
  980. ````````````````````````````````
  981. If you want a heading with `> foo` as its literal text, you can
  982. use backslash escapes:
  983. ```````````````````````````````` example
  984. \> foo
  985. ------
  986. .
  987. <h2>&gt; foo</h2>
  988. ````````````````````````````````
  989. **Compatibility note:** Most existing Markdown implementations
  990. do not allow the text of setext headings to span multiple lines.
  991. But there is no consensus about how to interpret
  992. ``` markdown
  993. Foo
  994. bar
  995. ---
  996. baz
  997. ```
  998. One can find four different interpretations:
  999. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1000. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1001. 3. paragraph "Foo bar --- baz"
  1002. 4. heading "Foo bar", paragraph "baz"
  1003. We find interpretation 4 most natural, and interpretation 4
  1004. increases the expressive power of CommonMark, by allowing
  1005. multiline headings. Authors who want interpretation 1 can
  1006. put a blank line after the first paragraph:
  1007. ```````````````````````````````` example
  1008. Foo
  1009. bar
  1010. ---
  1011. baz
  1012. .
  1013. <p>Foo</p>
  1014. <h2>bar</h2>
  1015. <p>baz</p>
  1016. ````````````````````````````````
  1017. Authors who want interpretation 2 can put blank lines around
  1018. the thematic break,
  1019. ```````````````````````````````` example
  1020. Foo
  1021. bar
  1022. ---
  1023. baz
  1024. .
  1025. <p>Foo
  1026. bar</p>
  1027. <hr />
  1028. <p>baz</p>
  1029. ````````````````````````````````
  1030. or use a thematic break that cannot count as a [setext heading
  1031. underline], such as
  1032. ```````````````````````````````` example
  1033. Foo
  1034. bar
  1035. * * *
  1036. baz
  1037. .
  1038. <p>Foo
  1039. bar</p>
  1040. <hr />
  1041. <p>baz</p>
  1042. ````````````````````````````````
  1043. Authors who want interpretation 3 can use backslash escapes:
  1044. ```````````````````````````````` example
  1045. Foo
  1046. bar
  1047. \---
  1048. baz
  1049. .
  1050. <p>Foo
  1051. bar
  1052. ---
  1053. baz</p>
  1054. ````````````````````````````````
  1055. ## Indented code blocks
  1056. An [indented code block](@) is composed of one or more
  1057. [indented chunks] separated by blank lines.
  1058. An [indented chunk](@) is a sequence of non-blank lines,
  1059. each indented four or more spaces. The contents of the code block are
  1060. the literal contents of the lines, including trailing
  1061. [line endings], minus four spaces of indentation.
  1062. An indented code block has no [info string].
  1063. An indented code block cannot interrupt a paragraph, so there must be
  1064. a blank line between a paragraph and a following indented code block.
  1065. (A blank line is not needed, however, between a code block and a following
  1066. paragraph.)
  1067. ```````````````````````````````` example
  1068. a simple
  1069. indented code block
  1070. .
  1071. <pre><code>a simple
  1072. indented code block
  1073. </code></pre>
  1074. ````````````````````````````````
  1075. If there is any ambiguity between an interpretation of indentation
  1076. as a code block and as indicating that material belongs to a [list
  1077. item][list items], the list item interpretation takes precedence:
  1078. ```````````````````````````````` example
  1079. - foo
  1080. bar
  1081. .
  1082. <ul>
  1083. <li>
  1084. <p>foo</p>
  1085. <p>bar</p>
  1086. </li>
  1087. </ul>
  1088. ````````````````````````````````
  1089. ```````````````````````````````` example
  1090. 1. foo
  1091. - bar
  1092. .
  1093. <ol>
  1094. <li>
  1095. <p>foo</p>
  1096. <ul>
  1097. <li>bar</li>
  1098. </ul>
  1099. </li>
  1100. </ol>
  1101. ````````````````````````````````
  1102. The contents of a code block are literal text, and do not get parsed
  1103. as Markdown:
  1104. ```````````````````````````````` example
  1105. <a/>
  1106. *hi*
  1107. - one
  1108. .
  1109. <pre><code>&lt;a/&gt;
  1110. *hi*
  1111. - one
  1112. </code></pre>
  1113. ````````````````````````````````
  1114. Here we have three chunks separated by blank lines:
  1115. ```````````````````````````````` example
  1116. chunk1
  1117. chunk2
  1118. chunk3
  1119. .
  1120. <pre><code>chunk1
  1121. chunk2
  1122. chunk3
  1123. </code></pre>
  1124. ````````````````````````````````
  1125. Any initial spaces beyond four will be included in the content, even
  1126. in interior blank lines:
  1127. ```````````````````````````````` example
  1128. chunk1
  1129. chunk2
  1130. .
  1131. <pre><code>chunk1
  1132. chunk2
  1133. </code></pre>
  1134. ````````````````````````````````
  1135. An indented code block cannot interrupt a paragraph. (This
  1136. allows hanging indents and the like.)
  1137. ```````````````````````````````` example
  1138. Foo
  1139. bar
  1140. .
  1141. <p>Foo
  1142. bar</p>
  1143. ````````````````````````````````
  1144. However, any non-blank line with fewer than four leading spaces ends
  1145. the code block immediately. So a paragraph may occur immediately
  1146. after indented code:
  1147. ```````````````````````````````` example
  1148. foo
  1149. bar
  1150. .
  1151. <pre><code>foo
  1152. </code></pre>
  1153. <p>bar</p>
  1154. ````````````````````````````````
  1155. And indented code can occur immediately before and after other kinds of
  1156. blocks:
  1157. ```````````````````````````````` example
  1158. # Heading
  1159. foo
  1160. Heading
  1161. ------
  1162. foo
  1163. ----
  1164. .
  1165. <h1>Heading</h1>
  1166. <pre><code>foo
  1167. </code></pre>
  1168. <h2>Heading</h2>
  1169. <pre><code>foo
  1170. </code></pre>
  1171. <hr />
  1172. ````````````````````````````````
  1173. The first line can be indented more than four spaces:
  1174. ```````````````````````````````` example
  1175. foo
  1176. bar
  1177. .
  1178. <pre><code> foo
  1179. bar
  1180. </code></pre>
  1181. ````````````````````````````````
  1182. Blank lines preceding or following an indented code block
  1183. are not included in it:
  1184. ```````````````````````````````` example
  1185. foo
  1186. .
  1187. <pre><code>foo
  1188. </code></pre>
  1189. ````````````````````````````````
  1190. Trailing spaces are included in the code block's content:
  1191. ```````````````````````````````` example
  1192. foo
  1193. .
  1194. <pre><code>foo
  1195. </code></pre>
  1196. ````````````````````````````````
  1197. ## Fenced code blocks
  1198. A [code fence](@) is a sequence
  1199. of at least three consecutive backtick characters (`` ` ``) or
  1200. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1201. A [fenced code block](@)
  1202. begins with a code fence, indented no more than three spaces.
  1203. The line with the opening code fence may optionally contain some text
  1204. following the code fence; this is trimmed of leading and trailing
  1205. spaces and called the [info string](@).
  1206. The [info string] may not contain any backtick
  1207. characters. (The reason for this restriction is that otherwise
  1208. some inline code would be incorrectly interpreted as the
  1209. beginning of a fenced code block.)
  1210. The content of the code block consists of all subsequent lines, until
  1211. a closing [code fence] of the same type as the code block
  1212. began with (backticks or tildes), and with at least as many backticks
  1213. or tildes as the opening code fence. If the leading code fence is
  1214. indented N spaces, then up to N spaces of indentation are removed from
  1215. each line of the content (if present). (If a content line is not
  1216. indented, it is preserved unchanged. If it is indented less than N
  1217. spaces, all of the indentation is removed.)
  1218. The closing code fence may be indented up to three spaces, and may be
  1219. followed only by spaces, which are ignored. If the end of the
  1220. containing block (or document) is reached and no closing code fence
  1221. has been found, the code block contains all of the lines after the
  1222. opening code fence until the end of the containing block (or
  1223. document). (An alternative spec would require backtracking in the
  1224. event that a closing code fence is not found. But this makes parsing
  1225. much less efficient, and there seems to be no real down side to the
  1226. behavior described here.)
  1227. A fenced code block may interrupt a paragraph, and does not require
  1228. a blank line either before or after.
  1229. The content of a code fence is treated as literal text, not parsed
  1230. as inlines. The first word of the [info string] is typically used to
  1231. specify the language of the code sample, and rendered in the `class`
  1232. attribute of the `code` tag. However, this spec does not mandate any
  1233. particular treatment of the [info string].
  1234. Here is a simple example with backticks:
  1235. ```````````````````````````````` example
  1236. ```
  1237. <
  1238. >
  1239. ```
  1240. .
  1241. <pre><code>&lt;
  1242. &gt;
  1243. </code></pre>
  1244. ````````````````````````````````
  1245. With tildes:
  1246. ```````````````````````````````` example
  1247. ~~~
  1248. <
  1249. >
  1250. ~~~
  1251. .
  1252. <pre><code>&lt;
  1253. &gt;
  1254. </code></pre>
  1255. ````````````````````````````````
  1256. The closing code fence must use the same character as the opening
  1257. fence:
  1258. ```````````````````````````````` example
  1259. ```
  1260. aaa
  1261. ~~~
  1262. ```
  1263. .
  1264. <pre><code>aaa
  1265. ~~~
  1266. </code></pre>
  1267. ````````````````````````````````
  1268. ```````````````````````````````` example
  1269. ~~~
  1270. aaa
  1271. ```
  1272. ~~~
  1273. .
  1274. <pre><code>aaa
  1275. ```
  1276. </code></pre>
  1277. ````````````````````````````````
  1278. The closing code fence must be at least as long as the opening fence:
  1279. ```````````````````````````````` example
  1280. ````
  1281. aaa
  1282. ```
  1283. ``````
  1284. .
  1285. <pre><code>aaa
  1286. ```
  1287. </code></pre>
  1288. ````````````````````````````````
  1289. ```````````````````````````````` example
  1290. ~~~~
  1291. aaa
  1292. ~~~
  1293. ~~~~
  1294. .
  1295. <pre><code>aaa
  1296. ~~~
  1297. </code></pre>
  1298. ````````````````````````````````
  1299. Unclosed code blocks are closed by the end of the document
  1300. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1301. ```````````````````````````````` example
  1302. ```
  1303. .
  1304. <pre><code></code></pre>
  1305. ````````````````````````````````
  1306. ```````````````````````````````` example
  1307. `````
  1308. ```
  1309. aaa
  1310. .
  1311. <pre><code>
  1312. ```
  1313. aaa
  1314. </code></pre>
  1315. ````````````````````````````````
  1316. ```````````````````````````````` example
  1317. > ```
  1318. > aaa
  1319. bbb
  1320. .
  1321. <blockquote>
  1322. <pre><code>aaa
  1323. </code></pre>
  1324. </blockquote>
  1325. <p>bbb</p>
  1326. ````````````````````````````````
  1327. A code block can have all empty lines as its content:
  1328. ```````````````````````````````` example
  1329. ```
  1330. ```
  1331. .
  1332. <pre><code>
  1333. </code></pre>
  1334. ````````````````````````````````
  1335. A code block can be empty:
  1336. ```````````````````````````````` example
  1337. ```
  1338. ```
  1339. .
  1340. <pre><code></code></pre>
  1341. ````````````````````````````````
  1342. Fences can be indented. If the opening fence is indented,
  1343. content lines will have equivalent opening indentation removed,
  1344. if present:
  1345. ```````````````````````````````` example
  1346. ```
  1347. aaa
  1348. aaa
  1349. ```
  1350. .
  1351. <pre><code>aaa
  1352. aaa
  1353. </code></pre>
  1354. ````````````````````````````````
  1355. ```````````````````````````````` example
  1356. ```
  1357. aaa
  1358. aaa
  1359. aaa
  1360. ```
  1361. .
  1362. <pre><code>aaa
  1363. aaa
  1364. aaa
  1365. </code></pre>
  1366. ````````````````````````````````
  1367. ```````````````````````````````` example
  1368. ```
  1369. aaa
  1370. aaa
  1371. aaa
  1372. ```
  1373. .
  1374. <pre><code>aaa
  1375. aaa
  1376. aaa
  1377. </code></pre>
  1378. ````````````````````````````````
  1379. Four spaces indentation produces an indented code block:
  1380. ```````````````````````````````` example
  1381. ```
  1382. aaa
  1383. ```
  1384. .
  1385. <pre><code>```
  1386. aaa
  1387. ```
  1388. </code></pre>
  1389. ````````````````````````````````
  1390. Closing fences may be indented by 0-3 spaces, and their indentation
  1391. need not match that of the opening fence:
  1392. ```````````````````````````````` example
  1393. ```
  1394. aaa
  1395. ```
  1396. .
  1397. <pre><code>aaa
  1398. </code></pre>
  1399. ````````````````````````````````
  1400. ```````````````````````````````` example
  1401. ```
  1402. aaa
  1403. ```
  1404. .
  1405. <pre><code>aaa
  1406. </code></pre>
  1407. ````````````````````````````````
  1408. This is not a closing fence, because it is indented 4 spaces:
  1409. ```````````````````````````````` example
  1410. ```
  1411. aaa
  1412. ```
  1413. .
  1414. <pre><code>aaa
  1415. ```
  1416. </code></pre>
  1417. ````````````````````````````````
  1418. Code fences (opening and closing) cannot contain internal spaces:
  1419. ```````````````````````````````` example
  1420. ``` ```
  1421. aaa
  1422. .
  1423. <p><code></code>
  1424. aaa</p>
  1425. ````````````````````````````````
  1426. ```````````````````````````````` example
  1427. ~~~~~~
  1428. aaa
  1429. ~~~ ~~
  1430. .
  1431. <pre><code>aaa
  1432. ~~~ ~~
  1433. </code></pre>
  1434. ````````````````````````````````
  1435. Fenced code blocks can interrupt paragraphs, and can be followed
  1436. directly by paragraphs, without a blank line between:
  1437. ```````````````````````````````` example
  1438. foo
  1439. ```
  1440. bar
  1441. ```
  1442. baz
  1443. .
  1444. <p>foo</p>
  1445. <pre><code>bar
  1446. </code></pre>
  1447. <p>baz</p>
  1448. ````````````````````````````````
  1449. Other blocks can also occur before and after fenced code blocks
  1450. without an intervening blank line:
  1451. ```````````````````````````````` example
  1452. foo
  1453. ---
  1454. ~~~
  1455. bar
  1456. ~~~
  1457. # baz
  1458. .
  1459. <h2>foo</h2>
  1460. <pre><code>bar
  1461. </code></pre>
  1462. <h1>baz</h1>
  1463. ````````````````````````````````
  1464. An [info string] can be provided after the opening code fence.
  1465. Opening and closing spaces will be stripped, and the first word, prefixed
  1466. with `language-`, is used as the value for the `class` attribute of the
  1467. `code` element within the enclosing `pre` element.
  1468. ```````````````````````````````` example
  1469. ```ruby
  1470. def foo(x)
  1471. return 3
  1472. end
  1473. ```
  1474. .
  1475. <pre><code class="language-ruby">def foo(x)
  1476. return 3
  1477. end
  1478. </code></pre>
  1479. ````````````````````````````````
  1480. ```````````````````````````````` example
  1481. ~~~~ ruby startline=3 $%@#$
  1482. def foo(x)
  1483. return 3
  1484. end
  1485. ~~~~~~~
  1486. .
  1487. <pre><code class="language-ruby">def foo(x)
  1488. return 3
  1489. end
  1490. </code></pre>
  1491. ````````````````````````````````
  1492. ```````````````````````````````` example
  1493. ````;
  1494. ````
  1495. .
  1496. <pre><code class="language-;"></code></pre>
  1497. ````````````````````````````````
  1498. [Info strings] for backtick code blocks cannot contain backticks:
  1499. ```````````````````````````````` example
  1500. ``` aa ```
  1501. foo
  1502. .
  1503. <p><code>aa</code>
  1504. foo</p>
  1505. ````````````````````````````````
  1506. Closing code fences cannot have [info strings]:
  1507. ```````````````````````````````` example
  1508. ```
  1509. ``` aaa
  1510. ```
  1511. .
  1512. <pre><code>``` aaa
  1513. </code></pre>
  1514. ````````````````````````````````
  1515. ## HTML blocks
  1516. An [HTML block](@) is a group of lines that is treated
  1517. as raw HTML (and will not be escaped in HTML output).
  1518. There are seven kinds of [HTML block], which can be defined
  1519. by their start and end conditions. The block begins with a line that
  1520. meets a [start condition](@) (after up to three spaces
  1521. optional indentation). It ends with the first subsequent line that
  1522. meets a matching [end condition](@), or the last line of
  1523. the document or other [container block]), if no line is encountered that meets the
  1524. [end condition]. If the first line meets both the [start condition]
  1525. and the [end condition], the block will contain just that line.
  1526. 1. **Start condition:** line begins with the string `<script`,
  1527. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1528. the string `>`, or the end of the line.\
  1529. **End condition:** line contains an end tag
  1530. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1531. need not match the start tag).
  1532. 2. **Start condition:** line begins with the string `<!--`.\
  1533. **End condition:** line contains the string `-->`.
  1534. 3. **Start condition:** line begins with the string `<?`.\
  1535. **End condition:** line contains the string `?>`.
  1536. 4. **Start condition:** line begins with the string `<!`
  1537. followed by an uppercase ASCII letter.\
  1538. **End condition:** line contains the character `>`.
  1539. 5. **Start condition:** line begins with the string
  1540. `<![CDATA[`.\
  1541. **End condition:** line contains the string `]]>`.
  1542. 6. **Start condition:** line begins the string `<` or `</`
  1543. followed by one of the strings (case-insensitive) `address`,
  1544. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1545. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1546. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1547. `footer`, `form`, `frame`, `frameset`,
  1548. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1549. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1550. `meta`, `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1551. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1552. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1553. by [whitespace], the end of the line, the string `>`, or
  1554. the string `/>`.\
  1555. **End condition:** line is followed by a [blank line].
  1556. 7. **Start condition:** line begins with a complete [open tag]
  1557. or [closing tag] (with any [tag name] other than `script`,
  1558. `style`, or `pre`) followed only by [whitespace]
  1559. or the end of the line.\
  1560. **End condition:** line is followed by a [blank line].
  1561. All types of [HTML blocks] except type 7 may interrupt
  1562. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1563. (This restriction is intended to prevent unwanted interpretation
  1564. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1565. Some simple examples follow. Here are some basic HTML blocks
  1566. of type 6:
  1567. ```````````````````````````````` example
  1568. <table>
  1569. <tr>
  1570. <td>
  1571. hi
  1572. </td>
  1573. </tr>
  1574. </table>
  1575. okay.
  1576. .
  1577. <table>
  1578. <tr>
  1579. <td>
  1580. hi
  1581. </td>
  1582. </tr>
  1583. </table>
  1584. <p>okay.</p>
  1585. ````````````````````````````````
  1586. ```````````````````````````````` example
  1587. <div>
  1588. *hello*
  1589. <foo><a>
  1590. .
  1591. <div>
  1592. *hello*
  1593. <foo><a>
  1594. ````````````````````````````````
  1595. A block can also start with a closing tag:
  1596. ```````````````````````````````` example
  1597. </div>
  1598. *foo*
  1599. .
  1600. </div>
  1601. *foo*
  1602. ````````````````````````````````
  1603. Here we have two HTML blocks with a Markdown paragraph between them:
  1604. ```````````````````````````````` example
  1605. <DIV CLASS="foo">
  1606. *Markdown*
  1607. </DIV>
  1608. .
  1609. <DIV CLASS="foo">
  1610. <p><em>Markdown</em></p>
  1611. </DIV>
  1612. ````````````````````````````````
  1613. The tag on the first line can be partial, as long
  1614. as it is split where there would be whitespace:
  1615. ```````````````````````````````` example
  1616. <div id="foo"
  1617. class="bar">
  1618. </div>
  1619. .
  1620. <div id="foo"
  1621. class="bar">
  1622. </div>
  1623. ````````````````````````````````
  1624. ```````````````````````````````` example
  1625. <div id="foo" class="bar
  1626. baz">
  1627. </div>
  1628. .
  1629. <div id="foo" class="bar
  1630. baz">
  1631. </div>
  1632. ````````````````````````````````
  1633. An open tag need not be closed:
  1634. ```````````````````````````````` example
  1635. <div>
  1636. *foo*
  1637. *bar*
  1638. .
  1639. <div>
  1640. *foo*
  1641. <p><em>bar</em></p>
  1642. ````````````````````````````````
  1643. A partial tag need not even be completed (garbage
  1644. in, garbage out):
  1645. ```````````````````````````````` example
  1646. <div id="foo"
  1647. *hi*
  1648. .
  1649. <div id="foo"
  1650. *hi*
  1651. ````````````````````````````````
  1652. ```````````````````````````````` example
  1653. <div class
  1654. foo
  1655. .
  1656. <div class
  1657. foo
  1658. ````````````````````````````````
  1659. The initial tag doesn't even need to be a valid
  1660. tag, as long as it starts like one:
  1661. ```````````````````````````````` example
  1662. <div *???-&&&-<---
  1663. *foo*
  1664. .
  1665. <div *???-&&&-<---
  1666. *foo*
  1667. ````````````````````````````````
  1668. In type 6 blocks, the initial tag need not be on a line by
  1669. itself:
  1670. ```````````````````````````````` example
  1671. <div><a href="bar">*foo*</a></div>
  1672. .
  1673. <div><a href="bar">*foo*</a></div>
  1674. ````````````````````````````````
  1675. ```````````````````````````````` example
  1676. <table><tr><td>
  1677. foo
  1678. </td></tr></table>
  1679. .
  1680. <table><tr><td>
  1681. foo
  1682. </td></tr></table>
  1683. ````````````````````````````````
  1684. Everything until the next blank line or end of document
  1685. gets included in the HTML block. So, in the following
  1686. example, what looks like a Markdown code block
  1687. is actually part of the HTML block, which continues until a blank
  1688. line or the end of the document is reached:
  1689. ```````````````````````````````` example
  1690. <div></div>
  1691. ``` c
  1692. int x = 33;
  1693. ```
  1694. .
  1695. <div></div>
  1696. ``` c
  1697. int x = 33;
  1698. ```
  1699. ````````````````````````````````
  1700. To start an [HTML block] with a tag that is *not* in the
  1701. list of block-level tags in (6), you must put the tag by
  1702. itself on the first line (and it must be complete):
  1703. ```````````````````````````````` example
  1704. <a href="foo">
  1705. *bar*
  1706. </a>
  1707. .
  1708. <a href="foo">
  1709. *bar*
  1710. </a>
  1711. ````````````````````````````````
  1712. In type 7 blocks, the [tag name] can be anything:
  1713. ```````````````````````````````` example
  1714. <Warning>
  1715. *bar*
  1716. </Warning>
  1717. .
  1718. <Warning>
  1719. *bar*
  1720. </Warning>
  1721. ````````````````````````````````
  1722. ```````````````````````````````` example
  1723. <i class="foo">
  1724. *bar*
  1725. </i>
  1726. .
  1727. <i class="foo">
  1728. *bar*
  1729. </i>
  1730. ````````````````````````````````
  1731. ```````````````````````````````` example
  1732. </ins>
  1733. *bar*
  1734. .
  1735. </ins>
  1736. *bar*
  1737. ````````````````````````````````
  1738. These rules are designed to allow us to work with tags that
  1739. can function as either block-level or inline-level tags.
  1740. The `<del>` tag is a nice example. We can surround content with
  1741. `<del>` tags in three different ways. In this case, we get a raw
  1742. HTML block, because the `<del>` tag is on a line by itself:
  1743. ```````````````````````````````` example
  1744. <del>
  1745. *foo*
  1746. </del>
  1747. .
  1748. <del>
  1749. *foo*
  1750. </del>
  1751. ````````````````````````````````
  1752. In this case, we get a raw HTML block that just includes
  1753. the `<del>` tag (because it ends with the following blank
  1754. line). So the contents get interpreted as CommonMark:
  1755. ```````````````````````````````` example
  1756. <del>
  1757. *foo*
  1758. </del>
  1759. .
  1760. <del>
  1761. <p><em>foo</em></p>
  1762. </del>
  1763. ````````````````````````````````
  1764. Finally, in this case, the `<del>` tags are interpreted
  1765. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1766. the tag is not on a line by itself, we get inline HTML
  1767. rather than an [HTML block].)
  1768. ```````````````````````````````` example
  1769. <del>*foo*</del>
  1770. .
  1771. <p><del><em>foo</em></del></p>
  1772. ````````````````````````````````
  1773. HTML tags designed to contain literal content
  1774. (`script`, `style`, `pre`), comments, processing instructions,
  1775. and declarations are treated somewhat differently.
  1776. Instead of ending at the first blank line, these blocks
  1777. end at the first line containing a corresponding end tag.
  1778. As a result, these blocks can contain blank lines:
  1779. A pre tag (type 1):
  1780. ```````````````````````````````` example
  1781. <pre language="haskell"><code>
  1782. import Text.HTML.TagSoup
  1783. main :: IO ()
  1784. main = print $ parseTags tags
  1785. </code></pre>
  1786. okay
  1787. .
  1788. <pre language="haskell"><code>
  1789. import Text.HTML.TagSoup
  1790. main :: IO ()
  1791. main = print $ parseTags tags
  1792. </code></pre>
  1793. <p>okay</p>
  1794. ````````````````````````````````
  1795. A script tag (type 1):
  1796. ```````````````````````````````` example
  1797. <script type="text/javascript">
  1798. // JavaScript example
  1799. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1800. </script>
  1801. okay
  1802. .
  1803. <script type="text/javascript">
  1804. // JavaScript example
  1805. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1806. </script>
  1807. <p>okay</p>
  1808. ````````````````````````````````
  1809. A style tag (type 1):
  1810. ```````````````````````````````` example
  1811. <style
  1812. type="text/css">
  1813. h1 {color:red;}
  1814. p {color:blue;}
  1815. </style>
  1816. okay
  1817. .
  1818. <style
  1819. type="text/css">
  1820. h1 {color:red;}
  1821. p {color:blue;}
  1822. </style>
  1823. <p>okay</p>
  1824. ````````````````````````````````
  1825. If there is no matching end tag, the block will end at the
  1826. end of the document (or the enclosing [block quote][block quotes]
  1827. or [list item][list items]):
  1828. ```````````````````````````````` example
  1829. <style
  1830. type="text/css">
  1831. foo
  1832. .
  1833. <style
  1834. type="text/css">
  1835. foo
  1836. ````````````````````````````````
  1837. ```````````````````````````````` example
  1838. > <div>
  1839. > foo
  1840. bar
  1841. .
  1842. <blockquote>
  1843. <div>
  1844. foo
  1845. </blockquote>
  1846. <p>bar</p>
  1847. ````````````````````````````````
  1848. ```````````````````````````````` example
  1849. - <div>
  1850. - foo
  1851. .
  1852. <ul>
  1853. <li>
  1854. <div>
  1855. </li>
  1856. <li>foo</li>
  1857. </ul>
  1858. ````````````````````````````````
  1859. The end tag can occur on the same line as the start tag:
  1860. ```````````````````````````````` example
  1861. <style>p{color:red;}</style>
  1862. *foo*
  1863. .
  1864. <style>p{color:red;}</style>
  1865. <p><em>foo</em></p>
  1866. ````````````````````````````````
  1867. ```````````````````````````````` example
  1868. <!-- foo -->*bar*
  1869. *baz*
  1870. .
  1871. <!-- foo -->*bar*
  1872. <p><em>baz</em></p>
  1873. ````````````````````````````````
  1874. Note that anything on the last line after the
  1875. end tag will be included in the [HTML block]:
  1876. ```````````````````````````````` example
  1877. <script>
  1878. foo
  1879. </script>1. *bar*
  1880. .
  1881. <script>
  1882. foo
  1883. </script>1. *bar*
  1884. ````````````````````````````````
  1885. A comment (type 2):
  1886. ```````````````````````````````` example
  1887. <!-- Foo
  1888. bar
  1889. baz -->
  1890. okay
  1891. .
  1892. <!-- Foo
  1893. bar
  1894. baz -->
  1895. <p>okay</p>
  1896. ````````````````````````````````
  1897. A processing instruction (type 3):
  1898. ```````````````````````````````` example
  1899. <?php
  1900. echo '>';
  1901. ?>
  1902. okay
  1903. .
  1904. <?php
  1905. echo '>';
  1906. ?>
  1907. <p>okay</p>
  1908. ````````````````````````````````
  1909. A declaration (type 4):
  1910. ```````````````````````````````` example
  1911. <!DOCTYPE html>
  1912. .
  1913. <!DOCTYPE html>
  1914. ````````````````````````````````
  1915. CDATA (type 5):
  1916. ```````````````````````````````` example
  1917. <![CDATA[
  1918. function matchwo(a,b)
  1919. {
  1920. if (a < b && a < 0) then {
  1921. return 1;
  1922. } else {
  1923. return 0;
  1924. }
  1925. }
  1926. ]]>
  1927. okay
  1928. .
  1929. <![CDATA[
  1930. function matchwo(a,b)
  1931. {
  1932. if (a < b && a < 0) then {
  1933. return 1;
  1934. } else {
  1935. return 0;
  1936. }
  1937. }
  1938. ]]>
  1939. <p>okay</p>
  1940. ````````````````````````````````
  1941. The opening tag can be indented 1-3 spaces, but not 4:
  1942. ```````````````````````````````` example
  1943. <!-- foo -->
  1944. <!-- foo -->
  1945. .
  1946. <!-- foo -->
  1947. <pre><code>&lt;!-- foo --&gt;
  1948. </code></pre>
  1949. ````````````````````````````````
  1950. ```````````````````````````````` example
  1951. <div>
  1952. <div>
  1953. .
  1954. <div>
  1955. <pre><code>&lt;div&gt;
  1956. </code></pre>
  1957. ````````````````````````````````
  1958. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  1959. preceded by a blank line.
  1960. ```````````````````````````````` example
  1961. Foo
  1962. <div>
  1963. bar
  1964. </div>
  1965. .
  1966. <p>Foo</p>
  1967. <div>
  1968. bar
  1969. </div>
  1970. ````````````````````````````````
  1971. However, a following blank line is needed, except at the end of
  1972. a document, and except for blocks of types 1--5, above:
  1973. ```````````````````````````````` example
  1974. <div>
  1975. bar
  1976. </div>
  1977. *foo*
  1978. .
  1979. <div>
  1980. bar
  1981. </div>
  1982. *foo*
  1983. ````````````````````````````````
  1984. HTML blocks of type 7 cannot interrupt a paragraph:
  1985. ```````````````````````````````` example
  1986. Foo
  1987. <a href="bar">
  1988. baz
  1989. .
  1990. <p>Foo
  1991. <a href="bar">
  1992. baz</p>
  1993. ````````````````````````````````
  1994. This rule differs from John Gruber's original Markdown syntax
  1995. specification, which says:
  1996. > The only restrictions are that block-level HTML elements —
  1997. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  1998. > surrounding content by blank lines, and the start and end tags of the
  1999. > block should not be indented with tabs or spaces.
  2000. In some ways Gruber's rule is more restrictive than the one given
  2001. here:
  2002. - It requires that an HTML block be preceded by a blank line.
  2003. - It does not allow the start tag to be indented.
  2004. - It requires a matching end tag, which it also does not allow to
  2005. be indented.
  2006. Most Markdown implementations (including some of Gruber's own) do not
  2007. respect all of these restrictions.
  2008. There is one respect, however, in which Gruber's rule is more liberal
  2009. than the one given here, since it allows blank lines to occur inside
  2010. an HTML block. There are two reasons for disallowing them here.
  2011. First, it removes the need to parse balanced tags, which is
  2012. expensive and can require backtracking from the end of the document
  2013. if no matching end tag is found. Second, it provides a very simple
  2014. and flexible way of including Markdown content inside HTML tags:
  2015. simply separate the Markdown from the HTML using blank lines:
  2016. Compare:
  2017. ```````````````````````````````` example
  2018. <div>
  2019. *Emphasized* text.
  2020. </div>
  2021. .
  2022. <div>
  2023. <p><em>Emphasized</em> text.</p>
  2024. </div>
  2025. ````````````````````````````````
  2026. ```````````````````````````````` example
  2027. <div>
  2028. *Emphasized* text.
  2029. </div>
  2030. .
  2031. <div>
  2032. *Emphasized* text.
  2033. </div>
  2034. ````````````````````````````````
  2035. Some Markdown implementations have adopted a convention of
  2036. interpreting content inside tags as text if the open tag has
  2037. the attribute `markdown=1`. The rule given above seems a simpler and
  2038. more elegant way of achieving the same expressive power, which is also
  2039. much simpler to parse.
  2040. The main potential drawback is that one can no longer paste HTML
  2041. blocks into Markdown documents with 100% reliability. However,
  2042. *in most cases* this will work fine, because the blank lines in
  2043. HTML are usually followed by HTML block tags. For example:
  2044. ```````````````````````````````` example
  2045. <table>
  2046. <tr>
  2047. <td>
  2048. Hi
  2049. </td>
  2050. </tr>
  2051. </table>
  2052. .
  2053. <table>
  2054. <tr>
  2055. <td>
  2056. Hi
  2057. </td>
  2058. </tr>
  2059. </table>
  2060. ````````````````````````````````
  2061. There are problems, however, if the inner tags are indented
  2062. *and* separated by spaces, as then they will be interpreted as
  2063. an indented code block:
  2064. ```````````````````````````````` example
  2065. <table>
  2066. <tr>
  2067. <td>
  2068. Hi
  2069. </td>
  2070. </tr>
  2071. </table>
  2072. .
  2073. <table>
  2074. <tr>
  2075. <pre><code>&lt;td&gt;
  2076. Hi
  2077. &lt;/td&gt;
  2078. </code></pre>
  2079. </tr>
  2080. </table>
  2081. ````````````````````````````````
  2082. Fortunately, blank lines are usually not necessary and can be
  2083. deleted. The exception is inside `<pre>` tags, but as described
  2084. above, raw HTML blocks starting with `<pre>` *can* contain blank
  2085. lines.
  2086. ## Link reference definitions
  2087. A [link reference definition](@)
  2088. consists of a [link label], indented up to three spaces, followed
  2089. by a colon (`:`), optional [whitespace] (including up to one
  2090. [line ending]), a [link destination],
  2091. optional [whitespace] (including up to one
  2092. [line ending]), and an optional [link
  2093. title], which if it is present must be separated
  2094. from the [link destination] by [whitespace].
  2095. No further [non-whitespace characters] may occur on the line.
  2096. A [link reference definition]
  2097. does not correspond to a structural element of a document. Instead, it
  2098. defines a label which can be used in [reference links]
  2099. and reference-style [images] elsewhere in the document. [Link
  2100. reference definitions] can come either before or after the links that use
  2101. them.
  2102. ```````````````````````````````` example
  2103. [foo]: /url "title"
  2104. [foo]
  2105. .
  2106. <p><a href="/url" title="title">foo</a></p>
  2107. ````````````````````````````````
  2108. ```````````````````````````````` example
  2109. [foo]:
  2110. /url
  2111. 'the title'
  2112. [foo]
  2113. .
  2114. <p><a href="/url" title="the title">foo</a></p>
  2115. ````````````````````````````````
  2116. ```````````````````````````````` example
  2117. [Foo*bar\]]:my_(url) 'title (with parens)'
  2118. [Foo*bar\]]
  2119. .
  2120. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2121. ````````````````````````````````
  2122. ```````````````````````````````` example
  2123. [Foo bar]:
  2124. <my%20url>
  2125. 'title'
  2126. [Foo bar]
  2127. .
  2128. <p><a href="my%20url" title="title">Foo bar</a></p>
  2129. ````````````````````````````````
  2130. The title may extend over multiple lines:
  2131. ```````````````````````````````` example
  2132. [foo]: /url '
  2133. title
  2134. line1
  2135. line2
  2136. '
  2137. [foo]
  2138. .
  2139. <p><a href="/url" title="
  2140. title
  2141. line1
  2142. line2
  2143. ">foo</a></p>
  2144. ````````````````````````````````
  2145. However, it may not contain a [blank line]:
  2146. ```````````````````````````````` example
  2147. [foo]: /url 'title
  2148. with blank line'
  2149. [foo]
  2150. .
  2151. <p>[foo]: /url 'title</p>
  2152. <p>with blank line'</p>
  2153. <p>[foo]</p>
  2154. ````````````````````````````````
  2155. The title may be omitted:
  2156. ```````````````````````````````` example
  2157. [foo]:
  2158. /url
  2159. [foo]
  2160. .
  2161. <p><a href="/url">foo</a></p>
  2162. ````````````````````````````````
  2163. The link destination may not be omitted:
  2164. ```````````````````````````````` example
  2165. [foo]:
  2166. [foo]
  2167. .
  2168. <p>[foo]:</p>
  2169. <p>[foo]</p>
  2170. ````````````````````````````````
  2171. Both title and destination can contain backslash escapes
  2172. and literal backslashes:
  2173. ```````````````````````````````` example
  2174. [foo]: /url\bar\*baz "foo\"bar\baz"
  2175. [foo]
  2176. .
  2177. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2178. ````````````````````````````````
  2179. A link can come before its corresponding definition:
  2180. ```````````````````````````````` example
  2181. [foo]
  2182. [foo]: url
  2183. .
  2184. <p><a href="url">foo</a></p>
  2185. ````````````````````````````````
  2186. If there are several matching definitions, the first one takes
  2187. precedence:
  2188. ```````````````````````````````` example
  2189. [foo]
  2190. [foo]: first
  2191. [foo]: second
  2192. .
  2193. <p><a href="first">foo</a></p>
  2194. ````````````````````````````````
  2195. As noted in the section on [Links], matching of labels is
  2196. case-insensitive (see [matches]).
  2197. ```````````````````````````````` example
  2198. [FOO]: /url
  2199. [Foo]
  2200. .
  2201. <p><a href="/url">Foo</a></p>
  2202. ````````````````````````````````
  2203. ```````````````````````````````` example
  2204. [ΑΓΩ]: /φου
  2205. [αγω]
  2206. .
  2207. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2208. ````````````````````````````````
  2209. Here is a link reference definition with no corresponding link.
  2210. It contributes nothing to the document.
  2211. ```````````````````````````````` example
  2212. [foo]: /url
  2213. .
  2214. ````````````````````````````````
  2215. Here is another one:
  2216. ```````````````````````````````` example
  2217. [
  2218. foo
  2219. ]: /url
  2220. bar
  2221. .
  2222. <p>bar</p>
  2223. ````````````````````````````````
  2224. This is not a link reference definition, because there are
  2225. [non-whitespace characters] after the title:
  2226. ```````````````````````````````` example
  2227. [foo]: /url "title" ok
  2228. .
  2229. <p>[foo]: /url &quot;title&quot; ok</p>
  2230. ````````````````````````````````
  2231. This is a link reference definition, but it has no title:
  2232. ```````````````````````````````` example
  2233. [foo]: /url
  2234. "title" ok
  2235. .
  2236. <p>&quot;title&quot; ok</p>
  2237. ````````````````````````````````
  2238. This is not a link reference definition, because it is indented
  2239. four spaces:
  2240. ```````````````````````````````` example
  2241. [foo]: /url "title"
  2242. [foo]
  2243. .
  2244. <pre><code>[foo]: /url &quot;title&quot;
  2245. </code></pre>
  2246. <p>[foo]</p>
  2247. ````````````````````````````````
  2248. This is not a link reference definition, because it occurs inside
  2249. a code block:
  2250. ```````````````````````````````` example
  2251. ```
  2252. [foo]: /url
  2253. ```
  2254. [foo]
  2255. .
  2256. <pre><code>[foo]: /url
  2257. </code></pre>
  2258. <p>[foo]</p>
  2259. ````````````````````````````````
  2260. A [link reference definition] cannot interrupt a paragraph.
  2261. ```````````````````````````````` example
  2262. Foo
  2263. [bar]: /baz
  2264. [bar]
  2265. .
  2266. <p>Foo
  2267. [bar]: /baz</p>
  2268. <p>[bar]</p>
  2269. ````````````````````````````````
  2270. However, it can directly follow other block elements, such as headings
  2271. and thematic breaks, and it need not be followed by a blank line.
  2272. ```````````````````````````````` example
  2273. # [Foo]
  2274. [foo]: /url
  2275. > bar
  2276. .
  2277. <h1><a href="/url">Foo</a></h1>
  2278. <blockquote>
  2279. <p>bar</p>
  2280. </blockquote>
  2281. ````````````````````````````````
  2282. Several [link reference definitions]
  2283. can occur one after another, without intervening blank lines.
  2284. ```````````````````````````````` example
  2285. [foo]: /foo-url "foo"
  2286. [bar]: /bar-url
  2287. "bar"
  2288. [baz]: /baz-url
  2289. [foo],
  2290. [bar],
  2291. [baz]
  2292. .
  2293. <p><a href="/foo-url" title="foo">foo</a>,
  2294. <a href="/bar-url" title="bar">bar</a>,
  2295. <a href="/baz-url">baz</a></p>
  2296. ````````````````````````````````
  2297. [Link reference definitions] can occur
  2298. inside block containers, like lists and block quotations. They
  2299. affect the entire document, not just the container in which they
  2300. are defined:
  2301. ```````````````````````````````` example
  2302. [foo]
  2303. > [foo]: /url
  2304. .
  2305. <p><a href="/url">foo</a></p>
  2306. <blockquote>
  2307. </blockquote>
  2308. ````````````````````````````````
  2309. ## Paragraphs
  2310. A sequence of non-blank lines that cannot be interpreted as other
  2311. kinds of blocks forms a [paragraph](@).
  2312. The contents of the paragraph are the result of parsing the
  2313. paragraph's raw content as inlines. The paragraph's raw content
  2314. is formed by concatenating the lines and removing initial and final
  2315. [whitespace].
  2316. A simple example with two paragraphs:
  2317. ```````````````````````````````` example
  2318. aaa
  2319. bbb
  2320. .
  2321. <p>aaa</p>
  2322. <p>bbb</p>
  2323. ````````````````````````````````
  2324. Paragraphs can contain multiple lines, but no blank lines:
  2325. ```````````````````````````````` example
  2326. aaa
  2327. bbb
  2328. ccc
  2329. ddd
  2330. .
  2331. <p>aaa
  2332. bbb</p>
  2333. <p>ccc
  2334. ddd</p>
  2335. ````````````````````````````````
  2336. Multiple blank lines between paragraph have no effect:
  2337. ```````````````````````````````` example
  2338. aaa
  2339. bbb
  2340. .
  2341. <p>aaa</p>
  2342. <p>bbb</p>
  2343. ````````````````````````````````
  2344. Leading spaces are skipped:
  2345. ```````````````````````````````` example
  2346. aaa
  2347. bbb
  2348. .
  2349. <p>aaa
  2350. bbb</p>
  2351. ````````````````````````````````
  2352. Lines after the first may be indented any amount, since indented
  2353. code blocks cannot interrupt paragraphs.
  2354. ```````````````````````````````` example
  2355. aaa
  2356. bbb
  2357. ccc
  2358. .
  2359. <p>aaa
  2360. bbb
  2361. ccc</p>
  2362. ````````````````````````````````
  2363. However, the first line may be indented at most three spaces,
  2364. or an indented code block will be triggered:
  2365. ```````````````````````````````` example
  2366. aaa
  2367. bbb
  2368. .
  2369. <p>aaa
  2370. bbb</p>
  2371. ````````````````````````````````
  2372. ```````````````````````````````` example
  2373. aaa
  2374. bbb
  2375. .
  2376. <pre><code>aaa
  2377. </code></pre>
  2378. <p>bbb</p>
  2379. ````````````````````````````````
  2380. Final spaces are stripped before inline parsing, so a paragraph
  2381. that ends with two or more spaces will not end with a [hard line
  2382. break]:
  2383. ```````````````````````````````` example
  2384. aaa
  2385. bbb
  2386. .
  2387. <p>aaa<br />
  2388. bbb</p>
  2389. ````````````````````````````````
  2390. ## Blank lines
  2391. [Blank lines] between block-level elements are ignored,
  2392. except for the role they play in determining whether a [list]
  2393. is [tight] or [loose].
  2394. Blank lines at the beginning and end of the document are also ignored.
  2395. ```````````````````````````````` example
  2396. aaa
  2397. # aaa
  2398. .
  2399. <p>aaa</p>
  2400. <h1>aaa</h1>
  2401. ````````````````````````````````
  2402. # Container blocks
  2403. A [container block] is a block that has other
  2404. blocks as its contents. There are two basic kinds of container blocks:
  2405. [block quotes] and [list items].
  2406. [Lists] are meta-containers for [list items].
  2407. We define the syntax for container blocks recursively. The general
  2408. form of the definition is:
  2409. > If X is a sequence of blocks, then the result of
  2410. > transforming X in such-and-such a way is a container of type Y
  2411. > with these blocks as its content.
  2412. So, we explain what counts as a block quote or list item by explaining
  2413. how these can be *generated* from their contents. This should suffice
  2414. to define the syntax, although it does not give a recipe for *parsing*
  2415. these constructions. (A recipe is provided below in the section entitled
  2416. [A parsing strategy](#appendix-a-parsing-strategy).)
  2417. ## Block quotes
  2418. A [block quote marker](@)
  2419. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2420. with a following space, or (b) a single character `>` not followed by a space.
  2421. The following rules define [block quotes]:
  2422. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2423. of blocks *Bs*, then the result of prepending a [block quote
  2424. marker] to the beginning of each line in *Ls*
  2425. is a [block quote](#block-quotes) containing *Bs*.
  2426. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2427. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2428. the initial [block quote marker] from one or
  2429. more lines in which the next [non-whitespace character] after the [block
  2430. quote marker] is [paragraph continuation
  2431. text] is a block quote with *Bs* as its content.
  2432. [Paragraph continuation text](@) is text
  2433. that will be parsed as part of the content of a paragraph, but does
  2434. not occur at the beginning of the paragraph.
  2435. 3. **Consecutiveness.** A document cannot contain two [block
  2436. quotes] in a row unless there is a [blank line] between them.
  2437. Nothing else counts as a [block quote](#block-quotes).
  2438. Here is a simple example:
  2439. ```````````````````````````````` example
  2440. > # Foo
  2441. > bar
  2442. > baz
  2443. .
  2444. <blockquote>
  2445. <h1>Foo</h1>
  2446. <p>bar
  2447. baz</p>
  2448. </blockquote>
  2449. ````````````````````````````````
  2450. The spaces after the `>` characters can be omitted:
  2451. ```````````````````````````````` example
  2452. ># Foo
  2453. >bar
  2454. > baz
  2455. .
  2456. <blockquote>
  2457. <h1>Foo</h1>
  2458. <p>bar
  2459. baz</p>
  2460. </blockquote>
  2461. ````````````````````````````````
  2462. The `>` characters can be indented 1-3 spaces:
  2463. ```````````````````````````````` example
  2464. > # Foo
  2465. > bar
  2466. > baz
  2467. .
  2468. <blockquote>
  2469. <h1>Foo</h1>
  2470. <p>bar
  2471. baz</p>
  2472. </blockquote>
  2473. ````````````````````````````````
  2474. Four spaces gives us a code block:
  2475. ```````````````````````````````` example
  2476. > # Foo
  2477. > bar
  2478. > baz
  2479. .
  2480. <pre><code>&gt; # Foo
  2481. &gt; bar
  2482. &gt; baz
  2483. </code></pre>
  2484. ````````````````````````````````
  2485. The Laziness clause allows us to omit the `>` before
  2486. [paragraph continuation text]:
  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. A block quote can contain some lazy and some non-lazy
  2499. continuation lines:
  2500. ```````````````````````````````` example
  2501. > bar
  2502. baz
  2503. > foo
  2504. .
  2505. <blockquote>
  2506. <p>bar
  2507. baz
  2508. foo</p>
  2509. </blockquote>
  2510. ````````````````````````````````
  2511. Laziness only applies to lines that would have been continuations of
  2512. paragraphs had they been prepended with [block quote markers].
  2513. For example, the `> ` cannot be omitted in the second line of
  2514. ``` markdown
  2515. > foo
  2516. > ---
  2517. ```
  2518. without changing the meaning:
  2519. ```````````````````````````````` example
  2520. > foo
  2521. ---
  2522. .
  2523. <blockquote>
  2524. <p>foo</p>
  2525. </blockquote>
  2526. <hr />
  2527. ````````````````````````````````
  2528. Similarly, if we omit the `> ` in the second line of
  2529. ``` markdown
  2530. > - foo
  2531. > - bar
  2532. ```
  2533. then the block quote ends after the first line:
  2534. ```````````````````````````````` example
  2535. > - foo
  2536. - bar
  2537. .
  2538. <blockquote>
  2539. <ul>
  2540. <li>foo</li>
  2541. </ul>
  2542. </blockquote>
  2543. <ul>
  2544. <li>bar</li>
  2545. </ul>
  2546. ````````````````````````````````
  2547. For the same reason, we can't omit the `> ` in front of
  2548. subsequent lines of an indented or fenced code block:
  2549. ```````````````````````````````` example
  2550. > foo
  2551. bar
  2552. .
  2553. <blockquote>
  2554. <pre><code>foo
  2555. </code></pre>
  2556. </blockquote>
  2557. <pre><code>bar
  2558. </code></pre>
  2559. ````````````````````````````````
  2560. ```````````````````````````````` example
  2561. > ```
  2562. foo
  2563. ```
  2564. .
  2565. <blockquote>
  2566. <pre><code></code></pre>
  2567. </blockquote>
  2568. <p>foo</p>
  2569. <pre><code></code></pre>
  2570. ````````````````````````````````
  2571. Note that in the following case, we have a [lazy
  2572. continuation line]:
  2573. ```````````````````````````````` example
  2574. > foo
  2575. - bar
  2576. .
  2577. <blockquote>
  2578. <p>foo
  2579. - bar</p>
  2580. </blockquote>
  2581. ````````````````````````````````
  2582. To see why, note that in
  2583. ```markdown
  2584. > foo
  2585. > - bar
  2586. ```
  2587. the `- bar` is indented too far to start a list, and can't
  2588. be an indented code block because indented code blocks cannot
  2589. interrupt paragraphs, so it is [paragraph continuation text].
  2590. A block quote can be empty:
  2591. ```````````````````````````````` example
  2592. >
  2593. .
  2594. <blockquote>
  2595. </blockquote>
  2596. ````````````````````````````````
  2597. ```````````````````````````````` example
  2598. >
  2599. >
  2600. >
  2601. .
  2602. <blockquote>
  2603. </blockquote>
  2604. ````````````````````````````````
  2605. A block quote can have initial or final blank lines:
  2606. ```````````````````````````````` example
  2607. >
  2608. > foo
  2609. >
  2610. .
  2611. <blockquote>
  2612. <p>foo</p>
  2613. </blockquote>
  2614. ````````````````````````````````
  2615. A blank line always separates block quotes:
  2616. ```````````````````````````````` example
  2617. > foo
  2618. > bar
  2619. .
  2620. <blockquote>
  2621. <p>foo</p>
  2622. </blockquote>
  2623. <blockquote>
  2624. <p>bar</p>
  2625. </blockquote>
  2626. ````````````````````````````````
  2627. (Most current Markdown implementations, including John Gruber's
  2628. original `Markdown.pl`, will parse this example as a single block quote
  2629. with two paragraphs. But it seems better to allow the author to decide
  2630. whether two block quotes or one are wanted.)
  2631. Consecutiveness means that if we put these block quotes together,
  2632. we get a single block quote:
  2633. ```````````````````````````````` example
  2634. > foo
  2635. > bar
  2636. .
  2637. <blockquote>
  2638. <p>foo
  2639. bar</p>
  2640. </blockquote>
  2641. ````````````````````````````````
  2642. To get a block quote with two paragraphs, use:
  2643. ```````````````````````````````` example
  2644. > foo
  2645. >
  2646. > bar
  2647. .
  2648. <blockquote>
  2649. <p>foo</p>
  2650. <p>bar</p>
  2651. </blockquote>
  2652. ````````````````````````````````
  2653. Block quotes can interrupt paragraphs:
  2654. ```````````````````````````````` example
  2655. foo
  2656. > bar
  2657. .
  2658. <p>foo</p>
  2659. <blockquote>
  2660. <p>bar</p>
  2661. </blockquote>
  2662. ````````````````````````````````
  2663. In general, blank lines are not needed before or after block
  2664. quotes:
  2665. ```````````````````````````````` example
  2666. > aaa
  2667. ***
  2668. > bbb
  2669. .
  2670. <blockquote>
  2671. <p>aaa</p>
  2672. </blockquote>
  2673. <hr />
  2674. <blockquote>
  2675. <p>bbb</p>
  2676. </blockquote>
  2677. ````````````````````````````````
  2678. However, because of laziness, a blank line is needed between
  2679. a block quote and a following paragraph:
  2680. ```````````````````````````````` example
  2681. > bar
  2682. baz
  2683. .
  2684. <blockquote>
  2685. <p>bar
  2686. baz</p>
  2687. </blockquote>
  2688. ````````````````````````````````
  2689. ```````````````````````````````` example
  2690. > bar
  2691. baz
  2692. .
  2693. <blockquote>
  2694. <p>bar</p>
  2695. </blockquote>
  2696. <p>baz</p>
  2697. ````````````````````````````````
  2698. ```````````````````````````````` example
  2699. > bar
  2700. >
  2701. baz
  2702. .
  2703. <blockquote>
  2704. <p>bar</p>
  2705. </blockquote>
  2706. <p>baz</p>
  2707. ````````````````````````````````
  2708. It is a consequence of the Laziness rule that any number
  2709. of initial `>`s may be omitted on a continuation line of a
  2710. nested block quote:
  2711. ```````````````````````````````` example
  2712. > > > foo
  2713. bar
  2714. .
  2715. <blockquote>
  2716. <blockquote>
  2717. <blockquote>
  2718. <p>foo
  2719. bar</p>
  2720. </blockquote>
  2721. </blockquote>
  2722. </blockquote>
  2723. ````````````````````````````````
  2724. ```````````````````````````````` example
  2725. >>> foo
  2726. > bar
  2727. >>baz
  2728. .
  2729. <blockquote>
  2730. <blockquote>
  2731. <blockquote>
  2732. <p>foo
  2733. bar
  2734. baz</p>
  2735. </blockquote>
  2736. </blockquote>
  2737. </blockquote>
  2738. ````````````````````````````````
  2739. When including an indented code block in a block quote,
  2740. remember that the [block quote marker] includes
  2741. both the `>` and a following space. So *five spaces* are needed after
  2742. the `>`:
  2743. ```````````````````````````````` example
  2744. > code
  2745. > not code
  2746. .
  2747. <blockquote>
  2748. <pre><code>code
  2749. </code></pre>
  2750. </blockquote>
  2751. <blockquote>
  2752. <p>not code</p>
  2753. </blockquote>
  2754. ````````````````````````````````
  2755. ## List items
  2756. A [list marker](@) is a
  2757. [bullet list marker] or an [ordered list marker].
  2758. A [bullet list marker](@)
  2759. is a `-`, `+`, or `*` character.
  2760. An [ordered list marker](@)
  2761. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2762. `.` character or a `)` character. (The reason for the length
  2763. limit is that with 10 digits we start seeing integer overflows
  2764. in some browsers.)
  2765. The following rules define [list items]:
  2766. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2767. blocks *Bs* starting with a [non-whitespace character] and not separated
  2768. from each other by more than one blank line, and *M* is a list
  2769. marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2770. of prepending *M* and the following spaces to the first line of
  2771. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2772. list item with *Bs* as its contents. The type of the list item
  2773. (bullet or ordered) is determined by the type of its list marker.
  2774. If the list item is ordered, then it is also assigned a start
  2775. number, based on the ordered list marker.
  2776. Exceptions: When the list item interrupts a paragraph---that
  2777. is, when it starts on a line that would otherwise count as
  2778. [paragraph continuation text]---then (a) the lines *Ls* must
  2779. not begin with a blank line, and (b) if the list item is
  2780. ordered, the start number must be 1.
  2781. For example, let *Ls* be the lines
  2782. ```````````````````````````````` example
  2783. A paragraph
  2784. with two lines.
  2785. indented code
  2786. > A block quote.
  2787. .
  2788. <p>A paragraph
  2789. with two lines.</p>
  2790. <pre><code>indented code
  2791. </code></pre>
  2792. <blockquote>
  2793. <p>A block quote.</p>
  2794. </blockquote>
  2795. ````````````````````````````````
  2796. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2797. that the following is an ordered list item with start number 1,
  2798. and the same contents as *Ls*:
  2799. ```````````````````````````````` example
  2800. 1. A paragraph
  2801. with two lines.
  2802. indented code
  2803. > A block quote.
  2804. .
  2805. <ol>
  2806. <li>
  2807. <p>A paragraph
  2808. with two lines.</p>
  2809. <pre><code>indented code
  2810. </code></pre>
  2811. <blockquote>
  2812. <p>A block quote.</p>
  2813. </blockquote>
  2814. </li>
  2815. </ol>
  2816. ````````````````````````````````
  2817. The most important thing to notice is that the position of
  2818. the text after the list marker determines how much indentation
  2819. is needed in subsequent blocks in the list item. If the list
  2820. marker takes up two spaces, and there are three spaces between
  2821. the list marker and the next [non-whitespace character], then blocks
  2822. must be indented five spaces in order to fall under the list
  2823. item.
  2824. Here are some examples showing how far content must be indented to be
  2825. put under the list item:
  2826. ```````````````````````````````` example
  2827. - one
  2828. two
  2829. .
  2830. <ul>
  2831. <li>one</li>
  2832. </ul>
  2833. <p>two</p>
  2834. ````````````````````````````````
  2835. ```````````````````````````````` example
  2836. - one
  2837. two
  2838. .
  2839. <ul>
  2840. <li>
  2841. <p>one</p>
  2842. <p>two</p>
  2843. </li>
  2844. </ul>
  2845. ````````````````````````````````
  2846. ```````````````````````````````` example
  2847. - one
  2848. two
  2849. .
  2850. <ul>
  2851. <li>one</li>
  2852. </ul>
  2853. <pre><code> two
  2854. </code></pre>
  2855. ````````````````````````````````
  2856. ```````````````````````````````` example
  2857. - one
  2858. two
  2859. .
  2860. <ul>
  2861. <li>
  2862. <p>one</p>
  2863. <p>two</p>
  2864. </li>
  2865. </ul>
  2866. ````````````````````````````````
  2867. It is tempting to think of this in terms of columns: the continuation
  2868. blocks must be indented at least to the column of the first
  2869. [non-whitespace character] after the list marker. However, that is not quite right.
  2870. The spaces after the list marker determine how much relative indentation
  2871. is needed. Which column this indentation reaches will depend on
  2872. how the list item is embedded in other constructions, as shown by
  2873. this example:
  2874. ```````````````````````````````` example
  2875. > > 1. one
  2876. >>
  2877. >> two
  2878. .
  2879. <blockquote>
  2880. <blockquote>
  2881. <ol>
  2882. <li>
  2883. <p>one</p>
  2884. <p>two</p>
  2885. </li>
  2886. </ol>
  2887. </blockquote>
  2888. </blockquote>
  2889. ````````````````````````````````
  2890. Here `two` occurs in the same column as the list marker `1.`,
  2891. but is actually contained in the list item, because there is
  2892. sufficient indentation after the last containing blockquote marker.
  2893. The converse is also possible. In the following example, the word `two`
  2894. occurs far to the right of the initial text of the list item, `one`, but
  2895. it is not considered part of the list item, because it is not indented
  2896. far enough past the blockquote marker:
  2897. ```````````````````````````````` example
  2898. >>- one
  2899. >>
  2900. > > two
  2901. .
  2902. <blockquote>
  2903. <blockquote>
  2904. <ul>
  2905. <li>one</li>
  2906. </ul>
  2907. <p>two</p>
  2908. </blockquote>
  2909. </blockquote>
  2910. ````````````````````````````````
  2911. Note that at least one space is needed between the list marker and
  2912. any following content, so these are not list items:
  2913. ```````````````````````````````` example
  2914. -one
  2915. 2.two
  2916. .
  2917. <p>-one</p>
  2918. <p>2.two</p>
  2919. ````````````````````````````````
  2920. A list item may contain blocks that are separated by more than
  2921. one blank line.
  2922. ```````````````````````````````` example
  2923. - foo
  2924. bar
  2925. .
  2926. <ul>
  2927. <li>
  2928. <p>foo</p>
  2929. <p>bar</p>
  2930. </li>
  2931. </ul>
  2932. ````````````````````````````````
  2933. A list item may contain any kind of block:
  2934. ```````````````````````````````` example
  2935. 1. foo
  2936. ```
  2937. bar
  2938. ```
  2939. baz
  2940. > bam
  2941. .
  2942. <ol>
  2943. <li>
  2944. <p>foo</p>
  2945. <pre><code>bar
  2946. </code></pre>
  2947. <p>baz</p>
  2948. <blockquote>
  2949. <p>bam</p>
  2950. </blockquote>
  2951. </li>
  2952. </ol>
  2953. ````````````````````````````````
  2954. A list item that contains an indented code block will preserve
  2955. empty lines within the code block verbatim.
  2956. ```````````````````````````````` example
  2957. - Foo
  2958. bar
  2959. baz
  2960. .
  2961. <ul>
  2962. <li>
  2963. <p>Foo</p>
  2964. <pre><code>bar
  2965. baz
  2966. </code></pre>
  2967. </li>
  2968. </ul>
  2969. ````````````````````````````````
  2970. Note that ordered list start numbers must be nine digits or less:
  2971. ```````````````````````````````` example
  2972. 123456789. ok
  2973. .
  2974. <ol start="123456789">
  2975. <li>ok</li>
  2976. </ol>
  2977. ````````````````````````````````
  2978. ```````````````````````````````` example
  2979. 1234567890. not ok
  2980. .
  2981. <p>1234567890. not ok</p>
  2982. ````````````````````````````````
  2983. A start number may begin with 0s:
  2984. ```````````````````````````````` example
  2985. 0. ok
  2986. .
  2987. <ol start="0">
  2988. <li>ok</li>
  2989. </ol>
  2990. ````````````````````````````````
  2991. ```````````````````````````````` example
  2992. 003. ok
  2993. .
  2994. <ol start="3">
  2995. <li>ok</li>
  2996. </ol>
  2997. ````````````````````````````````
  2998. A start number may not be negative:
  2999. ```````````````````````````````` example
  3000. -1. not ok
  3001. .
  3002. <p>-1. not ok</p>
  3003. ````````````````````````````````
  3004. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3005. constitute a sequence of blocks *Bs* starting with an indented code
  3006. block and not separated from each other by more than one blank line,
  3007. and *M* is a list marker of width *W* followed by
  3008. one space, then the result of prepending *M* and the following
  3009. space to the first line of *Ls*, and indenting subsequent lines of
  3010. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3011. If a line is empty, then it need not be indented. The type of the
  3012. list item (bullet or ordered) is determined by the type of its list
  3013. marker. If the list item is ordered, then it is also assigned a
  3014. start number, based on the ordered list marker.
  3015. An indented code block will have to be indented four spaces beyond
  3016. the edge of the region where text will be included in the list item.
  3017. In the following case that is 6 spaces:
  3018. ```````````````````````````````` example
  3019. - foo
  3020. bar
  3021. .
  3022. <ul>
  3023. <li>
  3024. <p>foo</p>
  3025. <pre><code>bar
  3026. </code></pre>
  3027. </li>
  3028. </ul>
  3029. ````````````````````````````````
  3030. And in this case it is 11 spaces:
  3031. ```````````````````````````````` example
  3032. 10. foo
  3033. bar
  3034. .
  3035. <ol start="10">
  3036. <li>
  3037. <p>foo</p>
  3038. <pre><code>bar
  3039. </code></pre>
  3040. </li>
  3041. </ol>
  3042. ````````````````````````````````
  3043. If the *first* block in the list item is an indented code block,
  3044. then by rule #2, the contents must be indented *one* space after the
  3045. list marker:
  3046. ```````````````````````````````` example
  3047. indented code
  3048. paragraph
  3049. more code
  3050. .
  3051. <pre><code>indented code
  3052. </code></pre>
  3053. <p>paragraph</p>
  3054. <pre><code>more code
  3055. </code></pre>
  3056. ````````````````````````````````
  3057. ```````````````````````````````` example
  3058. 1. indented code
  3059. paragraph
  3060. more code
  3061. .
  3062. <ol>
  3063. <li>
  3064. <pre><code>indented code
  3065. </code></pre>
  3066. <p>paragraph</p>
  3067. <pre><code>more code
  3068. </code></pre>
  3069. </li>
  3070. </ol>
  3071. ````````````````````````````````
  3072. Note that an additional space indent is interpreted as space
  3073. inside the code block:
  3074. ```````````````````````````````` example
  3075. 1. indented code
  3076. paragraph
  3077. more code
  3078. .
  3079. <ol>
  3080. <li>
  3081. <pre><code> indented code
  3082. </code></pre>
  3083. <p>paragraph</p>
  3084. <pre><code>more code
  3085. </code></pre>
  3086. </li>
  3087. </ol>
  3088. ````````````````````````````````
  3089. Note that rules #1 and #2 only apply to two cases: (a) cases
  3090. in which the lines to be included in a list item begin with a
  3091. [non-whitespace character], and (b) cases in which
  3092. they begin with an indented code
  3093. block. In a case like the following, where the first block begins with
  3094. a three-space indent, the rules do not allow us to form a list item by
  3095. indenting the whole thing and prepending a list marker:
  3096. ```````````````````````````````` example
  3097. foo
  3098. bar
  3099. .
  3100. <p>foo</p>
  3101. <p>bar</p>
  3102. ````````````````````````````````
  3103. ```````````````````````````````` example
  3104. - foo
  3105. bar
  3106. .
  3107. <ul>
  3108. <li>foo</li>
  3109. </ul>
  3110. <p>bar</p>
  3111. ````````````````````````````````
  3112. This is not a significant restriction, because when a block begins
  3113. with 1-3 spaces indent, the indentation can always be removed without
  3114. a change in interpretation, allowing rule #1 to be applied. So, in
  3115. the above case:
  3116. ```````````````````````````````` example
  3117. - foo
  3118. bar
  3119. .
  3120. <ul>
  3121. <li>
  3122. <p>foo</p>
  3123. <p>bar</p>
  3124. </li>
  3125. </ul>
  3126. ````````````````````````````````
  3127. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3128. starting with a single [blank line] constitute a (possibly empty)
  3129. sequence of blocks *Bs*, not separated from each other by more than
  3130. one blank line, and *M* is a list marker of width *W*,
  3131. then the result of prepending *M* to the first line of *Ls*, and
  3132. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3133. item with *Bs* as its contents.
  3134. If a line is empty, then it need not be indented. The type of the
  3135. list item (bullet or ordered) is determined by the type of its list
  3136. marker. If the list item is ordered, then it is also assigned a
  3137. start number, based on the ordered list marker.
  3138. Here are some list items that start with a blank line but are not empty:
  3139. ```````````````````````````````` example
  3140. -
  3141. foo
  3142. -
  3143. ```
  3144. bar
  3145. ```
  3146. -
  3147. baz
  3148. .
  3149. <ul>
  3150. <li>foo</li>
  3151. <li>
  3152. <pre><code>bar
  3153. </code></pre>
  3154. </li>
  3155. <li>
  3156. <pre><code>baz
  3157. </code></pre>
  3158. </li>
  3159. </ul>
  3160. ````````````````````````````````
  3161. When the list item starts with a blank line, the number of spaces
  3162. following the list marker doesn't change the required indentation:
  3163. ```````````````````````````````` example
  3164. -
  3165. foo
  3166. .
  3167. <ul>
  3168. <li>foo</li>
  3169. </ul>
  3170. ````````````````````````````````
  3171. A list item can begin with at most one blank line.
  3172. In the following example, `foo` is not part of the list
  3173. item:
  3174. ```````````````````````````````` example
  3175. -
  3176. foo
  3177. .
  3178. <ul>
  3179. <li></li>
  3180. </ul>
  3181. <p>foo</p>
  3182. ````````````````````````````````
  3183. Here is an empty bullet list item:
  3184. ```````````````````````````````` example
  3185. - foo
  3186. -
  3187. - bar
  3188. .
  3189. <ul>
  3190. <li>foo</li>
  3191. <li></li>
  3192. <li>bar</li>
  3193. </ul>
  3194. ````````````````````````````````
  3195. It does not matter whether there are spaces following the [list marker]:
  3196. ```````````````````````````````` example
  3197. - foo
  3198. -
  3199. - bar
  3200. .
  3201. <ul>
  3202. <li>foo</li>
  3203. <li></li>
  3204. <li>bar</li>
  3205. </ul>
  3206. ````````````````````````````````
  3207. Here is an empty ordered list item:
  3208. ```````````````````````````````` example
  3209. 1. foo
  3210. 2.
  3211. 3. bar
  3212. .
  3213. <ol>
  3214. <li>foo</li>
  3215. <li></li>
  3216. <li>bar</li>
  3217. </ol>
  3218. ````````````````````````````````
  3219. A list may start or end with an empty list item:
  3220. ```````````````````````````````` example
  3221. *
  3222. .
  3223. <ul>
  3224. <li></li>
  3225. </ul>
  3226. ````````````````````````````````
  3227. However, an empty list item cannot interrupt a paragraph:
  3228. ```````````````````````````````` example
  3229. foo
  3230. *
  3231. foo
  3232. 1.
  3233. .
  3234. <p>foo
  3235. *</p>
  3236. <p>foo
  3237. 1.</p>
  3238. ````````````````````````````````
  3239. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3240. according to rule #1, #2, or #3, then the result of indenting each line
  3241. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3242. list item with the same contents and attributes. If a line is
  3243. empty, then it need not be indented.
  3244. Indented one space:
  3245. ```````````````````````````````` example
  3246. 1. A paragraph
  3247. with two lines.
  3248. indented code
  3249. > A block quote.
  3250. .
  3251. <ol>
  3252. <li>
  3253. <p>A paragraph
  3254. with two lines.</p>
  3255. <pre><code>indented code
  3256. </code></pre>
  3257. <blockquote>
  3258. <p>A block quote.</p>
  3259. </blockquote>
  3260. </li>
  3261. </ol>
  3262. ````````````````````````````````
  3263. Indented two spaces:
  3264. ```````````````````````````````` example
  3265. 1. A paragraph
  3266. with two lines.
  3267. indented code
  3268. > A block quote.
  3269. .
  3270. <ol>
  3271. <li>
  3272. <p>A paragraph
  3273. with two lines.</p>
  3274. <pre><code>indented code
  3275. </code></pre>
  3276. <blockquote>
  3277. <p>A block quote.</p>
  3278. </blockquote>
  3279. </li>
  3280. </ol>
  3281. ````````````````````````````````
  3282. Indented three spaces:
  3283. ```````````````````````````````` example
  3284. 1. A paragraph
  3285. with two lines.
  3286. indented code
  3287. > A block quote.
  3288. .
  3289. <ol>
  3290. <li>
  3291. <p>A paragraph
  3292. with two lines.</p>
  3293. <pre><code>indented code
  3294. </code></pre>
  3295. <blockquote>
  3296. <p>A block quote.</p>
  3297. </blockquote>
  3298. </li>
  3299. </ol>
  3300. ````````````````````````````````
  3301. Four spaces indent gives a code block:
  3302. ```````````````````````````````` example
  3303. 1. A paragraph
  3304. with two lines.
  3305. indented code
  3306. > A block quote.
  3307. .
  3308. <pre><code>1. A paragraph
  3309. with two lines.
  3310. indented code
  3311. &gt; A block quote.
  3312. </code></pre>
  3313. ````````````````````````````````
  3314. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3315. item](#list-items) with contents *Bs*, then the result of deleting
  3316. some or all of the indentation from one or more lines in which the
  3317. next [non-whitespace character] after the indentation is
  3318. [paragraph continuation text] is a
  3319. list item with the same contents and attributes. The unindented
  3320. lines are called
  3321. [lazy continuation line](@)s.
  3322. Here is an example with [lazy continuation lines]:
  3323. ```````````````````````````````` example
  3324. 1. A paragraph
  3325. with two lines.
  3326. indented code
  3327. > A block quote.
  3328. .
  3329. <ol>
  3330. <li>
  3331. <p>A paragraph
  3332. with two lines.</p>
  3333. <pre><code>indented code
  3334. </code></pre>
  3335. <blockquote>
  3336. <p>A block quote.</p>
  3337. </blockquote>
  3338. </li>
  3339. </ol>
  3340. ````````````````````````````````
  3341. Indentation can be partially deleted:
  3342. ```````````````````````````````` example
  3343. 1. A paragraph
  3344. with two lines.
  3345. .
  3346. <ol>
  3347. <li>A paragraph
  3348. with two lines.</li>
  3349. </ol>
  3350. ````````````````````````````````
  3351. These examples show how laziness can work in nested structures:
  3352. ```````````````````````````````` example
  3353. > 1. > Blockquote
  3354. continued here.
  3355. .
  3356. <blockquote>
  3357. <ol>
  3358. <li>
  3359. <blockquote>
  3360. <p>Blockquote
  3361. continued here.</p>
  3362. </blockquote>
  3363. </li>
  3364. </ol>
  3365. </blockquote>
  3366. ````````````````````````````````
  3367. ```````````````````````````````` example
  3368. > 1. > Blockquote
  3369. > continued here.
  3370. .
  3371. <blockquote>
  3372. <ol>
  3373. <li>
  3374. <blockquote>
  3375. <p>Blockquote
  3376. continued here.</p>
  3377. </blockquote>
  3378. </li>
  3379. </ol>
  3380. </blockquote>
  3381. ````````````````````````````````
  3382. 6. **That's all.** Nothing that is not counted as a list item by rules
  3383. #1--5 counts as a [list item](#list-items).
  3384. The rules for sublists follow from the general rules above. A sublist
  3385. must be indented the same number of spaces a paragraph would need to be
  3386. in order to be included in the list item.
  3387. So, in this case we need two spaces indent:
  3388. ```````````````````````````````` example
  3389. - foo
  3390. - bar
  3391. - baz
  3392. - boo
  3393. .
  3394. <ul>
  3395. <li>foo
  3396. <ul>
  3397. <li>bar
  3398. <ul>
  3399. <li>baz
  3400. <ul>
  3401. <li>boo</li>
  3402. </ul>
  3403. </li>
  3404. </ul>
  3405. </li>
  3406. </ul>
  3407. </li>
  3408. </ul>
  3409. ````````````````````````````````
  3410. One is not enough:
  3411. ```````````````````````````````` example
  3412. - foo
  3413. - bar
  3414. - baz
  3415. - boo
  3416. .
  3417. <ul>
  3418. <li>foo</li>
  3419. <li>bar</li>
  3420. <li>baz</li>
  3421. <li>boo</li>
  3422. </ul>
  3423. ````````````````````````````````
  3424. Here we need four, because the list marker is wider:
  3425. ```````````````````````````````` example
  3426. 10) foo
  3427. - bar
  3428. .
  3429. <ol start="10">
  3430. <li>foo
  3431. <ul>
  3432. <li>bar</li>
  3433. </ul>
  3434. </li>
  3435. </ol>
  3436. ````````````````````````````````
  3437. Three is not enough:
  3438. ```````````````````````````````` example
  3439. 10) foo
  3440. - bar
  3441. .
  3442. <ol start="10">
  3443. <li>foo</li>
  3444. </ol>
  3445. <ul>
  3446. <li>bar</li>
  3447. </ul>
  3448. ````````````````````````````````
  3449. A list may be the first block in a list item:
  3450. ```````````````````````````````` example
  3451. - - foo
  3452. .
  3453. <ul>
  3454. <li>
  3455. <ul>
  3456. <li>foo</li>
  3457. </ul>
  3458. </li>
  3459. </ul>
  3460. ````````````````````````````````
  3461. ```````````````````````````````` example
  3462. 1. - 2. foo
  3463. .
  3464. <ol>
  3465. <li>
  3466. <ul>
  3467. <li>
  3468. <ol start="2">
  3469. <li>foo</li>
  3470. </ol>
  3471. </li>
  3472. </ul>
  3473. </li>
  3474. </ol>
  3475. ````````````````````````````````
  3476. A list item can contain a heading:
  3477. ```````````````````````````````` example
  3478. - # Foo
  3479. - Bar
  3480. ---
  3481. baz
  3482. .
  3483. <ul>
  3484. <li>
  3485. <h1>Foo</h1>
  3486. </li>
  3487. <li>
  3488. <h2>Bar</h2>
  3489. baz</li>
  3490. </ul>
  3491. ````````````````````````````````
  3492. ### Motivation
  3493. John Gruber's Markdown spec says the following about list items:
  3494. 1. "List markers typically start at the left margin, but may be indented
  3495. by up to three spaces. List markers must be followed by one or more
  3496. spaces or a tab."
  3497. 2. "To make lists look nice, you can wrap items with hanging indents....
  3498. But if you don't want to, you don't have to."
  3499. 3. "List items may consist of multiple paragraphs. Each subsequent
  3500. paragraph in a list item must be indented by either 4 spaces or one
  3501. tab."
  3502. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3503. but here again, Markdown will allow you to be lazy."
  3504. 5. "To put a blockquote within a list item, the blockquote's `>`
  3505. delimiters need to be indented."
  3506. 6. "To put a code block within a list item, the code block needs to be
  3507. indented twice — 8 spaces or two tabs."
  3508. These rules specify that a paragraph under a list item must be indented
  3509. four spaces (presumably, from the left margin, rather than the start of
  3510. the list marker, but this is not said), and that code under a list item
  3511. must be indented eight spaces instead of the usual four. They also say
  3512. that a block quote must be indented, but not by how much; however, the
  3513. example given has four spaces indentation. Although nothing is said
  3514. about other kinds of block-level content, it is certainly reasonable to
  3515. infer that *all* block elements under a list item, including other
  3516. lists, must be indented four spaces. This principle has been called the
  3517. *four-space rule*.
  3518. The four-space rule is clear and principled, and if the reference
  3519. implementation `Markdown.pl` had followed it, it probably would have
  3520. become the standard. However, `Markdown.pl` allowed paragraphs and
  3521. sublists to start with only two spaces indentation, at least on the
  3522. outer level. Worse, its behavior was inconsistent: a sublist of an
  3523. outer-level list needed two spaces indentation, but a sublist of this
  3524. sublist needed three spaces. It is not surprising, then, that different
  3525. implementations of Markdown have developed very different rules for
  3526. determining what comes under a list item. (Pandoc and python-Markdown,
  3527. for example, stuck with Gruber's syntax description and the four-space
  3528. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3529. followed `Markdown.pl`'s behavior more closely.)
  3530. Unfortunately, given the divergences between implementations, there
  3531. is no way to give a spec for list items that will be guaranteed not
  3532. to break any existing documents. However, the spec given here should
  3533. correctly handle lists formatted with either the four-space rule or
  3534. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3535. in a way that is natural for a human to read.
  3536. The strategy here is to let the width and indentation of the list marker
  3537. determine the indentation necessary for blocks to fall under the list
  3538. item, rather than having a fixed and arbitrary number. The writer can
  3539. think of the body of the list item as a unit which gets indented to the
  3540. right enough to fit the list marker (and any indentation on the list
  3541. marker). (The laziness rule, #5, then allows continuation lines to be
  3542. unindented if needed.)
  3543. This rule is superior, we claim, to any rule requiring a fixed level of
  3544. indentation from the margin. The four-space rule is clear but
  3545. unnatural. It is quite unintuitive that
  3546. ``` markdown
  3547. - foo
  3548. bar
  3549. - baz
  3550. ```
  3551. should be parsed as two lists with an intervening paragraph,
  3552. ``` html
  3553. <ul>
  3554. <li>foo</li>
  3555. </ul>
  3556. <p>bar</p>
  3557. <ul>
  3558. <li>baz</li>
  3559. </ul>
  3560. ```
  3561. as the four-space rule demands, rather than a single list,
  3562. ``` html
  3563. <ul>
  3564. <li>
  3565. <p>foo</p>
  3566. <p>bar</p>
  3567. <ul>
  3568. <li>baz</li>
  3569. </ul>
  3570. </li>
  3571. </ul>
  3572. ```
  3573. The choice of four spaces is arbitrary. It can be learned, but it is
  3574. not likely to be guessed, and it trips up beginners regularly.
  3575. Would it help to adopt a two-space rule? The problem is that such
  3576. a rule, together with the rule allowing 1--3 spaces indentation of the
  3577. initial list marker, allows text that is indented *less than* the
  3578. original list marker to be included in the list item. For example,
  3579. `Markdown.pl` parses
  3580. ``` markdown
  3581. - one
  3582. two
  3583. ```
  3584. as a single list item, with `two` a continuation paragraph:
  3585. ``` html
  3586. <ul>
  3587. <li>
  3588. <p>one</p>
  3589. <p>two</p>
  3590. </li>
  3591. </ul>
  3592. ```
  3593. and similarly
  3594. ``` markdown
  3595. > - one
  3596. >
  3597. > two
  3598. ```
  3599. as
  3600. ``` html
  3601. <blockquote>
  3602. <ul>
  3603. <li>
  3604. <p>one</p>
  3605. <p>two</p>
  3606. </li>
  3607. </ul>
  3608. </blockquote>
  3609. ```
  3610. This is extremely unintuitive.
  3611. Rather than requiring a fixed indent from the margin, we could require
  3612. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3613. may itself be indented). This proposal would remove the last anomaly
  3614. discussed. Unlike the spec presented above, it would count the following
  3615. as a list item with a subparagraph, even though the paragraph `bar`
  3616. is not indented as far as the first paragraph `foo`:
  3617. ``` markdown
  3618. 10. foo
  3619. bar
  3620. ```
  3621. Arguably this text does read like a list item with `bar` as a subparagraph,
  3622. which may count in favor of the proposal. However, on this proposal indented
  3623. code would have to be indented six spaces after the list marker. And this
  3624. would break a lot of existing Markdown, which has the pattern:
  3625. ``` markdown
  3626. 1. foo
  3627. indented code
  3628. ```
  3629. where the code is indented eight spaces. The spec above, by contrast, will
  3630. parse this text as expected, since the code block's indentation is measured
  3631. from the beginning of `foo`.
  3632. The one case that needs special treatment is a list item that *starts*
  3633. with indented code. How much indentation is required in that case, since
  3634. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3635. that in such cases, we require one space indentation from the list marker
  3636. (and then the normal four spaces for the indented code). This will match the
  3637. four-space rule in cases where the list marker plus its initial indentation
  3638. takes four spaces (a common case), but diverge in other cases.
  3639. ## Lists
  3640. A [list](@) is a sequence of one or more
  3641. list items [of the same type]. The list items
  3642. may be separated by any number of blank lines.
  3643. Two list items are [of the same type](@)
  3644. if they begin with a [list marker] of the same type.
  3645. Two list markers are of the
  3646. same type if (a) they are bullet list markers using the same character
  3647. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3648. delimiter (either `.` or `)`).
  3649. A list is an [ordered list](@)
  3650. if its constituent list items begin with
  3651. [ordered list markers], and a
  3652. [bullet list](@) if its constituent list
  3653. items begin with [bullet list markers].
  3654. The [start number](@)
  3655. of an [ordered list] is determined by the list number of
  3656. its initial list item. The numbers of subsequent list items are
  3657. disregarded.
  3658. A list is [loose](@) if any of its constituent
  3659. list items are separated by blank lines, or if any of its constituent
  3660. list items directly contain two block-level elements with a blank line
  3661. between them. Otherwise a list is [tight](@).
  3662. (The difference in HTML output is that paragraphs in a loose list are
  3663. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3664. Changing the bullet or ordered list delimiter starts a new list:
  3665. ```````````````````````````````` example
  3666. - foo
  3667. - bar
  3668. + baz
  3669. .
  3670. <ul>
  3671. <li>foo</li>
  3672. <li>bar</li>
  3673. </ul>
  3674. <ul>
  3675. <li>baz</li>
  3676. </ul>
  3677. ````````````````````````````````
  3678. ```````````````````````````````` example
  3679. 1. foo
  3680. 2. bar
  3681. 3) baz
  3682. .
  3683. <ol>
  3684. <li>foo</li>
  3685. <li>bar</li>
  3686. </ol>
  3687. <ol start="3">
  3688. <li>baz</li>
  3689. </ol>
  3690. ````````````````````````````````
  3691. In CommonMark, a list can interrupt a paragraph. That is,
  3692. no blank line is needed to separate a paragraph from a following
  3693. list:
  3694. ```````````````````````````````` example
  3695. Foo
  3696. - bar
  3697. - baz
  3698. .
  3699. <p>Foo</p>
  3700. <ul>
  3701. <li>bar</li>
  3702. <li>baz</li>
  3703. </ul>
  3704. ````````````````````````````````
  3705. `Markdown.pl` does not allow this, through fear of triggering a list
  3706. via a numeral in a hard-wrapped line:
  3707. ``` markdown
  3708. The number of windows in my house is
  3709. 14. The number of doors is 6.
  3710. ```
  3711. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3712. interrupt a paragraph, even though the same considerations might
  3713. apply.
  3714. In CommonMark, we do allow lists to interrupt paragraphs, for
  3715. two reasons. First, it is natural and not uncommon for people
  3716. to start lists without blank lines:
  3717. ``` markdown
  3718. I need to buy
  3719. - new shoes
  3720. - a coat
  3721. - a plane ticket
  3722. ```
  3723. Second, we are attracted to a
  3724. > [principle of uniformity](@):
  3725. > if a chunk of text has a certain
  3726. > meaning, it will continue to have the same meaning when put into a
  3727. > container block (such as a list item or blockquote).
  3728. (Indeed, the spec for [list items] and [block quotes] presupposes
  3729. this principle.) This principle implies that if
  3730. ``` markdown
  3731. * I need to buy
  3732. - new shoes
  3733. - a coat
  3734. - a plane ticket
  3735. ```
  3736. is a list item containing a paragraph followed by a nested sublist,
  3737. as all Markdown implementations agree it is (though the paragraph
  3738. may be rendered without `<p>` tags, since the list is "tight"),
  3739. then
  3740. ``` markdown
  3741. I need to buy
  3742. - new shoes
  3743. - a coat
  3744. - a plane ticket
  3745. ```
  3746. by itself should be a paragraph followed by a nested sublist.
  3747. Since it is well established Markdown practice to allow lists to
  3748. interrupt paragraphs inside list items, the [principle of
  3749. uniformity] requires us to allow this outside list items as
  3750. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3751. takes a different approach, requiring blank lines before lists
  3752. even inside other list items.)
  3753. In order to solve of unwanted lists in paragraphs with
  3754. hard-wrapped numerals, we allow only lists starting with `1` to
  3755. interrupt paragraphs. Thus,
  3756. ```````````````````````````````` example
  3757. The number of windows in my house is
  3758. 14. The number of doors is 6.
  3759. .
  3760. <p>The number of windows in my house is
  3761. 14. The number of doors is 6.</p>
  3762. ````````````````````````````````
  3763. We may still get an unintended result in cases like
  3764. ```````````````````````````````` example
  3765. The number of windows in my house is
  3766. 1. The number of doors is 6.
  3767. .
  3768. <p>The number of windows in my house is</p>
  3769. <ol>
  3770. <li>The number of doors is 6.</li>
  3771. </ol>
  3772. ````````````````````````````````
  3773. but this rule should prevent most spurious list captures.
  3774. There can be any number of blank lines between items:
  3775. ```````````````````````````````` example
  3776. - foo
  3777. - bar
  3778. - baz
  3779. .
  3780. <ul>
  3781. <li>
  3782. <p>foo</p>
  3783. </li>
  3784. <li>
  3785. <p>bar</p>
  3786. </li>
  3787. <li>
  3788. <p>baz</p>
  3789. </li>
  3790. </ul>
  3791. ````````````````````````````````
  3792. ```````````````````````````````` example
  3793. - foo
  3794. - bar
  3795. - baz
  3796. bim
  3797. .
  3798. <ul>
  3799. <li>foo
  3800. <ul>
  3801. <li>bar
  3802. <ul>
  3803. <li>
  3804. <p>baz</p>
  3805. <p>bim</p>
  3806. </li>
  3807. </ul>
  3808. </li>
  3809. </ul>
  3810. </li>
  3811. </ul>
  3812. ````````````````````````````````
  3813. To separate consecutive lists of the same type, or to separate a
  3814. list from an indented code block that would otherwise be parsed
  3815. as a subparagraph of the final list item, you can insert a blank HTML
  3816. comment:
  3817. ```````````````````````````````` example
  3818. - foo
  3819. - bar
  3820. <!-- -->
  3821. - baz
  3822. - bim
  3823. .
  3824. <ul>
  3825. <li>foo</li>
  3826. <li>bar</li>
  3827. </ul>
  3828. <!-- -->
  3829. <ul>
  3830. <li>baz</li>
  3831. <li>bim</li>
  3832. </ul>
  3833. ````````````````````````````````
  3834. ```````````````````````````````` example
  3835. - foo
  3836. notcode
  3837. - foo
  3838. <!-- -->
  3839. code
  3840. .
  3841. <ul>
  3842. <li>
  3843. <p>foo</p>
  3844. <p>notcode</p>
  3845. </li>
  3846. <li>
  3847. <p>foo</p>
  3848. </li>
  3849. </ul>
  3850. <!-- -->
  3851. <pre><code>code
  3852. </code></pre>
  3853. ````````````````````````````````
  3854. List items need not be indented to the same level. The following
  3855. list items will be treated as items at the same list level,
  3856. since none is indented enough to belong to the previous list
  3857. item:
  3858. ```````````````````````````````` example
  3859. - a
  3860. - b
  3861. - c
  3862. - d
  3863. - e
  3864. - f
  3865. - g
  3866. - h
  3867. - i
  3868. .
  3869. <ul>
  3870. <li>a</li>
  3871. <li>b</li>
  3872. <li>c</li>
  3873. <li>d</li>
  3874. <li>e</li>
  3875. <li>f</li>
  3876. <li>g</li>
  3877. <li>h</li>
  3878. <li>i</li>
  3879. </ul>
  3880. ````````````````````````````````
  3881. ```````````````````````````````` example
  3882. 1. a
  3883. 2. b
  3884. 3. c
  3885. .
  3886. <ol>
  3887. <li>
  3888. <p>a</p>
  3889. </li>
  3890. <li>
  3891. <p>b</p>
  3892. </li>
  3893. <li>
  3894. <p>c</p>
  3895. </li>
  3896. </ol>
  3897. ````````````````````````````````
  3898. This is a loose list, because there is a blank line between
  3899. two of the list items:
  3900. ```````````````````````````````` example
  3901. - a
  3902. - b
  3903. - c
  3904. .
  3905. <ul>
  3906. <li>
  3907. <p>a</p>
  3908. </li>
  3909. <li>
  3910. <p>b</p>
  3911. </li>
  3912. <li>
  3913. <p>c</p>
  3914. </li>
  3915. </ul>
  3916. ````````````````````````````````
  3917. So is this, with a empty second item:
  3918. ```````````````````````````````` example
  3919. * a
  3920. *
  3921. * c
  3922. .
  3923. <ul>
  3924. <li>
  3925. <p>a</p>
  3926. </li>
  3927. <li></li>
  3928. <li>
  3929. <p>c</p>
  3930. </li>
  3931. </ul>
  3932. ````````````````````````````````
  3933. These are loose lists, even though there is no space between the items,
  3934. because one of the items directly contains two block-level elements
  3935. with a blank line between them:
  3936. ```````````````````````````````` example
  3937. - a
  3938. - b
  3939. c
  3940. - d
  3941. .
  3942. <ul>
  3943. <li>
  3944. <p>a</p>
  3945. </li>
  3946. <li>
  3947. <p>b</p>
  3948. <p>c</p>
  3949. </li>
  3950. <li>
  3951. <p>d</p>
  3952. </li>
  3953. </ul>
  3954. ````````````````````````````````
  3955. ```````````````````````````````` example
  3956. - a
  3957. - b
  3958. [ref]: /url
  3959. - d
  3960. .
  3961. <ul>
  3962. <li>
  3963. <p>a</p>
  3964. </li>
  3965. <li>
  3966. <p>b</p>
  3967. </li>
  3968. <li>
  3969. <p>d</p>
  3970. </li>
  3971. </ul>
  3972. ````````````````````````````````
  3973. This is a tight list, because the blank lines are in a code block:
  3974. ```````````````````````````````` example
  3975. - a
  3976. - ```
  3977. b
  3978. ```
  3979. - c
  3980. .
  3981. <ul>
  3982. <li>a</li>
  3983. <li>
  3984. <pre><code>b
  3985. </code></pre>
  3986. </li>
  3987. <li>c</li>
  3988. </ul>
  3989. ````````````````````````````````
  3990. This is a tight list, because the blank line is between two
  3991. paragraphs of a sublist. So the sublist is loose while
  3992. the outer list is tight:
  3993. ```````````````````````````````` example
  3994. - a
  3995. - b
  3996. c
  3997. - d
  3998. .
  3999. <ul>
  4000. <li>a
  4001. <ul>
  4002. <li>
  4003. <p>b</p>
  4004. <p>c</p>
  4005. </li>
  4006. </ul>
  4007. </li>
  4008. <li>d</li>
  4009. </ul>
  4010. ````````````````````````````````
  4011. This is a tight list, because the blank line is inside the
  4012. block quote:
  4013. ```````````````````````````````` example
  4014. * a
  4015. > b
  4016. >
  4017. * c
  4018. .
  4019. <ul>
  4020. <li>a
  4021. <blockquote>
  4022. <p>b</p>
  4023. </blockquote>
  4024. </li>
  4025. <li>c</li>
  4026. </ul>
  4027. ````````````````````````````````
  4028. This list is tight, because the consecutive block elements
  4029. are not separated by blank lines:
  4030. ```````````````````````````````` example
  4031. - a
  4032. > b
  4033. ```
  4034. c
  4035. ```
  4036. - d
  4037. .
  4038. <ul>
  4039. <li>a
  4040. <blockquote>
  4041. <p>b</p>
  4042. </blockquote>
  4043. <pre><code>c
  4044. </code></pre>
  4045. </li>
  4046. <li>d</li>
  4047. </ul>
  4048. ````````````````````````````````
  4049. A single-paragraph list is tight:
  4050. ```````````````````````````````` example
  4051. - a
  4052. .
  4053. <ul>
  4054. <li>a</li>
  4055. </ul>
  4056. ````````````````````````````````
  4057. ```````````````````````````````` example
  4058. - a
  4059. - b
  4060. .
  4061. <ul>
  4062. <li>a
  4063. <ul>
  4064. <li>b</li>
  4065. </ul>
  4066. </li>
  4067. </ul>
  4068. ````````````````````````````````
  4069. This list is loose, because of the blank line between the
  4070. two block elements in the list item:
  4071. ```````````````````````````````` example
  4072. 1. ```
  4073. foo
  4074. ```
  4075. bar
  4076. .
  4077. <ol>
  4078. <li>
  4079. <pre><code>foo
  4080. </code></pre>
  4081. <p>bar</p>
  4082. </li>
  4083. </ol>
  4084. ````````````````````````````````
  4085. Here the outer list is loose, the inner list tight:
  4086. ```````````````````````````````` example
  4087. * foo
  4088. * bar
  4089. baz
  4090. .
  4091. <ul>
  4092. <li>
  4093. <p>foo</p>
  4094. <ul>
  4095. <li>bar</li>
  4096. </ul>
  4097. <p>baz</p>
  4098. </li>
  4099. </ul>
  4100. ````````````````````````````````
  4101. ```````````````````````````````` example
  4102. - a
  4103. - b
  4104. - c
  4105. - d
  4106. - e
  4107. - f
  4108. .
  4109. <ul>
  4110. <li>
  4111. <p>a</p>
  4112. <ul>
  4113. <li>b</li>
  4114. <li>c</li>
  4115. </ul>
  4116. </li>
  4117. <li>
  4118. <p>d</p>
  4119. <ul>
  4120. <li>e</li>
  4121. <li>f</li>
  4122. </ul>
  4123. </li>
  4124. </ul>
  4125. ````````````````````````````````
  4126. # Inlines
  4127. Inlines are parsed sequentially from the beginning of the character
  4128. stream to the end (left to right, in left-to-right languages).
  4129. Thus, for example, in
  4130. ```````````````````````````````` example
  4131. `hi`lo`
  4132. .
  4133. <p><code>hi</code>lo`</p>
  4134. ````````````````````````````````
  4135. `hi` is parsed as code, leaving the backtick at the end as a literal
  4136. backtick.
  4137. ## Backslash escapes
  4138. Any ASCII punctuation character may be backslash-escaped:
  4139. ```````````````````````````````` example
  4140. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4141. .
  4142. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4143. ````````````````````````````````
  4144. Backslashes before other characters are treated as literal
  4145. backslashes:
  4146. ```````````````````````````````` example
  4147. \→\A\a\ \3\φ\«
  4148. .
  4149. <p>\→\A\a\ \3\φ\«</p>
  4150. ````````````````````````````````
  4151. Escaped characters are treated as regular characters and do
  4152. not have their usual Markdown meanings:
  4153. ```````````````````````````````` example
  4154. \*not emphasized*
  4155. \<br/> not a tag
  4156. \[not a link](/foo)
  4157. \`not code`
  4158. 1\. not a list
  4159. \* not a list
  4160. \# not a heading
  4161. \[foo]: /url "not a reference"
  4162. .
  4163. <p>*not emphasized*
  4164. &lt;br/&gt; not a tag
  4165. [not a link](/foo)
  4166. `not code`
  4167. 1. not a list
  4168. * not a list
  4169. # not a heading
  4170. [foo]: /url &quot;not a reference&quot;</p>
  4171. ````````````````````````````````
  4172. If a backslash is itself escaped, the following character is not:
  4173. ```````````````````````````````` example
  4174. \\*emphasis*
  4175. .
  4176. <p>\<em>emphasis</em></p>
  4177. ````````````````````````````````
  4178. A backslash at the end of the line is a [hard line break]:
  4179. ```````````````````````````````` example
  4180. foo\
  4181. bar
  4182. .
  4183. <p>foo<br />
  4184. bar</p>
  4185. ````````````````````````````````
  4186. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4187. raw HTML:
  4188. ```````````````````````````````` example
  4189. `` \[\` ``
  4190. .
  4191. <p><code>\[\`</code></p>
  4192. ````````````````````````````````
  4193. ```````````````````````````````` example
  4194. \[\]
  4195. .
  4196. <pre><code>\[\]
  4197. </code></pre>
  4198. ````````````````````````````````
  4199. ```````````````````````````````` example
  4200. ~~~
  4201. \[\]
  4202. ~~~
  4203. .
  4204. <pre><code>\[\]
  4205. </code></pre>
  4206. ````````````````````````````````
  4207. ```````````````````````````````` example
  4208. <http://example.com?find=\*>
  4209. .
  4210. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4211. ````````````````````````````````
  4212. ```````````````````````````````` example
  4213. <a href="/bar\/)">
  4214. .
  4215. <a href="/bar\/)">
  4216. ````````````````````````````````
  4217. But they work in all other contexts, including URLs and link titles,
  4218. link references, and [info strings] in [fenced code blocks]:
  4219. ```````````````````````````````` example
  4220. [foo](/bar\* "ti\*tle")
  4221. .
  4222. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4223. ````````````````````````````````
  4224. ```````````````````````````````` example
  4225. [foo]
  4226. [foo]: /bar\* "ti\*tle"
  4227. .
  4228. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4229. ````````````````````````````````
  4230. ```````````````````````````````` example
  4231. ``` foo\+bar
  4232. foo
  4233. ```
  4234. .
  4235. <pre><code class="language-foo+bar">foo
  4236. </code></pre>
  4237. ````````````````````````````````
  4238. ## Entity and numeric character references
  4239. All valid HTML entity references and numeric character
  4240. references, except those occuring in code blocks and code spans,
  4241. are recognized as such and treated as equivalent to the
  4242. corresponding Unicode characters. Conforming CommonMark parsers
  4243. need not store information about whether a particular character
  4244. was represented in the source using a Unicode character or
  4245. an entity reference.
  4246. [Entity references](@) consist of `&` + any of the valid
  4247. HTML5 entity names + `;`. The
  4248. document <https://html.spec.whatwg.org/multipage/entities.json>
  4249. is used as an authoritative source for the valid entity
  4250. references and their corresponding code points.
  4251. ```````````````````````````````` example
  4252. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4253. &frac34; &HilbertSpace; &DifferentialD;
  4254. &ClockwiseContourIntegral; &ngE;
  4255. .
  4256. <p>  &amp; © Æ Ď
  4257. ¾ ℋ ⅆ
  4258. ∲ ≧̸</p>
  4259. ````````````````````````````````
  4260. [Decimal numeric character
  4261. references](@)
  4262. consist of `&#` + a string of 1--8 arabic digits + `;`. A
  4263. numeric character reference is parsed as the corresponding
  4264. Unicode character. Invalid Unicode code points will be replaced by
  4265. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4266. the code point `U+0000` will also be replaced by `U+FFFD`.
  4267. ```````````````````````````````` example
  4268. &#35; &#1234; &#992; &#98765432; &#0;
  4269. .
  4270. <p># Ӓ Ϡ � �</p>
  4271. ````````````````````````````````
  4272. [Hexadecimal numeric character
  4273. references](@) consist of `&#` +
  4274. either `X` or `x` + a string of 1-8 hexadecimal digits + `;`.
  4275. They too are parsed as the corresponding Unicode character (this
  4276. time specified with a hexadecimal numeral instead of decimal).
  4277. ```````````````````````````````` example
  4278. &#X22; &#XD06; &#xcab;
  4279. .
  4280. <p>&quot; ആ ಫ</p>
  4281. ````````````````````````````````
  4282. Here are some nonentities:
  4283. ```````````````````````````````` example
  4284. &nbsp &x; &#; &#x;
  4285. &ThisIsNotDefined; &hi?;
  4286. .
  4287. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4288. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4289. ````````````````````````````````
  4290. Although HTML5 does accept some entity references
  4291. without a trailing semicolon (such as `&copy`), these are not
  4292. recognized here, because it makes the grammar too ambiguous:
  4293. ```````````````````````````````` example
  4294. &copy
  4295. .
  4296. <p>&amp;copy</p>
  4297. ````````````````````````````````
  4298. Strings that are not on the list of HTML5 named entities are not
  4299. recognized as entity references either:
  4300. ```````````````````````````````` example
  4301. &MadeUpEntity;
  4302. .
  4303. <p>&amp;MadeUpEntity;</p>
  4304. ````````````````````````````````
  4305. Entity and numeric character references are recognized in any
  4306. context besides code spans or code blocks, including
  4307. URLs, [link titles], and [fenced code block][] [info strings]:
  4308. ```````````````````````````````` example
  4309. <a href="&ouml;&ouml;.html">
  4310. .
  4311. <a href="&ouml;&ouml;.html">
  4312. ````````````````````````````````
  4313. ```````````````````````````````` example
  4314. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4315. .
  4316. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4317. ````````````````````````````````
  4318. ```````````````````````````````` example
  4319. [foo]
  4320. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4321. .
  4322. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4323. ````````````````````````````````
  4324. ```````````````````````````````` example
  4325. ``` f&ouml;&ouml;
  4326. foo
  4327. ```
  4328. .
  4329. <pre><code class="language-föö">foo
  4330. </code></pre>
  4331. ````````````````````````````````
  4332. Entity and numeric character references are treated as literal
  4333. text in code spans and code blocks:
  4334. ```````````````````````````````` example
  4335. `f&ouml;&ouml;`
  4336. .
  4337. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4338. ````````````````````````````````
  4339. ```````````````````````````````` example
  4340. f&ouml;f&ouml;
  4341. .
  4342. <pre><code>f&amp;ouml;f&amp;ouml;
  4343. </code></pre>
  4344. ````````````````````````````````
  4345. ## Code spans
  4346. A [backtick string](@)
  4347. is a string of one or more backtick characters (`` ` ``) that is neither
  4348. preceded nor followed by a backtick.
  4349. A [code span](@) begins with a backtick string and ends with
  4350. a backtick string of equal length. The contents of the code span are
  4351. the characters between the two backtick strings, with leading and
  4352. trailing spaces and [line endings] removed, and
  4353. [whitespace] collapsed to single spaces.
  4354. This is a simple code span:
  4355. ```````````````````````````````` example
  4356. `foo`
  4357. .
  4358. <p><code>foo</code></p>
  4359. ````````````````````````````````
  4360. Here two backticks are used, because the code contains a backtick.
  4361. This example also illustrates stripping of leading and trailing spaces:
  4362. ```````````````````````````````` example
  4363. `` foo ` bar ``
  4364. .
  4365. <p><code>foo ` bar</code></p>
  4366. ````````````````````````````````
  4367. This example shows the motivation for stripping leading and trailing
  4368. spaces:
  4369. ```````````````````````````````` example
  4370. ` `` `
  4371. .
  4372. <p><code>``</code></p>
  4373. ````````````````````````````````
  4374. [Line endings] are treated like spaces:
  4375. ```````````````````````````````` example
  4376. ``
  4377. foo
  4378. ``
  4379. .
  4380. <p><code>foo</code></p>
  4381. ````````````````````````````````
  4382. Interior spaces and [line endings] are collapsed into
  4383. single spaces, just as they would be by a browser:
  4384. ```````````````````````````````` example
  4385. `foo bar
  4386. baz`
  4387. .
  4388. <p><code>foo bar baz</code></p>
  4389. ````````````````````````````````
  4390. Not all [Unicode whitespace] (for instance, non-breaking space) is
  4391. collapsed, however:
  4392. ```````````````````````````````` example
  4393. `a  b`
  4394. .
  4395. <p><code>a  b</code></p>
  4396. ````````````````````````````````
  4397. Q: Why not just leave the spaces, since browsers will collapse them
  4398. anyway? A: Because we might be targeting a non-HTML format, and we
  4399. shouldn't rely on HTML-specific rendering assumptions.
  4400. (Existing implementations differ in their treatment of internal
  4401. spaces and [line endings]. Some, including `Markdown.pl` and
  4402. `showdown`, convert an internal [line ending] into a
  4403. `<br />` tag. But this makes things difficult for those who like to
  4404. hard-wrap their paragraphs, since a line break in the midst of a code
  4405. span will cause an unintended line break in the output. Others just
  4406. leave internal spaces as they are, which is fine if only HTML is being
  4407. targeted.)
  4408. ```````````````````````````````` example
  4409. `foo `` bar`
  4410. .
  4411. <p><code>foo `` bar</code></p>
  4412. ````````````````````````````````
  4413. Note that backslash escapes do not work in code spans. All backslashes
  4414. are treated literally:
  4415. ```````````````````````````````` example
  4416. `foo\`bar`
  4417. .
  4418. <p><code>foo\</code>bar`</p>
  4419. ````````````````````````````````
  4420. Backslash escapes are never needed, because one can always choose a
  4421. string of *n* backtick characters as delimiters, where the code does
  4422. not contain any strings of exactly *n* backtick characters.
  4423. Code span backticks have higher precedence than any other inline
  4424. constructs except HTML tags and autolinks. Thus, for example, this is
  4425. not parsed as emphasized text, since the second `*` is part of a code
  4426. span:
  4427. ```````````````````````````````` example
  4428. *foo`*`
  4429. .
  4430. <p>*foo<code>*</code></p>
  4431. ````````````````````````````````
  4432. And this is not parsed as a link:
  4433. ```````````````````````````````` example
  4434. [not a `link](/foo`)
  4435. .
  4436. <p>[not a <code>link](/foo</code>)</p>
  4437. ````````````````````````````````
  4438. Code spans, HTML tags, and autolinks have the same precedence.
  4439. Thus, this is code:
  4440. ```````````````````````````````` example
  4441. `<a href="`">`
  4442. .
  4443. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4444. ````````````````````````````````
  4445. But this is an HTML tag:
  4446. ```````````````````````````````` example
  4447. <a href="`">`
  4448. .
  4449. <p><a href="`">`</p>
  4450. ````````````````````````````````
  4451. And this is code:
  4452. ```````````````````````````````` example
  4453. `<http://foo.bar.`baz>`
  4454. .
  4455. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4456. ````````````````````````````````
  4457. But this is an autolink:
  4458. ```````````````````````````````` example
  4459. <http://foo.bar.`baz>`
  4460. .
  4461. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4462. ````````````````````````````````
  4463. When a backtick string is not closed by a matching backtick string,
  4464. we just have literal backticks:
  4465. ```````````````````````````````` example
  4466. ```foo``
  4467. .
  4468. <p>```foo``</p>
  4469. ````````````````````````````````
  4470. ```````````````````````````````` example
  4471. `foo
  4472. .
  4473. <p>`foo</p>
  4474. ````````````````````````````````
  4475. ## Emphasis and strong emphasis
  4476. John Gruber's original [Markdown syntax
  4477. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4478. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4479. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4480. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4481. > tag.
  4482. This is enough for most users, but these rules leave much undecided,
  4483. especially when it comes to nested emphasis. The original
  4484. `Markdown.pl` test suite makes it clear that triple `***` and
  4485. `___` delimiters can be used for strong emphasis, and most
  4486. implementations have also allowed the following patterns:
  4487. ``` markdown
  4488. ***strong emph***
  4489. ***strong** in emph*
  4490. ***emph* in strong**
  4491. **in strong *emph***
  4492. *in emph **strong***
  4493. ```
  4494. The following patterns are less widely supported, but the intent
  4495. is clear and they are useful (especially in contexts like bibliography
  4496. entries):
  4497. ``` markdown
  4498. *emph *with emph* in it*
  4499. **strong **with strong** in it**
  4500. ```
  4501. Many implementations have also restricted intraword emphasis to
  4502. the `*` forms, to avoid unwanted emphasis in words containing
  4503. internal underscores. (It is best practice to put these in code
  4504. spans, but users often do not.)
  4505. ``` markdown
  4506. internal emphasis: foo*bar*baz
  4507. no emphasis: foo_bar_baz
  4508. ```
  4509. The rules given below capture all of these patterns, while allowing
  4510. for efficient parsing strategies that do not backtrack.
  4511. First, some definitions. A [delimiter run](@) is either
  4512. a sequence of one or more `*` characters that is not preceded or
  4513. followed by a `*` character, or a sequence of one or more `_`
  4514. characters that is not preceded or followed by a `_` character.
  4515. A [left-flanking delimiter run](@) is
  4516. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4517. and (b) either not followed by a [punctuation character], or
  4518. preceded by [Unicode whitespace] or a [punctuation character].
  4519. For purposes of this definition, the beginning and the end of
  4520. the line count as Unicode whitespace.
  4521. A [right-flanking delimiter run](@) is
  4522. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4523. and (b) either not preceded by a [punctuation character], or
  4524. followed by [Unicode whitespace] or a [punctuation character].
  4525. For purposes of this definition, the beginning and the end of
  4526. the line count as Unicode whitespace.
  4527. Here are some examples of delimiter runs.
  4528. - left-flanking but not right-flanking:
  4529. ```
  4530. ***abc
  4531. _abc
  4532. **"abc"
  4533. _"abc"
  4534. ```
  4535. - right-flanking but not left-flanking:
  4536. ```
  4537. abc***
  4538. abc_
  4539. "abc"**
  4540. "abc"_
  4541. ```
  4542. - Both left and right-flanking:
  4543. ```
  4544. abc***def
  4545. "abc"_"def"
  4546. ```
  4547. - Neither left nor right-flanking:
  4548. ```
  4549. abc *** def
  4550. a _ b
  4551. ```
  4552. (The idea of distinguishing left-flanking and right-flanking
  4553. delimiter runs based on the character before and the character
  4554. after comes from Roopesh Chander's
  4555. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4556. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4557. run," and its rules for distinguishing left- and right-flanking runs
  4558. are a bit more complex than the ones given here.)
  4559. The following rules define emphasis and strong emphasis:
  4560. 1. A single `*` character [can open emphasis](@)
  4561. iff (if and only if) it is part of a [left-flanking delimiter run].
  4562. 2. A single `_` character [can open emphasis] iff
  4563. it is part of a [left-flanking delimiter run]
  4564. and either (a) not part of a [right-flanking delimiter run]
  4565. or (b) part of a [right-flanking delimiter run]
  4566. preceded by punctuation.
  4567. 3. A single `*` character [can close emphasis](@)
  4568. iff it is part of a [right-flanking delimiter run].
  4569. 4. A single `_` character [can close emphasis] iff
  4570. it is part of a [right-flanking delimiter run]
  4571. and either (a) not part of a [left-flanking delimiter run]
  4572. or (b) part of a [left-flanking delimiter run]
  4573. followed by punctuation.
  4574. 5. A double `**` [can open strong emphasis](@)
  4575. iff it is part of a [left-flanking delimiter run].
  4576. 6. A double `__` [can open strong emphasis] iff
  4577. it is part of a [left-flanking delimiter run]
  4578. and either (a) not part of a [right-flanking delimiter run]
  4579. or (b) part of a [right-flanking delimiter run]
  4580. preceded by punctuation.
  4581. 7. A double `**` [can close strong emphasis](@)
  4582. iff it is part of a [right-flanking delimiter run].
  4583. 8. A double `__` [can close strong emphasis]
  4584. it is part of a [right-flanking delimiter run]
  4585. and either (a) not part of a [left-flanking delimiter run]
  4586. or (b) part of a [left-flanking delimiter run]
  4587. followed by punctuation.
  4588. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4589. with a delimiter that [can close emphasis], and that uses the same
  4590. character (`_` or `*`) as the opening delimiter. The
  4591. opening and closing delimiters must belong to separate
  4592. [delimiter runs]. If one of the delimiters can both
  4593. open and close emphasis, then the sum of the lengths of the
  4594. delimiter runs containing the opening and closing delimiters
  4595. must not be a multiple of 3.
  4596. 10. Strong emphasis begins with a delimiter that
  4597. [can open strong emphasis] and ends with a delimiter that
  4598. [can close strong emphasis], and that uses the same character
  4599. (`_` or `*`) as the opening delimiter. The
  4600. opening and closing delimiters must belong to separate
  4601. [delimiter runs]. If one of the delimiters can both open
  4602. and close strong emphasis, then the sum of the lengths of
  4603. the delimiter runs containing the opening and closing
  4604. delimiters must not be a multiple of 3.
  4605. 11. A literal `*` character cannot occur at the beginning or end of
  4606. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4607. is backslash-escaped.
  4608. 12. A literal `_` character cannot occur at the beginning or end of
  4609. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4610. is backslash-escaped.
  4611. Where rules 1--12 above are compatible with multiple parsings,
  4612. the following principles resolve ambiguity:
  4613. 13. The number of nestings should be minimized. Thus, for example,
  4614. an interpretation `<strong>...</strong>` is always preferred to
  4615. `<em><em>...</em></em>`.
  4616. 14. An interpretation `<strong><em>...</em></strong>` is always
  4617. preferred to `<em><strong>..</strong></em>`.
  4618. 15. When two potential emphasis or strong emphasis spans overlap,
  4619. so that the second begins before the first ends and ends after
  4620. the first ends, the first takes precedence. Thus, for example,
  4621. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4622. than `*foo <em>bar* baz</em>`.
  4623. 16. When there are two potential emphasis or strong emphasis spans
  4624. with the same closing delimiter, the shorter one (the one that
  4625. opens later) takes precedence. Thus, for example,
  4626. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4627. rather than `<strong>foo **bar baz</strong>`.
  4628. 17. Inline code spans, links, images, and HTML tags group more tightly
  4629. than emphasis. So, when there is a choice between an interpretation
  4630. that contains one of these elements and one that does not, the
  4631. former always wins. Thus, for example, `*[foo*](bar)` is
  4632. parsed as `*<a href="bar">foo*</a>` rather than as
  4633. `<em>[foo</em>](bar)`.
  4634. These rules can be illustrated through a series of examples.
  4635. Rule 1:
  4636. ```````````````````````````````` example
  4637. *foo bar*
  4638. .
  4639. <p><em>foo bar</em></p>
  4640. ````````````````````````````````
  4641. This is not emphasis, because the opening `*` is followed by
  4642. whitespace, and hence not part of a [left-flanking delimiter run]:
  4643. ```````````````````````````````` example
  4644. a * foo bar*
  4645. .
  4646. <p>a * foo bar*</p>
  4647. ````````````````````````````````
  4648. This is not emphasis, because the opening `*` is preceded
  4649. by an alphanumeric and followed by punctuation, and hence
  4650. not part of a [left-flanking delimiter run]:
  4651. ```````````````````````````````` example
  4652. a*"foo"*
  4653. .
  4654. <p>a*&quot;foo&quot;*</p>
  4655. ````````````````````````````````
  4656. Unicode nonbreaking spaces count as whitespace, too:
  4657. ```````````````````````````````` example
  4658. * a *
  4659. .
  4660. <p>* a *</p>
  4661. ````````````````````````````````
  4662. Intraword emphasis with `*` is permitted:
  4663. ```````````````````````````````` example
  4664. foo*bar*
  4665. .
  4666. <p>foo<em>bar</em></p>
  4667. ````````````````````````````````
  4668. ```````````````````````````````` example
  4669. 5*6*78
  4670. .
  4671. <p>5<em>6</em>78</p>
  4672. ````````````````````````````````
  4673. Rule 2:
  4674. ```````````````````````````````` example
  4675. _foo bar_
  4676. .
  4677. <p><em>foo bar</em></p>
  4678. ````````````````````````````````
  4679. This is not emphasis, because the opening `_` is followed by
  4680. whitespace:
  4681. ```````````````````````````````` example
  4682. _ foo bar_
  4683. .
  4684. <p>_ foo bar_</p>
  4685. ````````````````````````````````
  4686. This is not emphasis, because the opening `_` is preceded
  4687. by an alphanumeric and followed by punctuation:
  4688. ```````````````````````````````` example
  4689. a_"foo"_
  4690. .
  4691. <p>a_&quot;foo&quot;_</p>
  4692. ````````````````````````````````
  4693. Emphasis with `_` is not allowed inside words:
  4694. ```````````````````````````````` example
  4695. foo_bar_
  4696. .
  4697. <p>foo_bar_</p>
  4698. ````````````````````````````````
  4699. ```````````````````````````````` example
  4700. 5_6_78
  4701. .
  4702. <p>5_6_78</p>
  4703. ````````````````````````````````
  4704. ```````````````````````````````` example
  4705. пристаням_стремятся_
  4706. .
  4707. <p>пристаням_стремятся_</p>
  4708. ````````````````````````````````
  4709. Here `_` does not generate emphasis, because the first delimiter run
  4710. is right-flanking and the second left-flanking:
  4711. ```````````````````````````````` example
  4712. aa_"bb"_cc
  4713. .
  4714. <p>aa_&quot;bb&quot;_cc</p>
  4715. ````````````````````````````````
  4716. This is emphasis, even though the opening delimiter is
  4717. both left- and right-flanking, because it is preceded by
  4718. punctuation:
  4719. ```````````````````````````````` example
  4720. foo-_(bar)_
  4721. .
  4722. <p>foo-<em>(bar)</em></p>
  4723. ````````````````````````````````
  4724. Rule 3:
  4725. This is not emphasis, because the closing delimiter does
  4726. not match the opening delimiter:
  4727. ```````````````````````````````` example
  4728. _foo*
  4729. .
  4730. <p>_foo*</p>
  4731. ````````````````````````````````
  4732. This is not emphasis, because the closing `*` is preceded by
  4733. whitespace:
  4734. ```````````````````````````````` example
  4735. *foo bar *
  4736. .
  4737. <p>*foo bar *</p>
  4738. ````````````````````````````````
  4739. A newline also counts as whitespace:
  4740. ```````````````````````````````` example
  4741. *foo bar
  4742. *
  4743. .
  4744. <p>*foo bar
  4745. *</p>
  4746. ````````````````````````````````
  4747. This is not emphasis, because the second `*` is
  4748. preceded by punctuation and followed by an alphanumeric
  4749. (hence it is not part of a [right-flanking delimiter run]:
  4750. ```````````````````````````````` example
  4751. *(*foo)
  4752. .
  4753. <p>*(*foo)</p>
  4754. ````````````````````````````````
  4755. The point of this restriction is more easily appreciated
  4756. with this example:
  4757. ```````````````````````````````` example
  4758. *(*foo*)*
  4759. .
  4760. <p><em>(<em>foo</em>)</em></p>
  4761. ````````````````````````````````
  4762. Intraword emphasis with `*` is allowed:
  4763. ```````````````````````````````` example
  4764. *foo*bar
  4765. .
  4766. <p><em>foo</em>bar</p>
  4767. ````````````````````````````````
  4768. Rule 4:
  4769. This is not emphasis, because the closing `_` is preceded by
  4770. whitespace:
  4771. ```````````````````````````````` example
  4772. _foo bar _
  4773. .
  4774. <p>_foo bar _</p>
  4775. ````````````````````````````````
  4776. This is not emphasis, because the second `_` is
  4777. preceded by punctuation and followed by an alphanumeric:
  4778. ```````````````````````````````` example
  4779. _(_foo)
  4780. .
  4781. <p>_(_foo)</p>
  4782. ````````````````````````````````
  4783. This is emphasis within emphasis:
  4784. ```````````````````````````````` example
  4785. _(_foo_)_
  4786. .
  4787. <p><em>(<em>foo</em>)</em></p>
  4788. ````````````````````````````````
  4789. Intraword emphasis is disallowed for `_`:
  4790. ```````````````````````````````` example
  4791. _foo_bar
  4792. .
  4793. <p>_foo_bar</p>
  4794. ````````````````````````````````
  4795. ```````````````````````````````` example
  4796. _пристаням_стремятся
  4797. .
  4798. <p>_пристаням_стремятся</p>
  4799. ````````````````````````````````
  4800. ```````````````````````````````` example
  4801. _foo_bar_baz_
  4802. .
  4803. <p><em>foo_bar_baz</em></p>
  4804. ````````````````````````````````
  4805. This is emphasis, even though the closing delimiter is
  4806. both left- and right-flanking, because it is followed by
  4807. punctuation:
  4808. ```````````````````````````````` example
  4809. _(bar)_.
  4810. .
  4811. <p><em>(bar)</em>.</p>
  4812. ````````````````````````````````
  4813. Rule 5:
  4814. ```````````````````````````````` example
  4815. **foo bar**
  4816. .
  4817. <p><strong>foo bar</strong></p>
  4818. ````````````````````````````````
  4819. This is not strong emphasis, because the opening delimiter is
  4820. followed by whitespace:
  4821. ```````````````````````````````` example
  4822. ** foo bar**
  4823. .
  4824. <p>** foo bar**</p>
  4825. ````````````````````````````````
  4826. This is not strong emphasis, because the opening `**` is preceded
  4827. by an alphanumeric and followed by punctuation, and hence
  4828. not part of a [left-flanking delimiter run]:
  4829. ```````````````````````````````` example
  4830. a**"foo"**
  4831. .
  4832. <p>a**&quot;foo&quot;**</p>
  4833. ````````````````````````````````
  4834. Intraword strong emphasis with `**` is permitted:
  4835. ```````````````````````````````` example
  4836. foo**bar**
  4837. .
  4838. <p>foo<strong>bar</strong></p>
  4839. ````````````````````````````````
  4840. Rule 6:
  4841. ```````````````````````````````` example
  4842. __foo bar__
  4843. .
  4844. <p><strong>foo bar</strong></p>
  4845. ````````````````````````````````
  4846. This is not strong emphasis, because the opening delimiter is
  4847. followed by whitespace:
  4848. ```````````````````````````````` example
  4849. __ foo bar__
  4850. .
  4851. <p>__ foo bar__</p>
  4852. ````````````````````````````````
  4853. A newline counts as whitespace:
  4854. ```````````````````````````````` example
  4855. __
  4856. foo bar__
  4857. .
  4858. <p>__
  4859. foo bar__</p>
  4860. ````````````````````````````````
  4861. This is not strong emphasis, because the opening `__` is preceded
  4862. by an alphanumeric and followed by punctuation:
  4863. ```````````````````````````````` example
  4864. a__"foo"__
  4865. .
  4866. <p>a__&quot;foo&quot;__</p>
  4867. ````````````````````````````````
  4868. Intraword strong emphasis is forbidden with `__`:
  4869. ```````````````````````````````` example
  4870. foo__bar__
  4871. .
  4872. <p>foo__bar__</p>
  4873. ````````````````````````````````
  4874. ```````````````````````````````` example
  4875. 5__6__78
  4876. .
  4877. <p>5__6__78</p>
  4878. ````````````````````````````````
  4879. ```````````````````````````````` example
  4880. пристаням__стремятся__
  4881. .
  4882. <p>пристаням__стремятся__</p>
  4883. ````````````````````````````````
  4884. ```````````````````````````````` example
  4885. __foo, __bar__, baz__
  4886. .
  4887. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4888. ````````````````````````````````
  4889. This is strong emphasis, even though the opening delimiter is
  4890. both left- and right-flanking, because it is preceded by
  4891. punctuation:
  4892. ```````````````````````````````` example
  4893. foo-__(bar)__
  4894. .
  4895. <p>foo-<strong>(bar)</strong></p>
  4896. ````````````````````````````````
  4897. Rule 7:
  4898. This is not strong emphasis, because the closing delimiter is preceded
  4899. by whitespace:
  4900. ```````````````````````````````` example
  4901. **foo bar **
  4902. .
  4903. <p>**foo bar **</p>
  4904. ````````````````````````````````
  4905. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4906. Rule 11.)
  4907. This is not strong emphasis, because the second `**` is
  4908. preceded by punctuation and followed by an alphanumeric:
  4909. ```````````````````````````````` example
  4910. **(**foo)
  4911. .
  4912. <p>**(**foo)</p>
  4913. ````````````````````````````````
  4914. The point of this restriction is more easily appreciated
  4915. with these examples:
  4916. ```````````````````````````````` example
  4917. *(**foo**)*
  4918. .
  4919. <p><em>(<strong>foo</strong>)</em></p>
  4920. ````````````````````````````````
  4921. ```````````````````````````````` example
  4922. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4923. *Asclepias physocarpa*)**
  4924. .
  4925. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4926. <em>Asclepias physocarpa</em>)</strong></p>
  4927. ````````````````````````````````
  4928. ```````````````````````````````` example
  4929. **foo "*bar*" foo**
  4930. .
  4931. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4932. ````````````````````````````````
  4933. Intraword emphasis:
  4934. ```````````````````````````````` example
  4935. **foo**bar
  4936. .
  4937. <p><strong>foo</strong>bar</p>
  4938. ````````````````````````````````
  4939. Rule 8:
  4940. This is not strong emphasis, because the closing delimiter is
  4941. preceded by whitespace:
  4942. ```````````````````````````````` example
  4943. __foo bar __
  4944. .
  4945. <p>__foo bar __</p>
  4946. ````````````````````````````````
  4947. This is not strong emphasis, because the second `__` is
  4948. preceded by punctuation and followed by an alphanumeric:
  4949. ```````````````````````````````` example
  4950. __(__foo)
  4951. .
  4952. <p>__(__foo)</p>
  4953. ````````````````````````````````
  4954. The point of this restriction is more easily appreciated
  4955. with this example:
  4956. ```````````````````````````````` example
  4957. _(__foo__)_
  4958. .
  4959. <p><em>(<strong>foo</strong>)</em></p>
  4960. ````````````````````````````````
  4961. Intraword strong emphasis is forbidden with `__`:
  4962. ```````````````````````````````` example
  4963. __foo__bar
  4964. .
  4965. <p>__foo__bar</p>
  4966. ````````````````````````````````
  4967. ```````````````````````````````` example
  4968. __пристаням__стремятся
  4969. .
  4970. <p>__пристаням__стремятся</p>
  4971. ````````````````````````````````
  4972. ```````````````````````````````` example
  4973. __foo__bar__baz__
  4974. .
  4975. <p><strong>foo__bar__baz</strong></p>
  4976. ````````````````````````````````
  4977. This is strong emphasis, even though the closing delimiter is
  4978. both left- and right-flanking, because it is followed by
  4979. punctuation:
  4980. ```````````````````````````````` example
  4981. __(bar)__.
  4982. .
  4983. <p><strong>(bar)</strong>.</p>
  4984. ````````````````````````````````
  4985. Rule 9:
  4986. Any nonempty sequence of inline elements can be the contents of an
  4987. emphasized span.
  4988. ```````````````````````````````` example
  4989. *foo [bar](/url)*
  4990. .
  4991. <p><em>foo <a href="/url">bar</a></em></p>
  4992. ````````````````````````````````
  4993. ```````````````````````````````` example
  4994. *foo
  4995. bar*
  4996. .
  4997. <p><em>foo
  4998. bar</em></p>
  4999. ````````````````````````````````
  5000. In particular, emphasis and strong emphasis can be nested
  5001. inside emphasis:
  5002. ```````````````````````````````` example
  5003. _foo __bar__ baz_
  5004. .
  5005. <p><em>foo <strong>bar</strong> baz</em></p>
  5006. ````````````````````````````````
  5007. ```````````````````````````````` example
  5008. _foo _bar_ baz_
  5009. .
  5010. <p><em>foo <em>bar</em> baz</em></p>
  5011. ````````````````````````````````
  5012. ```````````````````````````````` example
  5013. __foo_ bar_
  5014. .
  5015. <p><em><em>foo</em> bar</em></p>
  5016. ````````````````````````````````
  5017. ```````````````````````````````` example
  5018. *foo *bar**
  5019. .
  5020. <p><em>foo <em>bar</em></em></p>
  5021. ````````````````````````````````
  5022. ```````````````````````````````` example
  5023. *foo **bar** baz*
  5024. .
  5025. <p><em>foo <strong>bar</strong> baz</em></p>
  5026. ````````````````````````````````
  5027. ```````````````````````````````` example
  5028. *foo**bar**baz*
  5029. .
  5030. <p><em>foo<strong>bar</strong>baz</em></p>
  5031. ````````````````````````````````
  5032. Note that in the preceding case, the interpretation
  5033. ``` markdown
  5034. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5035. ```
  5036. is precluded by the condition that a delimiter that
  5037. can both open and close (like the `*` after `foo`)
  5038. cannot form emphasis if the sum of the lengths of
  5039. the delimiter runs containing the opening and
  5040. closing delimiters is a multiple of 3.
  5041. The same condition ensures that the following
  5042. cases are all strong emphasis nested inside
  5043. emphasis, even when the interior spaces are
  5044. omitted:
  5045. ```````````````````````````````` example
  5046. ***foo** bar*
  5047. .
  5048. <p><em><strong>foo</strong> bar</em></p>
  5049. ````````````````````````````````
  5050. ```````````````````````````````` example
  5051. *foo **bar***
  5052. .
  5053. <p><em>foo <strong>bar</strong></em></p>
  5054. ````````````````````````````````
  5055. ```````````````````````````````` example
  5056. *foo**bar***
  5057. .
  5058. <p><em>foo<strong>bar</strong></em></p>
  5059. ````````````````````````````````
  5060. Indefinite levels of nesting are possible:
  5061. ```````````````````````````````` example
  5062. *foo **bar *baz* bim** bop*
  5063. .
  5064. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5065. ````````````````````````````````
  5066. ```````````````````````````````` example
  5067. *foo [*bar*](/url)*
  5068. .
  5069. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5070. ````````````````````````````````
  5071. There can be no empty emphasis or strong emphasis:
  5072. ```````````````````````````````` example
  5073. ** is not an empty emphasis
  5074. .
  5075. <p>** is not an empty emphasis</p>
  5076. ````````````````````````````````
  5077. ```````````````````````````````` example
  5078. **** is not an empty strong emphasis
  5079. .
  5080. <p>**** is not an empty strong emphasis</p>
  5081. ````````````````````````````````
  5082. Rule 10:
  5083. Any nonempty sequence of inline elements can be the contents of an
  5084. strongly emphasized span.
  5085. ```````````````````````````````` example
  5086. **foo [bar](/url)**
  5087. .
  5088. <p><strong>foo <a href="/url">bar</a></strong></p>
  5089. ````````````````````````````````
  5090. ```````````````````````````````` example
  5091. **foo
  5092. bar**
  5093. .
  5094. <p><strong>foo
  5095. bar</strong></p>
  5096. ````````````````````````````````
  5097. In particular, emphasis and strong emphasis can be nested
  5098. inside strong emphasis:
  5099. ```````````````````````````````` example
  5100. __foo _bar_ baz__
  5101. .
  5102. <p><strong>foo <em>bar</em> baz</strong></p>
  5103. ````````````````````````````````
  5104. ```````````````````````````````` example
  5105. __foo __bar__ baz__
  5106. .
  5107. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5108. ````````````````````````````````
  5109. ```````````````````````````````` example
  5110. ____foo__ bar__
  5111. .
  5112. <p><strong><strong>foo</strong> bar</strong></p>
  5113. ````````````````````````````````
  5114. ```````````````````````````````` example
  5115. **foo **bar****
  5116. .
  5117. <p><strong>foo <strong>bar</strong></strong></p>
  5118. ````````````````````````````````
  5119. ```````````````````````````````` example
  5120. **foo *bar* baz**
  5121. .
  5122. <p><strong>foo <em>bar</em> baz</strong></p>
  5123. ````````````````````````````````
  5124. ```````````````````````````````` example
  5125. **foo*bar*baz**
  5126. .
  5127. <p><strong>foo<em>bar</em>baz</strong></p>
  5128. ````````````````````````````````
  5129. ```````````````````````````````` example
  5130. ***foo* bar**
  5131. .
  5132. <p><strong><em>foo</em> bar</strong></p>
  5133. ````````````````````````````````
  5134. ```````````````````````````````` example
  5135. **foo *bar***
  5136. .
  5137. <p><strong>foo <em>bar</em></strong></p>
  5138. ````````````````````````````````
  5139. Indefinite levels of nesting are possible:
  5140. ```````````````````````````````` example
  5141. **foo *bar **baz**
  5142. bim* bop**
  5143. .
  5144. <p><strong>foo <em>bar <strong>baz</strong>
  5145. bim</em> bop</strong></p>
  5146. ````````````````````````````````
  5147. ```````````````````````````````` example
  5148. **foo [*bar*](/url)**
  5149. .
  5150. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5151. ````````````````````````````````
  5152. There can be no empty emphasis or strong emphasis:
  5153. ```````````````````````````````` example
  5154. __ is not an empty emphasis
  5155. .
  5156. <p>__ is not an empty emphasis</p>
  5157. ````````````````````````````````
  5158. ```````````````````````````````` example
  5159. ____ is not an empty strong emphasis
  5160. .
  5161. <p>____ is not an empty strong emphasis</p>
  5162. ````````````````````````````````
  5163. Rule 11:
  5164. ```````````````````````````````` example
  5165. foo ***
  5166. .
  5167. <p>foo ***</p>
  5168. ````````````````````````````````
  5169. ```````````````````````````````` example
  5170. foo *\**
  5171. .
  5172. <p>foo <em>*</em></p>
  5173. ````````````````````````````````
  5174. ```````````````````````````````` example
  5175. foo *_*
  5176. .
  5177. <p>foo <em>_</em></p>
  5178. ````````````````````````````````
  5179. ```````````````````````````````` example
  5180. foo *****
  5181. .
  5182. <p>foo *****</p>
  5183. ````````````````````````````````
  5184. ```````````````````````````````` example
  5185. foo **\***
  5186. .
  5187. <p>foo <strong>*</strong></p>
  5188. ````````````````````````````````
  5189. ```````````````````````````````` example
  5190. foo **_**
  5191. .
  5192. <p>foo <strong>_</strong></p>
  5193. ````````````````````````````````
  5194. Note that when delimiters do not match evenly, Rule 11 determines
  5195. that the excess literal `*` characters will appear outside of the
  5196. emphasis, rather than inside it:
  5197. ```````````````````````````````` example
  5198. **foo*
  5199. .
  5200. <p>*<em>foo</em></p>
  5201. ````````````````````````````````
  5202. ```````````````````````````````` example
  5203. *foo**
  5204. .
  5205. <p><em>foo</em>*</p>
  5206. ````````````````````````````````
  5207. ```````````````````````````````` example
  5208. ***foo**
  5209. .
  5210. <p>*<strong>foo</strong></p>
  5211. ````````````````````````````````
  5212. ```````````````````````````````` example
  5213. ****foo*
  5214. .
  5215. <p>***<em>foo</em></p>
  5216. ````````````````````````````````
  5217. ```````````````````````````````` example
  5218. **foo***
  5219. .
  5220. <p><strong>foo</strong>*</p>
  5221. ````````````````````````````````
  5222. ```````````````````````````````` example
  5223. *foo****
  5224. .
  5225. <p><em>foo</em>***</p>
  5226. ````````````````````````````````
  5227. Rule 12:
  5228. ```````````````````````````````` example
  5229. foo ___
  5230. .
  5231. <p>foo ___</p>
  5232. ````````````````````````````````
  5233. ```````````````````````````````` example
  5234. foo _\__
  5235. .
  5236. <p>foo <em>_</em></p>
  5237. ````````````````````````````````
  5238. ```````````````````````````````` example
  5239. foo _*_
  5240. .
  5241. <p>foo <em>*</em></p>
  5242. ````````````````````````````````
  5243. ```````````````````````````````` example
  5244. foo _____
  5245. .
  5246. <p>foo _____</p>
  5247. ````````````````````````````````
  5248. ```````````````````````````````` example
  5249. foo __\___
  5250. .
  5251. <p>foo <strong>_</strong></p>
  5252. ````````````````````````````````
  5253. ```````````````````````````````` example
  5254. foo __*__
  5255. .
  5256. <p>foo <strong>*</strong></p>
  5257. ````````````````````````````````
  5258. ```````````````````````````````` example
  5259. __foo_
  5260. .
  5261. <p>_<em>foo</em></p>
  5262. ````````````````````````````````
  5263. Note that when delimiters do not match evenly, Rule 12 determines
  5264. that the excess literal `_` characters will appear outside of the
  5265. emphasis, rather than inside it:
  5266. ```````````````````````````````` example
  5267. _foo__
  5268. .
  5269. <p><em>foo</em>_</p>
  5270. ````````````````````````````````
  5271. ```````````````````````````````` example
  5272. ___foo__
  5273. .
  5274. <p>_<strong>foo</strong></p>
  5275. ````````````````````````````````
  5276. ```````````````````````````````` example
  5277. ____foo_
  5278. .
  5279. <p>___<em>foo</em></p>
  5280. ````````````````````````````````
  5281. ```````````````````````````````` example
  5282. __foo___
  5283. .
  5284. <p><strong>foo</strong>_</p>
  5285. ````````````````````````````````
  5286. ```````````````````````````````` example
  5287. _foo____
  5288. .
  5289. <p><em>foo</em>___</p>
  5290. ````````````````````````````````
  5291. Rule 13 implies that if you want emphasis nested directly inside
  5292. emphasis, you must use different delimiters:
  5293. ```````````````````````````````` example
  5294. **foo**
  5295. .
  5296. <p><strong>foo</strong></p>
  5297. ````````````````````````````````
  5298. ```````````````````````````````` example
  5299. *_foo_*
  5300. .
  5301. <p><em><em>foo</em></em></p>
  5302. ````````````````````````````````
  5303. ```````````````````````````````` example
  5304. __foo__
  5305. .
  5306. <p><strong>foo</strong></p>
  5307. ````````````````````````````````
  5308. ```````````````````````````````` example
  5309. _*foo*_
  5310. .
  5311. <p><em><em>foo</em></em></p>
  5312. ````````````````````````````````
  5313. However, strong emphasis within strong emphasis is possible without
  5314. switching delimiters:
  5315. ```````````````````````````````` example
  5316. ****foo****
  5317. .
  5318. <p><strong><strong>foo</strong></strong></p>
  5319. ````````````````````````````````
  5320. ```````````````````````````````` example
  5321. ____foo____
  5322. .
  5323. <p><strong><strong>foo</strong></strong></p>
  5324. ````````````````````````````````
  5325. Rule 13 can be applied to arbitrarily long sequences of
  5326. delimiters:
  5327. ```````````````````````````````` example
  5328. ******foo******
  5329. .
  5330. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5331. ````````````````````````````````
  5332. Rule 14:
  5333. ```````````````````````````````` example
  5334. ***foo***
  5335. .
  5336. <p><strong><em>foo</em></strong></p>
  5337. ````````````````````````````````
  5338. ```````````````````````````````` example
  5339. _____foo_____
  5340. .
  5341. <p><strong><strong><em>foo</em></strong></strong></p>
  5342. ````````````````````````````````
  5343. Rule 15:
  5344. ```````````````````````````````` example
  5345. *foo _bar* baz_
  5346. .
  5347. <p><em>foo _bar</em> baz_</p>
  5348. ````````````````````````````````
  5349. ```````````````````````````````` example
  5350. *foo __bar *baz bim__ bam*
  5351. .
  5352. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5353. ````````````````````````````````
  5354. Rule 16:
  5355. ```````````````````````````````` example
  5356. **foo **bar baz**
  5357. .
  5358. <p>**foo <strong>bar baz</strong></p>
  5359. ````````````````````````````````
  5360. ```````````````````````````````` example
  5361. *foo *bar baz*
  5362. .
  5363. <p>*foo <em>bar baz</em></p>
  5364. ````````````````````````````````
  5365. Rule 17:
  5366. ```````````````````````````````` example
  5367. *[bar*](/url)
  5368. .
  5369. <p>*<a href="/url">bar*</a></p>
  5370. ````````````````````````````````
  5371. ```````````````````````````````` example
  5372. _foo [bar_](/url)
  5373. .
  5374. <p>_foo <a href="/url">bar_</a></p>
  5375. ````````````````````````````````
  5376. ```````````````````````````````` example
  5377. *<img src="foo" title="*"/>
  5378. .
  5379. <p>*<img src="foo" title="*"/></p>
  5380. ````````````````````````````````
  5381. ```````````````````````````````` example
  5382. **<a href="**">
  5383. .
  5384. <p>**<a href="**"></p>
  5385. ````````````````````````````````
  5386. ```````````````````````````````` example
  5387. __<a href="__">
  5388. .
  5389. <p>__<a href="__"></p>
  5390. ````````````````````````````````
  5391. ```````````````````````````````` example
  5392. *a `*`*
  5393. .
  5394. <p><em>a <code>*</code></em></p>
  5395. ````````````````````````````````
  5396. ```````````````````````````````` example
  5397. _a `_`_
  5398. .
  5399. <p><em>a <code>_</code></em></p>
  5400. ````````````````````````````````
  5401. ```````````````````````````````` example
  5402. **a<http://foo.bar/?q=**>
  5403. .
  5404. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5405. ````````````````````````````````
  5406. ```````````````````````````````` example
  5407. __a<http://foo.bar/?q=__>
  5408. .
  5409. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5410. ````````````````````````````````
  5411. ## Links
  5412. A link contains [link text] (the visible text), a [link destination]
  5413. (the URI that is the link destination), and optionally a [link title].
  5414. There are two basic kinds of links in Markdown. In [inline links] the
  5415. destination and title are given immediately after the link text. In
  5416. [reference links] the destination and title are defined elsewhere in
  5417. the document.
  5418. A [link text](@) consists of a sequence of zero or more
  5419. inline elements enclosed by square brackets (`[` and `]`). The
  5420. following rules apply:
  5421. - Links may not contain other links, at any level of nesting. If
  5422. multiple otherwise valid link definitions appear nested inside each
  5423. other, the inner-most definition is used.
  5424. - Brackets are allowed in the [link text] only if (a) they
  5425. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5426. with an open bracket `[`, a sequence of zero or more inlines, and
  5427. a close bracket `]`.
  5428. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5429. than the brackets in link text. Thus, for example,
  5430. `` [foo`]` `` could not be a link text, since the second `]`
  5431. is part of a code span.
  5432. - The brackets in link text bind more tightly than markers for
  5433. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5434. A [link destination](@) consists of either
  5435. - a sequence of zero or more characters between an opening `<` and a
  5436. closing `>` that contains no spaces, line breaks, or unescaped
  5437. `<` or `>` characters, or
  5438. - a nonempty sequence of characters that does not include
  5439. ASCII space or control characters, and includes parentheses
  5440. only if (a) they are backslash-escaped or (b) they are part of
  5441. a balanced pair of unescaped parentheses that is not itself
  5442. inside a balanced pair of unescaped parentheses.
  5443. A [link title](@) consists of either
  5444. - a sequence of zero or more characters between straight double-quote
  5445. characters (`"`), including a `"` character only if it is
  5446. backslash-escaped, or
  5447. - a sequence of zero or more characters between straight single-quote
  5448. characters (`'`), including a `'` character only if it is
  5449. backslash-escaped, or
  5450. - a sequence of zero or more characters between matching parentheses
  5451. (`(...)`), including a `)` character only if it is backslash-escaped.
  5452. Although [link titles] may span multiple lines, they may not contain
  5453. a [blank line].
  5454. An [inline link](@) consists of a [link text] followed immediately
  5455. by a left parenthesis `(`, optional [whitespace], an optional
  5456. [link destination], an optional [link title] separated from the link
  5457. destination by [whitespace], optional [whitespace], and a right
  5458. parenthesis `)`. The link's text consists of the inlines contained
  5459. in the [link text] (excluding the enclosing square brackets).
  5460. The link's URI consists of the link destination, excluding enclosing
  5461. `<...>` if present, with backslash-escapes in effect as described
  5462. above. The link's title consists of the link title, excluding its
  5463. enclosing delimiters, with backslash-escapes in effect as described
  5464. above.
  5465. Here is a simple inline link:
  5466. ```````````````````````````````` example
  5467. [link](/uri "title")
  5468. .
  5469. <p><a href="/uri" title="title">link</a></p>
  5470. ````````````````````````````````
  5471. The title may be omitted:
  5472. ```````````````````````````````` example
  5473. [link](/uri)
  5474. .
  5475. <p><a href="/uri">link</a></p>
  5476. ````````````````````````````````
  5477. Both the title and the destination may be omitted:
  5478. ```````````````````````````````` example
  5479. [link]()
  5480. .
  5481. <p><a href="">link</a></p>
  5482. ````````````````````````````````
  5483. ```````````````````````````````` example
  5484. [link](<>)
  5485. .
  5486. <p><a href="">link</a></p>
  5487. ````````````````````````````````
  5488. The destination cannot contain spaces or line breaks,
  5489. even if enclosed in pointy brackets:
  5490. ```````````````````````````````` example
  5491. [link](/my uri)
  5492. .
  5493. <p>[link](/my uri)</p>
  5494. ````````````````````````````````
  5495. ```````````````````````````````` example
  5496. [link](</my uri>)
  5497. .
  5498. <p>[link](&lt;/my uri&gt;)</p>
  5499. ````````````````````````````````
  5500. ```````````````````````````````` example
  5501. [link](foo
  5502. bar)
  5503. .
  5504. <p>[link](foo
  5505. bar)</p>
  5506. ````````````````````````````````
  5507. ```````````````````````````````` example
  5508. [link](<foo
  5509. bar>)
  5510. .
  5511. <p>[link](<foo
  5512. bar>)</p>
  5513. ````````````````````````````````
  5514. Parentheses inside the link destination may be escaped:
  5515. ```````````````````````````````` example
  5516. [link](\(foo\))
  5517. .
  5518. <p><a href="(foo)">link</a></p>
  5519. ````````````````````````````````
  5520. One level of balanced parentheses is allowed without escaping:
  5521. ```````````````````````````````` example
  5522. [link]((foo)and(bar))
  5523. .
  5524. <p><a href="(foo)and(bar)">link</a></p>
  5525. ````````````````````````````````
  5526. However, if you have parentheses within parentheses, you need to escape
  5527. or use the `<...>` form:
  5528. ```````````````````````````````` example
  5529. [link](foo(and(bar)))
  5530. .
  5531. <p>[link](foo(and(bar)))</p>
  5532. ````````````````````````````````
  5533. ```````````````````````````````` example
  5534. [link](foo(and\(bar\)))
  5535. .
  5536. <p><a href="foo(and(bar))">link</a></p>
  5537. ````````````````````````````````
  5538. ```````````````````````````````` example
  5539. [link](<foo(and(bar))>)
  5540. .
  5541. <p><a href="foo(and(bar))">link</a></p>
  5542. ````````````````````````````````
  5543. Parentheses and other symbols can also be escaped, as usual
  5544. in Markdown:
  5545. ```````````````````````````````` example
  5546. [link](foo\)\:)
  5547. .
  5548. <p><a href="foo):">link</a></p>
  5549. ````````````````````````````````
  5550. A link can contain fragment identifiers and queries:
  5551. ```````````````````````````````` example
  5552. [link](#fragment)
  5553. [link](http://example.com#fragment)
  5554. [link](http://example.com?foo=3#frag)
  5555. .
  5556. <p><a href="#fragment">link</a></p>
  5557. <p><a href="http://example.com#fragment">link</a></p>
  5558. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5559. ````````````````````````````````
  5560. Note that a backslash before a non-escapable character is
  5561. just a backslash:
  5562. ```````````````````````````````` example
  5563. [link](foo\bar)
  5564. .
  5565. <p><a href="foo%5Cbar">link</a></p>
  5566. ````````````````````````````````
  5567. URL-escaping should be left alone inside the destination, as all
  5568. URL-escaped characters are also valid URL characters. Entity and
  5569. numerical character references in the destination will be parsed
  5570. into the corresponding Unicode code points, as usual. These may
  5571. be optionally URL-escaped when written as HTML, but this spec
  5572. does not enforce any particular policy for rendering URLs in
  5573. HTML or other formats. Renderers may make different decisions
  5574. about how to escape or normalize URLs in the output.
  5575. ```````````````````````````````` example
  5576. [link](foo%20b&auml;)
  5577. .
  5578. <p><a href="foo%20b%C3%A4">link</a></p>
  5579. ````````````````````````````````
  5580. Note that, because titles can often be parsed as destinations,
  5581. if you try to omit the destination and keep the title, you'll
  5582. get unexpected results:
  5583. ```````````````````````````````` example
  5584. [link]("title")
  5585. .
  5586. <p><a href="%22title%22">link</a></p>
  5587. ````````````````````````````````
  5588. Titles may be in single quotes, double quotes, or parentheses:
  5589. ```````````````````````````````` example
  5590. [link](/url "title")
  5591. [link](/url 'title')
  5592. [link](/url (title))
  5593. .
  5594. <p><a href="/url" title="title">link</a>
  5595. <a href="/url" title="title">link</a>
  5596. <a href="/url" title="title">link</a></p>
  5597. ````````````````````````````````
  5598. Backslash escapes and entity and numeric character references
  5599. may be used in titles:
  5600. ```````````````````````````````` example
  5601. [link](/url "title \"&quot;")
  5602. .
  5603. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5604. ````````````````````````````````
  5605. Titles must be separated from the link using a [whitespace].
  5606. Other [Unicode whitespace] like non-breaking space doesn't work.
  5607. ```````````````````````````````` example
  5608. [link](/url "title")
  5609. .
  5610. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5611. ````````````````````````````````
  5612. Nested balanced quotes are not allowed without escaping:
  5613. ```````````````````````````````` example
  5614. [link](/url "title "and" title")
  5615. .
  5616. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5617. ````````````````````````````````
  5618. But it is easy to work around this by using a different quote type:
  5619. ```````````````````````````````` example
  5620. [link](/url 'title "and" title')
  5621. .
  5622. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5623. ````````````````````````````````
  5624. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5625. title, and its test suite included a test demonstrating this.
  5626. But it is hard to see a good rationale for the extra complexity this
  5627. brings, since there are already many ways---backslash escaping,
  5628. entity and numeric character references, or using a different
  5629. quote type for the enclosing title---to write titles containing
  5630. double quotes. `Markdown.pl`'s handling of titles has a number
  5631. of other strange features. For example, it allows single-quoted
  5632. titles in inline links, but not reference links. And, in
  5633. reference links but not inline links, it allows a title to begin
  5634. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5635. titles with no closing quotation mark, though 1.0.2b8 does not.
  5636. It seems preferable to adopt a simple, rational rule that works
  5637. the same way in inline links and link reference definitions.)
  5638. [Whitespace] is allowed around the destination and title:
  5639. ```````````````````````````````` example
  5640. [link]( /uri
  5641. "title" )
  5642. .
  5643. <p><a href="/uri" title="title">link</a></p>
  5644. ````````````````````````````````
  5645. But it is not allowed between the link text and the
  5646. following parenthesis:
  5647. ```````````````````````````````` example
  5648. [link] (/uri)
  5649. .
  5650. <p>[link] (/uri)</p>
  5651. ````````````````````````````````
  5652. The link text may contain balanced brackets, but not unbalanced ones,
  5653. unless they are escaped:
  5654. ```````````````````````````````` example
  5655. [link [foo [bar]]](/uri)
  5656. .
  5657. <p><a href="/uri">link [foo [bar]]</a></p>
  5658. ````````````````````````````````
  5659. ```````````````````````````````` example
  5660. [link] bar](/uri)
  5661. .
  5662. <p>[link] bar](/uri)</p>
  5663. ````````````````````````````````
  5664. ```````````````````````````````` example
  5665. [link [bar](/uri)
  5666. .
  5667. <p>[link <a href="/uri">bar</a></p>
  5668. ````````````````````````````````
  5669. ```````````````````````````````` example
  5670. [link \[bar](/uri)
  5671. .
  5672. <p><a href="/uri">link [bar</a></p>
  5673. ````````````````````````````````
  5674. The link text may contain inline content:
  5675. ```````````````````````````````` example
  5676. [link *foo **bar** `#`*](/uri)
  5677. .
  5678. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5679. ````````````````````````````````
  5680. ```````````````````````````````` example
  5681. [![moon](moon.jpg)](/uri)
  5682. .
  5683. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5684. ````````````````````````````````
  5685. However, links may not contain other links, at any level of nesting.
  5686. ```````````````````````````````` example
  5687. [foo [bar](/uri)](/uri)
  5688. .
  5689. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5690. ````````````````````````````````
  5691. ```````````````````````````````` example
  5692. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5693. .
  5694. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5695. ````````````````````````````````
  5696. ```````````````````````````````` example
  5697. ![[[foo](uri1)](uri2)](uri3)
  5698. .
  5699. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5700. ````````````````````````````````
  5701. These cases illustrate the precedence of link text grouping over
  5702. emphasis grouping:
  5703. ```````````````````````````````` example
  5704. *[foo*](/uri)
  5705. .
  5706. <p>*<a href="/uri">foo*</a></p>
  5707. ````````````````````````````````
  5708. ```````````````````````````````` example
  5709. [foo *bar](baz*)
  5710. .
  5711. <p><a href="baz*">foo *bar</a></p>
  5712. ````````````````````````````````
  5713. Note that brackets that *aren't* part of links do not take
  5714. precedence:
  5715. ```````````````````````````````` example
  5716. *foo [bar* baz]
  5717. .
  5718. <p><em>foo [bar</em> baz]</p>
  5719. ````````````````````````````````
  5720. These cases illustrate the precedence of HTML tags, code spans,
  5721. and autolinks over link grouping:
  5722. ```````````````````````````````` example
  5723. [foo <bar attr="](baz)">
  5724. .
  5725. <p>[foo <bar attr="](baz)"></p>
  5726. ````````````````````````````````
  5727. ```````````````````````````````` example
  5728. [foo`](/uri)`
  5729. .
  5730. <p>[foo<code>](/uri)</code></p>
  5731. ````````````````````````````````
  5732. ```````````````````````````````` example
  5733. [foo<http://example.com/?search=](uri)>
  5734. .
  5735. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5736. ````````````````````````````````
  5737. There are three kinds of [reference link](@)s:
  5738. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5739. and [shortcut](#shortcut-reference-link).
  5740. A [full reference link](@)
  5741. consists of a [link text] immediately followed by a [link label]
  5742. that [matches] a [link reference definition] elsewhere in the document.
  5743. A [link label](@) begins with a left bracket (`[`) and ends
  5744. with the first right bracket (`]`) that is not backslash-escaped.
  5745. Between these brackets there must be at least one [non-whitespace character].
  5746. Unescaped square bracket characters are not allowed in
  5747. [link labels]. A link label can have at most 999
  5748. characters inside the square brackets.
  5749. One label [matches](@)
  5750. another just in case their normalized forms are equal. To normalize a
  5751. label, perform the *Unicode case fold* and collapse consecutive internal
  5752. [whitespace] to a single space. If there are multiple
  5753. matching reference link definitions, the one that comes first in the
  5754. document is used. (It is desirable in such cases to emit a warning.)
  5755. The contents of the first link label are parsed as inlines, which are
  5756. used as the link's text. The link's URI and title are provided by the
  5757. matching [link reference definition].
  5758. Here is a simple example:
  5759. ```````````````````````````````` example
  5760. [foo][bar]
  5761. [bar]: /url "title"
  5762. .
  5763. <p><a href="/url" title="title">foo</a></p>
  5764. ````````````````````````````````
  5765. The rules for the [link text] are the same as with
  5766. [inline links]. Thus:
  5767. The link text may contain balanced brackets, but not unbalanced ones,
  5768. unless they are escaped:
  5769. ```````````````````````````````` example
  5770. [link [foo [bar]]][ref]
  5771. [ref]: /uri
  5772. .
  5773. <p><a href="/uri">link [foo [bar]]</a></p>
  5774. ````````````````````````````````
  5775. ```````````````````````````````` example
  5776. [link \[bar][ref]
  5777. [ref]: /uri
  5778. .
  5779. <p><a href="/uri">link [bar</a></p>
  5780. ````````````````````````````````
  5781. The link text may contain inline content:
  5782. ```````````````````````````````` example
  5783. [link *foo **bar** `#`*][ref]
  5784. [ref]: /uri
  5785. .
  5786. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5787. ````````````````````````````````
  5788. ```````````````````````````````` example
  5789. [![moon](moon.jpg)][ref]
  5790. [ref]: /uri
  5791. .
  5792. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5793. ````````````````````````````````
  5794. However, links may not contain other links, at any level of nesting.
  5795. ```````````````````````````````` example
  5796. [foo [bar](/uri)][ref]
  5797. [ref]: /uri
  5798. .
  5799. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5800. ````````````````````````````````
  5801. ```````````````````````````````` example
  5802. [foo *bar [baz][ref]*][ref]
  5803. [ref]: /uri
  5804. .
  5805. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5806. ````````````````````````````````
  5807. (In the examples above, we have two [shortcut reference links]
  5808. instead of one [full reference link].)
  5809. The following cases illustrate the precedence of link text grouping over
  5810. emphasis grouping:
  5811. ```````````````````````````````` example
  5812. *[foo*][ref]
  5813. [ref]: /uri
  5814. .
  5815. <p>*<a href="/uri">foo*</a></p>
  5816. ````````````````````````````````
  5817. ```````````````````````````````` example
  5818. [foo *bar][ref]
  5819. [ref]: /uri
  5820. .
  5821. <p><a href="/uri">foo *bar</a></p>
  5822. ````````````````````````````````
  5823. These cases illustrate the precedence of HTML tags, code spans,
  5824. and autolinks over link grouping:
  5825. ```````````````````````````````` example
  5826. [foo <bar attr="][ref]">
  5827. [ref]: /uri
  5828. .
  5829. <p>[foo <bar attr="][ref]"></p>
  5830. ````````````````````````````````
  5831. ```````````````````````````````` example
  5832. [foo`][ref]`
  5833. [ref]: /uri
  5834. .
  5835. <p>[foo<code>][ref]</code></p>
  5836. ````````````````````````````````
  5837. ```````````````````````````````` example
  5838. [foo<http://example.com/?search=][ref]>
  5839. [ref]: /uri
  5840. .
  5841. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5842. ````````````````````````````````
  5843. Matching is case-insensitive:
  5844. ```````````````````````````````` example
  5845. [foo][BaR]
  5846. [bar]: /url "title"
  5847. .
  5848. <p><a href="/url" title="title">foo</a></p>
  5849. ````````````````````````````````
  5850. Unicode case fold is used:
  5851. ```````````````````````````````` example
  5852. [Толпой][Толпой] is a Russian word.
  5853. [ТОЛПОЙ]: /url
  5854. .
  5855. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5856. ````````````````````````````````
  5857. Consecutive internal [whitespace] is treated as one space for
  5858. purposes of determining matching:
  5859. ```````````````````````````````` example
  5860. [Foo
  5861. bar]: /url
  5862. [Baz][Foo bar]
  5863. .
  5864. <p><a href="/url">Baz</a></p>
  5865. ````````````````````````````````
  5866. No [whitespace] is allowed between the [link text] and the
  5867. [link label]:
  5868. ```````````````````````````````` example
  5869. [foo] [bar]
  5870. [bar]: /url "title"
  5871. .
  5872. <p>[foo] <a href="/url" title="title">bar</a></p>
  5873. ````````````````````````````````
  5874. ```````````````````````````````` example
  5875. [foo]
  5876. [bar]
  5877. [bar]: /url "title"
  5878. .
  5879. <p>[foo]
  5880. <a href="/url" title="title">bar</a></p>
  5881. ````````````````````````````````
  5882. This is a departure from John Gruber's original Markdown syntax
  5883. description, which explicitly allows whitespace between the link
  5884. text and the link label. It brings reference links in line with
  5885. [inline links], which (according to both original Markdown and
  5886. this spec) cannot have whitespace after the link text. More
  5887. importantly, it prevents inadvertent capture of consecutive
  5888. [shortcut reference links]. If whitespace is allowed between the
  5889. link text and the link label, then in the following we will have
  5890. a single reference link, not two shortcut reference links, as
  5891. intended:
  5892. ``` markdown
  5893. [foo]
  5894. [bar]
  5895. [foo]: /url1
  5896. [bar]: /url2
  5897. ```
  5898. (Note that [shortcut reference links] were introduced by Gruber
  5899. himself in a beta version of `Markdown.pl`, but never included
  5900. in the official syntax description. Without shortcut reference
  5901. links, it is harmless to allow space between the link text and
  5902. link label; but once shortcut references are introduced, it is
  5903. too dangerous to allow this, as it frequently leads to
  5904. unintended results.)
  5905. When there are multiple matching [link reference definitions],
  5906. the first is used:
  5907. ```````````````````````````````` example
  5908. [foo]: /url1
  5909. [foo]: /url2
  5910. [bar][foo]
  5911. .
  5912. <p><a href="/url1">bar</a></p>
  5913. ````````````````````````````````
  5914. Note that matching is performed on normalized strings, not parsed
  5915. inline content. So the following does not match, even though the
  5916. labels define equivalent inline content:
  5917. ```````````````````````````````` example
  5918. [bar][foo\!]
  5919. [foo!]: /url
  5920. .
  5921. <p>[bar][foo!]</p>
  5922. ````````````````````````````````
  5923. [Link labels] cannot contain brackets, unless they are
  5924. backslash-escaped:
  5925. ```````````````````````````````` example
  5926. [foo][ref[]
  5927. [ref[]: /uri
  5928. .
  5929. <p>[foo][ref[]</p>
  5930. <p>[ref[]: /uri</p>
  5931. ````````````````````````````````
  5932. ```````````````````````````````` example
  5933. [foo][ref[bar]]
  5934. [ref[bar]]: /uri
  5935. .
  5936. <p>[foo][ref[bar]]</p>
  5937. <p>[ref[bar]]: /uri</p>
  5938. ````````````````````````````````
  5939. ```````````````````````````````` example
  5940. [[[foo]]]
  5941. [[[foo]]]: /url
  5942. .
  5943. <p>[[[foo]]]</p>
  5944. <p>[[[foo]]]: /url</p>
  5945. ````````````````````````````````
  5946. ```````````````````````````````` example
  5947. [foo][ref\[]
  5948. [ref\[]: /uri
  5949. .
  5950. <p><a href="/uri">foo</a></p>
  5951. ````````````````````````````````
  5952. Note that in this example `]` is not backslash-escaped:
  5953. ```````````````````````````````` example
  5954. [bar\\]: /uri
  5955. [bar\\]
  5956. .
  5957. <p><a href="/uri">bar\</a></p>
  5958. ````````````````````````````````
  5959. A [link label] must contain at least one [non-whitespace character]:
  5960. ```````````````````````````````` example
  5961. []
  5962. []: /uri
  5963. .
  5964. <p>[]</p>
  5965. <p>[]: /uri</p>
  5966. ````````````````````````````````
  5967. ```````````````````````````````` example
  5968. [
  5969. ]
  5970. [
  5971. ]: /uri
  5972. .
  5973. <p>[
  5974. ]</p>
  5975. <p>[
  5976. ]: /uri</p>
  5977. ````````````````````````````````
  5978. A [collapsed reference link](@)
  5979. consists of a [link label] that [matches] a
  5980. [link reference definition] elsewhere in the
  5981. document, followed by the string `[]`.
  5982. The contents of the first link label are parsed as inlines,
  5983. which are used as the link's text. The link's URI and title are
  5984. provided by the matching reference link definition. Thus,
  5985. `[foo][]` is equivalent to `[foo][foo]`.
  5986. ```````````````````````````````` example
  5987. [foo][]
  5988. [foo]: /url "title"
  5989. .
  5990. <p><a href="/url" title="title">foo</a></p>
  5991. ````````````````````````````````
  5992. ```````````````````````````````` example
  5993. [*foo* bar][]
  5994. [*foo* bar]: /url "title"
  5995. .
  5996. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  5997. ````````````````````````````````
  5998. The link labels are case-insensitive:
  5999. ```````````````````````````````` example
  6000. [Foo][]
  6001. [foo]: /url "title"
  6002. .
  6003. <p><a href="/url" title="title">Foo</a></p>
  6004. ````````````````````````````````
  6005. As with full reference links, [whitespace] is not
  6006. allowed between the two sets of brackets:
  6007. ```````````````````````````````` example
  6008. [foo]
  6009. []
  6010. [foo]: /url "title"
  6011. .
  6012. <p><a href="/url" title="title">foo</a>
  6013. []</p>
  6014. ````````````````````````````````
  6015. A [shortcut reference link](@)
  6016. consists of a [link label] that [matches] a
  6017. [link reference definition] elsewhere in the
  6018. document and is not followed by `[]` or a link label.
  6019. The contents of the first link label are parsed as inlines,
  6020. which are used as the link's text. The link's URI and title
  6021. are provided by the matching link reference definition.
  6022. Thus, `[foo]` is equivalent to `[foo][]`.
  6023. ```````````````````````````````` example
  6024. [foo]
  6025. [foo]: /url "title"
  6026. .
  6027. <p><a href="/url" title="title">foo</a></p>
  6028. ````````````````````````````````
  6029. ```````````````````````````````` example
  6030. [*foo* bar]
  6031. [*foo* bar]: /url "title"
  6032. .
  6033. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6034. ````````````````````````````````
  6035. ```````````````````````````````` example
  6036. [[*foo* bar]]
  6037. [*foo* bar]: /url "title"
  6038. .
  6039. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6040. ````````````````````````````````
  6041. ```````````````````````````````` example
  6042. [[bar [foo]
  6043. [foo]: /url
  6044. .
  6045. <p>[[bar <a href="/url">foo</a></p>
  6046. ````````````````````````````````
  6047. The link labels are case-insensitive:
  6048. ```````````````````````````````` example
  6049. [Foo]
  6050. [foo]: /url "title"
  6051. .
  6052. <p><a href="/url" title="title">Foo</a></p>
  6053. ````````````````````````````````
  6054. A space after the link text should be preserved:
  6055. ```````````````````````````````` example
  6056. [foo] bar
  6057. [foo]: /url
  6058. .
  6059. <p><a href="/url">foo</a> bar</p>
  6060. ````````````````````````````````
  6061. If you just want bracketed text, you can backslash-escape the
  6062. opening bracket to avoid links:
  6063. ```````````````````````````````` example
  6064. \[foo]
  6065. [foo]: /url "title"
  6066. .
  6067. <p>[foo]</p>
  6068. ````````````````````````````````
  6069. Note that this is a link, because a link label ends with the first
  6070. following closing bracket:
  6071. ```````````````````````````````` example
  6072. [foo*]: /url
  6073. *[foo*]
  6074. .
  6075. <p>*<a href="/url">foo*</a></p>
  6076. ````````````````````````````````
  6077. Full references take precedence over shortcut references:
  6078. ```````````````````````````````` example
  6079. [foo][bar]
  6080. [foo]: /url1
  6081. [bar]: /url2
  6082. .
  6083. <p><a href="/url2">foo</a></p>
  6084. ````````````````````````````````
  6085. In the following case `[bar][baz]` is parsed as a reference,
  6086. `[foo]` as normal text:
  6087. ```````````````````````````````` example
  6088. [foo][bar][baz]
  6089. [baz]: /url
  6090. .
  6091. <p>[foo]<a href="/url">bar</a></p>
  6092. ````````````````````````````````
  6093. Here, though, `[foo][bar]` is parsed as a reference, since
  6094. `[bar]` is defined:
  6095. ```````````````````````````````` example
  6096. [foo][bar][baz]
  6097. [baz]: /url1
  6098. [bar]: /url2
  6099. .
  6100. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6101. ````````````````````````````````
  6102. Here `[foo]` is not parsed as a shortcut reference, because it
  6103. is followed by a link label (even though `[bar]` is not defined):
  6104. ```````````````````````````````` example
  6105. [foo][bar][baz]
  6106. [baz]: /url1
  6107. [foo]: /url2
  6108. .
  6109. <p>[foo]<a href="/url1">bar</a></p>
  6110. ````````````````````````````````
  6111. ## Images
  6112. Syntax for images is like the syntax for links, with one
  6113. difference. Instead of [link text], we have an
  6114. [image description](@). The rules for this are the
  6115. same as for [link text], except that (a) an
  6116. image description starts with `![` rather than `[`, and
  6117. (b) an image description may contain links.
  6118. An image description has inline elements
  6119. as its contents. When an image is rendered to HTML,
  6120. this is standardly used as the image's `alt` attribute.
  6121. ```````````````````````````````` example
  6122. ![foo](/url "title")
  6123. .
  6124. <p><img src="/url" alt="foo" title="title" /></p>
  6125. ````````````````````````````````
  6126. ```````````````````````````````` example
  6127. ![foo *bar*]
  6128. [foo *bar*]: train.jpg "train & tracks"
  6129. .
  6130. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6131. ````````````````````````````````
  6132. ```````````````````````````````` example
  6133. ![foo ![bar](/url)](/url2)
  6134. .
  6135. <p><img src="/url2" alt="foo bar" /></p>
  6136. ````````````````````````````````
  6137. ```````````````````````````````` example
  6138. ![foo [bar](/url)](/url2)
  6139. .
  6140. <p><img src="/url2" alt="foo bar" /></p>
  6141. ````````````````````````````````
  6142. Though this spec is concerned with parsing, not rendering, it is
  6143. recommended that in rendering to HTML, only the plain string content
  6144. of the [image description] be used. Note that in
  6145. the above example, the alt attribute's value is `foo bar`, not `foo
  6146. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6147. content is rendered, without formatting.
  6148. ```````````````````````````````` example
  6149. ![foo *bar*][]
  6150. [foo *bar*]: train.jpg "train & tracks"
  6151. .
  6152. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6153. ````````````````````````````````
  6154. ```````````````````````````````` example
  6155. ![foo *bar*][foobar]
  6156. [FOOBAR]: train.jpg "train & tracks"
  6157. .
  6158. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6159. ````````````````````````````````
  6160. ```````````````````````````````` example
  6161. ![foo](train.jpg)
  6162. .
  6163. <p><img src="train.jpg" alt="foo" /></p>
  6164. ````````````````````````````````
  6165. ```````````````````````````````` example
  6166. My ![foo bar](/path/to/train.jpg "title" )
  6167. .
  6168. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6169. ````````````````````````````````
  6170. ```````````````````````````````` example
  6171. ![foo](<url>)
  6172. .
  6173. <p><img src="url" alt="foo" /></p>
  6174. ````````````````````````````````
  6175. ```````````````````````````````` example
  6176. ![](/url)
  6177. .
  6178. <p><img src="/url" alt="" /></p>
  6179. ````````````````````````````````
  6180. Reference-style:
  6181. ```````````````````````````````` example
  6182. ![foo][bar]
  6183. [bar]: /url
  6184. .
  6185. <p><img src="/url" alt="foo" /></p>
  6186. ````````````````````````````````
  6187. ```````````````````````````````` example
  6188. ![foo][bar]
  6189. [BAR]: /url
  6190. .
  6191. <p><img src="/url" alt="foo" /></p>
  6192. ````````````````````````````````
  6193. Collapsed:
  6194. ```````````````````````````````` example
  6195. ![foo][]
  6196. [foo]: /url "title"
  6197. .
  6198. <p><img src="/url" alt="foo" title="title" /></p>
  6199. ````````````````````````````````
  6200. ```````````````````````````````` example
  6201. ![*foo* bar][]
  6202. [*foo* bar]: /url "title"
  6203. .
  6204. <p><img src="/url" alt="foo bar" title="title" /></p>
  6205. ````````````````````````````````
  6206. The labels are case-insensitive:
  6207. ```````````````````````````````` example
  6208. ![Foo][]
  6209. [foo]: /url "title"
  6210. .
  6211. <p><img src="/url" alt="Foo" title="title" /></p>
  6212. ````````````````````````````````
  6213. As with reference links, [whitespace] is not allowed
  6214. between the two sets of brackets:
  6215. ```````````````````````````````` example
  6216. ![foo]
  6217. []
  6218. [foo]: /url "title"
  6219. .
  6220. <p><img src="/url" alt="foo" title="title" />
  6221. []</p>
  6222. ````````````````````````````````
  6223. Shortcut:
  6224. ```````````````````````````````` example
  6225. ![foo]
  6226. [foo]: /url "title"
  6227. .
  6228. <p><img src="/url" alt="foo" title="title" /></p>
  6229. ````````````````````````````````
  6230. ```````````````````````````````` example
  6231. ![*foo* bar]
  6232. [*foo* bar]: /url "title"
  6233. .
  6234. <p><img src="/url" alt="foo bar" title="title" /></p>
  6235. ````````````````````````````````
  6236. Note that link labels cannot contain unescaped brackets:
  6237. ```````````````````````````````` example
  6238. ![[foo]]
  6239. [[foo]]: /url "title"
  6240. .
  6241. <p>![[foo]]</p>
  6242. <p>[[foo]]: /url &quot;title&quot;</p>
  6243. ````````````````````````````````
  6244. The link labels are case-insensitive:
  6245. ```````````````````````````````` example
  6246. ![Foo]
  6247. [foo]: /url "title"
  6248. .
  6249. <p><img src="/url" alt="Foo" title="title" /></p>
  6250. ````````````````````````````````
  6251. If you just want bracketed text, you can backslash-escape the
  6252. opening `!` and `[`:
  6253. ```````````````````````````````` example
  6254. \!\[foo]
  6255. [foo]: /url "title"
  6256. .
  6257. <p>![foo]</p>
  6258. ````````````````````````````````
  6259. If you want a link after a literal `!`, backslash-escape the
  6260. `!`:
  6261. ```````````````````````````````` example
  6262. \![foo]
  6263. [foo]: /url "title"
  6264. .
  6265. <p>!<a href="/url" title="title">foo</a></p>
  6266. ````````````````````````````````
  6267. ## Autolinks
  6268. [Autolink](@)s are absolute URIs and email addresses inside
  6269. `<` and `>`. They are parsed as links, with the URL or email address
  6270. as the link label.
  6271. A [URI autolink](@) consists of `<`, followed by an
  6272. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6273. a link to the URI, with the URI as the link's label.
  6274. An [absolute URI](@),
  6275. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6276. followed by zero or more characters other than ASCII
  6277. [whitespace] and control characters, `<`, and `>`. If
  6278. the URI includes these characters, they must be percent-encoded
  6279. (e.g. `%20` for a space).
  6280. For purposes of this spec, a [scheme](@) is any sequence
  6281. of 2--32 characters beginning with an ASCII letter and followed
  6282. by any combination of ASCII letters, digits, or the symbols plus
  6283. ("+"), period ("."), or hyphen ("-").
  6284. Here are some valid autolinks:
  6285. ```````````````````````````````` example
  6286. <http://foo.bar.baz>
  6287. .
  6288. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6289. ````````````````````````````````
  6290. ```````````````````````````````` example
  6291. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6292. .
  6293. <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>
  6294. ````````````````````````````````
  6295. ```````````````````````````````` example
  6296. <irc://foo.bar:2233/baz>
  6297. .
  6298. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6299. ````````````````````````````````
  6300. Uppercase is also fine:
  6301. ```````````````````````````````` example
  6302. <MAILTO:FOO@BAR.BAZ>
  6303. .
  6304. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6305. ````````````````````````````````
  6306. Note that many strings that count as [absolute URIs] for
  6307. purposes of this spec are not valid URIs, because their
  6308. schemes are not registered or because of other problems
  6309. with their syntax:
  6310. ```````````````````````````````` example
  6311. <a+b+c:d>
  6312. .
  6313. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6314. ````````````````````````````````
  6315. ```````````````````````````````` example
  6316. <made-up-scheme://foo,bar>
  6317. .
  6318. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6319. ````````````````````````````````
  6320. ```````````````````````````````` example
  6321. <http://../>
  6322. .
  6323. <p><a href="http://../">http://../</a></p>
  6324. ````````````````````````````````
  6325. ```````````````````````````````` example
  6326. <localhost:5001/foo>
  6327. .
  6328. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6329. ````````````````````````````````
  6330. Spaces are not allowed in autolinks:
  6331. ```````````````````````````````` example
  6332. <http://foo.bar/baz bim>
  6333. .
  6334. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6335. ````````````````````````````````
  6336. Backslash-escapes do not work inside autolinks:
  6337. ```````````````````````````````` example
  6338. <http://example.com/\[\>
  6339. .
  6340. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6341. ````````````````````````````````
  6342. An [email autolink](@)
  6343. consists of `<`, followed by an [email address],
  6344. followed by `>`. The link's label is the email address,
  6345. and the URL is `mailto:` followed by the email address.
  6346. An [email address](@),
  6347. for these purposes, is anything that matches
  6348. the [non-normative regex from the HTML5
  6349. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6350. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6351. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6352. Examples of email autolinks:
  6353. ```````````````````````````````` example
  6354. <foo@bar.example.com>
  6355. .
  6356. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6357. ````````````````````````````````
  6358. ```````````````````````````````` example
  6359. <foo+special@Bar.baz-bar0.com>
  6360. .
  6361. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6362. ````````````````````````````````
  6363. Backslash-escapes do not work inside email autolinks:
  6364. ```````````````````````````````` example
  6365. <foo\+@bar.example.com>
  6366. .
  6367. <p>&lt;foo+@bar.example.com&gt;</p>
  6368. ````````````````````````````````
  6369. These are not autolinks:
  6370. ```````````````````````````````` example
  6371. <>
  6372. .
  6373. <p>&lt;&gt;</p>
  6374. ````````````````````````````````
  6375. ```````````````````````````````` example
  6376. < http://foo.bar >
  6377. .
  6378. <p>&lt; http://foo.bar &gt;</p>
  6379. ````````````````````````````````
  6380. ```````````````````````````````` example
  6381. <m:abc>
  6382. .
  6383. <p>&lt;m:abc&gt;</p>
  6384. ````````````````````````````````
  6385. ```````````````````````````````` example
  6386. <foo.bar.baz>
  6387. .
  6388. <p>&lt;foo.bar.baz&gt;</p>
  6389. ````````````````````````````````
  6390. ```````````````````````````````` example
  6391. http://example.com
  6392. .
  6393. <p>http://example.com</p>
  6394. ````````````````````````````````
  6395. ```````````````````````````````` example
  6396. foo@bar.example.com
  6397. .
  6398. <p>foo@bar.example.com</p>
  6399. ````````````````````````````````
  6400. ## Raw HTML
  6401. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6402. raw HTML tag and will be rendered in HTML without escaping.
  6403. Tag and attribute names are not limited to current HTML tags,
  6404. so custom tags (and even, say, DocBook tags) may be used.
  6405. Here is the grammar for tags:
  6406. A [tag name](@) consists of an ASCII letter
  6407. followed by zero or more ASCII letters, digits, or
  6408. hyphens (`-`).
  6409. An [attribute](@) consists of [whitespace],
  6410. an [attribute name], and an optional
  6411. [attribute value specification].
  6412. An [attribute name](@)
  6413. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6414. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6415. specification restricted to ASCII. HTML5 is laxer.)
  6416. An [attribute value specification](@)
  6417. consists of optional [whitespace],
  6418. a `=` character, optional [whitespace], and an [attribute
  6419. value].
  6420. An [attribute value](@)
  6421. consists of an [unquoted attribute value],
  6422. a [single-quoted attribute value], or a [double-quoted attribute value].
  6423. An [unquoted attribute value](@)
  6424. is a nonempty string of characters not
  6425. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6426. A [single-quoted attribute value](@)
  6427. consists of `'`, zero or more
  6428. characters not including `'`, and a final `'`.
  6429. A [double-quoted attribute value](@)
  6430. consists of `"`, zero or more
  6431. characters not including `"`, and a final `"`.
  6432. An [open tag](@) consists of a `<` character, a [tag name],
  6433. zero or more [attributes], optional [whitespace], an optional `/`
  6434. character, and a `>` character.
  6435. A [closing tag](@) consists of the string `</`, a
  6436. [tag name], optional [whitespace], and the character `>`.
  6437. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6438. where *text* does not start with `>` or `->`, does not end with `-`,
  6439. and does not contain `--`. (See the
  6440. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6441. A [processing instruction](@)
  6442. consists of the string `<?`, a string
  6443. of characters not including the string `?>`, and the string
  6444. `?>`.
  6445. A [declaration](@) consists of the
  6446. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6447. [whitespace], a string of characters not including the
  6448. character `>`, and the character `>`.
  6449. A [CDATA section](@) consists of
  6450. the string `<![CDATA[`, a string of characters not including the string
  6451. `]]>`, and the string `]]>`.
  6452. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6453. an [HTML comment], a [processing instruction], a [declaration],
  6454. or a [CDATA section].
  6455. Here are some simple open tags:
  6456. ```````````````````````````````` example
  6457. <a><bab><c2c>
  6458. .
  6459. <p><a><bab><c2c></p>
  6460. ````````````````````````````````
  6461. Empty elements:
  6462. ```````````````````````````````` example
  6463. <a/><b2/>
  6464. .
  6465. <p><a/><b2/></p>
  6466. ````````````````````````````````
  6467. [Whitespace] is allowed:
  6468. ```````````````````````````````` example
  6469. <a /><b2
  6470. data="foo" >
  6471. .
  6472. <p><a /><b2
  6473. data="foo" ></p>
  6474. ````````````````````````````````
  6475. With attributes:
  6476. ```````````````````````````````` example
  6477. <a foo="bar" bam = 'baz <em>"</em>'
  6478. _boolean zoop:33=zoop:33 />
  6479. .
  6480. <p><a foo="bar" bam = 'baz <em>"</em>'
  6481. _boolean zoop:33=zoop:33 /></p>
  6482. ````````````````````````````````
  6483. Custom tag names can be used:
  6484. ```````````````````````````````` example
  6485. Foo <responsive-image src="foo.jpg" />
  6486. .
  6487. <p>Foo <responsive-image src="foo.jpg" /></p>
  6488. ````````````````````````````````
  6489. Illegal tag names, not parsed as HTML:
  6490. ```````````````````````````````` example
  6491. <33> <__>
  6492. .
  6493. <p>&lt;33&gt; &lt;__&gt;</p>
  6494. ````````````````````````````````
  6495. Illegal attribute names:
  6496. ```````````````````````````````` example
  6497. <a h*#ref="hi">
  6498. .
  6499. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6500. ````````````````````````````````
  6501. Illegal attribute values:
  6502. ```````````````````````````````` example
  6503. <a href="hi'> <a href=hi'>
  6504. .
  6505. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6506. ````````````````````````````````
  6507. Illegal [whitespace]:
  6508. ```````````````````````````````` example
  6509. < a><
  6510. foo><bar/ >
  6511. .
  6512. <p>&lt; a&gt;&lt;
  6513. foo&gt;&lt;bar/ &gt;</p>
  6514. ````````````````````````````````
  6515. Missing [whitespace]:
  6516. ```````````````````````````````` example
  6517. <a href='bar'title=title>
  6518. .
  6519. <p>&lt;a href='bar'title=title&gt;</p>
  6520. ````````````````````````````````
  6521. Closing tags:
  6522. ```````````````````````````````` example
  6523. </a></foo >
  6524. .
  6525. <p></a></foo ></p>
  6526. ````````````````````````````````
  6527. Illegal attributes in closing tag:
  6528. ```````````````````````````````` example
  6529. </a href="foo">
  6530. .
  6531. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6532. ````````````````````````````````
  6533. Comments:
  6534. ```````````````````````````````` example
  6535. foo <!-- this is a
  6536. comment - with hyphen -->
  6537. .
  6538. <p>foo <!-- this is a
  6539. comment - with hyphen --></p>
  6540. ````````````````````````````````
  6541. ```````````````````````````````` example
  6542. foo <!-- not a comment -- two hyphens -->
  6543. .
  6544. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6545. ````````````````````````````````
  6546. Not comments:
  6547. ```````````````````````````````` example
  6548. foo <!--> foo -->
  6549. foo <!-- foo--->
  6550. .
  6551. <p>foo &lt;!--&gt; foo --&gt;</p>
  6552. <p>foo &lt;!-- foo---&gt;</p>
  6553. ````````````````````````````````
  6554. Processing instructions:
  6555. ```````````````````````````````` example
  6556. foo <?php echo $a; ?>
  6557. .
  6558. <p>foo <?php echo $a; ?></p>
  6559. ````````````````````````````````
  6560. Declarations:
  6561. ```````````````````````````````` example
  6562. foo <!ELEMENT br EMPTY>
  6563. .
  6564. <p>foo <!ELEMENT br EMPTY></p>
  6565. ````````````````````````````````
  6566. CDATA sections:
  6567. ```````````````````````````````` example
  6568. foo <![CDATA[>&<]]>
  6569. .
  6570. <p>foo <![CDATA[>&<]]></p>
  6571. ````````````````````````````````
  6572. Entity and numeric character references are preserved in HTML
  6573. attributes:
  6574. ```````````````````````````````` example
  6575. foo <a href="&ouml;">
  6576. .
  6577. <p>foo <a href="&ouml;"></p>
  6578. ````````````````````````````````
  6579. Backslash escapes do not work in HTML attributes:
  6580. ```````````````````````````````` example
  6581. foo <a href="\*">
  6582. .
  6583. <p>foo <a href="\*"></p>
  6584. ````````````````````````````````
  6585. ```````````````````````````````` example
  6586. <a href="\"">
  6587. .
  6588. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6589. ````````````````````````````````
  6590. ## Hard line breaks
  6591. A line break (not in a code span or HTML tag) that is preceded
  6592. by two or more spaces and does not occur at the end of a block
  6593. is parsed as a [hard line break](@) (rendered
  6594. in HTML as a `<br />` tag):
  6595. ```````````````````````````````` example
  6596. foo
  6597. baz
  6598. .
  6599. <p>foo<br />
  6600. baz</p>
  6601. ````````````````````````````````
  6602. For a more visible alternative, a backslash before the
  6603. [line ending] may be used instead of two spaces:
  6604. ```````````````````````````````` example
  6605. foo\
  6606. baz
  6607. .
  6608. <p>foo<br />
  6609. baz</p>
  6610. ````````````````````````````````
  6611. More than two spaces can be used:
  6612. ```````````````````````````````` example
  6613. foo
  6614. baz
  6615. .
  6616. <p>foo<br />
  6617. baz</p>
  6618. ````````````````````````````````
  6619. Leading spaces at the beginning of the next line are ignored:
  6620. ```````````````````````````````` example
  6621. foo
  6622. bar
  6623. .
  6624. <p>foo<br />
  6625. bar</p>
  6626. ````````````````````````````````
  6627. ```````````````````````````````` example
  6628. foo\
  6629. bar
  6630. .
  6631. <p>foo<br />
  6632. bar</p>
  6633. ````````````````````````````````
  6634. Line breaks can occur inside emphasis, links, and other constructs
  6635. that allow inline content:
  6636. ```````````````````````````````` example
  6637. *foo
  6638. bar*
  6639. .
  6640. <p><em>foo<br />
  6641. bar</em></p>
  6642. ````````````````````````````````
  6643. ```````````````````````````````` example
  6644. *foo\
  6645. bar*
  6646. .
  6647. <p><em>foo<br />
  6648. bar</em></p>
  6649. ````````````````````````````````
  6650. Line breaks do not occur inside code spans
  6651. ```````````````````````````````` example
  6652. `code
  6653. span`
  6654. .
  6655. <p><code>code span</code></p>
  6656. ````````````````````````````````
  6657. ```````````````````````````````` example
  6658. `code\
  6659. span`
  6660. .
  6661. <p><code>code\ span</code></p>
  6662. ````````````````````````````````
  6663. or HTML tags:
  6664. ```````````````````````````````` example
  6665. <a href="foo
  6666. bar">
  6667. .
  6668. <p><a href="foo
  6669. bar"></p>
  6670. ````````````````````````````````
  6671. ```````````````````````````````` example
  6672. <a href="foo\
  6673. bar">
  6674. .
  6675. <p><a href="foo\
  6676. bar"></p>
  6677. ````````````````````````````````
  6678. Hard line breaks are for separating inline content within a block.
  6679. Neither syntax for hard line breaks works at the end of a paragraph or
  6680. other block element:
  6681. ```````````````````````````````` example
  6682. foo\
  6683. .
  6684. <p>foo\</p>
  6685. ````````````````````````````````
  6686. ```````````````````````````````` example
  6687. foo
  6688. .
  6689. <p>foo</p>
  6690. ````````````````````````````````
  6691. ```````````````````````````````` example
  6692. ### foo\
  6693. .
  6694. <h3>foo\</h3>
  6695. ````````````````````````````````
  6696. ```````````````````````````````` example
  6697. ### foo
  6698. .
  6699. <h3>foo</h3>
  6700. ````````````````````````````````
  6701. ## Soft line breaks
  6702. A regular line break (not in a code span or HTML tag) that is not
  6703. preceded by two or more spaces or a backslash is parsed as a
  6704. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6705. [line ending] or as a space. The result will be the same in
  6706. browsers. In the examples here, a [line ending] will be used.)
  6707. ```````````````````````````````` example
  6708. foo
  6709. baz
  6710. .
  6711. <p>foo
  6712. baz</p>
  6713. ````````````````````````````````
  6714. Spaces at the end of the line and beginning of the next line are
  6715. removed:
  6716. ```````````````````````````````` example
  6717. foo
  6718. baz
  6719. .
  6720. <p>foo
  6721. baz</p>
  6722. ````````````````````````````````
  6723. A conforming parser may render a soft line break in HTML either as a
  6724. line break or as a space.
  6725. A renderer may also provide an option to render soft line breaks
  6726. as hard line breaks.
  6727. ## Textual content
  6728. Any characters not given an interpretation by the above rules will
  6729. be parsed as plain textual content.
  6730. ```````````````````````````````` example
  6731. hello $.;'there
  6732. .
  6733. <p>hello $.;'there</p>
  6734. ````````````````````````````````
  6735. ```````````````````````````````` example
  6736. Foo χρῆν
  6737. .
  6738. <p>Foo χρῆν</p>
  6739. ````````````````````````````````
  6740. Internal spaces are preserved verbatim:
  6741. ```````````````````````````````` example
  6742. Multiple spaces
  6743. .
  6744. <p>Multiple spaces</p>
  6745. ````````````````````````````````
  6746. <!-- END TESTS -->
  6747. # Appendix: A parsing strategy
  6748. In this appendix we describe some features of the parsing strategy
  6749. used in the CommonMark reference implementations.
  6750. ## Overview
  6751. Parsing has two phases:
  6752. 1. In the first phase, lines of input are consumed and the block
  6753. structure of the document---its division into paragraphs, block quotes,
  6754. list items, and so on---is constructed. Text is assigned to these
  6755. blocks but not parsed. Link reference definitions are parsed and a
  6756. map of links is constructed.
  6757. 2. In the second phase, the raw text contents of paragraphs and headings
  6758. are parsed into sequences of Markdown inline elements (strings,
  6759. code spans, links, emphasis, and so on), using the map of link
  6760. references constructed in phase 1.
  6761. At each point in processing, the document is represented as a tree of
  6762. **blocks**. The root of the tree is a `document` block. The `document`
  6763. may have any number of other blocks as **children**. These children
  6764. may, in turn, have other blocks as children. The last child of a block
  6765. is normally considered **open**, meaning that subsequent lines of input
  6766. can alter its contents. (Blocks that are not open are **closed**.)
  6767. Here, for example, is a possible document tree, with the open blocks
  6768. marked by arrows:
  6769. ``` tree
  6770. -> document
  6771. -> block_quote
  6772. paragraph
  6773. "Lorem ipsum dolor\nsit amet."
  6774. -> list (type=bullet tight=true bullet_char=-)
  6775. list_item
  6776. paragraph
  6777. "Qui *quodsi iracundia*"
  6778. -> list_item
  6779. -> paragraph
  6780. "aliquando id"
  6781. ```
  6782. ## Phase 1: block structure
  6783. Each line that is processed has an effect on this tree. The line is
  6784. analyzed and, depending on its contents, the document may be altered
  6785. in one or more of the following ways:
  6786. 1. One or more open blocks may be closed.
  6787. 2. One or more new blocks may be created as children of the
  6788. last open block.
  6789. 3. Text may be added to the last (deepest) open block remaining
  6790. on the tree.
  6791. Once a line has been incorporated into the tree in this way,
  6792. it can be discarded, so input can be read in a stream.
  6793. For each line, we follow this procedure:
  6794. 1. First we iterate through the open blocks, starting with the
  6795. root document, and descending through last children down to the last
  6796. open block. Each block imposes a condition that the line must satisfy
  6797. if the block is to remain open. For example, a block quote requires a
  6798. `>` character. A paragraph requires a non-blank line.
  6799. In this phase we may match all or just some of the open
  6800. blocks. But we cannot close unmatched blocks yet, because we may have a
  6801. [lazy continuation line].
  6802. 2. Next, after consuming the continuation markers for existing
  6803. blocks, we look for new block starts (e.g. `>` for a block quote).
  6804. If we encounter a new block start, we close any blocks unmatched
  6805. in step 1 before creating the new block as a child of the last
  6806. matched block.
  6807. 3. Finally, we look at the remainder of the line (after block
  6808. markers like `>`, list markers, and indentation have been consumed).
  6809. This is text that can be incorporated into the last open
  6810. block (a paragraph, code block, heading, or raw HTML).
  6811. Setext headings are formed when we see a line of a paragraph
  6812. that is a [setext heading underline].
  6813. Reference link definitions are detected when a paragraph is closed;
  6814. the accumulated text lines are parsed to see if they begin with
  6815. one or more reference link definitions. Any remainder becomes a
  6816. normal paragraph.
  6817. We can see how this works by considering how the tree above is
  6818. generated by four lines of Markdown:
  6819. ``` markdown
  6820. > Lorem ipsum dolor
  6821. sit amet.
  6822. > - Qui *quodsi iracundia*
  6823. > - aliquando id
  6824. ```
  6825. At the outset, our document model is just
  6826. ``` tree
  6827. -> document
  6828. ```
  6829. The first line of our text,
  6830. ``` markdown
  6831. > Lorem ipsum dolor
  6832. ```
  6833. causes a `block_quote` block to be created as a child of our
  6834. open `document` block, and a `paragraph` block as a child of
  6835. the `block_quote`. Then the text is added to the last open
  6836. block, the `paragraph`:
  6837. ``` tree
  6838. -> document
  6839. -> block_quote
  6840. -> paragraph
  6841. "Lorem ipsum dolor"
  6842. ```
  6843. The next line,
  6844. ``` markdown
  6845. sit amet.
  6846. ```
  6847. is a "lazy continuation" of the open `paragraph`, so it gets added
  6848. to the paragraph's text:
  6849. ``` tree
  6850. -> document
  6851. -> block_quote
  6852. -> paragraph
  6853. "Lorem ipsum dolor\nsit amet."
  6854. ```
  6855. The third line,
  6856. ``` markdown
  6857. > - Qui *quodsi iracundia*
  6858. ```
  6859. causes the `paragraph` block to be closed, and a new `list` block
  6860. opened as a child of the `block_quote`. A `list_item` is also
  6861. added as a child of the `list`, and a `paragraph` as a child of
  6862. the `list_item`. The text is then added to the new `paragraph`:
  6863. ``` tree
  6864. -> document
  6865. -> block_quote
  6866. paragraph
  6867. "Lorem ipsum dolor\nsit amet."
  6868. -> list (type=bullet tight=true bullet_char=-)
  6869. -> list_item
  6870. -> paragraph
  6871. "Qui *quodsi iracundia*"
  6872. ```
  6873. The fourth line,
  6874. ``` markdown
  6875. > - aliquando id
  6876. ```
  6877. causes the `list_item` (and its child the `paragraph`) to be closed,
  6878. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6879. is added as a child of the new `list_item`, to contain the text.
  6880. We thus obtain the final tree:
  6881. ``` tree
  6882. -> document
  6883. -> block_quote
  6884. paragraph
  6885. "Lorem ipsum dolor\nsit amet."
  6886. -> list (type=bullet tight=true bullet_char=-)
  6887. list_item
  6888. paragraph
  6889. "Qui *quodsi iracundia*"
  6890. -> list_item
  6891. -> paragraph
  6892. "aliquando id"
  6893. ```
  6894. ## Phase 2: inline structure
  6895. Once all of the input has been parsed, all open blocks are closed.
  6896. We then "walk the tree," visiting every node, and parse raw
  6897. string contents of paragraphs and headings as inlines. At this
  6898. point we have seen all the link reference definitions, so we can
  6899. resolve reference links as we go.
  6900. ``` tree
  6901. document
  6902. block_quote
  6903. paragraph
  6904. str "Lorem ipsum dolor"
  6905. softbreak
  6906. str "sit amet."
  6907. list (type=bullet tight=true bullet_char=-)
  6908. list_item
  6909. paragraph
  6910. str "Qui "
  6911. emph
  6912. str "quodsi iracundia"
  6913. list_item
  6914. paragraph
  6915. str "aliquando id"
  6916. ```
  6917. Notice how the [line ending] in the first paragraph has
  6918. been parsed as a `softbreak`, and the asterisks in the first list item
  6919. have become an `emph`.
  6920. ### An algorithm for parsing nested emphasis and links
  6921. By far the trickiest part of inline parsing is handling emphasis,
  6922. strong emphasis, links, and images. This is done using the following
  6923. algorithm.
  6924. When we're parsing inlines and we hit either
  6925. - a run of `*` or `_` characters, or
  6926. - a `[` or `![`
  6927. we insert a text node with these symbols as its literal content, and we
  6928. add a pointer to this text node to the [delimiter stack](@).
  6929. The [delimiter stack] is a doubly linked list. Each
  6930. element contains a pointer to a text node, plus information about
  6931. - the type of delimiter (`[`, `![`, `*`, `_`)
  6932. - the number of delimiters,
  6933. - whether the delimiter is "active" (all are active to start), and
  6934. - whether the delimiter is a potential opener, a potential closer,
  6935. or both (which depends on what sort of characters precede
  6936. and follow the delimiters).
  6937. When we hit a `]` character, we call the *look for link or image*
  6938. procedure (see below).
  6939. When we hit the end of the input, we call the *process emphasis*
  6940. procedure (see below), with `stack_bottom` = NULL.
  6941. #### *look for link or image*
  6942. Starting at the top of the delimiter stack, we look backwards
  6943. through the stack for an opening `[` or `![` delimiter.
  6944. - If we don't find one, we return a literal text node `]`.
  6945. - If we do find one, but it's not *active*, we remove the inactive
  6946. delimiter from the stack, and return a literal text node `]`.
  6947. - If we find one and it's active, then we parse ahead to see if
  6948. we have an inline link/image, reference link/image, compact reference
  6949. link/image, or shortcut reference link/image.
  6950. + If we don't, then we remove the opening delimiter from the
  6951. delimiter stack and return a literal text node `]`.
  6952. + If we do, then
  6953. * We return a link or image node whose children are the inlines
  6954. after the text node pointed to by the opening delimiter.
  6955. * We run *process emphasis* on these inlines, with the `[` opener
  6956. as `stack_bottom`.
  6957. * We remove the opening delimiter.
  6958. * If we have a link (and not an image), we also set all
  6959. `[` delimiters before the opening delimiter to *inactive*. (This
  6960. will prevent us from getting links within links.)
  6961. #### *process emphasis*
  6962. Parameter `stack_bottom` sets a lower bound to how far we
  6963. descend in the [delimiter stack]. If it is NULL, we can
  6964. go all the way to the bottom. Otherwise, we stop before
  6965. visiting `stack_bottom`.
  6966. Let `current_position` point to the element on the [delimiter stack]
  6967. just above `stack_bottom` (or the first element if `stack_bottom`
  6968. is NULL).
  6969. We keep track of the `openers_bottom` for each delimiter
  6970. type (`*`, `_`). Initialize this to `stack_bottom`.
  6971. Then we repeat the following until we run out of potential
  6972. closers:
  6973. - Move `current_position` forward in the delimiter stack (if needed)
  6974. until we find the first potential closer with delimiter `*` or `_`.
  6975. (This will be the potential closer closest
  6976. to the beginning of the input -- the first one in parse order.)
  6977. - Now, look back in the stack (staying above `stack_bottom` and
  6978. the `openers_bottom` for this delimiter type) for the
  6979. first matching potential opener ("matching" means same delimiter).
  6980. - If one is found:
  6981. + Figure out whether we have emphasis or strong emphasis:
  6982. if both closer and opener spans have length >= 2, we have
  6983. strong, otherwise regular.
  6984. + Insert an emph or strong emph node accordingly, after
  6985. the text node corresponding to the opener.
  6986. + Remove any delimiters between the opener and closer from
  6987. the delimiter stack.
  6988. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  6989. from the opening and closing text nodes. If they become empty
  6990. as a result, remove them and remove the corresponding element
  6991. of the delimiter stack. If the closing node is removed, reset
  6992. `current_position` to the next element in the stack.
  6993. - If none in found:
  6994. + Set `openers_bottom` to the element before `current_position`.
  6995. (We know that there are no openers for this kind of closer up to and
  6996. including this point, so this puts a lower bound on future searches.)
  6997. + If the closer at `current_position` is not a potential opener,
  6998. remove it from the delimiter stack (since we know it can't
  6999. be a closer either).
  7000. + Advance `current_position` to the next element in the stack.
  7001. After we're done, we remove all delimiters above `stack_bottom` from the
  7002. delimiter stack.