aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 690bbb167431be3422cabe9f3161013a98c8f5cd (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.27
  5. date: '2016-11-18'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a github wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/`, `:`, `;`, `<`, `=`, `>`, `?`, `@`,
  252. `[`, `\`, `]`, `^`, `_`, `` ` ``, `{`, `|`, `}`, or `~`.
  253. A [punctuation character](@) is an [ASCII
  254. punctuation character] or anything in
  255. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  256. ## Tabs
  257. Tabs in lines are not expanded to [spaces]. However,
  258. in contexts where whitespace helps to define block structure,
  259. tabs behave as if they were replaced by spaces with a tab stop
  260. of 4 characters.
  261. Thus, for example, a tab can be used instead of four spaces
  262. in an indented code block. (Note, however, that internal
  263. tabs are passed through as literal tabs, not expanded to
  264. spaces.)
  265. ```````````````````````````````` example
  266. →foo→baz→→bim
  267. .
  268. <pre><code>foo→baz→→bim
  269. </code></pre>
  270. ````````````````````````````````
  271. ```````````````````````````````` example
  272. →foo→baz→→bim
  273. .
  274. <pre><code>foo→baz→→bim
  275. </code></pre>
  276. ````````````````````````````````
  277. ```````````````````````````````` example
  278. a→a
  279. ὐ→a
  280. .
  281. <pre><code>a→a
  282. ὐ→a
  283. </code></pre>
  284. ````````````````````````````````
  285. In the following example, a continuation paragraph of a list
  286. item is indented with a tab; this has exactly the same effect
  287. as indentation with four spaces would:
  288. ```````````````````````````````` example
  289. - foo
  290. →bar
  291. .
  292. <ul>
  293. <li>
  294. <p>foo</p>
  295. <p>bar</p>
  296. </li>
  297. </ul>
  298. ````````````````````````````````
  299. ```````````````````````````````` example
  300. - foo
  301. →→bar
  302. .
  303. <ul>
  304. <li>
  305. <p>foo</p>
  306. <pre><code> bar
  307. </code></pre>
  308. </li>
  309. </ul>
  310. ````````````````````````````````
  311. Normally the `>` that begins a block quote may be followed
  312. optionally by a space, which is not considered part of the
  313. content. In the following case `>` is followed by a tab,
  314. which is treated as if it were expanded into three spaces.
  315. Since one of these spaces is considered part of the
  316. delimiter, `foo` is considered to be indented six spaces
  317. inside the block quote context, so we get an indented
  318. code block starting with two spaces.
  319. ```````````````````````````````` example
  320. >→→foo
  321. .
  322. <blockquote>
  323. <pre><code> foo
  324. </code></pre>
  325. </blockquote>
  326. ````````````````````````````````
  327. ```````````````````````````````` example
  328. -→→foo
  329. .
  330. <ul>
  331. <li>
  332. <pre><code> foo
  333. </code></pre>
  334. </li>
  335. </ul>
  336. ````````````````````````````````
  337. ```````````````````````````````` example
  338. foo
  339. →bar
  340. .
  341. <pre><code>foo
  342. bar
  343. </code></pre>
  344. ````````````````````````````````
  345. ```````````````````````````````` example
  346. - foo
  347. - bar
  348. → - baz
  349. .
  350. <ul>
  351. <li>foo
  352. <ul>
  353. <li>bar
  354. <ul>
  355. <li>baz</li>
  356. </ul>
  357. </li>
  358. </ul>
  359. </li>
  360. </ul>
  361. ````````````````````````````````
  362. ```````````````````````````````` example
  363. #→Foo
  364. .
  365. <h1>Foo</h1>
  366. ````````````````````````````````
  367. ```````````````````````````````` example
  368. *→*→*→
  369. .
  370. <hr />
  371. ````````````````````````````````
  372. ## Insecure characters
  373. For security reasons, the Unicode character `U+0000` must be replaced
  374. with the REPLACEMENT CHARACTER (`U+FFFD`).
  375. # Blocks and inlines
  376. We can think of a document as a sequence of
  377. [blocks](@)---structural elements like paragraphs, block
  378. quotations, lists, headings, rules, and code blocks. Some blocks (like
  379. block quotes and list items) contain other blocks; others (like
  380. headings and paragraphs) contain [inline](@) content---text,
  381. links, emphasized text, images, code, and so on.
  382. ## Precedence
  383. Indicators of block structure always take precedence over indicators
  384. of inline structure. So, for example, the following is a list with
  385. two items, not a list with one item containing a code span:
  386. ```````````````````````````````` example
  387. - `one
  388. - two`
  389. .
  390. <ul>
  391. <li>`one</li>
  392. <li>two`</li>
  393. </ul>
  394. ````````````````````````````````
  395. This means that parsing can proceed in two steps: first, the block
  396. structure of the document can be discerned; second, text lines inside
  397. paragraphs, headings, and other block constructs can be parsed for inline
  398. structure. The second step requires information about link reference
  399. definitions that will be available only at the end of the first
  400. step. Note that the first step requires processing lines in sequence,
  401. but the second can be parallelized, since the inline parsing of
  402. one block element does not affect the inline parsing of any other.
  403. ## Container blocks and leaf blocks
  404. We can divide blocks into two types:
  405. [container block](@)s,
  406. which can contain other blocks, and [leaf block](@)s,
  407. which cannot.
  408. # Leaf blocks
  409. This section describes the different kinds of leaf block that make up a
  410. Markdown document.
  411. ## Thematic breaks
  412. A line consisting of 0-3 spaces of indentation, followed by a sequence
  413. of three or more matching `-`, `_`, or `*` characters, each followed
  414. optionally by any number of spaces, forms a
  415. [thematic break](@).
  416. ```````````````````````````````` example
  417. ***
  418. ---
  419. ___
  420. .
  421. <hr />
  422. <hr />
  423. <hr />
  424. ````````````````````````````````
  425. Wrong characters:
  426. ```````````````````````````````` example
  427. +++
  428. .
  429. <p>+++</p>
  430. ````````````````````````````````
  431. ```````````````````````````````` example
  432. ===
  433. .
  434. <p>===</p>
  435. ````````````````````````````````
  436. Not enough characters:
  437. ```````````````````````````````` example
  438. --
  439. **
  440. __
  441. .
  442. <p>--
  443. **
  444. __</p>
  445. ````````````````````````````````
  446. One to three spaces indent are allowed:
  447. ```````````````````````````````` example
  448. ***
  449. ***
  450. ***
  451. .
  452. <hr />
  453. <hr />
  454. <hr />
  455. ````````````````````````````````
  456. Four spaces is too many:
  457. ```````````````````````````````` example
  458. ***
  459. .
  460. <pre><code>***
  461. </code></pre>
  462. ````````````````````````````````
  463. ```````````````````````````````` example
  464. Foo
  465. ***
  466. .
  467. <p>Foo
  468. ***</p>
  469. ````````````````````````````````
  470. More than three characters may be used:
  471. ```````````````````````````````` example
  472. _____________________________________
  473. .
  474. <hr />
  475. ````````````````````````````````
  476. Spaces are allowed between the characters:
  477. ```````````````````````````````` example
  478. - - -
  479. .
  480. <hr />
  481. ````````````````````````````````
  482. ```````````````````````````````` example
  483. ** * ** * ** * **
  484. .
  485. <hr />
  486. ````````````````````````````````
  487. ```````````````````````````````` example
  488. - - - -
  489. .
  490. <hr />
  491. ````````````````````````````````
  492. Spaces are allowed at the end:
  493. ```````````````````````````````` example
  494. - - - -
  495. .
  496. <hr />
  497. ````````````````````````````````
  498. However, no other characters may occur in the line:
  499. ```````````````````````````````` example
  500. _ _ _ _ a
  501. a------
  502. ---a---
  503. .
  504. <p>_ _ _ _ a</p>
  505. <p>a------</p>
  506. <p>---a---</p>
  507. ````````````````````````````````
  508. It is required that all of the [non-whitespace characters] be the same.
  509. So, this is not a thematic break:
  510. ```````````````````````````````` example
  511. *-*
  512. .
  513. <p><em>-</em></p>
  514. ````````````````````````````````
  515. Thematic breaks do not need blank lines before or after:
  516. ```````````````````````````````` example
  517. - foo
  518. ***
  519. - bar
  520. .
  521. <ul>
  522. <li>foo</li>
  523. </ul>
  524. <hr />
  525. <ul>
  526. <li>bar</li>
  527. </ul>
  528. ````````````````````````````````
  529. Thematic breaks can interrupt a paragraph:
  530. ```````````````````````````````` example
  531. Foo
  532. ***
  533. bar
  534. .
  535. <p>Foo</p>
  536. <hr />
  537. <p>bar</p>
  538. ````````````````````````````````
  539. If a line of dashes that meets the above conditions for being a
  540. thematic break could also be interpreted as the underline of a [setext
  541. heading], the interpretation as a
  542. [setext heading] takes precedence. Thus, for example,
  543. this is a setext heading, not a paragraph followed by a thematic break:
  544. ```````````````````````````````` example
  545. Foo
  546. ---
  547. bar
  548. .
  549. <h2>Foo</h2>
  550. <p>bar</p>
  551. ````````````````````````````````
  552. When both a thematic break and a list item are possible
  553. interpretations of a line, the thematic break takes precedence:
  554. ```````````````````````````````` example
  555. * Foo
  556. * * *
  557. * Bar
  558. .
  559. <ul>
  560. <li>Foo</li>
  561. </ul>
  562. <hr />
  563. <ul>
  564. <li>Bar</li>
  565. </ul>
  566. ````````````````````````````````
  567. If you want a thematic break in a list item, use a different bullet:
  568. ```````````````````````````````` example
  569. - Foo
  570. - * * *
  571. .
  572. <ul>
  573. <li>Foo</li>
  574. <li>
  575. <hr />
  576. </li>
  577. </ul>
  578. ````````````````````````````````
  579. ## ATX headings
  580. An [ATX heading](@)
  581. consists of a string of characters, parsed as inline content, between an
  582. opening sequence of 1--6 unescaped `#` characters and an optional
  583. closing sequence of any number of unescaped `#` characters.
  584. The opening sequence of `#` characters must be followed by a
  585. [space] or by the end of line. The optional closing sequence of `#`s must be
  586. preceded by a [space] and may be followed by spaces only. The opening
  587. `#` character may be indented 0-3 spaces. The raw contents of the
  588. heading are stripped of leading and trailing spaces before being parsed
  589. as inline content. The heading level is equal to the number of `#`
  590. characters in the opening sequence.
  591. Simple headings:
  592. ```````````````````````````````` example
  593. # foo
  594. ## foo
  595. ### foo
  596. #### foo
  597. ##### foo
  598. ###### foo
  599. .
  600. <h1>foo</h1>
  601. <h2>foo</h2>
  602. <h3>foo</h3>
  603. <h4>foo</h4>
  604. <h5>foo</h5>
  605. <h6>foo</h6>
  606. ````````````````````````````````
  607. More than six `#` characters is not a heading:
  608. ```````````````````````````````` example
  609. ####### foo
  610. .
  611. <p>####### foo</p>
  612. ````````````````````````````````
  613. At least one space is required between the `#` characters and the
  614. heading's contents, unless the heading is empty. Note that many
  615. implementations currently do not require the space. However, the
  616. space was required by the
  617. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  618. and it helps prevent things like the following from being parsed as
  619. headings:
  620. ```````````````````````````````` example
  621. #5 bolt
  622. #hashtag
  623. .
  624. <p>#5 bolt</p>
  625. <p>#hashtag</p>
  626. ````````````````````````````````
  627. This is not a heading, because the first `#` is escaped:
  628. ```````````````````````````````` example
  629. \## foo
  630. .
  631. <p>## foo</p>
  632. ````````````````````````````````
  633. Contents are parsed as inlines:
  634. ```````````````````````````````` example
  635. # foo *bar* \*baz\*
  636. .
  637. <h1>foo <em>bar</em> *baz*</h1>
  638. ````````````````````````````````
  639. Leading and trailing blanks are ignored in parsing inline content:
  640. ```````````````````````````````` example
  641. # foo
  642. .
  643. <h1>foo</h1>
  644. ````````````````````````````````
  645. One to three spaces indentation are allowed:
  646. ```````````````````````````````` example
  647. ### foo
  648. ## foo
  649. # foo
  650. .
  651. <h3>foo</h3>
  652. <h2>foo</h2>
  653. <h1>foo</h1>
  654. ````````````````````````````````
  655. Four spaces are too much:
  656. ```````````````````````````````` example
  657. # foo
  658. .
  659. <pre><code># foo
  660. </code></pre>
  661. ````````````````````````````````
  662. ```````````````````````````````` example
  663. foo
  664. # bar
  665. .
  666. <p>foo
  667. # bar</p>
  668. ````````````````````````````````
  669. A closing sequence of `#` characters is optional:
  670. ```````````````````````````````` example
  671. ## foo ##
  672. ### bar ###
  673. .
  674. <h2>foo</h2>
  675. <h3>bar</h3>
  676. ````````````````````````````````
  677. It need not be the same length as the opening sequence:
  678. ```````````````````````````````` example
  679. # foo ##################################
  680. ##### foo ##
  681. .
  682. <h1>foo</h1>
  683. <h5>foo</h5>
  684. ````````````````````````````````
  685. Spaces are allowed after the closing sequence:
  686. ```````````````````````````````` example
  687. ### foo ###
  688. .
  689. <h3>foo</h3>
  690. ````````````````````````````````
  691. A sequence of `#` characters with anything but [spaces] following it
  692. is not a closing sequence, but counts as part of the contents of the
  693. heading:
  694. ```````````````````````````````` example
  695. ### foo ### b
  696. .
  697. <h3>foo ### b</h3>
  698. ````````````````````````````````
  699. The closing sequence must be preceded by a space:
  700. ```````````````````````````````` example
  701. # foo#
  702. .
  703. <h1>foo#</h1>
  704. ````````````````````````````````
  705. Backslash-escaped `#` characters do not count as part
  706. of the closing sequence:
  707. ```````````````````````````````` example
  708. ### foo \###
  709. ## foo #\##
  710. # foo \#
  711. .
  712. <h3>foo ###</h3>
  713. <h2>foo ###</h2>
  714. <h1>foo #</h1>
  715. ````````````````````````````````
  716. ATX headings need not be separated from surrounding content by blank
  717. lines, and they can interrupt paragraphs:
  718. ```````````````````````````````` example
  719. ****
  720. ## foo
  721. ****
  722. .
  723. <hr />
  724. <h2>foo</h2>
  725. <hr />
  726. ````````````````````````````````
  727. ```````````````````````````````` example
  728. Foo bar
  729. # baz
  730. Bar foo
  731. .
  732. <p>Foo bar</p>
  733. <h1>baz</h1>
  734. <p>Bar foo</p>
  735. ````````````````````````````````
  736. ATX headings can be empty:
  737. ```````````````````````````````` example
  738. ##
  739. #
  740. ### ###
  741. .
  742. <h2></h2>
  743. <h1></h1>
  744. <h3></h3>
  745. ````````````````````````````````
  746. ## Setext headings
  747. A [setext heading](@) consists of one or more
  748. lines of text, each containing at least one [non-whitespace
  749. character], with no more than 3 spaces indentation, followed by
  750. a [setext heading underline]. The lines of text must be such
  751. that, were they not followed by the setext heading underline,
  752. they would be interpreted as a paragraph: they cannot be
  753. interpretable as a [code fence], [ATX heading][ATX headings],
  754. [block quote][block quotes], [thematic break][thematic breaks],
  755. [list item][list items], or [HTML block][HTML blocks].
  756. A [setext heading underline](@) is a sequence of
  757. `=` characters or a sequence of `-` characters, with no more than 3
  758. spaces indentation and any number of trailing spaces. If a line
  759. containing a single `-` can be interpreted as an
  760. empty [list items], it should be interpreted this way
  761. and not as a [setext heading underline].
  762. The heading is a level 1 heading if `=` characters are used in
  763. the [setext heading underline], and a level 2 heading if `-`
  764. characters are used. The contents of the heading are the result
  765. of parsing the preceding lines of text as CommonMark inline
  766. content.
  767. In general, a setext heading need not be preceded or followed by a
  768. blank line. However, it cannot interrupt a paragraph, so when a
  769. setext heading comes after a paragraph, a blank line is needed between
  770. them.
  771. Simple examples:
  772. ```````````````````````````````` example
  773. Foo *bar*
  774. =========
  775. Foo *bar*
  776. ---------
  777. .
  778. <h1>Foo <em>bar</em></h1>
  779. <h2>Foo <em>bar</em></h2>
  780. ````````````````````````````````
  781. The content of the header may span more than one line:
  782. ```````````````````````````````` example
  783. Foo *bar
  784. baz*
  785. ====
  786. .
  787. <h1>Foo <em>bar
  788. baz</em></h1>
  789. ````````````````````````````````
  790. The underlining can be any length:
  791. ```````````````````````````````` example
  792. Foo
  793. -------------------------
  794. Foo
  795. =
  796. .
  797. <h2>Foo</h2>
  798. <h1>Foo</h1>
  799. ````````````````````````````````
  800. The heading content can be indented up to three spaces, and need
  801. not line up with the underlining:
  802. ```````````````````````````````` example
  803. Foo
  804. ---
  805. Foo
  806. -----
  807. Foo
  808. ===
  809. .
  810. <h2>Foo</h2>
  811. <h2>Foo</h2>
  812. <h1>Foo</h1>
  813. ````````````````````````````````
  814. Four spaces indent is too much:
  815. ```````````````````````````````` example
  816. Foo
  817. ---
  818. Foo
  819. ---
  820. .
  821. <pre><code>Foo
  822. ---
  823. Foo
  824. </code></pre>
  825. <hr />
  826. ````````````````````````````````
  827. The setext heading underline can be indented up to three spaces, and
  828. may have trailing spaces:
  829. ```````````````````````````````` example
  830. Foo
  831. ----
  832. .
  833. <h2>Foo</h2>
  834. ````````````````````````````````
  835. Four spaces is too much:
  836. ```````````````````````````````` example
  837. Foo
  838. ---
  839. .
  840. <p>Foo
  841. ---</p>
  842. ````````````````````````````````
  843. The setext heading underline cannot contain internal spaces:
  844. ```````````````````````````````` example
  845. Foo
  846. = =
  847. Foo
  848. --- -
  849. .
  850. <p>Foo
  851. = =</p>
  852. <p>Foo</p>
  853. <hr />
  854. ````````````````````````````````
  855. Trailing spaces in the content line do not cause a line break:
  856. ```````````````````````````````` example
  857. Foo
  858. -----
  859. .
  860. <h2>Foo</h2>
  861. ````````````````````````````````
  862. Nor does a backslash at the end:
  863. ```````````````````````````````` example
  864. Foo\
  865. ----
  866. .
  867. <h2>Foo\</h2>
  868. ````````````````````````````````
  869. Since indicators of block structure take precedence over
  870. indicators of inline structure, the following are setext headings:
  871. ```````````````````````````````` example
  872. `Foo
  873. ----
  874. `
  875. <a title="a lot
  876. ---
  877. of dashes"/>
  878. .
  879. <h2>`Foo</h2>
  880. <p>`</p>
  881. <h2>&lt;a title=&quot;a lot</h2>
  882. <p>of dashes&quot;/&gt;</p>
  883. ````````````````````````````````
  884. The setext heading underline cannot be a [lazy continuation
  885. line] in a list item or block quote:
  886. ```````````````````````````````` example
  887. > Foo
  888. ---
  889. .
  890. <blockquote>
  891. <p>Foo</p>
  892. </blockquote>
  893. <hr />
  894. ````````````````````````````````
  895. ```````````````````````````````` example
  896. > foo
  897. bar
  898. ===
  899. .
  900. <blockquote>
  901. <p>foo
  902. bar
  903. ===</p>
  904. </blockquote>
  905. ````````````````````````````````
  906. ```````````````````````````````` example
  907. - Foo
  908. ---
  909. .
  910. <ul>
  911. <li>Foo</li>
  912. </ul>
  913. <hr />
  914. ````````````````````````````````
  915. A blank line is needed between a paragraph and a following
  916. setext heading, since otherwise the paragraph becomes part
  917. of the heading's content:
  918. ```````````````````````````````` example
  919. Foo
  920. Bar
  921. ---
  922. .
  923. <h2>Foo
  924. Bar</h2>
  925. ````````````````````````````````
  926. But in general a blank line is not required before or after
  927. setext headings:
  928. ```````````````````````````````` example
  929. ---
  930. Foo
  931. ---
  932. Bar
  933. ---
  934. Baz
  935. .
  936. <hr />
  937. <h2>Foo</h2>
  938. <h2>Bar</h2>
  939. <p>Baz</p>
  940. ````````````````````````````````
  941. Setext headings cannot be empty:
  942. ```````````````````````````````` example
  943. ====
  944. .
  945. <p>====</p>
  946. ````````````````````````````````
  947. Setext heading text lines must not be interpretable as block
  948. constructs other than paragraphs. So, the line of dashes
  949. in these examples gets interpreted as a thematic break:
  950. ```````````````````````````````` example
  951. ---
  952. ---
  953. .
  954. <hr />
  955. <hr />
  956. ````````````````````````````````
  957. ```````````````````````````````` example
  958. - foo
  959. -----
  960. .
  961. <ul>
  962. <li>foo</li>
  963. </ul>
  964. <hr />
  965. ````````````````````````````````
  966. ```````````````````````````````` example
  967. foo
  968. ---
  969. .
  970. <pre><code>foo
  971. </code></pre>
  972. <hr />
  973. ````````````````````````````````
  974. ```````````````````````````````` example
  975. > foo
  976. -----
  977. .
  978. <blockquote>
  979. <p>foo</p>
  980. </blockquote>
  981. <hr />
  982. ````````````````````````````````
  983. If you want a heading with `> foo` as its literal text, you can
  984. use backslash escapes:
  985. ```````````````````````````````` example
  986. \> foo
  987. ------
  988. .
  989. <h2>&gt; foo</h2>
  990. ````````````````````````````````
  991. **Compatibility note:** Most existing Markdown implementations
  992. do not allow the text of setext headings to span multiple lines.
  993. But there is no consensus about how to interpret
  994. ``` markdown
  995. Foo
  996. bar
  997. ---
  998. baz
  999. ```
  1000. One can find four different interpretations:
  1001. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1002. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1003. 3. paragraph "Foo bar --- baz"
  1004. 4. heading "Foo bar", paragraph "baz"
  1005. We find interpretation 4 most natural, and interpretation 4
  1006. increases the expressive power of CommonMark, by allowing
  1007. multiline headings. Authors who want interpretation 1 can
  1008. put a blank line after the first paragraph:
  1009. ```````````````````````````````` example
  1010. Foo
  1011. bar
  1012. ---
  1013. baz
  1014. .
  1015. <p>Foo</p>
  1016. <h2>bar</h2>
  1017. <p>baz</p>
  1018. ````````````````````````````````
  1019. Authors who want interpretation 2 can put blank lines around
  1020. the thematic break,
  1021. ```````````````````````````````` example
  1022. Foo
  1023. bar
  1024. ---
  1025. baz
  1026. .
  1027. <p>Foo
  1028. bar</p>
  1029. <hr />
  1030. <p>baz</p>
  1031. ````````````````````````````````
  1032. or use a thematic break that cannot count as a [setext heading
  1033. underline], such as
  1034. ```````````````````````````````` example
  1035. Foo
  1036. bar
  1037. * * *
  1038. baz
  1039. .
  1040. <p>Foo
  1041. bar</p>
  1042. <hr />
  1043. <p>baz</p>
  1044. ````````````````````````````````
  1045. Authors who want interpretation 3 can use backslash escapes:
  1046. ```````````````````````````````` example
  1047. Foo
  1048. bar
  1049. \---
  1050. baz
  1051. .
  1052. <p>Foo
  1053. bar
  1054. ---
  1055. baz</p>
  1056. ````````````````````````````````
  1057. ## Indented code blocks
  1058. An [indented code block](@) is composed of one or more
  1059. [indented chunks] separated by blank lines.
  1060. An [indented chunk](@) is a sequence of non-blank lines,
  1061. each indented four or more spaces. The contents of the code block are
  1062. the literal contents of the lines, including trailing
  1063. [line endings], minus four spaces of indentation.
  1064. An indented code block has no [info string].
  1065. An indented code block cannot interrupt a paragraph, so there must be
  1066. a blank line between a paragraph and a following indented code block.
  1067. (A blank line is not needed, however, between a code block and a following
  1068. paragraph.)
  1069. ```````````````````````````````` example
  1070. a simple
  1071. indented code block
  1072. .
  1073. <pre><code>a simple
  1074. indented code block
  1075. </code></pre>
  1076. ````````````````````````````````
  1077. If there is any ambiguity between an interpretation of indentation
  1078. as a code block and as indicating that material belongs to a [list
  1079. item][list items], the list item interpretation takes precedence:
  1080. ```````````````````````````````` example
  1081. - foo
  1082. bar
  1083. .
  1084. <ul>
  1085. <li>
  1086. <p>foo</p>
  1087. <p>bar</p>
  1088. </li>
  1089. </ul>
  1090. ````````````````````````````````
  1091. ```````````````````````````````` example
  1092. 1. foo
  1093. - bar
  1094. .
  1095. <ol>
  1096. <li>
  1097. <p>foo</p>
  1098. <ul>
  1099. <li>bar</li>
  1100. </ul>
  1101. </li>
  1102. </ol>
  1103. ````````````````````````````````
  1104. The contents of a code block are literal text, and do not get parsed
  1105. as Markdown:
  1106. ```````````````````````````````` example
  1107. <a/>
  1108. *hi*
  1109. - one
  1110. .
  1111. <pre><code>&lt;a/&gt;
  1112. *hi*
  1113. - one
  1114. </code></pre>
  1115. ````````````````````````````````
  1116. Here we have three chunks separated by blank lines:
  1117. ```````````````````````````````` example
  1118. chunk1
  1119. chunk2
  1120. chunk3
  1121. .
  1122. <pre><code>chunk1
  1123. chunk2
  1124. chunk3
  1125. </code></pre>
  1126. ````````````````````````````````
  1127. Any initial spaces beyond four will be included in the content, even
  1128. in interior blank lines:
  1129. ```````````````````````````````` example
  1130. chunk1
  1131. chunk2
  1132. .
  1133. <pre><code>chunk1
  1134. chunk2
  1135. </code></pre>
  1136. ````````````````````````````````
  1137. An indented code block cannot interrupt a paragraph. (This
  1138. allows hanging indents and the like.)
  1139. ```````````````````````````````` example
  1140. Foo
  1141. bar
  1142. .
  1143. <p>Foo
  1144. bar</p>
  1145. ````````````````````````````````
  1146. However, any non-blank line with fewer than four leading spaces ends
  1147. the code block immediately. So a paragraph may occur immediately
  1148. after indented code:
  1149. ```````````````````````````````` example
  1150. foo
  1151. bar
  1152. .
  1153. <pre><code>foo
  1154. </code></pre>
  1155. <p>bar</p>
  1156. ````````````````````````````````
  1157. And indented code can occur immediately before and after other kinds of
  1158. blocks:
  1159. ```````````````````````````````` example
  1160. # Heading
  1161. foo
  1162. Heading
  1163. ------
  1164. foo
  1165. ----
  1166. .
  1167. <h1>Heading</h1>
  1168. <pre><code>foo
  1169. </code></pre>
  1170. <h2>Heading</h2>
  1171. <pre><code>foo
  1172. </code></pre>
  1173. <hr />
  1174. ````````````````````````````````
  1175. The first line can be indented more than four spaces:
  1176. ```````````````````````````````` example
  1177. foo
  1178. bar
  1179. .
  1180. <pre><code> foo
  1181. bar
  1182. </code></pre>
  1183. ````````````````````````````````
  1184. Blank lines preceding or following an indented code block
  1185. are not included in it:
  1186. ```````````````````````````````` example
  1187. foo
  1188. .
  1189. <pre><code>foo
  1190. </code></pre>
  1191. ````````````````````````````````
  1192. Trailing spaces are included in the code block's content:
  1193. ```````````````````````````````` example
  1194. foo
  1195. .
  1196. <pre><code>foo
  1197. </code></pre>
  1198. ````````````````````````````````
  1199. ## Fenced code blocks
  1200. A [code fence](@) is a sequence
  1201. of at least three consecutive backtick characters (`` ` ``) or
  1202. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1203. A [fenced code block](@)
  1204. begins with a code fence, indented no more than three spaces.
  1205. The line with the opening code fence may optionally contain some text
  1206. following the code fence; this is trimmed of leading and trailing
  1207. spaces and called the [info string](@).
  1208. The [info string] may not contain any backtick
  1209. characters. (The reason for this restriction is that otherwise
  1210. some inline code would be incorrectly interpreted as the
  1211. beginning of a fenced code block.)
  1212. The content of the code block consists of all subsequent lines, until
  1213. a closing [code fence] of the same type as the code block
  1214. began with (backticks or tildes), and with at least as many backticks
  1215. or tildes as the opening code fence. If the leading code fence is
  1216. indented N spaces, then up to N spaces of indentation are removed from
  1217. each line of the content (if present). (If a content line is not
  1218. indented, it is preserved unchanged. If it is indented less than N
  1219. spaces, all of the indentation is removed.)
  1220. The closing code fence may be indented up to three spaces, and may be
  1221. followed only by spaces, which are ignored. If the end of the
  1222. containing block (or document) is reached and no closing code fence
  1223. has been found, the code block contains all of the lines after the
  1224. opening code fence until the end of the containing block (or
  1225. document). (An alternative spec would require backtracking in the
  1226. event that a closing code fence is not found. But this makes parsing
  1227. much less efficient, and there seems to be no real down side to the
  1228. behavior described here.)
  1229. A fenced code block may interrupt a paragraph, and does not require
  1230. a blank line either before or after.
  1231. The content of a code fence is treated as literal text, not parsed
  1232. as inlines. The first word of the [info string] is typically used to
  1233. specify the language of the code sample, and rendered in the `class`
  1234. attribute of the `code` tag. However, this spec does not mandate any
  1235. particular treatment of the [info string].
  1236. Here is a simple example with backticks:
  1237. ```````````````````````````````` example
  1238. ```
  1239. <
  1240. >
  1241. ```
  1242. .
  1243. <pre><code>&lt;
  1244. &gt;
  1245. </code></pre>
  1246. ````````````````````````````````
  1247. With tildes:
  1248. ```````````````````````````````` example
  1249. ~~~
  1250. <
  1251. >
  1252. ~~~
  1253. .
  1254. <pre><code>&lt;
  1255. &gt;
  1256. </code></pre>
  1257. ````````````````````````````````
  1258. The closing code fence must use the same character as the opening
  1259. fence:
  1260. ```````````````````````````````` example
  1261. ```
  1262. aaa
  1263. ~~~
  1264. ```
  1265. .
  1266. <pre><code>aaa
  1267. ~~~
  1268. </code></pre>
  1269. ````````````````````````````````
  1270. ```````````````````````````````` example
  1271. ~~~
  1272. aaa
  1273. ```
  1274. ~~~
  1275. .
  1276. <pre><code>aaa
  1277. ```
  1278. </code></pre>
  1279. ````````````````````````````````
  1280. The closing code fence must be at least as long as the opening fence:
  1281. ```````````````````````````````` example
  1282. ````
  1283. aaa
  1284. ```
  1285. ``````
  1286. .
  1287. <pre><code>aaa
  1288. ```
  1289. </code></pre>
  1290. ````````````````````````````````
  1291. ```````````````````````````````` example
  1292. ~~~~
  1293. aaa
  1294. ~~~
  1295. ~~~~
  1296. .
  1297. <pre><code>aaa
  1298. ~~~
  1299. </code></pre>
  1300. ````````````````````````````````
  1301. Unclosed code blocks are closed by the end of the document
  1302. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1303. ```````````````````````````````` example
  1304. ```
  1305. .
  1306. <pre><code></code></pre>
  1307. ````````````````````````````````
  1308. ```````````````````````````````` example
  1309. `````
  1310. ```
  1311. aaa
  1312. .
  1313. <pre><code>
  1314. ```
  1315. aaa
  1316. </code></pre>
  1317. ````````````````````````````````
  1318. ```````````````````````````````` example
  1319. > ```
  1320. > aaa
  1321. bbb
  1322. .
  1323. <blockquote>
  1324. <pre><code>aaa
  1325. </code></pre>
  1326. </blockquote>
  1327. <p>bbb</p>
  1328. ````````````````````````````````
  1329. A code block can have all empty lines as its content:
  1330. ```````````````````````````````` example
  1331. ```
  1332. ```
  1333. .
  1334. <pre><code>
  1335. </code></pre>
  1336. ````````````````````````````````
  1337. A code block can be empty:
  1338. ```````````````````````````````` example
  1339. ```
  1340. ```
  1341. .
  1342. <pre><code></code></pre>
  1343. ````````````````````````````````
  1344. Fences can be indented. If the opening fence is indented,
  1345. content lines will have equivalent opening indentation removed,
  1346. if present:
  1347. ```````````````````````````````` example
  1348. ```
  1349. aaa
  1350. aaa
  1351. ```
  1352. .
  1353. <pre><code>aaa
  1354. aaa
  1355. </code></pre>
  1356. ````````````````````````````````
  1357. ```````````````````````````````` example
  1358. ```
  1359. aaa
  1360. aaa
  1361. aaa
  1362. ```
  1363. .
  1364. <pre><code>aaa
  1365. aaa
  1366. aaa
  1367. </code></pre>
  1368. ````````````````````````````````
  1369. ```````````````````````````````` example
  1370. ```
  1371. aaa
  1372. aaa
  1373. aaa
  1374. ```
  1375. .
  1376. <pre><code>aaa
  1377. aaa
  1378. aaa
  1379. </code></pre>
  1380. ````````````````````````````````
  1381. Four spaces indentation produces an indented code block:
  1382. ```````````````````````````````` example
  1383. ```
  1384. aaa
  1385. ```
  1386. .
  1387. <pre><code>```
  1388. aaa
  1389. ```
  1390. </code></pre>
  1391. ````````````````````````````````
  1392. Closing fences may be indented by 0-3 spaces, and their indentation
  1393. need not match that of the opening fence:
  1394. ```````````````````````````````` example
  1395. ```
  1396. aaa
  1397. ```
  1398. .
  1399. <pre><code>aaa
  1400. </code></pre>
  1401. ````````````````````````````````
  1402. ```````````````````````````````` example
  1403. ```
  1404. aaa
  1405. ```
  1406. .
  1407. <pre><code>aaa
  1408. </code></pre>
  1409. ````````````````````````````````
  1410. This is not a closing fence, because it is indented 4 spaces:
  1411. ```````````````````````````````` example
  1412. ```
  1413. aaa
  1414. ```
  1415. .
  1416. <pre><code>aaa
  1417. ```
  1418. </code></pre>
  1419. ````````````````````````````````
  1420. Code fences (opening and closing) cannot contain internal spaces:
  1421. ```````````````````````````````` example
  1422. ``` ```
  1423. aaa
  1424. .
  1425. <p><code></code>
  1426. aaa</p>
  1427. ````````````````````````````````
  1428. ```````````````````````````````` example
  1429. ~~~~~~
  1430. aaa
  1431. ~~~ ~~
  1432. .
  1433. <pre><code>aaa
  1434. ~~~ ~~
  1435. </code></pre>
  1436. ````````````````````````````````
  1437. Fenced code blocks can interrupt paragraphs, and can be followed
  1438. directly by paragraphs, without a blank line between:
  1439. ```````````````````````````````` example
  1440. foo
  1441. ```
  1442. bar
  1443. ```
  1444. baz
  1445. .
  1446. <p>foo</p>
  1447. <pre><code>bar
  1448. </code></pre>
  1449. <p>baz</p>
  1450. ````````````````````````````````
  1451. Other blocks can also occur before and after fenced code blocks
  1452. without an intervening blank line:
  1453. ```````````````````````````````` example
  1454. foo
  1455. ---
  1456. ~~~
  1457. bar
  1458. ~~~
  1459. # baz
  1460. .
  1461. <h2>foo</h2>
  1462. <pre><code>bar
  1463. </code></pre>
  1464. <h1>baz</h1>
  1465. ````````````````````````````````
  1466. An [info string] can be provided after the opening code fence.
  1467. Opening and closing spaces will be stripped, and the first word, prefixed
  1468. with `language-`, is used as the value for the `class` attribute of the
  1469. `code` element within the enclosing `pre` element.
  1470. ```````````````````````````````` example
  1471. ```ruby
  1472. def foo(x)
  1473. return 3
  1474. end
  1475. ```
  1476. .
  1477. <pre><code class="language-ruby">def foo(x)
  1478. return 3
  1479. end
  1480. </code></pre>
  1481. ````````````````````````````````
  1482. ```````````````````````````````` example
  1483. ~~~~ ruby startline=3 $%@#$
  1484. def foo(x)
  1485. return 3
  1486. end
  1487. ~~~~~~~
  1488. .
  1489. <pre><code class="language-ruby">def foo(x)
  1490. return 3
  1491. end
  1492. </code></pre>
  1493. ````````````````````````````````
  1494. ```````````````````````````````` example
  1495. ````;
  1496. ````
  1497. .
  1498. <pre><code class="language-;"></code></pre>
  1499. ````````````````````````````````
  1500. [Info strings] for backtick code blocks cannot contain backticks:
  1501. ```````````````````````````````` example
  1502. ``` aa ```
  1503. foo
  1504. .
  1505. <p><code>aa</code>
  1506. foo</p>
  1507. ````````````````````````````````
  1508. Closing code fences cannot have [info strings]:
  1509. ```````````````````````````````` example
  1510. ```
  1511. ``` aaa
  1512. ```
  1513. .
  1514. <pre><code>``` aaa
  1515. </code></pre>
  1516. ````````````````````````````````
  1517. ## HTML blocks
  1518. An [HTML block](@) is a group of lines that is treated
  1519. as raw HTML (and will not be escaped in HTML output).
  1520. There are seven kinds of [HTML block], which can be defined
  1521. by their start and end conditions. The block begins with a line that
  1522. meets a [start condition](@) (after up to three spaces
  1523. optional indentation). It ends with the first subsequent line that
  1524. meets a matching [end condition](@), or the last line of
  1525. the document or other [container block]), if no line is encountered that meets the
  1526. [end condition]. If the first line meets both the [start condition]
  1527. and the [end condition], the block will contain just that line.
  1528. 1. **Start condition:** line begins with the string `<script`,
  1529. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1530. the string `>`, or the end of the line.\
  1531. **End condition:** line contains an end tag
  1532. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1533. need not match the start tag).
  1534. 2. **Start condition:** line begins with the string `<!--`.\
  1535. **End condition:** line contains the string `-->`.
  1536. 3. **Start condition:** line begins with the string `<?`.\
  1537. **End condition:** line contains the string `?>`.
  1538. 4. **Start condition:** line begins with the string `<!`
  1539. followed by an uppercase ASCII letter.\
  1540. **End condition:** line contains the character `>`.
  1541. 5. **Start condition:** line begins with the string
  1542. `<![CDATA[`.\
  1543. **End condition:** line contains the string `]]>`.
  1544. 6. **Start condition:** line begins the string `<` or `</`
  1545. followed by one of the strings (case-insensitive) `address`,
  1546. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1547. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1548. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1549. `footer`, `form`, `frame`, `frameset`,
  1550. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1551. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1552. `meta`, `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1553. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1554. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1555. by [whitespace], the end of the line, the string `>`, or
  1556. the string `/>`.\
  1557. **End condition:** line is followed by a [blank line].
  1558. 7. **Start condition:** line begins with a complete [open tag]
  1559. or [closing tag] (with any [tag name] other than `script`,
  1560. `style`, or `pre`) followed only by [whitespace]
  1561. or the end of the line.\
  1562. **End condition:** line is followed by a [blank line].
  1563. HTML blocks continue until they are closed by their appropriate
  1564. [end condition], or the last line of the document or other [container block].
  1565. This means any HTML **within an HTML block** that might otherwise be recognised
  1566. as a start condition will be ignored by the parser and passed through as-is,
  1567. without changing the parser's state.
  1568. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1569. the parser state; as the HTML block was started in by start condition 6, it
  1570. will end at any blank line. This can be surprising:
  1571. ```````````````````````````````` example
  1572. <table><tr><td>
  1573. <pre>
  1574. **Hello**,
  1575. _world_.
  1576. </pre>
  1577. </td></tr></table>
  1578. .
  1579. <table><tr><td>
  1580. <pre>
  1581. **Hello**,
  1582. <p><em>world</em>.
  1583. </pre></p>
  1584. </td></tr></table>
  1585. ````````````````````````````````
  1586. In this case, the HTML block is terminated by the newline — the `**hello**`
  1587. text remains verbatim — and regular parsing resumes, with a paragraph,
  1588. emphasised `world` and inline and block HTML following.
  1589. All types of [HTML blocks] except type 7 may interrupt
  1590. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1591. (This restriction is intended to prevent unwanted interpretation
  1592. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1593. Some simple examples follow. Here are some basic HTML blocks
  1594. of type 6:
  1595. ```````````````````````````````` example
  1596. <table>
  1597. <tr>
  1598. <td>
  1599. hi
  1600. </td>
  1601. </tr>
  1602. </table>
  1603. okay.
  1604. .
  1605. <table>
  1606. <tr>
  1607. <td>
  1608. hi
  1609. </td>
  1610. </tr>
  1611. </table>
  1612. <p>okay.</p>
  1613. ````````````````````````````````
  1614. ```````````````````````````````` example
  1615. <div>
  1616. *hello*
  1617. <foo><a>
  1618. .
  1619. <div>
  1620. *hello*
  1621. <foo><a>
  1622. ````````````````````````````````
  1623. A block can also start with a closing tag:
  1624. ```````````````````````````````` example
  1625. </div>
  1626. *foo*
  1627. .
  1628. </div>
  1629. *foo*
  1630. ````````````````````````````````
  1631. Here we have two HTML blocks with a Markdown paragraph between them:
  1632. ```````````````````````````````` example
  1633. <DIV CLASS="foo">
  1634. *Markdown*
  1635. </DIV>
  1636. .
  1637. <DIV CLASS="foo">
  1638. <p><em>Markdown</em></p>
  1639. </DIV>
  1640. ````````````````````````````````
  1641. The tag on the first line can be partial, as long
  1642. as it is split where there would be whitespace:
  1643. ```````````````````````````````` example
  1644. <div id="foo"
  1645. class="bar">
  1646. </div>
  1647. .
  1648. <div id="foo"
  1649. class="bar">
  1650. </div>
  1651. ````````````````````````````````
  1652. ```````````````````````````````` example
  1653. <div id="foo" class="bar
  1654. baz">
  1655. </div>
  1656. .
  1657. <div id="foo" class="bar
  1658. baz">
  1659. </div>
  1660. ````````````````````````````````
  1661. An open tag need not be closed:
  1662. ```````````````````````````````` example
  1663. <div>
  1664. *foo*
  1665. *bar*
  1666. .
  1667. <div>
  1668. *foo*
  1669. <p><em>bar</em></p>
  1670. ````````````````````````````````
  1671. A partial tag need not even be completed (garbage
  1672. in, garbage out):
  1673. ```````````````````````````````` example
  1674. <div id="foo"
  1675. *hi*
  1676. .
  1677. <div id="foo"
  1678. *hi*
  1679. ````````````````````````````````
  1680. ```````````````````````````````` example
  1681. <div class
  1682. foo
  1683. .
  1684. <div class
  1685. foo
  1686. ````````````````````````````````
  1687. The initial tag doesn't even need to be a valid
  1688. tag, as long as it starts like one:
  1689. ```````````````````````````````` example
  1690. <div *???-&&&-<---
  1691. *foo*
  1692. .
  1693. <div *???-&&&-<---
  1694. *foo*
  1695. ````````````````````````````````
  1696. In type 6 blocks, the initial tag need not be on a line by
  1697. itself:
  1698. ```````````````````````````````` example
  1699. <div><a href="bar">*foo*</a></div>
  1700. .
  1701. <div><a href="bar">*foo*</a></div>
  1702. ````````````````````````````````
  1703. ```````````````````````````````` example
  1704. <table><tr><td>
  1705. foo
  1706. </td></tr></table>
  1707. .
  1708. <table><tr><td>
  1709. foo
  1710. </td></tr></table>
  1711. ````````````````````````````````
  1712. Everything until the next blank line or end of document
  1713. gets included in the HTML block. So, in the following
  1714. example, what looks like a Markdown code block
  1715. is actually part of the HTML block, which continues until a blank
  1716. line or the end of the document is reached:
  1717. ```````````````````````````````` example
  1718. <div></div>
  1719. ``` c
  1720. int x = 33;
  1721. ```
  1722. .
  1723. <div></div>
  1724. ``` c
  1725. int x = 33;
  1726. ```
  1727. ````````````````````````````````
  1728. To start an [HTML block] with a tag that is *not* in the
  1729. list of block-level tags in (6), you must put the tag by
  1730. itself on the first line (and it must be complete):
  1731. ```````````````````````````````` example
  1732. <a href="foo">
  1733. *bar*
  1734. </a>
  1735. .
  1736. <a href="foo">
  1737. *bar*
  1738. </a>
  1739. ````````````````````````````````
  1740. In type 7 blocks, the [tag name] can be anything:
  1741. ```````````````````````````````` example
  1742. <Warning>
  1743. *bar*
  1744. </Warning>
  1745. .
  1746. <Warning>
  1747. *bar*
  1748. </Warning>
  1749. ````````````````````````````````
  1750. ```````````````````````````````` example
  1751. <i class="foo">
  1752. *bar*
  1753. </i>
  1754. .
  1755. <i class="foo">
  1756. *bar*
  1757. </i>
  1758. ````````````````````````````````
  1759. ```````````````````````````````` example
  1760. </ins>
  1761. *bar*
  1762. .
  1763. </ins>
  1764. *bar*
  1765. ````````````````````````````````
  1766. These rules are designed to allow us to work with tags that
  1767. can function as either block-level or inline-level tags.
  1768. The `<del>` tag is a nice example. We can surround content with
  1769. `<del>` tags in three different ways. In this case, we get a raw
  1770. HTML block, because the `<del>` tag is on a line by itself:
  1771. ```````````````````````````````` example
  1772. <del>
  1773. *foo*
  1774. </del>
  1775. .
  1776. <del>
  1777. *foo*
  1778. </del>
  1779. ````````````````````````````````
  1780. In this case, we get a raw HTML block that just includes
  1781. the `<del>` tag (because it ends with the following blank
  1782. line). So the contents get interpreted as CommonMark:
  1783. ```````````````````````````````` example
  1784. <del>
  1785. *foo*
  1786. </del>
  1787. .
  1788. <del>
  1789. <p><em>foo</em></p>
  1790. </del>
  1791. ````````````````````````````````
  1792. Finally, in this case, the `<del>` tags are interpreted
  1793. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1794. the tag is not on a line by itself, we get inline HTML
  1795. rather than an [HTML block].)
  1796. ```````````````````````````````` example
  1797. <del>*foo*</del>
  1798. .
  1799. <p><del><em>foo</em></del></p>
  1800. ````````````````````````````````
  1801. HTML tags designed to contain literal content
  1802. (`script`, `style`, `pre`), comments, processing instructions,
  1803. and declarations are treated somewhat differently.
  1804. Instead of ending at the first blank line, these blocks
  1805. end at the first line containing a corresponding end tag.
  1806. As a result, these blocks can contain blank lines:
  1807. A pre tag (type 1):
  1808. ```````````````````````````````` example
  1809. <pre language="haskell"><code>
  1810. import Text.HTML.TagSoup
  1811. main :: IO ()
  1812. main = print $ parseTags tags
  1813. </code></pre>
  1814. okay
  1815. .
  1816. <pre language="haskell"><code>
  1817. import Text.HTML.TagSoup
  1818. main :: IO ()
  1819. main = print $ parseTags tags
  1820. </code></pre>
  1821. <p>okay</p>
  1822. ````````````````````````````````
  1823. A script tag (type 1):
  1824. ```````````````````````````````` example
  1825. <script type="text/javascript">
  1826. // JavaScript example
  1827. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1828. </script>
  1829. okay
  1830. .
  1831. <script type="text/javascript">
  1832. // JavaScript example
  1833. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1834. </script>
  1835. <p>okay</p>
  1836. ````````````````````````````````
  1837. A style tag (type 1):
  1838. ```````````````````````````````` example
  1839. <style
  1840. type="text/css">
  1841. h1 {color:red;}
  1842. p {color:blue;}
  1843. </style>
  1844. okay
  1845. .
  1846. <style
  1847. type="text/css">
  1848. h1 {color:red;}
  1849. p {color:blue;}
  1850. </style>
  1851. <p>okay</p>
  1852. ````````````````````````````````
  1853. If there is no matching end tag, the block will end at the
  1854. end of the document (or the enclosing [block quote][block quotes]
  1855. or [list item][list items]):
  1856. ```````````````````````````````` example
  1857. <style
  1858. type="text/css">
  1859. foo
  1860. .
  1861. <style
  1862. type="text/css">
  1863. foo
  1864. ````````````````````````````````
  1865. ```````````````````````````````` example
  1866. > <div>
  1867. > foo
  1868. bar
  1869. .
  1870. <blockquote>
  1871. <div>
  1872. foo
  1873. </blockquote>
  1874. <p>bar</p>
  1875. ````````````````````````````````
  1876. ```````````````````````````````` example
  1877. - <div>
  1878. - foo
  1879. .
  1880. <ul>
  1881. <li>
  1882. <div>
  1883. </li>
  1884. <li>foo</li>
  1885. </ul>
  1886. ````````````````````````````````
  1887. The end tag can occur on the same line as the start tag:
  1888. ```````````````````````````````` example
  1889. <style>p{color:red;}</style>
  1890. *foo*
  1891. .
  1892. <style>p{color:red;}</style>
  1893. <p><em>foo</em></p>
  1894. ````````````````````````````````
  1895. ```````````````````````````````` example
  1896. <!-- foo -->*bar*
  1897. *baz*
  1898. .
  1899. <!-- foo -->*bar*
  1900. <p><em>baz</em></p>
  1901. ````````````````````````````````
  1902. Note that anything on the last line after the
  1903. end tag will be included in the [HTML block]:
  1904. ```````````````````````````````` example
  1905. <script>
  1906. foo
  1907. </script>1. *bar*
  1908. .
  1909. <script>
  1910. foo
  1911. </script>1. *bar*
  1912. ````````````````````````````````
  1913. A comment (type 2):
  1914. ```````````````````````````````` example
  1915. <!-- Foo
  1916. bar
  1917. baz -->
  1918. okay
  1919. .
  1920. <!-- Foo
  1921. bar
  1922. baz -->
  1923. <p>okay</p>
  1924. ````````````````````````````````
  1925. A processing instruction (type 3):
  1926. ```````````````````````````````` example
  1927. <?php
  1928. echo '>';
  1929. ?>
  1930. okay
  1931. .
  1932. <?php
  1933. echo '>';
  1934. ?>
  1935. <p>okay</p>
  1936. ````````````````````````````````
  1937. A declaration (type 4):
  1938. ```````````````````````````````` example
  1939. <!DOCTYPE html>
  1940. .
  1941. <!DOCTYPE html>
  1942. ````````````````````````````````
  1943. CDATA (type 5):
  1944. ```````````````````````````````` example
  1945. <![CDATA[
  1946. function matchwo(a,b)
  1947. {
  1948. if (a < b && a < 0) then {
  1949. return 1;
  1950. } else {
  1951. return 0;
  1952. }
  1953. }
  1954. ]]>
  1955. okay
  1956. .
  1957. <![CDATA[
  1958. function matchwo(a,b)
  1959. {
  1960. if (a < b && a < 0) then {
  1961. return 1;
  1962. } else {
  1963. return 0;
  1964. }
  1965. }
  1966. ]]>
  1967. <p>okay</p>
  1968. ````````````````````````````````
  1969. The opening tag can be indented 1-3 spaces, but not 4:
  1970. ```````````````````````````````` example
  1971. <!-- foo -->
  1972. <!-- foo -->
  1973. .
  1974. <!-- foo -->
  1975. <pre><code>&lt;!-- foo --&gt;
  1976. </code></pre>
  1977. ````````````````````````````````
  1978. ```````````````````````````````` example
  1979. <div>
  1980. <div>
  1981. .
  1982. <div>
  1983. <pre><code>&lt;div&gt;
  1984. </code></pre>
  1985. ````````````````````````````````
  1986. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  1987. preceded by a blank line.
  1988. ```````````````````````````````` example
  1989. Foo
  1990. <div>
  1991. bar
  1992. </div>
  1993. .
  1994. <p>Foo</p>
  1995. <div>
  1996. bar
  1997. </div>
  1998. ````````````````````````````````
  1999. However, a following blank line is needed, except at the end of
  2000. a document, and except for blocks of types 1--5, above:
  2001. ```````````````````````````````` example
  2002. <div>
  2003. bar
  2004. </div>
  2005. *foo*
  2006. .
  2007. <div>
  2008. bar
  2009. </div>
  2010. *foo*
  2011. ````````````````````````````````
  2012. HTML blocks of type 7 cannot interrupt a paragraph:
  2013. ```````````````````````````````` example
  2014. Foo
  2015. <a href="bar">
  2016. baz
  2017. .
  2018. <p>Foo
  2019. <a href="bar">
  2020. baz</p>
  2021. ````````````````````````````````
  2022. This rule differs from John Gruber's original Markdown syntax
  2023. specification, which says:
  2024. > The only restrictions are that block-level HTML elements —
  2025. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2026. > surrounding content by blank lines, and the start and end tags of the
  2027. > block should not be indented with tabs or spaces.
  2028. In some ways Gruber's rule is more restrictive than the one given
  2029. here:
  2030. - It requires that an HTML block be preceded by a blank line.
  2031. - It does not allow the start tag to be indented.
  2032. - It requires a matching end tag, which it also does not allow to
  2033. be indented.
  2034. Most Markdown implementations (including some of Gruber's own) do not
  2035. respect all of these restrictions.
  2036. There is one respect, however, in which Gruber's rule is more liberal
  2037. than the one given here, since it allows blank lines to occur inside
  2038. an HTML block. There are two reasons for disallowing them here.
  2039. First, it removes the need to parse balanced tags, which is
  2040. expensive and can require backtracking from the end of the document
  2041. if no matching end tag is found. Second, it provides a very simple
  2042. and flexible way of including Markdown content inside HTML tags:
  2043. simply separate the Markdown from the HTML using blank lines:
  2044. Compare:
  2045. ```````````````````````````````` example
  2046. <div>
  2047. *Emphasized* text.
  2048. </div>
  2049. .
  2050. <div>
  2051. <p><em>Emphasized</em> text.</p>
  2052. </div>
  2053. ````````````````````````````````
  2054. ```````````````````````````````` example
  2055. <div>
  2056. *Emphasized* text.
  2057. </div>
  2058. .
  2059. <div>
  2060. *Emphasized* text.
  2061. </div>
  2062. ````````````````````````````````
  2063. Some Markdown implementations have adopted a convention of
  2064. interpreting content inside tags as text if the open tag has
  2065. the attribute `markdown=1`. The rule given above seems a simpler and
  2066. more elegant way of achieving the same expressive power, which is also
  2067. much simpler to parse.
  2068. The main potential drawback is that one can no longer paste HTML
  2069. blocks into Markdown documents with 100% reliability. However,
  2070. *in most cases* this will work fine, because the blank lines in
  2071. HTML are usually followed by HTML block tags. For example:
  2072. ```````````````````````````````` example
  2073. <table>
  2074. <tr>
  2075. <td>
  2076. Hi
  2077. </td>
  2078. </tr>
  2079. </table>
  2080. .
  2081. <table>
  2082. <tr>
  2083. <td>
  2084. Hi
  2085. </td>
  2086. </tr>
  2087. </table>
  2088. ````````````````````````````````
  2089. There are problems, however, if the inner tags are indented
  2090. *and* separated by spaces, as then they will be interpreted as
  2091. an indented code block:
  2092. ```````````````````````````````` example
  2093. <table>
  2094. <tr>
  2095. <td>
  2096. Hi
  2097. </td>
  2098. </tr>
  2099. </table>
  2100. .
  2101. <table>
  2102. <tr>
  2103. <pre><code>&lt;td&gt;
  2104. Hi
  2105. &lt;/td&gt;
  2106. </code></pre>
  2107. </tr>
  2108. </table>
  2109. ````````````````````````````````
  2110. Fortunately, blank lines are usually not necessary and can be
  2111. deleted. The exception is inside `<pre>` tags, but as described
  2112. above, raw HTML blocks starting with `<pre>` *can* contain blank
  2113. lines.
  2114. ## Link reference definitions
  2115. A [link reference definition](@)
  2116. consists of a [link label], indented up to three spaces, followed
  2117. by a colon (`:`), optional [whitespace] (including up to one
  2118. [line ending]), a [link destination],
  2119. optional [whitespace] (including up to one
  2120. [line ending]), and an optional [link
  2121. title], which if it is present must be separated
  2122. from the [link destination] by [whitespace].
  2123. No further [non-whitespace characters] may occur on the line.
  2124. A [link reference definition]
  2125. does not correspond to a structural element of a document. Instead, it
  2126. defines a label which can be used in [reference links]
  2127. and reference-style [images] elsewhere in the document. [Link
  2128. reference definitions] can come either before or after the links that use
  2129. them.
  2130. ```````````````````````````````` example
  2131. [foo]: /url "title"
  2132. [foo]
  2133. .
  2134. <p><a href="/url" title="title">foo</a></p>
  2135. ````````````````````````````````
  2136. ```````````````````````````````` example
  2137. [foo]:
  2138. /url
  2139. 'the title'
  2140. [foo]
  2141. .
  2142. <p><a href="/url" title="the title">foo</a></p>
  2143. ````````````````````````````````
  2144. ```````````````````````````````` example
  2145. [Foo*bar\]]:my_(url) 'title (with parens)'
  2146. [Foo*bar\]]
  2147. .
  2148. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2149. ````````````````````````````````
  2150. ```````````````````````````````` example
  2151. [Foo bar]:
  2152. <my%20url>
  2153. 'title'
  2154. [Foo bar]
  2155. .
  2156. <p><a href="my%20url" title="title">Foo bar</a></p>
  2157. ````````````````````````````````
  2158. The title may extend over multiple lines:
  2159. ```````````````````````````````` example
  2160. [foo]: /url '
  2161. title
  2162. line1
  2163. line2
  2164. '
  2165. [foo]
  2166. .
  2167. <p><a href="/url" title="
  2168. title
  2169. line1
  2170. line2
  2171. ">foo</a></p>
  2172. ````````````````````````````````
  2173. However, it may not contain a [blank line]:
  2174. ```````````````````````````````` example
  2175. [foo]: /url 'title
  2176. with blank line'
  2177. [foo]
  2178. .
  2179. <p>[foo]: /url 'title</p>
  2180. <p>with blank line'</p>
  2181. <p>[foo]</p>
  2182. ````````````````````````````````
  2183. The title may be omitted:
  2184. ```````````````````````````````` example
  2185. [foo]:
  2186. /url
  2187. [foo]
  2188. .
  2189. <p><a href="/url">foo</a></p>
  2190. ````````````````````````````````
  2191. The link destination may not be omitted:
  2192. ```````````````````````````````` example
  2193. [foo]:
  2194. [foo]
  2195. .
  2196. <p>[foo]:</p>
  2197. <p>[foo]</p>
  2198. ````````````````````````````````
  2199. Both title and destination can contain backslash escapes
  2200. and literal backslashes:
  2201. ```````````````````````````````` example
  2202. [foo]: /url\bar\*baz "foo\"bar\baz"
  2203. [foo]
  2204. .
  2205. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2206. ````````````````````````````````
  2207. A link can come before its corresponding definition:
  2208. ```````````````````````````````` example
  2209. [foo]
  2210. [foo]: url
  2211. .
  2212. <p><a href="url">foo</a></p>
  2213. ````````````````````````````````
  2214. If there are several matching definitions, the first one takes
  2215. precedence:
  2216. ```````````````````````````````` example
  2217. [foo]
  2218. [foo]: first
  2219. [foo]: second
  2220. .
  2221. <p><a href="first">foo</a></p>
  2222. ````````````````````````````````
  2223. As noted in the section on [Links], matching of labels is
  2224. case-insensitive (see [matches]).
  2225. ```````````````````````````````` example
  2226. [FOO]: /url
  2227. [Foo]
  2228. .
  2229. <p><a href="/url">Foo</a></p>
  2230. ````````````````````````````````
  2231. ```````````````````````````````` example
  2232. [ΑΓΩ]: /φου
  2233. [αγω]
  2234. .
  2235. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2236. ````````````````````````````````
  2237. Here is a link reference definition with no corresponding link.
  2238. It contributes nothing to the document.
  2239. ```````````````````````````````` example
  2240. [foo]: /url
  2241. .
  2242. ````````````````````````````````
  2243. Here is another one:
  2244. ```````````````````````````````` example
  2245. [
  2246. foo
  2247. ]: /url
  2248. bar
  2249. .
  2250. <p>bar</p>
  2251. ````````````````````````````````
  2252. This is not a link reference definition, because there are
  2253. [non-whitespace characters] after the title:
  2254. ```````````````````````````````` example
  2255. [foo]: /url "title" ok
  2256. .
  2257. <p>[foo]: /url &quot;title&quot; ok</p>
  2258. ````````````````````````````````
  2259. This is a link reference definition, but it has no title:
  2260. ```````````````````````````````` example
  2261. [foo]: /url
  2262. "title" ok
  2263. .
  2264. <p>&quot;title&quot; ok</p>
  2265. ````````````````````````````````
  2266. This is not a link reference definition, because it is indented
  2267. four spaces:
  2268. ```````````````````````````````` example
  2269. [foo]: /url "title"
  2270. [foo]
  2271. .
  2272. <pre><code>[foo]: /url &quot;title&quot;
  2273. </code></pre>
  2274. <p>[foo]</p>
  2275. ````````````````````````````````
  2276. This is not a link reference definition, because it occurs inside
  2277. a code block:
  2278. ```````````````````````````````` example
  2279. ```
  2280. [foo]: /url
  2281. ```
  2282. [foo]
  2283. .
  2284. <pre><code>[foo]: /url
  2285. </code></pre>
  2286. <p>[foo]</p>
  2287. ````````````````````````````````
  2288. A [link reference definition] cannot interrupt a paragraph.
  2289. ```````````````````````````````` example
  2290. Foo
  2291. [bar]: /baz
  2292. [bar]
  2293. .
  2294. <p>Foo
  2295. [bar]: /baz</p>
  2296. <p>[bar]</p>
  2297. ````````````````````````````````
  2298. However, it can directly follow other block elements, such as headings
  2299. and thematic breaks, and it need not be followed by a blank line.
  2300. ```````````````````````````````` example
  2301. # [Foo]
  2302. [foo]: /url
  2303. > bar
  2304. .
  2305. <h1><a href="/url">Foo</a></h1>
  2306. <blockquote>
  2307. <p>bar</p>
  2308. </blockquote>
  2309. ````````````````````````````````
  2310. Several [link reference definitions]
  2311. can occur one after another, without intervening blank lines.
  2312. ```````````````````````````````` example
  2313. [foo]: /foo-url "foo"
  2314. [bar]: /bar-url
  2315. "bar"
  2316. [baz]: /baz-url
  2317. [foo],
  2318. [bar],
  2319. [baz]
  2320. .
  2321. <p><a href="/foo-url" title="foo">foo</a>,
  2322. <a href="/bar-url" title="bar">bar</a>,
  2323. <a href="/baz-url">baz</a></p>
  2324. ````````````````````````````````
  2325. [Link reference definitions] can occur
  2326. inside block containers, like lists and block quotations. They
  2327. affect the entire document, not just the container in which they
  2328. are defined:
  2329. ```````````````````````````````` example
  2330. [foo]
  2331. > [foo]: /url
  2332. .
  2333. <p><a href="/url">foo</a></p>
  2334. <blockquote>
  2335. </blockquote>
  2336. ````````````````````````````````
  2337. ## Paragraphs
  2338. A sequence of non-blank lines that cannot be interpreted as other
  2339. kinds of blocks forms a [paragraph](@).
  2340. The contents of the paragraph are the result of parsing the
  2341. paragraph's raw content as inlines. The paragraph's raw content
  2342. is formed by concatenating the lines and removing initial and final
  2343. [whitespace].
  2344. A simple example with two paragraphs:
  2345. ```````````````````````````````` example
  2346. aaa
  2347. bbb
  2348. .
  2349. <p>aaa</p>
  2350. <p>bbb</p>
  2351. ````````````````````````````````
  2352. Paragraphs can contain multiple lines, but no blank lines:
  2353. ```````````````````````````````` example
  2354. aaa
  2355. bbb
  2356. ccc
  2357. ddd
  2358. .
  2359. <p>aaa
  2360. bbb</p>
  2361. <p>ccc
  2362. ddd</p>
  2363. ````````````````````````````````
  2364. Multiple blank lines between paragraph have no effect:
  2365. ```````````````````````````````` example
  2366. aaa
  2367. bbb
  2368. .
  2369. <p>aaa</p>
  2370. <p>bbb</p>
  2371. ````````````````````````````````
  2372. Leading spaces are skipped:
  2373. ```````````````````````````````` example
  2374. aaa
  2375. bbb
  2376. .
  2377. <p>aaa
  2378. bbb</p>
  2379. ````````````````````````````````
  2380. Lines after the first may be indented any amount, since indented
  2381. code blocks cannot interrupt paragraphs.
  2382. ```````````````````````````````` example
  2383. aaa
  2384. bbb
  2385. ccc
  2386. .
  2387. <p>aaa
  2388. bbb
  2389. ccc</p>
  2390. ````````````````````````````````
  2391. However, the first line may be indented at most three spaces,
  2392. or an indented code block will be triggered:
  2393. ```````````````````````````````` example
  2394. aaa
  2395. bbb
  2396. .
  2397. <p>aaa
  2398. bbb</p>
  2399. ````````````````````````````````
  2400. ```````````````````````````````` example
  2401. aaa
  2402. bbb
  2403. .
  2404. <pre><code>aaa
  2405. </code></pre>
  2406. <p>bbb</p>
  2407. ````````````````````````````````
  2408. Final spaces are stripped before inline parsing, so a paragraph
  2409. that ends with two or more spaces will not end with a [hard line
  2410. break]:
  2411. ```````````````````````````````` example
  2412. aaa
  2413. bbb
  2414. .
  2415. <p>aaa<br />
  2416. bbb</p>
  2417. ````````````````````````````````
  2418. ## Blank lines
  2419. [Blank lines] between block-level elements are ignored,
  2420. except for the role they play in determining whether a [list]
  2421. is [tight] or [loose].
  2422. Blank lines at the beginning and end of the document are also ignored.
  2423. ```````````````````````````````` example
  2424. aaa
  2425. # aaa
  2426. .
  2427. <p>aaa</p>
  2428. <h1>aaa</h1>
  2429. ````````````````````````````````
  2430. # Container blocks
  2431. A [container block] is a block that has other
  2432. blocks as its contents. There are two basic kinds of container blocks:
  2433. [block quotes] and [list items].
  2434. [Lists] are meta-containers for [list items].
  2435. We define the syntax for container blocks recursively. The general
  2436. form of the definition is:
  2437. > If X is a sequence of blocks, then the result of
  2438. > transforming X in such-and-such a way is a container of type Y
  2439. > with these blocks as its content.
  2440. So, we explain what counts as a block quote or list item by explaining
  2441. how these can be *generated* from their contents. This should suffice
  2442. to define the syntax, although it does not give a recipe for *parsing*
  2443. these constructions. (A recipe is provided below in the section entitled
  2444. [A parsing strategy](#appendix-a-parsing-strategy).)
  2445. ## Block quotes
  2446. A [block quote marker](@)
  2447. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2448. with a following space, or (b) a single character `>` not followed by a space.
  2449. The following rules define [block quotes]:
  2450. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2451. of blocks *Bs*, then the result of prepending a [block quote
  2452. marker] to the beginning of each line in *Ls*
  2453. is a [block quote](#block-quotes) containing *Bs*.
  2454. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2455. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2456. the initial [block quote marker] from one or
  2457. more lines in which the next [non-whitespace character] after the [block
  2458. quote marker] is [paragraph continuation
  2459. text] is a block quote with *Bs* as its content.
  2460. [Paragraph continuation text](@) is text
  2461. that will be parsed as part of the content of a paragraph, but does
  2462. not occur at the beginning of the paragraph.
  2463. 3. **Consecutiveness.** A document cannot contain two [block
  2464. quotes] in a row unless there is a [blank line] between them.
  2465. Nothing else counts as a [block quote](#block-quotes).
  2466. Here is a simple example:
  2467. ```````````````````````````````` example
  2468. > # Foo
  2469. > bar
  2470. > baz
  2471. .
  2472. <blockquote>
  2473. <h1>Foo</h1>
  2474. <p>bar
  2475. baz</p>
  2476. </blockquote>
  2477. ````````````````````````````````
  2478. The spaces after the `>` characters can be omitted:
  2479. ```````````````````````````````` example
  2480. ># Foo
  2481. >bar
  2482. > baz
  2483. .
  2484. <blockquote>
  2485. <h1>Foo</h1>
  2486. <p>bar
  2487. baz</p>
  2488. </blockquote>
  2489. ````````````````````````````````
  2490. The `>` characters can be indented 1-3 spaces:
  2491. ```````````````````````````````` example
  2492. > # Foo
  2493. > bar
  2494. > baz
  2495. .
  2496. <blockquote>
  2497. <h1>Foo</h1>
  2498. <p>bar
  2499. baz</p>
  2500. </blockquote>
  2501. ````````````````````````````````
  2502. Four spaces gives us a code block:
  2503. ```````````````````````````````` example
  2504. > # Foo
  2505. > bar
  2506. > baz
  2507. .
  2508. <pre><code>&gt; # Foo
  2509. &gt; bar
  2510. &gt; baz
  2511. </code></pre>
  2512. ````````````````````````````````
  2513. The Laziness clause allows us to omit the `>` before
  2514. [paragraph continuation text]:
  2515. ```````````````````````````````` example
  2516. > # Foo
  2517. > bar
  2518. baz
  2519. .
  2520. <blockquote>
  2521. <h1>Foo</h1>
  2522. <p>bar
  2523. baz</p>
  2524. </blockquote>
  2525. ````````````````````````````````
  2526. A block quote can contain some lazy and some non-lazy
  2527. continuation lines:
  2528. ```````````````````````````````` example
  2529. > bar
  2530. baz
  2531. > foo
  2532. .
  2533. <blockquote>
  2534. <p>bar
  2535. baz
  2536. foo</p>
  2537. </blockquote>
  2538. ````````````````````````````````
  2539. Laziness only applies to lines that would have been continuations of
  2540. paragraphs had they been prepended with [block quote markers].
  2541. For example, the `> ` cannot be omitted in the second line of
  2542. ``` markdown
  2543. > foo
  2544. > ---
  2545. ```
  2546. without changing the meaning:
  2547. ```````````````````````````````` example
  2548. > foo
  2549. ---
  2550. .
  2551. <blockquote>
  2552. <p>foo</p>
  2553. </blockquote>
  2554. <hr />
  2555. ````````````````````````````````
  2556. Similarly, if we omit the `> ` in the second line of
  2557. ``` markdown
  2558. > - foo
  2559. > - bar
  2560. ```
  2561. then the block quote ends after the first line:
  2562. ```````````````````````````````` example
  2563. > - foo
  2564. - bar
  2565. .
  2566. <blockquote>
  2567. <ul>
  2568. <li>foo</li>
  2569. </ul>
  2570. </blockquote>
  2571. <ul>
  2572. <li>bar</li>
  2573. </ul>
  2574. ````````````````````````````````
  2575. For the same reason, we can't omit the `> ` in front of
  2576. subsequent lines of an indented or fenced code block:
  2577. ```````````````````````````````` example
  2578. > foo
  2579. bar
  2580. .
  2581. <blockquote>
  2582. <pre><code>foo
  2583. </code></pre>
  2584. </blockquote>
  2585. <pre><code>bar
  2586. </code></pre>
  2587. ````````````````````````````````
  2588. ```````````````````````````````` example
  2589. > ```
  2590. foo
  2591. ```
  2592. .
  2593. <blockquote>
  2594. <pre><code></code></pre>
  2595. </blockquote>
  2596. <p>foo</p>
  2597. <pre><code></code></pre>
  2598. ````````````````````````````````
  2599. Note that in the following case, we have a [lazy
  2600. continuation line]:
  2601. ```````````````````````````````` example
  2602. > foo
  2603. - bar
  2604. .
  2605. <blockquote>
  2606. <p>foo
  2607. - bar</p>
  2608. </blockquote>
  2609. ````````````````````````````````
  2610. To see why, note that in
  2611. ```markdown
  2612. > foo
  2613. > - bar
  2614. ```
  2615. the `- bar` is indented too far to start a list, and can't
  2616. be an indented code block because indented code blocks cannot
  2617. interrupt paragraphs, so it is [paragraph continuation text].
  2618. A block quote can be empty:
  2619. ```````````````````````````````` example
  2620. >
  2621. .
  2622. <blockquote>
  2623. </blockquote>
  2624. ````````````````````````````````
  2625. ```````````````````````````````` example
  2626. >
  2627. >
  2628. >
  2629. .
  2630. <blockquote>
  2631. </blockquote>
  2632. ````````````````````````````````
  2633. A block quote can have initial or final blank lines:
  2634. ```````````````````````````````` example
  2635. >
  2636. > foo
  2637. >
  2638. .
  2639. <blockquote>
  2640. <p>foo</p>
  2641. </blockquote>
  2642. ````````````````````````````````
  2643. A blank line always separates block quotes:
  2644. ```````````````````````````````` example
  2645. > foo
  2646. > bar
  2647. .
  2648. <blockquote>
  2649. <p>foo</p>
  2650. </blockquote>
  2651. <blockquote>
  2652. <p>bar</p>
  2653. </blockquote>
  2654. ````````````````````````````````
  2655. (Most current Markdown implementations, including John Gruber's
  2656. original `Markdown.pl`, will parse this example as a single block quote
  2657. with two paragraphs. But it seems better to allow the author to decide
  2658. whether two block quotes or one are wanted.)
  2659. Consecutiveness means that if we put these block quotes together,
  2660. we get a single block quote:
  2661. ```````````````````````````````` example
  2662. > foo
  2663. > bar
  2664. .
  2665. <blockquote>
  2666. <p>foo
  2667. bar</p>
  2668. </blockquote>
  2669. ````````````````````````````````
  2670. To get a block quote with two paragraphs, use:
  2671. ```````````````````````````````` example
  2672. > foo
  2673. >
  2674. > bar
  2675. .
  2676. <blockquote>
  2677. <p>foo</p>
  2678. <p>bar</p>
  2679. </blockquote>
  2680. ````````````````````````````````
  2681. Block quotes can interrupt paragraphs:
  2682. ```````````````````````````````` example
  2683. foo
  2684. > bar
  2685. .
  2686. <p>foo</p>
  2687. <blockquote>
  2688. <p>bar</p>
  2689. </blockquote>
  2690. ````````````````````````````````
  2691. In general, blank lines are not needed before or after block
  2692. quotes:
  2693. ```````````````````````````````` example
  2694. > aaa
  2695. ***
  2696. > bbb
  2697. .
  2698. <blockquote>
  2699. <p>aaa</p>
  2700. </blockquote>
  2701. <hr />
  2702. <blockquote>
  2703. <p>bbb</p>
  2704. </blockquote>
  2705. ````````````````````````````````
  2706. However, because of laziness, a blank line is needed between
  2707. a block quote and a following paragraph:
  2708. ```````````````````````````````` example
  2709. > bar
  2710. baz
  2711. .
  2712. <blockquote>
  2713. <p>bar
  2714. baz</p>
  2715. </blockquote>
  2716. ````````````````````````````````
  2717. ```````````````````````````````` example
  2718. > bar
  2719. baz
  2720. .
  2721. <blockquote>
  2722. <p>bar</p>
  2723. </blockquote>
  2724. <p>baz</p>
  2725. ````````````````````````````````
  2726. ```````````````````````````````` example
  2727. > bar
  2728. >
  2729. baz
  2730. .
  2731. <blockquote>
  2732. <p>bar</p>
  2733. </blockquote>
  2734. <p>baz</p>
  2735. ````````````````````````````````
  2736. It is a consequence of the Laziness rule that any number
  2737. of initial `>`s may be omitted on a continuation line of a
  2738. nested block quote:
  2739. ```````````````````````````````` example
  2740. > > > foo
  2741. bar
  2742. .
  2743. <blockquote>
  2744. <blockquote>
  2745. <blockquote>
  2746. <p>foo
  2747. bar</p>
  2748. </blockquote>
  2749. </blockquote>
  2750. </blockquote>
  2751. ````````````````````````````````
  2752. ```````````````````````````````` example
  2753. >>> foo
  2754. > bar
  2755. >>baz
  2756. .
  2757. <blockquote>
  2758. <blockquote>
  2759. <blockquote>
  2760. <p>foo
  2761. bar
  2762. baz</p>
  2763. </blockquote>
  2764. </blockquote>
  2765. </blockquote>
  2766. ````````````````````````````````
  2767. When including an indented code block in a block quote,
  2768. remember that the [block quote marker] includes
  2769. both the `>` and a following space. So *five spaces* are needed after
  2770. the `>`:
  2771. ```````````````````````````````` example
  2772. > code
  2773. > not code
  2774. .
  2775. <blockquote>
  2776. <pre><code>code
  2777. </code></pre>
  2778. </blockquote>
  2779. <blockquote>
  2780. <p>not code</p>
  2781. </blockquote>
  2782. ````````````````````````````````
  2783. ## List items
  2784. A [list marker](@) is a
  2785. [bullet list marker] or an [ordered list marker].
  2786. A [bullet list marker](@)
  2787. is a `-`, `+`, or `*` character.
  2788. An [ordered list marker](@)
  2789. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2790. `.` character or a `)` character. (The reason for the length
  2791. limit is that with 10 digits we start seeing integer overflows
  2792. in some browsers.)
  2793. The following rules define [list items]:
  2794. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2795. blocks *Bs* starting with a [non-whitespace character] and not separated
  2796. from each other by more than one blank line, and *M* is a list
  2797. marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2798. of prepending *M* and the following spaces to the first line of
  2799. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2800. list item with *Bs* as its contents. The type of the list item
  2801. (bullet or ordered) is determined by the type of its list marker.
  2802. If the list item is ordered, then it is also assigned a start
  2803. number, based on the ordered list marker.
  2804. Exceptions: When the first list item in a [list] interrupts
  2805. a paragraph---that is, when it starts on a line that would
  2806. otherwise count as [paragraph continuation text]---then (a)
  2807. the lines *Ls* must not begin with a blank line, and (b) if
  2808. the list item is ordered, the start number must be 1.
  2809. For example, let *Ls* be the lines
  2810. ```````````````````````````````` example
  2811. A paragraph
  2812. with two lines.
  2813. indented code
  2814. > A block quote.
  2815. .
  2816. <p>A paragraph
  2817. with two lines.</p>
  2818. <pre><code>indented code
  2819. </code></pre>
  2820. <blockquote>
  2821. <p>A block quote.</p>
  2822. </blockquote>
  2823. ````````````````````````````````
  2824. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2825. that the following is an ordered list item with start number 1,
  2826. and the same contents as *Ls*:
  2827. ```````````````````````````````` example
  2828. 1. A paragraph
  2829. with two lines.
  2830. indented code
  2831. > A block quote.
  2832. .
  2833. <ol>
  2834. <li>
  2835. <p>A paragraph
  2836. with two lines.</p>
  2837. <pre><code>indented code
  2838. </code></pre>
  2839. <blockquote>
  2840. <p>A block quote.</p>
  2841. </blockquote>
  2842. </li>
  2843. </ol>
  2844. ````````````````````````````````
  2845. The most important thing to notice is that the position of
  2846. the text after the list marker determines how much indentation
  2847. is needed in subsequent blocks in the list item. If the list
  2848. marker takes up two spaces, and there are three spaces between
  2849. the list marker and the next [non-whitespace character], then blocks
  2850. must be indented five spaces in order to fall under the list
  2851. item.
  2852. Here are some examples showing how far content must be indented to be
  2853. put under the list item:
  2854. ```````````````````````````````` example
  2855. - one
  2856. two
  2857. .
  2858. <ul>
  2859. <li>one</li>
  2860. </ul>
  2861. <p>two</p>
  2862. ````````````````````````````````
  2863. ```````````````````````````````` example
  2864. - one
  2865. two
  2866. .
  2867. <ul>
  2868. <li>
  2869. <p>one</p>
  2870. <p>two</p>
  2871. </li>
  2872. </ul>
  2873. ````````````````````````````````
  2874. ```````````````````````````````` example
  2875. - one
  2876. two
  2877. .
  2878. <ul>
  2879. <li>one</li>
  2880. </ul>
  2881. <pre><code> two
  2882. </code></pre>
  2883. ````````````````````````````````
  2884. ```````````````````````````````` example
  2885. - one
  2886. two
  2887. .
  2888. <ul>
  2889. <li>
  2890. <p>one</p>
  2891. <p>two</p>
  2892. </li>
  2893. </ul>
  2894. ````````````````````````````````
  2895. It is tempting to think of this in terms of columns: the continuation
  2896. blocks must be indented at least to the column of the first
  2897. [non-whitespace character] after the list marker. However, that is not quite right.
  2898. The spaces after the list marker determine how much relative indentation
  2899. is needed. Which column this indentation reaches will depend on
  2900. how the list item is embedded in other constructions, as shown by
  2901. this example:
  2902. ```````````````````````````````` example
  2903. > > 1. one
  2904. >>
  2905. >> two
  2906. .
  2907. <blockquote>
  2908. <blockquote>
  2909. <ol>
  2910. <li>
  2911. <p>one</p>
  2912. <p>two</p>
  2913. </li>
  2914. </ol>
  2915. </blockquote>
  2916. </blockquote>
  2917. ````````````````````````````````
  2918. Here `two` occurs in the same column as the list marker `1.`,
  2919. but is actually contained in the list item, because there is
  2920. sufficient indentation after the last containing blockquote marker.
  2921. The converse is also possible. In the following example, the word `two`
  2922. occurs far to the right of the initial text of the list item, `one`, but
  2923. it is not considered part of the list item, because it is not indented
  2924. far enough past the blockquote marker:
  2925. ```````````````````````````````` example
  2926. >>- one
  2927. >>
  2928. > > two
  2929. .
  2930. <blockquote>
  2931. <blockquote>
  2932. <ul>
  2933. <li>one</li>
  2934. </ul>
  2935. <p>two</p>
  2936. </blockquote>
  2937. </blockquote>
  2938. ````````````````````````````````
  2939. Note that at least one space is needed between the list marker and
  2940. any following content, so these are not list items:
  2941. ```````````````````````````````` example
  2942. -one
  2943. 2.two
  2944. .
  2945. <p>-one</p>
  2946. <p>2.two</p>
  2947. ````````````````````````````````
  2948. A list item may contain blocks that are separated by more than
  2949. one blank line.
  2950. ```````````````````````````````` example
  2951. - foo
  2952. bar
  2953. .
  2954. <ul>
  2955. <li>
  2956. <p>foo</p>
  2957. <p>bar</p>
  2958. </li>
  2959. </ul>
  2960. ````````````````````````````````
  2961. A list item may contain any kind of block:
  2962. ```````````````````````````````` example
  2963. 1. foo
  2964. ```
  2965. bar
  2966. ```
  2967. baz
  2968. > bam
  2969. .
  2970. <ol>
  2971. <li>
  2972. <p>foo</p>
  2973. <pre><code>bar
  2974. </code></pre>
  2975. <p>baz</p>
  2976. <blockquote>
  2977. <p>bam</p>
  2978. </blockquote>
  2979. </li>
  2980. </ol>
  2981. ````````````````````````````````
  2982. A list item that contains an indented code block will preserve
  2983. empty lines within the code block verbatim.
  2984. ```````````````````````````````` example
  2985. - Foo
  2986. bar
  2987. baz
  2988. .
  2989. <ul>
  2990. <li>
  2991. <p>Foo</p>
  2992. <pre><code>bar
  2993. baz
  2994. </code></pre>
  2995. </li>
  2996. </ul>
  2997. ````````````````````````````````
  2998. Note that ordered list start numbers must be nine digits or less:
  2999. ```````````````````````````````` example
  3000. 123456789. ok
  3001. .
  3002. <ol start="123456789">
  3003. <li>ok</li>
  3004. </ol>
  3005. ````````````````````````````````
  3006. ```````````````````````````````` example
  3007. 1234567890. not ok
  3008. .
  3009. <p>1234567890. not ok</p>
  3010. ````````````````````````````````
  3011. A start number may begin with 0s:
  3012. ```````````````````````````````` example
  3013. 0. ok
  3014. .
  3015. <ol start="0">
  3016. <li>ok</li>
  3017. </ol>
  3018. ````````````````````````````````
  3019. ```````````````````````````````` example
  3020. 003. ok
  3021. .
  3022. <ol start="3">
  3023. <li>ok</li>
  3024. </ol>
  3025. ````````````````````````````````
  3026. A start number may not be negative:
  3027. ```````````````````````````````` example
  3028. -1. not ok
  3029. .
  3030. <p>-1. not ok</p>
  3031. ````````````````````````````````
  3032. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3033. constitute a sequence of blocks *Bs* starting with an indented code
  3034. block and not separated from each other by more than one blank line,
  3035. and *M* is a list marker of width *W* followed by
  3036. one space, then the result of prepending *M* and the following
  3037. space to the first line of *Ls*, and indenting subsequent lines of
  3038. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3039. If a line is empty, then it need not be indented. The type of the
  3040. list item (bullet or ordered) is determined by the type of its list
  3041. marker. If the list item is ordered, then it is also assigned a
  3042. start number, based on the ordered list marker.
  3043. An indented code block will have to be indented four spaces beyond
  3044. the edge of the region where text will be included in the list item.
  3045. In the following case that is 6 spaces:
  3046. ```````````````````````````````` example
  3047. - foo
  3048. bar
  3049. .
  3050. <ul>
  3051. <li>
  3052. <p>foo</p>
  3053. <pre><code>bar
  3054. </code></pre>
  3055. </li>
  3056. </ul>
  3057. ````````````````````````````````
  3058. And in this case it is 11 spaces:
  3059. ```````````````````````````````` example
  3060. 10. foo
  3061. bar
  3062. .
  3063. <ol start="10">
  3064. <li>
  3065. <p>foo</p>
  3066. <pre><code>bar
  3067. </code></pre>
  3068. </li>
  3069. </ol>
  3070. ````````````````````````````````
  3071. If the *first* block in the list item is an indented code block,
  3072. then by rule #2, the contents must be indented *one* space after the
  3073. list marker:
  3074. ```````````````````````````````` example
  3075. indented code
  3076. paragraph
  3077. more code
  3078. .
  3079. <pre><code>indented code
  3080. </code></pre>
  3081. <p>paragraph</p>
  3082. <pre><code>more code
  3083. </code></pre>
  3084. ````````````````````````````````
  3085. ```````````````````````````````` example
  3086. 1. indented code
  3087. paragraph
  3088. more code
  3089. .
  3090. <ol>
  3091. <li>
  3092. <pre><code>indented code
  3093. </code></pre>
  3094. <p>paragraph</p>
  3095. <pre><code>more code
  3096. </code></pre>
  3097. </li>
  3098. </ol>
  3099. ````````````````````````````````
  3100. Note that an additional space indent is interpreted as space
  3101. inside the code block:
  3102. ```````````````````````````````` example
  3103. 1. indented code
  3104. paragraph
  3105. more code
  3106. .
  3107. <ol>
  3108. <li>
  3109. <pre><code> indented code
  3110. </code></pre>
  3111. <p>paragraph</p>
  3112. <pre><code>more code
  3113. </code></pre>
  3114. </li>
  3115. </ol>
  3116. ````````````````````````````````
  3117. Note that rules #1 and #2 only apply to two cases: (a) cases
  3118. in which the lines to be included in a list item begin with a
  3119. [non-whitespace character], and (b) cases in which
  3120. they begin with an indented code
  3121. block. In a case like the following, where the first block begins with
  3122. a three-space indent, the rules do not allow us to form a list item by
  3123. indenting the whole thing and prepending a list marker:
  3124. ```````````````````````````````` example
  3125. foo
  3126. bar
  3127. .
  3128. <p>foo</p>
  3129. <p>bar</p>
  3130. ````````````````````````````````
  3131. ```````````````````````````````` example
  3132. - foo
  3133. bar
  3134. .
  3135. <ul>
  3136. <li>foo</li>
  3137. </ul>
  3138. <p>bar</p>
  3139. ````````````````````````````````
  3140. This is not a significant restriction, because when a block begins
  3141. with 1-3 spaces indent, the indentation can always be removed without
  3142. a change in interpretation, allowing rule #1 to be applied. So, in
  3143. the above case:
  3144. ```````````````````````````````` example
  3145. - foo
  3146. bar
  3147. .
  3148. <ul>
  3149. <li>
  3150. <p>foo</p>
  3151. <p>bar</p>
  3152. </li>
  3153. </ul>
  3154. ````````````````````````````````
  3155. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3156. starting with a single [blank line] constitute a (possibly empty)
  3157. sequence of blocks *Bs*, not separated from each other by more than
  3158. one blank line, and *M* is a list marker of width *W*,
  3159. then the result of prepending *M* to the first line of *Ls*, and
  3160. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3161. item with *Bs* as its contents.
  3162. If a line is empty, then it need not be indented. The type of the
  3163. list item (bullet or ordered) is determined by the type of its list
  3164. marker. If the list item is ordered, then it is also assigned a
  3165. start number, based on the ordered list marker.
  3166. Here are some list items that start with a blank line but are not empty:
  3167. ```````````````````````````````` example
  3168. -
  3169. foo
  3170. -
  3171. ```
  3172. bar
  3173. ```
  3174. -
  3175. baz
  3176. .
  3177. <ul>
  3178. <li>foo</li>
  3179. <li>
  3180. <pre><code>bar
  3181. </code></pre>
  3182. </li>
  3183. <li>
  3184. <pre><code>baz
  3185. </code></pre>
  3186. </li>
  3187. </ul>
  3188. ````````````````````````````````
  3189. When the list item starts with a blank line, the number of spaces
  3190. following the list marker doesn't change the required indentation:
  3191. ```````````````````````````````` example
  3192. -
  3193. foo
  3194. .
  3195. <ul>
  3196. <li>foo</li>
  3197. </ul>
  3198. ````````````````````````````````
  3199. A list item can begin with at most one blank line.
  3200. In the following example, `foo` is not part of the list
  3201. item:
  3202. ```````````````````````````````` example
  3203. -
  3204. foo
  3205. .
  3206. <ul>
  3207. <li></li>
  3208. </ul>
  3209. <p>foo</p>
  3210. ````````````````````````````````
  3211. Here is an empty bullet list item:
  3212. ```````````````````````````````` example
  3213. - foo
  3214. -
  3215. - bar
  3216. .
  3217. <ul>
  3218. <li>foo</li>
  3219. <li></li>
  3220. <li>bar</li>
  3221. </ul>
  3222. ````````````````````````````````
  3223. It does not matter whether there are spaces following the [list marker]:
  3224. ```````````````````````````````` example
  3225. - foo
  3226. -
  3227. - bar
  3228. .
  3229. <ul>
  3230. <li>foo</li>
  3231. <li></li>
  3232. <li>bar</li>
  3233. </ul>
  3234. ````````````````````````````````
  3235. Here is an empty ordered list item:
  3236. ```````````````````````````````` example
  3237. 1. foo
  3238. 2.
  3239. 3. bar
  3240. .
  3241. <ol>
  3242. <li>foo</li>
  3243. <li></li>
  3244. <li>bar</li>
  3245. </ol>
  3246. ````````````````````````````````
  3247. A list may start or end with an empty list item:
  3248. ```````````````````````````````` example
  3249. *
  3250. .
  3251. <ul>
  3252. <li></li>
  3253. </ul>
  3254. ````````````````````````````````
  3255. However, an empty list item cannot interrupt a paragraph:
  3256. ```````````````````````````````` example
  3257. foo
  3258. *
  3259. foo
  3260. 1.
  3261. .
  3262. <p>foo
  3263. *</p>
  3264. <p>foo
  3265. 1.</p>
  3266. ````````````````````````````````
  3267. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3268. according to rule #1, #2, or #3, then the result of indenting each line
  3269. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3270. list item with the same contents and attributes. If a line is
  3271. empty, then it need not be indented.
  3272. Indented one space:
  3273. ```````````````````````````````` example
  3274. 1. A paragraph
  3275. with two lines.
  3276. indented code
  3277. > A block quote.
  3278. .
  3279. <ol>
  3280. <li>
  3281. <p>A paragraph
  3282. with two lines.</p>
  3283. <pre><code>indented code
  3284. </code></pre>
  3285. <blockquote>
  3286. <p>A block quote.</p>
  3287. </blockquote>
  3288. </li>
  3289. </ol>
  3290. ````````````````````````````````
  3291. Indented two spaces:
  3292. ```````````````````````````````` example
  3293. 1. A paragraph
  3294. with two lines.
  3295. indented code
  3296. > A block quote.
  3297. .
  3298. <ol>
  3299. <li>
  3300. <p>A paragraph
  3301. with two lines.</p>
  3302. <pre><code>indented code
  3303. </code></pre>
  3304. <blockquote>
  3305. <p>A block quote.</p>
  3306. </blockquote>
  3307. </li>
  3308. </ol>
  3309. ````````````````````````````````
  3310. Indented three spaces:
  3311. ```````````````````````````````` example
  3312. 1. A paragraph
  3313. with two lines.
  3314. indented code
  3315. > A block quote.
  3316. .
  3317. <ol>
  3318. <li>
  3319. <p>A paragraph
  3320. with two lines.</p>
  3321. <pre><code>indented code
  3322. </code></pre>
  3323. <blockquote>
  3324. <p>A block quote.</p>
  3325. </blockquote>
  3326. </li>
  3327. </ol>
  3328. ````````````````````````````````
  3329. Four spaces indent gives a code block:
  3330. ```````````````````````````````` example
  3331. 1. A paragraph
  3332. with two lines.
  3333. indented code
  3334. > A block quote.
  3335. .
  3336. <pre><code>1. A paragraph
  3337. with two lines.
  3338. indented code
  3339. &gt; A block quote.
  3340. </code></pre>
  3341. ````````````````````````````````
  3342. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3343. item](#list-items) with contents *Bs*, then the result of deleting
  3344. some or all of the indentation from one or more lines in which the
  3345. next [non-whitespace character] after the indentation is
  3346. [paragraph continuation text] is a
  3347. list item with the same contents and attributes. The unindented
  3348. lines are called
  3349. [lazy continuation line](@)s.
  3350. Here is an example with [lazy continuation lines]:
  3351. ```````````````````````````````` example
  3352. 1. A paragraph
  3353. with two lines.
  3354. indented code
  3355. > A block quote.
  3356. .
  3357. <ol>
  3358. <li>
  3359. <p>A paragraph
  3360. with two lines.</p>
  3361. <pre><code>indented code
  3362. </code></pre>
  3363. <blockquote>
  3364. <p>A block quote.</p>
  3365. </blockquote>
  3366. </li>
  3367. </ol>
  3368. ````````````````````````````````
  3369. Indentation can be partially deleted:
  3370. ```````````````````````````````` example
  3371. 1. A paragraph
  3372. with two lines.
  3373. .
  3374. <ol>
  3375. <li>A paragraph
  3376. with two lines.</li>
  3377. </ol>
  3378. ````````````````````````````````
  3379. These examples show how laziness can work in nested structures:
  3380. ```````````````````````````````` example
  3381. > 1. > Blockquote
  3382. continued here.
  3383. .
  3384. <blockquote>
  3385. <ol>
  3386. <li>
  3387. <blockquote>
  3388. <p>Blockquote
  3389. continued here.</p>
  3390. </blockquote>
  3391. </li>
  3392. </ol>
  3393. </blockquote>
  3394. ````````````````````````````````
  3395. ```````````````````````````````` example
  3396. > 1. > Blockquote
  3397. > continued here.
  3398. .
  3399. <blockquote>
  3400. <ol>
  3401. <li>
  3402. <blockquote>
  3403. <p>Blockquote
  3404. continued here.</p>
  3405. </blockquote>
  3406. </li>
  3407. </ol>
  3408. </blockquote>
  3409. ````````````````````````````````
  3410. 6. **That's all.** Nothing that is not counted as a list item by rules
  3411. #1--5 counts as a [list item](#list-items).
  3412. The rules for sublists follow from the general rules above. A sublist
  3413. must be indented the same number of spaces a paragraph would need to be
  3414. in order to be included in the list item.
  3415. So, in this case we need two spaces indent:
  3416. ```````````````````````````````` example
  3417. - foo
  3418. - bar
  3419. - baz
  3420. - boo
  3421. .
  3422. <ul>
  3423. <li>foo
  3424. <ul>
  3425. <li>bar
  3426. <ul>
  3427. <li>baz
  3428. <ul>
  3429. <li>boo</li>
  3430. </ul>
  3431. </li>
  3432. </ul>
  3433. </li>
  3434. </ul>
  3435. </li>
  3436. </ul>
  3437. ````````````````````````````````
  3438. One is not enough:
  3439. ```````````````````````````````` example
  3440. - foo
  3441. - bar
  3442. - baz
  3443. - boo
  3444. .
  3445. <ul>
  3446. <li>foo</li>
  3447. <li>bar</li>
  3448. <li>baz</li>
  3449. <li>boo</li>
  3450. </ul>
  3451. ````````````````````````````````
  3452. Here we need four, because the list marker is wider:
  3453. ```````````````````````````````` example
  3454. 10) foo
  3455. - bar
  3456. .
  3457. <ol start="10">
  3458. <li>foo
  3459. <ul>
  3460. <li>bar</li>
  3461. </ul>
  3462. </li>
  3463. </ol>
  3464. ````````````````````````````````
  3465. Three is not enough:
  3466. ```````````````````````````````` example
  3467. 10) foo
  3468. - bar
  3469. .
  3470. <ol start="10">
  3471. <li>foo</li>
  3472. </ol>
  3473. <ul>
  3474. <li>bar</li>
  3475. </ul>
  3476. ````````````````````````````````
  3477. A list may be the first block in a list item:
  3478. ```````````````````````````````` example
  3479. - - foo
  3480. .
  3481. <ul>
  3482. <li>
  3483. <ul>
  3484. <li>foo</li>
  3485. </ul>
  3486. </li>
  3487. </ul>
  3488. ````````````````````````````````
  3489. ```````````````````````````````` example
  3490. 1. - 2. foo
  3491. .
  3492. <ol>
  3493. <li>
  3494. <ul>
  3495. <li>
  3496. <ol start="2">
  3497. <li>foo</li>
  3498. </ol>
  3499. </li>
  3500. </ul>
  3501. </li>
  3502. </ol>
  3503. ````````````````````````````````
  3504. A list item can contain a heading:
  3505. ```````````````````````````````` example
  3506. - # Foo
  3507. - Bar
  3508. ---
  3509. baz
  3510. .
  3511. <ul>
  3512. <li>
  3513. <h1>Foo</h1>
  3514. </li>
  3515. <li>
  3516. <h2>Bar</h2>
  3517. baz</li>
  3518. </ul>
  3519. ````````````````````````````````
  3520. ### Motivation
  3521. John Gruber's Markdown spec says the following about list items:
  3522. 1. "List markers typically start at the left margin, but may be indented
  3523. by up to three spaces. List markers must be followed by one or more
  3524. spaces or a tab."
  3525. 2. "To make lists look nice, you can wrap items with hanging indents....
  3526. But if you don't want to, you don't have to."
  3527. 3. "List items may consist of multiple paragraphs. Each subsequent
  3528. paragraph in a list item must be indented by either 4 spaces or one
  3529. tab."
  3530. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3531. but here again, Markdown will allow you to be lazy."
  3532. 5. "To put a blockquote within a list item, the blockquote's `>`
  3533. delimiters need to be indented."
  3534. 6. "To put a code block within a list item, the code block needs to be
  3535. indented twice — 8 spaces or two tabs."
  3536. These rules specify that a paragraph under a list item must be indented
  3537. four spaces (presumably, from the left margin, rather than the start of
  3538. the list marker, but this is not said), and that code under a list item
  3539. must be indented eight spaces instead of the usual four. They also say
  3540. that a block quote must be indented, but not by how much; however, the
  3541. example given has four spaces indentation. Although nothing is said
  3542. about other kinds of block-level content, it is certainly reasonable to
  3543. infer that *all* block elements under a list item, including other
  3544. lists, must be indented four spaces. This principle has been called the
  3545. *four-space rule*.
  3546. The four-space rule is clear and principled, and if the reference
  3547. implementation `Markdown.pl` had followed it, it probably would have
  3548. become the standard. However, `Markdown.pl` allowed paragraphs and
  3549. sublists to start with only two spaces indentation, at least on the
  3550. outer level. Worse, its behavior was inconsistent: a sublist of an
  3551. outer-level list needed two spaces indentation, but a sublist of this
  3552. sublist needed three spaces. It is not surprising, then, that different
  3553. implementations of Markdown have developed very different rules for
  3554. determining what comes under a list item. (Pandoc and python-Markdown,
  3555. for example, stuck with Gruber's syntax description and the four-space
  3556. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3557. followed `Markdown.pl`'s behavior more closely.)
  3558. Unfortunately, given the divergences between implementations, there
  3559. is no way to give a spec for list items that will be guaranteed not
  3560. to break any existing documents. However, the spec given here should
  3561. correctly handle lists formatted with either the four-space rule or
  3562. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3563. in a way that is natural for a human to read.
  3564. The strategy here is to let the width and indentation of the list marker
  3565. determine the indentation necessary for blocks to fall under the list
  3566. item, rather than having a fixed and arbitrary number. The writer can
  3567. think of the body of the list item as a unit which gets indented to the
  3568. right enough to fit the list marker (and any indentation on the list
  3569. marker). (The laziness rule, #5, then allows continuation lines to be
  3570. unindented if needed.)
  3571. This rule is superior, we claim, to any rule requiring a fixed level of
  3572. indentation from the margin. The four-space rule is clear but
  3573. unnatural. It is quite unintuitive that
  3574. ``` markdown
  3575. - foo
  3576. bar
  3577. - baz
  3578. ```
  3579. should be parsed as two lists with an intervening paragraph,
  3580. ``` html
  3581. <ul>
  3582. <li>foo</li>
  3583. </ul>
  3584. <p>bar</p>
  3585. <ul>
  3586. <li>baz</li>
  3587. </ul>
  3588. ```
  3589. as the four-space rule demands, rather than a single list,
  3590. ``` html
  3591. <ul>
  3592. <li>
  3593. <p>foo</p>
  3594. <p>bar</p>
  3595. <ul>
  3596. <li>baz</li>
  3597. </ul>
  3598. </li>
  3599. </ul>
  3600. ```
  3601. The choice of four spaces is arbitrary. It can be learned, but it is
  3602. not likely to be guessed, and it trips up beginners regularly.
  3603. Would it help to adopt a two-space rule? The problem is that such
  3604. a rule, together with the rule allowing 1--3 spaces indentation of the
  3605. initial list marker, allows text that is indented *less than* the
  3606. original list marker to be included in the list item. For example,
  3607. `Markdown.pl` parses
  3608. ``` markdown
  3609. - one
  3610. two
  3611. ```
  3612. as a single list item, with `two` a continuation paragraph:
  3613. ``` html
  3614. <ul>
  3615. <li>
  3616. <p>one</p>
  3617. <p>two</p>
  3618. </li>
  3619. </ul>
  3620. ```
  3621. and similarly
  3622. ``` markdown
  3623. > - one
  3624. >
  3625. > two
  3626. ```
  3627. as
  3628. ``` html
  3629. <blockquote>
  3630. <ul>
  3631. <li>
  3632. <p>one</p>
  3633. <p>two</p>
  3634. </li>
  3635. </ul>
  3636. </blockquote>
  3637. ```
  3638. This is extremely unintuitive.
  3639. Rather than requiring a fixed indent from the margin, we could require
  3640. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3641. may itself be indented). This proposal would remove the last anomaly
  3642. discussed. Unlike the spec presented above, it would count the following
  3643. as a list item with a subparagraph, even though the paragraph `bar`
  3644. is not indented as far as the first paragraph `foo`:
  3645. ``` markdown
  3646. 10. foo
  3647. bar
  3648. ```
  3649. Arguably this text does read like a list item with `bar` as a subparagraph,
  3650. which may count in favor of the proposal. However, on this proposal indented
  3651. code would have to be indented six spaces after the list marker. And this
  3652. would break a lot of existing Markdown, which has the pattern:
  3653. ``` markdown
  3654. 1. foo
  3655. indented code
  3656. ```
  3657. where the code is indented eight spaces. The spec above, by contrast, will
  3658. parse this text as expected, since the code block's indentation is measured
  3659. from the beginning of `foo`.
  3660. The one case that needs special treatment is a list item that *starts*
  3661. with indented code. How much indentation is required in that case, since
  3662. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3663. that in such cases, we require one space indentation from the list marker
  3664. (and then the normal four spaces for the indented code). This will match the
  3665. four-space rule in cases where the list marker plus its initial indentation
  3666. takes four spaces (a common case), but diverge in other cases.
  3667. ## Lists
  3668. A [list](@) is a sequence of one or more
  3669. list items [of the same type]. The list items
  3670. may be separated by any number of blank lines.
  3671. Two list items are [of the same type](@)
  3672. if they begin with a [list marker] of the same type.
  3673. Two list markers are of the
  3674. same type if (a) they are bullet list markers using the same character
  3675. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3676. delimiter (either `.` or `)`).
  3677. A list is an [ordered list](@)
  3678. if its constituent list items begin with
  3679. [ordered list markers], and a
  3680. [bullet list](@) if its constituent list
  3681. items begin with [bullet list markers].
  3682. The [start number](@)
  3683. of an [ordered list] is determined by the list number of
  3684. its initial list item. The numbers of subsequent list items are
  3685. disregarded.
  3686. A list is [loose](@) if any of its constituent
  3687. list items are separated by blank lines, or if any of its constituent
  3688. list items directly contain two block-level elements with a blank line
  3689. between them. Otherwise a list is [tight](@).
  3690. (The difference in HTML output is that paragraphs in a loose list are
  3691. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3692. Changing the bullet or ordered list delimiter starts a new list:
  3693. ```````````````````````````````` example
  3694. - foo
  3695. - bar
  3696. + baz
  3697. .
  3698. <ul>
  3699. <li>foo</li>
  3700. <li>bar</li>
  3701. </ul>
  3702. <ul>
  3703. <li>baz</li>
  3704. </ul>
  3705. ````````````````````````````````
  3706. ```````````````````````````````` example
  3707. 1. foo
  3708. 2. bar
  3709. 3) baz
  3710. .
  3711. <ol>
  3712. <li>foo</li>
  3713. <li>bar</li>
  3714. </ol>
  3715. <ol start="3">
  3716. <li>baz</li>
  3717. </ol>
  3718. ````````````````````````````````
  3719. In CommonMark, a list can interrupt a paragraph. That is,
  3720. no blank line is needed to separate a paragraph from a following
  3721. list:
  3722. ```````````````````````````````` example
  3723. Foo
  3724. - bar
  3725. - baz
  3726. .
  3727. <p>Foo</p>
  3728. <ul>
  3729. <li>bar</li>
  3730. <li>baz</li>
  3731. </ul>
  3732. ````````````````````````````````
  3733. `Markdown.pl` does not allow this, through fear of triggering a list
  3734. via a numeral in a hard-wrapped line:
  3735. ``` markdown
  3736. The number of windows in my house is
  3737. 14. The number of doors is 6.
  3738. ```
  3739. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3740. interrupt a paragraph, even though the same considerations might
  3741. apply.
  3742. In CommonMark, we do allow lists to interrupt paragraphs, for
  3743. two reasons. First, it is natural and not uncommon for people
  3744. to start lists without blank lines:
  3745. ``` markdown
  3746. I need to buy
  3747. - new shoes
  3748. - a coat
  3749. - a plane ticket
  3750. ```
  3751. Second, we are attracted to a
  3752. > [principle of uniformity](@):
  3753. > if a chunk of text has a certain
  3754. > meaning, it will continue to have the same meaning when put into a
  3755. > container block (such as a list item or blockquote).
  3756. (Indeed, the spec for [list items] and [block quotes] presupposes
  3757. this principle.) This principle implies that if
  3758. ``` markdown
  3759. * I need to buy
  3760. - new shoes
  3761. - a coat
  3762. - a plane ticket
  3763. ```
  3764. is a list item containing a paragraph followed by a nested sublist,
  3765. as all Markdown implementations agree it is (though the paragraph
  3766. may be rendered without `<p>` tags, since the list is "tight"),
  3767. then
  3768. ``` markdown
  3769. I need to buy
  3770. - new shoes
  3771. - a coat
  3772. - a plane ticket
  3773. ```
  3774. by itself should be a paragraph followed by a nested sublist.
  3775. Since it is well established Markdown practice to allow lists to
  3776. interrupt paragraphs inside list items, the [principle of
  3777. uniformity] requires us to allow this outside list items as
  3778. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3779. takes a different approach, requiring blank lines before lists
  3780. even inside other list items.)
  3781. In order to solve of unwanted lists in paragraphs with
  3782. hard-wrapped numerals, we allow only lists starting with `1` to
  3783. interrupt paragraphs. Thus,
  3784. ```````````````````````````````` example
  3785. The number of windows in my house is
  3786. 14. The number of doors is 6.
  3787. .
  3788. <p>The number of windows in my house is
  3789. 14. The number of doors is 6.</p>
  3790. ````````````````````````````````
  3791. We may still get an unintended result in cases like
  3792. ```````````````````````````````` example
  3793. The number of windows in my house is
  3794. 1. The number of doors is 6.
  3795. .
  3796. <p>The number of windows in my house is</p>
  3797. <ol>
  3798. <li>The number of doors is 6.</li>
  3799. </ol>
  3800. ````````````````````````````````
  3801. but this rule should prevent most spurious list captures.
  3802. There can be any number of blank lines between items:
  3803. ```````````````````````````````` example
  3804. - foo
  3805. - bar
  3806. - baz
  3807. .
  3808. <ul>
  3809. <li>
  3810. <p>foo</p>
  3811. </li>
  3812. <li>
  3813. <p>bar</p>
  3814. </li>
  3815. <li>
  3816. <p>baz</p>
  3817. </li>
  3818. </ul>
  3819. ````````````````````````````````
  3820. ```````````````````````````````` example
  3821. - foo
  3822. - bar
  3823. - baz
  3824. bim
  3825. .
  3826. <ul>
  3827. <li>foo
  3828. <ul>
  3829. <li>bar
  3830. <ul>
  3831. <li>
  3832. <p>baz</p>
  3833. <p>bim</p>
  3834. </li>
  3835. </ul>
  3836. </li>
  3837. </ul>
  3838. </li>
  3839. </ul>
  3840. ````````````````````````````````
  3841. To separate consecutive lists of the same type, or to separate a
  3842. list from an indented code block that would otherwise be parsed
  3843. as a subparagraph of the final list item, you can insert a blank HTML
  3844. comment:
  3845. ```````````````````````````````` example
  3846. - foo
  3847. - bar
  3848. <!-- -->
  3849. - baz
  3850. - bim
  3851. .
  3852. <ul>
  3853. <li>foo</li>
  3854. <li>bar</li>
  3855. </ul>
  3856. <!-- -->
  3857. <ul>
  3858. <li>baz</li>
  3859. <li>bim</li>
  3860. </ul>
  3861. ````````````````````````````````
  3862. ```````````````````````````````` example
  3863. - foo
  3864. notcode
  3865. - foo
  3866. <!-- -->
  3867. code
  3868. .
  3869. <ul>
  3870. <li>
  3871. <p>foo</p>
  3872. <p>notcode</p>
  3873. </li>
  3874. <li>
  3875. <p>foo</p>
  3876. </li>
  3877. </ul>
  3878. <!-- -->
  3879. <pre><code>code
  3880. </code></pre>
  3881. ````````````````````````````````
  3882. List items need not be indented to the same level. The following
  3883. list items will be treated as items at the same list level,
  3884. since none is indented enough to belong to the previous list
  3885. item:
  3886. ```````````````````````````````` example
  3887. - a
  3888. - b
  3889. - c
  3890. - d
  3891. - e
  3892. - f
  3893. - g
  3894. - h
  3895. - i
  3896. .
  3897. <ul>
  3898. <li>a</li>
  3899. <li>b</li>
  3900. <li>c</li>
  3901. <li>d</li>
  3902. <li>e</li>
  3903. <li>f</li>
  3904. <li>g</li>
  3905. <li>h</li>
  3906. <li>i</li>
  3907. </ul>
  3908. ````````````````````````````````
  3909. ```````````````````````````````` example
  3910. 1. a
  3911. 2. b
  3912. 3. c
  3913. .
  3914. <ol>
  3915. <li>
  3916. <p>a</p>
  3917. </li>
  3918. <li>
  3919. <p>b</p>
  3920. </li>
  3921. <li>
  3922. <p>c</p>
  3923. </li>
  3924. </ol>
  3925. ````````````````````````````````
  3926. This is a loose list, because there is a blank line between
  3927. two of the list items:
  3928. ```````````````````````````````` example
  3929. - a
  3930. - b
  3931. - c
  3932. .
  3933. <ul>
  3934. <li>
  3935. <p>a</p>
  3936. </li>
  3937. <li>
  3938. <p>b</p>
  3939. </li>
  3940. <li>
  3941. <p>c</p>
  3942. </li>
  3943. </ul>
  3944. ````````````````````````````````
  3945. So is this, with a empty second item:
  3946. ```````````````````````````````` example
  3947. * a
  3948. *
  3949. * c
  3950. .
  3951. <ul>
  3952. <li>
  3953. <p>a</p>
  3954. </li>
  3955. <li></li>
  3956. <li>
  3957. <p>c</p>
  3958. </li>
  3959. </ul>
  3960. ````````````````````````````````
  3961. These are loose lists, even though there is no space between the items,
  3962. because one of the items directly contains two block-level elements
  3963. with a blank line between them:
  3964. ```````````````````````````````` example
  3965. - a
  3966. - b
  3967. c
  3968. - d
  3969. .
  3970. <ul>
  3971. <li>
  3972. <p>a</p>
  3973. </li>
  3974. <li>
  3975. <p>b</p>
  3976. <p>c</p>
  3977. </li>
  3978. <li>
  3979. <p>d</p>
  3980. </li>
  3981. </ul>
  3982. ````````````````````````````````
  3983. ```````````````````````````````` example
  3984. - a
  3985. - b
  3986. [ref]: /url
  3987. - d
  3988. .
  3989. <ul>
  3990. <li>
  3991. <p>a</p>
  3992. </li>
  3993. <li>
  3994. <p>b</p>
  3995. </li>
  3996. <li>
  3997. <p>d</p>
  3998. </li>
  3999. </ul>
  4000. ````````````````````````````````
  4001. This is a tight list, because the blank lines are in a code block:
  4002. ```````````````````````````````` example
  4003. - a
  4004. - ```
  4005. b
  4006. ```
  4007. - c
  4008. .
  4009. <ul>
  4010. <li>a</li>
  4011. <li>
  4012. <pre><code>b
  4013. </code></pre>
  4014. </li>
  4015. <li>c</li>
  4016. </ul>
  4017. ````````````````````````````````
  4018. This is a tight list, because the blank line is between two
  4019. paragraphs of a sublist. So the sublist is loose while
  4020. the outer list is tight:
  4021. ```````````````````````````````` example
  4022. - a
  4023. - b
  4024. c
  4025. - d
  4026. .
  4027. <ul>
  4028. <li>a
  4029. <ul>
  4030. <li>
  4031. <p>b</p>
  4032. <p>c</p>
  4033. </li>
  4034. </ul>
  4035. </li>
  4036. <li>d</li>
  4037. </ul>
  4038. ````````````````````````````````
  4039. This is a tight list, because the blank line is inside the
  4040. block quote:
  4041. ```````````````````````````````` example
  4042. * a
  4043. > b
  4044. >
  4045. * c
  4046. .
  4047. <ul>
  4048. <li>a
  4049. <blockquote>
  4050. <p>b</p>
  4051. </blockquote>
  4052. </li>
  4053. <li>c</li>
  4054. </ul>
  4055. ````````````````````````````````
  4056. This list is tight, because the consecutive block elements
  4057. are not separated by blank lines:
  4058. ```````````````````````````````` example
  4059. - a
  4060. > b
  4061. ```
  4062. c
  4063. ```
  4064. - d
  4065. .
  4066. <ul>
  4067. <li>a
  4068. <blockquote>
  4069. <p>b</p>
  4070. </blockquote>
  4071. <pre><code>c
  4072. </code></pre>
  4073. </li>
  4074. <li>d</li>
  4075. </ul>
  4076. ````````````````````````````````
  4077. A single-paragraph list is tight:
  4078. ```````````````````````````````` example
  4079. - a
  4080. .
  4081. <ul>
  4082. <li>a</li>
  4083. </ul>
  4084. ````````````````````````````````
  4085. ```````````````````````````````` example
  4086. - a
  4087. - b
  4088. .
  4089. <ul>
  4090. <li>a
  4091. <ul>
  4092. <li>b</li>
  4093. </ul>
  4094. </li>
  4095. </ul>
  4096. ````````````````````````````````
  4097. This list is loose, because of the blank line between the
  4098. two block elements in the list item:
  4099. ```````````````````````````````` example
  4100. 1. ```
  4101. foo
  4102. ```
  4103. bar
  4104. .
  4105. <ol>
  4106. <li>
  4107. <pre><code>foo
  4108. </code></pre>
  4109. <p>bar</p>
  4110. </li>
  4111. </ol>
  4112. ````````````````````````````````
  4113. Here the outer list is loose, the inner list tight:
  4114. ```````````````````````````````` example
  4115. * foo
  4116. * bar
  4117. baz
  4118. .
  4119. <ul>
  4120. <li>
  4121. <p>foo</p>
  4122. <ul>
  4123. <li>bar</li>
  4124. </ul>
  4125. <p>baz</p>
  4126. </li>
  4127. </ul>
  4128. ````````````````````````````````
  4129. ```````````````````````````````` example
  4130. - a
  4131. - b
  4132. - c
  4133. - d
  4134. - e
  4135. - f
  4136. .
  4137. <ul>
  4138. <li>
  4139. <p>a</p>
  4140. <ul>
  4141. <li>b</li>
  4142. <li>c</li>
  4143. </ul>
  4144. </li>
  4145. <li>
  4146. <p>d</p>
  4147. <ul>
  4148. <li>e</li>
  4149. <li>f</li>
  4150. </ul>
  4151. </li>
  4152. </ul>
  4153. ````````````````````````````````
  4154. # Inlines
  4155. Inlines are parsed sequentially from the beginning of the character
  4156. stream to the end (left to right, in left-to-right languages).
  4157. Thus, for example, in
  4158. ```````````````````````````````` example
  4159. `hi`lo`
  4160. .
  4161. <p><code>hi</code>lo`</p>
  4162. ````````````````````````````````
  4163. `hi` is parsed as code, leaving the backtick at the end as a literal
  4164. backtick.
  4165. ## Backslash escapes
  4166. Any ASCII punctuation character may be backslash-escaped:
  4167. ```````````````````````````````` example
  4168. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4169. .
  4170. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4171. ````````````````````````````````
  4172. Backslashes before other characters are treated as literal
  4173. backslashes:
  4174. ```````````````````````````````` example
  4175. \→\A\a\ \3\φ\«
  4176. .
  4177. <p>\→\A\a\ \3\φ\«</p>
  4178. ````````````````````````````````
  4179. Escaped characters are treated as regular characters and do
  4180. not have their usual Markdown meanings:
  4181. ```````````````````````````````` example
  4182. \*not emphasized*
  4183. \<br/> not a tag
  4184. \[not a link](/foo)
  4185. \`not code`
  4186. 1\. not a list
  4187. \* not a list
  4188. \# not a heading
  4189. \[foo]: /url "not a reference"
  4190. .
  4191. <p>*not emphasized*
  4192. &lt;br/&gt; not a tag
  4193. [not a link](/foo)
  4194. `not code`
  4195. 1. not a list
  4196. * not a list
  4197. # not a heading
  4198. [foo]: /url &quot;not a reference&quot;</p>
  4199. ````````````````````````````````
  4200. If a backslash is itself escaped, the following character is not:
  4201. ```````````````````````````````` example
  4202. \\*emphasis*
  4203. .
  4204. <p>\<em>emphasis</em></p>
  4205. ````````````````````````````````
  4206. A backslash at the end of the line is a [hard line break]:
  4207. ```````````````````````````````` example
  4208. foo\
  4209. bar
  4210. .
  4211. <p>foo<br />
  4212. bar</p>
  4213. ````````````````````````````````
  4214. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4215. raw HTML:
  4216. ```````````````````````````````` example
  4217. `` \[\` ``
  4218. .
  4219. <p><code>\[\`</code></p>
  4220. ````````````````````````````````
  4221. ```````````````````````````````` example
  4222. \[\]
  4223. .
  4224. <pre><code>\[\]
  4225. </code></pre>
  4226. ````````````````````````````````
  4227. ```````````````````````````````` example
  4228. ~~~
  4229. \[\]
  4230. ~~~
  4231. .
  4232. <pre><code>\[\]
  4233. </code></pre>
  4234. ````````````````````````````````
  4235. ```````````````````````````````` example
  4236. <http://example.com?find=\*>
  4237. .
  4238. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4239. ````````````````````````````````
  4240. ```````````````````````````````` example
  4241. <a href="/bar\/)">
  4242. .
  4243. <a href="/bar\/)">
  4244. ````````````````````````````````
  4245. But they work in all other contexts, including URLs and link titles,
  4246. link references, and [info strings] in [fenced code blocks]:
  4247. ```````````````````````````````` example
  4248. [foo](/bar\* "ti\*tle")
  4249. .
  4250. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4251. ````````````````````````````````
  4252. ```````````````````````````````` example
  4253. [foo]
  4254. [foo]: /bar\* "ti\*tle"
  4255. .
  4256. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4257. ````````````````````````````````
  4258. ```````````````````````````````` example
  4259. ``` foo\+bar
  4260. foo
  4261. ```
  4262. .
  4263. <pre><code class="language-foo+bar">foo
  4264. </code></pre>
  4265. ````````````````````````````````
  4266. ## Entity and numeric character references
  4267. All valid HTML entity references and numeric character
  4268. references, except those occuring in code blocks and code spans,
  4269. are recognized as such and treated as equivalent to the
  4270. corresponding Unicode characters. Conforming CommonMark parsers
  4271. need not store information about whether a particular character
  4272. was represented in the source using a Unicode character or
  4273. an entity reference.
  4274. [Entity references](@) consist of `&` + any of the valid
  4275. HTML5 entity names + `;`. The
  4276. document <https://html.spec.whatwg.org/multipage/entities.json>
  4277. is used as an authoritative source for the valid entity
  4278. references and their corresponding code points.
  4279. ```````````````````````````````` example
  4280. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4281. &frac34; &HilbertSpace; &DifferentialD;
  4282. &ClockwiseContourIntegral; &ngE;
  4283. .
  4284. <p>  &amp; © Æ Ď
  4285. ¾ ℋ ⅆ
  4286. ∲ ≧̸</p>
  4287. ````````````````````````````````
  4288. [Decimal numeric character
  4289. references](@)
  4290. consist of `&#` + a string of 1--8 arabic digits + `;`. A
  4291. numeric character reference is parsed as the corresponding
  4292. Unicode character. Invalid Unicode code points will be replaced by
  4293. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4294. the code point `U+0000` will also be replaced by `U+FFFD`.
  4295. ```````````````````````````````` example
  4296. &#35; &#1234; &#992; &#98765432; &#0;
  4297. .
  4298. <p># Ӓ Ϡ � �</p>
  4299. ````````````````````````````````
  4300. [Hexadecimal numeric character
  4301. references](@) consist of `&#` +
  4302. either `X` or `x` + a string of 1-8 hexadecimal digits + `;`.
  4303. They too are parsed as the corresponding Unicode character (this
  4304. time specified with a hexadecimal numeral instead of decimal).
  4305. ```````````````````````````````` example
  4306. &#X22; &#XD06; &#xcab;
  4307. .
  4308. <p>&quot; ആ ಫ</p>
  4309. ````````````````````````````````
  4310. Here are some nonentities:
  4311. ```````````````````````````````` example
  4312. &nbsp &x; &#; &#x;
  4313. &ThisIsNotDefined; &hi?;
  4314. .
  4315. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4316. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4317. ````````````````````````````````
  4318. Although HTML5 does accept some entity references
  4319. without a trailing semicolon (such as `&copy`), these are not
  4320. recognized here, because it makes the grammar too ambiguous:
  4321. ```````````````````````````````` example
  4322. &copy
  4323. .
  4324. <p>&amp;copy</p>
  4325. ````````````````````````````````
  4326. Strings that are not on the list of HTML5 named entities are not
  4327. recognized as entity references either:
  4328. ```````````````````````````````` example
  4329. &MadeUpEntity;
  4330. .
  4331. <p>&amp;MadeUpEntity;</p>
  4332. ````````````````````````````````
  4333. Entity and numeric character references are recognized in any
  4334. context besides code spans or code blocks, including
  4335. URLs, [link titles], and [fenced code block][] [info strings]:
  4336. ```````````````````````````````` example
  4337. <a href="&ouml;&ouml;.html">
  4338. .
  4339. <a href="&ouml;&ouml;.html">
  4340. ````````````````````````````````
  4341. ```````````````````````````````` example
  4342. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4343. .
  4344. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4345. ````````````````````````````````
  4346. ```````````````````````````````` example
  4347. [foo]
  4348. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4349. .
  4350. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4351. ````````````````````````````````
  4352. ```````````````````````````````` example
  4353. ``` f&ouml;&ouml;
  4354. foo
  4355. ```
  4356. .
  4357. <pre><code class="language-föö">foo
  4358. </code></pre>
  4359. ````````````````````````````````
  4360. Entity and numeric character references are treated as literal
  4361. text in code spans and code blocks:
  4362. ```````````````````````````````` example
  4363. `f&ouml;&ouml;`
  4364. .
  4365. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4366. ````````````````````````````````
  4367. ```````````````````````````````` example
  4368. f&ouml;f&ouml;
  4369. .
  4370. <pre><code>f&amp;ouml;f&amp;ouml;
  4371. </code></pre>
  4372. ````````````````````````````````
  4373. ## Code spans
  4374. A [backtick string](@)
  4375. is a string of one or more backtick characters (`` ` ``) that is neither
  4376. preceded nor followed by a backtick.
  4377. A [code span](@) begins with a backtick string and ends with
  4378. a backtick string of equal length. The contents of the code span are
  4379. the characters between the two backtick strings, with leading and
  4380. trailing spaces and [line endings] removed, and
  4381. [whitespace] collapsed to single spaces.
  4382. This is a simple code span:
  4383. ```````````````````````````````` example
  4384. `foo`
  4385. .
  4386. <p><code>foo</code></p>
  4387. ````````````````````````````````
  4388. Here two backticks are used, because the code contains a backtick.
  4389. This example also illustrates stripping of leading and trailing spaces:
  4390. ```````````````````````````````` example
  4391. `` foo ` bar ``
  4392. .
  4393. <p><code>foo ` bar</code></p>
  4394. ````````````````````````````````
  4395. This example shows the motivation for stripping leading and trailing
  4396. spaces:
  4397. ```````````````````````````````` example
  4398. ` `` `
  4399. .
  4400. <p><code>``</code></p>
  4401. ````````````````````````````````
  4402. [Line endings] are treated like spaces:
  4403. ```````````````````````````````` example
  4404. ``
  4405. foo
  4406. ``
  4407. .
  4408. <p><code>foo</code></p>
  4409. ````````````````````````````````
  4410. Interior spaces and [line endings] are collapsed into
  4411. single spaces, just as they would be by a browser:
  4412. ```````````````````````````````` example
  4413. `foo bar
  4414. baz`
  4415. .
  4416. <p><code>foo bar baz</code></p>
  4417. ````````````````````````````````
  4418. Not all [Unicode whitespace] (for instance, non-breaking space) is
  4419. collapsed, however:
  4420. ```````````````````````````````` example
  4421. `a  b`
  4422. .
  4423. <p><code>a  b</code></p>
  4424. ````````````````````````````````
  4425. Q: Why not just leave the spaces, since browsers will collapse them
  4426. anyway? A: Because we might be targeting a non-HTML format, and we
  4427. shouldn't rely on HTML-specific rendering assumptions.
  4428. (Existing implementations differ in their treatment of internal
  4429. spaces and [line endings]. Some, including `Markdown.pl` and
  4430. `showdown`, convert an internal [line ending] into a
  4431. `<br />` tag. But this makes things difficult for those who like to
  4432. hard-wrap their paragraphs, since a line break in the midst of a code
  4433. span will cause an unintended line break in the output. Others just
  4434. leave internal spaces as they are, which is fine if only HTML is being
  4435. targeted.)
  4436. ```````````````````````````````` example
  4437. `foo `` bar`
  4438. .
  4439. <p><code>foo `` bar</code></p>
  4440. ````````````````````````````````
  4441. Note that backslash escapes do not work in code spans. All backslashes
  4442. are treated literally:
  4443. ```````````````````````````````` example
  4444. `foo\`bar`
  4445. .
  4446. <p><code>foo\</code>bar`</p>
  4447. ````````````````````````````````
  4448. Backslash escapes are never needed, because one can always choose a
  4449. string of *n* backtick characters as delimiters, where the code does
  4450. not contain any strings of exactly *n* backtick characters.
  4451. Code span backticks have higher precedence than any other inline
  4452. constructs except HTML tags and autolinks. Thus, for example, this is
  4453. not parsed as emphasized text, since the second `*` is part of a code
  4454. span:
  4455. ```````````````````````````````` example
  4456. *foo`*`
  4457. .
  4458. <p>*foo<code>*</code></p>
  4459. ````````````````````````````````
  4460. And this is not parsed as a link:
  4461. ```````````````````````````````` example
  4462. [not a `link](/foo`)
  4463. .
  4464. <p>[not a <code>link](/foo</code>)</p>
  4465. ````````````````````````````````
  4466. Code spans, HTML tags, and autolinks have the same precedence.
  4467. Thus, this is code:
  4468. ```````````````````````````````` example
  4469. `<a href="`">`
  4470. .
  4471. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4472. ````````````````````````````````
  4473. But this is an HTML tag:
  4474. ```````````````````````````````` example
  4475. <a href="`">`
  4476. .
  4477. <p><a href="`">`</p>
  4478. ````````````````````````````````
  4479. And this is code:
  4480. ```````````````````````````````` example
  4481. `<http://foo.bar.`baz>`
  4482. .
  4483. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4484. ````````````````````````````````
  4485. But this is an autolink:
  4486. ```````````````````````````````` example
  4487. <http://foo.bar.`baz>`
  4488. .
  4489. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4490. ````````````````````````````````
  4491. When a backtick string is not closed by a matching backtick string,
  4492. we just have literal backticks:
  4493. ```````````````````````````````` example
  4494. ```foo``
  4495. .
  4496. <p>```foo``</p>
  4497. ````````````````````````````````
  4498. ```````````````````````````````` example
  4499. `foo
  4500. .
  4501. <p>`foo</p>
  4502. ````````````````````````````````
  4503. ## Emphasis and strong emphasis
  4504. John Gruber's original [Markdown syntax
  4505. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4506. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4507. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4508. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4509. > tag.
  4510. This is enough for most users, but these rules leave much undecided,
  4511. especially when it comes to nested emphasis. The original
  4512. `Markdown.pl` test suite makes it clear that triple `***` and
  4513. `___` delimiters can be used for strong emphasis, and most
  4514. implementations have also allowed the following patterns:
  4515. ``` markdown
  4516. ***strong emph***
  4517. ***strong** in emph*
  4518. ***emph* in strong**
  4519. **in strong *emph***
  4520. *in emph **strong***
  4521. ```
  4522. The following patterns are less widely supported, but the intent
  4523. is clear and they are useful (especially in contexts like bibliography
  4524. entries):
  4525. ``` markdown
  4526. *emph *with emph* in it*
  4527. **strong **with strong** in it**
  4528. ```
  4529. Many implementations have also restricted intraword emphasis to
  4530. the `*` forms, to avoid unwanted emphasis in words containing
  4531. internal underscores. (It is best practice to put these in code
  4532. spans, but users often do not.)
  4533. ``` markdown
  4534. internal emphasis: foo*bar*baz
  4535. no emphasis: foo_bar_baz
  4536. ```
  4537. The rules given below capture all of these patterns, while allowing
  4538. for efficient parsing strategies that do not backtrack.
  4539. First, some definitions. A [delimiter run](@) is either
  4540. a sequence of one or more `*` characters that is not preceded or
  4541. followed by a `*` character, or a sequence of one or more `_`
  4542. characters that is not preceded or followed by a `_` character.
  4543. A [left-flanking delimiter run](@) is
  4544. a [delimiter run] that is (a) not followed by [Unicode whitespace],
  4545. and (b) either not followed by a [punctuation character], or
  4546. preceded by [Unicode whitespace] or a [punctuation character].
  4547. For purposes of this definition, the beginning and the end of
  4548. the line count as Unicode whitespace.
  4549. A [right-flanking delimiter run](@) is
  4550. a [delimiter run] that is (a) not preceded by [Unicode whitespace],
  4551. and (b) either not preceded by a [punctuation character], or
  4552. followed by [Unicode whitespace] or a [punctuation character].
  4553. For purposes of this definition, the beginning and the end of
  4554. the line count as Unicode whitespace.
  4555. Here are some examples of delimiter runs.
  4556. - left-flanking but not right-flanking:
  4557. ```
  4558. ***abc
  4559. _abc
  4560. **"abc"
  4561. _"abc"
  4562. ```
  4563. - right-flanking but not left-flanking:
  4564. ```
  4565. abc***
  4566. abc_
  4567. "abc"**
  4568. "abc"_
  4569. ```
  4570. - Both left and right-flanking:
  4571. ```
  4572. abc***def
  4573. "abc"_"def"
  4574. ```
  4575. - Neither left nor right-flanking:
  4576. ```
  4577. abc *** def
  4578. a _ b
  4579. ```
  4580. (The idea of distinguishing left-flanking and right-flanking
  4581. delimiter runs based on the character before and the character
  4582. after comes from Roopesh Chander's
  4583. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4584. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4585. run," and its rules for distinguishing left- and right-flanking runs
  4586. are a bit more complex than the ones given here.)
  4587. The following rules define emphasis and strong emphasis:
  4588. 1. A single `*` character [can open emphasis](@)
  4589. iff (if and only if) it is part of a [left-flanking delimiter run].
  4590. 2. A single `_` character [can open emphasis] iff
  4591. it is part of a [left-flanking delimiter run]
  4592. and either (a) not part of a [right-flanking delimiter run]
  4593. or (b) part of a [right-flanking delimiter run]
  4594. preceded by punctuation.
  4595. 3. A single `*` character [can close emphasis](@)
  4596. iff it is part of a [right-flanking delimiter run].
  4597. 4. A single `_` character [can close emphasis] iff
  4598. it is part of a [right-flanking delimiter run]
  4599. and either (a) not part of a [left-flanking delimiter run]
  4600. or (b) part of a [left-flanking delimiter run]
  4601. followed by punctuation.
  4602. 5. A double `**` [can open strong emphasis](@)
  4603. iff it is part of a [left-flanking delimiter run].
  4604. 6. A double `__` [can open strong emphasis] iff
  4605. it is part of a [left-flanking delimiter run]
  4606. and either (a) not part of a [right-flanking delimiter run]
  4607. or (b) part of a [right-flanking delimiter run]
  4608. preceded by punctuation.
  4609. 7. A double `**` [can close strong emphasis](@)
  4610. iff it is part of a [right-flanking delimiter run].
  4611. 8. A double `__` [can close strong emphasis] iff
  4612. it is part of a [right-flanking delimiter run]
  4613. and either (a) not part of a [left-flanking delimiter run]
  4614. or (b) part of a [left-flanking delimiter run]
  4615. followed by punctuation.
  4616. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4617. with a delimiter that [can close emphasis], and that uses the same
  4618. character (`_` or `*`) as the opening delimiter. The
  4619. opening and closing delimiters must belong to separate
  4620. [delimiter runs]. If one of the delimiters can both
  4621. open and close emphasis, then the sum of the lengths of the
  4622. delimiter runs containing the opening and closing delimiters
  4623. must not be a multiple of 3.
  4624. 10. Strong emphasis begins with a delimiter that
  4625. [can open strong emphasis] and ends with a delimiter that
  4626. [can close strong emphasis], and that uses the same character
  4627. (`_` or `*`) as the opening delimiter. The
  4628. opening and closing delimiters must belong to separate
  4629. [delimiter runs]. If one of the delimiters can both open
  4630. and close strong emphasis, then the sum of the lengths of
  4631. the delimiter runs containing the opening and closing
  4632. delimiters must not be a multiple of 3.
  4633. 11. A literal `*` character cannot occur at the beginning or end of
  4634. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4635. is backslash-escaped.
  4636. 12. A literal `_` character cannot occur at the beginning or end of
  4637. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4638. is backslash-escaped.
  4639. Where rules 1--12 above are compatible with multiple parsings,
  4640. the following principles resolve ambiguity:
  4641. 13. The number of nestings should be minimized. Thus, for example,
  4642. an interpretation `<strong>...</strong>` is always preferred to
  4643. `<em><em>...</em></em>`.
  4644. 14. An interpretation `<em><strong>...</strong></em>` is always
  4645. preferred to `<strong><em>..</em></strong>`.
  4646. 15. When two potential emphasis or strong emphasis spans overlap,
  4647. so that the second begins before the first ends and ends after
  4648. the first ends, the first takes precedence. Thus, for example,
  4649. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4650. than `*foo <em>bar* baz</em>`.
  4651. 16. When there are two potential emphasis or strong emphasis spans
  4652. with the same closing delimiter, the shorter one (the one that
  4653. opens later) takes precedence. Thus, for example,
  4654. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4655. rather than `<strong>foo **bar baz</strong>`.
  4656. 17. Inline code spans, links, images, and HTML tags group more tightly
  4657. than emphasis. So, when there is a choice between an interpretation
  4658. that contains one of these elements and one that does not, the
  4659. former always wins. Thus, for example, `*[foo*](bar)` is
  4660. parsed as `*<a href="bar">foo*</a>` rather than as
  4661. `<em>[foo</em>](bar)`.
  4662. These rules can be illustrated through a series of examples.
  4663. Rule 1:
  4664. ```````````````````````````````` example
  4665. *foo bar*
  4666. .
  4667. <p><em>foo bar</em></p>
  4668. ````````````````````````````````
  4669. This is not emphasis, because the opening `*` is followed by
  4670. whitespace, and hence not part of a [left-flanking delimiter run]:
  4671. ```````````````````````````````` example
  4672. a * foo bar*
  4673. .
  4674. <p>a * foo bar*</p>
  4675. ````````````````````````````````
  4676. This is not emphasis, because the opening `*` is preceded
  4677. by an alphanumeric and followed by punctuation, and hence
  4678. not part of a [left-flanking delimiter run]:
  4679. ```````````````````````````````` example
  4680. a*"foo"*
  4681. .
  4682. <p>a*&quot;foo&quot;*</p>
  4683. ````````````````````````````````
  4684. Unicode nonbreaking spaces count as whitespace, too:
  4685. ```````````````````````````````` example
  4686. * a *
  4687. .
  4688. <p>* a *</p>
  4689. ````````````````````````````````
  4690. Intraword emphasis with `*` is permitted:
  4691. ```````````````````````````````` example
  4692. foo*bar*
  4693. .
  4694. <p>foo<em>bar</em></p>
  4695. ````````````````````````````````
  4696. ```````````````````````````````` example
  4697. 5*6*78
  4698. .
  4699. <p>5<em>6</em>78</p>
  4700. ````````````````````````````````
  4701. Rule 2:
  4702. ```````````````````````````````` example
  4703. _foo bar_
  4704. .
  4705. <p><em>foo bar</em></p>
  4706. ````````````````````````````````
  4707. This is not emphasis, because the opening `_` is followed by
  4708. whitespace:
  4709. ```````````````````````````````` example
  4710. _ foo bar_
  4711. .
  4712. <p>_ foo bar_</p>
  4713. ````````````````````````````````
  4714. This is not emphasis, because the opening `_` is preceded
  4715. by an alphanumeric and followed by punctuation:
  4716. ```````````````````````````````` example
  4717. a_"foo"_
  4718. .
  4719. <p>a_&quot;foo&quot;_</p>
  4720. ````````````````````````````````
  4721. Emphasis with `_` is not allowed inside words:
  4722. ```````````````````````````````` example
  4723. foo_bar_
  4724. .
  4725. <p>foo_bar_</p>
  4726. ````````````````````````````````
  4727. ```````````````````````````````` example
  4728. 5_6_78
  4729. .
  4730. <p>5_6_78</p>
  4731. ````````````````````````````````
  4732. ```````````````````````````````` example
  4733. пристаням_стремятся_
  4734. .
  4735. <p>пристаням_стремятся_</p>
  4736. ````````````````````````````````
  4737. Here `_` does not generate emphasis, because the first delimiter run
  4738. is right-flanking and the second left-flanking:
  4739. ```````````````````````````````` example
  4740. aa_"bb"_cc
  4741. .
  4742. <p>aa_&quot;bb&quot;_cc</p>
  4743. ````````````````````````````````
  4744. This is emphasis, even though the opening delimiter is
  4745. both left- and right-flanking, because it is preceded by
  4746. punctuation:
  4747. ```````````````````````````````` example
  4748. foo-_(bar)_
  4749. .
  4750. <p>foo-<em>(bar)</em></p>
  4751. ````````````````````````````````
  4752. Rule 3:
  4753. This is not emphasis, because the closing delimiter does
  4754. not match the opening delimiter:
  4755. ```````````````````````````````` example
  4756. _foo*
  4757. .
  4758. <p>_foo*</p>
  4759. ````````````````````````````````
  4760. This is not emphasis, because the closing `*` is preceded by
  4761. whitespace:
  4762. ```````````````````````````````` example
  4763. *foo bar *
  4764. .
  4765. <p>*foo bar *</p>
  4766. ````````````````````````````````
  4767. A newline also counts as whitespace:
  4768. ```````````````````````````````` example
  4769. *foo bar
  4770. *
  4771. .
  4772. <p>*foo bar
  4773. *</p>
  4774. ````````````````````````````````
  4775. This is not emphasis, because the second `*` is
  4776. preceded by punctuation and followed by an alphanumeric
  4777. (hence it is not part of a [right-flanking delimiter run]:
  4778. ```````````````````````````````` example
  4779. *(*foo)
  4780. .
  4781. <p>*(*foo)</p>
  4782. ````````````````````````````````
  4783. The point of this restriction is more easily appreciated
  4784. with this example:
  4785. ```````````````````````````````` example
  4786. *(*foo*)*
  4787. .
  4788. <p><em>(<em>foo</em>)</em></p>
  4789. ````````````````````````````````
  4790. Intraword emphasis with `*` is allowed:
  4791. ```````````````````````````````` example
  4792. *foo*bar
  4793. .
  4794. <p><em>foo</em>bar</p>
  4795. ````````````````````````````````
  4796. Rule 4:
  4797. This is not emphasis, because the closing `_` is preceded by
  4798. whitespace:
  4799. ```````````````````````````````` example
  4800. _foo bar _
  4801. .
  4802. <p>_foo bar _</p>
  4803. ````````````````````````````````
  4804. This is not emphasis, because the second `_` is
  4805. preceded by punctuation and followed by an alphanumeric:
  4806. ```````````````````````````````` example
  4807. _(_foo)
  4808. .
  4809. <p>_(_foo)</p>
  4810. ````````````````````````````````
  4811. This is emphasis within emphasis:
  4812. ```````````````````````````````` example
  4813. _(_foo_)_
  4814. .
  4815. <p><em>(<em>foo</em>)</em></p>
  4816. ````````````````````````````````
  4817. Intraword emphasis is disallowed for `_`:
  4818. ```````````````````````````````` example
  4819. _foo_bar
  4820. .
  4821. <p>_foo_bar</p>
  4822. ````````````````````````````````
  4823. ```````````````````````````````` example
  4824. _пристаням_стремятся
  4825. .
  4826. <p>_пристаням_стремятся</p>
  4827. ````````````````````````````````
  4828. ```````````````````````````````` example
  4829. _foo_bar_baz_
  4830. .
  4831. <p><em>foo_bar_baz</em></p>
  4832. ````````````````````````````````
  4833. This is emphasis, even though the closing delimiter is
  4834. both left- and right-flanking, because it is followed by
  4835. punctuation:
  4836. ```````````````````````````````` example
  4837. _(bar)_.
  4838. .
  4839. <p><em>(bar)</em>.</p>
  4840. ````````````````````````````````
  4841. Rule 5:
  4842. ```````````````````````````````` example
  4843. **foo bar**
  4844. .
  4845. <p><strong>foo bar</strong></p>
  4846. ````````````````````````````````
  4847. This is not strong emphasis, because the opening delimiter is
  4848. followed by whitespace:
  4849. ```````````````````````````````` example
  4850. ** foo bar**
  4851. .
  4852. <p>** foo bar**</p>
  4853. ````````````````````````````````
  4854. This is not strong emphasis, because the opening `**` is preceded
  4855. by an alphanumeric and followed by punctuation, and hence
  4856. not part of a [left-flanking delimiter run]:
  4857. ```````````````````````````````` example
  4858. a**"foo"**
  4859. .
  4860. <p>a**&quot;foo&quot;**</p>
  4861. ````````````````````````````````
  4862. Intraword strong emphasis with `**` is permitted:
  4863. ```````````````````````````````` example
  4864. foo**bar**
  4865. .
  4866. <p>foo<strong>bar</strong></p>
  4867. ````````````````````````````````
  4868. Rule 6:
  4869. ```````````````````````````````` example
  4870. __foo bar__
  4871. .
  4872. <p><strong>foo bar</strong></p>
  4873. ````````````````````````````````
  4874. This is not strong emphasis, because the opening delimiter is
  4875. followed by whitespace:
  4876. ```````````````````````````````` example
  4877. __ foo bar__
  4878. .
  4879. <p>__ foo bar__</p>
  4880. ````````````````````````````````
  4881. A newline counts as whitespace:
  4882. ```````````````````````````````` example
  4883. __
  4884. foo bar__
  4885. .
  4886. <p>__
  4887. foo bar__</p>
  4888. ````````````````````````````````
  4889. This is not strong emphasis, because the opening `__` is preceded
  4890. by an alphanumeric and followed by punctuation:
  4891. ```````````````````````````````` example
  4892. a__"foo"__
  4893. .
  4894. <p>a__&quot;foo&quot;__</p>
  4895. ````````````````````````````````
  4896. Intraword strong emphasis is forbidden with `__`:
  4897. ```````````````````````````````` example
  4898. foo__bar__
  4899. .
  4900. <p>foo__bar__</p>
  4901. ````````````````````````````````
  4902. ```````````````````````````````` example
  4903. 5__6__78
  4904. .
  4905. <p>5__6__78</p>
  4906. ````````````````````````````````
  4907. ```````````````````````````````` example
  4908. пристаням__стремятся__
  4909. .
  4910. <p>пристаням__стремятся__</p>
  4911. ````````````````````````````````
  4912. ```````````````````````````````` example
  4913. __foo, __bar__, baz__
  4914. .
  4915. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  4916. ````````````````````````````````
  4917. This is strong emphasis, even though the opening delimiter is
  4918. both left- and right-flanking, because it is preceded by
  4919. punctuation:
  4920. ```````````````````````````````` example
  4921. foo-__(bar)__
  4922. .
  4923. <p>foo-<strong>(bar)</strong></p>
  4924. ````````````````````````````````
  4925. Rule 7:
  4926. This is not strong emphasis, because the closing delimiter is preceded
  4927. by whitespace:
  4928. ```````````````````````````````` example
  4929. **foo bar **
  4930. .
  4931. <p>**foo bar **</p>
  4932. ````````````````````````````````
  4933. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  4934. Rule 11.)
  4935. This is not strong emphasis, because the second `**` is
  4936. preceded by punctuation and followed by an alphanumeric:
  4937. ```````````````````````````````` example
  4938. **(**foo)
  4939. .
  4940. <p>**(**foo)</p>
  4941. ````````````````````````````````
  4942. The point of this restriction is more easily appreciated
  4943. with these examples:
  4944. ```````````````````````````````` example
  4945. *(**foo**)*
  4946. .
  4947. <p><em>(<strong>foo</strong>)</em></p>
  4948. ````````````````````````````````
  4949. ```````````````````````````````` example
  4950. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  4951. *Asclepias physocarpa*)**
  4952. .
  4953. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  4954. <em>Asclepias physocarpa</em>)</strong></p>
  4955. ````````````````````````````````
  4956. ```````````````````````````````` example
  4957. **foo "*bar*" foo**
  4958. .
  4959. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  4960. ````````````````````````````````
  4961. Intraword emphasis:
  4962. ```````````````````````````````` example
  4963. **foo**bar
  4964. .
  4965. <p><strong>foo</strong>bar</p>
  4966. ````````````````````````````````
  4967. Rule 8:
  4968. This is not strong emphasis, because the closing delimiter is
  4969. preceded by whitespace:
  4970. ```````````````````````````````` example
  4971. __foo bar __
  4972. .
  4973. <p>__foo bar __</p>
  4974. ````````````````````````````````
  4975. This is not strong emphasis, because the second `__` is
  4976. preceded by punctuation and followed by an alphanumeric:
  4977. ```````````````````````````````` example
  4978. __(__foo)
  4979. .
  4980. <p>__(__foo)</p>
  4981. ````````````````````````````````
  4982. The point of this restriction is more easily appreciated
  4983. with this example:
  4984. ```````````````````````````````` example
  4985. _(__foo__)_
  4986. .
  4987. <p><em>(<strong>foo</strong>)</em></p>
  4988. ````````````````````````````````
  4989. Intraword strong emphasis is forbidden with `__`:
  4990. ```````````````````````````````` example
  4991. __foo__bar
  4992. .
  4993. <p>__foo__bar</p>
  4994. ````````````````````````````````
  4995. ```````````````````````````````` example
  4996. __пристаням__стремятся
  4997. .
  4998. <p>__пристаням__стремятся</p>
  4999. ````````````````````````````````
  5000. ```````````````````````````````` example
  5001. __foo__bar__baz__
  5002. .
  5003. <p><strong>foo__bar__baz</strong></p>
  5004. ````````````````````````````````
  5005. This is strong emphasis, even though the closing delimiter is
  5006. both left- and right-flanking, because it is followed by
  5007. punctuation:
  5008. ```````````````````````````````` example
  5009. __(bar)__.
  5010. .
  5011. <p><strong>(bar)</strong>.</p>
  5012. ````````````````````````````````
  5013. Rule 9:
  5014. Any nonempty sequence of inline elements can be the contents of an
  5015. emphasized span.
  5016. ```````````````````````````````` example
  5017. *foo [bar](/url)*
  5018. .
  5019. <p><em>foo <a href="/url">bar</a></em></p>
  5020. ````````````````````````````````
  5021. ```````````````````````````````` example
  5022. *foo
  5023. bar*
  5024. .
  5025. <p><em>foo
  5026. bar</em></p>
  5027. ````````````````````````````````
  5028. In particular, emphasis and strong emphasis can be nested
  5029. inside emphasis:
  5030. ```````````````````````````````` example
  5031. _foo __bar__ baz_
  5032. .
  5033. <p><em>foo <strong>bar</strong> baz</em></p>
  5034. ````````````````````````````````
  5035. ```````````````````````````````` example
  5036. _foo _bar_ baz_
  5037. .
  5038. <p><em>foo <em>bar</em> baz</em></p>
  5039. ````````````````````````````````
  5040. ```````````````````````````````` example
  5041. __foo_ bar_
  5042. .
  5043. <p><em><em>foo</em> bar</em></p>
  5044. ````````````````````````````````
  5045. ```````````````````````````````` example
  5046. *foo *bar**
  5047. .
  5048. <p><em>foo <em>bar</em></em></p>
  5049. ````````````````````````````````
  5050. ```````````````````````````````` example
  5051. *foo **bar** baz*
  5052. .
  5053. <p><em>foo <strong>bar</strong> baz</em></p>
  5054. ````````````````````````````````
  5055. ```````````````````````````````` example
  5056. *foo**bar**baz*
  5057. .
  5058. <p><em>foo<strong>bar</strong>baz</em></p>
  5059. ````````````````````````````````
  5060. Note that in the preceding case, the interpretation
  5061. ``` markdown
  5062. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5063. ```
  5064. is precluded by the condition that a delimiter that
  5065. can both open and close (like the `*` after `foo`)
  5066. cannot form emphasis if the sum of the lengths of
  5067. the delimiter runs containing the opening and
  5068. closing delimiters is a multiple of 3.
  5069. The same condition ensures that the following
  5070. cases are all strong emphasis nested inside
  5071. emphasis, even when the interior spaces are
  5072. omitted:
  5073. ```````````````````````````````` example
  5074. ***foo** bar*
  5075. .
  5076. <p><em><strong>foo</strong> bar</em></p>
  5077. ````````````````````````````````
  5078. ```````````````````````````````` example
  5079. *foo **bar***
  5080. .
  5081. <p><em>foo <strong>bar</strong></em></p>
  5082. ````````````````````````````````
  5083. ```````````````````````````````` example
  5084. *foo**bar***
  5085. .
  5086. <p><em>foo<strong>bar</strong></em></p>
  5087. ````````````````````````````````
  5088. Indefinite levels of nesting are possible:
  5089. ```````````````````````````````` example
  5090. *foo **bar *baz* bim** bop*
  5091. .
  5092. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5093. ````````````````````````````````
  5094. ```````````````````````````````` example
  5095. *foo [*bar*](/url)*
  5096. .
  5097. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5098. ````````````````````````````````
  5099. There can be no empty emphasis or strong emphasis:
  5100. ```````````````````````````````` example
  5101. ** is not an empty emphasis
  5102. .
  5103. <p>** is not an empty emphasis</p>
  5104. ````````````````````````````````
  5105. ```````````````````````````````` example
  5106. **** is not an empty strong emphasis
  5107. .
  5108. <p>**** is not an empty strong emphasis</p>
  5109. ````````````````````````````````
  5110. Rule 10:
  5111. Any nonempty sequence of inline elements can be the contents of an
  5112. strongly emphasized span.
  5113. ```````````````````````````````` example
  5114. **foo [bar](/url)**
  5115. .
  5116. <p><strong>foo <a href="/url">bar</a></strong></p>
  5117. ````````````````````````````````
  5118. ```````````````````````````````` example
  5119. **foo
  5120. bar**
  5121. .
  5122. <p><strong>foo
  5123. bar</strong></p>
  5124. ````````````````````````````````
  5125. In particular, emphasis and strong emphasis can be nested
  5126. inside strong emphasis:
  5127. ```````````````````````````````` example
  5128. __foo _bar_ baz__
  5129. .
  5130. <p><strong>foo <em>bar</em> baz</strong></p>
  5131. ````````````````````````````````
  5132. ```````````````````````````````` example
  5133. __foo __bar__ baz__
  5134. .
  5135. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5136. ````````````````````````````````
  5137. ```````````````````````````````` example
  5138. ____foo__ bar__
  5139. .
  5140. <p><strong><strong>foo</strong> bar</strong></p>
  5141. ````````````````````````````````
  5142. ```````````````````````````````` example
  5143. **foo **bar****
  5144. .
  5145. <p><strong>foo <strong>bar</strong></strong></p>
  5146. ````````````````````````````````
  5147. ```````````````````````````````` example
  5148. **foo *bar* baz**
  5149. .
  5150. <p><strong>foo <em>bar</em> baz</strong></p>
  5151. ````````````````````````````````
  5152. ```````````````````````````````` example
  5153. **foo*bar*baz**
  5154. .
  5155. <p><strong>foo<em>bar</em>baz</strong></p>
  5156. ````````````````````````````````
  5157. ```````````````````````````````` example
  5158. ***foo* bar**
  5159. .
  5160. <p><strong><em>foo</em> bar</strong></p>
  5161. ````````````````````````````````
  5162. ```````````````````````````````` example
  5163. **foo *bar***
  5164. .
  5165. <p><strong>foo <em>bar</em></strong></p>
  5166. ````````````````````````````````
  5167. Indefinite levels of nesting are possible:
  5168. ```````````````````````````````` example
  5169. **foo *bar **baz**
  5170. bim* bop**
  5171. .
  5172. <p><strong>foo <em>bar <strong>baz</strong>
  5173. bim</em> bop</strong></p>
  5174. ````````````````````````````````
  5175. ```````````````````````````````` example
  5176. **foo [*bar*](/url)**
  5177. .
  5178. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5179. ````````````````````````````````
  5180. There can be no empty emphasis or strong emphasis:
  5181. ```````````````````````````````` example
  5182. __ is not an empty emphasis
  5183. .
  5184. <p>__ is not an empty emphasis</p>
  5185. ````````````````````````````````
  5186. ```````````````````````````````` example
  5187. ____ is not an empty strong emphasis
  5188. .
  5189. <p>____ is not an empty strong emphasis</p>
  5190. ````````````````````````````````
  5191. Rule 11:
  5192. ```````````````````````````````` example
  5193. foo ***
  5194. .
  5195. <p>foo ***</p>
  5196. ````````````````````````````````
  5197. ```````````````````````````````` example
  5198. foo *\**
  5199. .
  5200. <p>foo <em>*</em></p>
  5201. ````````````````````````````````
  5202. ```````````````````````````````` example
  5203. foo *_*
  5204. .
  5205. <p>foo <em>_</em></p>
  5206. ````````````````````````````````
  5207. ```````````````````````````````` example
  5208. foo *****
  5209. .
  5210. <p>foo *****</p>
  5211. ````````````````````````````````
  5212. ```````````````````````````````` example
  5213. foo **\***
  5214. .
  5215. <p>foo <strong>*</strong></p>
  5216. ````````````````````````````````
  5217. ```````````````````````````````` example
  5218. foo **_**
  5219. .
  5220. <p>foo <strong>_</strong></p>
  5221. ````````````````````````````````
  5222. Note that when delimiters do not match evenly, Rule 11 determines
  5223. that the excess literal `*` characters will appear outside of the
  5224. emphasis, rather than inside it:
  5225. ```````````````````````````````` example
  5226. **foo*
  5227. .
  5228. <p>*<em>foo</em></p>
  5229. ````````````````````````````````
  5230. ```````````````````````````````` example
  5231. *foo**
  5232. .
  5233. <p><em>foo</em>*</p>
  5234. ````````````````````````````````
  5235. ```````````````````````````````` example
  5236. ***foo**
  5237. .
  5238. <p>*<strong>foo</strong></p>
  5239. ````````````````````````````````
  5240. ```````````````````````````````` example
  5241. ****foo*
  5242. .
  5243. <p>***<em>foo</em></p>
  5244. ````````````````````````````````
  5245. ```````````````````````````````` example
  5246. **foo***
  5247. .
  5248. <p><strong>foo</strong>*</p>
  5249. ````````````````````````````````
  5250. ```````````````````````````````` example
  5251. *foo****
  5252. .
  5253. <p><em>foo</em>***</p>
  5254. ````````````````````````````````
  5255. Rule 12:
  5256. ```````````````````````````````` example
  5257. foo ___
  5258. .
  5259. <p>foo ___</p>
  5260. ````````````````````````````````
  5261. ```````````````````````````````` example
  5262. foo _\__
  5263. .
  5264. <p>foo <em>_</em></p>
  5265. ````````````````````````````````
  5266. ```````````````````````````````` example
  5267. foo _*_
  5268. .
  5269. <p>foo <em>*</em></p>
  5270. ````````````````````````````````
  5271. ```````````````````````````````` example
  5272. foo _____
  5273. .
  5274. <p>foo _____</p>
  5275. ````````````````````````````````
  5276. ```````````````````````````````` example
  5277. foo __\___
  5278. .
  5279. <p>foo <strong>_</strong></p>
  5280. ````````````````````````````````
  5281. ```````````````````````````````` example
  5282. foo __*__
  5283. .
  5284. <p>foo <strong>*</strong></p>
  5285. ````````````````````````````````
  5286. ```````````````````````````````` example
  5287. __foo_
  5288. .
  5289. <p>_<em>foo</em></p>
  5290. ````````````````````````````````
  5291. Note that when delimiters do not match evenly, Rule 12 determines
  5292. that the excess literal `_` characters will appear outside of the
  5293. emphasis, rather than inside it:
  5294. ```````````````````````````````` example
  5295. _foo__
  5296. .
  5297. <p><em>foo</em>_</p>
  5298. ````````````````````````````````
  5299. ```````````````````````````````` example
  5300. ___foo__
  5301. .
  5302. <p>_<strong>foo</strong></p>
  5303. ````````````````````````````````
  5304. ```````````````````````````````` example
  5305. ____foo_
  5306. .
  5307. <p>___<em>foo</em></p>
  5308. ````````````````````````````````
  5309. ```````````````````````````````` example
  5310. __foo___
  5311. .
  5312. <p><strong>foo</strong>_</p>
  5313. ````````````````````````````````
  5314. ```````````````````````````````` example
  5315. _foo____
  5316. .
  5317. <p><em>foo</em>___</p>
  5318. ````````````````````````````````
  5319. Rule 13 implies that if you want emphasis nested directly inside
  5320. emphasis, you must use different delimiters:
  5321. ```````````````````````````````` example
  5322. **foo**
  5323. .
  5324. <p><strong>foo</strong></p>
  5325. ````````````````````````````````
  5326. ```````````````````````````````` example
  5327. *_foo_*
  5328. .
  5329. <p><em><em>foo</em></em></p>
  5330. ````````````````````````````````
  5331. ```````````````````````````````` example
  5332. __foo__
  5333. .
  5334. <p><strong>foo</strong></p>
  5335. ````````````````````````````````
  5336. ```````````````````````````````` example
  5337. _*foo*_
  5338. .
  5339. <p><em><em>foo</em></em></p>
  5340. ````````````````````````````````
  5341. However, strong emphasis within strong emphasis is possible without
  5342. switching delimiters:
  5343. ```````````````````````````````` example
  5344. ****foo****
  5345. .
  5346. <p><strong><strong>foo</strong></strong></p>
  5347. ````````````````````````````````
  5348. ```````````````````````````````` example
  5349. ____foo____
  5350. .
  5351. <p><strong><strong>foo</strong></strong></p>
  5352. ````````````````````````````````
  5353. Rule 13 can be applied to arbitrarily long sequences of
  5354. delimiters:
  5355. ```````````````````````````````` example
  5356. ******foo******
  5357. .
  5358. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5359. ````````````````````````````````
  5360. Rule 14:
  5361. ```````````````````````````````` example
  5362. ***foo***
  5363. .
  5364. <p><em><strong>foo</strong></em></p>
  5365. ````````````````````````````````
  5366. ```````````````````````````````` example
  5367. _____foo_____
  5368. .
  5369. <p><em><strong><strong>foo</strong></strong></em></p>
  5370. ````````````````````````````````
  5371. Rule 15:
  5372. ```````````````````````````````` example
  5373. *foo _bar* baz_
  5374. .
  5375. <p><em>foo _bar</em> baz_</p>
  5376. ````````````````````````````````
  5377. ```````````````````````````````` example
  5378. *foo __bar *baz bim__ bam*
  5379. .
  5380. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5381. ````````````````````````````````
  5382. Rule 16:
  5383. ```````````````````````````````` example
  5384. **foo **bar baz**
  5385. .
  5386. <p>**foo <strong>bar baz</strong></p>
  5387. ````````````````````````````````
  5388. ```````````````````````````````` example
  5389. *foo *bar baz*
  5390. .
  5391. <p>*foo <em>bar baz</em></p>
  5392. ````````````````````````````````
  5393. Rule 17:
  5394. ```````````````````````````````` example
  5395. *[bar*](/url)
  5396. .
  5397. <p>*<a href="/url">bar*</a></p>
  5398. ````````````````````````````````
  5399. ```````````````````````````````` example
  5400. _foo [bar_](/url)
  5401. .
  5402. <p>_foo <a href="/url">bar_</a></p>
  5403. ````````````````````````````````
  5404. ```````````````````````````````` example
  5405. *<img src="foo" title="*"/>
  5406. .
  5407. <p>*<img src="foo" title="*"/></p>
  5408. ````````````````````````````````
  5409. ```````````````````````````````` example
  5410. **<a href="**">
  5411. .
  5412. <p>**<a href="**"></p>
  5413. ````````````````````````````````
  5414. ```````````````````````````````` example
  5415. __<a href="__">
  5416. .
  5417. <p>__<a href="__"></p>
  5418. ````````````````````````````````
  5419. ```````````````````````````````` example
  5420. *a `*`*
  5421. .
  5422. <p><em>a <code>*</code></em></p>
  5423. ````````````````````````````````
  5424. ```````````````````````````````` example
  5425. _a `_`_
  5426. .
  5427. <p><em>a <code>_</code></em></p>
  5428. ````````````````````````````````
  5429. ```````````````````````````````` example
  5430. **a<http://foo.bar/?q=**>
  5431. .
  5432. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5433. ````````````````````````````````
  5434. ```````````````````````````````` example
  5435. __a<http://foo.bar/?q=__>
  5436. .
  5437. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5438. ````````````````````````````````
  5439. ## Links
  5440. A link contains [link text] (the visible text), a [link destination]
  5441. (the URI that is the link destination), and optionally a [link title].
  5442. There are two basic kinds of links in Markdown. In [inline links] the
  5443. destination and title are given immediately after the link text. In
  5444. [reference links] the destination and title are defined elsewhere in
  5445. the document.
  5446. A [link text](@) consists of a sequence of zero or more
  5447. inline elements enclosed by square brackets (`[` and `]`). The
  5448. following rules apply:
  5449. - Links may not contain other links, at any level of nesting. If
  5450. multiple otherwise valid link definitions appear nested inside each
  5451. other, the inner-most definition is used.
  5452. - Brackets are allowed in the [link text] only if (a) they
  5453. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5454. with an open bracket `[`, a sequence of zero or more inlines, and
  5455. a close bracket `]`.
  5456. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5457. than the brackets in link text. Thus, for example,
  5458. `` [foo`]` `` could not be a link text, since the second `]`
  5459. is part of a code span.
  5460. - The brackets in link text bind more tightly than markers for
  5461. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5462. A [link destination](@) consists of either
  5463. - a sequence of zero or more characters between an opening `<` and a
  5464. closing `>` that contains no spaces, line breaks, or unescaped
  5465. `<` or `>` characters, or
  5466. - a nonempty sequence of characters that does not include
  5467. ASCII space or control characters, and includes parentheses
  5468. only if (a) they are backslash-escaped or (b) they are part of
  5469. a balanced pair of unescaped parentheses.
  5470. A [link title](@) consists of either
  5471. - a sequence of zero or more characters between straight double-quote
  5472. characters (`"`), including a `"` character only if it is
  5473. backslash-escaped, or
  5474. - a sequence of zero or more characters between straight single-quote
  5475. characters (`'`), including a `'` character only if it is
  5476. backslash-escaped, or
  5477. - a sequence of zero or more characters between matching parentheses
  5478. (`(...)`), including a `)` character only if it is backslash-escaped.
  5479. Although [link titles] may span multiple lines, they may not contain
  5480. a [blank line].
  5481. An [inline link](@) consists of a [link text] followed immediately
  5482. by a left parenthesis `(`, optional [whitespace], an optional
  5483. [link destination], an optional [link title] separated from the link
  5484. destination by [whitespace], optional [whitespace], and a right
  5485. parenthesis `)`. The link's text consists of the inlines contained
  5486. in the [link text] (excluding the enclosing square brackets).
  5487. The link's URI consists of the link destination, excluding enclosing
  5488. `<...>` if present, with backslash-escapes in effect as described
  5489. above. The link's title consists of the link title, excluding its
  5490. enclosing delimiters, with backslash-escapes in effect as described
  5491. above.
  5492. Here is a simple inline link:
  5493. ```````````````````````````````` example
  5494. [link](/uri "title")
  5495. .
  5496. <p><a href="/uri" title="title">link</a></p>
  5497. ````````````````````````````````
  5498. The title may be omitted:
  5499. ```````````````````````````````` example
  5500. [link](/uri)
  5501. .
  5502. <p><a href="/uri">link</a></p>
  5503. ````````````````````````````````
  5504. Both the title and the destination may be omitted:
  5505. ```````````````````````````````` example
  5506. [link]()
  5507. .
  5508. <p><a href="">link</a></p>
  5509. ````````````````````````````````
  5510. ```````````````````````````````` example
  5511. [link](<>)
  5512. .
  5513. <p><a href="">link</a></p>
  5514. ````````````````````````````````
  5515. The destination cannot contain spaces or line breaks,
  5516. even if enclosed in pointy brackets:
  5517. ```````````````````````````````` example
  5518. [link](/my uri)
  5519. .
  5520. <p>[link](/my uri)</p>
  5521. ````````````````````````````````
  5522. ```````````````````````````````` example
  5523. [link](</my uri>)
  5524. .
  5525. <p>[link](&lt;/my uri&gt;)</p>
  5526. ````````````````````````````````
  5527. ```````````````````````````````` example
  5528. [link](foo
  5529. bar)
  5530. .
  5531. <p>[link](foo
  5532. bar)</p>
  5533. ````````````````````````````````
  5534. ```````````````````````````````` example
  5535. [link](<foo
  5536. bar>)
  5537. .
  5538. <p>[link](<foo
  5539. bar>)</p>
  5540. ````````````````````````````````
  5541. Parentheses inside the link destination may be escaped:
  5542. ```````````````````````````````` example
  5543. [link](\(foo\))
  5544. .
  5545. <p><a href="(foo)">link</a></p>
  5546. ````````````````````````````````
  5547. Any number parentheses are allowed without escaping, as long as they are
  5548. balanced:
  5549. ```````````````````````````````` example
  5550. [link](foo(and(bar)))
  5551. .
  5552. <p><a href="foo(and(bar))">link</a></p>
  5553. ````````````````````````````````
  5554. However, if you have unbalanced parentheses, you need to escape or use the
  5555. `<...>` form:
  5556. ```````````````````````````````` example
  5557. [link](foo\(and\(bar\))
  5558. .
  5559. <p><a href="foo(and(bar)">link</a></p>
  5560. ````````````````````````````````
  5561. ```````````````````````````````` example
  5562. [link](<foo(and(bar)>)
  5563. .
  5564. <p><a href="foo(and(bar)">link</a></p>
  5565. ````````````````````````````````
  5566. Parentheses and other symbols can also be escaped, as usual
  5567. in Markdown:
  5568. ```````````````````````````````` example
  5569. [link](foo\)\:)
  5570. .
  5571. <p><a href="foo):">link</a></p>
  5572. ````````````````````````````````
  5573. A link can contain fragment identifiers and queries:
  5574. ```````````````````````````````` example
  5575. [link](#fragment)
  5576. [link](http://example.com#fragment)
  5577. [link](http://example.com?foo=3#frag)
  5578. .
  5579. <p><a href="#fragment">link</a></p>
  5580. <p><a href="http://example.com#fragment">link</a></p>
  5581. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5582. ````````````````````````````````
  5583. Note that a backslash before a non-escapable character is
  5584. just a backslash:
  5585. ```````````````````````````````` example
  5586. [link](foo\bar)
  5587. .
  5588. <p><a href="foo%5Cbar">link</a></p>
  5589. ````````````````````````````````
  5590. URL-escaping should be left alone inside the destination, as all
  5591. URL-escaped characters are also valid URL characters. Entity and
  5592. numerical character references in the destination will be parsed
  5593. into the corresponding Unicode code points, as usual. These may
  5594. be optionally URL-escaped when written as HTML, but this spec
  5595. does not enforce any particular policy for rendering URLs in
  5596. HTML or other formats. Renderers may make different decisions
  5597. about how to escape or normalize URLs in the output.
  5598. ```````````````````````````````` example
  5599. [link](foo%20b&auml;)
  5600. .
  5601. <p><a href="foo%20b%C3%A4">link</a></p>
  5602. ````````````````````````````````
  5603. Note that, because titles can often be parsed as destinations,
  5604. if you try to omit the destination and keep the title, you'll
  5605. get unexpected results:
  5606. ```````````````````````````````` example
  5607. [link]("title")
  5608. .
  5609. <p><a href="%22title%22">link</a></p>
  5610. ````````````````````````````````
  5611. Titles may be in single quotes, double quotes, or parentheses:
  5612. ```````````````````````````````` example
  5613. [link](/url "title")
  5614. [link](/url 'title')
  5615. [link](/url (title))
  5616. .
  5617. <p><a href="/url" title="title">link</a>
  5618. <a href="/url" title="title">link</a>
  5619. <a href="/url" title="title">link</a></p>
  5620. ````````````````````````````````
  5621. Backslash escapes and entity and numeric character references
  5622. may be used in titles:
  5623. ```````````````````````````````` example
  5624. [link](/url "title \"&quot;")
  5625. .
  5626. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5627. ````````````````````````````````
  5628. Titles must be separated from the link using a [whitespace].
  5629. Other [Unicode whitespace] like non-breaking space doesn't work.
  5630. ```````````````````````````````` example
  5631. [link](/url "title")
  5632. .
  5633. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5634. ````````````````````````````````
  5635. Nested balanced quotes are not allowed without escaping:
  5636. ```````````````````````````````` example
  5637. [link](/url "title "and" title")
  5638. .
  5639. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5640. ````````````````````````````````
  5641. But it is easy to work around this by using a different quote type:
  5642. ```````````````````````````````` example
  5643. [link](/url 'title "and" title')
  5644. .
  5645. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5646. ````````````````````````````````
  5647. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5648. title, and its test suite included a test demonstrating this.
  5649. But it is hard to see a good rationale for the extra complexity this
  5650. brings, since there are already many ways---backslash escaping,
  5651. entity and numeric character references, or using a different
  5652. quote type for the enclosing title---to write titles containing
  5653. double quotes. `Markdown.pl`'s handling of titles has a number
  5654. of other strange features. For example, it allows single-quoted
  5655. titles in inline links, but not reference links. And, in
  5656. reference links but not inline links, it allows a title to begin
  5657. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5658. titles with no closing quotation mark, though 1.0.2b8 does not.
  5659. It seems preferable to adopt a simple, rational rule that works
  5660. the same way in inline links and link reference definitions.)
  5661. [Whitespace] is allowed around the destination and title:
  5662. ```````````````````````````````` example
  5663. [link]( /uri
  5664. "title" )
  5665. .
  5666. <p><a href="/uri" title="title">link</a></p>
  5667. ````````````````````````````````
  5668. But it is not allowed between the link text and the
  5669. following parenthesis:
  5670. ```````````````````````````````` example
  5671. [link] (/uri)
  5672. .
  5673. <p>[link] (/uri)</p>
  5674. ````````````````````````````````
  5675. The link text may contain balanced brackets, but not unbalanced ones,
  5676. unless they are escaped:
  5677. ```````````````````````````````` example
  5678. [link [foo [bar]]](/uri)
  5679. .
  5680. <p><a href="/uri">link [foo [bar]]</a></p>
  5681. ````````````````````````````````
  5682. ```````````````````````````````` example
  5683. [link] bar](/uri)
  5684. .
  5685. <p>[link] bar](/uri)</p>
  5686. ````````````````````````````````
  5687. ```````````````````````````````` example
  5688. [link [bar](/uri)
  5689. .
  5690. <p>[link <a href="/uri">bar</a></p>
  5691. ````````````````````````````````
  5692. ```````````````````````````````` example
  5693. [link \[bar](/uri)
  5694. .
  5695. <p><a href="/uri">link [bar</a></p>
  5696. ````````````````````````````````
  5697. The link text may contain inline content:
  5698. ```````````````````````````````` example
  5699. [link *foo **bar** `#`*](/uri)
  5700. .
  5701. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5702. ````````````````````````````````
  5703. ```````````````````````````````` example
  5704. [![moon](moon.jpg)](/uri)
  5705. .
  5706. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5707. ````````````````````````````````
  5708. However, links may not contain other links, at any level of nesting.
  5709. ```````````````````````````````` example
  5710. [foo [bar](/uri)](/uri)
  5711. .
  5712. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5713. ````````````````````````````````
  5714. ```````````````````````````````` example
  5715. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5716. .
  5717. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5718. ````````````````````````````````
  5719. ```````````````````````````````` example
  5720. ![[[foo](uri1)](uri2)](uri3)
  5721. .
  5722. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5723. ````````````````````````````````
  5724. These cases illustrate the precedence of link text grouping over
  5725. emphasis grouping:
  5726. ```````````````````````````````` example
  5727. *[foo*](/uri)
  5728. .
  5729. <p>*<a href="/uri">foo*</a></p>
  5730. ````````````````````````````````
  5731. ```````````````````````````````` example
  5732. [foo *bar](baz*)
  5733. .
  5734. <p><a href="baz*">foo *bar</a></p>
  5735. ````````````````````````````````
  5736. Note that brackets that *aren't* part of links do not take
  5737. precedence:
  5738. ```````````````````````````````` example
  5739. *foo [bar* baz]
  5740. .
  5741. <p><em>foo [bar</em> baz]</p>
  5742. ````````````````````````````````
  5743. These cases illustrate the precedence of HTML tags, code spans,
  5744. and autolinks over link grouping:
  5745. ```````````````````````````````` example
  5746. [foo <bar attr="](baz)">
  5747. .
  5748. <p>[foo <bar attr="](baz)"></p>
  5749. ````````````````````````````````
  5750. ```````````````````````````````` example
  5751. [foo`](/uri)`
  5752. .
  5753. <p>[foo<code>](/uri)</code></p>
  5754. ````````````````````````````````
  5755. ```````````````````````````````` example
  5756. [foo<http://example.com/?search=](uri)>
  5757. .
  5758. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5759. ````````````````````````````````
  5760. There are three kinds of [reference link](@)s:
  5761. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5762. and [shortcut](#shortcut-reference-link).
  5763. A [full reference link](@)
  5764. consists of a [link text] immediately followed by a [link label]
  5765. that [matches] a [link reference definition] elsewhere in the document.
  5766. A [link label](@) begins with a left bracket (`[`) and ends
  5767. with the first right bracket (`]`) that is not backslash-escaped.
  5768. Between these brackets there must be at least one [non-whitespace character].
  5769. Unescaped square bracket characters are not allowed in
  5770. [link labels]. A link label can have at most 999
  5771. characters inside the square brackets.
  5772. One label [matches](@)
  5773. another just in case their normalized forms are equal. To normalize a
  5774. label, perform the *Unicode case fold* and collapse consecutive internal
  5775. [whitespace] to a single space. If there are multiple
  5776. matching reference link definitions, the one that comes first in the
  5777. document is used. (It is desirable in such cases to emit a warning.)
  5778. The contents of the first link label are parsed as inlines, which are
  5779. used as the link's text. The link's URI and title are provided by the
  5780. matching [link reference definition].
  5781. Here is a simple example:
  5782. ```````````````````````````````` example
  5783. [foo][bar]
  5784. [bar]: /url "title"
  5785. .
  5786. <p><a href="/url" title="title">foo</a></p>
  5787. ````````````````````````````````
  5788. The rules for the [link text] are the same as with
  5789. [inline links]. Thus:
  5790. The link text may contain balanced brackets, but not unbalanced ones,
  5791. unless they are escaped:
  5792. ```````````````````````````````` example
  5793. [link [foo [bar]]][ref]
  5794. [ref]: /uri
  5795. .
  5796. <p><a href="/uri">link [foo [bar]]</a></p>
  5797. ````````````````````````````````
  5798. ```````````````````````````````` example
  5799. [link \[bar][ref]
  5800. [ref]: /uri
  5801. .
  5802. <p><a href="/uri">link [bar</a></p>
  5803. ````````````````````````````````
  5804. The link text may contain inline content:
  5805. ```````````````````````````````` example
  5806. [link *foo **bar** `#`*][ref]
  5807. [ref]: /uri
  5808. .
  5809. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5810. ````````````````````````````````
  5811. ```````````````````````````````` example
  5812. [![moon](moon.jpg)][ref]
  5813. [ref]: /uri
  5814. .
  5815. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5816. ````````````````````````````````
  5817. However, links may not contain other links, at any level of nesting.
  5818. ```````````````````````````````` example
  5819. [foo [bar](/uri)][ref]
  5820. [ref]: /uri
  5821. .
  5822. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5823. ````````````````````````````````
  5824. ```````````````````````````````` example
  5825. [foo *bar [baz][ref]*][ref]
  5826. [ref]: /uri
  5827. .
  5828. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5829. ````````````````````````````````
  5830. (In the examples above, we have two [shortcut reference links]
  5831. instead of one [full reference link].)
  5832. The following cases illustrate the precedence of link text grouping over
  5833. emphasis grouping:
  5834. ```````````````````````````````` example
  5835. *[foo*][ref]
  5836. [ref]: /uri
  5837. .
  5838. <p>*<a href="/uri">foo*</a></p>
  5839. ````````````````````````````````
  5840. ```````````````````````````````` example
  5841. [foo *bar][ref]
  5842. [ref]: /uri
  5843. .
  5844. <p><a href="/uri">foo *bar</a></p>
  5845. ````````````````````````````````
  5846. These cases illustrate the precedence of HTML tags, code spans,
  5847. and autolinks over link grouping:
  5848. ```````````````````````````````` example
  5849. [foo <bar attr="][ref]">
  5850. [ref]: /uri
  5851. .
  5852. <p>[foo <bar attr="][ref]"></p>
  5853. ````````````````````````````````
  5854. ```````````````````````````````` example
  5855. [foo`][ref]`
  5856. [ref]: /uri
  5857. .
  5858. <p>[foo<code>][ref]</code></p>
  5859. ````````````````````````````````
  5860. ```````````````````````````````` example
  5861. [foo<http://example.com/?search=][ref]>
  5862. [ref]: /uri
  5863. .
  5864. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5865. ````````````````````````````````
  5866. Matching is case-insensitive:
  5867. ```````````````````````````````` example
  5868. [foo][BaR]
  5869. [bar]: /url "title"
  5870. .
  5871. <p><a href="/url" title="title">foo</a></p>
  5872. ````````````````````````````````
  5873. Unicode case fold is used:
  5874. ```````````````````````````````` example
  5875. [Толпой][Толпой] is a Russian word.
  5876. [ТОЛПОЙ]: /url
  5877. .
  5878. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5879. ````````````````````````````````
  5880. Consecutive internal [whitespace] is treated as one space for
  5881. purposes of determining matching:
  5882. ```````````````````````````````` example
  5883. [Foo
  5884. bar]: /url
  5885. [Baz][Foo bar]
  5886. .
  5887. <p><a href="/url">Baz</a></p>
  5888. ````````````````````````````````
  5889. No [whitespace] is allowed between the [link text] and the
  5890. [link label]:
  5891. ```````````````````````````````` example
  5892. [foo] [bar]
  5893. [bar]: /url "title"
  5894. .
  5895. <p>[foo] <a href="/url" title="title">bar</a></p>
  5896. ````````````````````````````````
  5897. ```````````````````````````````` example
  5898. [foo]
  5899. [bar]
  5900. [bar]: /url "title"
  5901. .
  5902. <p>[foo]
  5903. <a href="/url" title="title">bar</a></p>
  5904. ````````````````````````````````
  5905. This is a departure from John Gruber's original Markdown syntax
  5906. description, which explicitly allows whitespace between the link
  5907. text and the link label. It brings reference links in line with
  5908. [inline links], which (according to both original Markdown and
  5909. this spec) cannot have whitespace after the link text. More
  5910. importantly, it prevents inadvertent capture of consecutive
  5911. [shortcut reference links]. If whitespace is allowed between the
  5912. link text and the link label, then in the following we will have
  5913. a single reference link, not two shortcut reference links, as
  5914. intended:
  5915. ``` markdown
  5916. [foo]
  5917. [bar]
  5918. [foo]: /url1
  5919. [bar]: /url2
  5920. ```
  5921. (Note that [shortcut reference links] were introduced by Gruber
  5922. himself in a beta version of `Markdown.pl`, but never included
  5923. in the official syntax description. Without shortcut reference
  5924. links, it is harmless to allow space between the link text and
  5925. link label; but once shortcut references are introduced, it is
  5926. too dangerous to allow this, as it frequently leads to
  5927. unintended results.)
  5928. When there are multiple matching [link reference definitions],
  5929. the first is used:
  5930. ```````````````````````````````` example
  5931. [foo]: /url1
  5932. [foo]: /url2
  5933. [bar][foo]
  5934. .
  5935. <p><a href="/url1">bar</a></p>
  5936. ````````````````````````````````
  5937. Note that matching is performed on normalized strings, not parsed
  5938. inline content. So the following does not match, even though the
  5939. labels define equivalent inline content:
  5940. ```````````````````````````````` example
  5941. [bar][foo\!]
  5942. [foo!]: /url
  5943. .
  5944. <p>[bar][foo!]</p>
  5945. ````````````````````````````````
  5946. [Link labels] cannot contain brackets, unless they are
  5947. backslash-escaped:
  5948. ```````````````````````````````` example
  5949. [foo][ref[]
  5950. [ref[]: /uri
  5951. .
  5952. <p>[foo][ref[]</p>
  5953. <p>[ref[]: /uri</p>
  5954. ````````````````````````````````
  5955. ```````````````````````````````` example
  5956. [foo][ref[bar]]
  5957. [ref[bar]]: /uri
  5958. .
  5959. <p>[foo][ref[bar]]</p>
  5960. <p>[ref[bar]]: /uri</p>
  5961. ````````````````````````````````
  5962. ```````````````````````````````` example
  5963. [[[foo]]]
  5964. [[[foo]]]: /url
  5965. .
  5966. <p>[[[foo]]]</p>
  5967. <p>[[[foo]]]: /url</p>
  5968. ````````````````````````````````
  5969. ```````````````````````````````` example
  5970. [foo][ref\[]
  5971. [ref\[]: /uri
  5972. .
  5973. <p><a href="/uri">foo</a></p>
  5974. ````````````````````````````````
  5975. Note that in this example `]` is not backslash-escaped:
  5976. ```````````````````````````````` example
  5977. [bar\\]: /uri
  5978. [bar\\]
  5979. .
  5980. <p><a href="/uri">bar\</a></p>
  5981. ````````````````````````````````
  5982. A [link label] must contain at least one [non-whitespace character]:
  5983. ```````````````````````````````` example
  5984. []
  5985. []: /uri
  5986. .
  5987. <p>[]</p>
  5988. <p>[]: /uri</p>
  5989. ````````````````````````````````
  5990. ```````````````````````````````` example
  5991. [
  5992. ]
  5993. [
  5994. ]: /uri
  5995. .
  5996. <p>[
  5997. ]</p>
  5998. <p>[
  5999. ]: /uri</p>
  6000. ````````````````````````````````
  6001. A [collapsed reference link](@)
  6002. consists of a [link label] that [matches] a
  6003. [link reference definition] elsewhere in the
  6004. document, followed by the string `[]`.
  6005. The contents of the first link label are parsed as inlines,
  6006. which are used as the link's text. The link's URI and title are
  6007. provided by the matching reference link definition. Thus,
  6008. `[foo][]` is equivalent to `[foo][foo]`.
  6009. ```````````````````````````````` example
  6010. [foo][]
  6011. [foo]: /url "title"
  6012. .
  6013. <p><a href="/url" title="title">foo</a></p>
  6014. ````````````````````````````````
  6015. ```````````````````````````````` example
  6016. [*foo* bar][]
  6017. [*foo* bar]: /url "title"
  6018. .
  6019. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6020. ````````````````````````````````
  6021. The link labels are case-insensitive:
  6022. ```````````````````````````````` example
  6023. [Foo][]
  6024. [foo]: /url "title"
  6025. .
  6026. <p><a href="/url" title="title">Foo</a></p>
  6027. ````````````````````````````````
  6028. As with full reference links, [whitespace] is not
  6029. allowed between the two sets of brackets:
  6030. ```````````````````````````````` example
  6031. [foo]
  6032. []
  6033. [foo]: /url "title"
  6034. .
  6035. <p><a href="/url" title="title">foo</a>
  6036. []</p>
  6037. ````````````````````````````````
  6038. A [shortcut reference link](@)
  6039. consists of a [link label] that [matches] a
  6040. [link reference definition] elsewhere in the
  6041. document and is not followed by `[]` or a link label.
  6042. The contents of the first link label are parsed as inlines,
  6043. which are used as the link's text. The link's URI and title
  6044. are provided by the matching link reference definition.
  6045. Thus, `[foo]` is equivalent to `[foo][]`.
  6046. ```````````````````````````````` example
  6047. [foo]
  6048. [foo]: /url "title"
  6049. .
  6050. <p><a href="/url" title="title">foo</a></p>
  6051. ````````````````````````````````
  6052. ```````````````````````````````` example
  6053. [*foo* bar]
  6054. [*foo* bar]: /url "title"
  6055. .
  6056. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6057. ````````````````````````````````
  6058. ```````````````````````````````` example
  6059. [[*foo* bar]]
  6060. [*foo* bar]: /url "title"
  6061. .
  6062. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6063. ````````````````````````````````
  6064. ```````````````````````````````` example
  6065. [[bar [foo]
  6066. [foo]: /url
  6067. .
  6068. <p>[[bar <a href="/url">foo</a></p>
  6069. ````````````````````````````````
  6070. The link labels are case-insensitive:
  6071. ```````````````````````````````` example
  6072. [Foo]
  6073. [foo]: /url "title"
  6074. .
  6075. <p><a href="/url" title="title">Foo</a></p>
  6076. ````````````````````````````````
  6077. A space after the link text should be preserved:
  6078. ```````````````````````````````` example
  6079. [foo] bar
  6080. [foo]: /url
  6081. .
  6082. <p><a href="/url">foo</a> bar</p>
  6083. ````````````````````````````````
  6084. If you just want bracketed text, you can backslash-escape the
  6085. opening bracket to avoid links:
  6086. ```````````````````````````````` example
  6087. \[foo]
  6088. [foo]: /url "title"
  6089. .
  6090. <p>[foo]</p>
  6091. ````````````````````````````````
  6092. Note that this is a link, because a link label ends with the first
  6093. following closing bracket:
  6094. ```````````````````````````````` example
  6095. [foo*]: /url
  6096. *[foo*]
  6097. .
  6098. <p>*<a href="/url">foo*</a></p>
  6099. ````````````````````````````````
  6100. Full and compact references take precedence over shortcut
  6101. references:
  6102. ```````````````````````````````` example
  6103. [foo][bar]
  6104. [foo]: /url1
  6105. [bar]: /url2
  6106. .
  6107. <p><a href="/url2">foo</a></p>
  6108. ````````````````````````````````
  6109. ```````````````````````````````` example
  6110. [foo][]
  6111. [foo]: /url1
  6112. .
  6113. <p><a href="/url1">foo</a></p>
  6114. ````````````````````````````````
  6115. Inline links also take precedence:
  6116. ```````````````````````````````` example
  6117. [foo]()
  6118. [foo]: /url1
  6119. .
  6120. <p><a href="">foo</a></p>
  6121. ````````````````````````````````
  6122. ```````````````````````````````` example
  6123. [foo](not a link)
  6124. [foo]: /url1
  6125. .
  6126. <p><a href="/url1">foo</a>(not a link)</p>
  6127. ````````````````````````````````
  6128. In the following case `[bar][baz]` is parsed as a reference,
  6129. `[foo]` as normal text:
  6130. ```````````````````````````````` example
  6131. [foo][bar][baz]
  6132. [baz]: /url
  6133. .
  6134. <p>[foo]<a href="/url">bar</a></p>
  6135. ````````````````````````````````
  6136. Here, though, `[foo][bar]` is parsed as a reference, since
  6137. `[bar]` is defined:
  6138. ```````````````````````````````` example
  6139. [foo][bar][baz]
  6140. [baz]: /url1
  6141. [bar]: /url2
  6142. .
  6143. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6144. ````````````````````````````````
  6145. Here `[foo]` is not parsed as a shortcut reference, because it
  6146. is followed by a link label (even though `[bar]` is not defined):
  6147. ```````````````````````````````` example
  6148. [foo][bar][baz]
  6149. [baz]: /url1
  6150. [foo]: /url2
  6151. .
  6152. <p>[foo]<a href="/url1">bar</a></p>
  6153. ````````````````````````````````
  6154. ## Images
  6155. Syntax for images is like the syntax for links, with one
  6156. difference. Instead of [link text], we have an
  6157. [image description](@). The rules for this are the
  6158. same as for [link text], except that (a) an
  6159. image description starts with `![` rather than `[`, and
  6160. (b) an image description may contain links.
  6161. An image description has inline elements
  6162. as its contents. When an image is rendered to HTML,
  6163. this is standardly used as the image's `alt` attribute.
  6164. ```````````````````````````````` example
  6165. ![foo](/url "title")
  6166. .
  6167. <p><img src="/url" alt="foo" title="title" /></p>
  6168. ````````````````````````````````
  6169. ```````````````````````````````` example
  6170. ![foo *bar*]
  6171. [foo *bar*]: train.jpg "train & tracks"
  6172. .
  6173. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6174. ````````````````````````````````
  6175. ```````````````````````````````` example
  6176. ![foo ![bar](/url)](/url2)
  6177. .
  6178. <p><img src="/url2" alt="foo bar" /></p>
  6179. ````````````````````````````````
  6180. ```````````````````````````````` example
  6181. ![foo [bar](/url)](/url2)
  6182. .
  6183. <p><img src="/url2" alt="foo bar" /></p>
  6184. ````````````````````````````````
  6185. Though this spec is concerned with parsing, not rendering, it is
  6186. recommended that in rendering to HTML, only the plain string content
  6187. of the [image description] be used. Note that in
  6188. the above example, the alt attribute's value is `foo bar`, not `foo
  6189. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6190. content is rendered, without formatting.
  6191. ```````````````````````````````` example
  6192. ![foo *bar*][]
  6193. [foo *bar*]: train.jpg "train & tracks"
  6194. .
  6195. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6196. ````````````````````````````````
  6197. ```````````````````````````````` example
  6198. ![foo *bar*][foobar]
  6199. [FOOBAR]: train.jpg "train & tracks"
  6200. .
  6201. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6202. ````````````````````````````````
  6203. ```````````````````````````````` example
  6204. ![foo](train.jpg)
  6205. .
  6206. <p><img src="train.jpg" alt="foo" /></p>
  6207. ````````````````````````````````
  6208. ```````````````````````````````` example
  6209. My ![foo bar](/path/to/train.jpg "title" )
  6210. .
  6211. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6212. ````````````````````````````````
  6213. ```````````````````````````````` example
  6214. ![foo](<url>)
  6215. .
  6216. <p><img src="url" alt="foo" /></p>
  6217. ````````````````````````````````
  6218. ```````````````````````````````` example
  6219. ![](/url)
  6220. .
  6221. <p><img src="/url" alt="" /></p>
  6222. ````````````````````````````````
  6223. Reference-style:
  6224. ```````````````````````````````` example
  6225. ![foo][bar]
  6226. [bar]: /url
  6227. .
  6228. <p><img src="/url" alt="foo" /></p>
  6229. ````````````````````````````````
  6230. ```````````````````````````````` example
  6231. ![foo][bar]
  6232. [BAR]: /url
  6233. .
  6234. <p><img src="/url" alt="foo" /></p>
  6235. ````````````````````````````````
  6236. Collapsed:
  6237. ```````````````````````````````` example
  6238. ![foo][]
  6239. [foo]: /url "title"
  6240. .
  6241. <p><img src="/url" alt="foo" title="title" /></p>
  6242. ````````````````````````````````
  6243. ```````````````````````````````` example
  6244. ![*foo* bar][]
  6245. [*foo* bar]: /url "title"
  6246. .
  6247. <p><img src="/url" alt="foo bar" title="title" /></p>
  6248. ````````````````````````````````
  6249. The labels are case-insensitive:
  6250. ```````````````````````````````` example
  6251. ![Foo][]
  6252. [foo]: /url "title"
  6253. .
  6254. <p><img src="/url" alt="Foo" title="title" /></p>
  6255. ````````````````````````````````
  6256. As with reference links, [whitespace] is not allowed
  6257. between the two sets of brackets:
  6258. ```````````````````````````````` example
  6259. ![foo]
  6260. []
  6261. [foo]: /url "title"
  6262. .
  6263. <p><img src="/url" alt="foo" title="title" />
  6264. []</p>
  6265. ````````````````````````````````
  6266. Shortcut:
  6267. ```````````````````````````````` example
  6268. ![foo]
  6269. [foo]: /url "title"
  6270. .
  6271. <p><img src="/url" alt="foo" title="title" /></p>
  6272. ````````````````````````````````
  6273. ```````````````````````````````` example
  6274. ![*foo* bar]
  6275. [*foo* bar]: /url "title"
  6276. .
  6277. <p><img src="/url" alt="foo bar" title="title" /></p>
  6278. ````````````````````````````````
  6279. Note that link labels cannot contain unescaped brackets:
  6280. ```````````````````````````````` example
  6281. ![[foo]]
  6282. [[foo]]: /url "title"
  6283. .
  6284. <p>![[foo]]</p>
  6285. <p>[[foo]]: /url &quot;title&quot;</p>
  6286. ````````````````````````````````
  6287. The link labels are case-insensitive:
  6288. ```````````````````````````````` example
  6289. ![Foo]
  6290. [foo]: /url "title"
  6291. .
  6292. <p><img src="/url" alt="Foo" title="title" /></p>
  6293. ````````````````````````````````
  6294. If you just want a literal `!` followed by bracketed text, you can
  6295. backslash-escape the opening `[`:
  6296. ```````````````````````````````` example
  6297. !\[foo]
  6298. [foo]: /url "title"
  6299. .
  6300. <p>![foo]</p>
  6301. ````````````````````````````````
  6302. If you want a link after a literal `!`, backslash-escape the
  6303. `!`:
  6304. ```````````````````````````````` example
  6305. \![foo]
  6306. [foo]: /url "title"
  6307. .
  6308. <p>!<a href="/url" title="title">foo</a></p>
  6309. ````````````````````````````````
  6310. ## Autolinks
  6311. [Autolink](@)s are absolute URIs and email addresses inside
  6312. `<` and `>`. They are parsed as links, with the URL or email address
  6313. as the link label.
  6314. A [URI autolink](@) consists of `<`, followed by an
  6315. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6316. a link to the URI, with the URI as the link's label.
  6317. An [absolute URI](@),
  6318. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6319. followed by zero or more characters other than ASCII
  6320. [whitespace] and control characters, `<`, and `>`. If
  6321. the URI includes these characters, they must be percent-encoded
  6322. (e.g. `%20` for a space).
  6323. For purposes of this spec, a [scheme](@) is any sequence
  6324. of 2--32 characters beginning with an ASCII letter and followed
  6325. by any combination of ASCII letters, digits, or the symbols plus
  6326. ("+"), period ("."), or hyphen ("-").
  6327. Here are some valid autolinks:
  6328. ```````````````````````````````` example
  6329. <http://foo.bar.baz>
  6330. .
  6331. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6332. ````````````````````````````````
  6333. ```````````````````````````````` example
  6334. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6335. .
  6336. <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>
  6337. ````````````````````````````````
  6338. ```````````````````````````````` example
  6339. <irc://foo.bar:2233/baz>
  6340. .
  6341. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6342. ````````````````````````````````
  6343. Uppercase is also fine:
  6344. ```````````````````````````````` example
  6345. <MAILTO:FOO@BAR.BAZ>
  6346. .
  6347. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6348. ````````````````````````````````
  6349. Note that many strings that count as [absolute URIs] for
  6350. purposes of this spec are not valid URIs, because their
  6351. schemes are not registered or because of other problems
  6352. with their syntax:
  6353. ```````````````````````````````` example
  6354. <a+b+c:d>
  6355. .
  6356. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6357. ````````````````````````````````
  6358. ```````````````````````````````` example
  6359. <made-up-scheme://foo,bar>
  6360. .
  6361. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6362. ````````````````````````````````
  6363. ```````````````````````````````` example
  6364. <http://../>
  6365. .
  6366. <p><a href="http://../">http://../</a></p>
  6367. ````````````````````````````````
  6368. ```````````````````````````````` example
  6369. <localhost:5001/foo>
  6370. .
  6371. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6372. ````````````````````````````````
  6373. Spaces are not allowed in autolinks:
  6374. ```````````````````````````````` example
  6375. <http://foo.bar/baz bim>
  6376. .
  6377. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6378. ````````````````````````````````
  6379. Backslash-escapes do not work inside autolinks:
  6380. ```````````````````````````````` example
  6381. <http://example.com/\[\>
  6382. .
  6383. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6384. ````````````````````````````````
  6385. An [email autolink](@)
  6386. consists of `<`, followed by an [email address],
  6387. followed by `>`. The link's label is the email address,
  6388. and the URL is `mailto:` followed by the email address.
  6389. An [email address](@),
  6390. for these purposes, is anything that matches
  6391. the [non-normative regex from the HTML5
  6392. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6393. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6394. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6395. Examples of email autolinks:
  6396. ```````````````````````````````` example
  6397. <foo@bar.example.com>
  6398. .
  6399. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6400. ````````````````````````````````
  6401. ```````````````````````````````` example
  6402. <foo+special@Bar.baz-bar0.com>
  6403. .
  6404. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6405. ````````````````````````````````
  6406. Backslash-escapes do not work inside email autolinks:
  6407. ```````````````````````````````` example
  6408. <foo\+@bar.example.com>
  6409. .
  6410. <p>&lt;foo+@bar.example.com&gt;</p>
  6411. ````````````````````````````````
  6412. These are not autolinks:
  6413. ```````````````````````````````` example
  6414. <>
  6415. .
  6416. <p>&lt;&gt;</p>
  6417. ````````````````````````````````
  6418. ```````````````````````````````` example
  6419. < http://foo.bar >
  6420. .
  6421. <p>&lt; http://foo.bar &gt;</p>
  6422. ````````````````````````````````
  6423. ```````````````````````````````` example
  6424. <m:abc>
  6425. .
  6426. <p>&lt;m:abc&gt;</p>
  6427. ````````````````````````````````
  6428. ```````````````````````````````` example
  6429. <foo.bar.baz>
  6430. .
  6431. <p>&lt;foo.bar.baz&gt;</p>
  6432. ````````````````````````````````
  6433. ```````````````````````````````` example
  6434. http://example.com
  6435. .
  6436. <p>http://example.com</p>
  6437. ````````````````````````````````
  6438. ```````````````````````````````` example
  6439. foo@bar.example.com
  6440. .
  6441. <p>foo@bar.example.com</p>
  6442. ````````````````````````````````
  6443. ## Raw HTML
  6444. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6445. raw HTML tag and will be rendered in HTML without escaping.
  6446. Tag and attribute names are not limited to current HTML tags,
  6447. so custom tags (and even, say, DocBook tags) may be used.
  6448. Here is the grammar for tags:
  6449. A [tag name](@) consists of an ASCII letter
  6450. followed by zero or more ASCII letters, digits, or
  6451. hyphens (`-`).
  6452. An [attribute](@) consists of [whitespace],
  6453. an [attribute name], and an optional
  6454. [attribute value specification].
  6455. An [attribute name](@)
  6456. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6457. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6458. specification restricted to ASCII. HTML5 is laxer.)
  6459. An [attribute value specification](@)
  6460. consists of optional [whitespace],
  6461. a `=` character, optional [whitespace], and an [attribute
  6462. value].
  6463. An [attribute value](@)
  6464. consists of an [unquoted attribute value],
  6465. a [single-quoted attribute value], or a [double-quoted attribute value].
  6466. An [unquoted attribute value](@)
  6467. is a nonempty string of characters not
  6468. including spaces, `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6469. A [single-quoted attribute value](@)
  6470. consists of `'`, zero or more
  6471. characters not including `'`, and a final `'`.
  6472. A [double-quoted attribute value](@)
  6473. consists of `"`, zero or more
  6474. characters not including `"`, and a final `"`.
  6475. An [open tag](@) consists of a `<` character, a [tag name],
  6476. zero or more [attributes], optional [whitespace], an optional `/`
  6477. character, and a `>` character.
  6478. A [closing tag](@) consists of the string `</`, a
  6479. [tag name], optional [whitespace], and the character `>`.
  6480. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6481. where *text* does not start with `>` or `->`, does not end with `-`,
  6482. and does not contain `--`. (See the
  6483. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6484. A [processing instruction](@)
  6485. consists of the string `<?`, a string
  6486. of characters not including the string `?>`, and the string
  6487. `?>`.
  6488. A [declaration](@) consists of the
  6489. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6490. [whitespace], a string of characters not including the
  6491. character `>`, and the character `>`.
  6492. A [CDATA section](@) consists of
  6493. the string `<![CDATA[`, a string of characters not including the string
  6494. `]]>`, and the string `]]>`.
  6495. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6496. an [HTML comment], a [processing instruction], a [declaration],
  6497. or a [CDATA section].
  6498. Here are some simple open tags:
  6499. ```````````````````````````````` example
  6500. <a><bab><c2c>
  6501. .
  6502. <p><a><bab><c2c></p>
  6503. ````````````````````````````````
  6504. Empty elements:
  6505. ```````````````````````````````` example
  6506. <a/><b2/>
  6507. .
  6508. <p><a/><b2/></p>
  6509. ````````````````````````````````
  6510. [Whitespace] is allowed:
  6511. ```````````````````````````````` example
  6512. <a /><b2
  6513. data="foo" >
  6514. .
  6515. <p><a /><b2
  6516. data="foo" ></p>
  6517. ````````````````````````````````
  6518. With attributes:
  6519. ```````````````````````````````` example
  6520. <a foo="bar" bam = 'baz <em>"</em>'
  6521. _boolean zoop:33=zoop:33 />
  6522. .
  6523. <p><a foo="bar" bam = 'baz <em>"</em>'
  6524. _boolean zoop:33=zoop:33 /></p>
  6525. ````````````````````````````````
  6526. Custom tag names can be used:
  6527. ```````````````````````````````` example
  6528. Foo <responsive-image src="foo.jpg" />
  6529. .
  6530. <p>Foo <responsive-image src="foo.jpg" /></p>
  6531. ````````````````````````````````
  6532. Illegal tag names, not parsed as HTML:
  6533. ```````````````````````````````` example
  6534. <33> <__>
  6535. .
  6536. <p>&lt;33&gt; &lt;__&gt;</p>
  6537. ````````````````````````````````
  6538. Illegal attribute names:
  6539. ```````````````````````````````` example
  6540. <a h*#ref="hi">
  6541. .
  6542. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6543. ````````````````````````````````
  6544. Illegal attribute values:
  6545. ```````````````````````````````` example
  6546. <a href="hi'> <a href=hi'>
  6547. .
  6548. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6549. ````````````````````````````````
  6550. Illegal [whitespace]:
  6551. ```````````````````````````````` example
  6552. < a><
  6553. foo><bar/ >
  6554. .
  6555. <p>&lt; a&gt;&lt;
  6556. foo&gt;&lt;bar/ &gt;</p>
  6557. ````````````````````````````````
  6558. Missing [whitespace]:
  6559. ```````````````````````````````` example
  6560. <a href='bar'title=title>
  6561. .
  6562. <p>&lt;a href='bar'title=title&gt;</p>
  6563. ````````````````````````````````
  6564. Closing tags:
  6565. ```````````````````````````````` example
  6566. </a></foo >
  6567. .
  6568. <p></a></foo ></p>
  6569. ````````````````````````````````
  6570. Illegal attributes in closing tag:
  6571. ```````````````````````````````` example
  6572. </a href="foo">
  6573. .
  6574. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6575. ````````````````````````````````
  6576. Comments:
  6577. ```````````````````````````````` example
  6578. foo <!-- this is a
  6579. comment - with hyphen -->
  6580. .
  6581. <p>foo <!-- this is a
  6582. comment - with hyphen --></p>
  6583. ````````````````````````````````
  6584. ```````````````````````````````` example
  6585. foo <!-- not a comment -- two hyphens -->
  6586. .
  6587. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6588. ````````````````````````````````
  6589. Not comments:
  6590. ```````````````````````````````` example
  6591. foo <!--> foo -->
  6592. foo <!-- foo--->
  6593. .
  6594. <p>foo &lt;!--&gt; foo --&gt;</p>
  6595. <p>foo &lt;!-- foo---&gt;</p>
  6596. ````````````````````````````````
  6597. Processing instructions:
  6598. ```````````````````````````````` example
  6599. foo <?php echo $a; ?>
  6600. .
  6601. <p>foo <?php echo $a; ?></p>
  6602. ````````````````````````````````
  6603. Declarations:
  6604. ```````````````````````````````` example
  6605. foo <!ELEMENT br EMPTY>
  6606. .
  6607. <p>foo <!ELEMENT br EMPTY></p>
  6608. ````````````````````````````````
  6609. CDATA sections:
  6610. ```````````````````````````````` example
  6611. foo <![CDATA[>&<]]>
  6612. .
  6613. <p>foo <![CDATA[>&<]]></p>
  6614. ````````````````````````````````
  6615. Entity and numeric character references are preserved in HTML
  6616. attributes:
  6617. ```````````````````````````````` example
  6618. foo <a href="&ouml;">
  6619. .
  6620. <p>foo <a href="&ouml;"></p>
  6621. ````````````````````````````````
  6622. Backslash escapes do not work in HTML attributes:
  6623. ```````````````````````````````` example
  6624. foo <a href="\*">
  6625. .
  6626. <p>foo <a href="\*"></p>
  6627. ````````````````````````````````
  6628. ```````````````````````````````` example
  6629. <a href="\"">
  6630. .
  6631. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6632. ````````````````````````````````
  6633. ## Hard line breaks
  6634. A line break (not in a code span or HTML tag) that is preceded
  6635. by two or more spaces and does not occur at the end of a block
  6636. is parsed as a [hard line break](@) (rendered
  6637. in HTML as a `<br />` tag):
  6638. ```````````````````````````````` example
  6639. foo
  6640. baz
  6641. .
  6642. <p>foo<br />
  6643. baz</p>
  6644. ````````````````````````````````
  6645. For a more visible alternative, a backslash before the
  6646. [line ending] may be used instead of two spaces:
  6647. ```````````````````````````````` example
  6648. foo\
  6649. baz
  6650. .
  6651. <p>foo<br />
  6652. baz</p>
  6653. ````````````````````````````````
  6654. More than two spaces can be used:
  6655. ```````````````````````````````` example
  6656. foo
  6657. baz
  6658. .
  6659. <p>foo<br />
  6660. baz</p>
  6661. ````````````````````````````````
  6662. Leading spaces at the beginning of the next line are ignored:
  6663. ```````````````````````````````` example
  6664. foo
  6665. bar
  6666. .
  6667. <p>foo<br />
  6668. bar</p>
  6669. ````````````````````````````````
  6670. ```````````````````````````````` example
  6671. foo\
  6672. bar
  6673. .
  6674. <p>foo<br />
  6675. bar</p>
  6676. ````````````````````````````````
  6677. Line breaks can occur inside emphasis, links, and other constructs
  6678. that allow inline content:
  6679. ```````````````````````````````` example
  6680. *foo
  6681. bar*
  6682. .
  6683. <p><em>foo<br />
  6684. bar</em></p>
  6685. ````````````````````````````````
  6686. ```````````````````````````````` example
  6687. *foo\
  6688. bar*
  6689. .
  6690. <p><em>foo<br />
  6691. bar</em></p>
  6692. ````````````````````````````````
  6693. Line breaks do not occur inside code spans
  6694. ```````````````````````````````` example
  6695. `code
  6696. span`
  6697. .
  6698. <p><code>code span</code></p>
  6699. ````````````````````````````````
  6700. ```````````````````````````````` example
  6701. `code\
  6702. span`
  6703. .
  6704. <p><code>code\ span</code></p>
  6705. ````````````````````````````````
  6706. or HTML tags:
  6707. ```````````````````````````````` example
  6708. <a href="foo
  6709. bar">
  6710. .
  6711. <p><a href="foo
  6712. bar"></p>
  6713. ````````````````````````````````
  6714. ```````````````````````````````` example
  6715. <a href="foo\
  6716. bar">
  6717. .
  6718. <p><a href="foo\
  6719. bar"></p>
  6720. ````````````````````````````````
  6721. Hard line breaks are for separating inline content within a block.
  6722. Neither syntax for hard line breaks works at the end of a paragraph or
  6723. other block element:
  6724. ```````````````````````````````` example
  6725. foo\
  6726. .
  6727. <p>foo\</p>
  6728. ````````````````````````````````
  6729. ```````````````````````````````` example
  6730. foo
  6731. .
  6732. <p>foo</p>
  6733. ````````````````````````````````
  6734. ```````````````````````````````` example
  6735. ### foo\
  6736. .
  6737. <h3>foo\</h3>
  6738. ````````````````````````````````
  6739. ```````````````````````````````` example
  6740. ### foo
  6741. .
  6742. <h3>foo</h3>
  6743. ````````````````````````````````
  6744. ## Soft line breaks
  6745. A regular line break (not in a code span or HTML tag) that is not
  6746. preceded by two or more spaces or a backslash is parsed as a
  6747. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6748. [line ending] or as a space. The result will be the same in
  6749. browsers. In the examples here, a [line ending] will be used.)
  6750. ```````````````````````````````` example
  6751. foo
  6752. baz
  6753. .
  6754. <p>foo
  6755. baz</p>
  6756. ````````````````````````````````
  6757. Spaces at the end of the line and beginning of the next line are
  6758. removed:
  6759. ```````````````````````````````` example
  6760. foo
  6761. baz
  6762. .
  6763. <p>foo
  6764. baz</p>
  6765. ````````````````````````````````
  6766. A conforming parser may render a soft line break in HTML either as a
  6767. line break or as a space.
  6768. A renderer may also provide an option to render soft line breaks
  6769. as hard line breaks.
  6770. ## Textual content
  6771. Any characters not given an interpretation by the above rules will
  6772. be parsed as plain textual content.
  6773. ```````````````````````````````` example
  6774. hello $.;'there
  6775. .
  6776. <p>hello $.;'there</p>
  6777. ````````````````````````````````
  6778. ```````````````````````````````` example
  6779. Foo χρῆν
  6780. .
  6781. <p>Foo χρῆν</p>
  6782. ````````````````````````````````
  6783. Internal spaces are preserved verbatim:
  6784. ```````````````````````````````` example
  6785. Multiple spaces
  6786. .
  6787. <p>Multiple spaces</p>
  6788. ````````````````````````````````
  6789. <!-- END TESTS -->
  6790. # Appendix: A parsing strategy
  6791. In this appendix we describe some features of the parsing strategy
  6792. used in the CommonMark reference implementations.
  6793. ## Overview
  6794. Parsing has two phases:
  6795. 1. In the first phase, lines of input are consumed and the block
  6796. structure of the document---its division into paragraphs, block quotes,
  6797. list items, and so on---is constructed. Text is assigned to these
  6798. blocks but not parsed. Link reference definitions are parsed and a
  6799. map of links is constructed.
  6800. 2. In the second phase, the raw text contents of paragraphs and headings
  6801. are parsed into sequences of Markdown inline elements (strings,
  6802. code spans, links, emphasis, and so on), using the map of link
  6803. references constructed in phase 1.
  6804. At each point in processing, the document is represented as a tree of
  6805. **blocks**. The root of the tree is a `document` block. The `document`
  6806. may have any number of other blocks as **children**. These children
  6807. may, in turn, have other blocks as children. The last child of a block
  6808. is normally considered **open**, meaning that subsequent lines of input
  6809. can alter its contents. (Blocks that are not open are **closed**.)
  6810. Here, for example, is a possible document tree, with the open blocks
  6811. marked by arrows:
  6812. ``` tree
  6813. -> document
  6814. -> block_quote
  6815. paragraph
  6816. "Lorem ipsum dolor\nsit amet."
  6817. -> list (type=bullet tight=true bullet_char=-)
  6818. list_item
  6819. paragraph
  6820. "Qui *quodsi iracundia*"
  6821. -> list_item
  6822. -> paragraph
  6823. "aliquando id"
  6824. ```
  6825. ## Phase 1: block structure
  6826. Each line that is processed has an effect on this tree. The line is
  6827. analyzed and, depending on its contents, the document may be altered
  6828. in one or more of the following ways:
  6829. 1. One or more open blocks may be closed.
  6830. 2. One or more new blocks may be created as children of the
  6831. last open block.
  6832. 3. Text may be added to the last (deepest) open block remaining
  6833. on the tree.
  6834. Once a line has been incorporated into the tree in this way,
  6835. it can be discarded, so input can be read in a stream.
  6836. For each line, we follow this procedure:
  6837. 1. First we iterate through the open blocks, starting with the
  6838. root document, and descending through last children down to the last
  6839. open block. Each block imposes a condition that the line must satisfy
  6840. if the block is to remain open. For example, a block quote requires a
  6841. `>` character. A paragraph requires a non-blank line.
  6842. In this phase we may match all or just some of the open
  6843. blocks. But we cannot close unmatched blocks yet, because we may have a
  6844. [lazy continuation line].
  6845. 2. Next, after consuming the continuation markers for existing
  6846. blocks, we look for new block starts (e.g. `>` for a block quote).
  6847. If we encounter a new block start, we close any blocks unmatched
  6848. in step 1 before creating the new block as a child of the last
  6849. matched block.
  6850. 3. Finally, we look at the remainder of the line (after block
  6851. markers like `>`, list markers, and indentation have been consumed).
  6852. This is text that can be incorporated into the last open
  6853. block (a paragraph, code block, heading, or raw HTML).
  6854. Setext headings are formed when we see a line of a paragraph
  6855. that is a [setext heading underline].
  6856. Reference link definitions are detected when a paragraph is closed;
  6857. the accumulated text lines are parsed to see if they begin with
  6858. one or more reference link definitions. Any remainder becomes a
  6859. normal paragraph.
  6860. We can see how this works by considering how the tree above is
  6861. generated by four lines of Markdown:
  6862. ``` markdown
  6863. > Lorem ipsum dolor
  6864. sit amet.
  6865. > - Qui *quodsi iracundia*
  6866. > - aliquando id
  6867. ```
  6868. At the outset, our document model is just
  6869. ``` tree
  6870. -> document
  6871. ```
  6872. The first line of our text,
  6873. ``` markdown
  6874. > Lorem ipsum dolor
  6875. ```
  6876. causes a `block_quote` block to be created as a child of our
  6877. open `document` block, and a `paragraph` block as a child of
  6878. the `block_quote`. Then the text is added to the last open
  6879. block, the `paragraph`:
  6880. ``` tree
  6881. -> document
  6882. -> block_quote
  6883. -> paragraph
  6884. "Lorem ipsum dolor"
  6885. ```
  6886. The next line,
  6887. ``` markdown
  6888. sit amet.
  6889. ```
  6890. is a "lazy continuation" of the open `paragraph`, so it gets added
  6891. to the paragraph's text:
  6892. ``` tree
  6893. -> document
  6894. -> block_quote
  6895. -> paragraph
  6896. "Lorem ipsum dolor\nsit amet."
  6897. ```
  6898. The third line,
  6899. ``` markdown
  6900. > - Qui *quodsi iracundia*
  6901. ```
  6902. causes the `paragraph` block to be closed, and a new `list` block
  6903. opened as a child of the `block_quote`. A `list_item` is also
  6904. added as a child of the `list`, and a `paragraph` as a child of
  6905. the `list_item`. The text is then added to the new `paragraph`:
  6906. ``` tree
  6907. -> document
  6908. -> block_quote
  6909. paragraph
  6910. "Lorem ipsum dolor\nsit amet."
  6911. -> list (type=bullet tight=true bullet_char=-)
  6912. -> list_item
  6913. -> paragraph
  6914. "Qui *quodsi iracundia*"
  6915. ```
  6916. The fourth line,
  6917. ``` markdown
  6918. > - aliquando id
  6919. ```
  6920. causes the `list_item` (and its child the `paragraph`) to be closed,
  6921. and a new `list_item` opened up as child of the `list`. A `paragraph`
  6922. is added as a child of the new `list_item`, to contain the text.
  6923. We thus obtain the final tree:
  6924. ``` tree
  6925. -> document
  6926. -> block_quote
  6927. paragraph
  6928. "Lorem ipsum dolor\nsit amet."
  6929. -> list (type=bullet tight=true bullet_char=-)
  6930. list_item
  6931. paragraph
  6932. "Qui *quodsi iracundia*"
  6933. -> list_item
  6934. -> paragraph
  6935. "aliquando id"
  6936. ```
  6937. ## Phase 2: inline structure
  6938. Once all of the input has been parsed, all open blocks are closed.
  6939. We then "walk the tree," visiting every node, and parse raw
  6940. string contents of paragraphs and headings as inlines. At this
  6941. point we have seen all the link reference definitions, so we can
  6942. resolve reference links as we go.
  6943. ``` tree
  6944. document
  6945. block_quote
  6946. paragraph
  6947. str "Lorem ipsum dolor"
  6948. softbreak
  6949. str "sit amet."
  6950. list (type=bullet tight=true bullet_char=-)
  6951. list_item
  6952. paragraph
  6953. str "Qui "
  6954. emph
  6955. str "quodsi iracundia"
  6956. list_item
  6957. paragraph
  6958. str "aliquando id"
  6959. ```
  6960. Notice how the [line ending] in the first paragraph has
  6961. been parsed as a `softbreak`, and the asterisks in the first list item
  6962. have become an `emph`.
  6963. ### An algorithm for parsing nested emphasis and links
  6964. By far the trickiest part of inline parsing is handling emphasis,
  6965. strong emphasis, links, and images. This is done using the following
  6966. algorithm.
  6967. When we're parsing inlines and we hit either
  6968. - a run of `*` or `_` characters, or
  6969. - a `[` or `![`
  6970. we insert a text node with these symbols as its literal content, and we
  6971. add a pointer to this text node to the [delimiter stack](@).
  6972. The [delimiter stack] is a doubly linked list. Each
  6973. element contains a pointer to a text node, plus information about
  6974. - the type of delimiter (`[`, `![`, `*`, `_`)
  6975. - the number of delimiters,
  6976. - whether the delimiter is "active" (all are active to start), and
  6977. - whether the delimiter is a potential opener, a potential closer,
  6978. or both (which depends on what sort of characters precede
  6979. and follow the delimiters).
  6980. When we hit a `]` character, we call the *look for link or image*
  6981. procedure (see below).
  6982. When we hit the end of the input, we call the *process emphasis*
  6983. procedure (see below), with `stack_bottom` = NULL.
  6984. #### *look for link or image*
  6985. Starting at the top of the delimiter stack, we look backwards
  6986. through the stack for an opening `[` or `![` delimiter.
  6987. - If we don't find one, we return a literal text node `]`.
  6988. - If we do find one, but it's not *active*, we remove the inactive
  6989. delimiter from the stack, and return a literal text node `]`.
  6990. - If we find one and it's active, then we parse ahead to see if
  6991. we have an inline link/image, reference link/image, compact reference
  6992. link/image, or shortcut reference link/image.
  6993. + If we don't, then we remove the opening delimiter from the
  6994. delimiter stack and return a literal text node `]`.
  6995. + If we do, then
  6996. * We return a link or image node whose children are the inlines
  6997. after the text node pointed to by the opening delimiter.
  6998. * We run *process emphasis* on these inlines, with the `[` opener
  6999. as `stack_bottom`.
  7000. * We remove the opening delimiter.
  7001. * If we have a link (and not an image), we also set all
  7002. `[` delimiters before the opening delimiter to *inactive*. (This
  7003. will prevent us from getting links within links.)
  7004. #### *process emphasis*
  7005. Parameter `stack_bottom` sets a lower bound to how far we
  7006. descend in the [delimiter stack]. If it is NULL, we can
  7007. go all the way to the bottom. Otherwise, we stop before
  7008. visiting `stack_bottom`.
  7009. Let `current_position` point to the element on the [delimiter stack]
  7010. just above `stack_bottom` (or the first element if `stack_bottom`
  7011. is NULL).
  7012. We keep track of the `openers_bottom` for each delimiter
  7013. type (`*`, `_`). Initialize this to `stack_bottom`.
  7014. Then we repeat the following until we run out of potential
  7015. closers:
  7016. - Move `current_position` forward in the delimiter stack (if needed)
  7017. until we find the first potential closer with delimiter `*` or `_`.
  7018. (This will be the potential closer closest
  7019. to the beginning of the input -- the first one in parse order.)
  7020. - Now, look back in the stack (staying above `stack_bottom` and
  7021. the `openers_bottom` for this delimiter type) for the
  7022. first matching potential opener ("matching" means same delimiter).
  7023. - If one is found:
  7024. + Figure out whether we have emphasis or strong emphasis:
  7025. if both closer and opener spans have length >= 2, we have
  7026. strong, otherwise regular.
  7027. + Insert an emph or strong emph node accordingly, after
  7028. the text node corresponding to the opener.
  7029. + Remove any delimiters between the opener and closer from
  7030. the delimiter stack.
  7031. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7032. from the opening and closing text nodes. If they become empty
  7033. as a result, remove them and remove the corresponding element
  7034. of the delimiter stack. If the closing node is removed, reset
  7035. `current_position` to the next element in the stack.
  7036. - If none in found:
  7037. + Set `openers_bottom` to the element before `current_position`.
  7038. (We know that there are no openers for this kind of closer up to and
  7039. including this point, so this puts a lower bound on future searches.)
  7040. + If the closer at `current_position` is not a potential opener,
  7041. remove it from the delimiter stack (since we know it can't
  7042. be a closer either).
  7043. + Advance `current_position` to the next element in the stack.
  7044. After we're done, we remove all delimiters above `stack_bottom` from the
  7045. delimiter stack.