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