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