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