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