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