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