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