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