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