aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: bd2affb3b507d12536cdaabaf40d8b10035a99a5 (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. The title must be separated from the link destination by
  2211. whitespace:
  2212. ```````````````````````````````` example
  2213. [foo]: <bar>(baz)
  2214. [foo]
  2215. .
  2216. <p>[foo]: <bar>(baz)</p>
  2217. <p>[foo]</p>
  2218. ````````````````````````````````
  2219. Both title and destination can contain backslash escapes
  2220. and literal backslashes:
  2221. ```````````````````````````````` example
  2222. [foo]: /url\bar\*baz "foo\"bar\baz"
  2223. [foo]
  2224. .
  2225. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2226. ````````````````````````````````
  2227. A link can come before its corresponding definition:
  2228. ```````````````````````````````` example
  2229. [foo]
  2230. [foo]: url
  2231. .
  2232. <p><a href="url">foo</a></p>
  2233. ````````````````````````````````
  2234. If there are several matching definitions, the first one takes
  2235. precedence:
  2236. ```````````````````````````````` example
  2237. [foo]
  2238. [foo]: first
  2239. [foo]: second
  2240. .
  2241. <p><a href="first">foo</a></p>
  2242. ````````````````````````````````
  2243. As noted in the section on [Links], matching of labels is
  2244. case-insensitive (see [matches]).
  2245. ```````````````````````````````` example
  2246. [FOO]: /url
  2247. [Foo]
  2248. .
  2249. <p><a href="/url">Foo</a></p>
  2250. ````````````````````````````````
  2251. ```````````````````````````````` example
  2252. [ΑΓΩ]: /φου
  2253. [αγω]
  2254. .
  2255. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2256. ````````````````````````````````
  2257. Here is a link reference definition with no corresponding link.
  2258. It contributes nothing to the document.
  2259. ```````````````````````````````` example
  2260. [foo]: /url
  2261. .
  2262. ````````````````````````````````
  2263. Here is another one:
  2264. ```````````````````````````````` example
  2265. [
  2266. foo
  2267. ]: /url
  2268. bar
  2269. .
  2270. <p>bar</p>
  2271. ````````````````````````````````
  2272. This is not a link reference definition, because there are
  2273. [non-whitespace characters] after the title:
  2274. ```````````````````````````````` example
  2275. [foo]: /url "title" ok
  2276. .
  2277. <p>[foo]: /url &quot;title&quot; ok</p>
  2278. ````````````````````````````````
  2279. This is a link reference definition, but it has no title:
  2280. ```````````````````````````````` example
  2281. [foo]: /url
  2282. "title" ok
  2283. .
  2284. <p>&quot;title&quot; ok</p>
  2285. ````````````````````````````````
  2286. This is not a link reference definition, because it is indented
  2287. four spaces:
  2288. ```````````````````````````````` example
  2289. [foo]: /url "title"
  2290. [foo]
  2291. .
  2292. <pre><code>[foo]: /url &quot;title&quot;
  2293. </code></pre>
  2294. <p>[foo]</p>
  2295. ````````````````````````````````
  2296. This is not a link reference definition, because it occurs inside
  2297. a code block:
  2298. ```````````````````````````````` example
  2299. ```
  2300. [foo]: /url
  2301. ```
  2302. [foo]
  2303. .
  2304. <pre><code>[foo]: /url
  2305. </code></pre>
  2306. <p>[foo]</p>
  2307. ````````````````````````````````
  2308. A [link reference definition] cannot interrupt a paragraph.
  2309. ```````````````````````````````` example
  2310. Foo
  2311. [bar]: /baz
  2312. [bar]
  2313. .
  2314. <p>Foo
  2315. [bar]: /baz</p>
  2316. <p>[bar]</p>
  2317. ````````````````````````````````
  2318. However, it can directly follow other block elements, such as headings
  2319. and thematic breaks, and it need not be followed by a blank line.
  2320. ```````````````````````````````` example
  2321. # [Foo]
  2322. [foo]: /url
  2323. > bar
  2324. .
  2325. <h1><a href="/url">Foo</a></h1>
  2326. <blockquote>
  2327. <p>bar</p>
  2328. </blockquote>
  2329. ````````````````````````````````
  2330. Several [link reference definitions]
  2331. can occur one after another, without intervening blank lines.
  2332. ```````````````````````````````` example
  2333. [foo]: /foo-url "foo"
  2334. [bar]: /bar-url
  2335. "bar"
  2336. [baz]: /baz-url
  2337. [foo],
  2338. [bar],
  2339. [baz]
  2340. .
  2341. <p><a href="/foo-url" title="foo">foo</a>,
  2342. <a href="/bar-url" title="bar">bar</a>,
  2343. <a href="/baz-url">baz</a></p>
  2344. ````````````````````````````````
  2345. [Link reference definitions] can occur
  2346. inside block containers, like lists and block quotations. They
  2347. affect the entire document, not just the container in which they
  2348. are defined:
  2349. ```````````````````````````````` example
  2350. [foo]
  2351. > [foo]: /url
  2352. .
  2353. <p><a href="/url">foo</a></p>
  2354. <blockquote>
  2355. </blockquote>
  2356. ````````````````````````````````
  2357. ## Paragraphs
  2358. A sequence of non-blank lines that cannot be interpreted as other
  2359. kinds of blocks forms a [paragraph](@).
  2360. The contents of the paragraph are the result of parsing the
  2361. paragraph's raw content as inlines. The paragraph's raw content
  2362. is formed by concatenating the lines and removing initial and final
  2363. [whitespace].
  2364. A simple example with two paragraphs:
  2365. ```````````````````````````````` example
  2366. aaa
  2367. bbb
  2368. .
  2369. <p>aaa</p>
  2370. <p>bbb</p>
  2371. ````````````````````````````````
  2372. Paragraphs can contain multiple lines, but no blank lines:
  2373. ```````````````````````````````` example
  2374. aaa
  2375. bbb
  2376. ccc
  2377. ddd
  2378. .
  2379. <p>aaa
  2380. bbb</p>
  2381. <p>ccc
  2382. ddd</p>
  2383. ````````````````````````````````
  2384. Multiple blank lines between paragraph have no effect:
  2385. ```````````````````````````````` example
  2386. aaa
  2387. bbb
  2388. .
  2389. <p>aaa</p>
  2390. <p>bbb</p>
  2391. ````````````````````````````````
  2392. Leading spaces are skipped:
  2393. ```````````````````````````````` example
  2394. aaa
  2395. bbb
  2396. .
  2397. <p>aaa
  2398. bbb</p>
  2399. ````````````````````````````````
  2400. Lines after the first may be indented any amount, since indented
  2401. code blocks cannot interrupt paragraphs.
  2402. ```````````````````````````````` example
  2403. aaa
  2404. bbb
  2405. ccc
  2406. .
  2407. <p>aaa
  2408. bbb
  2409. ccc</p>
  2410. ````````````````````````````````
  2411. However, the first line may be indented at most three spaces,
  2412. or an indented code block will be triggered:
  2413. ```````````````````````````````` example
  2414. aaa
  2415. bbb
  2416. .
  2417. <p>aaa
  2418. bbb</p>
  2419. ````````````````````````````````
  2420. ```````````````````````````````` example
  2421. aaa
  2422. bbb
  2423. .
  2424. <pre><code>aaa
  2425. </code></pre>
  2426. <p>bbb</p>
  2427. ````````````````````````````````
  2428. Final spaces are stripped before inline parsing, so a paragraph
  2429. that ends with two or more spaces will not end with a [hard line
  2430. break]:
  2431. ```````````````````````````````` example
  2432. aaa
  2433. bbb
  2434. .
  2435. <p>aaa<br />
  2436. bbb</p>
  2437. ````````````````````````````````
  2438. ## Blank lines
  2439. [Blank lines] between block-level elements are ignored,
  2440. except for the role they play in determining whether a [list]
  2441. is [tight] or [loose].
  2442. Blank lines at the beginning and end of the document are also ignored.
  2443. ```````````````````````````````` example
  2444. aaa
  2445. # aaa
  2446. .
  2447. <p>aaa</p>
  2448. <h1>aaa</h1>
  2449. ````````````````````````````````
  2450. # Container blocks
  2451. A [container block](#container-blocks) is a block that has other
  2452. blocks as its contents. There are two basic kinds of container blocks:
  2453. [block quotes] and [list items].
  2454. [Lists] are meta-containers for [list items].
  2455. We define the syntax for container blocks recursively. The general
  2456. form of the definition is:
  2457. > If X is a sequence of blocks, then the result of
  2458. > transforming X in such-and-such a way is a container of type Y
  2459. > with these blocks as its content.
  2460. So, we explain what counts as a block quote or list item by explaining
  2461. how these can be *generated* from their contents. This should suffice
  2462. to define the syntax, although it does not give a recipe for *parsing*
  2463. these constructions. (A recipe is provided below in the section entitled
  2464. [A parsing strategy](#appendix-a-parsing-strategy).)
  2465. ## Block quotes
  2466. A [block quote marker](@)
  2467. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2468. with a following space, or (b) a single character `>` not followed by a space.
  2469. The following rules define [block quotes]:
  2470. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2471. of blocks *Bs*, then the result of prepending a [block quote
  2472. marker] to the beginning of each line in *Ls*
  2473. is a [block quote](#block-quotes) containing *Bs*.
  2474. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2475. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2476. the initial [block quote marker] from one or
  2477. more lines in which the next [non-whitespace character] after the [block
  2478. quote marker] is [paragraph continuation
  2479. text] is a block quote with *Bs* as its content.
  2480. [Paragraph continuation text](@) is text
  2481. that will be parsed as part of the content of a paragraph, but does
  2482. not occur at the beginning of the paragraph.
  2483. 3. **Consecutiveness.** A document cannot contain two [block
  2484. quotes] in a row unless there is a [blank line] between them.
  2485. Nothing else counts as a [block quote](#block-quotes).
  2486. Here is a simple example:
  2487. ```````````````````````````````` example
  2488. > # Foo
  2489. > bar
  2490. > baz
  2491. .
  2492. <blockquote>
  2493. <h1>Foo</h1>
  2494. <p>bar
  2495. baz</p>
  2496. </blockquote>
  2497. ````````````````````````````````
  2498. The spaces after the `>` characters can be omitted:
  2499. ```````````````````````````````` example
  2500. ># Foo
  2501. >bar
  2502. > baz
  2503. .
  2504. <blockquote>
  2505. <h1>Foo</h1>
  2506. <p>bar
  2507. baz</p>
  2508. </blockquote>
  2509. ````````````````````````````````
  2510. The `>` characters can be indented 1-3 spaces:
  2511. ```````````````````````````````` example
  2512. > # Foo
  2513. > bar
  2514. > baz
  2515. .
  2516. <blockquote>
  2517. <h1>Foo</h1>
  2518. <p>bar
  2519. baz</p>
  2520. </blockquote>
  2521. ````````````````````````````````
  2522. Four spaces gives us a code block:
  2523. ```````````````````````````````` example
  2524. > # Foo
  2525. > bar
  2526. > baz
  2527. .
  2528. <pre><code>&gt; # Foo
  2529. &gt; bar
  2530. &gt; baz
  2531. </code></pre>
  2532. ````````````````````````````````
  2533. The Laziness clause allows us to omit the `>` before
  2534. [paragraph continuation text]:
  2535. ```````````````````````````````` example
  2536. > # Foo
  2537. > bar
  2538. baz
  2539. .
  2540. <blockquote>
  2541. <h1>Foo</h1>
  2542. <p>bar
  2543. baz</p>
  2544. </blockquote>
  2545. ````````````````````````````````
  2546. A block quote can contain some lazy and some non-lazy
  2547. continuation lines:
  2548. ```````````````````````````````` example
  2549. > bar
  2550. baz
  2551. > foo
  2552. .
  2553. <blockquote>
  2554. <p>bar
  2555. baz
  2556. foo</p>
  2557. </blockquote>
  2558. ````````````````````````````````
  2559. Laziness only applies to lines that would have been continuations of
  2560. paragraphs had they been prepended with [block quote markers].
  2561. For example, the `> ` cannot be omitted in the second line of
  2562. ``` markdown
  2563. > foo
  2564. > ---
  2565. ```
  2566. without changing the meaning:
  2567. ```````````````````````````````` example
  2568. > foo
  2569. ---
  2570. .
  2571. <blockquote>
  2572. <p>foo</p>
  2573. </blockquote>
  2574. <hr />
  2575. ````````````````````````````````
  2576. Similarly, if we omit the `> ` in the second line of
  2577. ``` markdown
  2578. > - foo
  2579. > - bar
  2580. ```
  2581. then the block quote ends after the first line:
  2582. ```````````````````````````````` example
  2583. > - foo
  2584. - bar
  2585. .
  2586. <blockquote>
  2587. <ul>
  2588. <li>foo</li>
  2589. </ul>
  2590. </blockquote>
  2591. <ul>
  2592. <li>bar</li>
  2593. </ul>
  2594. ````````````````````````````````
  2595. For the same reason, we can't omit the `> ` in front of
  2596. subsequent lines of an indented or fenced code block:
  2597. ```````````````````````````````` example
  2598. > foo
  2599. bar
  2600. .
  2601. <blockquote>
  2602. <pre><code>foo
  2603. </code></pre>
  2604. </blockquote>
  2605. <pre><code>bar
  2606. </code></pre>
  2607. ````````````````````````````````
  2608. ```````````````````````````````` example
  2609. > ```
  2610. foo
  2611. ```
  2612. .
  2613. <blockquote>
  2614. <pre><code></code></pre>
  2615. </blockquote>
  2616. <p>foo</p>
  2617. <pre><code></code></pre>
  2618. ````````````````````````````````
  2619. Note that in the following case, we have a [lazy
  2620. continuation line]:
  2621. ```````````````````````````````` example
  2622. > foo
  2623. - bar
  2624. .
  2625. <blockquote>
  2626. <p>foo
  2627. - bar</p>
  2628. </blockquote>
  2629. ````````````````````````````````
  2630. To see why, note that in
  2631. ```markdown
  2632. > foo
  2633. > - bar
  2634. ```
  2635. the `- bar` is indented too far to start a list, and can't
  2636. be an indented code block because indented code blocks cannot
  2637. interrupt paragraphs, so it is [paragraph continuation text].
  2638. A block quote can be empty:
  2639. ```````````````````````````````` example
  2640. >
  2641. .
  2642. <blockquote>
  2643. </blockquote>
  2644. ````````````````````````````````
  2645. ```````````````````````````````` example
  2646. >
  2647. >
  2648. >
  2649. .
  2650. <blockquote>
  2651. </blockquote>
  2652. ````````````````````````````````
  2653. A block quote can have initial or final blank lines:
  2654. ```````````````````````````````` example
  2655. >
  2656. > foo
  2657. >
  2658. .
  2659. <blockquote>
  2660. <p>foo</p>
  2661. </blockquote>
  2662. ````````````````````````````````
  2663. A blank line always separates block quotes:
  2664. ```````````````````````````````` example
  2665. > foo
  2666. > bar
  2667. .
  2668. <blockquote>
  2669. <p>foo</p>
  2670. </blockquote>
  2671. <blockquote>
  2672. <p>bar</p>
  2673. </blockquote>
  2674. ````````````````````````````````
  2675. (Most current Markdown implementations, including John Gruber's
  2676. original `Markdown.pl`, will parse this example as a single block quote
  2677. with two paragraphs. But it seems better to allow the author to decide
  2678. whether two block quotes or one are wanted.)
  2679. Consecutiveness means that if we put these block quotes together,
  2680. we get a single block quote:
  2681. ```````````````````````````````` example
  2682. > foo
  2683. > bar
  2684. .
  2685. <blockquote>
  2686. <p>foo
  2687. bar</p>
  2688. </blockquote>
  2689. ````````````````````````````````
  2690. To get a block quote with two paragraphs, use:
  2691. ```````````````````````````````` example
  2692. > foo
  2693. >
  2694. > bar
  2695. .
  2696. <blockquote>
  2697. <p>foo</p>
  2698. <p>bar</p>
  2699. </blockquote>
  2700. ````````````````````````````````
  2701. Block quotes can interrupt paragraphs:
  2702. ```````````````````````````````` example
  2703. foo
  2704. > bar
  2705. .
  2706. <p>foo</p>
  2707. <blockquote>
  2708. <p>bar</p>
  2709. </blockquote>
  2710. ````````````````````````````````
  2711. In general, blank lines are not needed before or after block
  2712. quotes:
  2713. ```````````````````````````````` example
  2714. > aaa
  2715. ***
  2716. > bbb
  2717. .
  2718. <blockquote>
  2719. <p>aaa</p>
  2720. </blockquote>
  2721. <hr />
  2722. <blockquote>
  2723. <p>bbb</p>
  2724. </blockquote>
  2725. ````````````````````````````````
  2726. However, because of laziness, a blank line is needed between
  2727. a block quote and a following paragraph:
  2728. ```````````````````````````````` example
  2729. > bar
  2730. baz
  2731. .
  2732. <blockquote>
  2733. <p>bar
  2734. baz</p>
  2735. </blockquote>
  2736. ````````````````````````````````
  2737. ```````````````````````````````` example
  2738. > bar
  2739. baz
  2740. .
  2741. <blockquote>
  2742. <p>bar</p>
  2743. </blockquote>
  2744. <p>baz</p>
  2745. ````````````````````````````````
  2746. ```````````````````````````````` example
  2747. > bar
  2748. >
  2749. baz
  2750. .
  2751. <blockquote>
  2752. <p>bar</p>
  2753. </blockquote>
  2754. <p>baz</p>
  2755. ````````````````````````````````
  2756. It is a consequence of the Laziness rule that any number
  2757. of initial `>`s may be omitted on a continuation line of a
  2758. nested block quote:
  2759. ```````````````````````````````` example
  2760. > > > foo
  2761. bar
  2762. .
  2763. <blockquote>
  2764. <blockquote>
  2765. <blockquote>
  2766. <p>foo
  2767. bar</p>
  2768. </blockquote>
  2769. </blockquote>
  2770. </blockquote>
  2771. ````````````````````````````````
  2772. ```````````````````````````````` example
  2773. >>> foo
  2774. > bar
  2775. >>baz
  2776. .
  2777. <blockquote>
  2778. <blockquote>
  2779. <blockquote>
  2780. <p>foo
  2781. bar
  2782. baz</p>
  2783. </blockquote>
  2784. </blockquote>
  2785. </blockquote>
  2786. ````````````````````````````````
  2787. When including an indented code block in a block quote,
  2788. remember that the [block quote marker] includes
  2789. both the `>` and a following space. So *five spaces* are needed after
  2790. the `>`:
  2791. ```````````````````````````````` example
  2792. > code
  2793. > not code
  2794. .
  2795. <blockquote>
  2796. <pre><code>code
  2797. </code></pre>
  2798. </blockquote>
  2799. <blockquote>
  2800. <p>not code</p>
  2801. </blockquote>
  2802. ````````````````````````````````
  2803. ## List items
  2804. A [list marker](@) is a
  2805. [bullet list marker] or an [ordered list marker].
  2806. A [bullet list marker](@)
  2807. is a `-`, `+`, or `*` character.
  2808. An [ordered list marker](@)
  2809. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2810. `.` character or a `)` character. (The reason for the length
  2811. limit is that with 10 digits we start seeing integer overflows
  2812. in some browsers.)
  2813. The following rules define [list items]:
  2814. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2815. blocks *Bs* starting with a [non-whitespace character] and not separated
  2816. from each other by more than one blank line, and *M* is a list
  2817. marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2818. of prepending *M* and the following spaces to the first line of
  2819. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2820. list item with *Bs* as its contents. The type of the list item
  2821. (bullet or ordered) is determined by the type of its list marker.
  2822. If the list item is ordered, then it is also assigned a start
  2823. number, based on the ordered list marker.
  2824. Exceptions:
  2825. 1. When the first list item in a [list] interrupts
  2826. a paragraph---that is, when it starts on a line that would
  2827. otherwise count as [paragraph continuation text]---then (a)
  2828. the lines *Ls* must not begin with a blank line, and (b) if
  2829. the list item is ordered, the start number must be 1.
  2830. 2. If any line is a [thematic break][thematic breaks] then
  2831. that line is not a list item.
  2832. For example, let *Ls* be the lines
  2833. ```````````````````````````````` example
  2834. A paragraph
  2835. with two lines.
  2836. indented code
  2837. > A block quote.
  2838. .
  2839. <p>A paragraph
  2840. with two lines.</p>
  2841. <pre><code>indented code
  2842. </code></pre>
  2843. <blockquote>
  2844. <p>A block quote.</p>
  2845. </blockquote>
  2846. ````````````````````````````````
  2847. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2848. that the following is an ordered list item with start number 1,
  2849. and the same contents as *Ls*:
  2850. ```````````````````````````````` example
  2851. 1. A paragraph
  2852. with two lines.
  2853. indented code
  2854. > A block quote.
  2855. .
  2856. <ol>
  2857. <li>
  2858. <p>A paragraph
  2859. with two lines.</p>
  2860. <pre><code>indented code
  2861. </code></pre>
  2862. <blockquote>
  2863. <p>A block quote.</p>
  2864. </blockquote>
  2865. </li>
  2866. </ol>
  2867. ````````````````````````````````
  2868. The most important thing to notice is that the position of
  2869. the text after the list marker determines how much indentation
  2870. is needed in subsequent blocks in the list item. If the list
  2871. marker takes up two spaces, and there are three spaces between
  2872. the list marker and the next [non-whitespace character], then blocks
  2873. must be indented five spaces in order to fall under the list
  2874. item.
  2875. Here are some examples showing how far content must be indented to be
  2876. put under the list item:
  2877. ```````````````````````````````` example
  2878. - one
  2879. two
  2880. .
  2881. <ul>
  2882. <li>one</li>
  2883. </ul>
  2884. <p>two</p>
  2885. ````````````````````````````````
  2886. ```````````````````````````````` example
  2887. - one
  2888. two
  2889. .
  2890. <ul>
  2891. <li>
  2892. <p>one</p>
  2893. <p>two</p>
  2894. </li>
  2895. </ul>
  2896. ````````````````````````````````
  2897. ```````````````````````````````` example
  2898. - one
  2899. two
  2900. .
  2901. <ul>
  2902. <li>one</li>
  2903. </ul>
  2904. <pre><code> two
  2905. </code></pre>
  2906. ````````````````````````````````
  2907. ```````````````````````````````` example
  2908. - one
  2909. two
  2910. .
  2911. <ul>
  2912. <li>
  2913. <p>one</p>
  2914. <p>two</p>
  2915. </li>
  2916. </ul>
  2917. ````````````````````````````````
  2918. It is tempting to think of this in terms of columns: the continuation
  2919. blocks must be indented at least to the column of the first
  2920. [non-whitespace character] after the list marker. However, that is not quite right.
  2921. The spaces after the list marker determine how much relative indentation
  2922. is needed. Which column this indentation reaches will depend on
  2923. how the list item is embedded in other constructions, as shown by
  2924. this example:
  2925. ```````````````````````````````` example
  2926. > > 1. one
  2927. >>
  2928. >> two
  2929. .
  2930. <blockquote>
  2931. <blockquote>
  2932. <ol>
  2933. <li>
  2934. <p>one</p>
  2935. <p>two</p>
  2936. </li>
  2937. </ol>
  2938. </blockquote>
  2939. </blockquote>
  2940. ````````````````````````````````
  2941. Here `two` occurs in the same column as the list marker `1.`,
  2942. but is actually contained in the list item, because there is
  2943. sufficient indentation after the last containing blockquote marker.
  2944. The converse is also possible. In the following example, the word `two`
  2945. occurs far to the right of the initial text of the list item, `one`, but
  2946. it is not considered part of the list item, because it is not indented
  2947. far enough past the blockquote marker:
  2948. ```````````````````````````````` example
  2949. >>- one
  2950. >>
  2951. > > two
  2952. .
  2953. <blockquote>
  2954. <blockquote>
  2955. <ul>
  2956. <li>one</li>
  2957. </ul>
  2958. <p>two</p>
  2959. </blockquote>
  2960. </blockquote>
  2961. ````````````````````````````````
  2962. Note that at least one space is needed between the list marker and
  2963. any following content, so these are not list items:
  2964. ```````````````````````````````` example
  2965. -one
  2966. 2.two
  2967. .
  2968. <p>-one</p>
  2969. <p>2.two</p>
  2970. ````````````````````````````````
  2971. A list item may contain blocks that are separated by more than
  2972. one blank line.
  2973. ```````````````````````````````` example
  2974. - foo
  2975. bar
  2976. .
  2977. <ul>
  2978. <li>
  2979. <p>foo</p>
  2980. <p>bar</p>
  2981. </li>
  2982. </ul>
  2983. ````````````````````````````````
  2984. A list item may contain any kind of block:
  2985. ```````````````````````````````` example
  2986. 1. foo
  2987. ```
  2988. bar
  2989. ```
  2990. baz
  2991. > bam
  2992. .
  2993. <ol>
  2994. <li>
  2995. <p>foo</p>
  2996. <pre><code>bar
  2997. </code></pre>
  2998. <p>baz</p>
  2999. <blockquote>
  3000. <p>bam</p>
  3001. </blockquote>
  3002. </li>
  3003. </ol>
  3004. ````````````````````````````````
  3005. A list item that contains an indented code block will preserve
  3006. empty lines within the code block verbatim.
  3007. ```````````````````````````````` example
  3008. - Foo
  3009. bar
  3010. baz
  3011. .
  3012. <ul>
  3013. <li>
  3014. <p>Foo</p>
  3015. <pre><code>bar
  3016. baz
  3017. </code></pre>
  3018. </li>
  3019. </ul>
  3020. ````````````````````````````````
  3021. Note that ordered list start numbers must be nine digits or less:
  3022. ```````````````````````````````` example
  3023. 123456789. ok
  3024. .
  3025. <ol start="123456789">
  3026. <li>ok</li>
  3027. </ol>
  3028. ````````````````````````````````
  3029. ```````````````````````````````` example
  3030. 1234567890. not ok
  3031. .
  3032. <p>1234567890. not ok</p>
  3033. ````````````````````````````````
  3034. A start number may begin with 0s:
  3035. ```````````````````````````````` example
  3036. 0. ok
  3037. .
  3038. <ol start="0">
  3039. <li>ok</li>
  3040. </ol>
  3041. ````````````````````````````````
  3042. ```````````````````````````````` example
  3043. 003. ok
  3044. .
  3045. <ol start="3">
  3046. <li>ok</li>
  3047. </ol>
  3048. ````````````````````````````````
  3049. A start number may not be negative:
  3050. ```````````````````````````````` example
  3051. -1. not ok
  3052. .
  3053. <p>-1. not ok</p>
  3054. ````````````````````````````````
  3055. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3056. constitute a sequence of blocks *Bs* starting with an indented code
  3057. block and not separated from each other by more than one blank line,
  3058. and *M* is a list marker of width *W* followed by
  3059. one space, then the result of prepending *M* and the following
  3060. space to the first line of *Ls*, and indenting subsequent lines of
  3061. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3062. If a line is empty, then it need not be indented. The type of the
  3063. list item (bullet or ordered) is determined by the type of its list
  3064. marker. If the list item is ordered, then it is also assigned a
  3065. start number, based on the ordered list marker.
  3066. An indented code block will have to be indented four spaces beyond
  3067. the edge of the region where text will be included in the list item.
  3068. In the following case that is 6 spaces:
  3069. ```````````````````````````````` example
  3070. - foo
  3071. bar
  3072. .
  3073. <ul>
  3074. <li>
  3075. <p>foo</p>
  3076. <pre><code>bar
  3077. </code></pre>
  3078. </li>
  3079. </ul>
  3080. ````````````````````````````````
  3081. And in this case it is 11 spaces:
  3082. ```````````````````````````````` example
  3083. 10. foo
  3084. bar
  3085. .
  3086. <ol start="10">
  3087. <li>
  3088. <p>foo</p>
  3089. <pre><code>bar
  3090. </code></pre>
  3091. </li>
  3092. </ol>
  3093. ````````````````````````````````
  3094. If the *first* block in the list item is an indented code block,
  3095. then by rule #2, the contents must be indented *one* space after the
  3096. list marker:
  3097. ```````````````````````````````` example
  3098. indented code
  3099. paragraph
  3100. more code
  3101. .
  3102. <pre><code>indented code
  3103. </code></pre>
  3104. <p>paragraph</p>
  3105. <pre><code>more code
  3106. </code></pre>
  3107. ````````````````````````````````
  3108. ```````````````````````````````` example
  3109. 1. indented code
  3110. paragraph
  3111. more code
  3112. .
  3113. <ol>
  3114. <li>
  3115. <pre><code>indented code
  3116. </code></pre>
  3117. <p>paragraph</p>
  3118. <pre><code>more code
  3119. </code></pre>
  3120. </li>
  3121. </ol>
  3122. ````````````````````````````````
  3123. Note that an additional space indent is interpreted as space
  3124. inside the code block:
  3125. ```````````````````````````````` example
  3126. 1. indented code
  3127. paragraph
  3128. more code
  3129. .
  3130. <ol>
  3131. <li>
  3132. <pre><code> indented code
  3133. </code></pre>
  3134. <p>paragraph</p>
  3135. <pre><code>more code
  3136. </code></pre>
  3137. </li>
  3138. </ol>
  3139. ````````````````````````````````
  3140. Note that rules #1 and #2 only apply to two cases: (a) cases
  3141. in which the lines to be included in a list item begin with a
  3142. [non-whitespace character], and (b) cases in which
  3143. they begin with an indented code
  3144. block. In a case like the following, where the first block begins with
  3145. a three-space indent, the rules do not allow us to form a list item by
  3146. indenting the whole thing and prepending a list marker:
  3147. ```````````````````````````````` example
  3148. foo
  3149. bar
  3150. .
  3151. <p>foo</p>
  3152. <p>bar</p>
  3153. ````````````````````````````````
  3154. ```````````````````````````````` example
  3155. - foo
  3156. bar
  3157. .
  3158. <ul>
  3159. <li>foo</li>
  3160. </ul>
  3161. <p>bar</p>
  3162. ````````````````````````````````
  3163. This is not a significant restriction, because when a block begins
  3164. with 1-3 spaces indent, the indentation can always be removed without
  3165. a change in interpretation, allowing rule #1 to be applied. So, in
  3166. the above case:
  3167. ```````````````````````````````` example
  3168. - foo
  3169. bar
  3170. .
  3171. <ul>
  3172. <li>
  3173. <p>foo</p>
  3174. <p>bar</p>
  3175. </li>
  3176. </ul>
  3177. ````````````````````````````````
  3178. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3179. starting with a single [blank line] constitute a (possibly empty)
  3180. sequence of blocks *Bs*, not separated from each other by more than
  3181. one blank line, and *M* is a list marker of width *W*,
  3182. then the result of prepending *M* to the first line of *Ls*, and
  3183. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3184. item with *Bs* as its contents.
  3185. If a line is empty, then it need not be indented. The type of the
  3186. list item (bullet or ordered) is determined by the type of its list
  3187. marker. If the list item is ordered, then it is also assigned a
  3188. start number, based on the ordered list marker.
  3189. Here are some list items that start with a blank line but are not empty:
  3190. ```````````````````````````````` example
  3191. -
  3192. foo
  3193. -
  3194. ```
  3195. bar
  3196. ```
  3197. -
  3198. baz
  3199. .
  3200. <ul>
  3201. <li>foo</li>
  3202. <li>
  3203. <pre><code>bar
  3204. </code></pre>
  3205. </li>
  3206. <li>
  3207. <pre><code>baz
  3208. </code></pre>
  3209. </li>
  3210. </ul>
  3211. ````````````````````````````````
  3212. When the list item starts with a blank line, the number of spaces
  3213. following the list marker doesn't change the required indentation:
  3214. ```````````````````````````````` example
  3215. -
  3216. foo
  3217. .
  3218. <ul>
  3219. <li>foo</li>
  3220. </ul>
  3221. ````````````````````````````````
  3222. A list item can begin with at most one blank line.
  3223. In the following example, `foo` is not part of the list
  3224. item:
  3225. ```````````````````````````````` example
  3226. -
  3227. foo
  3228. .
  3229. <ul>
  3230. <li></li>
  3231. </ul>
  3232. <p>foo</p>
  3233. ````````````````````````````````
  3234. Here is an empty bullet list item:
  3235. ```````````````````````````````` example
  3236. - foo
  3237. -
  3238. - bar
  3239. .
  3240. <ul>
  3241. <li>foo</li>
  3242. <li></li>
  3243. <li>bar</li>
  3244. </ul>
  3245. ````````````````````````````````
  3246. It does not matter whether there are spaces following the [list marker]:
  3247. ```````````````````````````````` example
  3248. - foo
  3249. -
  3250. - bar
  3251. .
  3252. <ul>
  3253. <li>foo</li>
  3254. <li></li>
  3255. <li>bar</li>
  3256. </ul>
  3257. ````````````````````````````````
  3258. Here is an empty ordered list item:
  3259. ```````````````````````````````` example
  3260. 1. foo
  3261. 2.
  3262. 3. bar
  3263. .
  3264. <ol>
  3265. <li>foo</li>
  3266. <li></li>
  3267. <li>bar</li>
  3268. </ol>
  3269. ````````````````````````````````
  3270. A list may start or end with an empty list item:
  3271. ```````````````````````````````` example
  3272. *
  3273. .
  3274. <ul>
  3275. <li></li>
  3276. </ul>
  3277. ````````````````````````````````
  3278. However, an empty list item cannot interrupt a paragraph:
  3279. ```````````````````````````````` example
  3280. foo
  3281. *
  3282. foo
  3283. 1.
  3284. .
  3285. <p>foo
  3286. *</p>
  3287. <p>foo
  3288. 1.</p>
  3289. ````````````````````````````````
  3290. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3291. according to rule #1, #2, or #3, then the result of indenting each line
  3292. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3293. list item with the same contents and attributes. If a line is
  3294. empty, then it need not be indented.
  3295. Indented one space:
  3296. ```````````````````````````````` example
  3297. 1. A paragraph
  3298. with two lines.
  3299. indented code
  3300. > A block quote.
  3301. .
  3302. <ol>
  3303. <li>
  3304. <p>A paragraph
  3305. with two lines.</p>
  3306. <pre><code>indented code
  3307. </code></pre>
  3308. <blockquote>
  3309. <p>A block quote.</p>
  3310. </blockquote>
  3311. </li>
  3312. </ol>
  3313. ````````````````````````````````
  3314. Indented two spaces:
  3315. ```````````````````````````````` example
  3316. 1. A paragraph
  3317. with two lines.
  3318. indented code
  3319. > A block quote.
  3320. .
  3321. <ol>
  3322. <li>
  3323. <p>A paragraph
  3324. with two lines.</p>
  3325. <pre><code>indented code
  3326. </code></pre>
  3327. <blockquote>
  3328. <p>A block quote.</p>
  3329. </blockquote>
  3330. </li>
  3331. </ol>
  3332. ````````````````````````````````
  3333. Indented three spaces:
  3334. ```````````````````````````````` example
  3335. 1. A paragraph
  3336. with two lines.
  3337. indented code
  3338. > A block quote.
  3339. .
  3340. <ol>
  3341. <li>
  3342. <p>A paragraph
  3343. with two lines.</p>
  3344. <pre><code>indented code
  3345. </code></pre>
  3346. <blockquote>
  3347. <p>A block quote.</p>
  3348. </blockquote>
  3349. </li>
  3350. </ol>
  3351. ````````````````````````````````
  3352. Four spaces indent gives a code block:
  3353. ```````````````````````````````` example
  3354. 1. A paragraph
  3355. with two lines.
  3356. indented code
  3357. > A block quote.
  3358. .
  3359. <pre><code>1. A paragraph
  3360. with two lines.
  3361. indented code
  3362. &gt; A block quote.
  3363. </code></pre>
  3364. ````````````````````````````````
  3365. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3366. item](#list-items) with contents *Bs*, then the result of deleting
  3367. some or all of the indentation from one or more lines in which the
  3368. next [non-whitespace character] after the indentation is
  3369. [paragraph continuation text] is a
  3370. list item with the same contents and attributes. The unindented
  3371. lines are called
  3372. [lazy continuation line](@)s.
  3373. Here is an example with [lazy continuation lines]:
  3374. ```````````````````````````````` example
  3375. 1. A paragraph
  3376. with two lines.
  3377. indented code
  3378. > A block quote.
  3379. .
  3380. <ol>
  3381. <li>
  3382. <p>A paragraph
  3383. with two lines.</p>
  3384. <pre><code>indented code
  3385. </code></pre>
  3386. <blockquote>
  3387. <p>A block quote.</p>
  3388. </blockquote>
  3389. </li>
  3390. </ol>
  3391. ````````````````````````````````
  3392. Indentation can be partially deleted:
  3393. ```````````````````````````````` example
  3394. 1. A paragraph
  3395. with two lines.
  3396. .
  3397. <ol>
  3398. <li>A paragraph
  3399. with two lines.</li>
  3400. </ol>
  3401. ````````````````````````````````
  3402. These examples show how laziness can work in nested structures:
  3403. ```````````````````````````````` example
  3404. > 1. > Blockquote
  3405. continued here.
  3406. .
  3407. <blockquote>
  3408. <ol>
  3409. <li>
  3410. <blockquote>
  3411. <p>Blockquote
  3412. continued here.</p>
  3413. </blockquote>
  3414. </li>
  3415. </ol>
  3416. </blockquote>
  3417. ````````````````````````````````
  3418. ```````````````````````````````` example
  3419. > 1. > Blockquote
  3420. > continued here.
  3421. .
  3422. <blockquote>
  3423. <ol>
  3424. <li>
  3425. <blockquote>
  3426. <p>Blockquote
  3427. continued here.</p>
  3428. </blockquote>
  3429. </li>
  3430. </ol>
  3431. </blockquote>
  3432. ````````````````````````````````
  3433. 6. **That's all.** Nothing that is not counted as a list item by rules
  3434. #1--5 counts as a [list item](#list-items).
  3435. The rules for sublists follow from the general rules
  3436. [above][List items]. A sublist must be indented the same number
  3437. of spaces a paragraph would need to be in order to be included
  3438. in the list item.
  3439. So, in this case we need two spaces indent:
  3440. ```````````````````````````````` example
  3441. - foo
  3442. - bar
  3443. - baz
  3444. - boo
  3445. .
  3446. <ul>
  3447. <li>foo
  3448. <ul>
  3449. <li>bar
  3450. <ul>
  3451. <li>baz
  3452. <ul>
  3453. <li>boo</li>
  3454. </ul>
  3455. </li>
  3456. </ul>
  3457. </li>
  3458. </ul>
  3459. </li>
  3460. </ul>
  3461. ````````````````````````````````
  3462. One is not enough:
  3463. ```````````````````````````````` example
  3464. - foo
  3465. - bar
  3466. - baz
  3467. - boo
  3468. .
  3469. <ul>
  3470. <li>foo</li>
  3471. <li>bar</li>
  3472. <li>baz</li>
  3473. <li>boo</li>
  3474. </ul>
  3475. ````````````````````````````````
  3476. Here we need four, because the list marker is wider:
  3477. ```````````````````````````````` example
  3478. 10) foo
  3479. - bar
  3480. .
  3481. <ol start="10">
  3482. <li>foo
  3483. <ul>
  3484. <li>bar</li>
  3485. </ul>
  3486. </li>
  3487. </ol>
  3488. ````````````````````````````````
  3489. Three is not enough:
  3490. ```````````````````````````````` example
  3491. 10) foo
  3492. - bar
  3493. .
  3494. <ol start="10">
  3495. <li>foo</li>
  3496. </ol>
  3497. <ul>
  3498. <li>bar</li>
  3499. </ul>
  3500. ````````````````````````````````
  3501. A list may be the first block in a list item:
  3502. ```````````````````````````````` example
  3503. - - foo
  3504. .
  3505. <ul>
  3506. <li>
  3507. <ul>
  3508. <li>foo</li>
  3509. </ul>
  3510. </li>
  3511. </ul>
  3512. ````````````````````````````````
  3513. ```````````````````````````````` example
  3514. 1. - 2. foo
  3515. .
  3516. <ol>
  3517. <li>
  3518. <ul>
  3519. <li>
  3520. <ol start="2">
  3521. <li>foo</li>
  3522. </ol>
  3523. </li>
  3524. </ul>
  3525. </li>
  3526. </ol>
  3527. ````````````````````````````````
  3528. A list item can contain a heading:
  3529. ```````````````````````````````` example
  3530. - # Foo
  3531. - Bar
  3532. ---
  3533. baz
  3534. .
  3535. <ul>
  3536. <li>
  3537. <h1>Foo</h1>
  3538. </li>
  3539. <li>
  3540. <h2>Bar</h2>
  3541. baz</li>
  3542. </ul>
  3543. ````````````````````````````````
  3544. ### Motivation
  3545. John Gruber's Markdown spec says the following about list items:
  3546. 1. "List markers typically start at the left margin, but may be indented
  3547. by up to three spaces. List markers must be followed by one or more
  3548. spaces or a tab."
  3549. 2. "To make lists look nice, you can wrap items with hanging indents....
  3550. But if you don't want to, you don't have to."
  3551. 3. "List items may consist of multiple paragraphs. Each subsequent
  3552. paragraph in a list item must be indented by either 4 spaces or one
  3553. tab."
  3554. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3555. but here again, Markdown will allow you to be lazy."
  3556. 5. "To put a blockquote within a list item, the blockquote's `>`
  3557. delimiters need to be indented."
  3558. 6. "To put a code block within a list item, the code block needs to be
  3559. indented twice — 8 spaces or two tabs."
  3560. These rules specify that a paragraph under a list item must be indented
  3561. four spaces (presumably, from the left margin, rather than the start of
  3562. the list marker, but this is not said), and that code under a list item
  3563. must be indented eight spaces instead of the usual four. They also say
  3564. that a block quote must be indented, but not by how much; however, the
  3565. example given has four spaces indentation. Although nothing is said
  3566. about other kinds of block-level content, it is certainly reasonable to
  3567. infer that *all* block elements under a list item, including other
  3568. lists, must be indented four spaces. This principle has been called the
  3569. *four-space rule*.
  3570. The four-space rule is clear and principled, and if the reference
  3571. implementation `Markdown.pl` had followed it, it probably would have
  3572. become the standard. However, `Markdown.pl` allowed paragraphs and
  3573. sublists to start with only two spaces indentation, at least on the
  3574. outer level. Worse, its behavior was inconsistent: a sublist of an
  3575. outer-level list needed two spaces indentation, but a sublist of this
  3576. sublist needed three spaces. It is not surprising, then, that different
  3577. implementations of Markdown have developed very different rules for
  3578. determining what comes under a list item. (Pandoc and python-Markdown,
  3579. for example, stuck with Gruber's syntax description and the four-space
  3580. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3581. followed `Markdown.pl`'s behavior more closely.)
  3582. Unfortunately, given the divergences between implementations, there
  3583. is no way to give a spec for list items that will be guaranteed not
  3584. to break any existing documents. However, the spec given here should
  3585. correctly handle lists formatted with either the four-space rule or
  3586. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3587. in a way that is natural for a human to read.
  3588. The strategy here is to let the width and indentation of the list marker
  3589. determine the indentation necessary for blocks to fall under the list
  3590. item, rather than having a fixed and arbitrary number. The writer can
  3591. think of the body of the list item as a unit which gets indented to the
  3592. right enough to fit the list marker (and any indentation on the list
  3593. marker). (The laziness rule, #5, then allows continuation lines to be
  3594. unindented if needed.)
  3595. This rule is superior, we claim, to any rule requiring a fixed level of
  3596. indentation from the margin. The four-space rule is clear but
  3597. unnatural. It is quite unintuitive that
  3598. ``` markdown
  3599. - foo
  3600. bar
  3601. - baz
  3602. ```
  3603. should be parsed as two lists with an intervening paragraph,
  3604. ``` html
  3605. <ul>
  3606. <li>foo</li>
  3607. </ul>
  3608. <p>bar</p>
  3609. <ul>
  3610. <li>baz</li>
  3611. </ul>
  3612. ```
  3613. as the four-space rule demands, rather than a single list,
  3614. ``` html
  3615. <ul>
  3616. <li>
  3617. <p>foo</p>
  3618. <p>bar</p>
  3619. <ul>
  3620. <li>baz</li>
  3621. </ul>
  3622. </li>
  3623. </ul>
  3624. ```
  3625. The choice of four spaces is arbitrary. It can be learned, but it is
  3626. not likely to be guessed, and it trips up beginners regularly.
  3627. Would it help to adopt a two-space rule? The problem is that such
  3628. a rule, together with the rule allowing 1--3 spaces indentation of the
  3629. initial list marker, allows text that is indented *less than* the
  3630. original list marker to be included in the list item. For example,
  3631. `Markdown.pl` parses
  3632. ``` markdown
  3633. - one
  3634. two
  3635. ```
  3636. as a single list item, with `two` a continuation paragraph:
  3637. ``` html
  3638. <ul>
  3639. <li>
  3640. <p>one</p>
  3641. <p>two</p>
  3642. </li>
  3643. </ul>
  3644. ```
  3645. and similarly
  3646. ``` markdown
  3647. > - one
  3648. >
  3649. > two
  3650. ```
  3651. as
  3652. ``` html
  3653. <blockquote>
  3654. <ul>
  3655. <li>
  3656. <p>one</p>
  3657. <p>two</p>
  3658. </li>
  3659. </ul>
  3660. </blockquote>
  3661. ```
  3662. This is extremely unintuitive.
  3663. Rather than requiring a fixed indent from the margin, we could require
  3664. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3665. may itself be indented). This proposal would remove the last anomaly
  3666. discussed. Unlike the spec presented above, it would count the following
  3667. as a list item with a subparagraph, even though the paragraph `bar`
  3668. is not indented as far as the first paragraph `foo`:
  3669. ``` markdown
  3670. 10. foo
  3671. bar
  3672. ```
  3673. Arguably this text does read like a list item with `bar` as a subparagraph,
  3674. which may count in favor of the proposal. However, on this proposal indented
  3675. code would have to be indented six spaces after the list marker. And this
  3676. would break a lot of existing Markdown, which has the pattern:
  3677. ``` markdown
  3678. 1. foo
  3679. indented code
  3680. ```
  3681. where the code is indented eight spaces. The spec above, by contrast, will
  3682. parse this text as expected, since the code block's indentation is measured
  3683. from the beginning of `foo`.
  3684. The one case that needs special treatment is a list item that *starts*
  3685. with indented code. How much indentation is required in that case, since
  3686. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3687. that in such cases, we require one space indentation from the list marker
  3688. (and then the normal four spaces for the indented code). This will match the
  3689. four-space rule in cases where the list marker plus its initial indentation
  3690. takes four spaces (a common case), but diverge in other cases.
  3691. ## Lists
  3692. A [list](@) is a sequence of one or more
  3693. list items [of the same type]. The list items
  3694. may be separated by any number of blank lines.
  3695. Two list items are [of the same type](@)
  3696. if they begin with a [list marker] of the same type.
  3697. Two list markers are of the
  3698. same type if (a) they are bullet list markers using the same character
  3699. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3700. delimiter (either `.` or `)`).
  3701. A list is an [ordered list](@)
  3702. if its constituent list items begin with
  3703. [ordered list markers], and a
  3704. [bullet list](@) if its constituent list
  3705. items begin with [bullet list markers].
  3706. The [start number](@)
  3707. of an [ordered list] is determined by the list number of
  3708. its initial list item. The numbers of subsequent list items are
  3709. disregarded.
  3710. A list is [loose](@) if any of its constituent
  3711. list items are separated by blank lines, or if any of its constituent
  3712. list items directly contain two block-level elements with a blank line
  3713. between them. Otherwise a list is [tight](@).
  3714. (The difference in HTML output is that paragraphs in a loose list are
  3715. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3716. Changing the bullet or ordered list delimiter starts a new list:
  3717. ```````````````````````````````` example
  3718. - foo
  3719. - bar
  3720. + baz
  3721. .
  3722. <ul>
  3723. <li>foo</li>
  3724. <li>bar</li>
  3725. </ul>
  3726. <ul>
  3727. <li>baz</li>
  3728. </ul>
  3729. ````````````````````````````````
  3730. ```````````````````````````````` example
  3731. 1. foo
  3732. 2. bar
  3733. 3) baz
  3734. .
  3735. <ol>
  3736. <li>foo</li>
  3737. <li>bar</li>
  3738. </ol>
  3739. <ol start="3">
  3740. <li>baz</li>
  3741. </ol>
  3742. ````````````````````````````````
  3743. In CommonMark, a list can interrupt a paragraph. That is,
  3744. no blank line is needed to separate a paragraph from a following
  3745. list:
  3746. ```````````````````````````````` example
  3747. Foo
  3748. - bar
  3749. - baz
  3750. .
  3751. <p>Foo</p>
  3752. <ul>
  3753. <li>bar</li>
  3754. <li>baz</li>
  3755. </ul>
  3756. ````````````````````````````````
  3757. `Markdown.pl` does not allow this, through fear of triggering a list
  3758. via a numeral in a hard-wrapped line:
  3759. ``` markdown
  3760. The number of windows in my house is
  3761. 14. The number of doors is 6.
  3762. ```
  3763. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3764. interrupt a paragraph, even though the same considerations might
  3765. apply.
  3766. In CommonMark, we do allow lists to interrupt paragraphs, for
  3767. two reasons. First, it is natural and not uncommon for people
  3768. to start lists without blank lines:
  3769. ``` markdown
  3770. I need to buy
  3771. - new shoes
  3772. - a coat
  3773. - a plane ticket
  3774. ```
  3775. Second, we are attracted to a
  3776. > [principle of uniformity](@):
  3777. > if a chunk of text has a certain
  3778. > meaning, it will continue to have the same meaning when put into a
  3779. > container block (such as a list item or blockquote).
  3780. (Indeed, the spec for [list items] and [block quotes] presupposes
  3781. this principle.) This principle implies that if
  3782. ``` markdown
  3783. * I need to buy
  3784. - new shoes
  3785. - a coat
  3786. - a plane ticket
  3787. ```
  3788. is a list item containing a paragraph followed by a nested sublist,
  3789. as all Markdown implementations agree it is (though the paragraph
  3790. may be rendered without `<p>` tags, since the list is "tight"),
  3791. then
  3792. ``` markdown
  3793. I need to buy
  3794. - new shoes
  3795. - a coat
  3796. - a plane ticket
  3797. ```
  3798. by itself should be a paragraph followed by a nested sublist.
  3799. Since it is well established Markdown practice to allow lists to
  3800. interrupt paragraphs inside list items, the [principle of
  3801. uniformity] requires us to allow this outside list items as
  3802. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3803. takes a different approach, requiring blank lines before lists
  3804. even inside other list items.)
  3805. In order to solve of unwanted lists in paragraphs with
  3806. hard-wrapped numerals, we allow only lists starting with `1` to
  3807. interrupt paragraphs. Thus,
  3808. ```````````````````````````````` example
  3809. The number of windows in my house is
  3810. 14. The number of doors is 6.
  3811. .
  3812. <p>The number of windows in my house is
  3813. 14. The number of doors is 6.</p>
  3814. ````````````````````````````````
  3815. We may still get an unintended result in cases like
  3816. ```````````````````````````````` example
  3817. The number of windows in my house is
  3818. 1. The number of doors is 6.
  3819. .
  3820. <p>The number of windows in my house is</p>
  3821. <ol>
  3822. <li>The number of doors is 6.</li>
  3823. </ol>
  3824. ````````````````````````````````
  3825. but this rule should prevent most spurious list captures.
  3826. There can be any number of blank lines between items:
  3827. ```````````````````````````````` example
  3828. - foo
  3829. - bar
  3830. - baz
  3831. .
  3832. <ul>
  3833. <li>
  3834. <p>foo</p>
  3835. </li>
  3836. <li>
  3837. <p>bar</p>
  3838. </li>
  3839. <li>
  3840. <p>baz</p>
  3841. </li>
  3842. </ul>
  3843. ````````````````````````````````
  3844. ```````````````````````````````` example
  3845. - foo
  3846. - bar
  3847. - baz
  3848. bim
  3849. .
  3850. <ul>
  3851. <li>foo
  3852. <ul>
  3853. <li>bar
  3854. <ul>
  3855. <li>
  3856. <p>baz</p>
  3857. <p>bim</p>
  3858. </li>
  3859. </ul>
  3860. </li>
  3861. </ul>
  3862. </li>
  3863. </ul>
  3864. ````````````````````````````````
  3865. To separate consecutive lists of the same type, or to separate a
  3866. list from an indented code block that would otherwise be parsed
  3867. as a subparagraph of the final list item, you can insert a blank HTML
  3868. comment:
  3869. ```````````````````````````````` example
  3870. - foo
  3871. - bar
  3872. <!-- -->
  3873. - baz
  3874. - bim
  3875. .
  3876. <ul>
  3877. <li>foo</li>
  3878. <li>bar</li>
  3879. </ul>
  3880. <!-- -->
  3881. <ul>
  3882. <li>baz</li>
  3883. <li>bim</li>
  3884. </ul>
  3885. ````````````````````````````````
  3886. ```````````````````````````````` example
  3887. - foo
  3888. notcode
  3889. - foo
  3890. <!-- -->
  3891. code
  3892. .
  3893. <ul>
  3894. <li>
  3895. <p>foo</p>
  3896. <p>notcode</p>
  3897. </li>
  3898. <li>
  3899. <p>foo</p>
  3900. </li>
  3901. </ul>
  3902. <!-- -->
  3903. <pre><code>code
  3904. </code></pre>
  3905. ````````````````````````````````
  3906. List items need not be indented to the same level. The following
  3907. list items will be treated as items at the same list level,
  3908. since none is indented enough to belong to the previous list
  3909. item:
  3910. ```````````````````````````````` example
  3911. - a
  3912. - b
  3913. - c
  3914. - d
  3915. - e
  3916. - f
  3917. - g
  3918. .
  3919. <ul>
  3920. <li>a</li>
  3921. <li>b</li>
  3922. <li>c</li>
  3923. <li>d</li>
  3924. <li>e</li>
  3925. <li>f</li>
  3926. <li>g</li>
  3927. </ul>
  3928. ````````````````````````````````
  3929. ```````````````````````````````` example
  3930. 1. a
  3931. 2. b
  3932. 3. c
  3933. .
  3934. <ol>
  3935. <li>
  3936. <p>a</p>
  3937. </li>
  3938. <li>
  3939. <p>b</p>
  3940. </li>
  3941. <li>
  3942. <p>c</p>
  3943. </li>
  3944. </ol>
  3945. ````````````````````````````````
  3946. Note, however, that list items may not be indented more than
  3947. three spaces. Here `- e` is treated as a paragraph continuation
  3948. line, because it is indented more than three spaces:
  3949. ```````````````````````````````` example
  3950. - a
  3951. - b
  3952. - c
  3953. - d
  3954. - e
  3955. .
  3956. <ul>
  3957. <li>a</li>
  3958. <li>b</li>
  3959. <li>c</li>
  3960. <li>d
  3961. - e</li>
  3962. </ul>
  3963. ````````````````````````````````
  3964. And here, `3. c` is treated as in indented code block,
  3965. because it is indented four spaces and preceded by a
  3966. blank line.
  3967. ```````````````````````````````` example
  3968. 1. a
  3969. 2. b
  3970. 3. c
  3971. .
  3972. <ol>
  3973. <li>
  3974. <p>a</p>
  3975. </li>
  3976. <li>
  3977. <p>b</p>
  3978. </li>
  3979. </ol>
  3980. <pre><code>3. c
  3981. </code></pre>
  3982. ````````````````````````````````
  3983. This is a loose list, because there is a blank line between
  3984. two of the list items:
  3985. ```````````````````````````````` example
  3986. - a
  3987. - b
  3988. - c
  3989. .
  3990. <ul>
  3991. <li>
  3992. <p>a</p>
  3993. </li>
  3994. <li>
  3995. <p>b</p>
  3996. </li>
  3997. <li>
  3998. <p>c</p>
  3999. </li>
  4000. </ul>
  4001. ````````````````````````````````
  4002. So is this, with a empty second item:
  4003. ```````````````````````````````` example
  4004. * a
  4005. *
  4006. * c
  4007. .
  4008. <ul>
  4009. <li>
  4010. <p>a</p>
  4011. </li>
  4012. <li></li>
  4013. <li>
  4014. <p>c</p>
  4015. </li>
  4016. </ul>
  4017. ````````````````````````````````
  4018. These are loose lists, even though there is no space between the items,
  4019. because one of the items directly contains two block-level elements
  4020. with a blank line between them:
  4021. ```````````````````````````````` example
  4022. - a
  4023. - b
  4024. c
  4025. - d
  4026. .
  4027. <ul>
  4028. <li>
  4029. <p>a</p>
  4030. </li>
  4031. <li>
  4032. <p>b</p>
  4033. <p>c</p>
  4034. </li>
  4035. <li>
  4036. <p>d</p>
  4037. </li>
  4038. </ul>
  4039. ````````````````````````````````
  4040. ```````````````````````````````` example
  4041. - a
  4042. - b
  4043. [ref]: /url
  4044. - d
  4045. .
  4046. <ul>
  4047. <li>
  4048. <p>a</p>
  4049. </li>
  4050. <li>
  4051. <p>b</p>
  4052. </li>
  4053. <li>
  4054. <p>d</p>
  4055. </li>
  4056. </ul>
  4057. ````````````````````````````````
  4058. This is a tight list, because the blank lines are in a code block:
  4059. ```````````````````````````````` example
  4060. - a
  4061. - ```
  4062. b
  4063. ```
  4064. - c
  4065. .
  4066. <ul>
  4067. <li>a</li>
  4068. <li>
  4069. <pre><code>b
  4070. </code></pre>
  4071. </li>
  4072. <li>c</li>
  4073. </ul>
  4074. ````````````````````````````````
  4075. This is a tight list, because the blank line is between two
  4076. paragraphs of a sublist. So the sublist is loose while
  4077. the outer list is tight:
  4078. ```````````````````````````````` example
  4079. - a
  4080. - b
  4081. c
  4082. - d
  4083. .
  4084. <ul>
  4085. <li>a
  4086. <ul>
  4087. <li>
  4088. <p>b</p>
  4089. <p>c</p>
  4090. </li>
  4091. </ul>
  4092. </li>
  4093. <li>d</li>
  4094. </ul>
  4095. ````````````````````````````````
  4096. This is a tight list, because the blank line is inside the
  4097. block quote:
  4098. ```````````````````````````````` example
  4099. * a
  4100. > b
  4101. >
  4102. * c
  4103. .
  4104. <ul>
  4105. <li>a
  4106. <blockquote>
  4107. <p>b</p>
  4108. </blockquote>
  4109. </li>
  4110. <li>c</li>
  4111. </ul>
  4112. ````````````````````````````````
  4113. This list is tight, because the consecutive block elements
  4114. are not separated by blank lines:
  4115. ```````````````````````````````` example
  4116. - a
  4117. > b
  4118. ```
  4119. c
  4120. ```
  4121. - d
  4122. .
  4123. <ul>
  4124. <li>a
  4125. <blockquote>
  4126. <p>b</p>
  4127. </blockquote>
  4128. <pre><code>c
  4129. </code></pre>
  4130. </li>
  4131. <li>d</li>
  4132. </ul>
  4133. ````````````````````````````````
  4134. A single-paragraph list is tight:
  4135. ```````````````````````````````` example
  4136. - a
  4137. .
  4138. <ul>
  4139. <li>a</li>
  4140. </ul>
  4141. ````````````````````````````````
  4142. ```````````````````````````````` example
  4143. - a
  4144. - b
  4145. .
  4146. <ul>
  4147. <li>a
  4148. <ul>
  4149. <li>b</li>
  4150. </ul>
  4151. </li>
  4152. </ul>
  4153. ````````````````````````````````
  4154. This list is loose, because of the blank line between the
  4155. two block elements in the list item:
  4156. ```````````````````````````````` example
  4157. 1. ```
  4158. foo
  4159. ```
  4160. bar
  4161. .
  4162. <ol>
  4163. <li>
  4164. <pre><code>foo
  4165. </code></pre>
  4166. <p>bar</p>
  4167. </li>
  4168. </ol>
  4169. ````````````````````````````````
  4170. Here the outer list is loose, the inner list tight:
  4171. ```````````````````````````````` example
  4172. * foo
  4173. * bar
  4174. baz
  4175. .
  4176. <ul>
  4177. <li>
  4178. <p>foo</p>
  4179. <ul>
  4180. <li>bar</li>
  4181. </ul>
  4182. <p>baz</p>
  4183. </li>
  4184. </ul>
  4185. ````````````````````````````````
  4186. ```````````````````````````````` example
  4187. - a
  4188. - b
  4189. - c
  4190. - d
  4191. - e
  4192. - f
  4193. .
  4194. <ul>
  4195. <li>
  4196. <p>a</p>
  4197. <ul>
  4198. <li>b</li>
  4199. <li>c</li>
  4200. </ul>
  4201. </li>
  4202. <li>
  4203. <p>d</p>
  4204. <ul>
  4205. <li>e</li>
  4206. <li>f</li>
  4207. </ul>
  4208. </li>
  4209. </ul>
  4210. ````````````````````````````````
  4211. # Inlines
  4212. Inlines are parsed sequentially from the beginning of the character
  4213. stream to the end (left to right, in left-to-right languages).
  4214. Thus, for example, in
  4215. ```````````````````````````````` example
  4216. `hi`lo`
  4217. .
  4218. <p><code>hi</code>lo`</p>
  4219. ````````````````````````````````
  4220. `hi` is parsed as code, leaving the backtick at the end as a literal
  4221. backtick.
  4222. ## Backslash escapes
  4223. Any ASCII punctuation character may be backslash-escaped:
  4224. ```````````````````````````````` example
  4225. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4226. .
  4227. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4228. ````````````````````````````````
  4229. Backslashes before other characters are treated as literal
  4230. backslashes:
  4231. ```````````````````````````````` example
  4232. \→\A\a\ \3\φ\«
  4233. .
  4234. <p>\→\A\a\ \3\φ\«</p>
  4235. ````````````````````````````````
  4236. Escaped characters are treated as regular characters and do
  4237. not have their usual Markdown meanings:
  4238. ```````````````````````````````` example
  4239. \*not emphasized*
  4240. \<br/> 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 "not a reference"
  4247. .
  4248. <p>*not emphasized*
  4249. &lt;br/&gt; not a tag
  4250. [not a link](/foo)
  4251. `not code`
  4252. 1. not a list
  4253. * not a list
  4254. # not a heading
  4255. [foo]: /url &quot;not a reference&quot;</p>
  4256. ````````````````````````````````
  4257. If a backslash is itself escaped, the following character is not:
  4258. ```````````````````````````````` example
  4259. \\*emphasis*
  4260. .
  4261. <p>\<em>emphasis</em></p>
  4262. ````````````````````````````````
  4263. A backslash at the end of the line is a [hard line break]:
  4264. ```````````````````````````````` example
  4265. foo\
  4266. bar
  4267. .
  4268. <p>foo<br />
  4269. bar</p>
  4270. ````````````````````````````````
  4271. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4272. raw HTML:
  4273. ```````````````````````````````` example
  4274. `` \[\` ``
  4275. .
  4276. <p><code>\[\`</code></p>
  4277. ````````````````````````````````
  4278. ```````````````````````````````` example
  4279. \[\]
  4280. .
  4281. <pre><code>\[\]
  4282. </code></pre>
  4283. ````````````````````````````````
  4284. ```````````````````````````````` example
  4285. ~~~
  4286. \[\]
  4287. ~~~
  4288. .
  4289. <pre><code>\[\]
  4290. </code></pre>
  4291. ````````````````````````````````
  4292. ```````````````````````````````` example
  4293. <http://example.com?find=\*>
  4294. .
  4295. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4296. ````````````````````````````````
  4297. ```````````````````````````````` example
  4298. <a href="/bar\/)">
  4299. .
  4300. <a href="/bar\/)">
  4301. ````````````````````````````````
  4302. But they work in all other contexts, including URLs and link titles,
  4303. link references, and [info strings] in [fenced code blocks]:
  4304. ```````````````````````````````` example
  4305. [foo](/bar\* "ti\*tle")
  4306. .
  4307. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4308. ````````````````````````````````
  4309. ```````````````````````````````` example
  4310. [foo]
  4311. [foo]: /bar\* "ti\*tle"
  4312. .
  4313. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4314. ````````````````````````````````
  4315. ```````````````````````````````` example
  4316. ``` foo\+bar
  4317. foo
  4318. ```
  4319. .
  4320. <pre><code class="language-foo+bar">foo
  4321. </code></pre>
  4322. ````````````````````````````````
  4323. ## Entity and numeric character references
  4324. All valid HTML entity references and numeric character
  4325. references, except those occurring in code blocks and code spans,
  4326. are recognized as such and treated as equivalent to the
  4327. corresponding Unicode characters. Conforming CommonMark parsers
  4328. need not store information about whether a particular character
  4329. was represented in the source using a Unicode character or
  4330. an entity reference.
  4331. [Entity references](@) consist of `&` + any of the valid
  4332. HTML5 entity names + `;`. The
  4333. document <https://html.spec.whatwg.org/multipage/entities.json>
  4334. is used as an authoritative source for the valid entity
  4335. references and their corresponding code points.
  4336. ```````````````````````````````` example
  4337. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4338. &frac34; &HilbertSpace; &DifferentialD;
  4339. &ClockwiseContourIntegral; &ngE;
  4340. .
  4341. <p>  &amp; © Æ Ď
  4342. ¾ ℋ ⅆ
  4343. ∲ ≧̸</p>
  4344. ````````````````````````````````
  4345. [Decimal numeric character
  4346. references](@)
  4347. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  4348. numeric character reference is parsed as the corresponding
  4349. Unicode character. Invalid Unicode code points will be replaced by
  4350. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4351. the code point `U+0000` will also be replaced by `U+FFFD`.
  4352. ```````````````````````````````` example
  4353. &#35; &#1234; &#992; &#0;
  4354. .
  4355. <p># Ӓ Ϡ �</p>
  4356. ````````````````````````````````
  4357. [Hexadecimal numeric character
  4358. references](@) consist of `&#` +
  4359. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  4360. They too are parsed as the corresponding Unicode character (this
  4361. time specified with a hexadecimal numeral instead of decimal).
  4362. ```````````````````````````````` example
  4363. &#X22; &#XD06; &#xcab;
  4364. .
  4365. <p>&quot; ആ ಫ</p>
  4366. ````````````````````````````````
  4367. Here are some nonentities:
  4368. ```````````````````````````````` example
  4369. &nbsp &x; &#; &#x;
  4370. &#987654321;
  4371. &#abcdef0;
  4372. &ThisIsNotDefined; &hi?;
  4373. .
  4374. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4375. &amp;#987654321;
  4376. &amp;#abcdef0;
  4377. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4378. ````````````````````````````````
  4379. Although HTML5 does accept some entity references
  4380. without a trailing semicolon (such as `&copy`), these are not
  4381. recognized here, because it makes the grammar too ambiguous:
  4382. ```````````````````````````````` example
  4383. &copy
  4384. .
  4385. <p>&amp;copy</p>
  4386. ````````````````````````````````
  4387. Strings that are not on the list of HTML5 named entities are not
  4388. recognized as entity references either:
  4389. ```````````````````````````````` example
  4390. &MadeUpEntity;
  4391. .
  4392. <p>&amp;MadeUpEntity;</p>
  4393. ````````````````````````````````
  4394. Entity and numeric character references are recognized in any
  4395. context besides code spans or code blocks, including
  4396. URLs, [link titles], and [fenced code block][] [info strings]:
  4397. ```````````````````````````````` example
  4398. <a href="&ouml;&ouml;.html">
  4399. .
  4400. <a href="&ouml;&ouml;.html">
  4401. ````````````````````````````````
  4402. ```````````````````````````````` example
  4403. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4404. .
  4405. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4406. ````````````````````````````````
  4407. ```````````````````````````````` example
  4408. [foo]
  4409. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4410. .
  4411. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4412. ````````````````````````````````
  4413. ```````````````````````````````` example
  4414. ``` f&ouml;&ouml;
  4415. foo
  4416. ```
  4417. .
  4418. <pre><code class="language-föö">foo
  4419. </code></pre>
  4420. ````````````````````````````````
  4421. Entity and numeric character references are treated as literal
  4422. text in code spans and code blocks:
  4423. ```````````````````````````````` example
  4424. `f&ouml;&ouml;`
  4425. .
  4426. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4427. ````````````````````````````````
  4428. ```````````````````````````````` example
  4429. f&ouml;f&ouml;
  4430. .
  4431. <pre><code>f&amp;ouml;f&amp;ouml;
  4432. </code></pre>
  4433. ````````````````````````````````
  4434. ## Code spans
  4435. A [backtick string](@)
  4436. is a string of one or more backtick characters (`` ` ``) that is neither
  4437. preceded nor followed by a backtick.
  4438. A [code span](@) begins with a backtick string and ends with
  4439. a backtick string of equal length. The contents of the code span are
  4440. the characters between the two backtick strings, normalized in the
  4441. following ways:
  4442. - First, [line endings] are converted to [spaces].
  4443. - If the resulting string both begins *and* ends with a [space]
  4444. character, a single [space] character is removed from the
  4445. front and back. This allows you to include code that begins
  4446. or ends with backtick characters, which must be separated by
  4447. whitespace from the opening or closing backtick strings.
  4448. This is a simple code span:
  4449. ```````````````````````````````` example
  4450. `foo`
  4451. .
  4452. <p><code>foo</code></p>
  4453. ````````````````````````````````
  4454. Here two backticks are used, because the code contains a backtick.
  4455. This example also illustrates stripping of a single leading and
  4456. trailing space:
  4457. ```````````````````````````````` example
  4458. `` foo ` bar ``
  4459. .
  4460. <p><code>foo ` bar</code></p>
  4461. ````````````````````````````````
  4462. This example shows the motivation for stripping leading and trailing
  4463. spaces:
  4464. ```````````````````````````````` example
  4465. ` `` `
  4466. .
  4467. <p><code>``</code></p>
  4468. ````````````````````````````````
  4469. Note that only *one* space is stripped:
  4470. ```````````````````````````````` example
  4471. ` `` `
  4472. .
  4473. <p><code> `` </code></p>
  4474. ````````````````````````````````
  4475. The stripping only happens if the space is on both
  4476. sides of the string:
  4477. ```````````````````````````````` example
  4478. ` a`
  4479. .
  4480. <p><code> a</code></p>
  4481. ````````````````````````````````
  4482. Only [spaces], and not [unicode whitespace] in general, are
  4483. stripped in this way:
  4484. ```````````````````````````````` example
  4485. ` b `
  4486. .
  4487. <p><code> b </code></p>
  4488. ````````````````````````````````
  4489. [Line endings] are treated like spaces:
  4490. ```````````````````````````````` example
  4491. ``
  4492. foo
  4493. bar
  4494. baz
  4495. ``
  4496. .
  4497. <p><code>foo bar baz</code></p>
  4498. ````````````````````````````````
  4499. ```````````````````````````````` example
  4500. ``
  4501. foo
  4502. ``
  4503. .
  4504. <p><code>foo </code></p>
  4505. ````````````````````````````````
  4506. Interior spaces are not collapsed:
  4507. ```````````````````````````````` example
  4508. `foo bar
  4509. baz`
  4510. .
  4511. <p><code>foo bar baz</code></p>
  4512. ````````````````````````````````
  4513. Note that browsers will typically collapse consecutive spaces
  4514. when rendering `<code>` elements, so it is recommended that
  4515. the following CSS be used:
  4516. code{white-space: pre-wrap;}
  4517. Note that backslash escapes do not work in code spans. All backslashes
  4518. are treated literally:
  4519. ```````````````````````````````` example
  4520. `foo\`bar`
  4521. .
  4522. <p><code>foo\</code>bar`</p>
  4523. ````````````````````````````````
  4524. Backslash escapes are never needed, because one can always choose a
  4525. string of *n* backtick characters as delimiters, where the code does
  4526. not contain any strings of exactly *n* backtick characters.
  4527. ```````````````````````````````` example
  4528. ``foo`bar``
  4529. .
  4530. <p><code>foo`bar</code></p>
  4531. ````````````````````````````````
  4532. ```````````````````````````````` example
  4533. ` foo `` bar `
  4534. .
  4535. <p><code>foo `` bar</code></p>
  4536. ````````````````````````````````
  4537. Code span backticks have higher precedence than any other inline
  4538. constructs except HTML tags and autolinks. Thus, for example, this is
  4539. not parsed as emphasized text, since the second `*` is part of a code
  4540. span:
  4541. ```````````````````````````````` example
  4542. *foo`*`
  4543. .
  4544. <p>*foo<code>*</code></p>
  4545. ````````````````````````````````
  4546. And this is not parsed as a link:
  4547. ```````````````````````````````` example
  4548. [not a `link](/foo`)
  4549. .
  4550. <p>[not a <code>link](/foo</code>)</p>
  4551. ````````````````````````````````
  4552. Code spans, HTML tags, and autolinks have the same precedence.
  4553. Thus, this is code:
  4554. ```````````````````````````````` example
  4555. `<a href="`">`
  4556. .
  4557. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4558. ````````````````````````````````
  4559. But this is an HTML tag:
  4560. ```````````````````````````````` example
  4561. <a href="`">`
  4562. .
  4563. <p><a href="`">`</p>
  4564. ````````````````````````````````
  4565. And this is code:
  4566. ```````````````````````````````` example
  4567. `<http://foo.bar.`baz>`
  4568. .
  4569. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4570. ````````````````````````````````
  4571. But this is an autolink:
  4572. ```````````````````````````````` example
  4573. <http://foo.bar.`baz>`
  4574. .
  4575. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4576. ````````````````````````````````
  4577. When a backtick string is not closed by a matching backtick string,
  4578. we just have literal backticks:
  4579. ```````````````````````````````` example
  4580. ```foo``
  4581. .
  4582. <p>```foo``</p>
  4583. ````````````````````````````````
  4584. ```````````````````````````````` example
  4585. `foo
  4586. .
  4587. <p>`foo</p>
  4588. ````````````````````````````````
  4589. The following case also illustrates the need for opening and
  4590. closing backtick strings to be equal in length:
  4591. ```````````````````````````````` example
  4592. `foo``bar``
  4593. .
  4594. <p>`foo<code>bar</code></p>
  4595. ````````````````````````````````
  4596. ## Emphasis and strong emphasis
  4597. John Gruber's original [Markdown syntax
  4598. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4599. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4600. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4601. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4602. > tag.
  4603. This is enough for most users, but these rules leave much undecided,
  4604. especially when it comes to nested emphasis. The original
  4605. `Markdown.pl` test suite makes it clear that triple `***` and
  4606. `___` delimiters can be used for strong emphasis, and most
  4607. implementations have also allowed the following patterns:
  4608. ``` markdown
  4609. ***strong emph***
  4610. ***strong** in emph*
  4611. ***emph* in strong**
  4612. **in strong *emph***
  4613. *in emph **strong***
  4614. ```
  4615. The following patterns are less widely supported, but the intent
  4616. is clear and they are useful (especially in contexts like bibliography
  4617. entries):
  4618. ``` markdown
  4619. *emph *with emph* in it*
  4620. **strong **with strong** in it**
  4621. ```
  4622. Many implementations have also restricted intraword emphasis to
  4623. the `*` forms, to avoid unwanted emphasis in words containing
  4624. internal underscores. (It is best practice to put these in code
  4625. spans, but users often do not.)
  4626. ``` markdown
  4627. internal emphasis: foo*bar*baz
  4628. no emphasis: foo_bar_baz
  4629. ```
  4630. The rules given below capture all of these patterns, while allowing
  4631. for efficient parsing strategies that do not backtrack.
  4632. First, some definitions. A [delimiter run](@) is either
  4633. a sequence of one or more `*` characters that is not preceded or
  4634. followed by a non-backslash-escaped `*` character, or a sequence
  4635. of one or more `_` characters that is not preceded or followed by
  4636. a non-backslash-escaped `_` character.
  4637. A [left-flanking delimiter run](@) is
  4638. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4639. and either (2a) not followed by a [punctuation character], or
  4640. (2b) followed by a [punctuation character] and
  4641. preceded by [Unicode whitespace] or a [punctuation character].
  4642. For purposes of this definition, the beginning and the end of
  4643. the line count as Unicode whitespace.
  4644. A [right-flanking delimiter run](@) is
  4645. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4646. and either (2a) not preceded by a [punctuation character], or
  4647. (2b) preceded by a [punctuation character] and
  4648. followed by [Unicode whitespace] or a [punctuation character].
  4649. For purposes of this definition, the beginning and the end of
  4650. the line count as Unicode whitespace.
  4651. Here are some examples of delimiter runs.
  4652. - left-flanking but not right-flanking:
  4653. ```
  4654. ***abc
  4655. _abc
  4656. **"abc"
  4657. _"abc"
  4658. ```
  4659. - right-flanking but not left-flanking:
  4660. ```
  4661. abc***
  4662. abc_
  4663. "abc"**
  4664. "abc"_
  4665. ```
  4666. - Both left and right-flanking:
  4667. ```
  4668. abc***def
  4669. "abc"_"def"
  4670. ```
  4671. - Neither left nor right-flanking:
  4672. ```
  4673. abc *** def
  4674. a _ b
  4675. ```
  4676. (The idea of distinguishing left-flanking and right-flanking
  4677. delimiter runs based on the character before and the character
  4678. after comes from Roopesh Chander's
  4679. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4680. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4681. run," and its rules for distinguishing left- and right-flanking runs
  4682. are a bit more complex than the ones given here.)
  4683. The following rules define emphasis and strong emphasis:
  4684. 1. A single `*` character [can open emphasis](@)
  4685. iff (if and only if) it is part of a [left-flanking delimiter run].
  4686. 2. A single `_` character [can open emphasis] iff
  4687. it is part of a [left-flanking delimiter run]
  4688. and either (a) not part of a [right-flanking delimiter run]
  4689. or (b) part of a [right-flanking delimiter run]
  4690. preceded by punctuation.
  4691. 3. A single `*` character [can close emphasis](@)
  4692. iff it is part of a [right-flanking delimiter run].
  4693. 4. A single `_` character [can close emphasis] iff
  4694. it is part of a [right-flanking delimiter run]
  4695. and either (a) not part of a [left-flanking delimiter run]
  4696. or (b) part of a [left-flanking delimiter run]
  4697. followed by punctuation.
  4698. 5. A double `**` [can open strong emphasis](@)
  4699. iff it is part of a [left-flanking delimiter run].
  4700. 6. A double `__` [can open strong emphasis] iff
  4701. it is part of a [left-flanking delimiter run]
  4702. and either (a) not part of a [right-flanking delimiter run]
  4703. or (b) part of a [right-flanking delimiter run]
  4704. preceded by punctuation.
  4705. 7. A double `**` [can close strong emphasis](@)
  4706. iff it is part of a [right-flanking delimiter run].
  4707. 8. A double `__` [can close strong emphasis] iff
  4708. it is part of a [right-flanking delimiter run]
  4709. and either (a) not part of a [left-flanking delimiter run]
  4710. or (b) part of a [left-flanking delimiter run]
  4711. followed by punctuation.
  4712. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4713. with a delimiter that [can close emphasis], and that uses the same
  4714. character (`_` or `*`) as the opening delimiter. The
  4715. opening and closing delimiters must belong to separate
  4716. [delimiter runs]. If one of the delimiters can both
  4717. open and close emphasis, then the sum of the lengths of the
  4718. delimiter runs containing the opening and closing delimiters
  4719. must not be a multiple of 3.
  4720. 10. Strong emphasis begins with a delimiter that
  4721. [can open strong emphasis] and ends with a delimiter that
  4722. [can close strong emphasis], and that uses the same character
  4723. (`_` or `*`) as the opening delimiter. The
  4724. opening and closing delimiters must belong to separate
  4725. [delimiter runs]. If one of the delimiters can both open
  4726. and close strong emphasis, then the sum of the lengths of
  4727. the delimiter runs containing the opening and closing
  4728. delimiters must not be a multiple of 3.
  4729. 11. A literal `*` character cannot occur at the beginning or end of
  4730. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4731. is backslash-escaped.
  4732. 12. A literal `_` character cannot occur at the beginning or end of
  4733. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4734. is backslash-escaped.
  4735. Where rules 1--12 above are compatible with multiple parsings,
  4736. the following principles resolve ambiguity:
  4737. 13. The number of nestings should be minimized. Thus, for example,
  4738. an interpretation `<strong>...</strong>` is always preferred to
  4739. `<em><em>...</em></em>`.
  4740. 14. An interpretation `<em><strong>...</strong></em>` is always
  4741. preferred to `<strong><em>...</em></strong>`.
  4742. 15. When two potential emphasis or strong emphasis spans overlap,
  4743. so that the second begins before the first ends and ends after
  4744. the first ends, the first takes precedence. Thus, for example,
  4745. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4746. than `*foo <em>bar* baz</em>`.
  4747. 16. When there are two potential emphasis or strong emphasis spans
  4748. with the same closing delimiter, the shorter one (the one that
  4749. opens later) takes precedence. Thus, for example,
  4750. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4751. rather than `<strong>foo **bar baz</strong>`.
  4752. 17. Inline code spans, links, images, and HTML tags group more tightly
  4753. than emphasis. So, when there is a choice between an interpretation
  4754. that contains one of these elements and one that does not, the
  4755. former always wins. Thus, for example, `*[foo*](bar)` is
  4756. parsed as `*<a href="bar">foo*</a>` rather than as
  4757. `<em>[foo</em>](bar)`.
  4758. These rules can be illustrated through a series of examples.
  4759. Rule 1:
  4760. ```````````````````````````````` example
  4761. *foo bar*
  4762. .
  4763. <p><em>foo bar</em></p>
  4764. ````````````````````````````````
  4765. This is not emphasis, because the opening `*` is followed by
  4766. whitespace, and hence not part of a [left-flanking delimiter run]:
  4767. ```````````````````````````````` example
  4768. a * foo bar*
  4769. .
  4770. <p>a * foo bar*</p>
  4771. ````````````````````````````````
  4772. This is not emphasis, because the opening `*` is preceded
  4773. by an alphanumeric and followed by punctuation, and hence
  4774. not part of a [left-flanking delimiter run]:
  4775. ```````````````````````````````` example
  4776. a*"foo"*
  4777. .
  4778. <p>a*&quot;foo&quot;*</p>
  4779. ````````````````````````````````
  4780. Unicode nonbreaking spaces count as whitespace, too:
  4781. ```````````````````````````````` example
  4782. * a *
  4783. .
  4784. <p>* a *</p>
  4785. ````````````````````````````````
  4786. Intraword emphasis with `*` is permitted:
  4787. ```````````````````````````````` example
  4788. foo*bar*
  4789. .
  4790. <p>foo<em>bar</em></p>
  4791. ````````````````````````````````
  4792. ```````````````````````````````` example
  4793. 5*6*78
  4794. .
  4795. <p>5<em>6</em>78</p>
  4796. ````````````````````````````````
  4797. Rule 2:
  4798. ```````````````````````````````` example
  4799. _foo bar_
  4800. .
  4801. <p><em>foo bar</em></p>
  4802. ````````````````````````````````
  4803. This is not emphasis, because the opening `_` is followed by
  4804. whitespace:
  4805. ```````````````````````````````` example
  4806. _ foo bar_
  4807. .
  4808. <p>_ foo bar_</p>
  4809. ````````````````````````````````
  4810. This is not emphasis, because the opening `_` is preceded
  4811. by an alphanumeric and followed by punctuation:
  4812. ```````````````````````````````` example
  4813. a_"foo"_
  4814. .
  4815. <p>a_&quot;foo&quot;_</p>
  4816. ````````````````````````````````
  4817. Emphasis with `_` is not allowed inside words:
  4818. ```````````````````````````````` example
  4819. foo_bar_
  4820. .
  4821. <p>foo_bar_</p>
  4822. ````````````````````````````````
  4823. ```````````````````````````````` example
  4824. 5_6_78
  4825. .
  4826. <p>5_6_78</p>
  4827. ````````````````````````````````
  4828. ```````````````````````````````` example
  4829. пристаням_стремятся_
  4830. .
  4831. <p>пристаням_стремятся_</p>
  4832. ````````````````````````````````
  4833. Here `_` does not generate emphasis, because the first delimiter run
  4834. is right-flanking and the second left-flanking:
  4835. ```````````````````````````````` example
  4836. aa_"bb"_cc
  4837. .
  4838. <p>aa_&quot;bb&quot;_cc</p>
  4839. ````````````````````````````````
  4840. This is emphasis, even though the opening delimiter is
  4841. both left- and right-flanking, because it is preceded by
  4842. punctuation:
  4843. ```````````````````````````````` example
  4844. foo-_(bar)_
  4845. .
  4846. <p>foo-<em>(bar)</em></p>
  4847. ````````````````````````````````
  4848. Rule 3:
  4849. This is not emphasis, because the closing delimiter does
  4850. not match the opening delimiter:
  4851. ```````````````````````````````` example
  4852. _foo*
  4853. .
  4854. <p>_foo*</p>
  4855. ````````````````````````````````
  4856. This is not emphasis, because the closing `*` is preceded by
  4857. whitespace:
  4858. ```````````````````````````````` example
  4859. *foo bar *
  4860. .
  4861. <p>*foo bar *</p>
  4862. ````````````````````````````````
  4863. A newline also counts as whitespace:
  4864. ```````````````````````````````` example
  4865. *foo bar
  4866. *
  4867. .
  4868. <p>*foo bar
  4869. *</p>
  4870. ````````````````````````````````
  4871. This is not emphasis, because the second `*` is
  4872. preceded by punctuation and followed by an alphanumeric
  4873. (hence it is not part of a [right-flanking delimiter run]:
  4874. ```````````````````````````````` example
  4875. *(*foo)
  4876. .
  4877. <p>*(*foo)</p>
  4878. ````````````````````````````````
  4879. The point of this restriction is more easily appreciated
  4880. with this example:
  4881. ```````````````````````````````` example
  4882. *(*foo*)*
  4883. .
  4884. <p><em>(<em>foo</em>)</em></p>
  4885. ````````````````````````````````
  4886. Intraword emphasis with `*` is allowed:
  4887. ```````````````````````````````` example
  4888. *foo*bar
  4889. .
  4890. <p><em>foo</em>bar</p>
  4891. ````````````````````````````````
  4892. Rule 4:
  4893. This is not emphasis, because the closing `_` is preceded by
  4894. whitespace:
  4895. ```````````````````````````````` example
  4896. _foo bar _
  4897. .
  4898. <p>_foo bar _</p>
  4899. ````````````````````````````````
  4900. This is not emphasis, because the second `_` is
  4901. preceded by punctuation and followed by an alphanumeric:
  4902. ```````````````````````````````` example
  4903. _(_foo)
  4904. .
  4905. <p>_(_foo)</p>
  4906. ````````````````````````````````
  4907. This is emphasis within emphasis:
  4908. ```````````````````````````````` example
  4909. _(_foo_)_
  4910. .
  4911. <p><em>(<em>foo</em>)</em></p>
  4912. ````````````````````````````````
  4913. Intraword emphasis is disallowed for `_`:
  4914. ```````````````````````````````` example
  4915. _foo_bar
  4916. .
  4917. <p>_foo_bar</p>
  4918. ````````````````````````````````
  4919. ```````````````````````````````` example
  4920. _пристаням_стремятся
  4921. .
  4922. <p>_пристаням_стремятся</p>
  4923. ````````````````````````````````
  4924. ```````````````````````````````` example
  4925. _foo_bar_baz_
  4926. .
  4927. <p><em>foo_bar_baz</em></p>
  4928. ````````````````````````````````
  4929. This is emphasis, even though the closing delimiter is
  4930. both left- and right-flanking, because it is followed by
  4931. punctuation:
  4932. ```````````````````````````````` example
  4933. _(bar)_.
  4934. .
  4935. <p><em>(bar)</em>.</p>
  4936. ````````````````````````````````
  4937. Rule 5:
  4938. ```````````````````````````````` example
  4939. **foo bar**
  4940. .
  4941. <p><strong>foo bar</strong></p>
  4942. ````````````````````````````````
  4943. This is not strong emphasis, because the opening delimiter is
  4944. followed by whitespace:
  4945. ```````````````````````````````` example
  4946. ** foo bar**
  4947. .
  4948. <p>** foo bar**</p>
  4949. ````````````````````````````````
  4950. This is not strong emphasis, because the opening `**` is preceded
  4951. by an alphanumeric and followed by punctuation, and hence
  4952. not part of a [left-flanking delimiter run]:
  4953. ```````````````````````````````` example
  4954. a**"foo"**
  4955. .
  4956. <p>a**&quot;foo&quot;**</p>
  4957. ````````````````````````````````
  4958. Intraword strong emphasis with `**` is permitted:
  4959. ```````````````````````````````` example
  4960. foo**bar**
  4961. .
  4962. <p>foo<strong>bar</strong></p>
  4963. ````````````````````````````````
  4964. Rule 6:
  4965. ```````````````````````````````` example
  4966. __foo bar__
  4967. .
  4968. <p><strong>foo bar</strong></p>
  4969. ````````````````````````````````
  4970. This is not strong emphasis, because the opening delimiter is
  4971. followed by whitespace:
  4972. ```````````````````````````````` example
  4973. __ foo bar__
  4974. .
  4975. <p>__ foo bar__</p>
  4976. ````````````````````````````````
  4977. A newline counts as whitespace:
  4978. ```````````````````````````````` example
  4979. __
  4980. foo bar__
  4981. .
  4982. <p>__
  4983. foo bar__</p>
  4984. ````````````````````````````````
  4985. This is not strong emphasis, because the opening `__` is preceded
  4986. by an alphanumeric and followed by punctuation:
  4987. ```````````````````````````````` example
  4988. a__"foo"__
  4989. .
  4990. <p>a__&quot;foo&quot;__</p>
  4991. ````````````````````````````````
  4992. Intraword strong emphasis is forbidden with `__`:
  4993. ```````````````````````````````` example
  4994. foo__bar__
  4995. .
  4996. <p>foo__bar__</p>
  4997. ````````````````````````````````
  4998. ```````````````````````````````` example
  4999. 5__6__78
  5000. .
  5001. <p>5__6__78</p>
  5002. ````````````````````````````````
  5003. ```````````````````````````````` example
  5004. пристаням__стремятся__
  5005. .
  5006. <p>пристаням__стремятся__</p>
  5007. ````````````````````````````````
  5008. ```````````````````````````````` example
  5009. __foo, __bar__, baz__
  5010. .
  5011. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5012. ````````````````````````````````
  5013. This is strong emphasis, even though the opening delimiter is
  5014. both left- and right-flanking, because it is preceded by
  5015. punctuation:
  5016. ```````````````````````````````` example
  5017. foo-__(bar)__
  5018. .
  5019. <p>foo-<strong>(bar)</strong></p>
  5020. ````````````````````````````````
  5021. Rule 7:
  5022. This is not strong emphasis, because the closing delimiter is preceded
  5023. by whitespace:
  5024. ```````````````````````````````` example
  5025. **foo bar **
  5026. .
  5027. <p>**foo bar **</p>
  5028. ````````````````````````````````
  5029. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5030. Rule 11.)
  5031. This is not strong emphasis, because the second `**` is
  5032. preceded by punctuation and followed by an alphanumeric:
  5033. ```````````````````````````````` example
  5034. **(**foo)
  5035. .
  5036. <p>**(**foo)</p>
  5037. ````````````````````````````````
  5038. The point of this restriction is more easily appreciated
  5039. with these examples:
  5040. ```````````````````````````````` example
  5041. *(**foo**)*
  5042. .
  5043. <p><em>(<strong>foo</strong>)</em></p>
  5044. ````````````````````````````````
  5045. ```````````````````````````````` example
  5046. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5047. *Asclepias physocarpa*)**
  5048. .
  5049. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5050. <em>Asclepias physocarpa</em>)</strong></p>
  5051. ````````````````````````````````
  5052. ```````````````````````````````` example
  5053. **foo "*bar*" foo**
  5054. .
  5055. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5056. ````````````````````````````````
  5057. Intraword emphasis:
  5058. ```````````````````````````````` example
  5059. **foo**bar
  5060. .
  5061. <p><strong>foo</strong>bar</p>
  5062. ````````````````````````````````
  5063. Rule 8:
  5064. This is not strong emphasis, because the closing delimiter is
  5065. preceded by whitespace:
  5066. ```````````````````````````````` example
  5067. __foo bar __
  5068. .
  5069. <p>__foo bar __</p>
  5070. ````````````````````````````````
  5071. This is not strong emphasis, because the second `__` is
  5072. preceded by punctuation and followed by an alphanumeric:
  5073. ```````````````````````````````` example
  5074. __(__foo)
  5075. .
  5076. <p>__(__foo)</p>
  5077. ````````````````````````````````
  5078. The point of this restriction is more easily appreciated
  5079. with this example:
  5080. ```````````````````````````````` example
  5081. _(__foo__)_
  5082. .
  5083. <p><em>(<strong>foo</strong>)</em></p>
  5084. ````````````````````````````````
  5085. Intraword strong emphasis is forbidden with `__`:
  5086. ```````````````````````````````` example
  5087. __foo__bar
  5088. .
  5089. <p>__foo__bar</p>
  5090. ````````````````````````````````
  5091. ```````````````````````````````` example
  5092. __пристаням__стремятся
  5093. .
  5094. <p>__пристаням__стремятся</p>
  5095. ````````````````````````````````
  5096. ```````````````````````````````` example
  5097. __foo__bar__baz__
  5098. .
  5099. <p><strong>foo__bar__baz</strong></p>
  5100. ````````````````````````````````
  5101. This is strong emphasis, even though the closing delimiter is
  5102. both left- and right-flanking, because it is followed by
  5103. punctuation:
  5104. ```````````````````````````````` example
  5105. __(bar)__.
  5106. .
  5107. <p><strong>(bar)</strong>.</p>
  5108. ````````````````````````````````
  5109. Rule 9:
  5110. Any nonempty sequence of inline elements can be the contents of an
  5111. emphasized span.
  5112. ```````````````````````````````` example
  5113. *foo [bar](/url)*
  5114. .
  5115. <p><em>foo <a href="/url">bar</a></em></p>
  5116. ````````````````````````````````
  5117. ```````````````````````````````` example
  5118. *foo
  5119. bar*
  5120. .
  5121. <p><em>foo
  5122. bar</em></p>
  5123. ````````````````````````````````
  5124. In particular, emphasis and strong emphasis can be nested
  5125. inside emphasis:
  5126. ```````````````````````````````` example
  5127. _foo __bar__ baz_
  5128. .
  5129. <p><em>foo <strong>bar</strong> baz</em></p>
  5130. ````````````````````````````````
  5131. ```````````````````````````````` example
  5132. _foo _bar_ baz_
  5133. .
  5134. <p><em>foo <em>bar</em> baz</em></p>
  5135. ````````````````````````````````
  5136. ```````````````````````````````` example
  5137. __foo_ bar_
  5138. .
  5139. <p><em><em>foo</em> bar</em></p>
  5140. ````````````````````````````````
  5141. ```````````````````````````````` example
  5142. *foo *bar**
  5143. .
  5144. <p><em>foo <em>bar</em></em></p>
  5145. ````````````````````````````````
  5146. ```````````````````````````````` example
  5147. *foo **bar** baz*
  5148. .
  5149. <p><em>foo <strong>bar</strong> baz</em></p>
  5150. ````````````````````````````````
  5151. ```````````````````````````````` example
  5152. *foo**bar**baz*
  5153. .
  5154. <p><em>foo<strong>bar</strong>baz</em></p>
  5155. ````````````````````````````````
  5156. Note that in the preceding case, the interpretation
  5157. ``` markdown
  5158. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5159. ```
  5160. is precluded by the condition that a delimiter that
  5161. can both open and close (like the `*` after `foo`)
  5162. cannot form emphasis if the sum of the lengths of
  5163. the delimiter runs containing the opening and
  5164. closing delimiters is a multiple of 3.
  5165. For the same reason, we don't get two consecutive
  5166. emphasis sections in this example:
  5167. ```````````````````````````````` example
  5168. *foo**bar*
  5169. .
  5170. <p><em>foo**bar</em></p>
  5171. ````````````````````````````````
  5172. The same condition ensures that the following
  5173. cases are all strong emphasis nested inside
  5174. emphasis, even when the interior spaces are
  5175. omitted:
  5176. ```````````````````````````````` example
  5177. ***foo** bar*
  5178. .
  5179. <p><em><strong>foo</strong> bar</em></p>
  5180. ````````````````````````````````
  5181. ```````````````````````````````` example
  5182. *foo **bar***
  5183. .
  5184. <p><em>foo <strong>bar</strong></em></p>
  5185. ````````````````````````````````
  5186. ```````````````````````````````` example
  5187. *foo**bar***
  5188. .
  5189. <p><em>foo<strong>bar</strong></em></p>
  5190. ````````````````````````````````
  5191. Indefinite levels of nesting are possible:
  5192. ```````````````````````````````` example
  5193. *foo **bar *baz* bim** bop*
  5194. .
  5195. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5196. ````````````````````````````````
  5197. ```````````````````````````````` example
  5198. *foo [*bar*](/url)*
  5199. .
  5200. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5201. ````````````````````````````````
  5202. There can be no empty emphasis or strong emphasis:
  5203. ```````````````````````````````` example
  5204. ** is not an empty emphasis
  5205. .
  5206. <p>** is not an empty emphasis</p>
  5207. ````````````````````````````````
  5208. ```````````````````````````````` example
  5209. **** is not an empty strong emphasis
  5210. .
  5211. <p>**** is not an empty strong emphasis</p>
  5212. ````````````````````````````````
  5213. Rule 10:
  5214. Any nonempty sequence of inline elements can be the contents of an
  5215. strongly emphasized span.
  5216. ```````````````````````````````` example
  5217. **foo [bar](/url)**
  5218. .
  5219. <p><strong>foo <a href="/url">bar</a></strong></p>
  5220. ````````````````````````````````
  5221. ```````````````````````````````` example
  5222. **foo
  5223. bar**
  5224. .
  5225. <p><strong>foo
  5226. bar</strong></p>
  5227. ````````````````````````````````
  5228. In particular, emphasis and strong emphasis can be nested
  5229. inside strong emphasis:
  5230. ```````````````````````````````` example
  5231. __foo _bar_ baz__
  5232. .
  5233. <p><strong>foo <em>bar</em> baz</strong></p>
  5234. ````````````````````````````````
  5235. ```````````````````````````````` example
  5236. __foo __bar__ baz__
  5237. .
  5238. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5239. ````````````````````````````````
  5240. ```````````````````````````````` example
  5241. ____foo__ bar__
  5242. .
  5243. <p><strong><strong>foo</strong> bar</strong></p>
  5244. ````````````````````````````````
  5245. ```````````````````````````````` example
  5246. **foo **bar****
  5247. .
  5248. <p><strong>foo <strong>bar</strong></strong></p>
  5249. ````````````````````````````````
  5250. ```````````````````````````````` example
  5251. **foo *bar* baz**
  5252. .
  5253. <p><strong>foo <em>bar</em> baz</strong></p>
  5254. ````````````````````````````````
  5255. ```````````````````````````````` example
  5256. **foo*bar*baz**
  5257. .
  5258. <p><strong>foo<em>bar</em>baz</strong></p>
  5259. ````````````````````````````````
  5260. ```````````````````````````````` example
  5261. ***foo* bar**
  5262. .
  5263. <p><strong><em>foo</em> bar</strong></p>
  5264. ````````````````````````````````
  5265. ```````````````````````````````` example
  5266. **foo *bar***
  5267. .
  5268. <p><strong>foo <em>bar</em></strong></p>
  5269. ````````````````````````````````
  5270. Indefinite levels of nesting are possible:
  5271. ```````````````````````````````` example
  5272. **foo *bar **baz**
  5273. bim* bop**
  5274. .
  5275. <p><strong>foo <em>bar <strong>baz</strong>
  5276. bim</em> bop</strong></p>
  5277. ````````````````````````````````
  5278. ```````````````````````````````` example
  5279. **foo [*bar*](/url)**
  5280. .
  5281. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5282. ````````````````````````````````
  5283. There can be no empty emphasis or strong emphasis:
  5284. ```````````````````````````````` example
  5285. __ is not an empty emphasis
  5286. .
  5287. <p>__ is not an empty emphasis</p>
  5288. ````````````````````````````````
  5289. ```````````````````````````````` example
  5290. ____ is not an empty strong emphasis
  5291. .
  5292. <p>____ is not an empty strong emphasis</p>
  5293. ````````````````````````````````
  5294. Rule 11:
  5295. ```````````````````````````````` example
  5296. foo ***
  5297. .
  5298. <p>foo ***</p>
  5299. ````````````````````````````````
  5300. ```````````````````````````````` example
  5301. foo *\**
  5302. .
  5303. <p>foo <em>*</em></p>
  5304. ````````````````````````````````
  5305. ```````````````````````````````` example
  5306. foo *_*
  5307. .
  5308. <p>foo <em>_</em></p>
  5309. ````````````````````````````````
  5310. ```````````````````````````````` example
  5311. foo *****
  5312. .
  5313. <p>foo *****</p>
  5314. ````````````````````````````````
  5315. ```````````````````````````````` example
  5316. foo **\***
  5317. .
  5318. <p>foo <strong>*</strong></p>
  5319. ````````````````````````````````
  5320. ```````````````````````````````` example
  5321. foo **_**
  5322. .
  5323. <p>foo <strong>_</strong></p>
  5324. ````````````````````````````````
  5325. Note that when delimiters do not match evenly, Rule 11 determines
  5326. that the excess literal `*` characters will appear outside of the
  5327. emphasis, rather than inside it:
  5328. ```````````````````````````````` example
  5329. **foo*
  5330. .
  5331. <p>*<em>foo</em></p>
  5332. ````````````````````````````````
  5333. ```````````````````````````````` example
  5334. *foo**
  5335. .
  5336. <p><em>foo</em>*</p>
  5337. ````````````````````````````````
  5338. ```````````````````````````````` example
  5339. ***foo**
  5340. .
  5341. <p>*<strong>foo</strong></p>
  5342. ````````````````````````````````
  5343. ```````````````````````````````` example
  5344. ****foo*
  5345. .
  5346. <p>***<em>foo</em></p>
  5347. ````````````````````````````````
  5348. ```````````````````````````````` example
  5349. **foo***
  5350. .
  5351. <p><strong>foo</strong>*</p>
  5352. ````````````````````````````````
  5353. ```````````````````````````````` example
  5354. *foo****
  5355. .
  5356. <p><em>foo</em>***</p>
  5357. ````````````````````````````````
  5358. Rule 12:
  5359. ```````````````````````````````` example
  5360. foo ___
  5361. .
  5362. <p>foo ___</p>
  5363. ````````````````````````````````
  5364. ```````````````````````````````` example
  5365. foo _\__
  5366. .
  5367. <p>foo <em>_</em></p>
  5368. ````````````````````````````````
  5369. ```````````````````````````````` example
  5370. foo _*_
  5371. .
  5372. <p>foo <em>*</em></p>
  5373. ````````````````````````````````
  5374. ```````````````````````````````` example
  5375. foo _____
  5376. .
  5377. <p>foo _____</p>
  5378. ````````````````````````````````
  5379. ```````````````````````````````` example
  5380. foo __\___
  5381. .
  5382. <p>foo <strong>_</strong></p>
  5383. ````````````````````````````````
  5384. ```````````````````````````````` example
  5385. foo __*__
  5386. .
  5387. <p>foo <strong>*</strong></p>
  5388. ````````````````````````````````
  5389. ```````````````````````````````` example
  5390. __foo_
  5391. .
  5392. <p>_<em>foo</em></p>
  5393. ````````````````````````````````
  5394. Note that when delimiters do not match evenly, Rule 12 determines
  5395. that the excess literal `_` characters will appear outside of the
  5396. emphasis, rather than inside it:
  5397. ```````````````````````````````` example
  5398. _foo__
  5399. .
  5400. <p><em>foo</em>_</p>
  5401. ````````````````````````````````
  5402. ```````````````````````````````` example
  5403. ___foo__
  5404. .
  5405. <p>_<strong>foo</strong></p>
  5406. ````````````````````````````````
  5407. ```````````````````````````````` example
  5408. ____foo_
  5409. .
  5410. <p>___<em>foo</em></p>
  5411. ````````````````````````````````
  5412. ```````````````````````````````` example
  5413. __foo___
  5414. .
  5415. <p><strong>foo</strong>_</p>
  5416. ````````````````````````````````
  5417. ```````````````````````````````` example
  5418. _foo____
  5419. .
  5420. <p><em>foo</em>___</p>
  5421. ````````````````````````````````
  5422. Rule 13 implies that if you want emphasis nested directly inside
  5423. emphasis, you must use different delimiters:
  5424. ```````````````````````````````` example
  5425. **foo**
  5426. .
  5427. <p><strong>foo</strong></p>
  5428. ````````````````````````````````
  5429. ```````````````````````````````` example
  5430. *_foo_*
  5431. .
  5432. <p><em><em>foo</em></em></p>
  5433. ````````````````````````````````
  5434. ```````````````````````````````` example
  5435. __foo__
  5436. .
  5437. <p><strong>foo</strong></p>
  5438. ````````````````````````````````
  5439. ```````````````````````````````` example
  5440. _*foo*_
  5441. .
  5442. <p><em><em>foo</em></em></p>
  5443. ````````````````````````````````
  5444. However, strong emphasis within strong emphasis is possible without
  5445. switching delimiters:
  5446. ```````````````````````````````` example
  5447. ****foo****
  5448. .
  5449. <p><strong><strong>foo</strong></strong></p>
  5450. ````````````````````````````````
  5451. ```````````````````````````````` example
  5452. ____foo____
  5453. .
  5454. <p><strong><strong>foo</strong></strong></p>
  5455. ````````````````````````````````
  5456. Rule 13 can be applied to arbitrarily long sequences of
  5457. delimiters:
  5458. ```````````````````````````````` example
  5459. ******foo******
  5460. .
  5461. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5462. ````````````````````````````````
  5463. Rule 14:
  5464. ```````````````````````````````` example
  5465. ***foo***
  5466. .
  5467. <p><em><strong>foo</strong></em></p>
  5468. ````````````````````````````````
  5469. ```````````````````````````````` example
  5470. _____foo_____
  5471. .
  5472. <p><em><strong><strong>foo</strong></strong></em></p>
  5473. ````````````````````````````````
  5474. Rule 15:
  5475. ```````````````````````````````` example
  5476. *foo _bar* baz_
  5477. .
  5478. <p><em>foo _bar</em> baz_</p>
  5479. ````````````````````````````````
  5480. ```````````````````````````````` example
  5481. *foo __bar *baz bim__ bam*
  5482. .
  5483. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5484. ````````````````````````````````
  5485. Rule 16:
  5486. ```````````````````````````````` example
  5487. **foo **bar baz**
  5488. .
  5489. <p>**foo <strong>bar baz</strong></p>
  5490. ````````````````````````````````
  5491. ```````````````````````````````` example
  5492. *foo *bar baz*
  5493. .
  5494. <p>*foo <em>bar baz</em></p>
  5495. ````````````````````````````````
  5496. Rule 17:
  5497. ```````````````````````````````` example
  5498. *[bar*](/url)
  5499. .
  5500. <p>*<a href="/url">bar*</a></p>
  5501. ````````````````````````````````
  5502. ```````````````````````````````` example
  5503. _foo [bar_](/url)
  5504. .
  5505. <p>_foo <a href="/url">bar_</a></p>
  5506. ````````````````````````````````
  5507. ```````````````````````````````` example
  5508. *<img src="foo" title="*"/>
  5509. .
  5510. <p>*<img src="foo" title="*"/></p>
  5511. ````````````````````````````````
  5512. ```````````````````````````````` example
  5513. **<a href="**">
  5514. .
  5515. <p>**<a href="**"></p>
  5516. ````````````````````````````````
  5517. ```````````````````````````````` example
  5518. __<a href="__">
  5519. .
  5520. <p>__<a href="__"></p>
  5521. ````````````````````````````````
  5522. ```````````````````````````````` example
  5523. *a `*`*
  5524. .
  5525. <p><em>a <code>*</code></em></p>
  5526. ````````````````````````````````
  5527. ```````````````````````````````` example
  5528. _a `_`_
  5529. .
  5530. <p><em>a <code>_</code></em></p>
  5531. ````````````````````````````````
  5532. ```````````````````````````````` example
  5533. **a<http://foo.bar/?q=**>
  5534. .
  5535. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5536. ````````````````````````````````
  5537. ```````````````````````````````` example
  5538. __a<http://foo.bar/?q=__>
  5539. .
  5540. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5541. ````````````````````````````````
  5542. ## Links
  5543. A link contains [link text] (the visible text), a [link destination]
  5544. (the URI that is the link destination), and optionally a [link title].
  5545. There are two basic kinds of links in Markdown. In [inline links] the
  5546. destination and title are given immediately after the link text. In
  5547. [reference links] the destination and title are defined elsewhere in
  5548. the document.
  5549. A [link text](@) consists of a sequence of zero or more
  5550. inline elements enclosed by square brackets (`[` and `]`). The
  5551. following rules apply:
  5552. - Links may not contain other links, at any level of nesting. If
  5553. multiple otherwise valid link definitions appear nested inside each
  5554. other, the inner-most definition is used.
  5555. - Brackets are allowed in the [link text] only if (a) they
  5556. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5557. with an open bracket `[`, a sequence of zero or more inlines, and
  5558. a close bracket `]`.
  5559. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5560. than the brackets in link text. Thus, for example,
  5561. `` [foo`]` `` could not be a link text, since the second `]`
  5562. is part of a code span.
  5563. - The brackets in link text bind more tightly than markers for
  5564. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5565. A [link destination](@) consists of either
  5566. - a sequence of zero or more characters between an opening `<` and a
  5567. closing `>` that contains no line breaks or unescaped
  5568. `<` or `>` characters, or
  5569. - a nonempty sequence of characters that does not include
  5570. ASCII space or control characters, and includes parentheses
  5571. only if (a) they are backslash-escaped or (b) they are part of
  5572. a balanced pair of unescaped parentheses. (Implementations
  5573. may impose limits on parentheses nesting to avoid performance
  5574. issues, but at least three levels of nesting should be supported.)
  5575. A [link title](@) consists of either
  5576. - a sequence of zero or more characters between straight double-quote
  5577. characters (`"`), including a `"` character only if it is
  5578. backslash-escaped, or
  5579. - a sequence of zero or more characters between straight single-quote
  5580. characters (`'`), including a `'` character only if it is
  5581. backslash-escaped, or
  5582. - a sequence of zero or more characters between matching parentheses
  5583. (`(...)`), including a `)` character only if it is backslash-escaped.
  5584. Although [link titles] may span multiple lines, they may not contain
  5585. a [blank line].
  5586. An [inline link](@) consists of a [link text] followed immediately
  5587. by a left parenthesis `(`, optional [whitespace], an optional
  5588. [link destination], an optional [link title] separated from the link
  5589. destination by [whitespace], optional [whitespace], and a right
  5590. parenthesis `)`. The link's text consists of the inlines contained
  5591. in the [link text] (excluding the enclosing square brackets).
  5592. The link's URI consists of the link destination, excluding enclosing
  5593. `<...>` if present, with backslash-escapes in effect as described
  5594. above. The link's title consists of the link title, excluding its
  5595. enclosing delimiters, with backslash-escapes in effect as described
  5596. above.
  5597. Here is a simple inline link:
  5598. ```````````````````````````````` example
  5599. [link](/uri "title")
  5600. .
  5601. <p><a href="/uri" title="title">link</a></p>
  5602. ````````````````````````````````
  5603. The title may be omitted:
  5604. ```````````````````````````````` example
  5605. [link](/uri)
  5606. .
  5607. <p><a href="/uri">link</a></p>
  5608. ````````````````````````````````
  5609. Both the title and the destination may be omitted:
  5610. ```````````````````````````````` example
  5611. [link]()
  5612. .
  5613. <p><a href="">link</a></p>
  5614. ````````````````````````````````
  5615. ```````````````````````````````` example
  5616. [link](<>)
  5617. .
  5618. <p><a href="">link</a></p>
  5619. ````````````````````````````````
  5620. The destination can only contain spaces if it is
  5621. enclosed in pointy brackets:
  5622. ```````````````````````````````` example
  5623. [link](/my uri)
  5624. .
  5625. <p>[link](/my uri)</p>
  5626. ````````````````````````````````
  5627. ```````````````````````````````` example
  5628. [link](</my uri>)
  5629. .
  5630. <p><a href="/my%20uri">link</a></p>
  5631. ````````````````````````````````
  5632. The destination cannot contain line breaks,
  5633. even if enclosed in pointy brackets:
  5634. ```````````````````````````````` example
  5635. [link](foo
  5636. bar)
  5637. .
  5638. <p>[link](foo
  5639. bar)</p>
  5640. ````````````````````````````````
  5641. ```````````````````````````````` example
  5642. [link](<foo
  5643. bar>)
  5644. .
  5645. <p>[link](<foo
  5646. bar>)</p>
  5647. ````````````````````````````````
  5648. Parentheses inside the link destination may be escaped:
  5649. ```````````````````````````````` example
  5650. [link](\(foo\))
  5651. .
  5652. <p><a href="(foo)">link</a></p>
  5653. ````````````````````````````````
  5654. Any number of parentheses are allowed without escaping, as long as they are
  5655. balanced:
  5656. ```````````````````````````````` example
  5657. [link](foo(and(bar)))
  5658. .
  5659. <p><a href="foo(and(bar))">link</a></p>
  5660. ````````````````````````````````
  5661. However, if you have unbalanced parentheses, you need to escape or use the
  5662. `<...>` form:
  5663. ```````````````````````````````` example
  5664. [link](foo\(and\(bar\))
  5665. .
  5666. <p><a href="foo(and(bar)">link</a></p>
  5667. ````````````````````````````````
  5668. ```````````````````````````````` example
  5669. [link](<foo(and(bar)>)
  5670. .
  5671. <p><a href="foo(and(bar)">link</a></p>
  5672. ````````````````````````````````
  5673. Parentheses and other symbols can also be escaped, as usual
  5674. in Markdown:
  5675. ```````````````````````````````` example
  5676. [link](foo\)\:)
  5677. .
  5678. <p><a href="foo):">link</a></p>
  5679. ````````````````````````````````
  5680. A link can contain fragment identifiers and queries:
  5681. ```````````````````````````````` example
  5682. [link](#fragment)
  5683. [link](http://example.com#fragment)
  5684. [link](http://example.com?foo=3#frag)
  5685. .
  5686. <p><a href="#fragment">link</a></p>
  5687. <p><a href="http://example.com#fragment">link</a></p>
  5688. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5689. ````````````````````````````````
  5690. Note that a backslash before a non-escapable character is
  5691. just a backslash:
  5692. ```````````````````````````````` example
  5693. [link](foo\bar)
  5694. .
  5695. <p><a href="foo%5Cbar">link</a></p>
  5696. ````````````````````````````````
  5697. URL-escaping should be left alone inside the destination, as all
  5698. URL-escaped characters are also valid URL characters. Entity and
  5699. numerical character references in the destination will be parsed
  5700. into the corresponding Unicode code points, as usual. These may
  5701. be optionally URL-escaped when written as HTML, but this spec
  5702. does not enforce any particular policy for rendering URLs in
  5703. HTML or other formats. Renderers may make different decisions
  5704. about how to escape or normalize URLs in the output.
  5705. ```````````````````````````````` example
  5706. [link](foo%20b&auml;)
  5707. .
  5708. <p><a href="foo%20b%C3%A4">link</a></p>
  5709. ````````````````````````````````
  5710. Note that, because titles can often be parsed as destinations,
  5711. if you try to omit the destination and keep the title, you'll
  5712. get unexpected results:
  5713. ```````````````````````````````` example
  5714. [link]("title")
  5715. .
  5716. <p><a href="%22title%22">link</a></p>
  5717. ````````````````````````````````
  5718. Titles may be in single quotes, double quotes, or parentheses:
  5719. ```````````````````````````````` example
  5720. [link](/url "title")
  5721. [link](/url 'title')
  5722. [link](/url (title))
  5723. .
  5724. <p><a href="/url" title="title">link</a>
  5725. <a href="/url" title="title">link</a>
  5726. <a href="/url" title="title">link</a></p>
  5727. ````````````````````````````````
  5728. Backslash escapes and entity and numeric character references
  5729. may be used in titles:
  5730. ```````````````````````````````` example
  5731. [link](/url "title \"&quot;")
  5732. .
  5733. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5734. ````````````````````````````````
  5735. Titles must be separated from the link using a [whitespace].
  5736. Other [Unicode whitespace] like non-breaking space doesn't work.
  5737. ```````````````````````````````` example
  5738. [link](/url "title")
  5739. .
  5740. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5741. ````````````````````````````````
  5742. Nested balanced quotes are not allowed without escaping:
  5743. ```````````````````````````````` example
  5744. [link](/url "title "and" title")
  5745. .
  5746. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5747. ````````````````````````````````
  5748. But it is easy to work around this by using a different quote type:
  5749. ```````````````````````````````` example
  5750. [link](/url 'title "and" title')
  5751. .
  5752. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5753. ````````````````````````````````
  5754. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5755. title, and its test suite included a test demonstrating this.
  5756. But it is hard to see a good rationale for the extra complexity this
  5757. brings, since there are already many ways---backslash escaping,
  5758. entity and numeric character references, or using a different
  5759. quote type for the enclosing title---to write titles containing
  5760. double quotes. `Markdown.pl`'s handling of titles has a number
  5761. of other strange features. For example, it allows single-quoted
  5762. titles in inline links, but not reference links. And, in
  5763. reference links but not inline links, it allows a title to begin
  5764. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5765. titles with no closing quotation mark, though 1.0.2b8 does not.
  5766. It seems preferable to adopt a simple, rational rule that works
  5767. the same way in inline links and link reference definitions.)
  5768. [Whitespace] is allowed around the destination and title:
  5769. ```````````````````````````````` example
  5770. [link]( /uri
  5771. "title" )
  5772. .
  5773. <p><a href="/uri" title="title">link</a></p>
  5774. ````````````````````````````````
  5775. But it is not allowed between the link text and the
  5776. following parenthesis:
  5777. ```````````````````````````````` example
  5778. [link] (/uri)
  5779. .
  5780. <p>[link] (/uri)</p>
  5781. ````````````````````````````````
  5782. The link text may contain balanced brackets, but not unbalanced ones,
  5783. unless they are escaped:
  5784. ```````````````````````````````` example
  5785. [link [foo [bar]]](/uri)
  5786. .
  5787. <p><a href="/uri">link [foo [bar]]</a></p>
  5788. ````````````````````````````````
  5789. ```````````````````````````````` example
  5790. [link] bar](/uri)
  5791. .
  5792. <p>[link] bar](/uri)</p>
  5793. ````````````````````````````````
  5794. ```````````````````````````````` example
  5795. [link [bar](/uri)
  5796. .
  5797. <p>[link <a href="/uri">bar</a></p>
  5798. ````````````````````````````````
  5799. ```````````````````````````````` example
  5800. [link \[bar](/uri)
  5801. .
  5802. <p><a href="/uri">link [bar</a></p>
  5803. ````````````````````````````````
  5804. The link text may contain inline content:
  5805. ```````````````````````````````` example
  5806. [link *foo **bar** `#`*](/uri)
  5807. .
  5808. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5809. ````````````````````````````````
  5810. ```````````````````````````````` example
  5811. [![moon](moon.jpg)](/uri)
  5812. .
  5813. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5814. ````````````````````````````````
  5815. However, links may not contain other links, at any level of nesting.
  5816. ```````````````````````````````` example
  5817. [foo [bar](/uri)](/uri)
  5818. .
  5819. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5820. ````````````````````````````````
  5821. ```````````````````````````````` example
  5822. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5823. .
  5824. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5825. ````````````````````````````````
  5826. ```````````````````````````````` example
  5827. ![[[foo](uri1)](uri2)](uri3)
  5828. .
  5829. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5830. ````````````````````````````````
  5831. These cases illustrate the precedence of link text grouping over
  5832. emphasis grouping:
  5833. ```````````````````````````````` example
  5834. *[foo*](/uri)
  5835. .
  5836. <p>*<a href="/uri">foo*</a></p>
  5837. ````````````````````````````````
  5838. ```````````````````````````````` example
  5839. [foo *bar](baz*)
  5840. .
  5841. <p><a href="baz*">foo *bar</a></p>
  5842. ````````````````````````````````
  5843. Note that brackets that *aren't* part of links do not take
  5844. precedence:
  5845. ```````````````````````````````` example
  5846. *foo [bar* baz]
  5847. .
  5848. <p><em>foo [bar</em> baz]</p>
  5849. ````````````````````````````````
  5850. These cases illustrate the precedence of HTML tags, code spans,
  5851. and autolinks over link grouping:
  5852. ```````````````````````````````` example
  5853. [foo <bar attr="](baz)">
  5854. .
  5855. <p>[foo <bar attr="](baz)"></p>
  5856. ````````````````````````````````
  5857. ```````````````````````````````` example
  5858. [foo`](/uri)`
  5859. .
  5860. <p>[foo<code>](/uri)</code></p>
  5861. ````````````````````````````````
  5862. ```````````````````````````````` example
  5863. [foo<http://example.com/?search=](uri)>
  5864. .
  5865. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5866. ````````````````````````````````
  5867. There are three kinds of [reference link](@)s:
  5868. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5869. and [shortcut](#shortcut-reference-link).
  5870. A [full reference link](@)
  5871. consists of a [link text] immediately followed by a [link label]
  5872. that [matches] a [link reference definition] elsewhere in the document.
  5873. A [link label](@) begins with a left bracket (`[`) and ends
  5874. with the first right bracket (`]`) that is not backslash-escaped.
  5875. Between these brackets there must be at least one [non-whitespace character].
  5876. Unescaped square bracket characters are not allowed inside the
  5877. opening and closing square brackets of [link labels]. A link
  5878. label can have at most 999 characters inside the square
  5879. brackets.
  5880. One label [matches](@)
  5881. another just in case their normalized forms are equal. To normalize a
  5882. label, strip off the opening and closing brackets,
  5883. perform the *Unicode case fold*, strip leading and trailing
  5884. [whitespace] and collapse consecutive internal
  5885. [whitespace] to a single space. If there are multiple
  5886. matching reference link definitions, the one that comes first in the
  5887. document is used. (It is desirable in such cases to emit a warning.)
  5888. The contents of the first link label are parsed as inlines, which are
  5889. used as the link's text. The link's URI and title are provided by the
  5890. matching [link reference definition].
  5891. Here is a simple example:
  5892. ```````````````````````````````` example
  5893. [foo][bar]
  5894. [bar]: /url "title"
  5895. .
  5896. <p><a href="/url" title="title">foo</a></p>
  5897. ````````````````````````````````
  5898. The rules for the [link text] are the same as with
  5899. [inline links]. Thus:
  5900. The link text may contain balanced brackets, but not unbalanced ones,
  5901. unless they are escaped:
  5902. ```````````````````````````````` example
  5903. [link [foo [bar]]][ref]
  5904. [ref]: /uri
  5905. .
  5906. <p><a href="/uri">link [foo [bar]]</a></p>
  5907. ````````````````````````````````
  5908. ```````````````````````````````` example
  5909. [link \[bar][ref]
  5910. [ref]: /uri
  5911. .
  5912. <p><a href="/uri">link [bar</a></p>
  5913. ````````````````````````````````
  5914. The link text may contain inline content:
  5915. ```````````````````````````````` example
  5916. [link *foo **bar** `#`*][ref]
  5917. [ref]: /uri
  5918. .
  5919. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5920. ````````````````````````````````
  5921. ```````````````````````````````` example
  5922. [![moon](moon.jpg)][ref]
  5923. [ref]: /uri
  5924. .
  5925. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5926. ````````````````````````````````
  5927. However, links may not contain other links, at any level of nesting.
  5928. ```````````````````````````````` example
  5929. [foo [bar](/uri)][ref]
  5930. [ref]: /uri
  5931. .
  5932. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5933. ````````````````````````````````
  5934. ```````````````````````````````` example
  5935. [foo *bar [baz][ref]*][ref]
  5936. [ref]: /uri
  5937. .
  5938. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5939. ````````````````````````````````
  5940. (In the examples above, we have two [shortcut reference links]
  5941. instead of one [full reference link].)
  5942. The following cases illustrate the precedence of link text grouping over
  5943. emphasis grouping:
  5944. ```````````````````````````````` example
  5945. *[foo*][ref]
  5946. [ref]: /uri
  5947. .
  5948. <p>*<a href="/uri">foo*</a></p>
  5949. ````````````````````````````````
  5950. ```````````````````````````````` example
  5951. [foo *bar][ref]
  5952. [ref]: /uri
  5953. .
  5954. <p><a href="/uri">foo *bar</a></p>
  5955. ````````````````````````````````
  5956. These cases illustrate the precedence of HTML tags, code spans,
  5957. and autolinks over link grouping:
  5958. ```````````````````````````````` example
  5959. [foo <bar attr="][ref]">
  5960. [ref]: /uri
  5961. .
  5962. <p>[foo <bar attr="][ref]"></p>
  5963. ````````````````````````````````
  5964. ```````````````````````````````` example
  5965. [foo`][ref]`
  5966. [ref]: /uri
  5967. .
  5968. <p>[foo<code>][ref]</code></p>
  5969. ````````````````````````````````
  5970. ```````````````````````````````` example
  5971. [foo<http://example.com/?search=][ref]>
  5972. [ref]: /uri
  5973. .
  5974. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  5975. ````````````````````````````````
  5976. Matching is case-insensitive:
  5977. ```````````````````````````````` example
  5978. [foo][BaR]
  5979. [bar]: /url "title"
  5980. .
  5981. <p><a href="/url" title="title">foo</a></p>
  5982. ````````````````````````````````
  5983. Unicode case fold is used:
  5984. ```````````````````````````````` example
  5985. [Толпой][Толпой] is a Russian word.
  5986. [ТОЛПОЙ]: /url
  5987. .
  5988. <p><a href="/url">Толпой</a> is a Russian word.</p>
  5989. ````````````````````````````````
  5990. Consecutive internal [whitespace] is treated as one space for
  5991. purposes of determining matching:
  5992. ```````````````````````````````` example
  5993. [Foo
  5994. bar]: /url
  5995. [Baz][Foo bar]
  5996. .
  5997. <p><a href="/url">Baz</a></p>
  5998. ````````````````````````````````
  5999. No [whitespace] is allowed between the [link text] and the
  6000. [link label]:
  6001. ```````````````````````````````` example
  6002. [foo] [bar]
  6003. [bar]: /url "title"
  6004. .
  6005. <p>[foo] <a href="/url" title="title">bar</a></p>
  6006. ````````````````````````````````
  6007. ```````````````````````````````` example
  6008. [foo]
  6009. [bar]
  6010. [bar]: /url "title"
  6011. .
  6012. <p>[foo]
  6013. <a href="/url" title="title">bar</a></p>
  6014. ````````````````````````````````
  6015. This is a departure from John Gruber's original Markdown syntax
  6016. description, which explicitly allows whitespace between the link
  6017. text and the link label. It brings reference links in line with
  6018. [inline links], which (according to both original Markdown and
  6019. this spec) cannot have whitespace after the link text. More
  6020. importantly, it prevents inadvertent capture of consecutive
  6021. [shortcut reference links]. If whitespace is allowed between the
  6022. link text and the link label, then in the following we will have
  6023. a single reference link, not two shortcut reference links, as
  6024. intended:
  6025. ``` markdown
  6026. [foo]
  6027. [bar]
  6028. [foo]: /url1
  6029. [bar]: /url2
  6030. ```
  6031. (Note that [shortcut reference links] were introduced by Gruber
  6032. himself in a beta version of `Markdown.pl`, but never included
  6033. in the official syntax description. Without shortcut reference
  6034. links, it is harmless to allow space between the link text and
  6035. link label; but once shortcut references are introduced, it is
  6036. too dangerous to allow this, as it frequently leads to
  6037. unintended results.)
  6038. When there are multiple matching [link reference definitions],
  6039. the first is used:
  6040. ```````````````````````````````` example
  6041. [foo]: /url1
  6042. [foo]: /url2
  6043. [bar][foo]
  6044. .
  6045. <p><a href="/url1">bar</a></p>
  6046. ````````````````````````````````
  6047. Note that matching is performed on normalized strings, not parsed
  6048. inline content. So the following does not match, even though the
  6049. labels define equivalent inline content:
  6050. ```````````````````````````````` example
  6051. [bar][foo\!]
  6052. [foo!]: /url
  6053. .
  6054. <p>[bar][foo!]</p>
  6055. ````````````````````````````````
  6056. [Link labels] cannot contain brackets, unless they are
  6057. backslash-escaped:
  6058. ```````````````````````````````` example
  6059. [foo][ref[]
  6060. [ref[]: /uri
  6061. .
  6062. <p>[foo][ref[]</p>
  6063. <p>[ref[]: /uri</p>
  6064. ````````````````````````````````
  6065. ```````````````````````````````` example
  6066. [foo][ref[bar]]
  6067. [ref[bar]]: /uri
  6068. .
  6069. <p>[foo][ref[bar]]</p>
  6070. <p>[ref[bar]]: /uri</p>
  6071. ````````````````````````````````
  6072. ```````````````````````````````` example
  6073. [[[foo]]]
  6074. [[[foo]]]: /url
  6075. .
  6076. <p>[[[foo]]]</p>
  6077. <p>[[[foo]]]: /url</p>
  6078. ````````````````````````````````
  6079. ```````````````````````````````` example
  6080. [foo][ref\[]
  6081. [ref\[]: /uri
  6082. .
  6083. <p><a href="/uri">foo</a></p>
  6084. ````````````````````````````````
  6085. Note that in this example `]` is not backslash-escaped:
  6086. ```````````````````````````````` example
  6087. [bar\\]: /uri
  6088. [bar\\]
  6089. .
  6090. <p><a href="/uri">bar\</a></p>
  6091. ````````````````````````````````
  6092. A [link label] must contain at least one [non-whitespace character]:
  6093. ```````````````````````````````` example
  6094. []
  6095. []: /uri
  6096. .
  6097. <p>[]</p>
  6098. <p>[]: /uri</p>
  6099. ````````````````````````````````
  6100. ```````````````````````````````` example
  6101. [
  6102. ]
  6103. [
  6104. ]: /uri
  6105. .
  6106. <p>[
  6107. ]</p>
  6108. <p>[
  6109. ]: /uri</p>
  6110. ````````````````````````````````
  6111. A [collapsed reference link](@)
  6112. consists of a [link label] that [matches] a
  6113. [link reference definition] elsewhere in the
  6114. document, followed by the string `[]`.
  6115. The contents of the first link label are parsed as inlines,
  6116. which are used as the link's text. The link's URI and title are
  6117. provided by the matching reference link definition. Thus,
  6118. `[foo][]` is equivalent to `[foo][foo]`.
  6119. ```````````````````````````````` example
  6120. [foo][]
  6121. [foo]: /url "title"
  6122. .
  6123. <p><a href="/url" title="title">foo</a></p>
  6124. ````````````````````````````````
  6125. ```````````````````````````````` example
  6126. [*foo* bar][]
  6127. [*foo* bar]: /url "title"
  6128. .
  6129. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6130. ````````````````````````````````
  6131. The link labels are case-insensitive:
  6132. ```````````````````````````````` example
  6133. [Foo][]
  6134. [foo]: /url "title"
  6135. .
  6136. <p><a href="/url" title="title">Foo</a></p>
  6137. ````````````````````````````````
  6138. As with full reference links, [whitespace] is not
  6139. allowed between the two sets of brackets:
  6140. ```````````````````````````````` example
  6141. [foo]
  6142. []
  6143. [foo]: /url "title"
  6144. .
  6145. <p><a href="/url" title="title">foo</a>
  6146. []</p>
  6147. ````````````````````````````````
  6148. A [shortcut reference link](@)
  6149. consists of a [link label] that [matches] a
  6150. [link reference definition] elsewhere in the
  6151. document and is not followed by `[]` or a link label.
  6152. The contents of the first link label are parsed as inlines,
  6153. which are used as the link's text. The link's URI and title
  6154. are provided by the matching link reference definition.
  6155. Thus, `[foo]` is equivalent to `[foo][]`.
  6156. ```````````````````````````````` example
  6157. [foo]
  6158. [foo]: /url "title"
  6159. .
  6160. <p><a href="/url" title="title">foo</a></p>
  6161. ````````````````````````````````
  6162. ```````````````````````````````` example
  6163. [*foo* bar]
  6164. [*foo* bar]: /url "title"
  6165. .
  6166. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6167. ````````````````````````````````
  6168. ```````````````````````````````` example
  6169. [[*foo* bar]]
  6170. [*foo* bar]: /url "title"
  6171. .
  6172. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6173. ````````````````````````````````
  6174. ```````````````````````````````` example
  6175. [[bar [foo]
  6176. [foo]: /url
  6177. .
  6178. <p>[[bar <a href="/url">foo</a></p>
  6179. ````````````````````````````````
  6180. The link labels are case-insensitive:
  6181. ```````````````````````````````` example
  6182. [Foo]
  6183. [foo]: /url "title"
  6184. .
  6185. <p><a href="/url" title="title">Foo</a></p>
  6186. ````````````````````````````````
  6187. A space after the link text should be preserved:
  6188. ```````````````````````````````` example
  6189. [foo] bar
  6190. [foo]: /url
  6191. .
  6192. <p><a href="/url">foo</a> bar</p>
  6193. ````````````````````````````````
  6194. If you just want bracketed text, you can backslash-escape the
  6195. opening bracket to avoid links:
  6196. ```````````````````````````````` example
  6197. \[foo]
  6198. [foo]: /url "title"
  6199. .
  6200. <p>[foo]</p>
  6201. ````````````````````````````````
  6202. Note that this is a link, because a link label ends with the first
  6203. following closing bracket:
  6204. ```````````````````````````````` example
  6205. [foo*]: /url
  6206. *[foo*]
  6207. .
  6208. <p>*<a href="/url">foo*</a></p>
  6209. ````````````````````````````````
  6210. Full and compact references take precedence over shortcut
  6211. references:
  6212. ```````````````````````````````` example
  6213. [foo][bar]
  6214. [foo]: /url1
  6215. [bar]: /url2
  6216. .
  6217. <p><a href="/url2">foo</a></p>
  6218. ````````````````````````````````
  6219. ```````````````````````````````` example
  6220. [foo][]
  6221. [foo]: /url1
  6222. .
  6223. <p><a href="/url1">foo</a></p>
  6224. ````````````````````````````````
  6225. Inline links also take precedence:
  6226. ```````````````````````````````` example
  6227. [foo]()
  6228. [foo]: /url1
  6229. .
  6230. <p><a href="">foo</a></p>
  6231. ````````````````````````````````
  6232. ```````````````````````````````` example
  6233. [foo](not a link)
  6234. [foo]: /url1
  6235. .
  6236. <p><a href="/url1">foo</a>(not a link)</p>
  6237. ````````````````````````````````
  6238. In the following case `[bar][baz]` is parsed as a reference,
  6239. `[foo]` as normal text:
  6240. ```````````````````````````````` example
  6241. [foo][bar][baz]
  6242. [baz]: /url
  6243. .
  6244. <p>[foo]<a href="/url">bar</a></p>
  6245. ````````````````````````````````
  6246. Here, though, `[foo][bar]` is parsed as a reference, since
  6247. `[bar]` is defined:
  6248. ```````````````````````````````` example
  6249. [foo][bar][baz]
  6250. [baz]: /url1
  6251. [bar]: /url2
  6252. .
  6253. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6254. ````````````````````````````````
  6255. Here `[foo]` is not parsed as a shortcut reference, because it
  6256. is followed by a link label (even though `[bar]` is not defined):
  6257. ```````````````````````````````` example
  6258. [foo][bar][baz]
  6259. [baz]: /url1
  6260. [foo]: /url2
  6261. .
  6262. <p>[foo]<a href="/url1">bar</a></p>
  6263. ````````````````````````````````
  6264. ## Images
  6265. Syntax for images is like the syntax for links, with one
  6266. difference. Instead of [link text], we have an
  6267. [image description](@). The rules for this are the
  6268. same as for [link text], except that (a) an
  6269. image description starts with `![` rather than `[`, and
  6270. (b) an image description may contain links.
  6271. An image description has inline elements
  6272. as its contents. When an image is rendered to HTML,
  6273. this is standardly used as the image's `alt` attribute.
  6274. ```````````````````````````````` example
  6275. ![foo](/url "title")
  6276. .
  6277. <p><img src="/url" alt="foo" title="title" /></p>
  6278. ````````````````````````````````
  6279. ```````````````````````````````` example
  6280. ![foo *bar*]
  6281. [foo *bar*]: train.jpg "train & tracks"
  6282. .
  6283. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6284. ````````````````````````````````
  6285. ```````````````````````````````` example
  6286. ![foo ![bar](/url)](/url2)
  6287. .
  6288. <p><img src="/url2" alt="foo bar" /></p>
  6289. ````````````````````````````````
  6290. ```````````````````````````````` example
  6291. ![foo [bar](/url)](/url2)
  6292. .
  6293. <p><img src="/url2" alt="foo bar" /></p>
  6294. ````````````````````````````````
  6295. Though this spec is concerned with parsing, not rendering, it is
  6296. recommended that in rendering to HTML, only the plain string content
  6297. of the [image description] be used. Note that in
  6298. the above example, the alt attribute's value is `foo bar`, not `foo
  6299. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6300. content is rendered, without formatting.
  6301. ```````````````````````````````` example
  6302. ![foo *bar*][]
  6303. [foo *bar*]: train.jpg "train & tracks"
  6304. .
  6305. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6306. ````````````````````````````````
  6307. ```````````````````````````````` example
  6308. ![foo *bar*][foobar]
  6309. [FOOBAR]: train.jpg "train & tracks"
  6310. .
  6311. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6312. ````````````````````````````````
  6313. ```````````````````````````````` example
  6314. ![foo](train.jpg)
  6315. .
  6316. <p><img src="train.jpg" alt="foo" /></p>
  6317. ````````````````````````````````
  6318. ```````````````````````````````` example
  6319. My ![foo bar](/path/to/train.jpg "title" )
  6320. .
  6321. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6322. ````````````````````````````````
  6323. ```````````````````````````````` example
  6324. ![foo](<url>)
  6325. .
  6326. <p><img src="url" alt="foo" /></p>
  6327. ````````````````````````````````
  6328. ```````````````````````````````` example
  6329. ![](/url)
  6330. .
  6331. <p><img src="/url" alt="" /></p>
  6332. ````````````````````````````````
  6333. Reference-style:
  6334. ```````````````````````````````` example
  6335. ![foo][bar]
  6336. [bar]: /url
  6337. .
  6338. <p><img src="/url" alt="foo" /></p>
  6339. ````````````````````````````````
  6340. ```````````````````````````````` example
  6341. ![foo][bar]
  6342. [BAR]: /url
  6343. .
  6344. <p><img src="/url" alt="foo" /></p>
  6345. ````````````````````````````````
  6346. Collapsed:
  6347. ```````````````````````````````` example
  6348. ![foo][]
  6349. [foo]: /url "title"
  6350. .
  6351. <p><img src="/url" alt="foo" title="title" /></p>
  6352. ````````````````````````````````
  6353. ```````````````````````````````` example
  6354. ![*foo* bar][]
  6355. [*foo* bar]: /url "title"
  6356. .
  6357. <p><img src="/url" alt="foo bar" title="title" /></p>
  6358. ````````````````````````````````
  6359. The labels are case-insensitive:
  6360. ```````````````````````````````` example
  6361. ![Foo][]
  6362. [foo]: /url "title"
  6363. .
  6364. <p><img src="/url" alt="Foo" title="title" /></p>
  6365. ````````````````````````````````
  6366. As with reference links, [whitespace] is not allowed
  6367. between the two sets of brackets:
  6368. ```````````````````````````````` example
  6369. ![foo]
  6370. []
  6371. [foo]: /url "title"
  6372. .
  6373. <p><img src="/url" alt="foo" title="title" />
  6374. []</p>
  6375. ````````````````````````````````
  6376. Shortcut:
  6377. ```````````````````````````````` example
  6378. ![foo]
  6379. [foo]: /url "title"
  6380. .
  6381. <p><img src="/url" alt="foo" title="title" /></p>
  6382. ````````````````````````````````
  6383. ```````````````````````````````` example
  6384. ![*foo* bar]
  6385. [*foo* bar]: /url "title"
  6386. .
  6387. <p><img src="/url" alt="foo bar" title="title" /></p>
  6388. ````````````````````````````````
  6389. Note that link labels cannot contain unescaped brackets:
  6390. ```````````````````````````````` example
  6391. ![[foo]]
  6392. [[foo]]: /url "title"
  6393. .
  6394. <p>![[foo]]</p>
  6395. <p>[[foo]]: /url &quot;title&quot;</p>
  6396. ````````````````````````````````
  6397. The link labels are case-insensitive:
  6398. ```````````````````````````````` example
  6399. ![Foo]
  6400. [foo]: /url "title"
  6401. .
  6402. <p><img src="/url" alt="Foo" title="title" /></p>
  6403. ````````````````````````````````
  6404. If you just want a literal `!` followed by bracketed text, you can
  6405. backslash-escape the opening `[`:
  6406. ```````````````````````````````` example
  6407. !\[foo]
  6408. [foo]: /url "title"
  6409. .
  6410. <p>![foo]</p>
  6411. ````````````````````````````````
  6412. If you want a link after a literal `!`, backslash-escape the
  6413. `!`:
  6414. ```````````````````````````````` example
  6415. \![foo]
  6416. [foo]: /url "title"
  6417. .
  6418. <p>!<a href="/url" title="title">foo</a></p>
  6419. ````````````````````````````````
  6420. ## Autolinks
  6421. [Autolink](@)s are absolute URIs and email addresses inside
  6422. `<` and `>`. They are parsed as links, with the URL or email address
  6423. as the link label.
  6424. A [URI autolink](@) consists of `<`, followed by an
  6425. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6426. a link to the URI, with the URI as the link's label.
  6427. An [absolute URI](@),
  6428. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6429. followed by zero or more characters other than ASCII
  6430. [whitespace] and control characters, `<`, and `>`. If
  6431. the URI includes these characters, they must be percent-encoded
  6432. (e.g. `%20` for a space).
  6433. For purposes of this spec, a [scheme](@) is any sequence
  6434. of 2--32 characters beginning with an ASCII letter and followed
  6435. by any combination of ASCII letters, digits, or the symbols plus
  6436. ("+"), period ("."), or hyphen ("-").
  6437. Here are some valid autolinks:
  6438. ```````````````````````````````` example
  6439. <http://foo.bar.baz>
  6440. .
  6441. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6442. ````````````````````````````````
  6443. ```````````````````````````````` example
  6444. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6445. .
  6446. <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>
  6447. ````````````````````````````````
  6448. ```````````````````````````````` example
  6449. <irc://foo.bar:2233/baz>
  6450. .
  6451. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6452. ````````````````````````````````
  6453. Uppercase is also fine:
  6454. ```````````````````````````````` example
  6455. <MAILTO:FOO@BAR.BAZ>
  6456. .
  6457. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6458. ````````````````````````````````
  6459. Note that many strings that count as [absolute URIs] for
  6460. purposes of this spec are not valid URIs, because their
  6461. schemes are not registered or because of other problems
  6462. with their syntax:
  6463. ```````````````````````````````` example
  6464. <a+b+c:d>
  6465. .
  6466. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6467. ````````````````````````````````
  6468. ```````````````````````````````` example
  6469. <made-up-scheme://foo,bar>
  6470. .
  6471. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6472. ````````````````````````````````
  6473. ```````````````````````````````` example
  6474. <http://../>
  6475. .
  6476. <p><a href="http://../">http://../</a></p>
  6477. ````````````````````````````````
  6478. ```````````````````````````````` example
  6479. <localhost:5001/foo>
  6480. .
  6481. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6482. ````````````````````````````````
  6483. Spaces are not allowed in autolinks:
  6484. ```````````````````````````````` example
  6485. <http://foo.bar/baz bim>
  6486. .
  6487. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6488. ````````````````````````````````
  6489. Backslash-escapes do not work inside autolinks:
  6490. ```````````````````````````````` example
  6491. <http://example.com/\[\>
  6492. .
  6493. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6494. ````````````````````````````````
  6495. An [email autolink](@)
  6496. consists of `<`, followed by an [email address],
  6497. followed by `>`. The link's label is the email address,
  6498. and the URL is `mailto:` followed by the email address.
  6499. An [email address](@),
  6500. for these purposes, is anything that matches
  6501. the [non-normative regex from the HTML5
  6502. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6503. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6504. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6505. Examples of email autolinks:
  6506. ```````````````````````````````` example
  6507. <foo@bar.example.com>
  6508. .
  6509. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6510. ````````````````````````````````
  6511. ```````````````````````````````` example
  6512. <foo+special@Bar.baz-bar0.com>
  6513. .
  6514. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6515. ````````````````````````````````
  6516. Backslash-escapes do not work inside email autolinks:
  6517. ```````````````````````````````` example
  6518. <foo\+@bar.example.com>
  6519. .
  6520. <p>&lt;foo+@bar.example.com&gt;</p>
  6521. ````````````````````````````````
  6522. These are not autolinks:
  6523. ```````````````````````````````` example
  6524. <>
  6525. .
  6526. <p>&lt;&gt;</p>
  6527. ````````````````````````````````
  6528. ```````````````````````````````` example
  6529. < http://foo.bar >
  6530. .
  6531. <p>&lt; http://foo.bar &gt;</p>
  6532. ````````````````````````````````
  6533. ```````````````````````````````` example
  6534. <m:abc>
  6535. .
  6536. <p>&lt;m:abc&gt;</p>
  6537. ````````````````````````````````
  6538. ```````````````````````````````` example
  6539. <foo.bar.baz>
  6540. .
  6541. <p>&lt;foo.bar.baz&gt;</p>
  6542. ````````````````````````````````
  6543. ```````````````````````````````` example
  6544. http://example.com
  6545. .
  6546. <p>http://example.com</p>
  6547. ````````````````````````````````
  6548. ```````````````````````````````` example
  6549. foo@bar.example.com
  6550. .
  6551. <p>foo@bar.example.com</p>
  6552. ````````````````````````````````
  6553. ## Raw HTML
  6554. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6555. raw HTML tag and will be rendered in HTML without escaping.
  6556. Tag and attribute names are not limited to current HTML tags,
  6557. so custom tags (and even, say, DocBook tags) may be used.
  6558. Here is the grammar for tags:
  6559. A [tag name](@) consists of an ASCII letter
  6560. followed by zero or more ASCII letters, digits, or
  6561. hyphens (`-`).
  6562. An [attribute](@) consists of [whitespace],
  6563. an [attribute name], and an optional
  6564. [attribute value specification].
  6565. An [attribute name](@)
  6566. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6567. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6568. specification restricted to ASCII. HTML5 is laxer.)
  6569. An [attribute value specification](@)
  6570. consists of optional [whitespace],
  6571. a `=` character, optional [whitespace], and an [attribute
  6572. value].
  6573. An [attribute value](@)
  6574. consists of an [unquoted attribute value],
  6575. a [single-quoted attribute value], or a [double-quoted attribute value].
  6576. An [unquoted attribute value](@)
  6577. is a nonempty string of characters not
  6578. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6579. A [single-quoted attribute value](@)
  6580. consists of `'`, zero or more
  6581. characters not including `'`, and a final `'`.
  6582. A [double-quoted attribute value](@)
  6583. consists of `"`, zero or more
  6584. characters not including `"`, and a final `"`.
  6585. An [open tag](@) consists of a `<` character, a [tag name],
  6586. zero or more [attributes], optional [whitespace], an optional `/`
  6587. character, and a `>` character.
  6588. A [closing tag](@) consists of the string `</`, a
  6589. [tag name], optional [whitespace], and the character `>`.
  6590. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6591. where *text* does not start with `>` or `->`, does not end with `-`,
  6592. and does not contain `--`. (See the
  6593. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6594. A [processing instruction](@)
  6595. consists of the string `<?`, a string
  6596. of characters not including the string `?>`, and the string
  6597. `?>`.
  6598. A [declaration](@) consists of the
  6599. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6600. [whitespace], a string of characters not including the
  6601. character `>`, and the character `>`.
  6602. A [CDATA section](@) consists of
  6603. the string `<![CDATA[`, a string of characters not including the string
  6604. `]]>`, and the string `]]>`.
  6605. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6606. an [HTML comment], a [processing instruction], a [declaration],
  6607. or a [CDATA section].
  6608. Here are some simple open tags:
  6609. ```````````````````````````````` example
  6610. <a><bab><c2c>
  6611. .
  6612. <p><a><bab><c2c></p>
  6613. ````````````````````````````````
  6614. Empty elements:
  6615. ```````````````````````````````` example
  6616. <a/><b2/>
  6617. .
  6618. <p><a/><b2/></p>
  6619. ````````````````````````````````
  6620. [Whitespace] is allowed:
  6621. ```````````````````````````````` example
  6622. <a /><b2
  6623. data="foo" >
  6624. .
  6625. <p><a /><b2
  6626. data="foo" ></p>
  6627. ````````````````````````````````
  6628. With attributes:
  6629. ```````````````````````````````` example
  6630. <a foo="bar" bam = 'baz <em>"</em>'
  6631. _boolean zoop:33=zoop:33 />
  6632. .
  6633. <p><a foo="bar" bam = 'baz <em>"</em>'
  6634. _boolean zoop:33=zoop:33 /></p>
  6635. ````````````````````````````````
  6636. Custom tag names can be used:
  6637. ```````````````````````````````` example
  6638. Foo <responsive-image src="foo.jpg" />
  6639. .
  6640. <p>Foo <responsive-image src="foo.jpg" /></p>
  6641. ````````````````````````````````
  6642. Illegal tag names, not parsed as HTML:
  6643. ```````````````````````````````` example
  6644. <33> <__>
  6645. .
  6646. <p>&lt;33&gt; &lt;__&gt;</p>
  6647. ````````````````````````````````
  6648. Illegal attribute names:
  6649. ```````````````````````````````` example
  6650. <a h*#ref="hi">
  6651. .
  6652. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6653. ````````````````````````````````
  6654. Illegal attribute values:
  6655. ```````````````````````````````` example
  6656. <a href="hi'> <a href=hi'>
  6657. .
  6658. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6659. ````````````````````````````````
  6660. Illegal [whitespace]:
  6661. ```````````````````````````````` example
  6662. < a><
  6663. foo><bar/ >
  6664. <foo bar=baz
  6665. bim!bop />
  6666. .
  6667. <p>&lt; a&gt;&lt;
  6668. foo&gt;&lt;bar/ &gt;
  6669. &lt;foo bar=baz
  6670. bim!bop /&gt;</p>
  6671. ````````````````````````````````
  6672. Missing [whitespace]:
  6673. ```````````````````````````````` example
  6674. <a href='bar'title=title>
  6675. .
  6676. <p>&lt;a href='bar'title=title&gt;</p>
  6677. ````````````````````````````````
  6678. Closing tags:
  6679. ```````````````````````````````` example
  6680. </a></foo >
  6681. .
  6682. <p></a></foo ></p>
  6683. ````````````````````````````````
  6684. Illegal attributes in closing tag:
  6685. ```````````````````````````````` example
  6686. </a href="foo">
  6687. .
  6688. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6689. ````````````````````````````````
  6690. Comments:
  6691. ```````````````````````````````` example
  6692. foo <!-- this is a
  6693. comment - with hyphen -->
  6694. .
  6695. <p>foo <!-- this is a
  6696. comment - with hyphen --></p>
  6697. ````````````````````````````````
  6698. ```````````````````````````````` example
  6699. foo <!-- not a comment -- two hyphens -->
  6700. .
  6701. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6702. ````````````````````````````````
  6703. Not comments:
  6704. ```````````````````````````````` example
  6705. foo <!--> foo -->
  6706. foo <!-- foo--->
  6707. .
  6708. <p>foo &lt;!--&gt; foo --&gt;</p>
  6709. <p>foo &lt;!-- foo---&gt;</p>
  6710. ````````````````````````````````
  6711. Processing instructions:
  6712. ```````````````````````````````` example
  6713. foo <?php echo $a; ?>
  6714. .
  6715. <p>foo <?php echo $a; ?></p>
  6716. ````````````````````````````````
  6717. Declarations:
  6718. ```````````````````````````````` example
  6719. foo <!ELEMENT br EMPTY>
  6720. .
  6721. <p>foo <!ELEMENT br EMPTY></p>
  6722. ````````````````````````````````
  6723. CDATA sections:
  6724. ```````````````````````````````` example
  6725. foo <![CDATA[>&<]]>
  6726. .
  6727. <p>foo <![CDATA[>&<]]></p>
  6728. ````````````````````````````````
  6729. Entity and numeric character references are preserved in HTML
  6730. attributes:
  6731. ```````````````````````````````` example
  6732. foo <a href="&ouml;">
  6733. .
  6734. <p>foo <a href="&ouml;"></p>
  6735. ````````````````````````````````
  6736. Backslash escapes do not work in HTML attributes:
  6737. ```````````````````````````````` example
  6738. foo <a href="\*">
  6739. .
  6740. <p>foo <a href="\*"></p>
  6741. ````````````````````````````````
  6742. ```````````````````````````````` example
  6743. <a href="\"">
  6744. .
  6745. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6746. ````````````````````````````````
  6747. ## Hard line breaks
  6748. A line break (not in a code span or HTML tag) that is preceded
  6749. by two or more spaces and does not occur at the end of a block
  6750. is parsed as a [hard line break](@) (rendered
  6751. in HTML as a `<br />` tag):
  6752. ```````````````````````````````` example
  6753. foo
  6754. baz
  6755. .
  6756. <p>foo<br />
  6757. baz</p>
  6758. ````````````````````````````````
  6759. For a more visible alternative, a backslash before the
  6760. [line ending] may be used instead of two spaces:
  6761. ```````````````````````````````` example
  6762. foo\
  6763. baz
  6764. .
  6765. <p>foo<br />
  6766. baz</p>
  6767. ````````````````````````````````
  6768. More than two spaces can be used:
  6769. ```````````````````````````````` example
  6770. foo
  6771. baz
  6772. .
  6773. <p>foo<br />
  6774. baz</p>
  6775. ````````````````````````````````
  6776. Leading spaces at the beginning of the next line are ignored:
  6777. ```````````````````````````````` example
  6778. foo
  6779. bar
  6780. .
  6781. <p>foo<br />
  6782. bar</p>
  6783. ````````````````````````````````
  6784. ```````````````````````````````` example
  6785. foo\
  6786. bar
  6787. .
  6788. <p>foo<br />
  6789. bar</p>
  6790. ````````````````````````````````
  6791. Line breaks can occur inside emphasis, links, and other constructs
  6792. that allow inline content:
  6793. ```````````````````````````````` example
  6794. *foo
  6795. bar*
  6796. .
  6797. <p><em>foo<br />
  6798. bar</em></p>
  6799. ````````````````````````````````
  6800. ```````````````````````````````` example
  6801. *foo\
  6802. bar*
  6803. .
  6804. <p><em>foo<br />
  6805. bar</em></p>
  6806. ````````````````````````````````
  6807. Line breaks do not occur inside code spans
  6808. ```````````````````````````````` example
  6809. `code
  6810. span`
  6811. .
  6812. <p><code>code span</code></p>
  6813. ````````````````````````````````
  6814. ```````````````````````````````` example
  6815. `code\
  6816. span`
  6817. .
  6818. <p><code>code\ span</code></p>
  6819. ````````````````````````````````
  6820. or HTML tags:
  6821. ```````````````````````````````` example
  6822. <a href="foo
  6823. bar">
  6824. .
  6825. <p><a href="foo
  6826. bar"></p>
  6827. ````````````````````````````````
  6828. ```````````````````````````````` example
  6829. <a href="foo\
  6830. bar">
  6831. .
  6832. <p><a href="foo\
  6833. bar"></p>
  6834. ````````````````````````````````
  6835. Hard line breaks are for separating inline content within a block.
  6836. Neither syntax for hard line breaks works at the end of a paragraph or
  6837. other block element:
  6838. ```````````````````````````````` example
  6839. foo\
  6840. .
  6841. <p>foo\</p>
  6842. ````````````````````````````````
  6843. ```````````````````````````````` example
  6844. foo
  6845. .
  6846. <p>foo</p>
  6847. ````````````````````````````````
  6848. ```````````````````````````````` example
  6849. ### foo\
  6850. .
  6851. <h3>foo\</h3>
  6852. ````````````````````````````````
  6853. ```````````````````````````````` example
  6854. ### foo
  6855. .
  6856. <h3>foo</h3>
  6857. ````````````````````````````````
  6858. ## Soft line breaks
  6859. A regular line break (not in a code span or HTML tag) that is not
  6860. preceded by two or more spaces or a backslash is parsed as a
  6861. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6862. [line ending] or as a space. The result will be the same in
  6863. browsers. In the examples here, a [line ending] will be used.)
  6864. ```````````````````````````````` example
  6865. foo
  6866. baz
  6867. .
  6868. <p>foo
  6869. baz</p>
  6870. ````````````````````````````````
  6871. Spaces at the end of the line and beginning of the next line are
  6872. removed:
  6873. ```````````````````````````````` example
  6874. foo
  6875. baz
  6876. .
  6877. <p>foo
  6878. baz</p>
  6879. ````````````````````````````````
  6880. A conforming parser may render a soft line break in HTML either as a
  6881. line break or as a space.
  6882. A renderer may also provide an option to render soft line breaks
  6883. as hard line breaks.
  6884. ## Textual content
  6885. Any characters not given an interpretation by the above rules will
  6886. be parsed as plain textual content.
  6887. ```````````````````````````````` example
  6888. hello $.;'there
  6889. .
  6890. <p>hello $.;'there</p>
  6891. ````````````````````````````````
  6892. ```````````````````````````````` example
  6893. Foo χρῆν
  6894. .
  6895. <p>Foo χρῆν</p>
  6896. ````````````````````````````````
  6897. Internal spaces are preserved verbatim:
  6898. ```````````````````````````````` example
  6899. Multiple spaces
  6900. .
  6901. <p>Multiple spaces</p>
  6902. ````````````````````````````````
  6903. <!-- END TESTS -->
  6904. # Appendix: A parsing strategy
  6905. In this appendix we describe some features of the parsing strategy
  6906. used in the CommonMark reference implementations.
  6907. ## Overview
  6908. Parsing has two phases:
  6909. 1. In the first phase, lines of input are consumed and the block
  6910. structure of the document---its division into paragraphs, block quotes,
  6911. list items, and so on---is constructed. Text is assigned to these
  6912. blocks but not parsed. Link reference definitions are parsed and a
  6913. map of links is constructed.
  6914. 2. In the second phase, the raw text contents of paragraphs and headings
  6915. are parsed into sequences of Markdown inline elements (strings,
  6916. code spans, links, emphasis, and so on), using the map of link
  6917. references constructed in phase 1.
  6918. At each point in processing, the document is represented as a tree of
  6919. **blocks**. The root of the tree is a `document` block. The `document`
  6920. may have any number of other blocks as **children**. These children
  6921. may, in turn, have other blocks as children. The last child of a block
  6922. is normally considered **open**, meaning that subsequent lines of input
  6923. can alter its contents. (Blocks that are not open are **closed**.)
  6924. Here, for example, is a possible document tree, with the open blocks
  6925. marked by arrows:
  6926. ``` tree
  6927. -> document
  6928. -> block_quote
  6929. paragraph
  6930. "Lorem ipsum dolor\nsit amet."
  6931. -> list (type=bullet tight=true bullet_char=-)
  6932. list_item
  6933. paragraph
  6934. "Qui *quodsi iracundia*"
  6935. -> list_item
  6936. -> paragraph
  6937. "aliquando id"
  6938. ```
  6939. ## Phase 1: block structure
  6940. Each line that is processed has an effect on this tree. The line is
  6941. analyzed and, depending on its contents, the document may be altered
  6942. in one or more of the following ways:
  6943. 1. One or more open blocks may be closed.
  6944. 2. One or more new blocks may be created as children of the
  6945. last open block.
  6946. 3. Text may be added to the last (deepest) open block remaining
  6947. on the tree.
  6948. Once a line has been incorporated into the tree in this way,
  6949. it can be discarded, so input can be read in a stream.
  6950. For each line, we follow this procedure:
  6951. 1. First we iterate through the open blocks, starting with the
  6952. root document, and descending through last children down to the last
  6953. open block. Each block imposes a condition that the line must satisfy
  6954. if the block is to remain open. For example, a block quote requires a
  6955. `>` character. A paragraph requires a non-blank line.
  6956. In this phase we may match all or just some of the open
  6957. blocks. But we cannot close unmatched blocks yet, because we may have a
  6958. [lazy continuation line].
  6959. 2. Next, after consuming the continuation markers for existing
  6960. blocks, we look for new block starts (e.g. `>` for a block quote).
  6961. If we encounter a new block start, we close any blocks unmatched
  6962. in step 1 before creating the new block as a child of the last
  6963. matched block.
  6964. 3. Finally, we look at the remainder of the line (after block
  6965. markers like `>`, list markers, and indentation have been consumed).
  6966. This is text that can be incorporated into the last open
  6967. block (a paragraph, code block, heading, or raw HTML).
  6968. Setext headings are formed when we see a line of a paragraph
  6969. that is a [setext heading underline].
  6970. Reference link definitions are detected when a paragraph is closed;
  6971. the accumulated text lines are parsed to see if they begin with
  6972. one or more reference link definitions. Any remainder becomes a
  6973. normal paragraph.
  6974. We can see how this works by considering how the tree above is
  6975. generated by four lines of Markdown:
  6976. ``` markdown
  6977. > Lorem ipsum dolor
  6978. sit amet.
  6979. > - Qui *quodsi iracundia*
  6980. > - aliquando id
  6981. ```
  6982. At the outset, our document model is just
  6983. ``` tree
  6984. -> document
  6985. ```
  6986. The first line of our text,
  6987. ``` markdown
  6988. > Lorem ipsum dolor
  6989. ```
  6990. causes a `block_quote` block to be created as a child of our
  6991. open `document` block, and a `paragraph` block as a child of
  6992. the `block_quote`. Then the text is added to the last open
  6993. block, the `paragraph`:
  6994. ``` tree
  6995. -> document
  6996. -> block_quote
  6997. -> paragraph
  6998. "Lorem ipsum dolor"
  6999. ```
  7000. The next line,
  7001. ``` markdown
  7002. sit amet.
  7003. ```
  7004. is a "lazy continuation" of the open `paragraph`, so it gets added
  7005. to the paragraph's text:
  7006. ``` tree
  7007. -> document
  7008. -> block_quote
  7009. -> paragraph
  7010. "Lorem ipsum dolor\nsit amet."
  7011. ```
  7012. The third line,
  7013. ``` markdown
  7014. > - Qui *quodsi iracundia*
  7015. ```
  7016. causes the `paragraph` block to be closed, and a new `list` block
  7017. opened as a child of the `block_quote`. A `list_item` is also
  7018. added as a child of the `list`, and a `paragraph` as a child of
  7019. the `list_item`. The text is then added to the new `paragraph`:
  7020. ``` tree
  7021. -> document
  7022. -> block_quote
  7023. paragraph
  7024. "Lorem ipsum dolor\nsit amet."
  7025. -> list (type=bullet tight=true bullet_char=-)
  7026. -> list_item
  7027. -> paragraph
  7028. "Qui *quodsi iracundia*"
  7029. ```
  7030. The fourth line,
  7031. ``` markdown
  7032. > - aliquando id
  7033. ```
  7034. causes the `list_item` (and its child the `paragraph`) to be closed,
  7035. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7036. is added as a child of the new `list_item`, to contain the text.
  7037. We thus obtain the final tree:
  7038. ``` tree
  7039. -> document
  7040. -> block_quote
  7041. paragraph
  7042. "Lorem ipsum dolor\nsit amet."
  7043. -> list (type=bullet tight=true bullet_char=-)
  7044. list_item
  7045. paragraph
  7046. "Qui *quodsi iracundia*"
  7047. -> list_item
  7048. -> paragraph
  7049. "aliquando id"
  7050. ```
  7051. ## Phase 2: inline structure
  7052. Once all of the input has been parsed, all open blocks are closed.
  7053. We then "walk the tree," visiting every node, and parse raw
  7054. string contents of paragraphs and headings as inlines. At this
  7055. point we have seen all the link reference definitions, so we can
  7056. resolve reference links as we go.
  7057. ``` tree
  7058. document
  7059. block_quote
  7060. paragraph
  7061. str "Lorem ipsum dolor"
  7062. softbreak
  7063. str "sit amet."
  7064. list (type=bullet tight=true bullet_char=-)
  7065. list_item
  7066. paragraph
  7067. str "Qui "
  7068. emph
  7069. str "quodsi iracundia"
  7070. list_item
  7071. paragraph
  7072. str "aliquando id"
  7073. ```
  7074. Notice how the [line ending] in the first paragraph has
  7075. been parsed as a `softbreak`, and the asterisks in the first list item
  7076. have become an `emph`.
  7077. ### An algorithm for parsing nested emphasis and links
  7078. By far the trickiest part of inline parsing is handling emphasis,
  7079. strong emphasis, links, and images. This is done using the following
  7080. algorithm.
  7081. When we're parsing inlines and we hit either
  7082. - a run of `*` or `_` characters, or
  7083. - a `[` or `![`
  7084. we insert a text node with these symbols as its literal content, and we
  7085. add a pointer to this text node to the [delimiter stack](@).
  7086. The [delimiter stack] is a doubly linked list. Each
  7087. element contains a pointer to a text node, plus information about
  7088. - the type of delimiter (`[`, `![`, `*`, `_`)
  7089. - the number of delimiters,
  7090. - whether the delimiter is "active" (all are active to start), and
  7091. - whether the delimiter is a potential opener, a potential closer,
  7092. or both (which depends on what sort of characters precede
  7093. and follow the delimiters).
  7094. When we hit a `]` character, we call the *look for link or image*
  7095. procedure (see below).
  7096. When we hit the end of the input, we call the *process emphasis*
  7097. procedure (see below), with `stack_bottom` = NULL.
  7098. #### *look for link or image*
  7099. Starting at the top of the delimiter stack, we look backwards
  7100. through the stack for an opening `[` or `![` delimiter.
  7101. - If we don't find one, we return a literal text node `]`.
  7102. - If we do find one, but it's not *active*, we remove the inactive
  7103. delimiter from the stack, and return a literal text node `]`.
  7104. - If we find one and it's active, then we parse ahead to see if
  7105. we have an inline link/image, reference link/image, compact reference
  7106. link/image, or shortcut reference link/image.
  7107. + If we don't, then we remove the opening delimiter from the
  7108. delimiter stack and return a literal text node `]`.
  7109. + If we do, then
  7110. * We return a link or image node whose children are the inlines
  7111. after the text node pointed to by the opening delimiter.
  7112. * We run *process emphasis* on these inlines, with the `[` opener
  7113. as `stack_bottom`.
  7114. * We remove the opening delimiter.
  7115. * If we have a link (and not an image), we also set all
  7116. `[` delimiters before the opening delimiter to *inactive*. (This
  7117. will prevent us from getting links within links.)
  7118. #### *process emphasis*
  7119. Parameter `stack_bottom` sets a lower bound to how far we
  7120. descend in the [delimiter stack]. If it is NULL, we can
  7121. go all the way to the bottom. Otherwise, we stop before
  7122. visiting `stack_bottom`.
  7123. Let `current_position` point to the element on the [delimiter stack]
  7124. just above `stack_bottom` (or the first element if `stack_bottom`
  7125. is NULL).
  7126. We keep track of the `openers_bottom` for each delimiter
  7127. type (`*`, `_`). Initialize this to `stack_bottom`.
  7128. Then we repeat the following until we run out of potential
  7129. closers:
  7130. - Move `current_position` forward in the delimiter stack (if needed)
  7131. until we find the first potential closer with delimiter `*` or `_`.
  7132. (This will be the potential closer closest
  7133. to the beginning of the input -- the first one in parse order.)
  7134. - Now, look back in the stack (staying above `stack_bottom` and
  7135. the `openers_bottom` for this delimiter type) for the
  7136. first matching potential opener ("matching" means same delimiter).
  7137. - If one is found:
  7138. + Figure out whether we have emphasis or strong emphasis:
  7139. if both closer and opener spans have length >= 2, we have
  7140. strong, otherwise regular.
  7141. + Insert an emph or strong emph node accordingly, after
  7142. the text node corresponding to the opener.
  7143. + Remove any delimiters between the opener and closer from
  7144. the delimiter stack.
  7145. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7146. from the opening and closing text nodes. If they become empty
  7147. as a result, remove them and remove the corresponding element
  7148. of the delimiter stack. If the closing node is removed, reset
  7149. `current_position` to the next element in the stack.
  7150. - If none in found:
  7151. + Set `openers_bottom` to the element before `current_position`.
  7152. (We know that there are no openers for this kind of closer up to and
  7153. including this point, so this puts a lower bound on future searches.)
  7154. + If the closer at `current_position` is not a potential opener,
  7155. remove it from the delimiter stack (since we know it can't
  7156. be a closer either).
  7157. + Advance `current_position` to the next element in the stack.
  7158. After we're done, we remove all delimiters above `stack_bottom` from the
  7159. delimiter stack.