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