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