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