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