aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: f14b227492bc42a9ad109ceaf527958515a597c5 (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.28
  5. date: '2017-08-01'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a github wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  252. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  253. A [punctuation character](@) is an [ASCII
  254. punctuation character] or anything in
  255. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  256. ## Tabs
  257. Tabs in lines are not expanded to [spaces]. However,
  258. in contexts where whitespace helps to define block structure,
  259. tabs behave as if they were replaced by spaces with a tab stop
  260. of 4 characters.
  261. Thus, for example, a tab can be used instead of four spaces
  262. in an indented code block. (Note, however, that internal
  263. tabs are passed through as literal tabs, not expanded to
  264. spaces.)
  265. ```````````````````````````````` example
  266. →foo→baz→→bim
  267. .
  268. <pre><code>foo→baz→→bim
  269. </code></pre>
  270. ````````````````````````````````
  271. ```````````````````````````````` example
  272. →foo→baz→→bim
  273. .
  274. <pre><code>foo→baz→→bim
  275. </code></pre>
  276. ````````````````````````````````
  277. ```````````````````````````````` example
  278. a→a
  279. ὐ→a
  280. .
  281. <pre><code>a→a
  282. ὐ→a
  283. </code></pre>
  284. ````````````````````````````````
  285. In the following example, a continuation paragraph of a list
  286. item is indented with a tab; this has exactly the same effect
  287. as indentation with four spaces would:
  288. ```````````````````````````````` example
  289. - foo
  290. →bar
  291. .
  292. <ul>
  293. <li>
  294. <p>foo</p>
  295. <p>bar</p>
  296. </li>
  297. </ul>
  298. ````````````````````````````````
  299. ```````````````````````````````` example
  300. - foo
  301. →→bar
  302. .
  303. <ul>
  304. <li>
  305. <p>foo</p>
  306. <pre><code> bar
  307. </code></pre>
  308. </li>
  309. </ul>
  310. ````````````````````````````````
  311. Normally the `>` that begins a block quote may be followed
  312. optionally by a space, which is not considered part of the
  313. content. In the following case `>` is followed by a tab,
  314. which is treated as if it were expanded into three spaces.
  315. Since one of these spaces is considered part of the
  316. delimiter, `foo` is considered to be indented six spaces
  317. inside the block quote context, so we get an indented
  318. code block starting with two spaces.
  319. ```````````````````````````````` example
  320. >→→foo
  321. .
  322. <blockquote>
  323. <pre><code> foo
  324. </code></pre>
  325. </blockquote>
  326. ````````````````````````````````
  327. ```````````````````````````````` example
  328. -→→foo
  329. .
  330. <ul>
  331. <li>
  332. <pre><code> foo
  333. </code></pre>
  334. </li>
  335. </ul>
  336. ````````````````````````````````
  337. ```````````````````````````````` example
  338. foo
  339. →bar
  340. .
  341. <pre><code>foo
  342. bar
  343. </code></pre>
  344. ````````````````````````````````
  345. ```````````````````````````````` example
  346. - foo
  347. - bar
  348. → - baz
  349. .
  350. <ul>
  351. <li>foo
  352. <ul>
  353. <li>bar
  354. <ul>
  355. <li>baz</li>
  356. </ul>
  357. </li>
  358. </ul>
  359. </li>
  360. </ul>
  361. ````````````````````````````````
  362. ```````````````````````````````` example
  363. #→Foo
  364. .
  365. <h1>Foo</h1>
  366. ````````````````````````````````
  367. ```````````````````````````````` example
  368. *→*→*→
  369. .
  370. <hr />
  371. ````````````````````````````````
  372. ## Insecure characters
  373. For security reasons, the Unicode character `U+0000` must be replaced
  374. with the REPLACEMENT CHARACTER (`U+FFFD`).
  375. # Blocks and inlines
  376. We can think of a document as a sequence of
  377. [blocks](@)---structural elements like paragraphs, block
  378. quotations, lists, headings, rules, and code blocks. Some blocks (like
  379. block quotes and list items) contain other blocks; others (like
  380. headings and paragraphs) contain [inline](@) content---text,
  381. links, emphasized text, images, code spans, and so on.
  382. ## Precedence
  383. Indicators of block structure always take precedence over indicators
  384. of inline structure. So, for example, the following is a list with
  385. two items, not a list with one item containing a code span:
  386. ```````````````````````````````` example
  387. - `one
  388. - two`
  389. .
  390. <ul>
  391. <li>`one</li>
  392. <li>two`</li>
  393. </ul>
  394. ````````````````````````````````
  395. This means that parsing can proceed in two steps: first, the block
  396. structure of the document can be discerned; second, text lines inside
  397. paragraphs, headings, and other block constructs can be parsed for inline
  398. structure. The second step requires information about link reference
  399. definitions that will be available only at the end of the first
  400. step. Note that the first step requires processing lines in sequence,
  401. but the second can be parallelized, since the inline parsing of
  402. one block element does not affect the inline parsing of any other.
  403. ## Container blocks and leaf blocks
  404. We can divide blocks into two types:
  405. [container block](@)s,
  406. which can contain other blocks, and [leaf block](@)s,
  407. which cannot.
  408. # Leaf blocks
  409. This section describes the different kinds of leaf block that make up a
  410. Markdown document.
  411. ## Thematic breaks
  412. A line consisting of 0-3 spaces of indentation, followed by a sequence
  413. of three or more matching `-`, `_`, or `*` characters, each followed
  414. optionally by any number of spaces, forms a
  415. [thematic break](@).
  416. ```````````````````````````````` example
  417. ***
  418. ---
  419. ___
  420. .
  421. <hr />
  422. <hr />
  423. <hr />
  424. ````````````````````````````````
  425. Wrong characters:
  426. ```````````````````````````````` example
  427. +++
  428. .
  429. <p>+++</p>
  430. ````````````````````````````````
  431. ```````````````````````````````` example
  432. ===
  433. .
  434. <p>===</p>
  435. ````````````````````````````````
  436. Not enough characters:
  437. ```````````````````````````````` example
  438. --
  439. **
  440. __
  441. .
  442. <p>--
  443. **
  444. __</p>
  445. ````````````````````````````````
  446. One to three spaces indent are allowed:
  447. ```````````````````````````````` example
  448. ***
  449. ***
  450. ***
  451. .
  452. <hr />
  453. <hr />
  454. <hr />
  455. ````````````````````````````````
  456. Four spaces is too many:
  457. ```````````````````````````````` example
  458. ***
  459. .
  460. <pre><code>***
  461. </code></pre>
  462. ````````````````````````````````
  463. ```````````````````````````````` example
  464. Foo
  465. ***
  466. .
  467. <p>Foo
  468. ***</p>
  469. ````````````````````````````````
  470. More than three characters may be used:
  471. ```````````````````````````````` example
  472. _____________________________________
  473. .
  474. <hr />
  475. ````````````````````````````````
  476. Spaces are allowed between the characters:
  477. ```````````````````````````````` example
  478. - - -
  479. .
  480. <hr />
  481. ````````````````````````````````
  482. ```````````````````````````````` example
  483. ** * ** * ** * **
  484. .
  485. <hr />
  486. ````````````````````````````````
  487. ```````````````````````````````` example
  488. - - - -
  489. .
  490. <hr />
  491. ````````````````````````````````
  492. Spaces are allowed at the end:
  493. ```````````````````````````````` example
  494. - - - -
  495. .
  496. <hr />
  497. ````````````````````````````````
  498. However, no other characters may occur in the line:
  499. ```````````````````````````````` example
  500. _ _ _ _ a
  501. a------
  502. ---a---
  503. .
  504. <p>_ _ _ _ a</p>
  505. <p>a------</p>
  506. <p>---a---</p>
  507. ````````````````````````````````
  508. It is required that all of the [non-whitespace characters] be the same.
  509. So, this is not a thematic break:
  510. ```````````````````````````````` example
  511. *-*
  512. .
  513. <p><em>-</em></p>
  514. ````````````````````````````````
  515. Thematic breaks do not need blank lines before or after:
  516. ```````````````````````````````` example
  517. - foo
  518. ***
  519. - bar
  520. .
  521. <ul>
  522. <li>foo</li>
  523. </ul>
  524. <hr />
  525. <ul>
  526. <li>bar</li>
  527. </ul>
  528. ````````````````````````````````
  529. Thematic breaks can interrupt a paragraph:
  530. ```````````````````````````````` example
  531. Foo
  532. ***
  533. bar
  534. .
  535. <p>Foo</p>
  536. <hr />
  537. <p>bar</p>
  538. ````````````````````````````````
  539. If a line of dashes that meets the above conditions for being a
  540. thematic break could also be interpreted as the underline of a [setext
  541. heading], the interpretation as a
  542. [setext heading] takes precedence. Thus, for example,
  543. this is a setext heading, not a paragraph followed by a thematic break:
  544. ```````````````````````````````` example
  545. Foo
  546. ---
  547. bar
  548. .
  549. <h2>Foo</h2>
  550. <p>bar</p>
  551. ````````````````````````````````
  552. When both a thematic break and a list item are possible
  553. interpretations of a line, the thematic break takes precedence:
  554. ```````````````````````````````` example
  555. * Foo
  556. * * *
  557. * Bar
  558. .
  559. <ul>
  560. <li>Foo</li>
  561. </ul>
  562. <hr />
  563. <ul>
  564. <li>Bar</li>
  565. </ul>
  566. ````````````````````````````````
  567. If you want a thematic break in a list item, use a different bullet:
  568. ```````````````````````````````` example
  569. - Foo
  570. - * * *
  571. .
  572. <ul>
  573. <li>Foo</li>
  574. <li>
  575. <hr />
  576. </li>
  577. </ul>
  578. ````````````````````````````````
  579. ## ATX headings
  580. An [ATX heading](@)
  581. consists of a string of characters, parsed as inline content, between an
  582. opening sequence of 1--6 unescaped `#` characters and an optional
  583. closing sequence of any number of unescaped `#` characters.
  584. The opening sequence of `#` characters must be followed by a
  585. [space] or by the end of line. The optional closing sequence of `#`s must be
  586. preceded by a [space] and may be followed by spaces only. The opening
  587. `#` character may be indented 0-3 spaces. The raw contents of the
  588. heading are stripped of leading and trailing spaces before being parsed
  589. as inline content. The heading level is equal to the number of `#`
  590. characters in the opening sequence.
  591. Simple headings:
  592. ```````````````````````````````` example
  593. # foo
  594. ## foo
  595. ### foo
  596. #### foo
  597. ##### foo
  598. ###### foo
  599. .
  600. <h1>foo</h1>
  601. <h2>foo</h2>
  602. <h3>foo</h3>
  603. <h4>foo</h4>
  604. <h5>foo</h5>
  605. <h6>foo</h6>
  606. ````````````````````````````````
  607. More than six `#` characters is not a heading:
  608. ```````````````````````````````` example
  609. ####### foo
  610. .
  611. <p>####### foo</p>
  612. ````````````````````````````````
  613. At least one space is required between the `#` characters and the
  614. heading's contents, unless the heading is empty. Note that many
  615. implementations currently do not require the space. However, the
  616. space was required by the
  617. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  618. and it helps prevent things like the following from being parsed as
  619. headings:
  620. ```````````````````````````````` example
  621. #5 bolt
  622. #hashtag
  623. .
  624. <p>#5 bolt</p>
  625. <p>#hashtag</p>
  626. ````````````````````````````````
  627. This is not a heading, because the first `#` is escaped:
  628. ```````````````````````````````` example
  629. \## foo
  630. .
  631. <p>## foo</p>
  632. ````````````````````````````````
  633. Contents are parsed as inlines:
  634. ```````````````````````````````` example
  635. # foo *bar* \*baz\*
  636. .
  637. <h1>foo <em>bar</em> *baz*</h1>
  638. ````````````````````````````````
  639. Leading and trailing blanks are ignored in parsing inline content:
  640. ```````````````````````````````` example
  641. # foo
  642. .
  643. <h1>foo</h1>
  644. ````````````````````````````````
  645. One to three spaces indentation are allowed:
  646. ```````````````````````````````` example
  647. ### foo
  648. ## foo
  649. # foo
  650. .
  651. <h3>foo</h3>
  652. <h2>foo</h2>
  653. <h1>foo</h1>
  654. ````````````````````````````````
  655. Four spaces are too much:
  656. ```````````````````````````````` example
  657. # foo
  658. .
  659. <pre><code># foo
  660. </code></pre>
  661. ````````````````````````````````
  662. ```````````````````````````````` example
  663. foo
  664. # bar
  665. .
  666. <p>foo
  667. # bar</p>
  668. ````````````````````````````````
  669. A closing sequence of `#` characters is optional:
  670. ```````````````````````````````` example
  671. ## foo ##
  672. ### bar ###
  673. .
  674. <h2>foo</h2>
  675. <h3>bar</h3>
  676. ````````````````````````````````
  677. It need not be the same length as the opening sequence:
  678. ```````````````````````````````` example
  679. # foo ##################################
  680. ##### foo ##
  681. .
  682. <h1>foo</h1>
  683. <h5>foo</h5>
  684. ````````````````````````````````
  685. Spaces are allowed after the closing sequence:
  686. ```````````````````````````````` example
  687. ### foo ###
  688. .
  689. <h3>foo</h3>
  690. ````````````````````````````````
  691. A sequence of `#` characters with anything but [spaces] following it
  692. is not a closing sequence, but counts as part of the contents of the
  693. heading:
  694. ```````````````````````````````` example
  695. ### foo ### b
  696. .
  697. <h3>foo ### b</h3>
  698. ````````````````````````````````
  699. The closing sequence must be preceded by a space:
  700. ```````````````````````````````` example
  701. # foo#
  702. .
  703. <h1>foo#</h1>
  704. ````````````````````````````````
  705. Backslash-escaped `#` characters do not count as part
  706. of the closing sequence:
  707. ```````````````````````````````` example
  708. ### foo \###
  709. ## foo #\##
  710. # foo \#
  711. .
  712. <h3>foo ###</h3>
  713. <h2>foo ###</h2>
  714. <h1>foo #</h1>
  715. ````````````````````````````````
  716. ATX headings need not be separated from surrounding content by blank
  717. lines, and they can interrupt paragraphs:
  718. ```````````````````````````````` example
  719. ****
  720. ## foo
  721. ****
  722. .
  723. <hr />
  724. <h2>foo</h2>
  725. <hr />
  726. ````````````````````````````````
  727. ```````````````````````````````` example
  728. Foo bar
  729. # baz
  730. Bar foo
  731. .
  732. <p>Foo bar</p>
  733. <h1>baz</h1>
  734. <p>Bar foo</p>
  735. ````````````````````````````````
  736. ATX headings can be empty:
  737. ```````````````````````````````` example
  738. ##
  739. #
  740. ### ###
  741. .
  742. <h2></h2>
  743. <h1></h1>
  744. <h3></h3>
  745. ````````````````````````````````
  746. ## Setext headings
  747. A [setext heading](@) consists of one or more
  748. lines of text, each containing at least one [non-whitespace
  749. character], with no more than 3 spaces indentation, followed by
  750. a [setext heading underline]. The lines of text must be such
  751. that, were they not followed by the setext heading underline,
  752. they would be interpreted as a paragraph: they cannot be
  753. interpretable as a [code fence], [ATX heading][ATX headings],
  754. [block quote][block quotes], [thematic break][thematic breaks],
  755. [list item][list items], or [HTML block][HTML blocks].
  756. A [setext heading underline](@) is a sequence of
  757. `=` characters or a sequence of `-` characters, with no more than 3
  758. spaces indentation and any number of trailing spaces. If a line
  759. containing a single `-` can be interpreted as an
  760. empty [list items], it should be interpreted this way
  761. and not as a [setext heading underline].
  762. The heading is a level 1 heading if `=` characters are used in
  763. the [setext heading underline], and a level 2 heading if `-`
  764. characters are used. The contents of the heading are the result
  765. of parsing the preceding lines of text as CommonMark inline
  766. content.
  767. In general, a setext heading need not be preceded or followed by a
  768. blank line. However, it cannot interrupt a paragraph, so when a
  769. setext heading comes after a paragraph, a blank line is needed between
  770. them.
  771. Simple examples:
  772. ```````````````````````````````` example
  773. Foo *bar*
  774. =========
  775. Foo *bar*
  776. ---------
  777. .
  778. <h1>Foo <em>bar</em></h1>
  779. <h2>Foo <em>bar</em></h2>
  780. ````````````````````````````````
  781. The content of the header may span more than one line:
  782. ```````````````````````````````` example
  783. Foo *bar
  784. baz*
  785. ====
  786. .
  787. <h1>Foo <em>bar
  788. baz</em></h1>
  789. ````````````````````````````````
  790. The underlining can be any length:
  791. ```````````````````````````````` example
  792. Foo
  793. -------------------------
  794. Foo
  795. =
  796. .
  797. <h2>Foo</h2>
  798. <h1>Foo</h1>
  799. ````````````````````````````````
  800. The heading content can be indented up to three spaces, and need
  801. not line up with the underlining:
  802. ```````````````````````````````` example
  803. Foo
  804. ---
  805. Foo
  806. -----
  807. Foo
  808. ===
  809. .
  810. <h2>Foo</h2>
  811. <h2>Foo</h2>
  812. <h1>Foo</h1>
  813. ````````````````````````````````
  814. Four spaces indent is too much:
  815. ```````````````````````````````` example
  816. Foo
  817. ---
  818. Foo
  819. ---
  820. .
  821. <pre><code>Foo
  822. ---
  823. Foo
  824. </code></pre>
  825. <hr />
  826. ````````````````````````````````
  827. The setext heading underline can be indented up to three spaces, and
  828. may have trailing spaces:
  829. ```````````````````````````````` example
  830. Foo
  831. ----
  832. .
  833. <h2>Foo</h2>
  834. ````````````````````````````````
  835. Four spaces is too much:
  836. ```````````````````````````````` example
  837. Foo
  838. ---
  839. .
  840. <p>Foo
  841. ---</p>
  842. ````````````````````````````````
  843. The setext heading underline cannot contain internal spaces:
  844. ```````````````````````````````` example
  845. Foo
  846. = =
  847. Foo
  848. --- -
  849. .
  850. <p>Foo
  851. = =</p>
  852. <p>Foo</p>
  853. <hr />
  854. ````````````````````````````````
  855. Trailing spaces in the content line do not cause a line break:
  856. ```````````````````````````````` example
  857. Foo
  858. -----
  859. .
  860. <h2>Foo</h2>
  861. ````````````````````````````````
  862. Nor does a backslash at the end:
  863. ```````````````````````````````` example
  864. Foo\
  865. ----
  866. .
  867. <h2>Foo\</h2>
  868. ````````````````````````````````
  869. Since indicators of block structure take precedence over
  870. indicators of inline structure, the following are setext headings:
  871. ```````````````````````````````` example
  872. `Foo
  873. ----
  874. `
  875. <a title="a lot
  876. ---
  877. of dashes"/>
  878. .
  879. <h2>`Foo</h2>
  880. <p>`</p>
  881. <h2>&lt;a title=&quot;a lot</h2>
  882. <p>of dashes&quot;/&gt;</p>
  883. ````````````````````````````````
  884. The setext heading underline cannot be a [lazy continuation
  885. line] in a list item or block quote:
  886. ```````````````````````````````` example
  887. > Foo
  888. ---
  889. .
  890. <blockquote>
  891. <p>Foo</p>
  892. </blockquote>
  893. <hr />
  894. ````````````````````````````````
  895. ```````````````````````````````` example
  896. > foo
  897. bar
  898. ===
  899. .
  900. <blockquote>
  901. <p>foo
  902. bar
  903. ===</p>
  904. </blockquote>
  905. ````````````````````````````````
  906. ```````````````````````````````` example
  907. - Foo
  908. ---
  909. .
  910. <ul>
  911. <li>Foo</li>
  912. </ul>
  913. <hr />
  914. ````````````````````````````````
  915. A blank line is needed between a paragraph and a following
  916. setext heading, since otherwise the paragraph becomes part
  917. of the heading's content:
  918. ```````````````````````````````` example
  919. Foo
  920. Bar
  921. ---
  922. .
  923. <h2>Foo
  924. Bar</h2>
  925. ````````````````````````````````
  926. But in general a blank line is not required before or after
  927. setext headings:
  928. ```````````````````````````````` example
  929. ---
  930. Foo
  931. ---
  932. Bar
  933. ---
  934. Baz
  935. .
  936. <hr />
  937. <h2>Foo</h2>
  938. <h2>Bar</h2>
  939. <p>Baz</p>
  940. ````````````````````````````````
  941. Setext headings cannot be empty:
  942. ```````````````````````````````` example
  943. ====
  944. .
  945. <p>====</p>
  946. ````````````````````````````````
  947. Setext heading text lines must not be interpretable as block
  948. constructs other than paragraphs. So, the line of dashes
  949. in these examples gets interpreted as a thematic break:
  950. ```````````````````````````````` example
  951. ---
  952. ---
  953. .
  954. <hr />
  955. <hr />
  956. ````````````````````````````````
  957. ```````````````````````````````` example
  958. - foo
  959. -----
  960. .
  961. <ul>
  962. <li>foo</li>
  963. </ul>
  964. <hr />
  965. ````````````````````````````````
  966. ```````````````````````````````` example
  967. foo
  968. ---
  969. .
  970. <pre><code>foo
  971. </code></pre>
  972. <hr />
  973. ````````````````````````````````
  974. ```````````````````````````````` example
  975. > foo
  976. -----
  977. .
  978. <blockquote>
  979. <p>foo</p>
  980. </blockquote>
  981. <hr />
  982. ````````````````````````````````
  983. If you want a heading with `> foo` as its literal text, you can
  984. use backslash escapes:
  985. ```````````````````````````````` example
  986. \> foo
  987. ------
  988. .
  989. <h2>&gt; foo</h2>
  990. ````````````````````````````````
  991. **Compatibility note:** Most existing Markdown implementations
  992. do not allow the text of setext headings to span multiple lines.
  993. But there is no consensus about how to interpret
  994. ``` markdown
  995. Foo
  996. bar
  997. ---
  998. baz
  999. ```
  1000. One can find four different interpretations:
  1001. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1002. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1003. 3. paragraph "Foo bar --- baz"
  1004. 4. heading "Foo bar", paragraph "baz"
  1005. We find interpretation 4 most natural, and interpretation 4
  1006. increases the expressive power of CommonMark, by allowing
  1007. multiline headings. Authors who want interpretation 1 can
  1008. put a blank line after the first paragraph:
  1009. ```````````````````````````````` example
  1010. Foo
  1011. bar
  1012. ---
  1013. baz
  1014. .
  1015. <p>Foo</p>
  1016. <h2>bar</h2>
  1017. <p>baz</p>
  1018. ````````````````````````````````
  1019. Authors who want interpretation 2 can put blank lines around
  1020. the thematic break,
  1021. ```````````````````````````````` example
  1022. Foo
  1023. bar
  1024. ---
  1025. baz
  1026. .
  1027. <p>Foo
  1028. bar</p>
  1029. <hr />
  1030. <p>baz</p>
  1031. ````````````````````````````````
  1032. or use a thematic break that cannot count as a [setext heading
  1033. underline], such as
  1034. ```````````````````````````````` example
  1035. Foo
  1036. bar
  1037. * * *
  1038. baz
  1039. .
  1040. <p>Foo
  1041. bar</p>
  1042. <hr />
  1043. <p>baz</p>
  1044. ````````````````````````````````
  1045. Authors who want interpretation 3 can use backslash escapes:
  1046. ```````````````````````````````` example
  1047. Foo
  1048. bar
  1049. \---
  1050. baz
  1051. .
  1052. <p>Foo
  1053. bar
  1054. ---
  1055. baz</p>
  1056. ````````````````````````````````
  1057. ## Indented code blocks
  1058. An [indented code block](@) is composed of one or more
  1059. [indented chunks] separated by blank lines.
  1060. An [indented chunk](@) is a sequence of non-blank lines,
  1061. each indented four or more spaces. The contents of the code block are
  1062. the literal contents of the lines, including trailing
  1063. [line endings], minus four spaces of indentation.
  1064. An indented code block has no [info string].
  1065. An indented code block cannot interrupt a paragraph, so there must be
  1066. a blank line between a paragraph and a following indented code block.
  1067. (A blank line is not needed, however, between a code block and a following
  1068. paragraph.)
  1069. ```````````````````````````````` example
  1070. a simple
  1071. indented code block
  1072. .
  1073. <pre><code>a simple
  1074. indented code block
  1075. </code></pre>
  1076. ````````````````````````````````
  1077. If there is any ambiguity between an interpretation of indentation
  1078. as a code block and as indicating that material belongs to a [list
  1079. item][list items], the list item interpretation takes precedence:
  1080. ```````````````````````````````` example
  1081. - foo
  1082. bar
  1083. .
  1084. <ul>
  1085. <li>
  1086. <p>foo</p>
  1087. <p>bar</p>
  1088. </li>
  1089. </ul>
  1090. ````````````````````````````````
  1091. ```````````````````````````````` example
  1092. 1. foo
  1093. - bar
  1094. .
  1095. <ol>
  1096. <li>
  1097. <p>foo</p>
  1098. <ul>
  1099. <li>bar</li>
  1100. </ul>
  1101. </li>
  1102. </ol>
  1103. ````````````````````````````````
  1104. The contents of a code block are literal text, and do not get parsed
  1105. as Markdown:
  1106. ```````````````````````````````` example
  1107. <a/>
  1108. *hi*
  1109. - one
  1110. .
  1111. <pre><code>&lt;a/&gt;
  1112. *hi*
  1113. - one
  1114. </code></pre>
  1115. ````````````````````````````````
  1116. Here we have three chunks separated by blank lines:
  1117. ```````````````````````````````` example
  1118. chunk1
  1119. chunk2
  1120. chunk3
  1121. .
  1122. <pre><code>chunk1
  1123. chunk2
  1124. chunk3
  1125. </code></pre>
  1126. ````````````````````````````````
  1127. Any initial spaces beyond four will be included in the content, even
  1128. in interior blank lines:
  1129. ```````````````````````````````` example
  1130. chunk1
  1131. chunk2
  1132. .
  1133. <pre><code>chunk1
  1134. chunk2
  1135. </code></pre>
  1136. ````````````````````````````````
  1137. An indented code block cannot interrupt a paragraph. (This
  1138. allows hanging indents and the like.)
  1139. ```````````````````````````````` example
  1140. Foo
  1141. bar
  1142. .
  1143. <p>Foo
  1144. bar</p>
  1145. ````````````````````````````````
  1146. However, any non-blank line with fewer than four leading spaces ends
  1147. the code block immediately. So a paragraph may occur immediately
  1148. after indented code:
  1149. ```````````````````````````````` example
  1150. foo
  1151. bar
  1152. .
  1153. <pre><code>foo
  1154. </code></pre>
  1155. <p>bar</p>
  1156. ````````````````````````````````
  1157. And indented code can occur immediately before and after other kinds of
  1158. blocks:
  1159. ```````````````````````````````` example
  1160. # Heading
  1161. foo
  1162. Heading
  1163. ------
  1164. foo
  1165. ----
  1166. .
  1167. <h1>Heading</h1>
  1168. <pre><code>foo
  1169. </code></pre>
  1170. <h2>Heading</h2>
  1171. <pre><code>foo
  1172. </code></pre>
  1173. <hr />
  1174. ````````````````````````````````
  1175. The first line can be indented more than four spaces:
  1176. ```````````````````````````````` example
  1177. foo
  1178. bar
  1179. .
  1180. <pre><code> foo
  1181. bar
  1182. </code></pre>
  1183. ````````````````````````````````
  1184. Blank lines preceding or following an indented code block
  1185. are not included in it:
  1186. ```````````````````````````````` example
  1187. foo
  1188. .
  1189. <pre><code>foo
  1190. </code></pre>
  1191. ````````````````````````````````
  1192. Trailing spaces are included in the code block's content:
  1193. ```````````````````````````````` example
  1194. foo
  1195. .
  1196. <pre><code>foo
  1197. </code></pre>
  1198. ````````````````````````````````
  1199. ## Fenced code blocks
  1200. A [code fence](@) is a sequence
  1201. of at least three consecutive backtick characters (`` ` ``) or
  1202. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1203. A [fenced code block](@)
  1204. begins with a code fence, indented no more than three spaces.
  1205. The line with the opening code fence may optionally contain some text
  1206. following the code fence; this is trimmed of leading and trailing
  1207. spaces and called the [info string](@).
  1208. The [info string] may not contain any backtick
  1209. characters. (The reason for this restriction is that otherwise
  1210. some inline code would be incorrectly interpreted as the
  1211. beginning of a fenced code block.)
  1212. The content of the code block consists of all subsequent lines, until
  1213. a closing [code fence] of the same type as the code block
  1214. began with (backticks or tildes), and with at least as many backticks
  1215. or tildes as the opening code fence. If the leading code fence is
  1216. indented N spaces, then up to N spaces of indentation are removed from
  1217. each line of the content (if present). (If a content line is not
  1218. indented, it is preserved unchanged. If it is indented less than N
  1219. spaces, all of the indentation is removed.)
  1220. The closing code fence may be indented up to three spaces, and may be
  1221. followed only by spaces, which are ignored. If the end of the
  1222. containing block (or document) is reached and no closing code fence
  1223. has been found, the code block contains all of the lines after the
  1224. opening code fence until the end of the containing block (or
  1225. document). (An alternative spec would require backtracking in the
  1226. event that a closing code fence is not found. But this makes parsing
  1227. much less efficient, and there seems to be no real down side to the
  1228. behavior described here.)
  1229. A fenced code block may interrupt a paragraph, and does not require
  1230. a blank line either before or after.
  1231. The content of a code fence is treated as literal text, not parsed
  1232. as inlines. The first word of the [info string] is typically used to
  1233. specify the language of the code sample, and rendered in the `class`
  1234. attribute of the `code` tag. However, this spec does not mandate any
  1235. particular treatment of the [info string].
  1236. Here is a simple example with backticks:
  1237. ```````````````````````````````` example
  1238. ```
  1239. <
  1240. >
  1241. ```
  1242. .
  1243. <pre><code>&lt;
  1244. &gt;
  1245. </code></pre>
  1246. ````````````````````````````````
  1247. With tildes:
  1248. ```````````````````````````````` example
  1249. ~~~
  1250. <
  1251. >
  1252. ~~~
  1253. .
  1254. <pre><code>&lt;
  1255. &gt;
  1256. </code></pre>
  1257. ````````````````````````````````
  1258. Fewer than three backticks is not enough:
  1259. ```````````````````````````````` example
  1260. ``
  1261. foo
  1262. ``
  1263. .
  1264. <p><code>foo</code></p>
  1265. ````````````````````````````````
  1266. The closing code fence must use the same character as the opening
  1267. fence:
  1268. ```````````````````````````````` example
  1269. ```
  1270. aaa
  1271. ~~~
  1272. ```
  1273. .
  1274. <pre><code>aaa
  1275. ~~~
  1276. </code></pre>
  1277. ````````````````````````````````
  1278. ```````````````````````````````` example
  1279. ~~~
  1280. aaa
  1281. ```
  1282. ~~~
  1283. .
  1284. <pre><code>aaa
  1285. ```
  1286. </code></pre>
  1287. ````````````````````````````````
  1288. The closing code fence must be at least as long as the opening fence:
  1289. ```````````````````````````````` example
  1290. ````
  1291. aaa
  1292. ```
  1293. ``````
  1294. .
  1295. <pre><code>aaa
  1296. ```
  1297. </code></pre>
  1298. ````````````````````````````````
  1299. ```````````````````````````````` example
  1300. ~~~~
  1301. aaa
  1302. ~~~
  1303. ~~~~
  1304. .
  1305. <pre><code>aaa
  1306. ~~~
  1307. </code></pre>
  1308. ````````````````````````````````
  1309. Unclosed code blocks are closed by the end of the document
  1310. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1311. ```````````````````````````````` example
  1312. ```
  1313. .
  1314. <pre><code></code></pre>
  1315. ````````````````````````````````
  1316. ```````````````````````````````` example
  1317. `````
  1318. ```
  1319. aaa
  1320. .
  1321. <pre><code>
  1322. ```
  1323. aaa
  1324. </code></pre>
  1325. ````````````````````````````````
  1326. ```````````````````````````````` example
  1327. > ```
  1328. > aaa
  1329. bbb
  1330. .
  1331. <blockquote>
  1332. <pre><code>aaa
  1333. </code></pre>
  1334. </blockquote>
  1335. <p>bbb</p>
  1336. ````````````````````````````````
  1337. A code block can have all empty lines as its content:
  1338. ```````````````````````````````` example
  1339. ```
  1340. ```
  1341. .
  1342. <pre><code>
  1343. </code></pre>
  1344. ````````````````````````````````
  1345. A code block can be empty:
  1346. ```````````````````````````````` example
  1347. ```
  1348. ```
  1349. .
  1350. <pre><code></code></pre>
  1351. ````````````````````````````````
  1352. Fences can be indented. If the opening fence is indented,
  1353. content lines will have equivalent opening indentation removed,
  1354. if present:
  1355. ```````````````````````````````` example
  1356. ```
  1357. aaa
  1358. aaa
  1359. ```
  1360. .
  1361. <pre><code>aaa
  1362. aaa
  1363. </code></pre>
  1364. ````````````````````````````````
  1365. ```````````````````````````````` example
  1366. ```
  1367. aaa
  1368. aaa
  1369. aaa
  1370. ```
  1371. .
  1372. <pre><code>aaa
  1373. aaa
  1374. aaa
  1375. </code></pre>
  1376. ````````````````````````````````
  1377. ```````````````````````````````` example
  1378. ```
  1379. aaa
  1380. aaa
  1381. aaa
  1382. ```
  1383. .
  1384. <pre><code>aaa
  1385. aaa
  1386. aaa
  1387. </code></pre>
  1388. ````````````````````````````````
  1389. Four spaces indentation produces an indented code block:
  1390. ```````````````````````````````` example
  1391. ```
  1392. aaa
  1393. ```
  1394. .
  1395. <pre><code>```
  1396. aaa
  1397. ```
  1398. </code></pre>
  1399. ````````````````````````````````
  1400. Closing fences may be indented by 0-3 spaces, and their indentation
  1401. need not match that of the opening fence:
  1402. ```````````````````````````````` example
  1403. ```
  1404. aaa
  1405. ```
  1406. .
  1407. <pre><code>aaa
  1408. </code></pre>
  1409. ````````````````````````````````
  1410. ```````````````````````````````` example
  1411. ```
  1412. aaa
  1413. ```
  1414. .
  1415. <pre><code>aaa
  1416. </code></pre>
  1417. ````````````````````````````````
  1418. This is not a closing fence, because it is indented 4 spaces:
  1419. ```````````````````````````````` example
  1420. ```
  1421. aaa
  1422. ```
  1423. .
  1424. <pre><code>aaa
  1425. ```
  1426. </code></pre>
  1427. ````````````````````````````````
  1428. Code fences (opening and closing) cannot contain internal spaces:
  1429. ```````````````````````````````` example
  1430. ``` ```
  1431. aaa
  1432. .
  1433. <p><code></code>
  1434. aaa</p>
  1435. ````````````````````````````````
  1436. ```````````````````````````````` example
  1437. ~~~~~~
  1438. aaa
  1439. ~~~ ~~
  1440. .
  1441. <pre><code>aaa
  1442. ~~~ ~~
  1443. </code></pre>
  1444. ````````````````````````````````
  1445. Fenced code blocks can interrupt paragraphs, and can be followed
  1446. directly by paragraphs, without a blank line between:
  1447. ```````````````````````````````` example
  1448. foo
  1449. ```
  1450. bar
  1451. ```
  1452. baz
  1453. .
  1454. <p>foo</p>
  1455. <pre><code>bar
  1456. </code></pre>
  1457. <p>baz</p>
  1458. ````````````````````````````````
  1459. Other blocks can also occur before and after fenced code blocks
  1460. without an intervening blank line:
  1461. ```````````````````````````````` example
  1462. foo
  1463. ---
  1464. ~~~
  1465. bar
  1466. ~~~
  1467. # baz
  1468. .
  1469. <h2>foo</h2>
  1470. <pre><code>bar
  1471. </code></pre>
  1472. <h1>baz</h1>
  1473. ````````````````````````````````
  1474. An [info string] can be provided after the opening code fence.
  1475. Opening and closing spaces will be stripped, and the first word, prefixed
  1476. with `language-`, is used as the value for the `class` attribute of the
  1477. `code` element within the enclosing `pre` element.
  1478. ```````````````````````````````` example
  1479. ```ruby
  1480. def foo(x)
  1481. return 3
  1482. end
  1483. ```
  1484. .
  1485. <pre><code class="language-ruby">def foo(x)
  1486. return 3
  1487. end
  1488. </code></pre>
  1489. ````````````````````````````````
  1490. ```````````````````````````````` example
  1491. ~~~~ ruby startline=3 $%@#$
  1492. def foo(x)
  1493. return 3
  1494. end
  1495. ~~~~~~~
  1496. .
  1497. <pre><code class="language-ruby">def foo(x)
  1498. return 3
  1499. end
  1500. </code></pre>
  1501. ````````````````````````````````
  1502. ```````````````````````````````` example
  1503. ````;
  1504. ````
  1505. .
  1506. <pre><code class="language-;"></code></pre>
  1507. ````````````````````````````````
  1508. [Info strings] for backtick code blocks cannot contain backticks:
  1509. ```````````````````````````````` example
  1510. ``` aa ```
  1511. foo
  1512. .
  1513. <p><code>aa</code>
  1514. foo</p>
  1515. ````````````````````````````````
  1516. Closing code fences cannot have [info strings]:
  1517. ```````````````````````````````` example
  1518. ```
  1519. ``` aaa
  1520. ```
  1521. .
  1522. <pre><code>``` aaa
  1523. </code></pre>
  1524. ````````````````````````````````
  1525. ## HTML blocks
  1526. An [HTML block](@) is a group of lines that is treated
  1527. as raw HTML (and will not be escaped in HTML output).
  1528. There are seven kinds of [HTML block], which can be defined
  1529. by their start and end conditions. The block begins with a line that
  1530. meets a [start condition](@) (after up to three spaces
  1531. optional indentation). It ends with the first subsequent line that
  1532. meets a matching [end condition](@), or the last line of
  1533. the document or other [container block]), if no line is encountered that meets the
  1534. [end condition]. If the first line meets both the [start condition]
  1535. and the [end condition], the block will contain just that line.
  1536. 1. **Start condition:** line begins with the string `<script`,
  1537. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1538. the string `>`, or the end of the line.\
  1539. **End condition:** line contains an end tag
  1540. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1541. need not match the start tag).
  1542. 2. **Start condition:** line begins with the string `<!--`.\
  1543. **End condition:** line contains the string `-->`.
  1544. 3. **Start condition:** line begins with the string `<?`.\
  1545. **End condition:** line contains the string `?>`.
  1546. 4. **Start condition:** line begins with the string `<!`
  1547. followed by an uppercase ASCII letter.\
  1548. **End condition:** line contains the character `>`.
  1549. 5. **Start condition:** line begins with the string
  1550. `<![CDATA[`.\
  1551. **End condition:** line contains the string `]]>`.
  1552. 6. **Start condition:** line begins the string `<` or `</`
  1553. followed by one of the strings (case-insensitive) `address`,
  1554. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1555. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1556. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1557. `footer`, `form`, `frame`, `frameset`,
  1558. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1559. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1560. `meta`, `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1561. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1562. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1563. by [whitespace], the end of the line, the string `>`, or
  1564. the string `/>`.\
  1565. **End condition:** line is followed by a [blank line].
  1566. 7. **Start condition:** line begins with a complete [open tag]
  1567. or [closing tag] (with any [tag name] other than `script`,
  1568. `style`, or `pre`) followed only by [whitespace]
  1569. or the end of the line.\
  1570. **End condition:** line is followed by a [blank line].
  1571. HTML blocks continue until they are closed by their appropriate
  1572. [end condition], or the last line of the document or other [container block].
  1573. This means any HTML **within an HTML block** that might otherwise be recognised
  1574. as a start condition will be ignored by the parser and passed through as-is,
  1575. without changing the parser's state.
  1576. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1577. the parser state; as the HTML block was started in by start condition 6, it
  1578. will end at any blank line. This can be surprising:
  1579. ```````````````````````````````` example
  1580. <table><tr><td>
  1581. <pre>
  1582. **Hello**,
  1583. _world_.
  1584. </pre>
  1585. </td></tr></table>
  1586. .
  1587. <table><tr><td>
  1588. <pre>
  1589. **Hello**,
  1590. <p><em>world</em>.
  1591. </pre></p>
  1592. </td></tr></table>
  1593. ````````````````````````````````
  1594. In this case, the HTML block is terminated by the newline — the `**hello**`
  1595. text remains verbatim — and regular parsing resumes, with a paragraph,
  1596. emphasised `world` and inline and block HTML following.
  1597. All types of [HTML blocks] except type 7 may interrupt
  1598. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1599. (This restriction is intended to prevent unwanted interpretation
  1600. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1601. Some simple examples follow. Here are some basic HTML blocks
  1602. of type 6:
  1603. ```````````````````````````````` example
  1604. <table>
  1605. <tr>
  1606. <td>
  1607. hi
  1608. </td>
  1609. </tr>
  1610. </table>
  1611. okay.
  1612. .
  1613. <table>
  1614. <tr>
  1615. <td>
  1616. hi
  1617. </td>
  1618. </tr>
  1619. </table>
  1620. <p>okay.</p>
  1621. ````````````````````````````````
  1622. ```````````````````````````````` example
  1623. <div>
  1624. *hello*
  1625. <foo><a>
  1626. .
  1627. <div>
  1628. *hello*
  1629. <foo><a>
  1630. ````````````````````````````````
  1631. A block can also start with a closing tag:
  1632. ```````````````````````````````` example
  1633. </div>
  1634. *foo*
  1635. .
  1636. </div>
  1637. *foo*
  1638. ````````````````````````````````
  1639. Here we have two HTML blocks with a Markdown paragraph between them:
  1640. ```````````````````````````````` example
  1641. <DIV CLASS="foo">
  1642. *Markdown*
  1643. </DIV>
  1644. .
  1645. <DIV CLASS="foo">
  1646. <p><em>Markdown</em></p>
  1647. </DIV>
  1648. ````````````````````````````````
  1649. The tag on the first line can be partial, as long
  1650. as it is split where there would be whitespace:
  1651. ```````````````````````````````` example
  1652. <div id="foo"
  1653. class="bar">
  1654. </div>
  1655. .
  1656. <div id="foo"
  1657. class="bar">
  1658. </div>
  1659. ````````````````````````````````
  1660. ```````````````````````````````` example
  1661. <div id="foo" class="bar
  1662. baz">
  1663. </div>
  1664. .
  1665. <div id="foo" class="bar
  1666. baz">
  1667. </div>
  1668. ````````````````````````````````
  1669. An open tag need not be closed:
  1670. ```````````````````````````````` example
  1671. <div>
  1672. *foo*
  1673. *bar*
  1674. .
  1675. <div>
  1676. *foo*
  1677. <p><em>bar</em></p>
  1678. ````````````````````````````````
  1679. A partial tag need not even be completed (garbage
  1680. in, garbage out):
  1681. ```````````````````````````````` example
  1682. <div id="foo"
  1683. *hi*
  1684. .
  1685. <div id="foo"
  1686. *hi*
  1687. ````````````````````````````````
  1688. ```````````````````````````````` example
  1689. <div class
  1690. foo
  1691. .
  1692. <div class
  1693. foo
  1694. ````````````````````````````````
  1695. The initial tag doesn't even need to be a valid
  1696. tag, as long as it starts like one:
  1697. ```````````````````````````````` example
  1698. <div *???-&&&-<---
  1699. *foo*
  1700. .
  1701. <div *???-&&&-<---
  1702. *foo*
  1703. ````````````````````````````````
  1704. In type 6 blocks, the initial tag need not be on a line by
  1705. itself:
  1706. ```````````````````````````````` example
  1707. <div><a href="bar">*foo*</a></div>
  1708. .
  1709. <div><a href="bar">*foo*</a></div>
  1710. ````````````````````````````````
  1711. ```````````````````````````````` example
  1712. <table><tr><td>
  1713. foo
  1714. </td></tr></table>
  1715. .
  1716. <table><tr><td>
  1717. foo
  1718. </td></tr></table>
  1719. ````````````````````````````````
  1720. Everything until the next blank line or end of document
  1721. gets included in the HTML block. So, in the following
  1722. example, what looks like a Markdown code block
  1723. is actually part of the HTML block, which continues until a blank
  1724. line or the end of the document is reached:
  1725. ```````````````````````````````` example
  1726. <div></div>
  1727. ``` c
  1728. int x = 33;
  1729. ```
  1730. .
  1731. <div></div>
  1732. ``` c
  1733. int x = 33;
  1734. ```
  1735. ````````````````````````````````
  1736. To start an [HTML block] with a tag that is *not* in the
  1737. list of block-level tags in (6), you must put the tag by
  1738. itself on the first line (and it must be complete):
  1739. ```````````````````````````````` example
  1740. <a href="foo">
  1741. *bar*
  1742. </a>
  1743. .
  1744. <a href="foo">
  1745. *bar*
  1746. </a>
  1747. ````````````````````````````````
  1748. In type 7 blocks, the [tag name] can be anything:
  1749. ```````````````````````````````` example
  1750. <Warning>
  1751. *bar*
  1752. </Warning>
  1753. .
  1754. <Warning>
  1755. *bar*
  1756. </Warning>
  1757. ````````````````````````````````
  1758. ```````````````````````````````` example
  1759. <i class="foo">
  1760. *bar*
  1761. </i>
  1762. .
  1763. <i class="foo">
  1764. *bar*
  1765. </i>
  1766. ````````````````````````````````
  1767. ```````````````````````````````` example
  1768. </ins>
  1769. *bar*
  1770. .
  1771. </ins>
  1772. *bar*
  1773. ````````````````````````````````
  1774. These rules are designed to allow us to work with tags that
  1775. can function as either block-level or inline-level tags.
  1776. The `<del>` tag is a nice example. We can surround content with
  1777. `<del>` tags in three different ways. In this case, we get a raw
  1778. HTML block, because the `<del>` tag is on a line by itself:
  1779. ```````````````````````````````` example
  1780. <del>
  1781. *foo*
  1782. </del>
  1783. .
  1784. <del>
  1785. *foo*
  1786. </del>
  1787. ````````````````````````````````
  1788. In this case, we get a raw HTML block that just includes
  1789. the `<del>` tag (because it ends with the following blank
  1790. line). So the contents get interpreted as CommonMark:
  1791. ```````````````````````````````` example
  1792. <del>
  1793. *foo*
  1794. </del>
  1795. .
  1796. <del>
  1797. <p><em>foo</em></p>
  1798. </del>
  1799. ````````````````````````````````
  1800. Finally, in this case, the `<del>` tags are interpreted
  1801. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1802. the tag is not on a line by itself, we get inline HTML
  1803. rather than an [HTML block].)
  1804. ```````````````````````````````` example
  1805. <del>*foo*</del>
  1806. .
  1807. <p><del><em>foo</em></del></p>
  1808. ````````````````````````````````
  1809. HTML tags designed to contain literal content
  1810. (`script`, `style`, `pre`), comments, processing instructions,
  1811. and declarations are treated somewhat differently.
  1812. Instead of ending at the first blank line, these blocks
  1813. end at the first line containing a corresponding end tag.
  1814. As a result, these blocks can contain blank lines:
  1815. A pre tag (type 1):
  1816. ```````````````````````````````` example
  1817. <pre language="haskell"><code>
  1818. import Text.HTML.TagSoup
  1819. main :: IO ()
  1820. main = print $ parseTags tags
  1821. </code></pre>
  1822. okay
  1823. .
  1824. <pre language="haskell"><code>
  1825. import Text.HTML.TagSoup
  1826. main :: IO ()
  1827. main = print $ parseTags tags
  1828. </code></pre>
  1829. <p>okay</p>
  1830. ````````````````````````````````
  1831. A script tag (type 1):
  1832. ```````````````````````````````` example
  1833. <script type="text/javascript">
  1834. // JavaScript example
  1835. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1836. </script>
  1837. okay
  1838. .
  1839. <script type="text/javascript">
  1840. // JavaScript example
  1841. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1842. </script>
  1843. <p>okay</p>
  1844. ````````````````````````````````
  1845. A style tag (type 1):
  1846. ```````````````````````````````` example
  1847. <style
  1848. type="text/css">
  1849. h1 {color:red;}
  1850. p {color:blue;}
  1851. </style>
  1852. okay
  1853. .
  1854. <style
  1855. type="text/css">
  1856. h1 {color:red;}
  1857. p {color:blue;}
  1858. </style>
  1859. <p>okay</p>
  1860. ````````````````````````````````
  1861. If there is no matching end tag, the block will end at the
  1862. end of the document (or the enclosing [block quote][block quotes]
  1863. or [list item][list items]):
  1864. ```````````````````````````````` example
  1865. <style
  1866. type="text/css">
  1867. foo
  1868. .
  1869. <style
  1870. type="text/css">
  1871. foo
  1872. ````````````````````````````````
  1873. ```````````````````````````````` example
  1874. > <div>
  1875. > foo
  1876. bar
  1877. .
  1878. <blockquote>
  1879. <div>
  1880. foo
  1881. </blockquote>
  1882. <p>bar</p>
  1883. ````````````````````````````````
  1884. ```````````````````````````````` example
  1885. - <div>
  1886. - foo
  1887. .
  1888. <ul>
  1889. <li>
  1890. <div>
  1891. </li>
  1892. <li>foo</li>
  1893. </ul>
  1894. ````````````````````````````````
  1895. The end tag can occur on the same line as the start tag:
  1896. ```````````````````````````````` example
  1897. <style>p{color:red;}</style>
  1898. *foo*
  1899. .
  1900. <style>p{color:red;}</style>
  1901. <p><em>foo</em></p>
  1902. ````````````````````````````````
  1903. ```````````````````````````````` example
  1904. <!-- foo -->*bar*
  1905. *baz*
  1906. .
  1907. <!-- foo -->*bar*
  1908. <p><em>baz</em></p>
  1909. ````````````````````````````````
  1910. Note that anything on the last line after the
  1911. end tag will be included in the [HTML block]:
  1912. ```````````````````````````````` example
  1913. <script>
  1914. foo
  1915. </script>1. *bar*
  1916. .
  1917. <script>
  1918. foo
  1919. </script>1. *bar*
  1920. ````````````````````````````````
  1921. A comment (type 2):
  1922. ```````````````````````````````` example
  1923. <!-- Foo
  1924. bar
  1925. baz -->
  1926. okay
  1927. .
  1928. <!-- Foo
  1929. bar
  1930. baz -->
  1931. <p>okay</p>
  1932. ````````````````````````````````
  1933. A processing instruction (type 3):
  1934. ```````````````````````````````` example
  1935. <?php
  1936. echo '>';
  1937. ?>
  1938. okay
  1939. .
  1940. <?php
  1941. echo '>';
  1942. ?>
  1943. <p>okay</p>
  1944. ````````````````````````````````
  1945. A declaration (type 4):
  1946. ```````````````````````````````` example
  1947. <!DOCTYPE html>
  1948. .
  1949. <!DOCTYPE html>
  1950. ````````````````````````````````
  1951. CDATA (type 5):
  1952. ```````````````````````````````` example
  1953. <![CDATA[
  1954. function matchwo(a,b)
  1955. {
  1956. if (a < b && a < 0) then {
  1957. return 1;
  1958. } else {
  1959. return 0;
  1960. }
  1961. }
  1962. ]]>
  1963. okay
  1964. .
  1965. <![CDATA[
  1966. function matchwo(a,b)
  1967. {
  1968. if (a < b && a < 0) then {
  1969. return 1;
  1970. } else {
  1971. return 0;
  1972. }
  1973. }
  1974. ]]>
  1975. <p>okay</p>
  1976. ````````````````````````````````
  1977. The opening tag can be indented 1-3 spaces, but not 4:
  1978. ```````````````````````````````` example
  1979. <!-- foo -->
  1980. <!-- foo -->
  1981. .
  1982. <!-- foo -->
  1983. <pre><code>&lt;!-- foo --&gt;
  1984. </code></pre>
  1985. ````````````````````````````````
  1986. ```````````````````````````````` example
  1987. <div>
  1988. <div>
  1989. .
  1990. <div>
  1991. <pre><code>&lt;div&gt;
  1992. </code></pre>
  1993. ````````````````````````````````
  1994. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  1995. preceded by a blank line.
  1996. ```````````````````````````````` example
  1997. Foo
  1998. <div>
  1999. bar
  2000. </div>
  2001. .
  2002. <p>Foo</p>
  2003. <div>
  2004. bar
  2005. </div>
  2006. ````````````````````````````````
  2007. However, a following blank line is needed, except at the end of
  2008. a document, and except for blocks of types 1--5, above:
  2009. ```````````````````````````````` example
  2010. <div>
  2011. bar
  2012. </div>
  2013. *foo*
  2014. .
  2015. <div>
  2016. bar
  2017. </div>
  2018. *foo*
  2019. ````````````````````````````````
  2020. HTML blocks of type 7 cannot interrupt a paragraph:
  2021. ```````````````````````````````` example
  2022. Foo
  2023. <a href="bar">
  2024. baz
  2025. .
  2026. <p>Foo
  2027. <a href="bar">
  2028. baz</p>
  2029. ````````````````````````````````
  2030. This rule differs from John Gruber's original Markdown syntax
  2031. specification, which says:
  2032. > The only restrictions are that block-level HTML elements —
  2033. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2034. > surrounding content by blank lines, and the start and end tags of the
  2035. > block should not be indented with tabs or spaces.
  2036. In some ways Gruber's rule is more restrictive than the one given
  2037. here:
  2038. - It requires that an HTML block be preceded by a blank line.
  2039. - It does not allow the start tag to be indented.
  2040. - It requires a matching end tag, which it also does not allow to
  2041. be indented.
  2042. Most Markdown implementations (including some of Gruber's own) do not
  2043. respect all of these restrictions.
  2044. There is one respect, however, in which Gruber's rule is more liberal
  2045. than the one given here, since it allows blank lines to occur inside
  2046. an HTML block. There are two reasons for disallowing them here.
  2047. First, it removes the need to parse balanced tags, which is
  2048. expensive and can require backtracking from the end of the document
  2049. if no matching end tag is found. Second, it provides a very simple
  2050. and flexible way of including Markdown content inside HTML tags:
  2051. simply separate the Markdown from the HTML using blank lines:
  2052. Compare:
  2053. ```````````````````````````````` example
  2054. <div>
  2055. *Emphasized* text.
  2056. </div>
  2057. .
  2058. <div>
  2059. <p><em>Emphasized</em> text.</p>
  2060. </div>
  2061. ````````````````````````````````
  2062. ```````````````````````````````` example
  2063. <div>
  2064. *Emphasized* text.
  2065. </div>
  2066. .
  2067. <div>
  2068. *Emphasized* text.
  2069. </div>
  2070. ````````````````````````````````
  2071. Some Markdown implementations have adopted a convention of
  2072. interpreting content inside tags as text if the open tag has
  2073. the attribute `markdown=1`. The rule given above seems a simpler and
  2074. more elegant way of achieving the same expressive power, which is also
  2075. much simpler to parse.
  2076. The main potential drawback is that one can no longer paste HTML
  2077. blocks into Markdown documents with 100% reliability. However,
  2078. *in most cases* this will work fine, because the blank lines in
  2079. HTML are usually followed by HTML block tags. For example:
  2080. ```````````````````````````````` example
  2081. <table>
  2082. <tr>
  2083. <td>
  2084. Hi
  2085. </td>
  2086. </tr>
  2087. </table>
  2088. .
  2089. <table>
  2090. <tr>
  2091. <td>
  2092. Hi
  2093. </td>
  2094. </tr>
  2095. </table>
  2096. ````````````````````````````````
  2097. There are problems, however, if the inner tags are indented
  2098. *and* separated by spaces, as then they will be interpreted as
  2099. an indented code block:
  2100. ```````````````````````````````` example
  2101. <table>
  2102. <tr>
  2103. <td>
  2104. Hi
  2105. </td>
  2106. </tr>
  2107. </table>
  2108. .
  2109. <table>
  2110. <tr>
  2111. <pre><code>&lt;td&gt;
  2112. Hi
  2113. &lt;/td&gt;
  2114. </code></pre>
  2115. </tr>
  2116. </table>
  2117. ````````````````````````````````
  2118. Fortunately, blank lines are usually not necessary and can be
  2119. deleted. The exception is inside `<pre>` tags, but as described
  2120. above, raw HTML blocks starting with `<pre>` *can* contain blank
  2121. lines.
  2122. ## Link reference definitions
  2123. A [link reference definition](@)
  2124. consists of a [link label], indented up to three spaces, followed
  2125. by a colon (`:`), optional [whitespace] (including up to one
  2126. [line ending]), a [link destination],
  2127. optional [whitespace] (including up to one
  2128. [line ending]), and an optional [link
  2129. title], which if it is present must be separated
  2130. from the [link destination] by [whitespace].
  2131. No further [non-whitespace characters] may occur on the line.
  2132. A [link reference definition]
  2133. does not correspond to a structural element of a document. Instead, it
  2134. defines a label which can be used in [reference links]
  2135. and reference-style [images] elsewhere in the document. [Link
  2136. reference definitions] can come either before or after the links that use
  2137. them.
  2138. ```````````````````````````````` example
  2139. [foo]: /url "title"
  2140. [foo]
  2141. .
  2142. <p><a href="/url" title="title">foo</a></p>
  2143. ````````````````````````````````
  2144. ```````````````````````````````` example
  2145. [foo]:
  2146. /url
  2147. 'the title'
  2148. [foo]
  2149. .
  2150. <p><a href="/url" title="the title">foo</a></p>
  2151. ````````````````````````````````
  2152. ```````````````````````````````` example
  2153. [Foo*bar\]]:my_(url) 'title (with parens)'
  2154. [Foo*bar\]]
  2155. .
  2156. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2157. ````````````````````````````````
  2158. ```````````````````````````````` example
  2159. [Foo bar]:
  2160. <my%20url>
  2161. 'title'
  2162. [Foo bar]
  2163. .
  2164. <p><a href="my%20url" title="title">Foo bar</a></p>
  2165. ````````````````````````````````
  2166. The title may extend over multiple lines:
  2167. ```````````````````````````````` example
  2168. [foo]: /url '
  2169. title
  2170. line1
  2171. line2
  2172. '
  2173. [foo]
  2174. .
  2175. <p><a href="/url" title="
  2176. title
  2177. line1
  2178. line2
  2179. ">foo</a></p>
  2180. ````````````````````````````````
  2181. However, it may not contain a [blank line]:
  2182. ```````````````````````````````` example
  2183. [foo]: /url 'title
  2184. with blank line'
  2185. [foo]
  2186. .
  2187. <p>[foo]: /url 'title</p>
  2188. <p>with blank line'</p>
  2189. <p>[foo]</p>
  2190. ````````````````````````````````
  2191. The title may be omitted:
  2192. ```````````````````````````````` example
  2193. [foo]:
  2194. /url
  2195. [foo]
  2196. .
  2197. <p><a href="/url">foo</a></p>
  2198. ````````````````````````````````
  2199. The link destination may not be omitted:
  2200. ```````````````````````````````` example
  2201. [foo]:
  2202. [foo]
  2203. .
  2204. <p>[foo]:</p>
  2205. <p>[foo]</p>
  2206. ````````````````````````````````
  2207. Both title and destination can contain backslash escapes
  2208. and literal backslashes:
  2209. ```````````````````````````````` example
  2210. [foo]: /url\bar\*baz "foo\"bar\baz"
  2211. [foo]
  2212. .
  2213. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2214. ````````````````````````````````
  2215. A link can come before its corresponding definition:
  2216. ```````````````````````````````` example
  2217. [foo]
  2218. [foo]: url
  2219. .
  2220. <p><a href="url">foo</a></p>
  2221. ````````````````````````````````
  2222. If there are several matching definitions, the first one takes
  2223. precedence:
  2224. ```````````````````````````````` example
  2225. [foo]
  2226. [foo]: first
  2227. [foo]: second
  2228. .
  2229. <p><a href="first">foo</a></p>
  2230. ````````````````````````````````
  2231. As noted in the section on [Links], matching of labels is
  2232. case-insensitive (see [matches]).
  2233. ```````````````````````````````` example
  2234. [FOO]: /url
  2235. [Foo]
  2236. .
  2237. <p><a href="/url">Foo</a></p>
  2238. ````````````````````````````````
  2239. ```````````````````````````````` example
  2240. [ΑΓΩ]: /φου
  2241. [αγω]
  2242. .
  2243. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2244. ````````````````````````````````
  2245. Here is a link reference definition with no corresponding link.
  2246. It contributes nothing to the document.
  2247. ```````````````````````````````` example
  2248. [foo]: /url
  2249. .
  2250. ````````````````````````````````
  2251. Here is another one:
  2252. ```````````````````````````````` example
  2253. [
  2254. foo
  2255. ]: /url
  2256. bar
  2257. .
  2258. <p>bar</p>
  2259. ````````````````````````````````
  2260. This is not a link reference definition, because there are
  2261. [non-whitespace characters] after the title:
  2262. ```````````````````````````````` example
  2263. [foo]: /url "title" ok
  2264. .
  2265. <p>[foo]: /url &quot;title&quot; ok</p>
  2266. ````````````````````````````````
  2267. This is a link reference definition, but it has no title:
  2268. ```````````````````````````````` example
  2269. [foo]: /url
  2270. "title" ok
  2271. .
  2272. <p>&quot;title&quot; ok</p>
  2273. ````````````````````````````````
  2274. This is not a link reference definition, because it is indented
  2275. four spaces:
  2276. ```````````````````````````````` example
  2277. [foo]: /url "title"
  2278. [foo]
  2279. .
  2280. <pre><code>[foo]: /url &quot;title&quot;
  2281. </code></pre>
  2282. <p>[foo]</p>
  2283. ````````````````````````````````
  2284. This is not a link reference definition, because it occurs inside
  2285. a code block:
  2286. ```````````````````````````````` example
  2287. ```
  2288. [foo]: /url
  2289. ```
  2290. [foo]
  2291. .
  2292. <pre><code>[foo]: /url
  2293. </code></pre>
  2294. <p>[foo]</p>
  2295. ````````````````````````````````
  2296. A [link reference definition] cannot interrupt a paragraph.
  2297. ```````````````````````````````` example
  2298. Foo
  2299. [bar]: /baz
  2300. [bar]
  2301. .
  2302. <p>Foo
  2303. [bar]: /baz</p>
  2304. <p>[bar]</p>
  2305. ````````````````````````````````
  2306. However, it can directly follow other block elements, such as headings
  2307. and thematic breaks, and it need not be followed by a blank line.
  2308. ```````````````````````````````` example
  2309. # [Foo]
  2310. [foo]: /url
  2311. > bar
  2312. .
  2313. <h1><a href="/url">Foo</a></h1>
  2314. <blockquote>
  2315. <p>bar</p>
  2316. </blockquote>
  2317. ````````````````````````````````
  2318. Several [link reference definitions]
  2319. can occur one after another, without intervening blank lines.
  2320. ```````````````````````````````` example
  2321. [foo]: /foo-url "foo"
  2322. [bar]: /bar-url
  2323. "bar"
  2324. [baz]: /baz-url
  2325. [foo],
  2326. [bar],
  2327. [baz]
  2328. .
  2329. <p><a href="/foo-url" title="foo">foo</a>,
  2330. <a href="/bar-url" title="bar">bar</a>,
  2331. <a href="/baz-url">baz</a></p>
  2332. ````````````````````````````````
  2333. [Link reference definitions] can occur
  2334. inside block containers, like lists and block quotations. They
  2335. affect the entire document, not just the container in which they
  2336. are defined:
  2337. ```````````````````````````````` example
  2338. [foo]
  2339. > [foo]: /url
  2340. .
  2341. <p><a href="/url">foo</a></p>
  2342. <blockquote>
  2343. </blockquote>
  2344. ````````````````````````````````
  2345. ## Paragraphs
  2346. A sequence of non-blank lines that cannot be interpreted as other
  2347. kinds of blocks forms a [paragraph](@).
  2348. The contents of the paragraph are the result of parsing the
  2349. paragraph's raw content as inlines. The paragraph's raw content
  2350. is formed by concatenating the lines and removing initial and final
  2351. [whitespace].
  2352. A simple example with two paragraphs:
  2353. ```````````````````````````````` example
  2354. aaa
  2355. bbb
  2356. .
  2357. <p>aaa</p>
  2358. <p>bbb</p>
  2359. ````````````````````````````````
  2360. Paragraphs can contain multiple lines, but no blank lines:
  2361. ```````````````````````````````` example
  2362. aaa
  2363. bbb
  2364. ccc
  2365. ddd
  2366. .
  2367. <p>aaa
  2368. bbb</p>
  2369. <p>ccc
  2370. ddd</p>
  2371. ````````````````````````````````
  2372. Multiple blank lines between paragraph have no effect:
  2373. ```````````````````````````````` example
  2374. aaa
  2375. bbb
  2376. .
  2377. <p>aaa</p>
  2378. <p>bbb</p>
  2379. ````````````````````````````````
  2380. Leading spaces are skipped:
  2381. ```````````````````````````````` example
  2382. aaa
  2383. bbb
  2384. .
  2385. <p>aaa
  2386. bbb</p>
  2387. ````````````````````````````````
  2388. Lines after the first may be indented any amount, since indented
  2389. code blocks cannot interrupt paragraphs.
  2390. ```````````````````````````````` example
  2391. aaa
  2392. bbb
  2393. ccc
  2394. .
  2395. <p>aaa
  2396. bbb
  2397. ccc</p>
  2398. ````````````````````````````````
  2399. However, the first line may be indented at most three spaces,
  2400. or an indented code block will be triggered:
  2401. ```````````````````````````````` example
  2402. aaa
  2403. bbb
  2404. .
  2405. <p>aaa
  2406. bbb</p>
  2407. ````````````````````````````````
  2408. ```````````````````````````````` example
  2409. aaa
  2410. bbb
  2411. .
  2412. <pre><code>aaa
  2413. </code></pre>
  2414. <p>bbb</p>
  2415. ````````````````````````````````
  2416. Final spaces are stripped before inline parsing, so a paragraph
  2417. that ends with two or more spaces will not end with a [hard line
  2418. break]:
  2419. ```````````````````````````````` example
  2420. aaa
  2421. bbb
  2422. .
  2423. <p>aaa<br />
  2424. bbb</p>
  2425. ````````````````````````````````
  2426. ## Blank lines
  2427. [Blank lines] between block-level elements are ignored,
  2428. except for the role they play in determining whether a [list]
  2429. is [tight] or [loose].
  2430. Blank lines at the beginning and end of the document are also ignored.
  2431. ```````````````````````````````` example
  2432. aaa
  2433. # aaa
  2434. .
  2435. <p>aaa</p>
  2436. <h1>aaa</h1>
  2437. ````````````````````````````````
  2438. # Container blocks
  2439. A [container block] is a block that has other
  2440. blocks as its contents. There are two basic kinds of container blocks:
  2441. [block quotes] and [list items].
  2442. [Lists] are meta-containers for [list items].
  2443. We define the syntax for container blocks recursively. The general
  2444. form of the definition is:
  2445. > If X is a sequence of blocks, then the result of
  2446. > transforming X in such-and-such a way is a container of type Y
  2447. > with these blocks as its content.
  2448. So, we explain what counts as a block quote or list item by explaining
  2449. how these can be *generated* from their contents. This should suffice
  2450. to define the syntax, although it does not give a recipe for *parsing*
  2451. these constructions. (A recipe is provided below in the section entitled
  2452. [A parsing strategy](#appendix-a-parsing-strategy).)
  2453. ## Block quotes
  2454. A [block quote marker](@)
  2455. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2456. with a following space, or (b) a single character `>` not followed by a space.
  2457. The following rules define [block quotes]:
  2458. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2459. of blocks *Bs*, then the result of prepending a [block quote
  2460. marker] to the beginning of each line in *Ls*
  2461. is a [block quote](#block-quotes) containing *Bs*.
  2462. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2463. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2464. the initial [block quote marker] from one or
  2465. more lines in which the next [non-whitespace character] after the [block
  2466. quote marker] is [paragraph continuation
  2467. text] is a block quote with *Bs* as its content.
  2468. [Paragraph continuation text](@) is text
  2469. that will be parsed as part of the content of a paragraph, but does
  2470. not occur at the beginning of the paragraph.
  2471. 3. **Consecutiveness.** A document cannot contain two [block
  2472. quotes] in a row unless there is a [blank line] between them.
  2473. Nothing else counts as a [block quote](#block-quotes).
  2474. Here is a simple example:
  2475. ```````````````````````````````` example
  2476. > # Foo
  2477. > bar
  2478. > baz
  2479. .
  2480. <blockquote>
  2481. <h1>Foo</h1>
  2482. <p>bar
  2483. baz</p>
  2484. </blockquote>
  2485. ````````````````````````````````
  2486. The spaces after the `>` characters can be omitted:
  2487. ```````````````````````````````` example
  2488. ># Foo
  2489. >bar
  2490. > baz
  2491. .
  2492. <blockquote>
  2493. <h1>Foo</h1>
  2494. <p>bar
  2495. baz</p>
  2496. </blockquote>
  2497. ````````````````````````````````
  2498. The `>` characters can be indented 1-3 spaces:
  2499. ```````````````````````````````` example
  2500. > # Foo
  2501. > bar
  2502. > baz
  2503. .
  2504. <blockquote>
  2505. <h1>Foo</h1>
  2506. <p>bar
  2507. baz</p>
  2508. </blockquote>
  2509. ````````````````````````````````
  2510. Four spaces gives us a code block:
  2511. ```````````````````````````````` example
  2512. > # Foo
  2513. > bar
  2514. > baz
  2515. .
  2516. <pre><code>&gt; # Foo
  2517. &gt; bar
  2518. &gt; baz
  2519. </code></pre>
  2520. ````````````````````````````````
  2521. The Laziness clause allows us to omit the `>` before
  2522. [paragraph continuation text]:
  2523. ```````````````````````````````` example
  2524. > # Foo
  2525. > bar
  2526. baz
  2527. .
  2528. <blockquote>
  2529. <h1>Foo</h1>
  2530. <p>bar
  2531. baz</p>
  2532. </blockquote>
  2533. ````````````````````````````````
  2534. A block quote can contain some lazy and some non-lazy
  2535. continuation lines:
  2536. ```````````````````````````````` example
  2537. > bar
  2538. baz
  2539. > foo
  2540. .
  2541. <blockquote>
  2542. <p>bar
  2543. baz
  2544. foo</p>
  2545. </blockquote>
  2546. ````````````````````````````````
  2547. Laziness only applies to lines that would have been continuations of
  2548. paragraphs had they been prepended with [block quote markers].
  2549. For example, the `> ` cannot be omitted in the second line of
  2550. ``` markdown
  2551. > foo
  2552. > ---
  2553. ```
  2554. without changing the meaning:
  2555. ```````````````````````````````` example
  2556. > foo
  2557. ---
  2558. .
  2559. <blockquote>
  2560. <p>foo</p>
  2561. </blockquote>
  2562. <hr />
  2563. ````````````````````````````````
  2564. Similarly, if we omit the `> ` in the second line of
  2565. ``` markdown
  2566. > - foo
  2567. > - bar
  2568. ```
  2569. then the block quote ends after the first line:
  2570. ```````````````````````````````` example
  2571. > - foo
  2572. - bar
  2573. .
  2574. <blockquote>
  2575. <ul>
  2576. <li>foo</li>
  2577. </ul>
  2578. </blockquote>
  2579. <ul>
  2580. <li>bar</li>
  2581. </ul>
  2582. ````````````````````````````````
  2583. For the same reason, we can't omit the `> ` in front of
  2584. subsequent lines of an indented or fenced code block:
  2585. ```````````````````````````````` example
  2586. > foo
  2587. bar
  2588. .
  2589. <blockquote>
  2590. <pre><code>foo
  2591. </code></pre>
  2592. </blockquote>
  2593. <pre><code>bar
  2594. </code></pre>
  2595. ````````````````````````````````
  2596. ```````````````````````````````` example
  2597. > ```
  2598. foo
  2599. ```
  2600. .
  2601. <blockquote>
  2602. <pre><code></code></pre>
  2603. </blockquote>
  2604. <p>foo</p>
  2605. <pre><code></code></pre>
  2606. ````````````````````````````````
  2607. Note that in the following case, we have a [lazy
  2608. continuation line]:
  2609. ```````````````````````````````` example
  2610. > foo
  2611. - bar
  2612. .
  2613. <blockquote>
  2614. <p>foo
  2615. - bar</p>
  2616. </blockquote>
  2617. ````````````````````````````````
  2618. To see why, note that in
  2619. ```markdown
  2620. > foo
  2621. > - bar
  2622. ```
  2623. the `- bar` is indented too far to start a list, and can't
  2624. be an indented code block because indented code blocks cannot
  2625. interrupt paragraphs, so it is [paragraph continuation text].
  2626. A block quote can be empty:
  2627. ```````````````````````````````` example
  2628. >
  2629. .
  2630. <blockquote>
  2631. </blockquote>
  2632. ````````````````````````````````
  2633. ```````````````````````````````` example
  2634. >
  2635. >
  2636. >
  2637. .
  2638. <blockquote>
  2639. </blockquote>
  2640. ````````````````````````````````
  2641. A block quote can have initial or final blank lines:
  2642. ```````````````````````````````` example
  2643. >
  2644. > foo
  2645. >
  2646. .
  2647. <blockquote>
  2648. <p>foo</p>
  2649. </blockquote>
  2650. ````````````````````````````````
  2651. A blank line always separates block quotes:
  2652. ```````````````````````````````` example
  2653. > foo
  2654. > bar
  2655. .
  2656. <blockquote>
  2657. <p>foo</p>
  2658. </blockquote>
  2659. <blockquote>
  2660. <p>bar</p>
  2661. </blockquote>
  2662. ````````````````````````````````
  2663. (Most current Markdown implementations, including John Gruber's
  2664. original `Markdown.pl`, will parse this example as a single block quote
  2665. with two paragraphs. But it seems better to allow the author to decide
  2666. whether two block quotes or one are wanted.)
  2667. Consecutiveness means that if we put these block quotes together,
  2668. we get a single block quote:
  2669. ```````````````````````````````` example
  2670. > foo
  2671. > bar
  2672. .
  2673. <blockquote>
  2674. <p>foo
  2675. bar</p>
  2676. </blockquote>
  2677. ````````````````````````````````
  2678. To get a block quote with two paragraphs, use:
  2679. ```````````````````````````````` example
  2680. > foo
  2681. >
  2682. > bar
  2683. .
  2684. <blockquote>
  2685. <p>foo</p>
  2686. <p>bar</p>
  2687. </blockquote>
  2688. ````````````````````````````````
  2689. Block quotes can interrupt paragraphs:
  2690. ```````````````````````````````` example
  2691. foo
  2692. > bar
  2693. .
  2694. <p>foo</p>
  2695. <blockquote>
  2696. <p>bar</p>
  2697. </blockquote>
  2698. ````````````````````````````````
  2699. In general, blank lines are not needed before or after block
  2700. quotes:
  2701. ```````````````````````````````` example
  2702. > aaa
  2703. ***
  2704. > bbb
  2705. .
  2706. <blockquote>
  2707. <p>aaa</p>
  2708. </blockquote>
  2709. <hr />
  2710. <blockquote>
  2711. <p>bbb</p>
  2712. </blockquote>
  2713. ````````````````````````````````
  2714. However, because of laziness, a blank line is needed between
  2715. a block quote and a following paragraph:
  2716. ```````````````````````````````` example
  2717. > bar
  2718. baz
  2719. .
  2720. <blockquote>
  2721. <p>bar
  2722. baz</p>
  2723. </blockquote>
  2724. ````````````````````````````````
  2725. ```````````````````````````````` example
  2726. > bar
  2727. baz
  2728. .
  2729. <blockquote>
  2730. <p>bar</p>
  2731. </blockquote>
  2732. <p>baz</p>
  2733. ````````````````````````````````
  2734. ```````````````````````````````` example
  2735. > bar
  2736. >
  2737. baz
  2738. .
  2739. <blockquote>
  2740. <p>bar</p>
  2741. </blockquote>
  2742. <p>baz</p>
  2743. ````````````````````````````````
  2744. It is a consequence of the Laziness rule that any number
  2745. of initial `>`s may be omitted on a continuation line of a
  2746. nested block quote:
  2747. ```````````````````````````````` example
  2748. > > > foo
  2749. bar
  2750. .
  2751. <blockquote>
  2752. <blockquote>
  2753. <blockquote>
  2754. <p>foo
  2755. bar</p>
  2756. </blockquote>
  2757. </blockquote>
  2758. </blockquote>
  2759. ````````````````````````````````
  2760. ```````````````````````````````` example
  2761. >>> foo
  2762. > bar
  2763. >>baz
  2764. .
  2765. <blockquote>
  2766. <blockquote>
  2767. <blockquote>
  2768. <p>foo
  2769. bar
  2770. baz</p>
  2771. </blockquote>
  2772. </blockquote>
  2773. </blockquote>
  2774. ````````````````````````````````
  2775. When including an indented code block in a block quote,
  2776. remember that the [block quote marker] includes
  2777. both the `>` and a following space. So *five spaces* are needed after
  2778. the `>`:
  2779. ```````````````````````````````` example
  2780. > code
  2781. > not code
  2782. .
  2783. <blockquote>
  2784. <pre><code>code
  2785. </code></pre>
  2786. </blockquote>
  2787. <blockquote>
  2788. <p>not code</p>
  2789. </blockquote>
  2790. ````````````````````````````````
  2791. ## List items
  2792. A [list marker](@) is a
  2793. [bullet list marker] or an [ordered list marker].
  2794. A [bullet list marker](@)
  2795. is a `-`, `+`, or `*` character.
  2796. An [ordered list marker](@)
  2797. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2798. `.` character or a `)` character. (The reason for the length
  2799. limit is that with 10 digits we start seeing integer overflows
  2800. in some browsers.)
  2801. The following rules define [list items]:
  2802. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2803. blocks *Bs* starting with a [non-whitespace character] and not separated
  2804. from each other by more than one blank line, and *M* is a list
  2805. marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2806. of prepending *M* and the following spaces to the first line of
  2807. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2808. list item with *Bs* as its contents. The type of the list item
  2809. (bullet or ordered) is determined by the type of its list marker.
  2810. If the list item is ordered, then it is also assigned a start
  2811. number, based on the ordered list marker.
  2812. Exceptions:
  2813. 1. When the first list item in a [list] interrupts
  2814. a paragraph---that is, when it starts on a line that would
  2815. otherwise count as [paragraph continuation text]---then (a)
  2816. the lines *Ls* must not begin with a blank line, and (b) if
  2817. the list item is ordered, the start number must be 1.
  2818. 2. If any line is a [thematic break][thematic breaks] then
  2819. that line is not a list item.
  2820. For example, let *Ls* be the lines
  2821. ```````````````````````````````` example
  2822. A paragraph
  2823. with two lines.
  2824. indented code
  2825. > A block quote.
  2826. .
  2827. <p>A paragraph
  2828. with two lines.</p>
  2829. <pre><code>indented code
  2830. </code></pre>
  2831. <blockquote>
  2832. <p>A block quote.</p>
  2833. </blockquote>
  2834. ````````````````````````````````
  2835. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2836. that the following is an ordered list item with start number 1,
  2837. and the same contents as *Ls*:
  2838. ```````````````````````````````` example
  2839. 1. A paragraph
  2840. with two lines.
  2841. indented code
  2842. > A block quote.
  2843. .
  2844. <ol>
  2845. <li>
  2846. <p>A paragraph
  2847. with two lines.</p>
  2848. <pre><code>indented code
  2849. </code></pre>
  2850. <blockquote>
  2851. <p>A block quote.</p>
  2852. </blockquote>
  2853. </li>
  2854. </ol>
  2855. ````````````````````````````````
  2856. The most important thing to notice is that the position of
  2857. the text after the list marker determines how much indentation
  2858. is needed in subsequent blocks in the list item. If the list
  2859. marker takes up two spaces, and there are three spaces between
  2860. the list marker and the next [non-whitespace character], then blocks
  2861. must be indented five spaces in order to fall under the list
  2862. item.
  2863. Here are some examples showing how far content must be indented to be
  2864. put under the list item:
  2865. ```````````````````````````````` example
  2866. - one
  2867. two
  2868. .
  2869. <ul>
  2870. <li>one</li>
  2871. </ul>
  2872. <p>two</p>
  2873. ````````````````````````````````
  2874. ```````````````````````````````` example
  2875. - one
  2876. two
  2877. .
  2878. <ul>
  2879. <li>
  2880. <p>one</p>
  2881. <p>two</p>
  2882. </li>
  2883. </ul>
  2884. ````````````````````````````````
  2885. ```````````````````````````````` example
  2886. - one
  2887. two
  2888. .
  2889. <ul>
  2890. <li>one</li>
  2891. </ul>
  2892. <pre><code> two
  2893. </code></pre>
  2894. ````````````````````````````````
  2895. ```````````````````````````````` example
  2896. - one
  2897. two
  2898. .
  2899. <ul>
  2900. <li>
  2901. <p>one</p>
  2902. <p>two</p>
  2903. </li>
  2904. </ul>
  2905. ````````````````````````````````
  2906. It is tempting to think of this in terms of columns: the continuation
  2907. blocks must be indented at least to the column of the first
  2908. [non-whitespace character] after the list marker. However, that is not quite right.
  2909. The spaces after the list marker determine how much relative indentation
  2910. is needed. Which column this indentation reaches will depend on
  2911. how the list item is embedded in other constructions, as shown by
  2912. this example:
  2913. ```````````````````````````````` example
  2914. > > 1. one
  2915. >>
  2916. >> two
  2917. .
  2918. <blockquote>
  2919. <blockquote>
  2920. <ol>
  2921. <li>
  2922. <p>one</p>
  2923. <p>two</p>
  2924. </li>
  2925. </ol>
  2926. </blockquote>
  2927. </blockquote>
  2928. ````````````````````````````````
  2929. Here `two` occurs in the same column as the list marker `1.`,
  2930. but is actually contained in the list item, because there is
  2931. sufficient indentation after the last containing blockquote marker.
  2932. The converse is also possible. In the following example, the word `two`
  2933. occurs far to the right of the initial text of the list item, `one`, but
  2934. it is not considered part of the list item, because it is not indented
  2935. far enough past the blockquote marker:
  2936. ```````````````````````````````` example
  2937. >>- one
  2938. >>
  2939. > > two
  2940. .
  2941. <blockquote>
  2942. <blockquote>
  2943. <ul>
  2944. <li>one</li>
  2945. </ul>
  2946. <p>two</p>
  2947. </blockquote>
  2948. </blockquote>
  2949. ````````````````````````````````
  2950. Note that at least one space is needed between the list marker and
  2951. any following content, so these are not list items:
  2952. ```````````````````````````````` example
  2953. -one
  2954. 2.two
  2955. .
  2956. <p>-one</p>
  2957. <p>2.two</p>
  2958. ````````````````````````````````
  2959. A list item may contain blocks that are separated by more than
  2960. one blank line.
  2961. ```````````````````````````````` example
  2962. - foo
  2963. bar
  2964. .
  2965. <ul>
  2966. <li>
  2967. <p>foo</p>
  2968. <p>bar</p>
  2969. </li>
  2970. </ul>
  2971. ````````````````````````````````
  2972. A list item may contain any kind of block:
  2973. ```````````````````````````````` example
  2974. 1. foo
  2975. ```
  2976. bar
  2977. ```
  2978. baz
  2979. > bam
  2980. .
  2981. <ol>
  2982. <li>
  2983. <p>foo</p>
  2984. <pre><code>bar
  2985. </code></pre>
  2986. <p>baz</p>
  2987. <blockquote>
  2988. <p>bam</p>
  2989. </blockquote>
  2990. </li>
  2991. </ol>
  2992. ````````````````````````````````
  2993. A list item that contains an indented code block will preserve
  2994. empty lines within the code block verbatim.
  2995. ```````````````````````````````` example
  2996. - Foo
  2997. bar
  2998. baz
  2999. .
  3000. <ul>
  3001. <li>
  3002. <p>Foo</p>
  3003. <pre><code>bar
  3004. baz
  3005. </code></pre>
  3006. </li>
  3007. </ul>
  3008. ````````````````````````````````
  3009. Note that ordered list start numbers must be nine digits or less:
  3010. ```````````````````````````````` example
  3011. 123456789. ok
  3012. .
  3013. <ol start="123456789">
  3014. <li>ok</li>
  3015. </ol>
  3016. ````````````````````````````````
  3017. ```````````````````````````````` example
  3018. 1234567890. not ok
  3019. .
  3020. <p>1234567890. not ok</p>
  3021. ````````````````````````````````
  3022. A start number may begin with 0s:
  3023. ```````````````````````````````` example
  3024. 0. ok
  3025. .
  3026. <ol start="0">
  3027. <li>ok</li>
  3028. </ol>
  3029. ````````````````````````````````
  3030. ```````````````````````````````` example
  3031. 003. ok
  3032. .
  3033. <ol start="3">
  3034. <li>ok</li>
  3035. </ol>
  3036. ````````````````````````````````
  3037. A start number may not be negative:
  3038. ```````````````````````````````` example
  3039. -1. not ok
  3040. .
  3041. <p>-1. not ok</p>
  3042. ````````````````````````````````
  3043. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3044. constitute a sequence of blocks *Bs* starting with an indented code
  3045. block and not separated from each other by more than one blank line,
  3046. and *M* is a list marker of width *W* followed by
  3047. one space, then the result of prepending *M* and the following
  3048. space to the first line of *Ls*, and indenting subsequent lines of
  3049. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3050. If a line is empty, then it need not be indented. The type of the
  3051. list item (bullet or ordered) is determined by the type of its list
  3052. marker. If the list item is ordered, then it is also assigned a
  3053. start number, based on the ordered list marker.
  3054. An indented code block will have to be indented four spaces beyond
  3055. the edge of the region where text will be included in the list item.
  3056. In the following case that is 6 spaces:
  3057. ```````````````````````````````` example
  3058. - foo
  3059. bar
  3060. .
  3061. <ul>
  3062. <li>
  3063. <p>foo</p>
  3064. <pre><code>bar
  3065. </code></pre>
  3066. </li>
  3067. </ul>
  3068. ````````````````````````````````
  3069. And in this case it is 11 spaces:
  3070. ```````````````````````````````` example
  3071. 10. foo
  3072. bar
  3073. .
  3074. <ol start="10">
  3075. <li>
  3076. <p>foo</p>
  3077. <pre><code>bar
  3078. </code></pre>
  3079. </li>
  3080. </ol>
  3081. ````````````````````````````````
  3082. If the *first* block in the list item is an indented code block,
  3083. then by rule #2, the contents must be indented *one* space after the
  3084. list marker:
  3085. ```````````````````````````````` example
  3086. indented code
  3087. paragraph
  3088. more code
  3089. .
  3090. <pre><code>indented code
  3091. </code></pre>
  3092. <p>paragraph</p>
  3093. <pre><code>more code
  3094. </code></pre>
  3095. ````````````````````````````````
  3096. ```````````````````````````````` example
  3097. 1. indented code
  3098. paragraph
  3099. more code
  3100. .
  3101. <ol>
  3102. <li>
  3103. <pre><code>indented code
  3104. </code></pre>
  3105. <p>paragraph</p>
  3106. <pre><code>more code
  3107. </code></pre>
  3108. </li>
  3109. </ol>
  3110. ````````````````````````````````
  3111. Note that an additional space indent is interpreted as space
  3112. inside the code block:
  3113. ```````````````````````````````` example
  3114. 1. indented code
  3115. paragraph
  3116. more code
  3117. .
  3118. <ol>
  3119. <li>
  3120. <pre><code> indented code
  3121. </code></pre>
  3122. <p>paragraph</p>
  3123. <pre><code>more code
  3124. </code></pre>
  3125. </li>
  3126. </ol>
  3127. ````````````````````````````````
  3128. Note that rules #1 and #2 only apply to two cases: (a) cases
  3129. in which the lines to be included in a list item begin with a
  3130. [non-whitespace character], and (b) cases in which
  3131. they begin with an indented code
  3132. block. In a case like the following, where the first block begins with
  3133. a three-space indent, the rules do not allow us to form a list item by
  3134. indenting the whole thing and prepending a list marker:
  3135. ```````````````````````````````` example
  3136. foo
  3137. bar
  3138. .
  3139. <p>foo</p>
  3140. <p>bar</p>
  3141. ````````````````````````````````
  3142. ```````````````````````````````` example
  3143. - foo
  3144. bar
  3145. .
  3146. <ul>
  3147. <li>foo</li>
  3148. </ul>
  3149. <p>bar</p>
  3150. ````````````````````````````````
  3151. This is not a significant restriction, because when a block begins
  3152. with 1-3 spaces indent, the indentation can always be removed without
  3153. a change in interpretation, allowing rule #1 to be applied. So, in
  3154. the above case:
  3155. ```````````````````````````````` example
  3156. - foo
  3157. bar
  3158. .
  3159. <ul>
  3160. <li>
  3161. <p>foo</p>
  3162. <p>bar</p>
  3163. </li>
  3164. </ul>
  3165. ````````````````````````````````
  3166. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3167. starting with a single [blank line] constitute a (possibly empty)
  3168. sequence of blocks *Bs*, not separated from each other by more than
  3169. one blank line, and *M* is a list marker of width *W*,
  3170. then the result of prepending *M* to the first line of *Ls*, and
  3171. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3172. item with *Bs* as its contents.
  3173. If a line is empty, then it need not be indented. The type of the
  3174. list item (bullet or ordered) is determined by the type of its list
  3175. marker. If the list item is ordered, then it is also assigned a
  3176. start number, based on the ordered list marker.
  3177. Here are some list items that start with a blank line but are not empty:
  3178. ```````````````````````````````` example
  3179. -
  3180. foo
  3181. -
  3182. ```
  3183. bar
  3184. ```
  3185. -
  3186. baz
  3187. .
  3188. <ul>
  3189. <li>foo</li>
  3190. <li>
  3191. <pre><code>bar
  3192. </code></pre>
  3193. </li>
  3194. <li>
  3195. <pre><code>baz
  3196. </code></pre>
  3197. </li>
  3198. </ul>
  3199. ````````````````````````````````
  3200. When the list item starts with a blank line, the number of spaces
  3201. following the list marker doesn't change the required indentation:
  3202. ```````````````````````````````` example
  3203. -
  3204. foo
  3205. .
  3206. <ul>
  3207. <li>foo</li>
  3208. </ul>
  3209. ````````````````````````````````
  3210. A list item can begin with at most one blank line.
  3211. In the following example, `foo` is not part of the list
  3212. item:
  3213. ```````````````````````````````` example
  3214. -
  3215. foo
  3216. .
  3217. <ul>
  3218. <li></li>
  3219. </ul>
  3220. <p>foo</p>
  3221. ````````````````````````````````
  3222. Here is an empty bullet list item:
  3223. ```````````````````````````````` example
  3224. - foo
  3225. -
  3226. - bar
  3227. .
  3228. <ul>
  3229. <li>foo</li>
  3230. <li></li>
  3231. <li>bar</li>
  3232. </ul>
  3233. ````````````````````````````````
  3234. It does not matter whether there are spaces following the [list marker]:
  3235. ```````````````````````````````` example
  3236. - foo
  3237. -
  3238. - bar
  3239. .
  3240. <ul>
  3241. <li>foo</li>
  3242. <li></li>
  3243. <li>bar</li>
  3244. </ul>
  3245. ````````````````````````````````
  3246. Here is an empty ordered list item:
  3247. ```````````````````````````````` example
  3248. 1. foo
  3249. 2.
  3250. 3. bar
  3251. .
  3252. <ol>
  3253. <li>foo</li>
  3254. <li></li>
  3255. <li>bar</li>
  3256. </ol>
  3257. ````````````````````````````````
  3258. A list may start or end with an empty list item:
  3259. ```````````````````````````````` example
  3260. *
  3261. .
  3262. <ul>
  3263. <li></li>
  3264. </ul>
  3265. ````````````````````````````````
  3266. However, an empty list item cannot interrupt a paragraph:
  3267. ```````````````````````````````` example
  3268. foo
  3269. *
  3270. foo
  3271. 1.
  3272. .
  3273. <p>foo
  3274. *</p>
  3275. <p>foo
  3276. 1.</p>
  3277. ````````````````````````````````
  3278. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3279. according to rule #1, #2, or #3, then the result of indenting each line
  3280. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3281. list item with the same contents and attributes. If a line is
  3282. empty, then it need not be indented.
  3283. Indented one space:
  3284. ```````````````````````````````` example
  3285. 1. A paragraph
  3286. with two lines.
  3287. indented code
  3288. > A block quote.
  3289. .
  3290. <ol>
  3291. <li>
  3292. <p>A paragraph
  3293. with two lines.</p>
  3294. <pre><code>indented code
  3295. </code></pre>
  3296. <blockquote>
  3297. <p>A block quote.</p>
  3298. </blockquote>
  3299. </li>
  3300. </ol>
  3301. ````````````````````````````````
  3302. Indented two spaces:
  3303. ```````````````````````````````` example
  3304. 1. A paragraph
  3305. with two lines.
  3306. indented code
  3307. > A block quote.
  3308. .
  3309. <ol>
  3310. <li>
  3311. <p>A paragraph
  3312. with two lines.</p>
  3313. <pre><code>indented code
  3314. </code></pre>
  3315. <blockquote>
  3316. <p>A block quote.</p>
  3317. </blockquote>
  3318. </li>
  3319. </ol>
  3320. ````````````````````````````````
  3321. Indented three spaces:
  3322. ```````````````````````````````` example
  3323. 1. A paragraph
  3324. with two lines.
  3325. indented code
  3326. > A block quote.
  3327. .
  3328. <ol>
  3329. <li>
  3330. <p>A paragraph
  3331. with two lines.</p>
  3332. <pre><code>indented code
  3333. </code></pre>
  3334. <blockquote>
  3335. <p>A block quote.</p>
  3336. </blockquote>
  3337. </li>
  3338. </ol>
  3339. ````````````````````````````````
  3340. Four spaces indent gives a code block:
  3341. ```````````````````````````````` example
  3342. 1. A paragraph
  3343. with two lines.
  3344. indented code
  3345. > A block quote.
  3346. .
  3347. <pre><code>1. A paragraph
  3348. with two lines.
  3349. indented code
  3350. &gt; A block quote.
  3351. </code></pre>
  3352. ````````````````````````````````
  3353. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3354. item](#list-items) with contents *Bs*, then the result of deleting
  3355. some or all of the indentation from one or more lines in which the
  3356. next [non-whitespace character] after the indentation is
  3357. [paragraph continuation text] is a
  3358. list item with the same contents and attributes. The unindented
  3359. lines are called
  3360. [lazy continuation line](@)s.
  3361. Here is an example with [lazy continuation lines]:
  3362. ```````````````````````````````` example
  3363. 1. A paragraph
  3364. with two lines.
  3365. indented code
  3366. > A block quote.
  3367. .
  3368. <ol>
  3369. <li>
  3370. <p>A paragraph
  3371. with two lines.</p>
  3372. <pre><code>indented code
  3373. </code></pre>
  3374. <blockquote>
  3375. <p>A block quote.</p>
  3376. </blockquote>
  3377. </li>
  3378. </ol>
  3379. ````````````````````````````````
  3380. Indentation can be partially deleted:
  3381. ```````````````````````````````` example
  3382. 1. A paragraph
  3383. with two lines.
  3384. .
  3385. <ol>
  3386. <li>A paragraph
  3387. with two lines.</li>
  3388. </ol>
  3389. ````````````````````````````````
  3390. These examples show how laziness can work in nested structures:
  3391. ```````````````````````````````` example
  3392. > 1. > Blockquote
  3393. continued here.
  3394. .
  3395. <blockquote>
  3396. <ol>
  3397. <li>
  3398. <blockquote>
  3399. <p>Blockquote
  3400. continued here.</p>
  3401. </blockquote>
  3402. </li>
  3403. </ol>
  3404. </blockquote>
  3405. ````````````````````````````````
  3406. ```````````````````````````````` example
  3407. > 1. > Blockquote
  3408. > continued here.
  3409. .
  3410. <blockquote>
  3411. <ol>
  3412. <li>
  3413. <blockquote>
  3414. <p>Blockquote
  3415. continued here.</p>
  3416. </blockquote>
  3417. </li>
  3418. </ol>
  3419. </blockquote>
  3420. ````````````````````````````````
  3421. 6. **That's all.** Nothing that is not counted as a list item by rules
  3422. #1--5 counts as a [list item](#list-items).
  3423. The rules for sublists follow from the general rules above. A sublist
  3424. must be indented the same number of spaces a paragraph would need to be
  3425. in order to be included in the list item.
  3426. So, in this case we need two spaces indent:
  3427. ```````````````````````````````` example
  3428. - foo
  3429. - bar
  3430. - baz
  3431. - boo
  3432. .
  3433. <ul>
  3434. <li>foo
  3435. <ul>
  3436. <li>bar
  3437. <ul>
  3438. <li>baz
  3439. <ul>
  3440. <li>boo</li>
  3441. </ul>
  3442. </li>
  3443. </ul>
  3444. </li>
  3445. </ul>
  3446. </li>
  3447. </ul>
  3448. ````````````````````````````````
  3449. One is not enough:
  3450. ```````````````````````````````` example
  3451. - foo
  3452. - bar
  3453. - baz
  3454. - boo
  3455. .
  3456. <ul>
  3457. <li>foo</li>
  3458. <li>bar</li>
  3459. <li>baz</li>
  3460. <li>boo</li>
  3461. </ul>
  3462. ````````````````````````````````
  3463. Here we need four, because the list marker is wider:
  3464. ```````````````````````````````` example
  3465. 10) foo
  3466. - bar
  3467. .
  3468. <ol start="10">
  3469. <li>foo
  3470. <ul>
  3471. <li>bar</li>
  3472. </ul>
  3473. </li>
  3474. </ol>
  3475. ````````````````````````````````
  3476. Three is not enough:
  3477. ```````````````````````````````` example
  3478. 10) foo
  3479. - bar
  3480. .
  3481. <ol start="10">
  3482. <li>foo</li>
  3483. </ol>
  3484. <ul>
  3485. <li>bar</li>
  3486. </ul>
  3487. ````````````````````````````````
  3488. A list may be the first block in a list item:
  3489. ```````````````````````````````` example
  3490. - - foo
  3491. .
  3492. <ul>
  3493. <li>
  3494. <ul>
  3495. <li>foo</li>
  3496. </ul>
  3497. </li>
  3498. </ul>
  3499. ````````````````````````````````
  3500. ```````````````````````````````` example
  3501. 1. - 2. foo
  3502. .
  3503. <ol>
  3504. <li>
  3505. <ul>
  3506. <li>
  3507. <ol start="2">
  3508. <li>foo</li>
  3509. </ol>
  3510. </li>
  3511. </ul>
  3512. </li>
  3513. </ol>
  3514. ````````````````````````````````
  3515. A list item can contain a heading:
  3516. ```````````````````````````````` example
  3517. - # Foo
  3518. - Bar
  3519. ---
  3520. baz
  3521. .
  3522. <ul>
  3523. <li>
  3524. <h1>Foo</h1>
  3525. </li>
  3526. <li>
  3527. <h2>Bar</h2>
  3528. baz</li>
  3529. </ul>
  3530. ````````````````````````````````
  3531. ### Motivation
  3532. John Gruber's Markdown spec says the following about list items:
  3533. 1. "List markers typically start at the left margin, but may be indented
  3534. by up to three spaces. List markers must be followed by one or more
  3535. spaces or a tab."
  3536. 2. "To make lists look nice, you can wrap items with hanging indents....
  3537. But if you don't want to, you don't have to."
  3538. 3. "List items may consist of multiple paragraphs. Each subsequent
  3539. paragraph in a list item must be indented by either 4 spaces or one
  3540. tab."
  3541. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3542. but here again, Markdown will allow you to be lazy."
  3543. 5. "To put a blockquote within a list item, the blockquote's `>`
  3544. delimiters need to be indented."
  3545. 6. "To put a code block within a list item, the code block needs to be
  3546. indented twice — 8 spaces or two tabs."
  3547. These rules specify that a paragraph under a list item must be indented
  3548. four spaces (presumably, from the left margin, rather than the start of
  3549. the list marker, but this is not said), and that code under a list item
  3550. must be indented eight spaces instead of the usual four. They also say
  3551. that a block quote must be indented, but not by how much; however, the
  3552. example given has four spaces indentation. Although nothing is said
  3553. about other kinds of block-level content, it is certainly reasonable to
  3554. infer that *all* block elements under a list item, including other
  3555. lists, must be indented four spaces. This principle has been called the
  3556. *four-space rule*.
  3557. The four-space rule is clear and principled, and if the reference
  3558. implementation `Markdown.pl` had followed it, it probably would have
  3559. become the standard. However, `Markdown.pl` allowed paragraphs and
  3560. sublists to start with only two spaces indentation, at least on the
  3561. outer level. Worse, its behavior was inconsistent: a sublist of an
  3562. outer-level list needed two spaces indentation, but a sublist of this
  3563. sublist needed three spaces. It is not surprising, then, that different
  3564. implementations of Markdown have developed very different rules for
  3565. determining what comes under a list item. (Pandoc and python-Markdown,
  3566. for example, stuck with Gruber's syntax description and the four-space
  3567. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3568. followed `Markdown.pl`'s behavior more closely.)
  3569. Unfortunately, given the divergences between implementations, there
  3570. is no way to give a spec for list items that will be guaranteed not
  3571. to break any existing documents. However, the spec given here should
  3572. correctly handle lists formatted with either the four-space rule or
  3573. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3574. in a way that is natural for a human to read.
  3575. The strategy here is to let the width and indentation of the list marker
  3576. determine the indentation necessary for blocks to fall under the list
  3577. item, rather than having a fixed and arbitrary number. The writer can
  3578. think of the body of the list item as a unit which gets indented to the
  3579. right enough to fit the list marker (and any indentation on the list
  3580. marker). (The laziness rule, #5, then allows continuation lines to be
  3581. unindented if needed.)
  3582. This rule is superior, we claim, to any rule requiring a fixed level of
  3583. indentation from the margin. The four-space rule is clear but
  3584. unnatural. It is quite unintuitive that
  3585. ``` markdown
  3586. - foo
  3587. bar
  3588. - baz
  3589. ```
  3590. should be parsed as two lists with an intervening paragraph,
  3591. ``` html
  3592. <ul>
  3593. <li>foo</li>
  3594. </ul>
  3595. <p>bar</p>
  3596. <ul>
  3597. <li>baz</li>
  3598. </ul>
  3599. ```
  3600. as the four-space rule demands, rather than a single list,
  3601. ``` html
  3602. <ul>
  3603. <li>
  3604. <p>foo</p>
  3605. <p>bar</p>
  3606. <ul>
  3607. <li>baz</li>
  3608. </ul>
  3609. </li>
  3610. </ul>
  3611. ```
  3612. The choice of four spaces is arbitrary. It can be learned, but it is
  3613. not likely to be guessed, and it trips up beginners regularly.
  3614. Would it help to adopt a two-space rule? The problem is that such
  3615. a rule, together with the rule allowing 1--3 spaces indentation of the
  3616. initial list marker, allows text that is indented *less than* the
  3617. original list marker to be included in the list item. For example,
  3618. `Markdown.pl` parses
  3619. ``` markdown
  3620. - one
  3621. two
  3622. ```
  3623. as a single list item, with `two` a continuation paragraph:
  3624. ``` html
  3625. <ul>
  3626. <li>
  3627. <p>one</p>
  3628. <p>two</p>
  3629. </li>
  3630. </ul>
  3631. ```
  3632. and similarly
  3633. ``` markdown
  3634. > - one
  3635. >
  3636. > two
  3637. ```
  3638. as
  3639. ``` html
  3640. <blockquote>
  3641. <ul>
  3642. <li>
  3643. <p>one</p>
  3644. <p>two</p>
  3645. </li>
  3646. </ul>
  3647. </blockquote>
  3648. ```
  3649. This is extremely unintuitive.
  3650. Rather than requiring a fixed indent from the margin, we could require
  3651. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3652. may itself be indented). This proposal would remove the last anomaly
  3653. discussed. Unlike the spec presented above, it would count the following
  3654. as a list item with a subparagraph, even though the paragraph `bar`
  3655. is not indented as far as the first paragraph `foo`:
  3656. ``` markdown
  3657. 10. foo
  3658. bar
  3659. ```
  3660. Arguably this text does read like a list item with `bar` as a subparagraph,
  3661. which may count in favor of the proposal. However, on this proposal indented
  3662. code would have to be indented six spaces after the list marker. And this
  3663. would break a lot of existing Markdown, which has the pattern:
  3664. ``` markdown
  3665. 1. foo
  3666. indented code
  3667. ```
  3668. where the code is indented eight spaces. The spec above, by contrast, will
  3669. parse this text as expected, since the code block's indentation is measured
  3670. from the beginning of `foo`.
  3671. The one case that needs special treatment is a list item that *starts*
  3672. with indented code. How much indentation is required in that case, since
  3673. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3674. that in such cases, we require one space indentation from the list marker
  3675. (and then the normal four spaces for the indented code). This will match the
  3676. four-space rule in cases where the list marker plus its initial indentation
  3677. takes four spaces (a common case), but diverge in other cases.
  3678. ## Lists
  3679. A [list](@) is a sequence of one or more
  3680. list items [of the same type]. The list items
  3681. may be separated by any number of blank lines.
  3682. Two list items are [of the same type](@)
  3683. if they begin with a [list marker] of the same type.
  3684. Two list markers are of the
  3685. same type if (a) they are bullet list markers using the same character
  3686. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3687. delimiter (either `.` or `)`).
  3688. A list is an [ordered list](@)
  3689. if its constituent list items begin with
  3690. [ordered list markers], and a
  3691. [bullet list](@) if its constituent list
  3692. items begin with [bullet list markers].
  3693. The [start number](@)
  3694. of an [ordered list] is determined by the list number of
  3695. its initial list item. The numbers of subsequent list items are
  3696. disregarded.
  3697. A list is [loose](@) if any of its constituent
  3698. list items are separated by blank lines, or if any of its constituent
  3699. list items directly contain two block-level elements with a blank line
  3700. between them. Otherwise a list is [tight](@).
  3701. (The difference in HTML output is that paragraphs in a loose list are
  3702. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3703. Changing the bullet or ordered list delimiter starts a new list:
  3704. ```````````````````````````````` example
  3705. - foo
  3706. - bar
  3707. + baz
  3708. .
  3709. <ul>
  3710. <li>foo</li>
  3711. <li>bar</li>
  3712. </ul>
  3713. <ul>
  3714. <li>baz</li>
  3715. </ul>
  3716. ````````````````````````````````
  3717. ```````````````````````````````` example
  3718. 1. foo
  3719. 2. bar
  3720. 3) baz
  3721. .
  3722. <ol>
  3723. <li>foo</li>
  3724. <li>bar</li>
  3725. </ol>
  3726. <ol start="3">
  3727. <li>baz</li>
  3728. </ol>
  3729. ````````````````````````````````
  3730. In CommonMark, a list can interrupt a paragraph. That is,
  3731. no blank line is needed to separate a paragraph from a following
  3732. list:
  3733. ```````````````````````````````` example
  3734. Foo
  3735. - bar
  3736. - baz
  3737. .
  3738. <p>Foo</p>
  3739. <ul>
  3740. <li>bar</li>
  3741. <li>baz</li>
  3742. </ul>
  3743. ````````````````````````````````
  3744. `Markdown.pl` does not allow this, through fear of triggering a list
  3745. via a numeral in a hard-wrapped line:
  3746. ``` markdown
  3747. The number of windows in my house is
  3748. 14. The number of doors is 6.
  3749. ```
  3750. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3751. interrupt a paragraph, even though the same considerations might
  3752. apply.
  3753. In CommonMark, we do allow lists to interrupt paragraphs, for
  3754. two reasons. First, it is natural and not uncommon for people
  3755. to start lists without blank lines:
  3756. ``` markdown
  3757. I need to buy
  3758. - new shoes
  3759. - a coat
  3760. - a plane ticket
  3761. ```
  3762. Second, we are attracted to a
  3763. > [principle of uniformity](@):
  3764. > if a chunk of text has a certain
  3765. > meaning, it will continue to have the same meaning when put into a
  3766. > container block (such as a list item or blockquote).
  3767. (Indeed, the spec for [list items] and [block quotes] presupposes
  3768. this principle.) This principle implies that if
  3769. ``` markdown
  3770. * I need to buy
  3771. - new shoes
  3772. - a coat
  3773. - a plane ticket
  3774. ```
  3775. is a list item containing a paragraph followed by a nested sublist,
  3776. as all Markdown implementations agree it is (though the paragraph
  3777. may be rendered without `<p>` tags, since the list is "tight"),
  3778. then
  3779. ``` markdown
  3780. I need to buy
  3781. - new shoes
  3782. - a coat
  3783. - a plane ticket
  3784. ```
  3785. by itself should be a paragraph followed by a nested sublist.
  3786. Since it is well established Markdown practice to allow lists to
  3787. interrupt paragraphs inside list items, the [principle of
  3788. uniformity] requires us to allow this outside list items as
  3789. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3790. takes a different approach, requiring blank lines before lists
  3791. even inside other list items.)
  3792. In order to solve of unwanted lists in paragraphs with
  3793. hard-wrapped numerals, we allow only lists starting with `1` to
  3794. interrupt paragraphs. Thus,
  3795. ```````````````````````````````` example
  3796. The number of windows in my house is
  3797. 14. The number of doors is 6.
  3798. .
  3799. <p>The number of windows in my house is
  3800. 14. The number of doors is 6.</p>
  3801. ````````````````````````````````
  3802. We may still get an unintended result in cases like
  3803. ```````````````````````````````` example
  3804. The number of windows in my house is
  3805. 1. The number of doors is 6.
  3806. .
  3807. <p>The number of windows in my house is</p>
  3808. <ol>
  3809. <li>The number of doors is 6.</li>
  3810. </ol>
  3811. ````````````````````````````````
  3812. but this rule should prevent most spurious list captures.
  3813. There can be any number of blank lines between items:
  3814. ```````````````````````````````` example
  3815. - foo
  3816. - bar
  3817. - baz
  3818. .
  3819. <ul>
  3820. <li>
  3821. <p>foo</p>
  3822. </li>
  3823. <li>
  3824. <p>bar</p>
  3825. </li>
  3826. <li>
  3827. <p>baz</p>
  3828. </li>
  3829. </ul>
  3830. ````````````````````````````````
  3831. ```````````````````````````````` example
  3832. - foo
  3833. - bar
  3834. - baz
  3835. bim
  3836. .
  3837. <ul>
  3838. <li>foo
  3839. <ul>
  3840. <li>bar
  3841. <ul>
  3842. <li>
  3843. <p>baz</p>
  3844. <p>bim</p>
  3845. </li>
  3846. </ul>
  3847. </li>
  3848. </ul>
  3849. </li>
  3850. </ul>
  3851. ````````````````````````````````
  3852. To separate consecutive lists of the same type, or to separate a
  3853. list from an indented code block that would otherwise be parsed
  3854. as a subparagraph of the final list item, you can insert a blank HTML
  3855. comment:
  3856. ```````````````````````````````` example
  3857. - foo
  3858. - bar
  3859. <!-- -->
  3860. - baz
  3861. - bim
  3862. .
  3863. <ul>
  3864. <li>foo</li>
  3865. <li>bar</li>
  3866. </ul>
  3867. <!-- -->
  3868. <ul>
  3869. <li>baz</li>
  3870. <li>bim</li>
  3871. </ul>
  3872. ````````````````````````````````
  3873. ```````````````````````````````` example
  3874. - foo
  3875. notcode
  3876. - foo
  3877. <!-- -->
  3878. code
  3879. .
  3880. <ul>
  3881. <li>
  3882. <p>foo</p>
  3883. <p>notcode</p>
  3884. </li>
  3885. <li>
  3886. <p>foo</p>
  3887. </li>
  3888. </ul>
  3889. <!-- -->
  3890. <pre><code>code
  3891. </code></pre>
  3892. ````````````````````````````````
  3893. List items need not be indented to the same level. The following
  3894. list items will be treated as items at the same list level,
  3895. since none is indented enough to belong to the previous list
  3896. item:
  3897. ```````````````````````````````` example
  3898. - a
  3899. - b
  3900. - c
  3901. - d
  3902. - e
  3903. - f
  3904. - g
  3905. - h
  3906. - i
  3907. .
  3908. <ul>
  3909. <li>a</li>
  3910. <li>b</li>
  3911. <li>c</li>
  3912. <li>d</li>
  3913. <li>e</li>
  3914. <li>f</li>
  3915. <li>g</li>
  3916. <li>h</li>
  3917. <li>i</li>
  3918. </ul>
  3919. ````````````````````````````````
  3920. ```````````````````````````````` example
  3921. 1. a
  3922. 2. b
  3923. 3. c
  3924. .
  3925. <ol>
  3926. <li>
  3927. <p>a</p>
  3928. </li>
  3929. <li>
  3930. <p>b</p>
  3931. </li>
  3932. <li>
  3933. <p>c</p>
  3934. </li>
  3935. </ol>
  3936. ````````````````````````````````
  3937. This is a loose list, because there is a blank line between
  3938. two of the list items:
  3939. ```````````````````````````````` example
  3940. - a
  3941. - b
  3942. - c
  3943. .
  3944. <ul>
  3945. <li>
  3946. <p>a</p>
  3947. </li>
  3948. <li>
  3949. <p>b</p>
  3950. </li>
  3951. <li>
  3952. <p>c</p>
  3953. </li>
  3954. </ul>
  3955. ````````````````````````````````
  3956. So is this, with a empty second item:
  3957. ```````````````````````````````` example
  3958. * a
  3959. *
  3960. * c
  3961. .
  3962. <ul>
  3963. <li>
  3964. <p>a</p>
  3965. </li>
  3966. <li></li>
  3967. <li>
  3968. <p>c</p>
  3969. </li>
  3970. </ul>
  3971. ````````````````````````````````
  3972. These are loose lists, even though there is no space between the items,
  3973. because one of the items directly contains two block-level elements
  3974. with a blank line between them:
  3975. ```````````````````````````````` example
  3976. - a
  3977. - b
  3978. c
  3979. - d
  3980. .
  3981. <ul>
  3982. <li>
  3983. <p>a</p>
  3984. </li>
  3985. <li>
  3986. <p>b</p>
  3987. <p>c</p>
  3988. </li>
  3989. <li>
  3990. <p>d</p>
  3991. </li>
  3992. </ul>
  3993. ````````````````````````````````
  3994. ```````````````````````````````` example
  3995. - a
  3996. - b
  3997. [ref]: /url
  3998. - d
  3999. .
  4000. <ul>
  4001. <li>
  4002. <p>a</p>
  4003. </li>
  4004. <li>
  4005. <p>b</p>
  4006. </li>
  4007. <li>
  4008. <p>d</p>
  4009. </li>
  4010. </ul>
  4011. ````````````````````````````````
  4012. This is a tight list, because the blank lines are in a code block:
  4013. ```````````````````````````````` example
  4014. - a
  4015. - ```
  4016. b
  4017. ```
  4018. - c
  4019. .
  4020. <ul>
  4021. <li>a</li>
  4022. <li>
  4023. <pre><code>b
  4024. </code></pre>
  4025. </li>
  4026. <li>c</li>
  4027. </ul>
  4028. ````````````````````````````````
  4029. This is a tight list, because the blank line is between two
  4030. paragraphs of a sublist. So the sublist is loose while
  4031. the outer list is tight:
  4032. ```````````````````````````````` example
  4033. - a
  4034. - b
  4035. c
  4036. - d
  4037. .
  4038. <ul>
  4039. <li>a
  4040. <ul>
  4041. <li>
  4042. <p>b</p>
  4043. <p>c</p>
  4044. </li>
  4045. </ul>
  4046. </li>
  4047. <li>d</li>
  4048. </ul>
  4049. ````````````````````````````````
  4050. This is a tight list, because the blank line is inside the
  4051. block quote:
  4052. ```````````````````````````````` example
  4053. * a
  4054. > b
  4055. >
  4056. * c
  4057. .
  4058. <ul>
  4059. <li>a
  4060. <blockquote>
  4061. <p>b</p>
  4062. </blockquote>
  4063. </li>
  4064. <li>c</li>
  4065. </ul>
  4066. ````````````````````````````````
  4067. This list is tight, because the consecutive block elements
  4068. are not separated by blank lines:
  4069. ```````````````````````````````` example
  4070. - a
  4071. > b
  4072. ```
  4073. c
  4074. ```
  4075. - d
  4076. .
  4077. <ul>
  4078. <li>a
  4079. <blockquote>
  4080. <p>b</p>
  4081. </blockquote>
  4082. <pre><code>c
  4083. </code></pre>
  4084. </li>
  4085. <li>d</li>
  4086. </ul>
  4087. ````````````````````````````````
  4088. A single-paragraph list is tight:
  4089. ```````````````````````````````` example
  4090. - a
  4091. .
  4092. <ul>
  4093. <li>a</li>
  4094. </ul>
  4095. ````````````````````````````````
  4096. ```````````````````````````````` example
  4097. - a
  4098. - b
  4099. .
  4100. <ul>
  4101. <li>a
  4102. <ul>
  4103. <li>b</li>
  4104. </ul>
  4105. </li>
  4106. </ul>
  4107. ````````````````````````````````
  4108. This list is loose, because of the blank line between the
  4109. two block elements in the list item:
  4110. ```````````````````````````````` example
  4111. 1. ```
  4112. foo
  4113. ```
  4114. bar
  4115. .
  4116. <ol>
  4117. <li>
  4118. <pre><code>foo
  4119. </code></pre>
  4120. <p>bar</p>
  4121. </li>
  4122. </ol>
  4123. ````````````````````````````````
  4124. Here the outer list is loose, the inner list tight:
  4125. ```````````````````````````````` example
  4126. * foo
  4127. * bar
  4128. baz
  4129. .
  4130. <ul>
  4131. <li>
  4132. <p>foo</p>
  4133. <ul>
  4134. <li>bar</li>
  4135. </ul>
  4136. <p>baz</p>
  4137. </li>
  4138. </ul>
  4139. ````````````````````````````````
  4140. ```````````````````````````````` example
  4141. - a
  4142. - b
  4143. - c
  4144. - d
  4145. - e
  4146. - f
  4147. .
  4148. <ul>
  4149. <li>
  4150. <p>a</p>
  4151. <ul>
  4152. <li>b</li>
  4153. <li>c</li>
  4154. </ul>
  4155. </li>
  4156. <li>
  4157. <p>d</p>
  4158. <ul>
  4159. <li>e</li>
  4160. <li>f</li>
  4161. </ul>
  4162. </li>
  4163. </ul>
  4164. ````````````````````````````````
  4165. # Inlines
  4166. Inlines are parsed sequentially from the beginning of the character
  4167. stream to the end (left to right, in left-to-right languages).
  4168. Thus, for example, in
  4169. ```````````````````````````````` example
  4170. `hi`lo`
  4171. .
  4172. <p><code>hi</code>lo`</p>
  4173. ````````````````````````````````
  4174. `hi` is parsed as code, leaving the backtick at the end as a literal
  4175. backtick.
  4176. ## Backslash escapes
  4177. Any ASCII punctuation character may be backslash-escaped:
  4178. ```````````````````````````````` example
  4179. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4180. .
  4181. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4182. ````````````````````````````````
  4183. Backslashes before other characters are treated as literal
  4184. backslashes:
  4185. ```````````````````````````````` example
  4186. \→\A\a\ \3\φ\«
  4187. .
  4188. <p>\→\A\a\ \3\φ\«</p>
  4189. ````````````````````````````````
  4190. Escaped characters are treated as regular characters and do
  4191. not have their usual Markdown meanings:
  4192. ```````````````````````````````` example
  4193. \*not emphasized*
  4194. \<br/> not a tag
  4195. \[not a link](/foo)
  4196. \`not code`
  4197. 1\. not a list
  4198. \* not a list
  4199. \# not a heading
  4200. \[foo]: /url "not a reference"
  4201. .
  4202. <p>*not emphasized*
  4203. &lt;br/&gt; not a tag
  4204. [not a link](/foo)
  4205. `not code`
  4206. 1. not a list
  4207. * not a list
  4208. # not a heading
  4209. [foo]: /url &quot;not a reference&quot;</p>
  4210. ````````````````````````````````
  4211. If a backslash is itself escaped, the following character is not:
  4212. ```````````````````````````````` example
  4213. \\*emphasis*
  4214. .
  4215. <p>\<em>emphasis</em></p>
  4216. ````````````````````````````````
  4217. A backslash at the end of the line is a [hard line break]:
  4218. ```````````````````````````````` example
  4219. foo\
  4220. bar
  4221. .
  4222. <p>foo<br />
  4223. bar</p>
  4224. ````````````````````````````````
  4225. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4226. raw HTML:
  4227. ```````````````````````````````` example
  4228. `` \[\` ``
  4229. .
  4230. <p><code>\[\`</code></p>
  4231. ````````````````````````````````
  4232. ```````````````````````````````` example
  4233. \[\]
  4234. .
  4235. <pre><code>\[\]
  4236. </code></pre>
  4237. ````````````````````````````````
  4238. ```````````````````````````````` example
  4239. ~~~
  4240. \[\]
  4241. ~~~
  4242. .
  4243. <pre><code>\[\]
  4244. </code></pre>
  4245. ````````````````````````````````
  4246. ```````````````````````````````` example
  4247. <http://example.com?find=\*>
  4248. .
  4249. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4250. ````````````````````````````````
  4251. ```````````````````````````````` example
  4252. <a href="/bar\/)">
  4253. .
  4254. <a href="/bar\/)">
  4255. ````````````````````````````````
  4256. But they work in all other contexts, including URLs and link titles,
  4257. link references, and [info strings] in [fenced code blocks]:
  4258. ```````````````````````````````` example
  4259. [foo](/bar\* "ti\*tle")
  4260. .
  4261. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4262. ````````````````````````````````
  4263. ```````````````````````````````` example
  4264. [foo]
  4265. [foo]: /bar\* "ti\*tle"
  4266. .
  4267. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4268. ````````````````````````````````
  4269. ```````````````````````````````` example
  4270. ``` foo\+bar
  4271. foo
  4272. ```
  4273. .
  4274. <pre><code class="language-foo+bar">foo
  4275. </code></pre>
  4276. ````````````````````````````````
  4277. ## Entity and numeric character references
  4278. All valid HTML entity references and numeric character
  4279. references, except those occuring in code blocks and code spans,
  4280. are recognized as such and treated as equivalent to the
  4281. corresponding Unicode characters. Conforming CommonMark parsers
  4282. need not store information about whether a particular character
  4283. was represented in the source using a Unicode character or
  4284. an entity reference.
  4285. [Entity references](@) consist of `&` + any of the valid
  4286. HTML5 entity names + `;`. The
  4287. document <https://html.spec.whatwg.org/multipage/entities.json>
  4288. is used as an authoritative source for the valid entity
  4289. references and their corresponding code points.
  4290. ```````````````````````````````` example
  4291. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4292. &frac34; &HilbertSpace; &DifferentialD;
  4293. &ClockwiseContourIntegral; &ngE;
  4294. .
  4295. <p>  &amp; © Æ Ď
  4296. ¾ ℋ ⅆ
  4297. ∲ ≧̸</p>
  4298. ````````````````````````````````
  4299. [Decimal numeric character
  4300. references](@)
  4301. consist of `&#` + a string of 1--8 arabic digits + `;`. A
  4302. numeric character reference is parsed as the corresponding
  4303. Unicode character. Invalid Unicode code points will be replaced by
  4304. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4305. the code point `U+0000` will also be replaced by `U+FFFD`.
  4306. ```````````````````````````````` example
  4307. &#35; &#1234; &#992; &#98765432; &#0;
  4308. .
  4309. <p># Ӓ Ϡ � �</p>
  4310. ````````````````````````````````
  4311. [Hexadecimal numeric character
  4312. references](@) consist of `&#` +
  4313. either `X` or `x` + a string of 1-8 hexadecimal digits + `;`.
  4314. They too are parsed as the corresponding Unicode character (this
  4315. time specified with a hexadecimal numeral instead of decimal).
  4316. ```````````````````````````````` example
  4317. &#X22; &#XD06; &#xcab;
  4318. .
  4319. <p>&quot; ആ ಫ</p>
  4320. ````````````````````````````````
  4321. Here are some nonentities:
  4322. ```````````````````````````````` example
  4323. &nbsp &x; &#; &#x;
  4324. &ThisIsNotDefined; &hi?;
  4325. .
  4326. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4327. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4328. ````````````````````````````````
  4329. Although HTML5 does accept some entity references
  4330. without a trailing semicolon (such as `&copy`), these are not
  4331. recognized here, because it makes the grammar too ambiguous:
  4332. ```````````````````````````````` example
  4333. &copy
  4334. .
  4335. <p>&amp;copy</p>
  4336. ````````````````````````````````
  4337. Strings that are not on the list of HTML5 named entities are not
  4338. recognized as entity references either:
  4339. ```````````````````````````````` example
  4340. &MadeUpEntity;
  4341. .
  4342. <p>&amp;MadeUpEntity;</p>
  4343. ````````````````````````````````
  4344. Entity and numeric character references are recognized in any
  4345. context besides code spans or code blocks, including
  4346. URLs, [link titles], and [fenced code block][] [info strings]:
  4347. ```````````````````````````````` example
  4348. <a href="&ouml;&ouml;.html">
  4349. .
  4350. <a href="&ouml;&ouml;.html">
  4351. ````````````````````````````````
  4352. ```````````````````````````````` example
  4353. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4354. .
  4355. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4356. ````````````````````````````````
  4357. ```````````````````````````````` example
  4358. [foo]
  4359. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4360. .
  4361. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4362. ````````````````````````````````
  4363. ```````````````````````````````` example
  4364. ``` f&ouml;&ouml;
  4365. foo
  4366. ```
  4367. .
  4368. <pre><code class="language-föö">foo
  4369. </code></pre>
  4370. ````````````````````````````````
  4371. Entity and numeric character references are treated as literal
  4372. text in code spans and code blocks:
  4373. ```````````````````````````````` example
  4374. `f&ouml;&ouml;`
  4375. .
  4376. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4377. ````````````````````````````````
  4378. ```````````````````````````````` example
  4379. f&ouml;f&ouml;
  4380. .
  4381. <pre><code>f&amp;ouml;f&amp;ouml;
  4382. </code></pre>
  4383. ````````````````````````````````
  4384. ## Code spans
  4385. A [backtick string](@)
  4386. is a string of one or more backtick characters (`` ` ``) that is neither
  4387. preceded nor followed by a backtick.
  4388. A [code span](@) begins with a backtick string and ends with
  4389. a backtick string of equal length. The contents of the code span are
  4390. the characters between the two backtick strings, with leading and
  4391. trailing spaces and [line endings] removed, and
  4392. [whitespace] collapsed to single spaces.
  4393. This is a simple code span:
  4394. ```````````````````````````````` example
  4395. `foo`
  4396. .
  4397. <p><code>foo</code></p>
  4398. ````````````````````````````````
  4399. Here two backticks are used, because the code contains a backtick.
  4400. This example also illustrates stripping of leading and trailing spaces:
  4401. ```````````````````````````````` example
  4402. `` foo ` bar ``
  4403. .
  4404. <p><code>foo ` bar</code></p>
  4405. ````````````````````````````````
  4406. This example shows the motivation for stripping leading and trailing
  4407. spaces:
  4408. ```````````````````````````````` example
  4409. ` `` `
  4410. .
  4411. <p><code>``</code></p>
  4412. ````````````````````````````````
  4413. [Line endings] are treated like spaces:
  4414. ```````````````````````````````` example
  4415. ``
  4416. foo
  4417. ``
  4418. .
  4419. <p><code>foo</code></p>
  4420. ````````````````````````````````
  4421. Interior spaces and [line endings] are collapsed into
  4422. single spaces, just as they would be by a browser:
  4423. ```````````````````````````````` example
  4424. `foo bar
  4425. baz`
  4426. .
  4427. <p><code>foo bar baz</code></p>
  4428. ````````````````````````````````
  4429. Not all [Unicode whitespace] (for instance, non-breaking space) is
  4430. collapsed, however:
  4431. ```````````````````````````````` example
  4432. `a  b`
  4433. .
  4434. <p><code>a  b</code></p>
  4435. ````````````````````````````````
  4436. Q: Why not just leave the spaces, since browsers will collapse them
  4437. anyway? A: Because we might be targeting a non-HTML format, and we
  4438. shouldn't rely on HTML-specific rendering assumptions.
  4439. (Existing implementations differ in their treatment of internal
  4440. spaces and [line endings]. Some, including `Markdown.pl` and
  4441. `showdown`, convert an internal [line ending] into a
  4442. `<br />` tag. But this makes things difficult for those who like to
  4443. hard-wrap their paragraphs, since a line break in the midst of a code
  4444. span will cause an unintended line break in the output. Others just
  4445. leave internal spaces as they are, which is fine if only HTML is being
  4446. targeted.)
  4447. ```````````````````````````````` example
  4448. `foo `` bar`
  4449. .
  4450. <p><code>foo `` bar</code></p>
  4451. ````````````````````````````````
  4452. Note that backslash escapes do not work in code spans. All backslashes
  4453. are treated literally:
  4454. ```````````````````````````````` example
  4455. `foo\`bar`
  4456. .
  4457. <p><code>foo\</code>bar`</p>
  4458. ````````````````````````````````
  4459. Backslash escapes are never needed, because one can always choose a
  4460. string of *n* backtick characters as delimiters, where the code does
  4461. not contain any strings of exactly *n* backtick characters.
  4462. Code span backticks have higher precedence than any other inline
  4463. constructs except HTML tags and autolinks. Thus, for example, this is
  4464. not parsed as emphasized text, since the second `*` is part of a code
  4465. span:
  4466. ```````````````````````````````` example
  4467. *foo`*`
  4468. .
  4469. <p>*foo<code>*</code></p>
  4470. ````````````````````````````````
  4471. And this is not parsed as a link:
  4472. ```````````````````````````````` example
  4473. [not a `link](/foo`)
  4474. .
  4475. <p>[not a <code>link](/foo</code>)</p>
  4476. ````````````````````````````````
  4477. Code spans, HTML tags, and autolinks have the same precedence.
  4478. Thus, this is code:
  4479. ```````````````````````````````` example
  4480. `<a href="`">`
  4481. .
  4482. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4483. ````````````````````````````````
  4484. But this is an HTML tag:
  4485. ```````````````````````````````` example
  4486. <a href="`">`
  4487. .
  4488. <p><a href="`">`</p>
  4489. ````````````````````````````````
  4490. And this is code:
  4491. ```````````````````````````````` example
  4492. `<http://foo.bar.`baz>`
  4493. .
  4494. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4495. ````````````````````````````````
  4496. But this is an autolink:
  4497. ```````````````````````````````` example
  4498. <http://foo.bar.`baz>`
  4499. .
  4500. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4501. ````````````````````````````````
  4502. When a backtick string is not closed by a matching backtick string,
  4503. we just have literal backticks:
  4504. ```````````````````````````````` example
  4505. ```foo``
  4506. .
  4507. <p>```foo``</p>
  4508. ````````````````````````````````
  4509. ```````````````````````````````` example
  4510. `foo
  4511. .
  4512. <p>`foo</p>
  4513. ````````````````````````````````
  4514. The following case also illustrates the need for opening and
  4515. closing backtick strings to be equal in length:
  4516. ```````````````````````````````` example
  4517. `foo``bar``
  4518. .
  4519. <p>`foo<code>bar</code></p>
  4520. ````````````````````````````````
  4521. ## Emphasis and strong emphasis
  4522. John Gruber's original [Markdown syntax
  4523. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4524. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4525. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4526. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4527. > tag.
  4528. This is enough for most users, but these rules leave much undecided,
  4529. especially when it comes to nested emphasis. The original
  4530. `Markdown.pl` test suite makes it clear that triple `***` and
  4531. `___` delimiters can be used for strong emphasis, and most
  4532. implementations have also allowed the following patterns:
  4533. ``` markdown
  4534. ***strong emph***
  4535. ***strong** in emph*
  4536. ***emph* in strong**
  4537. **in strong *emph***
  4538. *in emph **strong***
  4539. ```
  4540. The following patterns are less widely supported, but the intent
  4541. is clear and they are useful (especially in contexts like bibliography
  4542. entries):
  4543. ``` markdown
  4544. *emph *with emph* in it*
  4545. **strong **with strong** in it**
  4546. ```
  4547. Many implementations have also restricted intraword emphasis to
  4548. the `*` forms, to avoid unwanted emphasis in words containing
  4549. internal underscores. (It is best practice to put these in code
  4550. spans, but users often do not.)
  4551. ``` markdown
  4552. internal emphasis: foo*bar*baz
  4553. no emphasis: foo_bar_baz
  4554. ```
  4555. The rules given below capture all of these patterns, while allowing
  4556. for efficient parsing strategies that do not backtrack.
  4557. First, some definitions. A [delimiter run](@) is either
  4558. a sequence of one or more `*` characters that is not preceded or
  4559. followed by a non-backslash-escaped `*` character, or a sequence
  4560. of one or more `_` characters that is not preceded or followed by
  4561. a non-backslash-escaped `_` character.
  4562. A [left-flanking delimiter run](@) is
  4563. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4564. and (b) not followed by a [punctuation character], or
  4565. preceded by [Unicode whitespace] or a [punctuation character].
  4566. For purposes of this definition, the beginning and the end of
  4567. the line count as Unicode whitespace.
  4568. A [right-flanking delimiter run](@) is
  4569. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4570. and (b) not preceded by a [punctuation character], or
  4571. followed by [Unicode whitespace] or a [punctuation character].
  4572. For purposes of this definition, the beginning and the end of
  4573. the line count as Unicode whitespace.
  4574. Here are some examples of delimiter runs.
  4575. - left-flanking but not right-flanking:
  4576. ```
  4577. ***abc
  4578. _abc
  4579. **"abc"
  4580. _"abc"
  4581. ```
  4582. - right-flanking but not left-flanking:
  4583. ```
  4584. abc***
  4585. abc_
  4586. "abc"**
  4587. "abc"_
  4588. ```
  4589. - Both left and right-flanking:
  4590. ```
  4591. abc***def
  4592. "abc"_"def"
  4593. ```
  4594. - Neither left nor right-flanking:
  4595. ```
  4596. abc *** def
  4597. a _ b
  4598. ```
  4599. (The idea of distinguishing left-flanking and right-flanking
  4600. delimiter runs based on the character before and the character
  4601. after comes from Roopesh Chander's
  4602. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4603. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4604. run," and its rules for distinguishing left- and right-flanking runs
  4605. are a bit more complex than the ones given here.)
  4606. The following rules define emphasis and strong emphasis:
  4607. 1. A single `*` character [can open emphasis](@)
  4608. iff (if and only if) it is part of a [left-flanking delimiter run].
  4609. 2. A single `_` character [can open emphasis] iff
  4610. it is part of a [left-flanking delimiter run]
  4611. and either (a) not part of a [right-flanking delimiter run]
  4612. or (b) part of a [right-flanking delimiter run]
  4613. preceded by punctuation.
  4614. 3. A single `*` character [can close emphasis](@)
  4615. iff it is part of a [right-flanking delimiter run].
  4616. 4. A single `_` character [can close emphasis] iff
  4617. it is part of a [right-flanking delimiter run]
  4618. and either (a) not part of a [left-flanking delimiter run]
  4619. or (b) part of a [left-flanking delimiter run]
  4620. followed by punctuation.
  4621. 5. A double `**` [can open strong emphasis](@)
  4622. iff it is part of a [left-flanking delimiter run].
  4623. 6. A double `__` [can open strong emphasis] iff
  4624. it is part of a [left-flanking delimiter run]
  4625. and either (a) not part of a [right-flanking delimiter run]
  4626. or (b) part of a [right-flanking delimiter run]
  4627. preceded by punctuation.
  4628. 7. A double `**` [can close strong emphasis](@)
  4629. iff it is part of a [right-flanking delimiter run].
  4630. 8. A double `__` [can close strong emphasis] iff
  4631. it is part of a [right-flanking delimiter run]
  4632. and either (a) not part of a [left-flanking delimiter run]
  4633. or (b) part of a [left-flanking delimiter run]
  4634. followed by punctuation.
  4635. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4636. with a delimiter that [can close emphasis], and that uses the same
  4637. character (`_` or `*`) as the opening delimiter. The
  4638. opening and closing delimiters must belong to separate
  4639. [delimiter runs]. If one of the delimiters can both
  4640. open and close emphasis, then the sum of the lengths of the
  4641. delimiter runs containing the opening and closing delimiters
  4642. must not be a multiple of 3.
  4643. 10. Strong emphasis begins with a delimiter that
  4644. [can open strong emphasis] and ends with a delimiter that
  4645. [can close strong emphasis], and that uses the same character
  4646. (`_` or `*`) as the opening delimiter. The
  4647. opening and closing delimiters must belong to separate
  4648. [delimiter runs]. If one of the delimiters can both open
  4649. and close strong emphasis, then the sum of the lengths of
  4650. the delimiter runs containing the opening and closing
  4651. delimiters must not be a multiple of 3.
  4652. 11. A literal `*` character cannot occur at the beginning or end of
  4653. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4654. is backslash-escaped.
  4655. 12. A literal `_` character cannot occur at the beginning or end of
  4656. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4657. is backslash-escaped.
  4658. Where rules 1--12 above are compatible with multiple parsings,
  4659. the following principles resolve ambiguity:
  4660. 13. The number of nestings should be minimized. Thus, for example,
  4661. an interpretation `<strong>...</strong>` is always preferred to
  4662. `<em><em>...</em></em>`.
  4663. 14. An interpretation `<em><strong>...</strong></em>` is always
  4664. preferred to `<strong><em>...</em></strong>`.
  4665. 15. When two potential emphasis or strong emphasis spans overlap,
  4666. so that the second begins before the first ends and ends after
  4667. the first ends, the first takes precedence. Thus, for example,
  4668. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4669. than `*foo <em>bar* baz</em>`.
  4670. 16. When there are two potential emphasis or strong emphasis spans
  4671. with the same closing delimiter, the shorter one (the one that
  4672. opens later) takes precedence. Thus, for example,
  4673. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4674. rather than `<strong>foo **bar baz</strong>`.
  4675. 17. Inline code spans, links, images, and HTML tags group more tightly
  4676. than emphasis. So, when there is a choice between an interpretation
  4677. that contains one of these elements and one that does not, the
  4678. former always wins. Thus, for example, `*[foo*](bar)` is
  4679. parsed as `*<a href="bar">foo*</a>` rather than as
  4680. `<em>[foo</em>](bar)`.
  4681. These rules can be illustrated through a series of examples.
  4682. Rule 1:
  4683. ```````````````````````````````` example
  4684. *foo bar*
  4685. .
  4686. <p><em>foo bar</em></p>
  4687. ````````````````````````````````
  4688. This is not emphasis, because the opening `*` is followed by
  4689. whitespace, and hence not part of a [left-flanking delimiter run]:
  4690. ```````````````````````````````` example
  4691. a * foo bar*
  4692. .
  4693. <p>a * foo bar*</p>
  4694. ````````````````````````````````
  4695. This is not emphasis, because the opening `*` is preceded
  4696. by an alphanumeric and followed by punctuation, and hence
  4697. not part of a [left-flanking delimiter run]:
  4698. ```````````````````````````````` example
  4699. a*"foo"*
  4700. .
  4701. <p>a*&quot;foo&quot;*</p>
  4702. ````````````````````````````````
  4703. Unicode nonbreaking spaces count as whitespace, too:
  4704. ```````````````````````````````` example
  4705. * a *
  4706. .
  4707. <p>* a *</p>
  4708. ````````````````````````````````
  4709. Intraword emphasis with `*` is permitted:
  4710. ```````````````````````````````` example
  4711. foo*bar*
  4712. .
  4713. <p>foo<em>bar</em></p>
  4714. ````````````````````````````````
  4715. ```````````````````````````````` example
  4716. 5*6*78
  4717. .
  4718. <p>5<em>6</em>78</p>
  4719. ````````````````````````````````
  4720. Rule 2:
  4721. ```````````````````````````````` example
  4722. _foo bar_
  4723. .
  4724. <p><em>foo bar</em></p>
  4725. ````````````````````````````````
  4726. This is not emphasis, because the opening `_` is followed by
  4727. whitespace:
  4728. ```````````````````````````````` example
  4729. _ foo bar_
  4730. .
  4731. <p>_ foo bar_</p>
  4732. ````````````````````````````````
  4733. This is not emphasis, because the opening `_` is preceded
  4734. by an alphanumeric and followed by punctuation:
  4735. ```````````````````````````````` example
  4736. a_"foo"_
  4737. .
  4738. <p>a_&quot;foo&quot;_</p>
  4739. ````````````````````````````````
  4740. Emphasis with `_` is not allowed inside words:
  4741. ```````````````````````````````` example
  4742. foo_bar_
  4743. .
  4744. <p>foo_bar_</p>
  4745. ````````````````````````````````
  4746. ```````````````````````````````` example
  4747. 5_6_78
  4748. .
  4749. <p>5_6_78</p>
  4750. ````````````````````````````````
  4751. ```````````````````````````````` example
  4752. пристаням_стремятся_
  4753. .
  4754. <p>пристаням_стремятся_</p>
  4755. ````````````````````````````````
  4756. Here `_` does not generate emphasis, because the first delimiter run
  4757. is right-flanking and the second left-flanking:
  4758. ```````````````````````````````` example
  4759. aa_"bb"_cc
  4760. .
  4761. <p>aa_&quot;bb&quot;_cc</p>
  4762. ````````````````````````````````
  4763. This is emphasis, even though the opening delimiter is
  4764. both left- and right-flanking, because it is preceded by
  4765. punctuation:
  4766. ```````````````````````````````` example
  4767. foo-_(bar)_
  4768. .
  4769. <p>foo-<em>(bar)</em></p>
  4770. ````````````````````````````````
  4771. Rule 3:
  4772. This is not emphasis, because the closing delimiter does
  4773. not match the opening delimiter:
  4774. ```````````````````````````````` example
  4775. _foo*
  4776. .
  4777. <p>_foo*</p>
  4778. ````````````````````````````````
  4779. This is not emphasis, because the closing `*` is preceded by
  4780. whitespace:
  4781. ```````````````````````````````` example
  4782. *foo bar *
  4783. .
  4784. <p>*foo bar *</p>
  4785. ````````````````````````````````
  4786. A newline also counts as whitespace:
  4787. ```````````````````````````````` example
  4788. *foo bar
  4789. *
  4790. .
  4791. <p>*foo bar
  4792. *</p>
  4793. ````````````````````````````````
  4794. This is not emphasis, because the second `*` is
  4795. preceded by punctuation and followed by an alphanumeric
  4796. (hence it is not part of a [right-flanking delimiter run]:
  4797. ```````````````````````````````` example
  4798. *(*foo)
  4799. .
  4800. <p>*(*foo)</p>
  4801. ````````````````````````````````
  4802. The point of this restriction is more easily appreciated
  4803. with this example:
  4804. ```````````````````````````````` example
  4805. *(*foo*)*
  4806. .
  4807. <p><em>(<em>foo</em>)</em></p>
  4808. ````````````````````````````````
  4809. Intraword emphasis with `*` is allowed:
  4810. ```````````````````````````````` example
  4811. *foo*bar
  4812. .
  4813. <p><em>foo</em>bar</p>
  4814. ````````````````````````````````
  4815. Rule 4:
  4816. This is not emphasis, because the closing `_` is preceded by
  4817. whitespace:
  4818. ```````````````````````````````` example
  4819. _foo bar _
  4820. .
  4821. <p>_foo bar _</p>
  4822. ````````````````````````````````
  4823. This is not emphasis, because the second `_` is
  4824. preceded by punctuation and followed by an alphanumeric:
  4825. ```````````````````````````````` example
  4826. _(_foo)
  4827. .
  4828. <p>_(_foo)</p>
  4829. ````````````````````````````````
  4830. This is emphasis within emphasis:
  4831. ```````````````````````````````` example
  4832. _(_foo_)_
  4833. .
  4834. <p><em>(<em>foo</em>)</em></p>
  4835. ````````````````````````````````
  4836. Intraword emphasis is disallowed for `_`:
  4837. ```````````````````````````````` example
  4838. _foo_bar
  4839. .
  4840. <p>_foo_bar</p>
  4841. ````````````````````````````````
  4842. ```````````````````````````````` example
  4843. _пристаням_стремятся
  4844. .
  4845. <p>_пристаням_стремятся</p>
  4846. ````````````````````````````````
  4847. ```````````````````````````````` example
  4848. _foo_bar_baz_
  4849. .
  4850. <p><em>foo_bar_baz</em></p>
  4851. ````````````````````````````````
  4852. This is emphasis, even though the closing delimiter is
  4853. both left- and right-flanking, because it is followed by
  4854. punctuation:
  4855. ```````````````````````````````` example
  4856. _(bar)_.
  4857. .
  4858. <p><em>(bar)</em>.</p>
  4859. ````````````````````````````````
  4860. Rule 5:
  4861. ```````````````````````````````` example
  4862. **foo bar**
  4863. .
  4864. <p><strong>foo bar</strong></p>
  4865. ````````````````````````````````
  4866. This is not strong emphasis, because the opening delimiter is
  4867. followed by whitespace:
  4868. ```````````````````````````````` example
  4869. ** foo bar**
  4870. .
  4871. <p>** foo bar**</p>
  4872. ````````````````````````````````
  4873. This is not strong emphasis, because the opening `**` is preceded
  4874. by an alphanumeric and followed by punctuation, and hence
  4875. not part of a [left-flanking delimiter run]:
  4876. ```````````````````````````````` example
  4877. a**"foo"**
  4878. .
  4879. <p>a**&quot;foo&quot;**</p>
  4880. ````````````````````````````````
  4881. Intraword strong emphasis with `**` is permitted:
  4882. ```````````````````````````````` example
  4883. foo**bar**
  4884. .
  4885. <p>foo<strong>bar</strong></p>
  4886. ````````````````````````````````
  4887. Rule 6:
  4888. ```````````````````````````````` example
  4889. __foo bar__
  4890. .
  4891. <p><strong>foo bar</strong></p>
  4892. ````````````````````````````````
  4893. This is not strong emphasis, because the opening delimiter is
  4894. followed by whitespace:
  4895. ```````````````````````````````` example
  4896. __ foo bar__
  4897. .
  4898. <p>__ foo bar__</p>
  4899. ````````````````````````````````
  4900. A newline counts as whitespace:
  4901. ```````````````````````````````` example
  4902. __
  4903. foo bar__
  4904. .
  4905. <p>__
  4906. foo bar__</p>
  4907. ````````````````````````````````
  4908. This is not strong emphasis, because the opening `__` is preceded
  4909. by an alphanumeric and followed by punctuation:
  4910. ```````````````````````````````` example
  4911. a__"foo"__
  4912. .
  4913. <p>a__&quot;foo&quot;__</p>
  4914. ````````````````````````````````
  4915. Intraword strong emphasis is forbidden with `__`:
  4916. ```````````````````````````````` example
  4917. foo__bar__
  4918. .
  4919. <p>foo__bar__</p>
  4920. ````````````````````````````````
  4921. ```````````````````````````````` example
  4922. 5__6__78
  4923. .
  4924. <p>5__6__78</p>
  4925. ````````````````````````````````
  4926. ```````````````````````````````` example
  4927. пристаням__стремятся__
  4928. .
  4929. <p>пристаням__стремятся__</p>
  4930. ````````````````````````````````
  4931. ```````````````````````````````` example
  4932. __foo, __bar__, baz__
  4933. .
  4934. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4935. ````````````````````````````````
  4936. This is strong emphasis, even though the opening delimiter is
  4937. both left- and right-flanking, because it is preceded by
  4938. punctuation:
  4939. ```````````````````````````````` example
  4940. foo-__(bar)__
  4941. .
  4942. <p>foo-<strong>(bar)</strong></p>
  4943. ````````````````````````````````
  4944. Rule 7:
  4945. This is not strong emphasis, because the closing delimiter is preceded
  4946. by whitespace:
  4947. ```````````````````````````````` example
  4948. **foo bar **
  4949. .
  4950. <p>**foo bar **</p>
  4951. ````````````````````````````````
  4952. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4953. Rule 11.)
  4954. This is not strong emphasis, because the second `**` is
  4955. preceded by punctuation and followed by an alphanumeric:
  4956. ```````````````````````````````` example
  4957. **(**foo)
  4958. .
  4959. <p>**(**foo)</p>
  4960. ````````````````````````````````
  4961. The point of this restriction is more easily appreciated
  4962. with these examples:
  4963. ```````````````````````````````` example
  4964. *(**foo**)*
  4965. .
  4966. <p><em>(<strong>foo</strong>)</em></p>
  4967. ````````````````````````````````
  4968. ```````````````````````````````` example
  4969. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4970. *Asclepias physocarpa*)**
  4971. .
  4972. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4973. <em>Asclepias physocarpa</em>)</strong></p>
  4974. ````````````````````````````````
  4975. ```````````````````````````````` example
  4976. **foo "*bar*" foo**
  4977. .
  4978. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4979. ````````````````````````````````
  4980. Intraword emphasis:
  4981. ```````````````````````````````` example
  4982. **foo**bar
  4983. .
  4984. <p><strong>foo</strong>bar</p>
  4985. ````````````````````````````````
  4986. Rule 8:
  4987. This is not strong emphasis, because the closing delimiter is
  4988. preceded by whitespace:
  4989. ```````````````````````````````` example
  4990. __foo bar __
  4991. .
  4992. <p>__foo bar __</p>
  4993. ````````````````````````````````
  4994. This is not strong emphasis, because the second `__` is
  4995. preceded by punctuation and followed by an alphanumeric:
  4996. ```````````````````````````````` example
  4997. __(__foo)
  4998. .
  4999. <p>__(__foo)</p>
  5000. ````````````````````````````````
  5001. The point of this restriction is more easily appreciated
  5002. with this example:
  5003. ```````````````````````````````` example
  5004. _(__foo__)_
  5005. .
  5006. <p><em>(<strong>foo</strong>)</em></p>
  5007. ````````````````````````````````
  5008. Intraword strong emphasis is forbidden with `__`:
  5009. ```````````````````````````````` example
  5010. __foo__bar
  5011. .
  5012. <p>__foo__bar</p>
  5013. ````````````````````````````````
  5014. ```````````````````````````````` example
  5015. __пристаням__стремятся
  5016. .
  5017. <p>__пристаням__стремятся</p>
  5018. ````````````````````````````````
  5019. ```````````````````````````````` example
  5020. __foo__bar__baz__
  5021. .
  5022. <p><strong>foo__bar__baz</strong></p>
  5023. ````````````````````````````````
  5024. This is strong emphasis, even though the closing delimiter is
  5025. both left- and right-flanking, because it is followed by
  5026. punctuation:
  5027. ```````````````````````````````` example
  5028. __(bar)__.
  5029. .
  5030. <p><strong>(bar)</strong>.</p>
  5031. ````````````````````````````````
  5032. Rule 9:
  5033. Any nonempty sequence of inline elements can be the contents of an
  5034. emphasized span.
  5035. ```````````````````````````````` example
  5036. *foo [bar](/url)*
  5037. .
  5038. <p><em>foo <a href="/url">bar</a></em></p>
  5039. ````````````````````````````````
  5040. ```````````````````````````````` example
  5041. *foo
  5042. bar*
  5043. .
  5044. <p><em>foo
  5045. bar</em></p>
  5046. ````````````````````````````````
  5047. In particular, emphasis and strong emphasis can be nested
  5048. inside emphasis:
  5049. ```````````````````````````````` example
  5050. _foo __bar__ baz_
  5051. .
  5052. <p><em>foo <strong>bar</strong> baz</em></p>
  5053. ````````````````````````````````
  5054. ```````````````````````````````` example
  5055. _foo _bar_ baz_
  5056. .
  5057. <p><em>foo <em>bar</em> baz</em></p>
  5058. ````````````````````````````````
  5059. ```````````````````````````````` example
  5060. __foo_ bar_
  5061. .
  5062. <p><em><em>foo</em> bar</em></p>
  5063. ````````````````````````````````
  5064. ```````````````````````````````` example
  5065. *foo *bar**
  5066. .
  5067. <p><em>foo <em>bar</em></em></p>
  5068. ````````````````````````````````
  5069. ```````````````````````````````` example
  5070. *foo **bar** baz*
  5071. .
  5072. <p><em>foo <strong>bar</strong> baz</em></p>
  5073. ````````````````````````````````
  5074. ```````````````````````````````` example
  5075. *foo**bar**baz*
  5076. .
  5077. <p><em>foo<strong>bar</strong>baz</em></p>
  5078. ````````````````````````````````
  5079. Note that in the preceding case, the interpretation
  5080. ``` markdown
  5081. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5082. ```
  5083. is precluded by the condition that a delimiter that
  5084. can both open and close (like the `*` after `foo`)
  5085. cannot form emphasis if the sum of the lengths of
  5086. the delimiter runs containing the opening and
  5087. closing delimiters is a multiple of 3.
  5088. The same condition ensures that the following
  5089. cases are all strong emphasis nested inside
  5090. emphasis, even when the interior spaces are
  5091. omitted:
  5092. ```````````````````````````````` example
  5093. ***foo** bar*
  5094. .
  5095. <p><em><strong>foo</strong> bar</em></p>
  5096. ````````````````````````````````
  5097. ```````````````````````````````` example
  5098. *foo **bar***
  5099. .
  5100. <p><em>foo <strong>bar</strong></em></p>
  5101. ````````````````````````````````
  5102. ```````````````````````````````` example
  5103. *foo**bar***
  5104. .
  5105. <p><em>foo<strong>bar</strong></em></p>
  5106. ````````````````````````````````
  5107. Indefinite levels of nesting are possible:
  5108. ```````````````````````````````` example
  5109. *foo **bar *baz* bim** bop*
  5110. .
  5111. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5112. ````````````````````````````````
  5113. ```````````````````````````````` example
  5114. *foo [*bar*](/url)*
  5115. .
  5116. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5117. ````````````````````````````````
  5118. There can be no empty emphasis or strong emphasis:
  5119. ```````````````````````````````` example
  5120. ** is not an empty emphasis
  5121. .
  5122. <p>** is not an empty emphasis</p>
  5123. ````````````````````````````````
  5124. ```````````````````````````````` example
  5125. **** is not an empty strong emphasis
  5126. .
  5127. <p>**** is not an empty strong emphasis</p>
  5128. ````````````````````````````````
  5129. Rule 10:
  5130. Any nonempty sequence of inline elements can be the contents of an
  5131. strongly emphasized span.
  5132. ```````````````````````````````` example
  5133. **foo [bar](/url)**
  5134. .
  5135. <p><strong>foo <a href="/url">bar</a></strong></p>
  5136. ````````````````````````````````
  5137. ```````````````````````````````` example
  5138. **foo
  5139. bar**
  5140. .
  5141. <p><strong>foo
  5142. bar</strong></p>
  5143. ````````````````````````````````
  5144. In particular, emphasis and strong emphasis can be nested
  5145. inside strong emphasis:
  5146. ```````````````````````````````` example
  5147. __foo _bar_ baz__
  5148. .
  5149. <p><strong>foo <em>bar</em> baz</strong></p>
  5150. ````````````````````````````````
  5151. ```````````````````````````````` example
  5152. __foo __bar__ baz__
  5153. .
  5154. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5155. ````````````````````````````````
  5156. ```````````````````````````````` example
  5157. ____foo__ bar__
  5158. .
  5159. <p><strong><strong>foo</strong> bar</strong></p>
  5160. ````````````````````````````````
  5161. ```````````````````````````````` example
  5162. **foo **bar****
  5163. .
  5164. <p><strong>foo <strong>bar</strong></strong></p>
  5165. ````````````````````````````````
  5166. ```````````````````````````````` example
  5167. **foo *bar* baz**
  5168. .
  5169. <p><strong>foo <em>bar</em> baz</strong></p>
  5170. ````````````````````````````````
  5171. ```````````````````````````````` example
  5172. **foo*bar*baz**
  5173. .
  5174. <p><strong>foo<em>bar</em>baz</strong></p>
  5175. ````````````````````````````````
  5176. ```````````````````````````````` example
  5177. ***foo* bar**
  5178. .
  5179. <p><strong><em>foo</em> bar</strong></p>
  5180. ````````````````````````````````
  5181. ```````````````````````````````` example
  5182. **foo *bar***
  5183. .
  5184. <p><strong>foo <em>bar</em></strong></p>
  5185. ````````````````````````````````
  5186. Indefinite levels of nesting are possible:
  5187. ```````````````````````````````` example
  5188. **foo *bar **baz**
  5189. bim* bop**
  5190. .
  5191. <p><strong>foo <em>bar <strong>baz</strong>
  5192. bim</em> bop</strong></p>
  5193. ````````````````````````````````
  5194. ```````````````````````````````` example
  5195. **foo [*bar*](/url)**
  5196. .
  5197. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5198. ````````````````````````````````
  5199. There can be no empty emphasis or strong emphasis:
  5200. ```````````````````````````````` example
  5201. __ is not an empty emphasis
  5202. .
  5203. <p>__ is not an empty emphasis</p>
  5204. ````````````````````````````````
  5205. ```````````````````````````````` example
  5206. ____ is not an empty strong emphasis
  5207. .
  5208. <p>____ is not an empty strong emphasis</p>
  5209. ````````````````````````````````
  5210. Rule 11:
  5211. ```````````````````````````````` example
  5212. foo ***
  5213. .
  5214. <p>foo ***</p>
  5215. ````````````````````````````````
  5216. ```````````````````````````````` example
  5217. foo *\**
  5218. .
  5219. <p>foo <em>*</em></p>
  5220. ````````````````````````````````
  5221. ```````````````````````````````` example
  5222. foo *_*
  5223. .
  5224. <p>foo <em>_</em></p>
  5225. ````````````````````````````````
  5226. ```````````````````````````````` example
  5227. foo *****
  5228. .
  5229. <p>foo *****</p>
  5230. ````````````````````````````````
  5231. ```````````````````````````````` example
  5232. foo **\***
  5233. .
  5234. <p>foo <strong>*</strong></p>
  5235. ````````````````````````````````
  5236. ```````````````````````````````` example
  5237. foo **_**
  5238. .
  5239. <p>foo <strong>_</strong></p>
  5240. ````````````````````````````````
  5241. Note that when delimiters do not match evenly, Rule 11 determines
  5242. that the excess literal `*` characters will appear outside of the
  5243. emphasis, rather than inside it:
  5244. ```````````````````````````````` example
  5245. **foo*
  5246. .
  5247. <p>*<em>foo</em></p>
  5248. ````````````````````````````````
  5249. ```````````````````````````````` example
  5250. *foo**
  5251. .
  5252. <p><em>foo</em>*</p>
  5253. ````````````````````````````````
  5254. ```````````````````````````````` example
  5255. ***foo**
  5256. .
  5257. <p>*<strong>foo</strong></p>
  5258. ````````````````````````````````
  5259. ```````````````````````````````` example
  5260. ****foo*
  5261. .
  5262. <p>***<em>foo</em></p>
  5263. ````````````````````````````````
  5264. ```````````````````````````````` example
  5265. **foo***
  5266. .
  5267. <p><strong>foo</strong>*</p>
  5268. ````````````````````````````````
  5269. ```````````````````````````````` example
  5270. *foo****
  5271. .
  5272. <p><em>foo</em>***</p>
  5273. ````````````````````````````````
  5274. Rule 12:
  5275. ```````````````````````````````` example
  5276. foo ___
  5277. .
  5278. <p>foo ___</p>
  5279. ````````````````````````````````
  5280. ```````````````````````````````` example
  5281. foo _\__
  5282. .
  5283. <p>foo <em>_</em></p>
  5284. ````````````````````````````````
  5285. ```````````````````````````````` example
  5286. foo _*_
  5287. .
  5288. <p>foo <em>*</em></p>
  5289. ````````````````````````````````
  5290. ```````````````````````````````` example
  5291. foo _____
  5292. .
  5293. <p>foo _____</p>
  5294. ````````````````````````````````
  5295. ```````````````````````````````` example
  5296. foo __\___
  5297. .
  5298. <p>foo <strong>_</strong></p>
  5299. ````````````````````````````````
  5300. ```````````````````````````````` example
  5301. foo __*__
  5302. .
  5303. <p>foo <strong>*</strong></p>
  5304. ````````````````````````````````
  5305. ```````````````````````````````` example
  5306. __foo_
  5307. .
  5308. <p>_<em>foo</em></p>
  5309. ````````````````````````````````
  5310. Note that when delimiters do not match evenly, Rule 12 determines
  5311. that the excess literal `_` characters will appear outside of the
  5312. emphasis, rather than inside it:
  5313. ```````````````````````````````` example
  5314. _foo__
  5315. .
  5316. <p><em>foo</em>_</p>
  5317. ````````````````````````````````
  5318. ```````````````````````````````` example
  5319. ___foo__
  5320. .
  5321. <p>_<strong>foo</strong></p>
  5322. ````````````````````````````````
  5323. ```````````````````````````````` example
  5324. ____foo_
  5325. .
  5326. <p>___<em>foo</em></p>
  5327. ````````````````````````````````
  5328. ```````````````````````````````` example
  5329. __foo___
  5330. .
  5331. <p><strong>foo</strong>_</p>
  5332. ````````````````````````````````
  5333. ```````````````````````````````` example
  5334. _foo____
  5335. .
  5336. <p><em>foo</em>___</p>
  5337. ````````````````````````````````
  5338. Rule 13 implies that if you want emphasis nested directly inside
  5339. emphasis, you must use different delimiters:
  5340. ```````````````````````````````` example
  5341. **foo**
  5342. .
  5343. <p><strong>foo</strong></p>
  5344. ````````````````````````````````
  5345. ```````````````````````````````` example
  5346. *_foo_*
  5347. .
  5348. <p><em><em>foo</em></em></p>
  5349. ````````````````````````````````
  5350. ```````````````````````````````` example
  5351. __foo__
  5352. .
  5353. <p><strong>foo</strong></p>
  5354. ````````````````````````````````
  5355. ```````````````````````````````` example
  5356. _*foo*_
  5357. .
  5358. <p><em><em>foo</em></em></p>
  5359. ````````````````````````````````
  5360. However, strong emphasis within strong emphasis is possible without
  5361. switching delimiters:
  5362. ```````````````````````````````` example
  5363. ****foo****
  5364. .
  5365. <p><strong><strong>foo</strong></strong></p>
  5366. ````````````````````````````````
  5367. ```````````````````````````````` example
  5368. ____foo____
  5369. .
  5370. <p><strong><strong>foo</strong></strong></p>
  5371. ````````````````````````````````
  5372. Rule 13 can be applied to arbitrarily long sequences of
  5373. delimiters:
  5374. ```````````````````````````````` example
  5375. ******foo******
  5376. .
  5377. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5378. ````````````````````````````````
  5379. Rule 14:
  5380. ```````````````````````````````` example
  5381. ***foo***
  5382. .
  5383. <p><em><strong>foo</strong></em></p>
  5384. ````````````````````````````````
  5385. ```````````````````````````````` example
  5386. _____foo_____
  5387. .
  5388. <p><em><strong><strong>foo</strong></strong></em></p>
  5389. ````````````````````````````````
  5390. Rule 15:
  5391. ```````````````````````````````` example
  5392. *foo _bar* baz_
  5393. .
  5394. <p><em>foo _bar</em> baz_</p>
  5395. ````````````````````````````````
  5396. ```````````````````````````````` example
  5397. *foo __bar *baz bim__ bam*
  5398. .
  5399. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5400. ````````````````````````````````
  5401. Rule 16:
  5402. ```````````````````````````````` example
  5403. **foo **bar baz**
  5404. .
  5405. <p>**foo <strong>bar baz</strong></p>
  5406. ````````````````````````````````
  5407. ```````````````````````````````` example
  5408. *foo *bar baz*
  5409. .
  5410. <p>*foo <em>bar baz</em></p>
  5411. ````````````````````````````````
  5412. Rule 17:
  5413. ```````````````````````````````` example
  5414. *[bar*](/url)
  5415. .
  5416. <p>*<a href="/url">bar*</a></p>
  5417. ````````````````````````````````
  5418. ```````````````````````````````` example
  5419. _foo [bar_](/url)
  5420. .
  5421. <p>_foo <a href="/url">bar_</a></p>
  5422. ````````````````````````````````
  5423. ```````````````````````````````` example
  5424. *<img src="foo" title="*"/>
  5425. .
  5426. <p>*<img src="foo" title="*"/></p>
  5427. ````````````````````````````````
  5428. ```````````````````````````````` example
  5429. **<a href="**">
  5430. .
  5431. <p>**<a href="**"></p>
  5432. ````````````````````````````````
  5433. ```````````````````````````````` example
  5434. __<a href="__">
  5435. .
  5436. <p>__<a href="__"></p>
  5437. ````````````````````````````````
  5438. ```````````````````````````````` example
  5439. *a `*`*
  5440. .
  5441. <p><em>a <code>*</code></em></p>
  5442. ````````````````````````````````
  5443. ```````````````````````````````` example
  5444. _a `_`_
  5445. .
  5446. <p><em>a <code>_</code></em></p>
  5447. ````````````````````````````````
  5448. ```````````````````````````````` example
  5449. **a<http://foo.bar/?q=**>
  5450. .
  5451. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5452. ````````````````````````````````
  5453. ```````````````````````````````` example
  5454. __a<http://foo.bar/?q=__>
  5455. .
  5456. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5457. ````````````````````````````````
  5458. ## Links
  5459. A link contains [link text] (the visible text), a [link destination]
  5460. (the URI that is the link destination), and optionally a [link title].
  5461. There are two basic kinds of links in Markdown. In [inline links] the
  5462. destination and title are given immediately after the link text. In
  5463. [reference links] the destination and title are defined elsewhere in
  5464. the document.
  5465. A [link text](@) consists of a sequence of zero or more
  5466. inline elements enclosed by square brackets (`[` and `]`). The
  5467. following rules apply:
  5468. - Links may not contain other links, at any level of nesting. If
  5469. multiple otherwise valid link definitions appear nested inside each
  5470. other, the inner-most definition is used.
  5471. - Brackets are allowed in the [link text] only if (a) they
  5472. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5473. with an open bracket `[`, a sequence of zero or more inlines, and
  5474. a close bracket `]`.
  5475. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5476. than the brackets in link text. Thus, for example,
  5477. `` [foo`]` `` could not be a link text, since the second `]`
  5478. is part of a code span.
  5479. - The brackets in link text bind more tightly than markers for
  5480. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5481. A [link destination](@) consists of either
  5482. - a sequence of zero or more characters between an opening `<` and a
  5483. closing `>` that contains no spaces, line breaks, or unescaped
  5484. `<` or `>` characters, or
  5485. - a nonempty sequence of characters that does not include
  5486. ASCII space or control characters, and includes parentheses
  5487. only if (a) they are backslash-escaped or (b) they are part of
  5488. a balanced pair of unescaped parentheses.
  5489. A [link title](@) consists of either
  5490. - a sequence of zero or more characters between straight double-quote
  5491. characters (`"`), including a `"` character only if it is
  5492. backslash-escaped, or
  5493. - a sequence of zero or more characters between straight single-quote
  5494. characters (`'`), including a `'` character only if it is
  5495. backslash-escaped, or
  5496. - a sequence of zero or more characters between matching parentheses
  5497. (`(...)`), including a `)` character only if it is backslash-escaped.
  5498. Although [link titles] may span multiple lines, they may not contain
  5499. a [blank line].
  5500. An [inline link](@) consists of a [link text] followed immediately
  5501. by a left parenthesis `(`, optional [whitespace], an optional
  5502. [link destination], an optional [link title] separated from the link
  5503. destination by [whitespace], optional [whitespace], and a right
  5504. parenthesis `)`. The link's text consists of the inlines contained
  5505. in the [link text] (excluding the enclosing square brackets).
  5506. The link's URI consists of the link destination, excluding enclosing
  5507. `<...>` if present, with backslash-escapes in effect as described
  5508. above. The link's title consists of the link title, excluding its
  5509. enclosing delimiters, with backslash-escapes in effect as described
  5510. above.
  5511. Here is a simple inline link:
  5512. ```````````````````````````````` example
  5513. [link](/uri "title")
  5514. .
  5515. <p><a href="/uri" title="title">link</a></p>
  5516. ````````````````````````````````
  5517. The title may be omitted:
  5518. ```````````````````````````````` example
  5519. [link](/uri)
  5520. .
  5521. <p><a href="/uri">link</a></p>
  5522. ````````````````````````````````
  5523. Both the title and the destination may be omitted:
  5524. ```````````````````````````````` example
  5525. [link]()
  5526. .
  5527. <p><a href="">link</a></p>
  5528. ````````````````````````````````
  5529. ```````````````````````````````` example
  5530. [link](<>)
  5531. .
  5532. <p><a href="">link</a></p>
  5533. ````````````````````````````````
  5534. The destination cannot contain spaces or line breaks,
  5535. even if enclosed in pointy brackets:
  5536. ```````````````````````````````` example
  5537. [link](/my uri)
  5538. .
  5539. <p>[link](/my uri)</p>
  5540. ````````````````````````````````
  5541. ```````````````````````````````` example
  5542. [link](</my uri>)
  5543. .
  5544. <p>[link](&lt;/my uri&gt;)</p>
  5545. ````````````````````````````````
  5546. ```````````````````````````````` example
  5547. [link](foo
  5548. bar)
  5549. .
  5550. <p>[link](foo
  5551. bar)</p>
  5552. ````````````````````````````````
  5553. ```````````````````````````````` example
  5554. [link](<foo
  5555. bar>)
  5556. .
  5557. <p>[link](<foo
  5558. bar>)</p>
  5559. ````````````````````````````````
  5560. Parentheses inside the link destination may be escaped:
  5561. ```````````````````````````````` example
  5562. [link](\(foo\))
  5563. .
  5564. <p><a href="(foo)">link</a></p>
  5565. ````````````````````````````````
  5566. Any number parentheses are allowed without escaping, as long as they are
  5567. balanced:
  5568. ```````````````````````````````` example
  5569. [link](foo(and(bar)))
  5570. .
  5571. <p><a href="foo(and(bar))">link</a></p>
  5572. ````````````````````````````````
  5573. However, if you have unbalanced parentheses, you need to escape or use the
  5574. `<...>` form:
  5575. ```````````````````````````````` example
  5576. [link](foo\(and\(bar\))
  5577. .
  5578. <p><a href="foo(and(bar)">link</a></p>
  5579. ````````````````````````````````
  5580. ```````````````````````````````` example
  5581. [link](<foo(and(bar)>)
  5582. .
  5583. <p><a href="foo(and(bar)">link</a></p>
  5584. ````````````````````````````````
  5585. Parentheses and other symbols can also be escaped, as usual
  5586. in Markdown:
  5587. ```````````````````````````````` example
  5588. [link](foo\)\:)
  5589. .
  5590. <p><a href="foo):">link</a></p>
  5591. ````````````````````````````````
  5592. A link can contain fragment identifiers and queries:
  5593. ```````````````````````````````` example
  5594. [link](#fragment)
  5595. [link](http://example.com#fragment)
  5596. [link](http://example.com?foo=3#frag)
  5597. .
  5598. <p><a href="#fragment">link</a></p>
  5599. <p><a href="http://example.com#fragment">link</a></p>
  5600. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5601. ````````````````````````````````
  5602. Note that a backslash before a non-escapable character is
  5603. just a backslash:
  5604. ```````````````````````````````` example
  5605. [link](foo\bar)
  5606. .
  5607. <p><a href="foo%5Cbar">link</a></p>
  5608. ````````````````````````````````
  5609. URL-escaping should be left alone inside the destination, as all
  5610. URL-escaped characters are also valid URL characters. Entity and
  5611. numerical character references in the destination will be parsed
  5612. into the corresponding Unicode code points, as usual. These may
  5613. be optionally URL-escaped when written as HTML, but this spec
  5614. does not enforce any particular policy for rendering URLs in
  5615. HTML or other formats. Renderers may make different decisions
  5616. about how to escape or normalize URLs in the output.
  5617. ```````````````````````````````` example
  5618. [link](foo%20b&auml;)
  5619. .
  5620. <p><a href="foo%20b%C3%A4">link</a></p>
  5621. ````````````````````````````````
  5622. Note that, because titles can often be parsed as destinations,
  5623. if you try to omit the destination and keep the title, you'll
  5624. get unexpected results:
  5625. ```````````````````````````````` example
  5626. [link]("title")
  5627. .
  5628. <p><a href="%22title%22">link</a></p>
  5629. ````````````````````````````````
  5630. Titles may be in single quotes, double quotes, or parentheses:
  5631. ```````````````````````````````` example
  5632. [link](/url "title")
  5633. [link](/url 'title')
  5634. [link](/url (title))
  5635. .
  5636. <p><a href="/url" title="title">link</a>
  5637. <a href="/url" title="title">link</a>
  5638. <a href="/url" title="title">link</a></p>
  5639. ````````````````````````````````
  5640. Backslash escapes and entity and numeric character references
  5641. may be used in titles:
  5642. ```````````````````````````````` example
  5643. [link](/url "title \"&quot;")
  5644. .
  5645. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5646. ````````````````````````````````
  5647. Titles must be separated from the link using a [whitespace].
  5648. Other [Unicode whitespace] like non-breaking space doesn't work.
  5649. ```````````````````````````````` example
  5650. [link](/url "title")
  5651. .
  5652. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5653. ````````````````````````````````
  5654. Nested balanced quotes are not allowed without escaping:
  5655. ```````````````````````````````` example
  5656. [link](/url "title "and" title")
  5657. .
  5658. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5659. ````````````````````````````````
  5660. But it is easy to work around this by using a different quote type:
  5661. ```````````````````````````````` example
  5662. [link](/url 'title "and" title')
  5663. .
  5664. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5665. ````````````````````````````````
  5666. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5667. title, and its test suite included a test demonstrating this.
  5668. But it is hard to see a good rationale for the extra complexity this
  5669. brings, since there are already many ways---backslash escaping,
  5670. entity and numeric character references, or using a different
  5671. quote type for the enclosing title---to write titles containing
  5672. double quotes. `Markdown.pl`'s handling of titles has a number
  5673. of other strange features. For example, it allows single-quoted
  5674. titles in inline links, but not reference links. And, in
  5675. reference links but not inline links, it allows a title to begin
  5676. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5677. titles with no closing quotation mark, though 1.0.2b8 does not.
  5678. It seems preferable to adopt a simple, rational rule that works
  5679. the same way in inline links and link reference definitions.)
  5680. [Whitespace] is allowed around the destination and title:
  5681. ```````````````````````````````` example
  5682. [link]( /uri
  5683. "title" )
  5684. .
  5685. <p><a href="/uri" title="title">link</a></p>
  5686. ````````````````````````````````
  5687. But it is not allowed between the link text and the
  5688. following parenthesis:
  5689. ```````````````````````````````` example
  5690. [link] (/uri)
  5691. .
  5692. <p>[link] (/uri)</p>
  5693. ````````````````````````````````
  5694. The link text may contain balanced brackets, but not unbalanced ones,
  5695. unless they are escaped:
  5696. ```````````````````````````````` example
  5697. [link [foo [bar]]](/uri)
  5698. .
  5699. <p><a href="/uri">link [foo [bar]]</a></p>
  5700. ````````````````````````````````
  5701. ```````````````````````````````` example
  5702. [link] bar](/uri)
  5703. .
  5704. <p>[link] bar](/uri)</p>
  5705. ````````````````````````````````
  5706. ```````````````````````````````` example
  5707. [link [bar](/uri)
  5708. .
  5709. <p>[link <a href="/uri">bar</a></p>
  5710. ````````````````````````````````
  5711. ```````````````````````````````` example
  5712. [link \[bar](/uri)
  5713. .
  5714. <p><a href="/uri">link [bar</a></p>
  5715. ````````````````````````````````
  5716. The link text may contain inline content:
  5717. ```````````````````````````````` example
  5718. [link *foo **bar** `#`*](/uri)
  5719. .
  5720. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5721. ````````````````````````````````
  5722. ```````````````````````````````` example
  5723. [![moon](moon.jpg)](/uri)
  5724. .
  5725. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5726. ````````````````````````````````
  5727. However, links may not contain other links, at any level of nesting.
  5728. ```````````````````````````````` example
  5729. [foo [bar](/uri)](/uri)
  5730. .
  5731. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5732. ````````````````````````````````
  5733. ```````````````````````````````` example
  5734. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5735. .
  5736. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5737. ````````````````````````````````
  5738. ```````````````````````````````` example
  5739. ![[[foo](uri1)](uri2)](uri3)
  5740. .
  5741. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5742. ````````````````````````````````
  5743. These cases illustrate the precedence of link text grouping over
  5744. emphasis grouping:
  5745. ```````````````````````````````` example
  5746. *[foo*](/uri)
  5747. .
  5748. <p>*<a href="/uri">foo*</a></p>
  5749. ````````````````````````````````
  5750. ```````````````````````````````` example
  5751. [foo *bar](baz*)
  5752. .
  5753. <p><a href="baz*">foo *bar</a></p>
  5754. ````````````````````````````````
  5755. Note that brackets that *aren't* part of links do not take
  5756. precedence:
  5757. ```````````````````````````````` example
  5758. *foo [bar* baz]
  5759. .
  5760. <p><em>foo [bar</em> baz]</p>
  5761. ````````````````````````````````
  5762. These cases illustrate the precedence of HTML tags, code spans,
  5763. and autolinks over link grouping:
  5764. ```````````````````````````````` example
  5765. [foo <bar attr="](baz)">
  5766. .
  5767. <p>[foo <bar attr="](baz)"></p>
  5768. ````````````````````````````````
  5769. ```````````````````````````````` example
  5770. [foo`](/uri)`
  5771. .
  5772. <p>[foo<code>](/uri)</code></p>
  5773. ````````````````````````````````
  5774. ```````````````````````````````` example
  5775. [foo<http://example.com/?search=](uri)>
  5776. .
  5777. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5778. ````````````````````````````````
  5779. There are three kinds of [reference link](@)s:
  5780. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5781. and [shortcut](#shortcut-reference-link).
  5782. A [full reference link](@)
  5783. consists of a [link text] immediately followed by a [link label]
  5784. that [matches] a [link reference definition] elsewhere in the document.
  5785. A [link label](@) begins with a left bracket (`[`) and ends
  5786. with the first right bracket (`]`) that is not backslash-escaped.
  5787. Between these brackets there must be at least one [non-whitespace character].
  5788. Unescaped square bracket characters are not allowed in
  5789. [link labels]. A link label can have at most 999
  5790. characters inside the square brackets.
  5791. One label [matches](@)
  5792. another just in case their normalized forms are equal. To normalize a
  5793. label, perform the *Unicode case fold*, strip leading and trailing
  5794. [whitespace] and collapse consecutive internal
  5795. [whitespace] to a single space. If there are multiple
  5796. matching reference link definitions, the one that comes first in the
  5797. document is used. (It is desirable in such cases to emit a warning.)
  5798. The contents of the first link label are parsed as inlines, which are
  5799. used as the link's text. The link's URI and title are provided by the
  5800. matching [link reference definition].
  5801. Here is a simple example:
  5802. ```````````````````````````````` example
  5803. [foo][bar]
  5804. [bar]: /url "title"
  5805. .
  5806. <p><a href="/url" title="title">foo</a></p>
  5807. ````````````````````````````````
  5808. The rules for the [link text] are the same as with
  5809. [inline links]. Thus:
  5810. The link text may contain balanced brackets, but not unbalanced ones,
  5811. unless they are escaped:
  5812. ```````````````````````````````` example
  5813. [link [foo [bar]]][ref]
  5814. [ref]: /uri
  5815. .
  5816. <p><a href="/uri">link [foo [bar]]</a></p>
  5817. ````````````````````````````````
  5818. ```````````````````````````````` example
  5819. [link \[bar][ref]
  5820. [ref]: /uri
  5821. .
  5822. <p><a href="/uri">link [bar</a></p>
  5823. ````````````````````````````````
  5824. The link text may contain inline content:
  5825. ```````````````````````````````` example
  5826. [link *foo **bar** `#`*][ref]
  5827. [ref]: /uri
  5828. .
  5829. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5830. ````````````````````````````````
  5831. ```````````````````````````````` example
  5832. [![moon](moon.jpg)][ref]
  5833. [ref]: /uri
  5834. .
  5835. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5836. ````````````````````````````````
  5837. However, links may not contain other links, at any level of nesting.
  5838. ```````````````````````````````` example
  5839. [foo [bar](/uri)][ref]
  5840. [ref]: /uri
  5841. .
  5842. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5843. ````````````````````````````````
  5844. ```````````````````````````````` example
  5845. [foo *bar [baz][ref]*][ref]
  5846. [ref]: /uri
  5847. .
  5848. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5849. ````````````````````````````````
  5850. (In the examples above, we have two [shortcut reference links]
  5851. instead of one [full reference link].)
  5852. The following cases illustrate the precedence of link text grouping over
  5853. emphasis grouping:
  5854. ```````````````````````````````` example
  5855. *[foo*][ref]
  5856. [ref]: /uri
  5857. .
  5858. <p>*<a href="/uri">foo*</a></p>
  5859. ````````````````````````````````
  5860. ```````````````````````````````` example
  5861. [foo *bar][ref]
  5862. [ref]: /uri
  5863. .
  5864. <p><a href="/uri">foo *bar</a></p>
  5865. ````````````````````````````````
  5866. These cases illustrate the precedence of HTML tags, code spans,
  5867. and autolinks over link grouping:
  5868. ```````````````````````````````` example
  5869. [foo <bar attr="][ref]">
  5870. [ref]: /uri
  5871. .
  5872. <p>[foo <bar attr="][ref]"></p>
  5873. ````````````````````````````````
  5874. ```````````````````````````````` example
  5875. [foo`][ref]`
  5876. [ref]: /uri
  5877. .
  5878. <p>[foo<code>][ref]</code></p>
  5879. ````````````````````````````````
  5880. ```````````````````````````````` example
  5881. [foo<http://example.com/?search=][ref]>
  5882. [ref]: /uri
  5883. .
  5884. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5885. ````````````````````````````````
  5886. Matching is case-insensitive:
  5887. ```````````````````````````````` example
  5888. [foo][BaR]
  5889. [bar]: /url "title"
  5890. .
  5891. <p><a href="/url" title="title">foo</a></p>
  5892. ````````````````````````````````
  5893. Unicode case fold is used:
  5894. ```````````````````````````````` example
  5895. [Толпой][Толпой] is a Russian word.
  5896. [ТОЛПОЙ]: /url
  5897. .
  5898. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5899. ````````````````````````````````
  5900. Consecutive internal [whitespace] is treated as one space for
  5901. purposes of determining matching:
  5902. ```````````````````````````````` example
  5903. [Foo
  5904. bar]: /url
  5905. [Baz][Foo bar]
  5906. .
  5907. <p><a href="/url">Baz</a></p>
  5908. ````````````````````````````````
  5909. No [whitespace] is allowed between the [link text] and the
  5910. [link label]:
  5911. ```````````````````````````````` example
  5912. [foo] [bar]
  5913. [bar]: /url "title"
  5914. .
  5915. <p>[foo] <a href="/url" title="title">bar</a></p>
  5916. ````````````````````````````````
  5917. ```````````````````````````````` example
  5918. [foo]
  5919. [bar]
  5920. [bar]: /url "title"
  5921. .
  5922. <p>[foo]
  5923. <a href="/url" title="title">bar</a></p>
  5924. ````````````````````````````````
  5925. This is a departure from John Gruber's original Markdown syntax
  5926. description, which explicitly allows whitespace between the link
  5927. text and the link label. It brings reference links in line with
  5928. [inline links], which (according to both original Markdown and
  5929. this spec) cannot have whitespace after the link text. More
  5930. importantly, it prevents inadvertent capture of consecutive
  5931. [shortcut reference links]. If whitespace is allowed between the
  5932. link text and the link label, then in the following we will have
  5933. a single reference link, not two shortcut reference links, as
  5934. intended:
  5935. ``` markdown
  5936. [foo]
  5937. [bar]
  5938. [foo]: /url1
  5939. [bar]: /url2
  5940. ```
  5941. (Note that [shortcut reference links] were introduced by Gruber
  5942. himself in a beta version of `Markdown.pl`, but never included
  5943. in the official syntax description. Without shortcut reference
  5944. links, it is harmless to allow space between the link text and
  5945. link label; but once shortcut references are introduced, it is
  5946. too dangerous to allow this, as it frequently leads to
  5947. unintended results.)
  5948. When there are multiple matching [link reference definitions],
  5949. the first is used:
  5950. ```````````````````````````````` example
  5951. [foo]: /url1
  5952. [foo]: /url2
  5953. [bar][foo]
  5954. .
  5955. <p><a href="/url1">bar</a></p>
  5956. ````````````````````````````````
  5957. Note that matching is performed on normalized strings, not parsed
  5958. inline content. So the following does not match, even though the
  5959. labels define equivalent inline content:
  5960. ```````````````````````````````` example
  5961. [bar][foo\!]
  5962. [foo!]: /url
  5963. .
  5964. <p>[bar][foo!]</p>
  5965. ````````````````````````````````
  5966. [Link labels] cannot contain brackets, unless they are
  5967. backslash-escaped:
  5968. ```````````````````````````````` example
  5969. [foo][ref[]
  5970. [ref[]: /uri
  5971. .
  5972. <p>[foo][ref[]</p>
  5973. <p>[ref[]: /uri</p>
  5974. ````````````````````````````````
  5975. ```````````````````````````````` example
  5976. [foo][ref[bar]]
  5977. [ref[bar]]: /uri
  5978. .
  5979. <p>[foo][ref[bar]]</p>
  5980. <p>[ref[bar]]: /uri</p>
  5981. ````````````````````````````````
  5982. ```````````````````````````````` example
  5983. [[[foo]]]
  5984. [[[foo]]]: /url
  5985. .
  5986. <p>[[[foo]]]</p>
  5987. <p>[[[foo]]]: /url</p>
  5988. ````````````````````````````````
  5989. ```````````````````````````````` example
  5990. [foo][ref\[]
  5991. [ref\[]: /uri
  5992. .
  5993. <p><a href="/uri">foo</a></p>
  5994. ````````````````````````````````
  5995. Note that in this example `]` is not backslash-escaped:
  5996. ```````````````````````````````` example
  5997. [bar\\]: /uri
  5998. [bar\\]
  5999. .
  6000. <p><a href="/uri">bar\</a></p>
  6001. ````````````````````````````````
  6002. A [link label] must contain at least one [non-whitespace character]:
  6003. ```````````````````````````````` example
  6004. []
  6005. []: /uri
  6006. .
  6007. <p>[]</p>
  6008. <p>[]: /uri</p>
  6009. ````````````````````````````````
  6010. ```````````````````````````````` example
  6011. [
  6012. ]
  6013. [
  6014. ]: /uri
  6015. .
  6016. <p>[
  6017. ]</p>
  6018. <p>[
  6019. ]: /uri</p>
  6020. ````````````````````````````````
  6021. A [collapsed reference link](@)
  6022. consists of a [link label] that [matches] a
  6023. [link reference definition] elsewhere in the
  6024. document, followed by the string `[]`.
  6025. The contents of the first link label are parsed as inlines,
  6026. which are used as the link's text. The link's URI and title are
  6027. provided by the matching reference link definition. Thus,
  6028. `[foo][]` is equivalent to `[foo][foo]`.
  6029. ```````````````````````````````` example
  6030. [foo][]
  6031. [foo]: /url "title"
  6032. .
  6033. <p><a href="/url" title="title">foo</a></p>
  6034. ````````````````````````````````
  6035. ```````````````````````````````` example
  6036. [*foo* bar][]
  6037. [*foo* bar]: /url "title"
  6038. .
  6039. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6040. ````````````````````````````````
  6041. The link labels are case-insensitive:
  6042. ```````````````````````````````` example
  6043. [Foo][]
  6044. [foo]: /url "title"
  6045. .
  6046. <p><a href="/url" title="title">Foo</a></p>
  6047. ````````````````````````````````
  6048. As with full reference links, [whitespace] is not
  6049. allowed between the two sets of brackets:
  6050. ```````````````````````````````` example
  6051. [foo]
  6052. []
  6053. [foo]: /url "title"
  6054. .
  6055. <p><a href="/url" title="title">foo</a>
  6056. []</p>
  6057. ````````````````````````````````
  6058. A [shortcut reference link](@)
  6059. consists of a [link label] that [matches] a
  6060. [link reference definition] elsewhere in the
  6061. document and is not followed by `[]` or a link label.
  6062. The contents of the first link label are parsed as inlines,
  6063. which are used as the link's text. The link's URI and title
  6064. are provided by the matching link reference definition.
  6065. Thus, `[foo]` is equivalent to `[foo][]`.
  6066. ```````````````````````````````` example
  6067. [foo]
  6068. [foo]: /url "title"
  6069. .
  6070. <p><a href="/url" title="title">foo</a></p>
  6071. ````````````````````````````````
  6072. ```````````````````````````````` example
  6073. [*foo* bar]
  6074. [*foo* bar]: /url "title"
  6075. .
  6076. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6077. ````````````````````````````````
  6078. ```````````````````````````````` example
  6079. [[*foo* bar]]
  6080. [*foo* bar]: /url "title"
  6081. .
  6082. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6083. ````````````````````````````````
  6084. ```````````````````````````````` example
  6085. [[bar [foo]
  6086. [foo]: /url
  6087. .
  6088. <p>[[bar <a href="/url">foo</a></p>
  6089. ````````````````````````````````
  6090. The link labels are case-insensitive:
  6091. ```````````````````````````````` example
  6092. [Foo]
  6093. [foo]: /url "title"
  6094. .
  6095. <p><a href="/url" title="title">Foo</a></p>
  6096. ````````````````````````````````
  6097. A space after the link text should be preserved:
  6098. ```````````````````````````````` example
  6099. [foo] bar
  6100. [foo]: /url
  6101. .
  6102. <p><a href="/url">foo</a> bar</p>
  6103. ````````````````````````````````
  6104. If you just want bracketed text, you can backslash-escape the
  6105. opening bracket to avoid links:
  6106. ```````````````````````````````` example
  6107. \[foo]
  6108. [foo]: /url "title"
  6109. .
  6110. <p>[foo]</p>
  6111. ````````````````````````````````
  6112. Note that this is a link, because a link label ends with the first
  6113. following closing bracket:
  6114. ```````````````````````````````` example
  6115. [foo*]: /url
  6116. *[foo*]
  6117. .
  6118. <p>*<a href="/url">foo*</a></p>
  6119. ````````````````````````````````
  6120. Full and compact references take precedence over shortcut
  6121. references:
  6122. ```````````````````````````````` example
  6123. [foo][bar]
  6124. [foo]: /url1
  6125. [bar]: /url2
  6126. .
  6127. <p><a href="/url2">foo</a></p>
  6128. ````````````````````````````````
  6129. ```````````````````````````````` example
  6130. [foo][]
  6131. [foo]: /url1
  6132. .
  6133. <p><a href="/url1">foo</a></p>
  6134. ````````````````````````````````
  6135. Inline links also take precedence:
  6136. ```````````````````````````````` example
  6137. [foo]()
  6138. [foo]: /url1
  6139. .
  6140. <p><a href="">foo</a></p>
  6141. ````````````````````````````````
  6142. ```````````````````````````````` example
  6143. [foo](not a link)
  6144. [foo]: /url1
  6145. .
  6146. <p><a href="/url1">foo</a>(not a link)</p>
  6147. ````````````````````````````````
  6148. In the following case `[bar][baz]` is parsed as a reference,
  6149. `[foo]` as normal text:
  6150. ```````````````````````````````` example
  6151. [foo][bar][baz]
  6152. [baz]: /url
  6153. .
  6154. <p>[foo]<a href="/url">bar</a></p>
  6155. ````````````````````````````````
  6156. Here, though, `[foo][bar]` is parsed as a reference, since
  6157. `[bar]` is defined:
  6158. ```````````````````````````````` example
  6159. [foo][bar][baz]
  6160. [baz]: /url1
  6161. [bar]: /url2
  6162. .
  6163. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6164. ````````````````````````````````
  6165. Here `[foo]` is not parsed as a shortcut reference, because it
  6166. is followed by a link label (even though `[bar]` is not defined):
  6167. ```````````````````````````````` example
  6168. [foo][bar][baz]
  6169. [baz]: /url1
  6170. [foo]: /url2
  6171. .
  6172. <p>[foo]<a href="/url1">bar</a></p>
  6173. ````````````````````````````````
  6174. ## Images
  6175. Syntax for images is like the syntax for links, with one
  6176. difference. Instead of [link text], we have an
  6177. [image description](@). The rules for this are the
  6178. same as for [link text], except that (a) an
  6179. image description starts with `![` rather than `[`, and
  6180. (b) an image description may contain links.
  6181. An image description has inline elements
  6182. as its contents. When an image is rendered to HTML,
  6183. this is standardly used as the image's `alt` attribute.
  6184. ```````````````````````````````` example
  6185. ![foo](/url "title")
  6186. .
  6187. <p><img src="/url" alt="foo" title="title" /></p>
  6188. ````````````````````````````````
  6189. ```````````````````````````````` example
  6190. ![foo *bar*]
  6191. [foo *bar*]: train.jpg "train & tracks"
  6192. .
  6193. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6194. ````````````````````````````````
  6195. ```````````````````````````````` example
  6196. ![foo ![bar](/url)](/url2)
  6197. .
  6198. <p><img src="/url2" alt="foo bar" /></p>
  6199. ````````````````````````````````
  6200. ```````````````````````````````` example
  6201. ![foo [bar](/url)](/url2)
  6202. .
  6203. <p><img src="/url2" alt="foo bar" /></p>
  6204. ````````````````````````````````
  6205. Though this spec is concerned with parsing, not rendering, it is
  6206. recommended that in rendering to HTML, only the plain string content
  6207. of the [image description] be used. Note that in
  6208. the above example, the alt attribute's value is `foo bar`, not `foo
  6209. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6210. content is rendered, without formatting.
  6211. ```````````````````````````````` example
  6212. ![foo *bar*][]
  6213. [foo *bar*]: train.jpg "train & tracks"
  6214. .
  6215. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6216. ````````````````````````````````
  6217. ```````````````````````````````` example
  6218. ![foo *bar*][foobar]
  6219. [FOOBAR]: train.jpg "train & tracks"
  6220. .
  6221. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6222. ````````````````````````````````
  6223. ```````````````````````````````` example
  6224. ![foo](train.jpg)
  6225. .
  6226. <p><img src="train.jpg" alt="foo" /></p>
  6227. ````````````````````````````````
  6228. ```````````````````````````````` example
  6229. My ![foo bar](/path/to/train.jpg "title" )
  6230. .
  6231. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6232. ````````````````````````````````
  6233. ```````````````````````````````` example
  6234. ![foo](<url>)
  6235. .
  6236. <p><img src="url" alt="foo" /></p>
  6237. ````````````````````````````````
  6238. ```````````````````````````````` example
  6239. ![](/url)
  6240. .
  6241. <p><img src="/url" alt="" /></p>
  6242. ````````````````````````````````
  6243. Reference-style:
  6244. ```````````````````````````````` example
  6245. ![foo][bar]
  6246. [bar]: /url
  6247. .
  6248. <p><img src="/url" alt="foo" /></p>
  6249. ````````````````````````````````
  6250. ```````````````````````````````` example
  6251. ![foo][bar]
  6252. [BAR]: /url
  6253. .
  6254. <p><img src="/url" alt="foo" /></p>
  6255. ````````````````````````````````
  6256. Collapsed:
  6257. ```````````````````````````````` example
  6258. ![foo][]
  6259. [foo]: /url "title"
  6260. .
  6261. <p><img src="/url" alt="foo" title="title" /></p>
  6262. ````````````````````````````````
  6263. ```````````````````````````````` example
  6264. ![*foo* bar][]
  6265. [*foo* bar]: /url "title"
  6266. .
  6267. <p><img src="/url" alt="foo bar" title="title" /></p>
  6268. ````````````````````````````````
  6269. The labels are case-insensitive:
  6270. ```````````````````````````````` example
  6271. ![Foo][]
  6272. [foo]: /url "title"
  6273. .
  6274. <p><img src="/url" alt="Foo" title="title" /></p>
  6275. ````````````````````````````````
  6276. As with reference links, [whitespace] is not allowed
  6277. between the two sets of brackets:
  6278. ```````````````````````````````` example
  6279. ![foo]
  6280. []
  6281. [foo]: /url "title"
  6282. .
  6283. <p><img src="/url" alt="foo" title="title" />
  6284. []</p>
  6285. ````````````````````````````````
  6286. Shortcut:
  6287. ```````````````````````````````` example
  6288. ![foo]
  6289. [foo]: /url "title"
  6290. .
  6291. <p><img src="/url" alt="foo" title="title" /></p>
  6292. ````````````````````````````````
  6293. ```````````````````````````````` example
  6294. ![*foo* bar]
  6295. [*foo* bar]: /url "title"
  6296. .
  6297. <p><img src="/url" alt="foo bar" title="title" /></p>
  6298. ````````````````````````````````
  6299. Note that link labels cannot contain unescaped brackets:
  6300. ```````````````````````````````` example
  6301. ![[foo]]
  6302. [[foo]]: /url "title"
  6303. .
  6304. <p>![[foo]]</p>
  6305. <p>[[foo]]: /url &quot;title&quot;</p>
  6306. ````````````````````````````````
  6307. The link labels are case-insensitive:
  6308. ```````````````````````````````` example
  6309. ![Foo]
  6310. [foo]: /url "title"
  6311. .
  6312. <p><img src="/url" alt="Foo" title="title" /></p>
  6313. ````````````````````````````````
  6314. If you just want a literal `!` followed by bracketed text, you can
  6315. backslash-escape the opening `[`:
  6316. ```````````````````````````````` example
  6317. !\[foo]
  6318. [foo]: /url "title"
  6319. .
  6320. <p>![foo]</p>
  6321. ````````````````````````````````
  6322. If you want a link after a literal `!`, backslash-escape the
  6323. `!`:
  6324. ```````````````````````````````` example
  6325. \![foo]
  6326. [foo]: /url "title"
  6327. .
  6328. <p>!<a href="/url" title="title">foo</a></p>
  6329. ````````````````````````````````
  6330. ## Autolinks
  6331. [Autolink](@)s are absolute URIs and email addresses inside
  6332. `<` and `>`. They are parsed as links, with the URL or email address
  6333. as the link label.
  6334. A [URI autolink](@) consists of `<`, followed by an
  6335. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6336. a link to the URI, with the URI as the link's label.
  6337. An [absolute URI](@),
  6338. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6339. followed by zero or more characters other than ASCII
  6340. [whitespace] and control characters, `<`, and `>`. If
  6341. the URI includes these characters, they must be percent-encoded
  6342. (e.g. `%20` for a space).
  6343. For purposes of this spec, a [scheme](@) is any sequence
  6344. of 2--32 characters beginning with an ASCII letter and followed
  6345. by any combination of ASCII letters, digits, or the symbols plus
  6346. ("+"), period ("."), or hyphen ("-").
  6347. Here are some valid autolinks:
  6348. ```````````````````````````````` example
  6349. <http://foo.bar.baz>
  6350. .
  6351. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6352. ````````````````````````````````
  6353. ```````````````````````````````` example
  6354. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6355. .
  6356. <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>
  6357. ````````````````````````````````
  6358. ```````````````````````````````` example
  6359. <irc://foo.bar:2233/baz>
  6360. .
  6361. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6362. ````````````````````````````````
  6363. Uppercase is also fine:
  6364. ```````````````````````````````` example
  6365. <MAILTO:FOO@BAR.BAZ>
  6366. .
  6367. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6368. ````````````````````````````````
  6369. Note that many strings that count as [absolute URIs] for
  6370. purposes of this spec are not valid URIs, because their
  6371. schemes are not registered or because of other problems
  6372. with their syntax:
  6373. ```````````````````````````````` example
  6374. <a+b+c:d>
  6375. .
  6376. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6377. ````````````````````````````````
  6378. ```````````````````````````````` example
  6379. <made-up-scheme://foo,bar>
  6380. .
  6381. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6382. ````````````````````````````````
  6383. ```````````````````````````````` example
  6384. <http://../>
  6385. .
  6386. <p><a href="http://../">http://../</a></p>
  6387. ````````````````````````````````
  6388. ```````````````````````````````` example
  6389. <localhost:5001/foo>
  6390. .
  6391. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6392. ````````````````````````````````
  6393. Spaces are not allowed in autolinks:
  6394. ```````````````````````````````` example
  6395. <http://foo.bar/baz bim>
  6396. .
  6397. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6398. ````````````````````````````````
  6399. Backslash-escapes do not work inside autolinks:
  6400. ```````````````````````````````` example
  6401. <http://example.com/\[\>
  6402. .
  6403. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6404. ````````````````````````````````
  6405. An [email autolink](@)
  6406. consists of `<`, followed by an [email address],
  6407. followed by `>`. The link's label is the email address,
  6408. and the URL is `mailto:` followed by the email address.
  6409. An [email address](@),
  6410. for these purposes, is anything that matches
  6411. the [non-normative regex from the HTML5
  6412. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6413. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6414. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6415. Examples of email autolinks:
  6416. ```````````````````````````````` example
  6417. <foo@bar.example.com>
  6418. .
  6419. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6420. ````````````````````````````````
  6421. ```````````````````````````````` example
  6422. <foo+special@Bar.baz-bar0.com>
  6423. .
  6424. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6425. ````````````````````````````````
  6426. Backslash-escapes do not work inside email autolinks:
  6427. ```````````````````````````````` example
  6428. <foo\+@bar.example.com>
  6429. .
  6430. <p>&lt;foo+@bar.example.com&gt;</p>
  6431. ````````````````````````````````
  6432. These are not autolinks:
  6433. ```````````````````````````````` example
  6434. <>
  6435. .
  6436. <p>&lt;&gt;</p>
  6437. ````````````````````````````````
  6438. ```````````````````````````````` example
  6439. < http://foo.bar >
  6440. .
  6441. <p>&lt; http://foo.bar &gt;</p>
  6442. ````````````````````````````````
  6443. ```````````````````````````````` example
  6444. <m:abc>
  6445. .
  6446. <p>&lt;m:abc&gt;</p>
  6447. ````````````````````````````````
  6448. ```````````````````````````````` example
  6449. <foo.bar.baz>
  6450. .
  6451. <p>&lt;foo.bar.baz&gt;</p>
  6452. ````````````````````````````````
  6453. ```````````````````````````````` example
  6454. http://example.com
  6455. .
  6456. <p>http://example.com</p>
  6457. ````````````````````````````````
  6458. ```````````````````````````````` example
  6459. foo@bar.example.com
  6460. .
  6461. <p>foo@bar.example.com</p>
  6462. ````````````````````````````````
  6463. ## Raw HTML
  6464. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6465. raw HTML tag and will be rendered in HTML without escaping.
  6466. Tag and attribute names are not limited to current HTML tags,
  6467. so custom tags (and even, say, DocBook tags) may be used.
  6468. Here is the grammar for tags:
  6469. A [tag name](@) consists of an ASCII letter
  6470. followed by zero or more ASCII letters, digits, or
  6471. hyphens (`-`).
  6472. An [attribute](@) consists of [whitespace],
  6473. an [attribute name], and an optional
  6474. [attribute value specification].
  6475. An [attribute name](@)
  6476. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6477. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6478. specification restricted to ASCII. HTML5 is laxer.)
  6479. An [attribute value specification](@)
  6480. consists of optional [whitespace],
  6481. a `=` character, optional [whitespace], and an [attribute
  6482. value].
  6483. An [attribute value](@)
  6484. consists of an [unquoted attribute value],
  6485. a [single-quoted attribute value], or a [double-quoted attribute value].
  6486. An [unquoted attribute value](@)
  6487. is a nonempty string of characters not
  6488. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6489. A [single-quoted attribute value](@)
  6490. consists of `'`, zero or more
  6491. characters not including `'`, and a final `'`.
  6492. A [double-quoted attribute value](@)
  6493. consists of `"`, zero or more
  6494. characters not including `"`, and a final `"`.
  6495. An [open tag](@) consists of a `<` character, a [tag name],
  6496. zero or more [attributes], optional [whitespace], an optional `/`
  6497. character, and a `>` character.
  6498. A [closing tag](@) consists of the string `</`, a
  6499. [tag name], optional [whitespace], and the character `>`.
  6500. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6501. where *text* does not start with `>` or `->`, does not end with `-`,
  6502. and does not contain `--`. (See the
  6503. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6504. A [processing instruction](@)
  6505. consists of the string `<?`, a string
  6506. of characters not including the string `?>`, and the string
  6507. `?>`.
  6508. A [declaration](@) consists of the
  6509. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6510. [whitespace], a string of characters not including the
  6511. character `>`, and the character `>`.
  6512. A [CDATA section](@) consists of
  6513. the string `<![CDATA[`, a string of characters not including the string
  6514. `]]>`, and the string `]]>`.
  6515. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6516. an [HTML comment], a [processing instruction], a [declaration],
  6517. or a [CDATA section].
  6518. Here are some simple open tags:
  6519. ```````````````````````````````` example
  6520. <a><bab><c2c>
  6521. .
  6522. <p><a><bab><c2c></p>
  6523. ````````````````````````````````
  6524. Empty elements:
  6525. ```````````````````````````````` example
  6526. <a/><b2/>
  6527. .
  6528. <p><a/><b2/></p>
  6529. ````````````````````````````````
  6530. [Whitespace] is allowed:
  6531. ```````````````````````````````` example
  6532. <a /><b2
  6533. data="foo" >
  6534. .
  6535. <p><a /><b2
  6536. data="foo" ></p>
  6537. ````````````````````````````````
  6538. With attributes:
  6539. ```````````````````````````````` example
  6540. <a foo="bar" bam = 'baz <em>"</em>'
  6541. _boolean zoop:33=zoop:33 />
  6542. .
  6543. <p><a foo="bar" bam = 'baz <em>"</em>'
  6544. _boolean zoop:33=zoop:33 /></p>
  6545. ````````````````````````````````
  6546. Custom tag names can be used:
  6547. ```````````````````````````````` example
  6548. Foo <responsive-image src="foo.jpg" />
  6549. .
  6550. <p>Foo <responsive-image src="foo.jpg" /></p>
  6551. ````````````````````````````````
  6552. Illegal tag names, not parsed as HTML:
  6553. ```````````````````````````````` example
  6554. <33> <__>
  6555. .
  6556. <p>&lt;33&gt; &lt;__&gt;</p>
  6557. ````````````````````````````````
  6558. Illegal attribute names:
  6559. ```````````````````````````````` example
  6560. <a h*#ref="hi">
  6561. .
  6562. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6563. ````````````````````````````````
  6564. Illegal attribute values:
  6565. ```````````````````````````````` example
  6566. <a href="hi'> <a href=hi'>
  6567. .
  6568. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6569. ````````````````````````````````
  6570. Illegal [whitespace]:
  6571. ```````````````````````````````` example
  6572. < a><
  6573. foo><bar/ >
  6574. .
  6575. <p>&lt; a&gt;&lt;
  6576. foo&gt;&lt;bar/ &gt;</p>
  6577. ````````````````````````````````
  6578. Missing [whitespace]:
  6579. ```````````````````````````````` example
  6580. <a href='bar'title=title>
  6581. .
  6582. <p>&lt;a href='bar'title=title&gt;</p>
  6583. ````````````````````````````````
  6584. Closing tags:
  6585. ```````````````````````````````` example
  6586. </a></foo >
  6587. .
  6588. <p></a></foo ></p>
  6589. ````````````````````````````````
  6590. Illegal attributes in closing tag:
  6591. ```````````````````````````````` example
  6592. </a href="foo">
  6593. .
  6594. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6595. ````````````````````````````````
  6596. Comments:
  6597. ```````````````````````````````` example
  6598. foo <!-- this is a
  6599. comment - with hyphen -->
  6600. .
  6601. <p>foo <!-- this is a
  6602. comment - with hyphen --></p>
  6603. ````````````````````````````````
  6604. ```````````````````````````````` example
  6605. foo <!-- not a comment -- two hyphens -->
  6606. .
  6607. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6608. ````````````````````````````````
  6609. Not comments:
  6610. ```````````````````````````````` example
  6611. foo <!--> foo -->
  6612. foo <!-- foo--->
  6613. .
  6614. <p>foo &lt;!--&gt; foo --&gt;</p>
  6615. <p>foo &lt;!-- foo---&gt;</p>
  6616. ````````````````````````````````
  6617. Processing instructions:
  6618. ```````````````````````````````` example
  6619. foo <?php echo $a; ?>
  6620. .
  6621. <p>foo <?php echo $a; ?></p>
  6622. ````````````````````````````````
  6623. Declarations:
  6624. ```````````````````````````````` example
  6625. foo <!ELEMENT br EMPTY>
  6626. .
  6627. <p>foo <!ELEMENT br EMPTY></p>
  6628. ````````````````````````````````
  6629. CDATA sections:
  6630. ```````````````````````````````` example
  6631. foo <![CDATA[>&<]]>
  6632. .
  6633. <p>foo <![CDATA[>&<]]></p>
  6634. ````````````````````````````````
  6635. Entity and numeric character references are preserved in HTML
  6636. attributes:
  6637. ```````````````````````````````` example
  6638. foo <a href="&ouml;">
  6639. .
  6640. <p>foo <a href="&ouml;"></p>
  6641. ````````````````````````````````
  6642. Backslash escapes do not work in HTML attributes:
  6643. ```````````````````````````````` example
  6644. foo <a href="\*">
  6645. .
  6646. <p>foo <a href="\*"></p>
  6647. ````````````````````````````````
  6648. ```````````````````````````````` example
  6649. <a href="\"">
  6650. .
  6651. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6652. ````````````````````````````````
  6653. ## Hard line breaks
  6654. A line break (not in a code span or HTML tag) that is preceded
  6655. by two or more spaces and does not occur at the end of a block
  6656. is parsed as a [hard line break](@) (rendered
  6657. in HTML as a `<br />` tag):
  6658. ```````````````````````````````` example
  6659. foo
  6660. baz
  6661. .
  6662. <p>foo<br />
  6663. baz</p>
  6664. ````````````````````````````````
  6665. For a more visible alternative, a backslash before the
  6666. [line ending] may be used instead of two spaces:
  6667. ```````````````````````````````` example
  6668. foo\
  6669. baz
  6670. .
  6671. <p>foo<br />
  6672. baz</p>
  6673. ````````````````````````````````
  6674. More than two spaces can be used:
  6675. ```````````````````````````````` example
  6676. foo
  6677. baz
  6678. .
  6679. <p>foo<br />
  6680. baz</p>
  6681. ````````````````````````````````
  6682. Leading spaces at the beginning of the next line are ignored:
  6683. ```````````````````````````````` example
  6684. foo
  6685. bar
  6686. .
  6687. <p>foo<br />
  6688. bar</p>
  6689. ````````````````````````````````
  6690. ```````````````````````````````` example
  6691. foo\
  6692. bar
  6693. .
  6694. <p>foo<br />
  6695. bar</p>
  6696. ````````````````````````````````
  6697. Line breaks can occur inside emphasis, links, and other constructs
  6698. that allow inline content:
  6699. ```````````````````````````````` example
  6700. *foo
  6701. bar*
  6702. .
  6703. <p><em>foo<br />
  6704. bar</em></p>
  6705. ````````````````````````````````
  6706. ```````````````````````````````` example
  6707. *foo\
  6708. bar*
  6709. .
  6710. <p><em>foo<br />
  6711. bar</em></p>
  6712. ````````````````````````````````
  6713. Line breaks do not occur inside code spans
  6714. ```````````````````````````````` example
  6715. `code
  6716. span`
  6717. .
  6718. <p><code>code span</code></p>
  6719. ````````````````````````````````
  6720. ```````````````````````````````` example
  6721. `code\
  6722. span`
  6723. .
  6724. <p><code>code\ span</code></p>
  6725. ````````````````````````````````
  6726. or HTML tags:
  6727. ```````````````````````````````` example
  6728. <a href="foo
  6729. bar">
  6730. .
  6731. <p><a href="foo
  6732. bar"></p>
  6733. ````````````````````````````````
  6734. ```````````````````````````````` example
  6735. <a href="foo\
  6736. bar">
  6737. .
  6738. <p><a href="foo\
  6739. bar"></p>
  6740. ````````````````````````````````
  6741. Hard line breaks are for separating inline content within a block.
  6742. Neither syntax for hard line breaks works at the end of a paragraph or
  6743. other block element:
  6744. ```````````````````````````````` example
  6745. foo\
  6746. .
  6747. <p>foo\</p>
  6748. ````````````````````````````````
  6749. ```````````````````````````````` example
  6750. foo
  6751. .
  6752. <p>foo</p>
  6753. ````````````````````````````````
  6754. ```````````````````````````````` example
  6755. ### foo\
  6756. .
  6757. <h3>foo\</h3>
  6758. ````````````````````````````````
  6759. ```````````````````````````````` example
  6760. ### foo
  6761. .
  6762. <h3>foo</h3>
  6763. ````````````````````````````````
  6764. ## Soft line breaks
  6765. A regular line break (not in a code span or HTML tag) that is not
  6766. preceded by two or more spaces or a backslash is parsed as a
  6767. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6768. [line ending] or as a space. The result will be the same in
  6769. browsers. In the examples here, a [line ending] will be used.)
  6770. ```````````````````````````````` example
  6771. foo
  6772. baz
  6773. .
  6774. <p>foo
  6775. baz</p>
  6776. ````````````````````````````````
  6777. Spaces at the end of the line and beginning of the next line are
  6778. removed:
  6779. ```````````````````````````````` example
  6780. foo
  6781. baz
  6782. .
  6783. <p>foo
  6784. baz</p>
  6785. ````````````````````````````````
  6786. A conforming parser may render a soft line break in HTML either as a
  6787. line break or as a space.
  6788. A renderer may also provide an option to render soft line breaks
  6789. as hard line breaks.
  6790. ## Textual content
  6791. Any characters not given an interpretation by the above rules will
  6792. be parsed as plain textual content.
  6793. ```````````````````````````````` example
  6794. hello $.;'there
  6795. .
  6796. <p>hello $.;'there</p>
  6797. ````````````````````````````````
  6798. ```````````````````````````````` example
  6799. Foo χρῆν
  6800. .
  6801. <p>Foo χρῆν</p>
  6802. ````````````````````````````````
  6803. Internal spaces are preserved verbatim:
  6804. ```````````````````````````````` example
  6805. Multiple spaces
  6806. .
  6807. <p>Multiple spaces</p>
  6808. ````````````````````````````````
  6809. <!-- END TESTS -->
  6810. # Appendix: A parsing strategy
  6811. In this appendix we describe some features of the parsing strategy
  6812. used in the CommonMark reference implementations.
  6813. ## Overview
  6814. Parsing has two phases:
  6815. 1. In the first phase, lines of input are consumed and the block
  6816. structure of the document---its division into paragraphs, block quotes,
  6817. list items, and so on---is constructed. Text is assigned to these
  6818. blocks but not parsed. Link reference definitions are parsed and a
  6819. map of links is constructed.
  6820. 2. In the second phase, the raw text contents of paragraphs and headings
  6821. are parsed into sequences of Markdown inline elements (strings,
  6822. code spans, links, emphasis, and so on), using the map of link
  6823. references constructed in phase 1.
  6824. At each point in processing, the document is represented as a tree of
  6825. **blocks**. The root of the tree is a `document` block. The `document`
  6826. may have any number of other blocks as **children**. These children
  6827. may, in turn, have other blocks as children. The last child of a block
  6828. is normally considered **open**, meaning that subsequent lines of input
  6829. can alter its contents. (Blocks that are not open are **closed**.)
  6830. Here, for example, is a possible document tree, with the open blocks
  6831. marked by arrows:
  6832. ``` tree
  6833. -> document
  6834. -> block_quote
  6835. paragraph
  6836. "Lorem ipsum dolor\nsit amet."
  6837. -> list (type=bullet tight=true bullet_char=-)
  6838. list_item
  6839. paragraph
  6840. "Qui *quodsi iracundia*"
  6841. -> list_item
  6842. -> paragraph
  6843. "aliquando id"
  6844. ```
  6845. ## Phase 1: block structure
  6846. Each line that is processed has an effect on this tree. The line is
  6847. analyzed and, depending on its contents, the document may be altered
  6848. in one or more of the following ways:
  6849. 1. One or more open blocks may be closed.
  6850. 2. One or more new blocks may be created as children of the
  6851. last open block.
  6852. 3. Text may be added to the last (deepest) open block remaining
  6853. on the tree.
  6854. Once a line has been incorporated into the tree in this way,
  6855. it can be discarded, so input can be read in a stream.
  6856. For each line, we follow this procedure:
  6857. 1. First we iterate through the open blocks, starting with the
  6858. root document, and descending through last children down to the last
  6859. open block. Each block imposes a condition that the line must satisfy
  6860. if the block is to remain open. For example, a block quote requires a
  6861. `>` character. A paragraph requires a non-blank line.
  6862. In this phase we may match all or just some of the open
  6863. blocks. But we cannot close unmatched blocks yet, because we may have a
  6864. [lazy continuation line].
  6865. 2. Next, after consuming the continuation markers for existing
  6866. blocks, we look for new block starts (e.g. `>` for a block quote).
  6867. If we encounter a new block start, we close any blocks unmatched
  6868. in step 1 before creating the new block as a child of the last
  6869. matched block.
  6870. 3. Finally, we look at the remainder of the line (after block
  6871. markers like `>`, list markers, and indentation have been consumed).
  6872. This is text that can be incorporated into the last open
  6873. block (a paragraph, code block, heading, or raw HTML).
  6874. Setext headings are formed when we see a line of a paragraph
  6875. that is a [setext heading underline].
  6876. Reference link definitions are detected when a paragraph is closed;
  6877. the accumulated text lines are parsed to see if they begin with
  6878. one or more reference link definitions. Any remainder becomes a
  6879. normal paragraph.
  6880. We can see how this works by considering how the tree above is
  6881. generated by four lines of Markdown:
  6882. ``` markdown
  6883. > Lorem ipsum dolor
  6884. sit amet.
  6885. > - Qui *quodsi iracundia*
  6886. > - aliquando id
  6887. ```
  6888. At the outset, our document model is just
  6889. ``` tree
  6890. -> document
  6891. ```
  6892. The first line of our text,
  6893. ``` markdown
  6894. > Lorem ipsum dolor
  6895. ```
  6896. causes a `block_quote` block to be created as a child of our
  6897. open `document` block, and a `paragraph` block as a child of
  6898. the `block_quote`. Then the text is added to the last open
  6899. block, the `paragraph`:
  6900. ``` tree
  6901. -> document
  6902. -> block_quote
  6903. -> paragraph
  6904. "Lorem ipsum dolor"
  6905. ```
  6906. The next line,
  6907. ``` markdown
  6908. sit amet.
  6909. ```
  6910. is a "lazy continuation" of the open `paragraph`, so it gets added
  6911. to the paragraph's text:
  6912. ``` tree
  6913. -> document
  6914. -> block_quote
  6915. -> paragraph
  6916. "Lorem ipsum dolor\nsit amet."
  6917. ```
  6918. The third line,
  6919. ``` markdown
  6920. > - Qui *quodsi iracundia*
  6921. ```
  6922. causes the `paragraph` block to be closed, and a new `list` block
  6923. opened as a child of the `block_quote`. A `list_item` is also
  6924. added as a child of the `list`, and a `paragraph` as a child of
  6925. the `list_item`. The text is then added to the new `paragraph`:
  6926. ``` tree
  6927. -> document
  6928. -> block_quote
  6929. paragraph
  6930. "Lorem ipsum dolor\nsit amet."
  6931. -> list (type=bullet tight=true bullet_char=-)
  6932. -> list_item
  6933. -> paragraph
  6934. "Qui *quodsi iracundia*"
  6935. ```
  6936. The fourth line,
  6937. ``` markdown
  6938. > - aliquando id
  6939. ```
  6940. causes the `list_item` (and its child the `paragraph`) to be closed,
  6941. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6942. is added as a child of the new `list_item`, to contain the text.
  6943. We thus obtain the final tree:
  6944. ``` tree
  6945. -> document
  6946. -> block_quote
  6947. paragraph
  6948. "Lorem ipsum dolor\nsit amet."
  6949. -> list (type=bullet tight=true bullet_char=-)
  6950. list_item
  6951. paragraph
  6952. "Qui *quodsi iracundia*"
  6953. -> list_item
  6954. -> paragraph
  6955. "aliquando id"
  6956. ```
  6957. ## Phase 2: inline structure
  6958. Once all of the input has been parsed, all open blocks are closed.
  6959. We then "walk the tree," visiting every node, and parse raw
  6960. string contents of paragraphs and headings as inlines. At this
  6961. point we have seen all the link reference definitions, so we can
  6962. resolve reference links as we go.
  6963. ``` tree
  6964. document
  6965. block_quote
  6966. paragraph
  6967. str "Lorem ipsum dolor"
  6968. softbreak
  6969. str "sit amet."
  6970. list (type=bullet tight=true bullet_char=-)
  6971. list_item
  6972. paragraph
  6973. str "Qui "
  6974. emph
  6975. str "quodsi iracundia"
  6976. list_item
  6977. paragraph
  6978. str "aliquando id"
  6979. ```
  6980. Notice how the [line ending] in the first paragraph has
  6981. been parsed as a `softbreak`, and the asterisks in the first list item
  6982. have become an `emph`.
  6983. ### An algorithm for parsing nested emphasis and links
  6984. By far the trickiest part of inline parsing is handling emphasis,
  6985. strong emphasis, links, and images. This is done using the following
  6986. algorithm.
  6987. When we're parsing inlines and we hit either
  6988. - a run of `*` or `_` characters, or
  6989. - a `[` or `![`
  6990. we insert a text node with these symbols as its literal content, and we
  6991. add a pointer to this text node to the [delimiter stack](@).
  6992. The [delimiter stack] is a doubly linked list. Each
  6993. element contains a pointer to a text node, plus information about
  6994. - the type of delimiter (`[`, `![`, `*`, `_`)
  6995. - the number of delimiters,
  6996. - whether the delimiter is "active" (all are active to start), and
  6997. - whether the delimiter is a potential opener, a potential closer,
  6998. or both (which depends on what sort of characters precede
  6999. and follow the delimiters).
  7000. When we hit a `]` character, we call the *look for link or image*
  7001. procedure (see below).
  7002. When we hit the end of the input, we call the *process emphasis*
  7003. procedure (see below), with `stack_bottom` = NULL.
  7004. #### *look for link or image*
  7005. Starting at the top of the delimiter stack, we look backwards
  7006. through the stack for an opening `[` or `![` delimiter.
  7007. - If we don't find one, we return a literal text node `]`.
  7008. - If we do find one, but it's not *active*, we remove the inactive
  7009. delimiter from the stack, and return a literal text node `]`.
  7010. - If we find one and it's active, then we parse ahead to see if
  7011. we have an inline link/image, reference link/image, compact reference
  7012. link/image, or shortcut reference link/image.
  7013. + If we don't, then we remove the opening delimiter from the
  7014. delimiter stack and return a literal text node `]`.
  7015. + If we do, then
  7016. * We return a link or image node whose children are the inlines
  7017. after the text node pointed to by the opening delimiter.
  7018. * We run *process emphasis* on these inlines, with the `[` opener
  7019. as `stack_bottom`.
  7020. * We remove the opening delimiter.
  7021. * If we have a link (and not an image), we also set all
  7022. `[` delimiters before the opening delimiter to *inactive*. (This
  7023. will prevent us from getting links within links.)
  7024. #### *process emphasis*
  7025. Parameter `stack_bottom` sets a lower bound to how far we
  7026. descend in the [delimiter stack]. If it is NULL, we can
  7027. go all the way to the bottom. Otherwise, we stop before
  7028. visiting `stack_bottom`.
  7029. Let `current_position` point to the element on the [delimiter stack]
  7030. just above `stack_bottom` (or the first element if `stack_bottom`
  7031. is NULL).
  7032. We keep track of the `openers_bottom` for each delimiter
  7033. type (`*`, `_`). Initialize this to `stack_bottom`.
  7034. Then we repeat the following until we run out of potential
  7035. closers:
  7036. - Move `current_position` forward in the delimiter stack (if needed)
  7037. until we find the first potential closer with delimiter `*` or `_`.
  7038. (This will be the potential closer closest
  7039. to the beginning of the input -- the first one in parse order.)
  7040. - Now, look back in the stack (staying above `stack_bottom` and
  7041. the `openers_bottom` for this delimiter type) for the
  7042. first matching potential opener ("matching" means same delimiter).
  7043. - If one is found:
  7044. + Figure out whether we have emphasis or strong emphasis:
  7045. if both closer and opener spans have length >= 2, we have
  7046. strong, otherwise regular.
  7047. + Insert an emph or strong emph node accordingly, after
  7048. the text node corresponding to the opener.
  7049. + Remove any delimiters between the opener and closer from
  7050. the delimiter stack.
  7051. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7052. from the opening and closing text nodes. If they become empty
  7053. as a result, remove them and remove the corresponding element
  7054. of the delimiter stack. If the closing node is removed, reset
  7055. `current_position` to the next element in the stack.
  7056. - If none in found:
  7057. + Set `openers_bottom` to the element before `current_position`.
  7058. (We know that there are no openers for this kind of closer up to and
  7059. including this point, so this puts a lower bound on future searches.)
  7060. + If the closer at `current_position` is not a potential opener,
  7061. remove it from the delimiter stack (since we know it can't
  7062. be a closer either).
  7063. + Advance `current_position` to the next element in the stack.
  7064. After we're done, we remove all delimiters above `stack_bottom` from the
  7065. delimiter stack.