aboutsummaryrefslogtreecommitdiff
path: root/spec.txt
blob: 63843909ad18691005ab699e28c871a42e8aecdd (plain)
  1. ---
  2. title: CommonMark Spec
  3. author: John MacFarlane
  4. version: 0.28
  5. date: '2017-08-01'
  6. license: '[CC-BY-SA 4.0](http://creativecommons.org/licenses/by-sa/4.0/)'
  7. ...
  8. # Introduction
  9. ## What is Markdown?
  10. Markdown is a plain text format for writing structured documents,
  11. based on conventions for indicating formatting in email
  12. and usenet posts. It was developed by John Gruber (with
  13. help from Aaron Swartz) and released in 2004 in the form of a
  14. [syntax description](http://daringfireball.net/projects/markdown/syntax)
  15. and a Perl script (`Markdown.pl`) for converting Markdown to
  16. HTML. In the next decade, dozens of implementations were
  17. developed in many languages. Some extended the original
  18. Markdown syntax with conventions for footnotes, tables, and
  19. other document elements. Some allowed Markdown documents to be
  20. rendered in formats other than HTML. Websites like Reddit,
  21. StackOverflow, and GitHub had millions of people using Markdown.
  22. And Markdown started to be used beyond the web, to author books,
  23. articles, slide shows, letters, and lecture notes.
  24. What distinguishes Markdown from many other lightweight markup
  25. syntaxes, which are often easier to write, is its readability.
  26. As Gruber writes:
  27. > The overriding design goal for Markdown's formatting syntax is
  28. > to make it as readable as possible. The idea is that a
  29. > Markdown-formatted document should be publishable as-is, as
  30. > plain text, without looking like it's been marked up with tags
  31. > or formatting instructions.
  32. > (<http://daringfireball.net/projects/markdown/>)
  33. The point can be illustrated by comparing a sample of
  34. [AsciiDoc](http://www.methods.co.nz/asciidoc/) with
  35. an equivalent sample of Markdown. Here is a sample of
  36. AsciiDoc from the AsciiDoc manual:
  37. ```
  38. 1. List item one.
  39. +
  40. List item one continued with a second paragraph followed by an
  41. Indented block.
  42. +
  43. .................
  44. $ ls *.sh
  45. $ mv *.sh ~/tmp
  46. .................
  47. +
  48. List item continued with a third paragraph.
  49. 2. List item two continued with an open block.
  50. +
  51. --
  52. This paragraph is part of the preceding list item.
  53. a. This list is nested and does not require explicit item
  54. continuation.
  55. +
  56. This paragraph is part of the preceding list item.
  57. b. List item b.
  58. This paragraph belongs to item two of the outer list.
  59. --
  60. ```
  61. And here is the equivalent in Markdown:
  62. ```
  63. 1. List item one.
  64. List item one continued with a second paragraph followed by an
  65. Indented block.
  66. $ ls *.sh
  67. $ mv *.sh ~/tmp
  68. List item continued with a third paragraph.
  69. 2. List item two continued with an open block.
  70. This paragraph is part of the preceding list item.
  71. 1. This list is nested and does not require explicit item continuation.
  72. This paragraph is part of the preceding list item.
  73. 2. List item b.
  74. This paragraph belongs to item two of the outer list.
  75. ```
  76. The AsciiDoc version is, arguably, easier to write. You don't need
  77. to worry about indentation. But the Markdown version is much easier
  78. to read. The nesting of list items is apparent to the eye in the
  79. source, not just in the processed document.
  80. ## Why is a spec needed?
  81. John Gruber's [canonical description of Markdown's
  82. syntax](http://daringfireball.net/projects/markdown/syntax)
  83. does not specify the syntax unambiguously. Here are some examples of
  84. questions it does not answer:
  85. 1. How much indentation is needed for a sublist? The spec says that
  86. continuation paragraphs need to be indented four spaces, but is
  87. not fully explicit about sublists. It is natural to think that
  88. they, too, must be indented four spaces, but `Markdown.pl` does
  89. not require that. This is hardly a "corner case," and divergences
  90. between implementations on this issue often lead to surprises for
  91. users in real documents. (See [this comment by John
  92. Gruber](http://article.gmane.org/gmane.text.markdown.general/1997).)
  93. 2. Is a blank line needed before a block quote or heading?
  94. Most implementations do not require the blank line. However,
  95. this can lead to unexpected results in hard-wrapped text, and
  96. also to ambiguities in parsing (note that some implementations
  97. put the heading inside the blockquote, while others do not).
  98. (John Gruber has also spoken [in favor of requiring the blank
  99. lines](http://article.gmane.org/gmane.text.markdown.general/2146).)
  100. 3. Is a blank line needed before an indented code block?
  101. (`Markdown.pl` requires it, but this is not mentioned in the
  102. documentation, and some implementations do not require it.)
  103. ``` markdown
  104. paragraph
  105. code?
  106. ```
  107. 4. What is the exact rule for determining when list items get
  108. wrapped in `<p>` tags? Can a list be partially "loose" and partially
  109. "tight"? What should we do with a list like this?
  110. ``` markdown
  111. 1. one
  112. 2. two
  113. 3. three
  114. ```
  115. Or this?
  116. ``` markdown
  117. 1. one
  118. - a
  119. - b
  120. 2. two
  121. ```
  122. (There are some relevant comments by John Gruber
  123. [here](http://article.gmane.org/gmane.text.markdown.general/2554).)
  124. 5. Can list markers be indented? Can ordered list markers be right-aligned?
  125. ``` markdown
  126. 8. item 1
  127. 9. item 2
  128. 10. item 2a
  129. ```
  130. 6. Is this one list with a thematic break in its second item,
  131. or two lists separated by a thematic break?
  132. ``` markdown
  133. * a
  134. * * * * *
  135. * b
  136. ```
  137. 7. When list markers change from numbers to bullets, do we have
  138. two lists or one? (The Markdown syntax description suggests two,
  139. but the perl scripts and many other implementations produce one.)
  140. ``` markdown
  141. 1. fee
  142. 2. fie
  143. - foe
  144. - fum
  145. ```
  146. 8. What are the precedence rules for the markers of inline structure?
  147. For example, is the following a valid link, or does the code span
  148. take precedence ?
  149. ``` markdown
  150. [a backtick (`)](/url) and [another backtick (`)](/url).
  151. ```
  152. 9. What are the precedence rules for markers of emphasis and strong
  153. emphasis? For example, how should the following be parsed?
  154. ``` markdown
  155. *foo *bar* baz*
  156. ```
  157. 10. What are the precedence rules between block-level and inline-level
  158. structure? For example, how should the following be parsed?
  159. ``` markdown
  160. - `a long code span can contain a hyphen like this
  161. - and it can screw things up`
  162. ```
  163. 11. Can list items include section headings? (`Markdown.pl` does not
  164. allow this, but does allow blockquotes to include headings.)
  165. ``` markdown
  166. - # Heading
  167. ```
  168. 12. Can list items be empty?
  169. ``` markdown
  170. * a
  171. *
  172. * b
  173. ```
  174. 13. Can link references be defined inside block quotes or list items?
  175. ``` markdown
  176. > Blockquote [foo].
  177. >
  178. > [foo]: /url
  179. ```
  180. 14. If there are multiple definitions for the same reference, which takes
  181. precedence?
  182. ``` markdown
  183. [foo]: /url1
  184. [foo]: /url2
  185. [foo][]
  186. ```
  187. In the absence of a spec, early implementers consulted `Markdown.pl`
  188. to resolve these ambiguities. But `Markdown.pl` was quite buggy, and
  189. gave manifestly bad results in many cases, so it was not a
  190. satisfactory replacement for a spec.
  191. Because there is no unambiguous spec, implementations have diverged
  192. considerably. As a result, users are often surprised to find that
  193. a document that renders one way on one system (say, a GitHub wiki)
  194. renders differently on another (say, converting to docbook using
  195. pandoc). To make matters worse, because nothing in Markdown counts
  196. as a "syntax error," the divergence often isn't discovered right away.
  197. ## About this document
  198. This document attempts to specify Markdown syntax unambiguously.
  199. It contains many examples with side-by-side Markdown and
  200. HTML. These are intended to double as conformance tests. An
  201. accompanying script `spec_tests.py` can be used to run the tests
  202. against any Markdown program:
  203. python test/spec_tests.py --spec spec.txt --program PROGRAM
  204. Since this document describes how Markdown is to be parsed into
  205. an abstract syntax tree, it would have made sense to use an abstract
  206. representation of the syntax tree instead of HTML. But HTML is capable
  207. of representing the structural distinctions we need to make, and the
  208. choice of HTML for the tests makes it possible to run the tests against
  209. an implementation without writing an abstract syntax tree renderer.
  210. This document is generated from a text file, `spec.txt`, written
  211. in Markdown with a small extension for the side-by-side tests.
  212. The script `tools/makespec.py` can be used to convert `spec.txt` into
  213. HTML or CommonMark (which can then be converted into other formats).
  214. In the examples, the `→` character is used to represent tabs.
  215. # Preliminaries
  216. ## Characters and lines
  217. Any sequence of [characters] is a valid CommonMark
  218. document.
  219. A [character](@) is a Unicode code point. Although some
  220. code points (for example, combining accents) do not correspond to
  221. characters in an intuitive sense, all code points count as characters
  222. for purposes of this spec.
  223. This spec does not specify an encoding; it thinks of lines as composed
  224. of [characters] rather than bytes. A conforming parser may be limited
  225. to a certain encoding.
  226. A [line](@) is a sequence of zero or more [characters]
  227. other than newline (`U+000A`) or carriage return (`U+000D`),
  228. followed by a [line ending] or by the end of file.
  229. A [line ending](@) is a newline (`U+000A`), a carriage return
  230. (`U+000D`) not followed by a newline, or a carriage return and a
  231. following newline.
  232. A line containing no characters, or a line containing only spaces
  233. (`U+0020`) or tabs (`U+0009`), is called a [blank line](@).
  234. The following definitions of character classes will be used in this spec:
  235. A [whitespace character](@) is a space
  236. (`U+0020`), tab (`U+0009`), newline (`U+000A`), line tabulation (`U+000B`),
  237. form feed (`U+000C`), or carriage return (`U+000D`).
  238. [Whitespace](@) is a sequence of one or more [whitespace
  239. characters].
  240. A [Unicode whitespace character](@) is
  241. any code point in the Unicode `Zs` general category, or a tab (`U+0009`),
  242. carriage return (`U+000D`), newline (`U+000A`), or form feed
  243. (`U+000C`).
  244. [Unicode whitespace](@) is a sequence of one
  245. or more [Unicode whitespace characters].
  246. A [space](@) is `U+0020`.
  247. A [non-whitespace character](@) is any character
  248. that is not a [whitespace character].
  249. An [ASCII punctuation character](@)
  250. is `!`, `"`, `#`, `$`, `%`, `&`, `'`, `(`, `)`,
  251. `*`, `+`, `,`, `-`, `.`, `/` (U+0021–2F),
  252. `:`, `;`, `<`, `=`, `>`, `?`, `@` (U+003A–0040),
  253. `[`, `\`, `]`, `^`, `_`, `` ` `` (U+005B–0060),
  254. `{`, `|`, `}`, or `~` (U+007B–007E).
  255. A [punctuation character](@) is an [ASCII
  256. punctuation character] or anything in
  257. the general Unicode categories `Pc`, `Pd`, `Pe`, `Pf`, `Pi`, `Po`, or `Ps`.
  258. ## Tabs
  259. Tabs in lines are not expanded to [spaces]. However,
  260. in contexts where whitespace helps to define block structure,
  261. tabs behave as if they were replaced by spaces with a tab stop
  262. of 4 characters.
  263. Thus, for example, a tab can be used instead of four spaces
  264. in an indented code block. (Note, however, that internal
  265. tabs are passed through as literal tabs, not expanded to
  266. spaces.)
  267. ```````````````````````````````` example
  268. →foo→baz→→bim
  269. .
  270. <pre><code>foo→baz→→bim
  271. </code></pre>
  272. ````````````````````````````````
  273. ```````````````````````````````` example
  274. →foo→baz→→bim
  275. .
  276. <pre><code>foo→baz→→bim
  277. </code></pre>
  278. ````````````````````````````````
  279. ```````````````````````````````` example
  280. a→a
  281. ὐ→a
  282. .
  283. <pre><code>a→a
  284. ὐ→a
  285. </code></pre>
  286. ````````````````````````````````
  287. In the following example, a continuation paragraph of a list
  288. item is indented with a tab; this has exactly the same effect
  289. as indentation with four spaces would:
  290. ```````````````````````````````` example
  291. - foo
  292. →bar
  293. .
  294. <ul>
  295. <li>
  296. <p>foo</p>
  297. <p>bar</p>
  298. </li>
  299. </ul>
  300. ````````````````````````````````
  301. ```````````````````````````````` example
  302. - foo
  303. →→bar
  304. .
  305. <ul>
  306. <li>
  307. <p>foo</p>
  308. <pre><code> bar
  309. </code></pre>
  310. </li>
  311. </ul>
  312. ````````````````````````````````
  313. Normally the `>` that begins a block quote may be followed
  314. optionally by a space, which is not considered part of the
  315. content. In the following case `>` is followed by a tab,
  316. which is treated as if it were expanded into three spaces.
  317. Since one of these spaces is considered part of the
  318. delimiter, `foo` is considered to be indented six spaces
  319. inside the block quote context, so we get an indented
  320. code block starting with two spaces.
  321. ```````````````````````````````` example
  322. >→→foo
  323. .
  324. <blockquote>
  325. <pre><code> foo
  326. </code></pre>
  327. </blockquote>
  328. ````````````````````````````````
  329. ```````````````````````````````` example
  330. -→→foo
  331. .
  332. <ul>
  333. <li>
  334. <pre><code> foo
  335. </code></pre>
  336. </li>
  337. </ul>
  338. ````````````````````````````````
  339. ```````````````````````````````` example
  340. foo
  341. →bar
  342. .
  343. <pre><code>foo
  344. bar
  345. </code></pre>
  346. ````````````````````````````````
  347. ```````````````````````````````` example
  348. - foo
  349. - bar
  350. → - baz
  351. .
  352. <ul>
  353. <li>foo
  354. <ul>
  355. <li>bar
  356. <ul>
  357. <li>baz</li>
  358. </ul>
  359. </li>
  360. </ul>
  361. </li>
  362. </ul>
  363. ````````````````````````````````
  364. ```````````````````````````````` example
  365. #→Foo
  366. .
  367. <h1>Foo</h1>
  368. ````````````````````````````````
  369. ```````````````````````````````` example
  370. *→*→*→
  371. .
  372. <hr />
  373. ````````````````````````````````
  374. ## Insecure characters
  375. For security reasons, the Unicode character `U+0000` must be replaced
  376. with the REPLACEMENT CHARACTER (`U+FFFD`).
  377. # Blocks and inlines
  378. We can think of a document as a sequence of
  379. [blocks](@)---structural elements like paragraphs, block
  380. quotations, lists, headings, rules, and code blocks. Some blocks (like
  381. block quotes and list items) contain other blocks; others (like
  382. headings and paragraphs) contain [inline](@) content---text,
  383. links, emphasized text, images, code spans, and so on.
  384. ## Precedence
  385. Indicators of block structure always take precedence over indicators
  386. of inline structure. So, for example, the following is a list with
  387. two items, not a list with one item containing a code span:
  388. ```````````````````````````````` example
  389. - `one
  390. - two`
  391. .
  392. <ul>
  393. <li>`one</li>
  394. <li>two`</li>
  395. </ul>
  396. ````````````````````````````````
  397. This means that parsing can proceed in two steps: first, the block
  398. structure of the document can be discerned; second, text lines inside
  399. paragraphs, headings, and other block constructs can be parsed for inline
  400. structure. The second step requires information about link reference
  401. definitions that will be available only at the end of the first
  402. step. Note that the first step requires processing lines in sequence,
  403. but the second can be parallelized, since the inline parsing of
  404. one block element does not affect the inline parsing of any other.
  405. ## Container blocks and leaf blocks
  406. We can divide blocks into two types:
  407. [container blocks](@),
  408. which can contain other blocks, and [leaf blocks](@),
  409. which cannot.
  410. # Leaf blocks
  411. This section describes the different kinds of leaf block that make up a
  412. Markdown document.
  413. ## Thematic breaks
  414. A line consisting of 0-3 spaces of indentation, followed by a sequence
  415. of three or more matching `-`, `_`, or `*` characters, each followed
  416. optionally by any number of spaces or tabs, forms a
  417. [thematic break](@).
  418. ```````````````````````````````` example
  419. ***
  420. ---
  421. ___
  422. .
  423. <hr />
  424. <hr />
  425. <hr />
  426. ````````````````````````````````
  427. Wrong characters:
  428. ```````````````````````````````` example
  429. +++
  430. .
  431. <p>+++</p>
  432. ````````````````````````````````
  433. ```````````````````````````````` example
  434. ===
  435. .
  436. <p>===</p>
  437. ````````````````````````````````
  438. Not enough characters:
  439. ```````````````````````````````` example
  440. --
  441. **
  442. __
  443. .
  444. <p>--
  445. **
  446. __</p>
  447. ````````````````````````````````
  448. One to three spaces indent are allowed:
  449. ```````````````````````````````` example
  450. ***
  451. ***
  452. ***
  453. .
  454. <hr />
  455. <hr />
  456. <hr />
  457. ````````````````````````````````
  458. Four spaces is too many:
  459. ```````````````````````````````` example
  460. ***
  461. .
  462. <pre><code>***
  463. </code></pre>
  464. ````````````````````````````````
  465. ```````````````````````````````` example
  466. Foo
  467. ***
  468. .
  469. <p>Foo
  470. ***</p>
  471. ````````````````````````````````
  472. More than three characters may be used:
  473. ```````````````````````````````` example
  474. _____________________________________
  475. .
  476. <hr />
  477. ````````````````````````````````
  478. Spaces are allowed between the characters:
  479. ```````````````````````````````` example
  480. - - -
  481. .
  482. <hr />
  483. ````````````````````````````````
  484. ```````````````````````````````` example
  485. ** * ** * ** * **
  486. .
  487. <hr />
  488. ````````````````````````````````
  489. ```````````````````````````````` example
  490. - - - -
  491. .
  492. <hr />
  493. ````````````````````````````````
  494. Spaces are allowed at the end:
  495. ```````````````````````````````` example
  496. - - - -
  497. .
  498. <hr />
  499. ````````````````````````````````
  500. However, no other characters may occur in the line:
  501. ```````````````````````````````` example
  502. _ _ _ _ a
  503. a------
  504. ---a---
  505. .
  506. <p>_ _ _ _ a</p>
  507. <p>a------</p>
  508. <p>---a---</p>
  509. ````````````````````````````````
  510. It is required that all of the [non-whitespace characters] be the same.
  511. So, this is not a thematic break:
  512. ```````````````````````````````` example
  513. *-*
  514. .
  515. <p><em>-</em></p>
  516. ````````````````````````````````
  517. Thematic breaks do not need blank lines before or after:
  518. ```````````````````````````````` example
  519. - foo
  520. ***
  521. - bar
  522. .
  523. <ul>
  524. <li>foo</li>
  525. </ul>
  526. <hr />
  527. <ul>
  528. <li>bar</li>
  529. </ul>
  530. ````````````````````````````````
  531. Thematic breaks can interrupt a paragraph:
  532. ```````````````````````````````` example
  533. Foo
  534. ***
  535. bar
  536. .
  537. <p>Foo</p>
  538. <hr />
  539. <p>bar</p>
  540. ````````````````````````````````
  541. If a line of dashes that meets the above conditions for being a
  542. thematic break could also be interpreted as the underline of a [setext
  543. heading], the interpretation as a
  544. [setext heading] takes precedence. Thus, for example,
  545. this is a setext heading, not a paragraph followed by a thematic break:
  546. ```````````````````````````````` example
  547. Foo
  548. ---
  549. bar
  550. .
  551. <h2>Foo</h2>
  552. <p>bar</p>
  553. ````````````````````````````````
  554. When both a thematic break and a list item are possible
  555. interpretations of a line, the thematic break takes precedence:
  556. ```````````````````````````````` example
  557. * Foo
  558. * * *
  559. * Bar
  560. .
  561. <ul>
  562. <li>Foo</li>
  563. </ul>
  564. <hr />
  565. <ul>
  566. <li>Bar</li>
  567. </ul>
  568. ````````````````````````````````
  569. If you want a thematic break in a list item, use a different bullet:
  570. ```````````````````````````````` example
  571. - Foo
  572. - * * *
  573. .
  574. <ul>
  575. <li>Foo</li>
  576. <li>
  577. <hr />
  578. </li>
  579. </ul>
  580. ````````````````````````````````
  581. ## ATX headings
  582. An [ATX heading](@)
  583. consists of a string of characters, parsed as inline content, between an
  584. opening sequence of 1--6 unescaped `#` characters and an optional
  585. closing sequence of any number of unescaped `#` characters.
  586. The opening sequence of `#` characters must be followed by a
  587. [space] or by the end of line. The optional closing sequence of `#`s must be
  588. preceded by a [space] and may be followed by spaces only. The opening
  589. `#` character may be indented 0-3 spaces. The raw contents of the
  590. heading are stripped of leading and trailing spaces before being parsed
  591. as inline content. The heading level is equal to the number of `#`
  592. characters in the opening sequence.
  593. Simple headings:
  594. ```````````````````````````````` example
  595. # foo
  596. ## foo
  597. ### foo
  598. #### foo
  599. ##### foo
  600. ###### foo
  601. .
  602. <h1>foo</h1>
  603. <h2>foo</h2>
  604. <h3>foo</h3>
  605. <h4>foo</h4>
  606. <h5>foo</h5>
  607. <h6>foo</h6>
  608. ````````````````````````````````
  609. More than six `#` characters is not a heading:
  610. ```````````````````````````````` example
  611. ####### foo
  612. .
  613. <p>####### foo</p>
  614. ````````````````````````````````
  615. At least one space is required between the `#` characters and the
  616. heading's contents, unless the heading is empty. Note that many
  617. implementations currently do not require the space. However, the
  618. space was required by the
  619. [original ATX implementation](http://www.aaronsw.com/2002/atx/atx.py),
  620. and it helps prevent things like the following from being parsed as
  621. headings:
  622. ```````````````````````````````` example
  623. #5 bolt
  624. #hashtag
  625. .
  626. <p>#5 bolt</p>
  627. <p>#hashtag</p>
  628. ````````````````````````````````
  629. This is not a heading, because the first `#` is escaped:
  630. ```````````````````````````````` example
  631. \## foo
  632. .
  633. <p>## foo</p>
  634. ````````````````````````````````
  635. Contents are parsed as inlines:
  636. ```````````````````````````````` example
  637. # foo *bar* \*baz\*
  638. .
  639. <h1>foo <em>bar</em> *baz*</h1>
  640. ````````````````````````````````
  641. Leading and trailing blanks are ignored in parsing inline content:
  642. ```````````````````````````````` example
  643. # foo
  644. .
  645. <h1>foo</h1>
  646. ````````````````````````````````
  647. One to three spaces indentation are allowed:
  648. ```````````````````````````````` example
  649. ### foo
  650. ## foo
  651. # foo
  652. .
  653. <h3>foo</h3>
  654. <h2>foo</h2>
  655. <h1>foo</h1>
  656. ````````````````````````````````
  657. Four spaces are too much:
  658. ```````````````````````````````` example
  659. # foo
  660. .
  661. <pre><code># foo
  662. </code></pre>
  663. ````````````````````````````````
  664. ```````````````````````````````` example
  665. foo
  666. # bar
  667. .
  668. <p>foo
  669. # bar</p>
  670. ````````````````````````````````
  671. A closing sequence of `#` characters is optional:
  672. ```````````````````````````````` example
  673. ## foo ##
  674. ### bar ###
  675. .
  676. <h2>foo</h2>
  677. <h3>bar</h3>
  678. ````````````````````````````````
  679. It need not be the same length as the opening sequence:
  680. ```````````````````````````````` example
  681. # foo ##################################
  682. ##### foo ##
  683. .
  684. <h1>foo</h1>
  685. <h5>foo</h5>
  686. ````````````````````````````````
  687. Spaces are allowed after the closing sequence:
  688. ```````````````````````````````` example
  689. ### foo ###
  690. .
  691. <h3>foo</h3>
  692. ````````````````````````````````
  693. A sequence of `#` characters with anything but [spaces] following it
  694. is not a closing sequence, but counts as part of the contents of the
  695. heading:
  696. ```````````````````````````````` example
  697. ### foo ### b
  698. .
  699. <h3>foo ### b</h3>
  700. ````````````````````````````````
  701. The closing sequence must be preceded by a space:
  702. ```````````````````````````````` example
  703. # foo#
  704. .
  705. <h1>foo#</h1>
  706. ````````````````````````````````
  707. Backslash-escaped `#` characters do not count as part
  708. of the closing sequence:
  709. ```````````````````````````````` example
  710. ### foo \###
  711. ## foo #\##
  712. # foo \#
  713. .
  714. <h3>foo ###</h3>
  715. <h2>foo ###</h2>
  716. <h1>foo #</h1>
  717. ````````````````````````````````
  718. ATX headings need not be separated from surrounding content by blank
  719. lines, and they can interrupt paragraphs:
  720. ```````````````````````````````` example
  721. ****
  722. ## foo
  723. ****
  724. .
  725. <hr />
  726. <h2>foo</h2>
  727. <hr />
  728. ````````````````````````````````
  729. ```````````````````````````````` example
  730. Foo bar
  731. # baz
  732. Bar foo
  733. .
  734. <p>Foo bar</p>
  735. <h1>baz</h1>
  736. <p>Bar foo</p>
  737. ````````````````````````````````
  738. ATX headings can be empty:
  739. ```````````````````````````````` example
  740. ##
  741. #
  742. ### ###
  743. .
  744. <h2></h2>
  745. <h1></h1>
  746. <h3></h3>
  747. ````````````````````````````````
  748. ## Setext headings
  749. A [setext heading](@) consists of one or more
  750. lines of text, each containing at least one [non-whitespace
  751. character], with no more than 3 spaces indentation, followed by
  752. a [setext heading underline]. The lines of text must be such
  753. that, were they not followed by the setext heading underline,
  754. they would be interpreted as a paragraph: they cannot be
  755. interpretable as a [code fence], [ATX heading][ATX headings],
  756. [block quote][block quotes], [thematic break][thematic breaks],
  757. [list item][list items], or [HTML block][HTML blocks].
  758. A [setext heading underline](@) is a sequence of
  759. `=` characters or a sequence of `-` characters, with no more than 3
  760. spaces indentation and any number of trailing spaces. If a line
  761. containing a single `-` can be interpreted as an
  762. empty [list items], it should be interpreted this way
  763. and not as a [setext heading underline].
  764. The heading is a level 1 heading if `=` characters are used in
  765. the [setext heading underline], and a level 2 heading if `-`
  766. characters are used. The contents of the heading are the result
  767. of parsing the preceding lines of text as CommonMark inline
  768. content.
  769. In general, a setext heading need not be preceded or followed by a
  770. blank line. However, it cannot interrupt a paragraph, so when a
  771. setext heading comes after a paragraph, a blank line is needed between
  772. them.
  773. Simple examples:
  774. ```````````````````````````````` example
  775. Foo *bar*
  776. =========
  777. Foo *bar*
  778. ---------
  779. .
  780. <h1>Foo <em>bar</em></h1>
  781. <h2>Foo <em>bar</em></h2>
  782. ````````````````````````````````
  783. The content of the header may span more than one line:
  784. ```````````````````````````````` example
  785. Foo *bar
  786. baz*
  787. ====
  788. .
  789. <h1>Foo <em>bar
  790. baz</em></h1>
  791. ````````````````````````````````
  792. The underlining can be any length:
  793. ```````````````````````````````` example
  794. Foo
  795. -------------------------
  796. Foo
  797. =
  798. .
  799. <h2>Foo</h2>
  800. <h1>Foo</h1>
  801. ````````````````````````````````
  802. The heading content can be indented up to three spaces, and need
  803. not line up with the underlining:
  804. ```````````````````````````````` example
  805. Foo
  806. ---
  807. Foo
  808. -----
  809. Foo
  810. ===
  811. .
  812. <h2>Foo</h2>
  813. <h2>Foo</h2>
  814. <h1>Foo</h1>
  815. ````````````````````````````````
  816. Four spaces indent is too much:
  817. ```````````````````````````````` example
  818. Foo
  819. ---
  820. Foo
  821. ---
  822. .
  823. <pre><code>Foo
  824. ---
  825. Foo
  826. </code></pre>
  827. <hr />
  828. ````````````````````````````````
  829. The setext heading underline can be indented up to three spaces, and
  830. may have trailing spaces:
  831. ```````````````````````````````` example
  832. Foo
  833. ----
  834. .
  835. <h2>Foo</h2>
  836. ````````````````````````````````
  837. Four spaces is too much:
  838. ```````````````````````````````` example
  839. Foo
  840. ---
  841. .
  842. <p>Foo
  843. ---</p>
  844. ````````````````````````````````
  845. The setext heading underline cannot contain internal spaces:
  846. ```````````````````````````````` example
  847. Foo
  848. = =
  849. Foo
  850. --- -
  851. .
  852. <p>Foo
  853. = =</p>
  854. <p>Foo</p>
  855. <hr />
  856. ````````````````````````````````
  857. Trailing spaces in the content line do not cause a line break:
  858. ```````````````````````````````` example
  859. Foo
  860. -----
  861. .
  862. <h2>Foo</h2>
  863. ````````````````````````````````
  864. Nor does a backslash at the end:
  865. ```````````````````````````````` example
  866. Foo\
  867. ----
  868. .
  869. <h2>Foo\</h2>
  870. ````````````````````````````````
  871. Since indicators of block structure take precedence over
  872. indicators of inline structure, the following are setext headings:
  873. ```````````````````````````````` example
  874. `Foo
  875. ----
  876. `
  877. <a title="a lot
  878. ---
  879. of dashes"/>
  880. .
  881. <h2>`Foo</h2>
  882. <p>`</p>
  883. <h2>&lt;a title=&quot;a lot</h2>
  884. <p>of dashes&quot;/&gt;</p>
  885. ````````````````````````````````
  886. The setext heading underline cannot be a [lazy continuation
  887. line] in a list item or block quote:
  888. ```````````````````````````````` example
  889. > Foo
  890. ---
  891. .
  892. <blockquote>
  893. <p>Foo</p>
  894. </blockquote>
  895. <hr />
  896. ````````````````````````````````
  897. ```````````````````````````````` example
  898. > foo
  899. bar
  900. ===
  901. .
  902. <blockquote>
  903. <p>foo
  904. bar
  905. ===</p>
  906. </blockquote>
  907. ````````````````````````````````
  908. ```````````````````````````````` example
  909. - Foo
  910. ---
  911. .
  912. <ul>
  913. <li>Foo</li>
  914. </ul>
  915. <hr />
  916. ````````````````````````````````
  917. A blank line is needed between a paragraph and a following
  918. setext heading, since otherwise the paragraph becomes part
  919. of the heading's content:
  920. ```````````````````````````````` example
  921. Foo
  922. Bar
  923. ---
  924. .
  925. <h2>Foo
  926. Bar</h2>
  927. ````````````````````````````````
  928. But in general a blank line is not required before or after
  929. setext headings:
  930. ```````````````````````````````` example
  931. ---
  932. Foo
  933. ---
  934. Bar
  935. ---
  936. Baz
  937. .
  938. <hr />
  939. <h2>Foo</h2>
  940. <h2>Bar</h2>
  941. <p>Baz</p>
  942. ````````````````````````````````
  943. Setext headings cannot be empty:
  944. ```````````````````````````````` example
  945. ====
  946. .
  947. <p>====</p>
  948. ````````````````````````````````
  949. Setext heading text lines must not be interpretable as block
  950. constructs other than paragraphs. So, the line of dashes
  951. in these examples gets interpreted as a thematic break:
  952. ```````````````````````````````` example
  953. ---
  954. ---
  955. .
  956. <hr />
  957. <hr />
  958. ````````````````````````````````
  959. ```````````````````````````````` example
  960. - foo
  961. -----
  962. .
  963. <ul>
  964. <li>foo</li>
  965. </ul>
  966. <hr />
  967. ````````````````````````````````
  968. ```````````````````````````````` example
  969. foo
  970. ---
  971. .
  972. <pre><code>foo
  973. </code></pre>
  974. <hr />
  975. ````````````````````````````````
  976. ```````````````````````````````` example
  977. > foo
  978. -----
  979. .
  980. <blockquote>
  981. <p>foo</p>
  982. </blockquote>
  983. <hr />
  984. ````````````````````````````````
  985. If you want a heading with `> foo` as its literal text, you can
  986. use backslash escapes:
  987. ```````````````````````````````` example
  988. \> foo
  989. ------
  990. .
  991. <h2>&gt; foo</h2>
  992. ````````````````````````````````
  993. **Compatibility note:** Most existing Markdown implementations
  994. do not allow the text of setext headings to span multiple lines.
  995. But there is no consensus about how to interpret
  996. ``` markdown
  997. Foo
  998. bar
  999. ---
  1000. baz
  1001. ```
  1002. One can find four different interpretations:
  1003. 1. paragraph "Foo", heading "bar", paragraph "baz"
  1004. 2. paragraph "Foo bar", thematic break, paragraph "baz"
  1005. 3. paragraph "Foo bar --- baz"
  1006. 4. heading "Foo bar", paragraph "baz"
  1007. We find interpretation 4 most natural, and interpretation 4
  1008. increases the expressive power of CommonMark, by allowing
  1009. multiline headings. Authors who want interpretation 1 can
  1010. put a blank line after the first paragraph:
  1011. ```````````````````````````````` example
  1012. Foo
  1013. bar
  1014. ---
  1015. baz
  1016. .
  1017. <p>Foo</p>
  1018. <h2>bar</h2>
  1019. <p>baz</p>
  1020. ````````````````````````````````
  1021. Authors who want interpretation 2 can put blank lines around
  1022. the thematic break,
  1023. ```````````````````````````````` example
  1024. Foo
  1025. bar
  1026. ---
  1027. baz
  1028. .
  1029. <p>Foo
  1030. bar</p>
  1031. <hr />
  1032. <p>baz</p>
  1033. ````````````````````````````````
  1034. or use a thematic break that cannot count as a [setext heading
  1035. underline], such as
  1036. ```````````````````````````````` example
  1037. Foo
  1038. bar
  1039. * * *
  1040. baz
  1041. .
  1042. <p>Foo
  1043. bar</p>
  1044. <hr />
  1045. <p>baz</p>
  1046. ````````````````````````````````
  1047. Authors who want interpretation 3 can use backslash escapes:
  1048. ```````````````````````````````` example
  1049. Foo
  1050. bar
  1051. \---
  1052. baz
  1053. .
  1054. <p>Foo
  1055. bar
  1056. ---
  1057. baz</p>
  1058. ````````````````````````````````
  1059. ## Indented code blocks
  1060. An [indented code block](@) is composed of one or more
  1061. [indented chunks] separated by blank lines.
  1062. An [indented chunk](@) is a sequence of non-blank lines,
  1063. each indented four or more spaces. The contents of the code block are
  1064. the literal contents of the lines, including trailing
  1065. [line endings], minus four spaces of indentation.
  1066. An indented code block has no [info string].
  1067. An indented code block cannot interrupt a paragraph, so there must be
  1068. a blank line between a paragraph and a following indented code block.
  1069. (A blank line is not needed, however, between a code block and a following
  1070. paragraph.)
  1071. ```````````````````````````````` example
  1072. a simple
  1073. indented code block
  1074. .
  1075. <pre><code>a simple
  1076. indented code block
  1077. </code></pre>
  1078. ````````````````````````````````
  1079. If there is any ambiguity between an interpretation of indentation
  1080. as a code block and as indicating that material belongs to a [list
  1081. item][list items], the list item interpretation takes precedence:
  1082. ```````````````````````````````` example
  1083. - foo
  1084. bar
  1085. .
  1086. <ul>
  1087. <li>
  1088. <p>foo</p>
  1089. <p>bar</p>
  1090. </li>
  1091. </ul>
  1092. ````````````````````````````````
  1093. ```````````````````````````````` example
  1094. 1. foo
  1095. - bar
  1096. .
  1097. <ol>
  1098. <li>
  1099. <p>foo</p>
  1100. <ul>
  1101. <li>bar</li>
  1102. </ul>
  1103. </li>
  1104. </ol>
  1105. ````````````````````````````````
  1106. The contents of a code block are literal text, and do not get parsed
  1107. as Markdown:
  1108. ```````````````````````````````` example
  1109. <a/>
  1110. *hi*
  1111. - one
  1112. .
  1113. <pre><code>&lt;a/&gt;
  1114. *hi*
  1115. - one
  1116. </code></pre>
  1117. ````````````````````````````````
  1118. Here we have three chunks separated by blank lines:
  1119. ```````````````````````````````` example
  1120. chunk1
  1121. chunk2
  1122. chunk3
  1123. .
  1124. <pre><code>chunk1
  1125. chunk2
  1126. chunk3
  1127. </code></pre>
  1128. ````````````````````````````````
  1129. Any initial spaces beyond four will be included in the content, even
  1130. in interior blank lines:
  1131. ```````````````````````````````` example
  1132. chunk1
  1133. chunk2
  1134. .
  1135. <pre><code>chunk1
  1136. chunk2
  1137. </code></pre>
  1138. ````````````````````````````````
  1139. An indented code block cannot interrupt a paragraph. (This
  1140. allows hanging indents and the like.)
  1141. ```````````````````````````````` example
  1142. Foo
  1143. bar
  1144. .
  1145. <p>Foo
  1146. bar</p>
  1147. ````````````````````````````````
  1148. However, any non-blank line with fewer than four leading spaces ends
  1149. the code block immediately. So a paragraph may occur immediately
  1150. after indented code:
  1151. ```````````````````````````````` example
  1152. foo
  1153. bar
  1154. .
  1155. <pre><code>foo
  1156. </code></pre>
  1157. <p>bar</p>
  1158. ````````````````````````````````
  1159. And indented code can occur immediately before and after other kinds of
  1160. blocks:
  1161. ```````````````````````````````` example
  1162. # Heading
  1163. foo
  1164. Heading
  1165. ------
  1166. foo
  1167. ----
  1168. .
  1169. <h1>Heading</h1>
  1170. <pre><code>foo
  1171. </code></pre>
  1172. <h2>Heading</h2>
  1173. <pre><code>foo
  1174. </code></pre>
  1175. <hr />
  1176. ````````````````````````````````
  1177. The first line can be indented more than four spaces:
  1178. ```````````````````````````````` example
  1179. foo
  1180. bar
  1181. .
  1182. <pre><code> foo
  1183. bar
  1184. </code></pre>
  1185. ````````````````````````````````
  1186. Blank lines preceding or following an indented code block
  1187. are not included in it:
  1188. ```````````````````````````````` example
  1189. foo
  1190. .
  1191. <pre><code>foo
  1192. </code></pre>
  1193. ````````````````````````````````
  1194. Trailing spaces are included in the code block's content:
  1195. ```````````````````````````````` example
  1196. foo
  1197. .
  1198. <pre><code>foo
  1199. </code></pre>
  1200. ````````````````````````````````
  1201. ## Fenced code blocks
  1202. A [code fence](@) is a sequence
  1203. of at least three consecutive backtick characters (`` ` ``) or
  1204. tildes (`~`). (Tildes and backticks cannot be mixed.)
  1205. A [fenced code block](@)
  1206. begins with a code fence, indented no more than three spaces.
  1207. The line with the opening code fence may optionally contain some text
  1208. following the code fence; this is trimmed of leading and trailing
  1209. whitespace and called the [info string](@). If the [info string] comes
  1210. after a backtick fence, it may not contain any backtick
  1211. characters. (The reason for this restriction is that otherwise
  1212. some inline code would be incorrectly interpreted as the
  1213. beginning of a fenced code block.)
  1214. The content of the code block consists of all subsequent lines, until
  1215. a closing [code fence] of the same type as the code block
  1216. began with (backticks or tildes), and with at least as many backticks
  1217. or tildes as the opening code fence. If the leading code fence is
  1218. indented N spaces, then up to N spaces of indentation are removed from
  1219. each line of the content (if present). (If a content line is not
  1220. indented, it is preserved unchanged. If it is indented less than N
  1221. spaces, all of the indentation is removed.)
  1222. The closing code fence may be indented up to three spaces, and may be
  1223. followed only by spaces, which are ignored. If the end of the
  1224. containing block (or document) is reached and no closing code fence
  1225. has been found, the code block contains all of the lines after the
  1226. opening code fence until the end of the containing block (or
  1227. document). (An alternative spec would require backtracking in the
  1228. event that a closing code fence is not found. But this makes parsing
  1229. much less efficient, and there seems to be no real down side to the
  1230. behavior described here.)
  1231. A fenced code block may interrupt a paragraph, and does not require
  1232. a blank line either before or after.
  1233. The content of a code fence is treated as literal text, not parsed
  1234. as inlines. The first word of the [info string] is typically used to
  1235. specify the language of the code sample, and rendered in the `class`
  1236. attribute of the `code` tag. However, this spec does not mandate any
  1237. particular treatment of the [info string].
  1238. Here is a simple example with backticks:
  1239. ```````````````````````````````` example
  1240. ```
  1241. <
  1242. >
  1243. ```
  1244. .
  1245. <pre><code>&lt;
  1246. &gt;
  1247. </code></pre>
  1248. ````````````````````````````````
  1249. With tildes:
  1250. ```````````````````````````````` example
  1251. ~~~
  1252. <
  1253. >
  1254. ~~~
  1255. .
  1256. <pre><code>&lt;
  1257. &gt;
  1258. </code></pre>
  1259. ````````````````````````````````
  1260. Fewer than three backticks is not enough:
  1261. ```````````````````````````````` example
  1262. ``
  1263. foo
  1264. ``
  1265. .
  1266. <p><code>foo</code></p>
  1267. ````````````````````````````````
  1268. The closing code fence must use the same character as the opening
  1269. fence:
  1270. ```````````````````````````````` example
  1271. ```
  1272. aaa
  1273. ~~~
  1274. ```
  1275. .
  1276. <pre><code>aaa
  1277. ~~~
  1278. </code></pre>
  1279. ````````````````````````````````
  1280. ```````````````````````````````` example
  1281. ~~~
  1282. aaa
  1283. ```
  1284. ~~~
  1285. .
  1286. <pre><code>aaa
  1287. ```
  1288. </code></pre>
  1289. ````````````````````````````````
  1290. The closing code fence must be at least as long as the opening fence:
  1291. ```````````````````````````````` example
  1292. ````
  1293. aaa
  1294. ```
  1295. ``````
  1296. .
  1297. <pre><code>aaa
  1298. ```
  1299. </code></pre>
  1300. ````````````````````````````````
  1301. ```````````````````````````````` example
  1302. ~~~~
  1303. aaa
  1304. ~~~
  1305. ~~~~
  1306. .
  1307. <pre><code>aaa
  1308. ~~~
  1309. </code></pre>
  1310. ````````````````````````````````
  1311. Unclosed code blocks are closed by the end of the document
  1312. (or the enclosing [block quote][block quotes] or [list item][list items]):
  1313. ```````````````````````````````` example
  1314. ```
  1315. .
  1316. <pre><code></code></pre>
  1317. ````````````````````````````````
  1318. ```````````````````````````````` example
  1319. `````
  1320. ```
  1321. aaa
  1322. .
  1323. <pre><code>
  1324. ```
  1325. aaa
  1326. </code></pre>
  1327. ````````````````````````````````
  1328. ```````````````````````````````` example
  1329. > ```
  1330. > aaa
  1331. bbb
  1332. .
  1333. <blockquote>
  1334. <pre><code>aaa
  1335. </code></pre>
  1336. </blockquote>
  1337. <p>bbb</p>
  1338. ````````````````````````````````
  1339. A code block can have all empty lines as its content:
  1340. ```````````````````````````````` example
  1341. ```
  1342. ```
  1343. .
  1344. <pre><code>
  1345. </code></pre>
  1346. ````````````````````````````````
  1347. A code block can be empty:
  1348. ```````````````````````````````` example
  1349. ```
  1350. ```
  1351. .
  1352. <pre><code></code></pre>
  1353. ````````````````````````````````
  1354. Fences can be indented. If the opening fence is indented,
  1355. content lines will have equivalent opening indentation removed,
  1356. if present:
  1357. ```````````````````````````````` example
  1358. ```
  1359. aaa
  1360. aaa
  1361. ```
  1362. .
  1363. <pre><code>aaa
  1364. aaa
  1365. </code></pre>
  1366. ````````````````````````````````
  1367. ```````````````````````````````` example
  1368. ```
  1369. aaa
  1370. aaa
  1371. aaa
  1372. ```
  1373. .
  1374. <pre><code>aaa
  1375. aaa
  1376. aaa
  1377. </code></pre>
  1378. ````````````````````````````````
  1379. ```````````````````````````````` example
  1380. ```
  1381. aaa
  1382. aaa
  1383. aaa
  1384. ```
  1385. .
  1386. <pre><code>aaa
  1387. aaa
  1388. aaa
  1389. </code></pre>
  1390. ````````````````````````````````
  1391. Four spaces indentation produces an indented code block:
  1392. ```````````````````````````````` example
  1393. ```
  1394. aaa
  1395. ```
  1396. .
  1397. <pre><code>```
  1398. aaa
  1399. ```
  1400. </code></pre>
  1401. ````````````````````````````````
  1402. Closing fences may be indented by 0-3 spaces, and their indentation
  1403. need not match that of the opening fence:
  1404. ```````````````````````````````` example
  1405. ```
  1406. aaa
  1407. ```
  1408. .
  1409. <pre><code>aaa
  1410. </code></pre>
  1411. ````````````````````````````````
  1412. ```````````````````````````````` example
  1413. ```
  1414. aaa
  1415. ```
  1416. .
  1417. <pre><code>aaa
  1418. </code></pre>
  1419. ````````````````````````````````
  1420. This is not a closing fence, because it is indented 4 spaces:
  1421. ```````````````````````````````` example
  1422. ```
  1423. aaa
  1424. ```
  1425. .
  1426. <pre><code>aaa
  1427. ```
  1428. </code></pre>
  1429. ````````````````````````````````
  1430. Code fences (opening and closing) cannot contain internal spaces:
  1431. ```````````````````````````````` example
  1432. ``` ```
  1433. aaa
  1434. .
  1435. <p><code> </code>
  1436. aaa</p>
  1437. ````````````````````````````````
  1438. ```````````````````````````````` example
  1439. ~~~~~~
  1440. aaa
  1441. ~~~ ~~
  1442. .
  1443. <pre><code>aaa
  1444. ~~~ ~~
  1445. </code></pre>
  1446. ````````````````````````````````
  1447. Fenced code blocks can interrupt paragraphs, and can be followed
  1448. directly by paragraphs, without a blank line between:
  1449. ```````````````````````````````` example
  1450. foo
  1451. ```
  1452. bar
  1453. ```
  1454. baz
  1455. .
  1456. <p>foo</p>
  1457. <pre><code>bar
  1458. </code></pre>
  1459. <p>baz</p>
  1460. ````````````````````````````````
  1461. Other blocks can also occur before and after fenced code blocks
  1462. without an intervening blank line:
  1463. ```````````````````````````````` example
  1464. foo
  1465. ---
  1466. ~~~
  1467. bar
  1468. ~~~
  1469. # baz
  1470. .
  1471. <h2>foo</h2>
  1472. <pre><code>bar
  1473. </code></pre>
  1474. <h1>baz</h1>
  1475. ````````````````````````````````
  1476. An [info string] can be provided after the opening code fence.
  1477. Although this spec doesn't mandate any particular treatment of
  1478. the info string, the first word is typically used to specify
  1479. the language of the code block. In HTML output, the language is
  1480. normally indicated by adding a class to the `code` element consisting
  1481. of `language-` followed by the language name.
  1482. ```````````````````````````````` example
  1483. ```ruby
  1484. def foo(x)
  1485. return 3
  1486. end
  1487. ```
  1488. .
  1489. <pre><code class="language-ruby">def foo(x)
  1490. return 3
  1491. end
  1492. </code></pre>
  1493. ````````````````````````````````
  1494. ```````````````````````````````` example
  1495. ~~~~ ruby startline=3 $%@#$
  1496. def foo(x)
  1497. return 3
  1498. end
  1499. ~~~~~~~
  1500. .
  1501. <pre><code class="language-ruby">def foo(x)
  1502. return 3
  1503. end
  1504. </code></pre>
  1505. ````````````````````````````````
  1506. ```````````````````````````````` example
  1507. ````;
  1508. ````
  1509. .
  1510. <pre><code class="language-;"></code></pre>
  1511. ````````````````````````````````
  1512. [Info strings] for backtick code blocks cannot contain backticks:
  1513. ```````````````````````````````` example
  1514. ``` aa ```
  1515. foo
  1516. .
  1517. <p><code>aa</code>
  1518. foo</p>
  1519. ````````````````````````````````
  1520. [Info strings] for tilde code blocks can contain backticks and tildes:
  1521. ```````````````````````````````` example
  1522. ~~~ aa ``` ~~~
  1523. foo
  1524. ~~~
  1525. .
  1526. <pre><code class="language-aa">foo
  1527. </code></pre>
  1528. ````````````````````````````````
  1529. Closing code fences cannot have [info strings]:
  1530. ```````````````````````````````` example
  1531. ```
  1532. ``` aaa
  1533. ```
  1534. .
  1535. <pre><code>``` aaa
  1536. </code></pre>
  1537. ````````````````````````````````
  1538. ## HTML blocks
  1539. An [HTML block](@) is a group of lines that is treated
  1540. as raw HTML (and will not be escaped in HTML output).
  1541. There are seven kinds of [HTML block], which can be defined by their
  1542. start and end conditions. The block begins with a line that meets a
  1543. [start condition](@) (after up to three spaces optional indentation).
  1544. It ends with the first subsequent line that meets a matching [end
  1545. condition](@), or the last line of the document, or the last line of
  1546. the [container block](#container-blocks) containing the current HTML
  1547. block, if no line is encountered that meets the [end condition]. If
  1548. the first line meets both the [start condition] and the [end
  1549. condition], the block will contain just that line.
  1550. 1. **Start condition:** line begins with the string `<script`,
  1551. `<pre`, or `<style` (case-insensitive), followed by whitespace,
  1552. the string `>`, or the end of the line.\
  1553. **End condition:** line contains an end tag
  1554. `</script>`, `</pre>`, or `</style>` (case-insensitive; it
  1555. need not match the start tag).
  1556. 2. **Start condition:** line begins with the string `<!--`.\
  1557. **End condition:** line contains the string `-->`.
  1558. 3. **Start condition:** line begins with the string `<?`.\
  1559. **End condition:** line contains the string `?>`.
  1560. 4. **Start condition:** line begins with the string `<!`
  1561. followed by an uppercase ASCII letter.\
  1562. **End condition:** line contains the character `>`.
  1563. 5. **Start condition:** line begins with the string
  1564. `<![CDATA[`.\
  1565. **End condition:** line contains the string `]]>`.
  1566. 6. **Start condition:** line begins the string `<` or `</`
  1567. followed by one of the strings (case-insensitive) `address`,
  1568. `article`, `aside`, `base`, `basefont`, `blockquote`, `body`,
  1569. `caption`, `center`, `col`, `colgroup`, `dd`, `details`, `dialog`,
  1570. `dir`, `div`, `dl`, `dt`, `fieldset`, `figcaption`, `figure`,
  1571. `footer`, `form`, `frame`, `frameset`,
  1572. `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `head`, `header`, `hr`,
  1573. `html`, `iframe`, `legend`, `li`, `link`, `main`, `menu`, `menuitem`,
  1574. `nav`, `noframes`, `ol`, `optgroup`, `option`, `p`, `param`,
  1575. `section`, `source`, `summary`, `table`, `tbody`, `td`,
  1576. `tfoot`, `th`, `thead`, `title`, `tr`, `track`, `ul`, followed
  1577. by [whitespace], the end of the line, the string `>`, or
  1578. the string `/>`.\
  1579. **End condition:** line is followed by a [blank line].
  1580. 7. **Start condition:** line begins with a complete [open tag]
  1581. (with any [tag name] other than `script`,
  1582. `style`, or `pre`) or a complete [closing tag],
  1583. followed only by [whitespace] or the end of the line.\
  1584. **End condition:** line is followed by a [blank line].
  1585. HTML blocks continue until they are closed by their appropriate
  1586. [end condition], or the last line of the document or other [container
  1587. block](#container-blocks). This means any HTML **within an HTML
  1588. block** that might otherwise be recognised as a start condition will
  1589. be ignored by the parser and passed through as-is, without changing
  1590. the parser's state.
  1591. For instance, `<pre>` within a HTML block started by `<table>` will not affect
  1592. the parser state; as the HTML block was started in by start condition 6, it
  1593. will end at any blank line. This can be surprising:
  1594. ```````````````````````````````` example
  1595. <table><tr><td>
  1596. <pre>
  1597. **Hello**,
  1598. _world_.
  1599. </pre>
  1600. </td></tr></table>
  1601. .
  1602. <table><tr><td>
  1603. <pre>
  1604. **Hello**,
  1605. <p><em>world</em>.
  1606. </pre></p>
  1607. </td></tr></table>
  1608. ````````````````````````````````
  1609. In this case, the HTML block is terminated by the newline — the `**Hello**`
  1610. text remains verbatim — and regular parsing resumes, with a paragraph,
  1611. emphasised `world` and inline and block HTML following.
  1612. All types of [HTML blocks] except type 7 may interrupt
  1613. a paragraph. Blocks of type 7 may not interrupt a paragraph.
  1614. (This restriction is intended to prevent unwanted interpretation
  1615. of long tags inside a wrapped paragraph as starting HTML blocks.)
  1616. Some simple examples follow. Here are some basic HTML blocks
  1617. of type 6:
  1618. ```````````````````````````````` example
  1619. <table>
  1620. <tr>
  1621. <td>
  1622. hi
  1623. </td>
  1624. </tr>
  1625. </table>
  1626. okay.
  1627. .
  1628. <table>
  1629. <tr>
  1630. <td>
  1631. hi
  1632. </td>
  1633. </tr>
  1634. </table>
  1635. <p>okay.</p>
  1636. ````````````````````````````````
  1637. ```````````````````````````````` example
  1638. <div>
  1639. *hello*
  1640. <foo><a>
  1641. .
  1642. <div>
  1643. *hello*
  1644. <foo><a>
  1645. ````````````````````````````````
  1646. A block can also start with a closing tag:
  1647. ```````````````````````````````` example
  1648. </div>
  1649. *foo*
  1650. .
  1651. </div>
  1652. *foo*
  1653. ````````````````````````````````
  1654. Here we have two HTML blocks with a Markdown paragraph between them:
  1655. ```````````````````````````````` example
  1656. <DIV CLASS="foo">
  1657. *Markdown*
  1658. </DIV>
  1659. .
  1660. <DIV CLASS="foo">
  1661. <p><em>Markdown</em></p>
  1662. </DIV>
  1663. ````````````````````````````````
  1664. The tag on the first line can be partial, as long
  1665. as it is split where there would be whitespace:
  1666. ```````````````````````````````` example
  1667. <div id="foo"
  1668. class="bar">
  1669. </div>
  1670. .
  1671. <div id="foo"
  1672. class="bar">
  1673. </div>
  1674. ````````````````````````````````
  1675. ```````````````````````````````` example
  1676. <div id="foo" class="bar
  1677. baz">
  1678. </div>
  1679. .
  1680. <div id="foo" class="bar
  1681. baz">
  1682. </div>
  1683. ````````````````````````````````
  1684. An open tag need not be closed:
  1685. ```````````````````````````````` example
  1686. <div>
  1687. *foo*
  1688. *bar*
  1689. .
  1690. <div>
  1691. *foo*
  1692. <p><em>bar</em></p>
  1693. ````````````````````````````````
  1694. A partial tag need not even be completed (garbage
  1695. in, garbage out):
  1696. ```````````````````````````````` example
  1697. <div id="foo"
  1698. *hi*
  1699. .
  1700. <div id="foo"
  1701. *hi*
  1702. ````````````````````````````````
  1703. ```````````````````````````````` example
  1704. <div class
  1705. foo
  1706. .
  1707. <div class
  1708. foo
  1709. ````````````````````````````````
  1710. The initial tag doesn't even need to be a valid
  1711. tag, as long as it starts like one:
  1712. ```````````````````````````````` example
  1713. <div *???-&&&-<---
  1714. *foo*
  1715. .
  1716. <div *???-&&&-<---
  1717. *foo*
  1718. ````````````````````````````````
  1719. In type 6 blocks, the initial tag need not be on a line by
  1720. itself:
  1721. ```````````````````````````````` example
  1722. <div><a href="bar">*foo*</a></div>
  1723. .
  1724. <div><a href="bar">*foo*</a></div>
  1725. ````````````````````````````````
  1726. ```````````````````````````````` example
  1727. <table><tr><td>
  1728. foo
  1729. </td></tr></table>
  1730. .
  1731. <table><tr><td>
  1732. foo
  1733. </td></tr></table>
  1734. ````````````````````````````````
  1735. Everything until the next blank line or end of document
  1736. gets included in the HTML block. So, in the following
  1737. example, what looks like a Markdown code block
  1738. is actually part of the HTML block, which continues until a blank
  1739. line or the end of the document is reached:
  1740. ```````````````````````````````` example
  1741. <div></div>
  1742. ``` c
  1743. int x = 33;
  1744. ```
  1745. .
  1746. <div></div>
  1747. ``` c
  1748. int x = 33;
  1749. ```
  1750. ````````````````````````````````
  1751. To start an [HTML block] with a tag that is *not* in the
  1752. list of block-level tags in (6), you must put the tag by
  1753. itself on the first line (and it must be complete):
  1754. ```````````````````````````````` example
  1755. <a href="foo">
  1756. *bar*
  1757. </a>
  1758. .
  1759. <a href="foo">
  1760. *bar*
  1761. </a>
  1762. ````````````````````````````````
  1763. In type 7 blocks, the [tag name] can be anything:
  1764. ```````````````````````````````` example
  1765. <Warning>
  1766. *bar*
  1767. </Warning>
  1768. .
  1769. <Warning>
  1770. *bar*
  1771. </Warning>
  1772. ````````````````````````````````
  1773. ```````````````````````````````` example
  1774. <i class="foo">
  1775. *bar*
  1776. </i>
  1777. .
  1778. <i class="foo">
  1779. *bar*
  1780. </i>
  1781. ````````````````````````````````
  1782. ```````````````````````````````` example
  1783. </ins>
  1784. *bar*
  1785. .
  1786. </ins>
  1787. *bar*
  1788. ````````````````````````````````
  1789. These rules are designed to allow us to work with tags that
  1790. can function as either block-level or inline-level tags.
  1791. The `<del>` tag is a nice example. We can surround content with
  1792. `<del>` tags in three different ways. In this case, we get a raw
  1793. HTML block, because the `<del>` tag is on a line by itself:
  1794. ```````````````````````````````` example
  1795. <del>
  1796. *foo*
  1797. </del>
  1798. .
  1799. <del>
  1800. *foo*
  1801. </del>
  1802. ````````````````````````````````
  1803. In this case, we get a raw HTML block that just includes
  1804. the `<del>` tag (because it ends with the following blank
  1805. line). So the contents get interpreted as CommonMark:
  1806. ```````````````````````````````` example
  1807. <del>
  1808. *foo*
  1809. </del>
  1810. .
  1811. <del>
  1812. <p><em>foo</em></p>
  1813. </del>
  1814. ````````````````````````````````
  1815. Finally, in this case, the `<del>` tags are interpreted
  1816. as [raw HTML] *inside* the CommonMark paragraph. (Because
  1817. the tag is not on a line by itself, we get inline HTML
  1818. rather than an [HTML block].)
  1819. ```````````````````````````````` example
  1820. <del>*foo*</del>
  1821. .
  1822. <p><del><em>foo</em></del></p>
  1823. ````````````````````````````````
  1824. HTML tags designed to contain literal content
  1825. (`script`, `style`, `pre`), comments, processing instructions,
  1826. and declarations are treated somewhat differently.
  1827. Instead of ending at the first blank line, these blocks
  1828. end at the first line containing a corresponding end tag.
  1829. As a result, these blocks can contain blank lines:
  1830. A pre tag (type 1):
  1831. ```````````````````````````````` example
  1832. <pre language="haskell"><code>
  1833. import Text.HTML.TagSoup
  1834. main :: IO ()
  1835. main = print $ parseTags tags
  1836. </code></pre>
  1837. okay
  1838. .
  1839. <pre language="haskell"><code>
  1840. import Text.HTML.TagSoup
  1841. main :: IO ()
  1842. main = print $ parseTags tags
  1843. </code></pre>
  1844. <p>okay</p>
  1845. ````````````````````````````````
  1846. A script tag (type 1):
  1847. ```````````````````````````````` example
  1848. <script type="text/javascript">
  1849. // JavaScript example
  1850. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1851. </script>
  1852. okay
  1853. .
  1854. <script type="text/javascript">
  1855. // JavaScript example
  1856. document.getElementById("demo").innerHTML = "Hello JavaScript!";
  1857. </script>
  1858. <p>okay</p>
  1859. ````````````````````````````````
  1860. A style tag (type 1):
  1861. ```````````````````````````````` example
  1862. <style
  1863. type="text/css">
  1864. h1 {color:red;}
  1865. p {color:blue;}
  1866. </style>
  1867. okay
  1868. .
  1869. <style
  1870. type="text/css">
  1871. h1 {color:red;}
  1872. p {color:blue;}
  1873. </style>
  1874. <p>okay</p>
  1875. ````````````````````````````````
  1876. If there is no matching end tag, the block will end at the
  1877. end of the document (or the enclosing [block quote][block quotes]
  1878. or [list item][list items]):
  1879. ```````````````````````````````` example
  1880. <style
  1881. type="text/css">
  1882. foo
  1883. .
  1884. <style
  1885. type="text/css">
  1886. foo
  1887. ````````````````````````````````
  1888. ```````````````````````````````` example
  1889. > <div>
  1890. > foo
  1891. bar
  1892. .
  1893. <blockquote>
  1894. <div>
  1895. foo
  1896. </blockquote>
  1897. <p>bar</p>
  1898. ````````````````````````````````
  1899. ```````````````````````````````` example
  1900. - <div>
  1901. - foo
  1902. .
  1903. <ul>
  1904. <li>
  1905. <div>
  1906. </li>
  1907. <li>foo</li>
  1908. </ul>
  1909. ````````````````````````````````
  1910. The end tag can occur on the same line as the start tag:
  1911. ```````````````````````````````` example
  1912. <style>p{color:red;}</style>
  1913. *foo*
  1914. .
  1915. <style>p{color:red;}</style>
  1916. <p><em>foo</em></p>
  1917. ````````````````````````````````
  1918. ```````````````````````````````` example
  1919. <!-- foo -->*bar*
  1920. *baz*
  1921. .
  1922. <!-- foo -->*bar*
  1923. <p><em>baz</em></p>
  1924. ````````````````````````````````
  1925. Note that anything on the last line after the
  1926. end tag will be included in the [HTML block]:
  1927. ```````````````````````````````` example
  1928. <script>
  1929. foo
  1930. </script>1. *bar*
  1931. .
  1932. <script>
  1933. foo
  1934. </script>1. *bar*
  1935. ````````````````````````````````
  1936. A comment (type 2):
  1937. ```````````````````````````````` example
  1938. <!-- Foo
  1939. bar
  1940. baz -->
  1941. okay
  1942. .
  1943. <!-- Foo
  1944. bar
  1945. baz -->
  1946. <p>okay</p>
  1947. ````````````````````````````````
  1948. A processing instruction (type 3):
  1949. ```````````````````````````````` example
  1950. <?php
  1951. echo '>';
  1952. ?>
  1953. okay
  1954. .
  1955. <?php
  1956. echo '>';
  1957. ?>
  1958. <p>okay</p>
  1959. ````````````````````````````````
  1960. A declaration (type 4):
  1961. ```````````````````````````````` example
  1962. <!DOCTYPE html>
  1963. .
  1964. <!DOCTYPE html>
  1965. ````````````````````````````````
  1966. CDATA (type 5):
  1967. ```````````````````````````````` example
  1968. <![CDATA[
  1969. function matchwo(a,b)
  1970. {
  1971. if (a < b && a < 0) then {
  1972. return 1;
  1973. } else {
  1974. return 0;
  1975. }
  1976. }
  1977. ]]>
  1978. okay
  1979. .
  1980. <![CDATA[
  1981. function matchwo(a,b)
  1982. {
  1983. if (a < b && a < 0) then {
  1984. return 1;
  1985. } else {
  1986. return 0;
  1987. }
  1988. }
  1989. ]]>
  1990. <p>okay</p>
  1991. ````````````````````````````````
  1992. The opening tag can be indented 1-3 spaces, but not 4:
  1993. ```````````````````````````````` example
  1994. <!-- foo -->
  1995. <!-- foo -->
  1996. .
  1997. <!-- foo -->
  1998. <pre><code>&lt;!-- foo --&gt;
  1999. </code></pre>
  2000. ````````````````````````````````
  2001. ```````````````````````````````` example
  2002. <div>
  2003. <div>
  2004. .
  2005. <div>
  2006. <pre><code>&lt;div&gt;
  2007. </code></pre>
  2008. ````````````````````````````````
  2009. An HTML block of types 1--6 can interrupt a paragraph, and need not be
  2010. preceded by a blank line.
  2011. ```````````````````````````````` example
  2012. Foo
  2013. <div>
  2014. bar
  2015. </div>
  2016. .
  2017. <p>Foo</p>
  2018. <div>
  2019. bar
  2020. </div>
  2021. ````````````````````````````````
  2022. However, a following blank line is needed, except at the end of
  2023. a document, and except for blocks of types 1--5, [above][HTML
  2024. block]:
  2025. ```````````````````````````````` example
  2026. <div>
  2027. bar
  2028. </div>
  2029. *foo*
  2030. .
  2031. <div>
  2032. bar
  2033. </div>
  2034. *foo*
  2035. ````````````````````````````````
  2036. HTML blocks of type 7 cannot interrupt a paragraph:
  2037. ```````````````````````````````` example
  2038. Foo
  2039. <a href="bar">
  2040. baz
  2041. .
  2042. <p>Foo
  2043. <a href="bar">
  2044. baz</p>
  2045. ````````````````````````````````
  2046. This rule differs from John Gruber's original Markdown syntax
  2047. specification, which says:
  2048. > The only restrictions are that block-level HTML elements —
  2049. > e.g. `<div>`, `<table>`, `<pre>`, `<p>`, etc. — must be separated from
  2050. > surrounding content by blank lines, and the start and end tags of the
  2051. > block should not be indented with tabs or spaces.
  2052. In some ways Gruber's rule is more restrictive than the one given
  2053. here:
  2054. - It requires that an HTML block be preceded by a blank line.
  2055. - It does not allow the start tag to be indented.
  2056. - It requires a matching end tag, which it also does not allow to
  2057. be indented.
  2058. Most Markdown implementations (including some of Gruber's own) do not
  2059. respect all of these restrictions.
  2060. There is one respect, however, in which Gruber's rule is more liberal
  2061. than the one given here, since it allows blank lines to occur inside
  2062. an HTML block. There are two reasons for disallowing them here.
  2063. First, it removes the need to parse balanced tags, which is
  2064. expensive and can require backtracking from the end of the document
  2065. if no matching end tag is found. Second, it provides a very simple
  2066. and flexible way of including Markdown content inside HTML tags:
  2067. simply separate the Markdown from the HTML using blank lines:
  2068. Compare:
  2069. ```````````````````````````````` example
  2070. <div>
  2071. *Emphasized* text.
  2072. </div>
  2073. .
  2074. <div>
  2075. <p><em>Emphasized</em> text.</p>
  2076. </div>
  2077. ````````````````````````````````
  2078. ```````````````````````````````` example
  2079. <div>
  2080. *Emphasized* text.
  2081. </div>
  2082. .
  2083. <div>
  2084. *Emphasized* text.
  2085. </div>
  2086. ````````````````````````````````
  2087. Some Markdown implementations have adopted a convention of
  2088. interpreting content inside tags as text if the open tag has
  2089. the attribute `markdown=1`. The rule given above seems a simpler and
  2090. more elegant way of achieving the same expressive power, which is also
  2091. much simpler to parse.
  2092. The main potential drawback is that one can no longer paste HTML
  2093. blocks into Markdown documents with 100% reliability. However,
  2094. *in most cases* this will work fine, because the blank lines in
  2095. HTML are usually followed by HTML block tags. For example:
  2096. ```````````````````````````````` example
  2097. <table>
  2098. <tr>
  2099. <td>
  2100. Hi
  2101. </td>
  2102. </tr>
  2103. </table>
  2104. .
  2105. <table>
  2106. <tr>
  2107. <td>
  2108. Hi
  2109. </td>
  2110. </tr>
  2111. </table>
  2112. ````````````````````````````````
  2113. There are problems, however, if the inner tags are indented
  2114. *and* separated by spaces, as then they will be interpreted as
  2115. an indented code block:
  2116. ```````````````````````````````` example
  2117. <table>
  2118. <tr>
  2119. <td>
  2120. Hi
  2121. </td>
  2122. </tr>
  2123. </table>
  2124. .
  2125. <table>
  2126. <tr>
  2127. <pre><code>&lt;td&gt;
  2128. Hi
  2129. &lt;/td&gt;
  2130. </code></pre>
  2131. </tr>
  2132. </table>
  2133. ````````````````````````````````
  2134. Fortunately, blank lines are usually not necessary and can be
  2135. deleted. The exception is inside `<pre>` tags, but as described
  2136. [above][HTML blocks], raw HTML blocks starting with `<pre>`
  2137. *can* contain blank lines.
  2138. ## Link reference definitions
  2139. A [link reference definition](@)
  2140. consists of a [link label], indented up to three spaces, followed
  2141. by a colon (`:`), optional [whitespace] (including up to one
  2142. [line ending]), a [link destination],
  2143. optional [whitespace] (including up to one
  2144. [line ending]), and an optional [link
  2145. title], which if it is present must be separated
  2146. from the [link destination] by [whitespace].
  2147. No further [non-whitespace characters] may occur on the line.
  2148. A [link reference definition]
  2149. does not correspond to a structural element of a document. Instead, it
  2150. defines a label which can be used in [reference links]
  2151. and reference-style [images] elsewhere in the document. [Link
  2152. reference definitions] can come either before or after the links that use
  2153. them.
  2154. ```````````````````````````````` example
  2155. [foo]: /url "title"
  2156. [foo]
  2157. .
  2158. <p><a href="/url" title="title">foo</a></p>
  2159. ````````````````````````````````
  2160. ```````````````````````````````` example
  2161. [foo]:
  2162. /url
  2163. 'the title'
  2164. [foo]
  2165. .
  2166. <p><a href="/url" title="the title">foo</a></p>
  2167. ````````````````````````````````
  2168. ```````````````````````````````` example
  2169. [Foo*bar\]]:my_(url) 'title (with parens)'
  2170. [Foo*bar\]]
  2171. .
  2172. <p><a href="my_(url)" title="title (with parens)">Foo*bar]</a></p>
  2173. ````````````````````````````````
  2174. ```````````````````````````````` example
  2175. [Foo bar]:
  2176. <my url>
  2177. 'title'
  2178. [Foo bar]
  2179. .
  2180. <p><a href="my%20url" title="title">Foo bar</a></p>
  2181. ````````````````````````````````
  2182. The title may extend over multiple lines:
  2183. ```````````````````````````````` example
  2184. [foo]: /url '
  2185. title
  2186. line1
  2187. line2
  2188. '
  2189. [foo]
  2190. .
  2191. <p><a href="/url" title="
  2192. title
  2193. line1
  2194. line2
  2195. ">foo</a></p>
  2196. ````````````````````````````````
  2197. However, it may not contain a [blank line]:
  2198. ```````````````````````````````` example
  2199. [foo]: /url 'title
  2200. with blank line'
  2201. [foo]
  2202. .
  2203. <p>[foo]: /url 'title</p>
  2204. <p>with blank line'</p>
  2205. <p>[foo]</p>
  2206. ````````````````````````````````
  2207. The title may be omitted:
  2208. ```````````````````````````````` example
  2209. [foo]:
  2210. /url
  2211. [foo]
  2212. .
  2213. <p><a href="/url">foo</a></p>
  2214. ````````````````````````````````
  2215. The link destination may not be omitted:
  2216. ```````````````````````````````` example
  2217. [foo]:
  2218. [foo]
  2219. .
  2220. <p>[foo]:</p>
  2221. <p>[foo]</p>
  2222. ````````````````````````````````
  2223. However, an empty link destination may be specified using
  2224. angle brackets:
  2225. ```````````````````````````````` example
  2226. [foo]: <>
  2227. [foo]
  2228. .
  2229. <p><a href="">foo</a></p>
  2230. ````````````````````````````````
  2231. The title must be separated from the link destination by
  2232. whitespace:
  2233. ```````````````````````````````` example
  2234. [foo]: <bar>(baz)
  2235. [foo]
  2236. .
  2237. <p>[foo]: <bar>(baz)</p>
  2238. <p>[foo]</p>
  2239. ````````````````````````````````
  2240. Both title and destination can contain backslash escapes
  2241. and literal backslashes:
  2242. ```````````````````````````````` example
  2243. [foo]: /url\bar\*baz "foo\"bar\baz"
  2244. [foo]
  2245. .
  2246. <p><a href="/url%5Cbar*baz" title="foo&quot;bar\baz">foo</a></p>
  2247. ````````````````````````````````
  2248. A link can come before its corresponding definition:
  2249. ```````````````````````````````` example
  2250. [foo]
  2251. [foo]: url
  2252. .
  2253. <p><a href="url">foo</a></p>
  2254. ````````````````````````````````
  2255. If there are several matching definitions, the first one takes
  2256. precedence:
  2257. ```````````````````````````````` example
  2258. [foo]
  2259. [foo]: first
  2260. [foo]: second
  2261. .
  2262. <p><a href="first">foo</a></p>
  2263. ````````````````````````````````
  2264. As noted in the section on [Links], matching of labels is
  2265. case-insensitive (see [matches]).
  2266. ```````````````````````````````` example
  2267. [FOO]: /url
  2268. [Foo]
  2269. .
  2270. <p><a href="/url">Foo</a></p>
  2271. ````````````````````````````````
  2272. ```````````````````````````````` example
  2273. [ΑΓΩ]: /φου
  2274. [αγω]
  2275. .
  2276. <p><a href="/%CF%86%CE%BF%CF%85">αγω</a></p>
  2277. ````````````````````````````````
  2278. Here is a link reference definition with no corresponding link.
  2279. It contributes nothing to the document.
  2280. ```````````````````````````````` example
  2281. [foo]: /url
  2282. .
  2283. ````````````````````````````````
  2284. Here is another one:
  2285. ```````````````````````````````` example
  2286. [
  2287. foo
  2288. ]: /url
  2289. bar
  2290. .
  2291. <p>bar</p>
  2292. ````````````````````````````````
  2293. This is not a link reference definition, because there are
  2294. [non-whitespace characters] after the title:
  2295. ```````````````````````````````` example
  2296. [foo]: /url "title" ok
  2297. .
  2298. <p>[foo]: /url &quot;title&quot; ok</p>
  2299. ````````````````````````````````
  2300. This is a link reference definition, but it has no title:
  2301. ```````````````````````````````` example
  2302. [foo]: /url
  2303. "title" ok
  2304. .
  2305. <p>&quot;title&quot; ok</p>
  2306. ````````````````````````````````
  2307. This is not a link reference definition, because it is indented
  2308. four spaces:
  2309. ```````````````````````````````` example
  2310. [foo]: /url "title"
  2311. [foo]
  2312. .
  2313. <pre><code>[foo]: /url &quot;title&quot;
  2314. </code></pre>
  2315. <p>[foo]</p>
  2316. ````````````````````````````````
  2317. This is not a link reference definition, because it occurs inside
  2318. a code block:
  2319. ```````````````````````````````` example
  2320. ```
  2321. [foo]: /url
  2322. ```
  2323. [foo]
  2324. .
  2325. <pre><code>[foo]: /url
  2326. </code></pre>
  2327. <p>[foo]</p>
  2328. ````````````````````````````````
  2329. A [link reference definition] cannot interrupt a paragraph.
  2330. ```````````````````````````````` example
  2331. Foo
  2332. [bar]: /baz
  2333. [bar]
  2334. .
  2335. <p>Foo
  2336. [bar]: /baz</p>
  2337. <p>[bar]</p>
  2338. ````````````````````````````````
  2339. However, it can directly follow other block elements, such as headings
  2340. and thematic breaks, and it need not be followed by a blank line.
  2341. ```````````````````````````````` example
  2342. # [Foo]
  2343. [foo]: /url
  2344. > bar
  2345. .
  2346. <h1><a href="/url">Foo</a></h1>
  2347. <blockquote>
  2348. <p>bar</p>
  2349. </blockquote>
  2350. ````````````````````````````````
  2351. Several [link reference definitions]
  2352. can occur one after another, without intervening blank lines.
  2353. ```````````````````````````````` example
  2354. [foo]: /foo-url "foo"
  2355. [bar]: /bar-url
  2356. "bar"
  2357. [baz]: /baz-url
  2358. [foo],
  2359. [bar],
  2360. [baz]
  2361. .
  2362. <p><a href="/foo-url" title="foo">foo</a>,
  2363. <a href="/bar-url" title="bar">bar</a>,
  2364. <a href="/baz-url">baz</a></p>
  2365. ````````````````````````````````
  2366. [Link reference definitions] can occur
  2367. inside block containers, like lists and block quotations. They
  2368. affect the entire document, not just the container in which they
  2369. are defined:
  2370. ```````````````````````````````` example
  2371. [foo]
  2372. > [foo]: /url
  2373. .
  2374. <p><a href="/url">foo</a></p>
  2375. <blockquote>
  2376. </blockquote>
  2377. ````````````````````````````````
  2378. ## Paragraphs
  2379. A sequence of non-blank lines that cannot be interpreted as other
  2380. kinds of blocks forms a [paragraph](@).
  2381. The contents of the paragraph are the result of parsing the
  2382. paragraph's raw content as inlines. The paragraph's raw content
  2383. is formed by concatenating the lines and removing initial and final
  2384. [whitespace].
  2385. A simple example with two paragraphs:
  2386. ```````````````````````````````` example
  2387. aaa
  2388. bbb
  2389. .
  2390. <p>aaa</p>
  2391. <p>bbb</p>
  2392. ````````````````````````````````
  2393. Paragraphs can contain multiple lines, but no blank lines:
  2394. ```````````````````````````````` example
  2395. aaa
  2396. bbb
  2397. ccc
  2398. ddd
  2399. .
  2400. <p>aaa
  2401. bbb</p>
  2402. <p>ccc
  2403. ddd</p>
  2404. ````````````````````````````````
  2405. Multiple blank lines between paragraph have no effect:
  2406. ```````````````````````````````` example
  2407. aaa
  2408. bbb
  2409. .
  2410. <p>aaa</p>
  2411. <p>bbb</p>
  2412. ````````````````````````````````
  2413. Leading spaces are skipped:
  2414. ```````````````````````````````` example
  2415. aaa
  2416. bbb
  2417. .
  2418. <p>aaa
  2419. bbb</p>
  2420. ````````````````````````````````
  2421. Lines after the first may be indented any amount, since indented
  2422. code blocks cannot interrupt paragraphs.
  2423. ```````````````````````````````` example
  2424. aaa
  2425. bbb
  2426. ccc
  2427. .
  2428. <p>aaa
  2429. bbb
  2430. ccc</p>
  2431. ````````````````````````````````
  2432. However, the first line may be indented at most three spaces,
  2433. or an indented code block will be triggered:
  2434. ```````````````````````````````` example
  2435. aaa
  2436. bbb
  2437. .
  2438. <p>aaa
  2439. bbb</p>
  2440. ````````````````````````````````
  2441. ```````````````````````````````` example
  2442. aaa
  2443. bbb
  2444. .
  2445. <pre><code>aaa
  2446. </code></pre>
  2447. <p>bbb</p>
  2448. ````````````````````````````````
  2449. Final spaces are stripped before inline parsing, so a paragraph
  2450. that ends with two or more spaces will not end with a [hard line
  2451. break]:
  2452. ```````````````````````````````` example
  2453. aaa
  2454. bbb
  2455. .
  2456. <p>aaa<br />
  2457. bbb</p>
  2458. ````````````````````````````````
  2459. ## Blank lines
  2460. [Blank lines] between block-level elements are ignored,
  2461. except for the role they play in determining whether a [list]
  2462. is [tight] or [loose].
  2463. Blank lines at the beginning and end of the document are also ignored.
  2464. ```````````````````````````````` example
  2465. aaa
  2466. # aaa
  2467. .
  2468. <p>aaa</p>
  2469. <h1>aaa</h1>
  2470. ````````````````````````````````
  2471. # Container blocks
  2472. A [container block](#container-blocks) is a block that has other
  2473. blocks as its contents. There are two basic kinds of container blocks:
  2474. [block quotes] and [list items].
  2475. [Lists] are meta-containers for [list items].
  2476. We define the syntax for container blocks recursively. The general
  2477. form of the definition is:
  2478. > If X is a sequence of blocks, then the result of
  2479. > transforming X in such-and-such a way is a container of type Y
  2480. > with these blocks as its content.
  2481. So, we explain what counts as a block quote or list item by explaining
  2482. how these can be *generated* from their contents. This should suffice
  2483. to define the syntax, although it does not give a recipe for *parsing*
  2484. these constructions. (A recipe is provided below in the section entitled
  2485. [A parsing strategy](#appendix-a-parsing-strategy).)
  2486. ## Block quotes
  2487. A [block quote marker](@)
  2488. consists of 0-3 spaces of initial indent, plus (a) the character `>` together
  2489. with a following space, or (b) a single character `>` not followed by a space.
  2490. The following rules define [block quotes]:
  2491. 1. **Basic case.** If a string of lines *Ls* constitute a sequence
  2492. of blocks *Bs*, then the result of prepending a [block quote
  2493. marker] to the beginning of each line in *Ls*
  2494. is a [block quote](#block-quotes) containing *Bs*.
  2495. 2. **Laziness.** If a string of lines *Ls* constitute a [block
  2496. quote](#block-quotes) with contents *Bs*, then the result of deleting
  2497. the initial [block quote marker] from one or
  2498. more lines in which the next [non-whitespace character] after the [block
  2499. quote marker] is [paragraph continuation
  2500. text] is a block quote with *Bs* as its content.
  2501. [Paragraph continuation text](@) is text
  2502. that will be parsed as part of the content of a paragraph, but does
  2503. not occur at the beginning of the paragraph.
  2504. 3. **Consecutiveness.** A document cannot contain two [block
  2505. quotes] in a row unless there is a [blank line] between them.
  2506. Nothing else counts as a [block quote](#block-quotes).
  2507. Here is a simple example:
  2508. ```````````````````````````````` example
  2509. > # Foo
  2510. > bar
  2511. > baz
  2512. .
  2513. <blockquote>
  2514. <h1>Foo</h1>
  2515. <p>bar
  2516. baz</p>
  2517. </blockquote>
  2518. ````````````````````````````````
  2519. The spaces after the `>` characters can be omitted:
  2520. ```````````````````````````````` example
  2521. ># Foo
  2522. >bar
  2523. > baz
  2524. .
  2525. <blockquote>
  2526. <h1>Foo</h1>
  2527. <p>bar
  2528. baz</p>
  2529. </blockquote>
  2530. ````````````````````````````````
  2531. The `>` characters can be indented 1-3 spaces:
  2532. ```````````````````````````````` example
  2533. > # Foo
  2534. > bar
  2535. > baz
  2536. .
  2537. <blockquote>
  2538. <h1>Foo</h1>
  2539. <p>bar
  2540. baz</p>
  2541. </blockquote>
  2542. ````````````````````````````````
  2543. Four spaces gives us a code block:
  2544. ```````````````````````````````` example
  2545. > # Foo
  2546. > bar
  2547. > baz
  2548. .
  2549. <pre><code>&gt; # Foo
  2550. &gt; bar
  2551. &gt; baz
  2552. </code></pre>
  2553. ````````````````````````````````
  2554. The Laziness clause allows us to omit the `>` before
  2555. [paragraph continuation text]:
  2556. ```````````````````````````````` example
  2557. > # Foo
  2558. > bar
  2559. baz
  2560. .
  2561. <blockquote>
  2562. <h1>Foo</h1>
  2563. <p>bar
  2564. baz</p>
  2565. </blockquote>
  2566. ````````````````````````````````
  2567. A block quote can contain some lazy and some non-lazy
  2568. continuation lines:
  2569. ```````````````````````````````` example
  2570. > bar
  2571. baz
  2572. > foo
  2573. .
  2574. <blockquote>
  2575. <p>bar
  2576. baz
  2577. foo</p>
  2578. </blockquote>
  2579. ````````````````````````````````
  2580. Laziness only applies to lines that would have been continuations of
  2581. paragraphs had they been prepended with [block quote markers].
  2582. For example, the `> ` cannot be omitted in the second line of
  2583. ``` markdown
  2584. > foo
  2585. > ---
  2586. ```
  2587. without changing the meaning:
  2588. ```````````````````````````````` example
  2589. > foo
  2590. ---
  2591. .
  2592. <blockquote>
  2593. <p>foo</p>
  2594. </blockquote>
  2595. <hr />
  2596. ````````````````````````````````
  2597. Similarly, if we omit the `> ` in the second line of
  2598. ``` markdown
  2599. > - foo
  2600. > - bar
  2601. ```
  2602. then the block quote ends after the first line:
  2603. ```````````````````````````````` example
  2604. > - foo
  2605. - bar
  2606. .
  2607. <blockquote>
  2608. <ul>
  2609. <li>foo</li>
  2610. </ul>
  2611. </blockquote>
  2612. <ul>
  2613. <li>bar</li>
  2614. </ul>
  2615. ````````````````````````````````
  2616. For the same reason, we can't omit the `> ` in front of
  2617. subsequent lines of an indented or fenced code block:
  2618. ```````````````````````````````` example
  2619. > foo
  2620. bar
  2621. .
  2622. <blockquote>
  2623. <pre><code>foo
  2624. </code></pre>
  2625. </blockquote>
  2626. <pre><code>bar
  2627. </code></pre>
  2628. ````````````````````````````````
  2629. ```````````````````````````````` example
  2630. > ```
  2631. foo
  2632. ```
  2633. .
  2634. <blockquote>
  2635. <pre><code></code></pre>
  2636. </blockquote>
  2637. <p>foo</p>
  2638. <pre><code></code></pre>
  2639. ````````````````````````````````
  2640. Note that in the following case, we have a [lazy
  2641. continuation line]:
  2642. ```````````````````````````````` example
  2643. > foo
  2644. - bar
  2645. .
  2646. <blockquote>
  2647. <p>foo
  2648. - bar</p>
  2649. </blockquote>
  2650. ````````````````````````````````
  2651. To see why, note that in
  2652. ```markdown
  2653. > foo
  2654. > - bar
  2655. ```
  2656. the `- bar` is indented too far to start a list, and can't
  2657. be an indented code block because indented code blocks cannot
  2658. interrupt paragraphs, so it is [paragraph continuation text].
  2659. A block quote can be empty:
  2660. ```````````````````````````````` example
  2661. >
  2662. .
  2663. <blockquote>
  2664. </blockquote>
  2665. ````````````````````````````````
  2666. ```````````````````````````````` example
  2667. >
  2668. >
  2669. >
  2670. .
  2671. <blockquote>
  2672. </blockquote>
  2673. ````````````````````````````````
  2674. A block quote can have initial or final blank lines:
  2675. ```````````````````````````````` example
  2676. >
  2677. > foo
  2678. >
  2679. .
  2680. <blockquote>
  2681. <p>foo</p>
  2682. </blockquote>
  2683. ````````````````````````````````
  2684. A blank line always separates block quotes:
  2685. ```````````````````````````````` example
  2686. > foo
  2687. > bar
  2688. .
  2689. <blockquote>
  2690. <p>foo</p>
  2691. </blockquote>
  2692. <blockquote>
  2693. <p>bar</p>
  2694. </blockquote>
  2695. ````````````````````````````````
  2696. (Most current Markdown implementations, including John Gruber's
  2697. original `Markdown.pl`, will parse this example as a single block quote
  2698. with two paragraphs. But it seems better to allow the author to decide
  2699. whether two block quotes or one are wanted.)
  2700. Consecutiveness means that if we put these block quotes together,
  2701. we get a single block quote:
  2702. ```````````````````````````````` example
  2703. > foo
  2704. > bar
  2705. .
  2706. <blockquote>
  2707. <p>foo
  2708. bar</p>
  2709. </blockquote>
  2710. ````````````````````````````````
  2711. To get a block quote with two paragraphs, use:
  2712. ```````````````````````````````` example
  2713. > foo
  2714. >
  2715. > bar
  2716. .
  2717. <blockquote>
  2718. <p>foo</p>
  2719. <p>bar</p>
  2720. </blockquote>
  2721. ````````````````````````````````
  2722. Block quotes can interrupt paragraphs:
  2723. ```````````````````````````````` example
  2724. foo
  2725. > bar
  2726. .
  2727. <p>foo</p>
  2728. <blockquote>
  2729. <p>bar</p>
  2730. </blockquote>
  2731. ````````````````````````````````
  2732. In general, blank lines are not needed before or after block
  2733. quotes:
  2734. ```````````````````````````````` example
  2735. > aaa
  2736. ***
  2737. > bbb
  2738. .
  2739. <blockquote>
  2740. <p>aaa</p>
  2741. </blockquote>
  2742. <hr />
  2743. <blockquote>
  2744. <p>bbb</p>
  2745. </blockquote>
  2746. ````````````````````````````````
  2747. However, because of laziness, a blank line is needed between
  2748. a block quote and a following paragraph:
  2749. ```````````````````````````````` example
  2750. > bar
  2751. baz
  2752. .
  2753. <blockquote>
  2754. <p>bar
  2755. baz</p>
  2756. </blockquote>
  2757. ````````````````````````````````
  2758. ```````````````````````````````` example
  2759. > bar
  2760. baz
  2761. .
  2762. <blockquote>
  2763. <p>bar</p>
  2764. </blockquote>
  2765. <p>baz</p>
  2766. ````````````````````````````````
  2767. ```````````````````````````````` example
  2768. > bar
  2769. >
  2770. baz
  2771. .
  2772. <blockquote>
  2773. <p>bar</p>
  2774. </blockquote>
  2775. <p>baz</p>
  2776. ````````````````````````````````
  2777. It is a consequence of the Laziness rule that any number
  2778. of initial `>`s may be omitted on a continuation line of a
  2779. nested block quote:
  2780. ```````````````````````````````` example
  2781. > > > foo
  2782. bar
  2783. .
  2784. <blockquote>
  2785. <blockquote>
  2786. <blockquote>
  2787. <p>foo
  2788. bar</p>
  2789. </blockquote>
  2790. </blockquote>
  2791. </blockquote>
  2792. ````````````````````````````````
  2793. ```````````````````````````````` example
  2794. >>> foo
  2795. > bar
  2796. >>baz
  2797. .
  2798. <blockquote>
  2799. <blockquote>
  2800. <blockquote>
  2801. <p>foo
  2802. bar
  2803. baz</p>
  2804. </blockquote>
  2805. </blockquote>
  2806. </blockquote>
  2807. ````````````````````````````````
  2808. When including an indented code block in a block quote,
  2809. remember that the [block quote marker] includes
  2810. both the `>` and a following space. So *five spaces* are needed after
  2811. the `>`:
  2812. ```````````````````````````````` example
  2813. > code
  2814. > not code
  2815. .
  2816. <blockquote>
  2817. <pre><code>code
  2818. </code></pre>
  2819. </blockquote>
  2820. <blockquote>
  2821. <p>not code</p>
  2822. </blockquote>
  2823. ````````````````````````````````
  2824. ## List items
  2825. A [list marker](@) is a
  2826. [bullet list marker] or an [ordered list marker].
  2827. A [bullet list marker](@)
  2828. is a `-`, `+`, or `*` character.
  2829. An [ordered list marker](@)
  2830. is a sequence of 1--9 arabic digits (`0-9`), followed by either a
  2831. `.` character or a `)` character. (The reason for the length
  2832. limit is that with 10 digits we start seeing integer overflows
  2833. in some browsers.)
  2834. The following rules define [list items]:
  2835. 1. **Basic case.** If a sequence of lines *Ls* constitute a sequence of
  2836. blocks *Bs* starting with a [non-whitespace character], and *M* is a
  2837. list marker of width *W* followed by 1 ≤ *N* ≤ 4 spaces, then the result
  2838. of prepending *M* and the following spaces to the first line of
  2839. *Ls*, and indenting subsequent lines of *Ls* by *W + N* spaces, is a
  2840. list item with *Bs* as its contents. The type of the list item
  2841. (bullet or ordered) is determined by the type of its list marker.
  2842. If the list item is ordered, then it is also assigned a start
  2843. number, based on the ordered list marker.
  2844. Exceptions:
  2845. 1. When the first list item in a [list] interrupts
  2846. a paragraph---that is, when it starts on a line that would
  2847. otherwise count as [paragraph continuation text]---then (a)
  2848. the lines *Ls* must not begin with a blank line, and (b) if
  2849. the list item is ordered, the start number must be 1.
  2850. 2. If any line is a [thematic break][thematic breaks] then
  2851. that line is not a list item.
  2852. For example, let *Ls* be the lines
  2853. ```````````````````````````````` example
  2854. A paragraph
  2855. with two lines.
  2856. indented code
  2857. > A block quote.
  2858. .
  2859. <p>A paragraph
  2860. with two lines.</p>
  2861. <pre><code>indented code
  2862. </code></pre>
  2863. <blockquote>
  2864. <p>A block quote.</p>
  2865. </blockquote>
  2866. ````````````````````````````````
  2867. And let *M* be the marker `1.`, and *N* = 2. Then rule #1 says
  2868. that the following is an ordered list item with start number 1,
  2869. and the same contents as *Ls*:
  2870. ```````````````````````````````` example
  2871. 1. A paragraph
  2872. with two lines.
  2873. indented code
  2874. > A block quote.
  2875. .
  2876. <ol>
  2877. <li>
  2878. <p>A paragraph
  2879. with two lines.</p>
  2880. <pre><code>indented code
  2881. </code></pre>
  2882. <blockquote>
  2883. <p>A block quote.</p>
  2884. </blockquote>
  2885. </li>
  2886. </ol>
  2887. ````````````````````````````````
  2888. The most important thing to notice is that the position of
  2889. the text after the list marker determines how much indentation
  2890. is needed in subsequent blocks in the list item. If the list
  2891. marker takes up two spaces, and there are three spaces between
  2892. the list marker and the next [non-whitespace character], then blocks
  2893. must be indented five spaces in order to fall under the list
  2894. item.
  2895. Here are some examples showing how far content must be indented to be
  2896. put under the list item:
  2897. ```````````````````````````````` example
  2898. - one
  2899. two
  2900. .
  2901. <ul>
  2902. <li>one</li>
  2903. </ul>
  2904. <p>two</p>
  2905. ````````````````````````````````
  2906. ```````````````````````````````` example
  2907. - one
  2908. two
  2909. .
  2910. <ul>
  2911. <li>
  2912. <p>one</p>
  2913. <p>two</p>
  2914. </li>
  2915. </ul>
  2916. ````````````````````````````````
  2917. ```````````````````````````````` example
  2918. - one
  2919. two
  2920. .
  2921. <ul>
  2922. <li>one</li>
  2923. </ul>
  2924. <pre><code> two
  2925. </code></pre>
  2926. ````````````````````````````````
  2927. ```````````````````````````````` example
  2928. - one
  2929. two
  2930. .
  2931. <ul>
  2932. <li>
  2933. <p>one</p>
  2934. <p>two</p>
  2935. </li>
  2936. </ul>
  2937. ````````````````````````````````
  2938. It is tempting to think of this in terms of columns: the continuation
  2939. blocks must be indented at least to the column of the first
  2940. [non-whitespace character] after the list marker. However, that is not quite right.
  2941. The spaces after the list marker determine how much relative indentation
  2942. is needed. Which column this indentation reaches will depend on
  2943. how the list item is embedded in other constructions, as shown by
  2944. this example:
  2945. ```````````````````````````````` example
  2946. > > 1. one
  2947. >>
  2948. >> two
  2949. .
  2950. <blockquote>
  2951. <blockquote>
  2952. <ol>
  2953. <li>
  2954. <p>one</p>
  2955. <p>two</p>
  2956. </li>
  2957. </ol>
  2958. </blockquote>
  2959. </blockquote>
  2960. ````````````````````````````````
  2961. Here `two` occurs in the same column as the list marker `1.`,
  2962. but is actually contained in the list item, because there is
  2963. sufficient indentation after the last containing blockquote marker.
  2964. The converse is also possible. In the following example, the word `two`
  2965. occurs far to the right of the initial text of the list item, `one`, but
  2966. it is not considered part of the list item, because it is not indented
  2967. far enough past the blockquote marker:
  2968. ```````````````````````````````` example
  2969. >>- one
  2970. >>
  2971. > > two
  2972. .
  2973. <blockquote>
  2974. <blockquote>
  2975. <ul>
  2976. <li>one</li>
  2977. </ul>
  2978. <p>two</p>
  2979. </blockquote>
  2980. </blockquote>
  2981. ````````````````````````````````
  2982. Note that at least one space is needed between the list marker and
  2983. any following content, so these are not list items:
  2984. ```````````````````````````````` example
  2985. -one
  2986. 2.two
  2987. .
  2988. <p>-one</p>
  2989. <p>2.two</p>
  2990. ````````````````````````````````
  2991. A list item may contain blocks that are separated by more than
  2992. one blank line.
  2993. ```````````````````````````````` example
  2994. - foo
  2995. bar
  2996. .
  2997. <ul>
  2998. <li>
  2999. <p>foo</p>
  3000. <p>bar</p>
  3001. </li>
  3002. </ul>
  3003. ````````````````````````````````
  3004. A list item may contain any kind of block:
  3005. ```````````````````````````````` example
  3006. 1. foo
  3007. ```
  3008. bar
  3009. ```
  3010. baz
  3011. > bam
  3012. .
  3013. <ol>
  3014. <li>
  3015. <p>foo</p>
  3016. <pre><code>bar
  3017. </code></pre>
  3018. <p>baz</p>
  3019. <blockquote>
  3020. <p>bam</p>
  3021. </blockquote>
  3022. </li>
  3023. </ol>
  3024. ````````````````````````````````
  3025. A list item that contains an indented code block will preserve
  3026. empty lines within the code block verbatim.
  3027. ```````````````````````````````` example
  3028. - Foo
  3029. bar
  3030. baz
  3031. .
  3032. <ul>
  3033. <li>
  3034. <p>Foo</p>
  3035. <pre><code>bar
  3036. baz
  3037. </code></pre>
  3038. </li>
  3039. </ul>
  3040. ````````````````````````````````
  3041. Note that ordered list start numbers must be nine digits or less:
  3042. ```````````````````````````````` example
  3043. 123456789. ok
  3044. .
  3045. <ol start="123456789">
  3046. <li>ok</li>
  3047. </ol>
  3048. ````````````````````````````````
  3049. ```````````````````````````````` example
  3050. 1234567890. not ok
  3051. .
  3052. <p>1234567890. not ok</p>
  3053. ````````````````````````````````
  3054. A start number may begin with 0s:
  3055. ```````````````````````````````` example
  3056. 0. ok
  3057. .
  3058. <ol start="0">
  3059. <li>ok</li>
  3060. </ol>
  3061. ````````````````````````````````
  3062. ```````````````````````````````` example
  3063. 003. ok
  3064. .
  3065. <ol start="3">
  3066. <li>ok</li>
  3067. </ol>
  3068. ````````````````````````````````
  3069. A start number may not be negative:
  3070. ```````````````````````````````` example
  3071. -1. not ok
  3072. .
  3073. <p>-1. not ok</p>
  3074. ````````````````````````````````
  3075. 2. **Item starting with indented code.** If a sequence of lines *Ls*
  3076. constitute a sequence of blocks *Bs* starting with an indented code
  3077. block, and *M* is a list marker of width *W* followed by
  3078. one space, then the result of prepending *M* and the following
  3079. space to the first line of *Ls*, and indenting subsequent lines of
  3080. *Ls* by *W + 1* spaces, is a list item with *Bs* as its contents.
  3081. If a line is empty, then it need not be indented. The type of the
  3082. list item (bullet or ordered) is determined by the type of its list
  3083. marker. If the list item is ordered, then it is also assigned a
  3084. start number, based on the ordered list marker.
  3085. An indented code block will have to be indented four spaces beyond
  3086. the edge of the region where text will be included in the list item.
  3087. In the following case that is 6 spaces:
  3088. ```````````````````````````````` example
  3089. - foo
  3090. bar
  3091. .
  3092. <ul>
  3093. <li>
  3094. <p>foo</p>
  3095. <pre><code>bar
  3096. </code></pre>
  3097. </li>
  3098. </ul>
  3099. ````````````````````````````````
  3100. And in this case it is 11 spaces:
  3101. ```````````````````````````````` example
  3102. 10. foo
  3103. bar
  3104. .
  3105. <ol start="10">
  3106. <li>
  3107. <p>foo</p>
  3108. <pre><code>bar
  3109. </code></pre>
  3110. </li>
  3111. </ol>
  3112. ````````````````````````````````
  3113. If the *first* block in the list item is an indented code block,
  3114. then by rule #2, the contents must be indented *one* space after the
  3115. list marker:
  3116. ```````````````````````````````` example
  3117. indented code
  3118. paragraph
  3119. more code
  3120. .
  3121. <pre><code>indented code
  3122. </code></pre>
  3123. <p>paragraph</p>
  3124. <pre><code>more code
  3125. </code></pre>
  3126. ````````````````````````````````
  3127. ```````````````````````````````` example
  3128. 1. indented code
  3129. paragraph
  3130. more code
  3131. .
  3132. <ol>
  3133. <li>
  3134. <pre><code>indented code
  3135. </code></pre>
  3136. <p>paragraph</p>
  3137. <pre><code>more code
  3138. </code></pre>
  3139. </li>
  3140. </ol>
  3141. ````````````````````````````````
  3142. Note that an additional space indent is interpreted as space
  3143. inside the code block:
  3144. ```````````````````````````````` example
  3145. 1. indented code
  3146. paragraph
  3147. more code
  3148. .
  3149. <ol>
  3150. <li>
  3151. <pre><code> indented code
  3152. </code></pre>
  3153. <p>paragraph</p>
  3154. <pre><code>more code
  3155. </code></pre>
  3156. </li>
  3157. </ol>
  3158. ````````````````````````````````
  3159. Note that rules #1 and #2 only apply to two cases: (a) cases
  3160. in which the lines to be included in a list item begin with a
  3161. [non-whitespace character], and (b) cases in which
  3162. they begin with an indented code
  3163. block. In a case like the following, where the first block begins with
  3164. a three-space indent, the rules do not allow us to form a list item by
  3165. indenting the whole thing and prepending a list marker:
  3166. ```````````````````````````````` example
  3167. foo
  3168. bar
  3169. .
  3170. <p>foo</p>
  3171. <p>bar</p>
  3172. ````````````````````````````````
  3173. ```````````````````````````````` example
  3174. - foo
  3175. bar
  3176. .
  3177. <ul>
  3178. <li>foo</li>
  3179. </ul>
  3180. <p>bar</p>
  3181. ````````````````````````````````
  3182. This is not a significant restriction, because when a block begins
  3183. with 1-3 spaces indent, the indentation can always be removed without
  3184. a change in interpretation, allowing rule #1 to be applied. So, in
  3185. the above case:
  3186. ```````````````````````````````` example
  3187. - foo
  3188. bar
  3189. .
  3190. <ul>
  3191. <li>
  3192. <p>foo</p>
  3193. <p>bar</p>
  3194. </li>
  3195. </ul>
  3196. ````````````````````````````````
  3197. 3. **Item starting with a blank line.** If a sequence of lines *Ls*
  3198. starting with a single [blank line] constitute a (possibly empty)
  3199. sequence of blocks *Bs*, not separated from each other by more than
  3200. one blank line, and *M* is a list marker of width *W*,
  3201. then the result of prepending *M* to the first line of *Ls*, and
  3202. indenting subsequent lines of *Ls* by *W + 1* spaces, is a list
  3203. item with *Bs* as its contents.
  3204. If a line is empty, then it need not be indented. The type of the
  3205. list item (bullet or ordered) is determined by the type of its list
  3206. marker. If the list item is ordered, then it is also assigned a
  3207. start number, based on the ordered list marker.
  3208. Here are some list items that start with a blank line but are not empty:
  3209. ```````````````````````````````` example
  3210. -
  3211. foo
  3212. -
  3213. ```
  3214. bar
  3215. ```
  3216. -
  3217. baz
  3218. .
  3219. <ul>
  3220. <li>foo</li>
  3221. <li>
  3222. <pre><code>bar
  3223. </code></pre>
  3224. </li>
  3225. <li>
  3226. <pre><code>baz
  3227. </code></pre>
  3228. </li>
  3229. </ul>
  3230. ````````````````````````````````
  3231. When the list item starts with a blank line, the number of spaces
  3232. following the list marker doesn't change the required indentation:
  3233. ```````````````````````````````` example
  3234. -
  3235. foo
  3236. .
  3237. <ul>
  3238. <li>foo</li>
  3239. </ul>
  3240. ````````````````````````````````
  3241. A list item can begin with at most one blank line.
  3242. In the following example, `foo` is not part of the list
  3243. item:
  3244. ```````````````````````````````` example
  3245. -
  3246. foo
  3247. .
  3248. <ul>
  3249. <li></li>
  3250. </ul>
  3251. <p>foo</p>
  3252. ````````````````````````````````
  3253. Here is an empty bullet list item:
  3254. ```````````````````````````````` example
  3255. - foo
  3256. -
  3257. - bar
  3258. .
  3259. <ul>
  3260. <li>foo</li>
  3261. <li></li>
  3262. <li>bar</li>
  3263. </ul>
  3264. ````````````````````````````````
  3265. It does not matter whether there are spaces following the [list marker]:
  3266. ```````````````````````````````` example
  3267. - foo
  3268. -
  3269. - bar
  3270. .
  3271. <ul>
  3272. <li>foo</li>
  3273. <li></li>
  3274. <li>bar</li>
  3275. </ul>
  3276. ````````````````````````````````
  3277. Here is an empty ordered list item:
  3278. ```````````````````````````````` example
  3279. 1. foo
  3280. 2.
  3281. 3. bar
  3282. .
  3283. <ol>
  3284. <li>foo</li>
  3285. <li></li>
  3286. <li>bar</li>
  3287. </ol>
  3288. ````````````````````````````````
  3289. A list may start or end with an empty list item:
  3290. ```````````````````````````````` example
  3291. *
  3292. .
  3293. <ul>
  3294. <li></li>
  3295. </ul>
  3296. ````````````````````````````````
  3297. However, an empty list item cannot interrupt a paragraph:
  3298. ```````````````````````````````` example
  3299. foo
  3300. *
  3301. foo
  3302. 1.
  3303. .
  3304. <p>foo
  3305. *</p>
  3306. <p>foo
  3307. 1.</p>
  3308. ````````````````````````````````
  3309. 4. **Indentation.** If a sequence of lines *Ls* constitutes a list item
  3310. according to rule #1, #2, or #3, then the result of indenting each line
  3311. of *Ls* by 1-3 spaces (the same for each line) also constitutes a
  3312. list item with the same contents and attributes. If a line is
  3313. empty, then it need not be indented.
  3314. Indented one space:
  3315. ```````````````````````````````` example
  3316. 1. A paragraph
  3317. with two lines.
  3318. indented code
  3319. > A block quote.
  3320. .
  3321. <ol>
  3322. <li>
  3323. <p>A paragraph
  3324. with two lines.</p>
  3325. <pre><code>indented code
  3326. </code></pre>
  3327. <blockquote>
  3328. <p>A block quote.</p>
  3329. </blockquote>
  3330. </li>
  3331. </ol>
  3332. ````````````````````````````````
  3333. Indented two spaces:
  3334. ```````````````````````````````` example
  3335. 1. A paragraph
  3336. with two lines.
  3337. indented code
  3338. > A block quote.
  3339. .
  3340. <ol>
  3341. <li>
  3342. <p>A paragraph
  3343. with two lines.</p>
  3344. <pre><code>indented code
  3345. </code></pre>
  3346. <blockquote>
  3347. <p>A block quote.</p>
  3348. </blockquote>
  3349. </li>
  3350. </ol>
  3351. ````````````````````````````````
  3352. Indented three spaces:
  3353. ```````````````````````````````` example
  3354. 1. A paragraph
  3355. with two lines.
  3356. indented code
  3357. > A block quote.
  3358. .
  3359. <ol>
  3360. <li>
  3361. <p>A paragraph
  3362. with two lines.</p>
  3363. <pre><code>indented code
  3364. </code></pre>
  3365. <blockquote>
  3366. <p>A block quote.</p>
  3367. </blockquote>
  3368. </li>
  3369. </ol>
  3370. ````````````````````````````````
  3371. Four spaces indent gives a code block:
  3372. ```````````````````````````````` example
  3373. 1. A paragraph
  3374. with two lines.
  3375. indented code
  3376. > A block quote.
  3377. .
  3378. <pre><code>1. A paragraph
  3379. with two lines.
  3380. indented code
  3381. &gt; A block quote.
  3382. </code></pre>
  3383. ````````````````````````````````
  3384. 5. **Laziness.** If a string of lines *Ls* constitute a [list
  3385. item](#list-items) with contents *Bs*, then the result of deleting
  3386. some or all of the indentation from one or more lines in which the
  3387. next [non-whitespace character] after the indentation is
  3388. [paragraph continuation text] is a
  3389. list item with the same contents and attributes. The unindented
  3390. lines are called
  3391. [lazy continuation line](@)s.
  3392. Here is an example with [lazy continuation lines]:
  3393. ```````````````````````````````` example
  3394. 1. A paragraph
  3395. with two lines.
  3396. indented code
  3397. > A block quote.
  3398. .
  3399. <ol>
  3400. <li>
  3401. <p>A paragraph
  3402. with two lines.</p>
  3403. <pre><code>indented code
  3404. </code></pre>
  3405. <blockquote>
  3406. <p>A block quote.</p>
  3407. </blockquote>
  3408. </li>
  3409. </ol>
  3410. ````````````````````````````````
  3411. Indentation can be partially deleted:
  3412. ```````````````````````````````` example
  3413. 1. A paragraph
  3414. with two lines.
  3415. .
  3416. <ol>
  3417. <li>A paragraph
  3418. with two lines.</li>
  3419. </ol>
  3420. ````````````````````````````````
  3421. These examples show how laziness can work in nested structures:
  3422. ```````````````````````````````` example
  3423. > 1. > Blockquote
  3424. continued here.
  3425. .
  3426. <blockquote>
  3427. <ol>
  3428. <li>
  3429. <blockquote>
  3430. <p>Blockquote
  3431. continued here.</p>
  3432. </blockquote>
  3433. </li>
  3434. </ol>
  3435. </blockquote>
  3436. ````````````````````````````````
  3437. ```````````````````````````````` example
  3438. > 1. > Blockquote
  3439. > continued here.
  3440. .
  3441. <blockquote>
  3442. <ol>
  3443. <li>
  3444. <blockquote>
  3445. <p>Blockquote
  3446. continued here.</p>
  3447. </blockquote>
  3448. </li>
  3449. </ol>
  3450. </blockquote>
  3451. ````````````````````````````````
  3452. 6. **That's all.** Nothing that is not counted as a list item by rules
  3453. #1--5 counts as a [list item](#list-items).
  3454. The rules for sublists follow from the general rules
  3455. [above][List items]. A sublist must be indented the same number
  3456. of spaces a paragraph would need to be in order to be included
  3457. in the list item.
  3458. So, in this case we need two spaces indent:
  3459. ```````````````````````````````` example
  3460. - foo
  3461. - bar
  3462. - baz
  3463. - boo
  3464. .
  3465. <ul>
  3466. <li>foo
  3467. <ul>
  3468. <li>bar
  3469. <ul>
  3470. <li>baz
  3471. <ul>
  3472. <li>boo</li>
  3473. </ul>
  3474. </li>
  3475. </ul>
  3476. </li>
  3477. </ul>
  3478. </li>
  3479. </ul>
  3480. ````````````````````````````````
  3481. One is not enough:
  3482. ```````````````````````````````` example
  3483. - foo
  3484. - bar
  3485. - baz
  3486. - boo
  3487. .
  3488. <ul>
  3489. <li>foo</li>
  3490. <li>bar</li>
  3491. <li>baz</li>
  3492. <li>boo</li>
  3493. </ul>
  3494. ````````````````````````````````
  3495. Here we need four, because the list marker is wider:
  3496. ```````````````````````````````` example
  3497. 10) foo
  3498. - bar
  3499. .
  3500. <ol start="10">
  3501. <li>foo
  3502. <ul>
  3503. <li>bar</li>
  3504. </ul>
  3505. </li>
  3506. </ol>
  3507. ````````````````````````````````
  3508. Three is not enough:
  3509. ```````````````````````````````` example
  3510. 10) foo
  3511. - bar
  3512. .
  3513. <ol start="10">
  3514. <li>foo</li>
  3515. </ol>
  3516. <ul>
  3517. <li>bar</li>
  3518. </ul>
  3519. ````````````````````````````````
  3520. A list may be the first block in a list item:
  3521. ```````````````````````````````` example
  3522. - - foo
  3523. .
  3524. <ul>
  3525. <li>
  3526. <ul>
  3527. <li>foo</li>
  3528. </ul>
  3529. </li>
  3530. </ul>
  3531. ````````````````````````````````
  3532. ```````````````````````````````` example
  3533. 1. - 2. foo
  3534. .
  3535. <ol>
  3536. <li>
  3537. <ul>
  3538. <li>
  3539. <ol start="2">
  3540. <li>foo</li>
  3541. </ol>
  3542. </li>
  3543. </ul>
  3544. </li>
  3545. </ol>
  3546. ````````````````````````````````
  3547. A list item can contain a heading:
  3548. ```````````````````````````````` example
  3549. - # Foo
  3550. - Bar
  3551. ---
  3552. baz
  3553. .
  3554. <ul>
  3555. <li>
  3556. <h1>Foo</h1>
  3557. </li>
  3558. <li>
  3559. <h2>Bar</h2>
  3560. baz</li>
  3561. </ul>
  3562. ````````````````````````````````
  3563. ### Motivation
  3564. John Gruber's Markdown spec says the following about list items:
  3565. 1. "List markers typically start at the left margin, but may be indented
  3566. by up to three spaces. List markers must be followed by one or more
  3567. spaces or a tab."
  3568. 2. "To make lists look nice, you can wrap items with hanging indents....
  3569. But if you don't want to, you don't have to."
  3570. 3. "List items may consist of multiple paragraphs. Each subsequent
  3571. paragraph in a list item must be indented by either 4 spaces or one
  3572. tab."
  3573. 4. "It looks nice if you indent every line of the subsequent paragraphs,
  3574. but here again, Markdown will allow you to be lazy."
  3575. 5. "To put a blockquote within a list item, the blockquote's `>`
  3576. delimiters need to be indented."
  3577. 6. "To put a code block within a list item, the code block needs to be
  3578. indented twice — 8 spaces or two tabs."
  3579. These rules specify that a paragraph under a list item must be indented
  3580. four spaces (presumably, from the left margin, rather than the start of
  3581. the list marker, but this is not said), and that code under a list item
  3582. must be indented eight spaces instead of the usual four. They also say
  3583. that a block quote must be indented, but not by how much; however, the
  3584. example given has four spaces indentation. Although nothing is said
  3585. about other kinds of block-level content, it is certainly reasonable to
  3586. infer that *all* block elements under a list item, including other
  3587. lists, must be indented four spaces. This principle has been called the
  3588. *four-space rule*.
  3589. The four-space rule is clear and principled, and if the reference
  3590. implementation `Markdown.pl` had followed it, it probably would have
  3591. become the standard. However, `Markdown.pl` allowed paragraphs and
  3592. sublists to start with only two spaces indentation, at least on the
  3593. outer level. Worse, its behavior was inconsistent: a sublist of an
  3594. outer-level list needed two spaces indentation, but a sublist of this
  3595. sublist needed three spaces. It is not surprising, then, that different
  3596. implementations of Markdown have developed very different rules for
  3597. determining what comes under a list item. (Pandoc and python-Markdown,
  3598. for example, stuck with Gruber's syntax description and the four-space
  3599. rule, while discount, redcarpet, marked, PHP Markdown, and others
  3600. followed `Markdown.pl`'s behavior more closely.)
  3601. Unfortunately, given the divergences between implementations, there
  3602. is no way to give a spec for list items that will be guaranteed not
  3603. to break any existing documents. However, the spec given here should
  3604. correctly handle lists formatted with either the four-space rule or
  3605. the more forgiving `Markdown.pl` behavior, provided they are laid out
  3606. in a way that is natural for a human to read.
  3607. The strategy here is to let the width and indentation of the list marker
  3608. determine the indentation necessary for blocks to fall under the list
  3609. item, rather than having a fixed and arbitrary number. The writer can
  3610. think of the body of the list item as a unit which gets indented to the
  3611. right enough to fit the list marker (and any indentation on the list
  3612. marker). (The laziness rule, #5, then allows continuation lines to be
  3613. unindented if needed.)
  3614. This rule is superior, we claim, to any rule requiring a fixed level of
  3615. indentation from the margin. The four-space rule is clear but
  3616. unnatural. It is quite unintuitive that
  3617. ``` markdown
  3618. - foo
  3619. bar
  3620. - baz
  3621. ```
  3622. should be parsed as two lists with an intervening paragraph,
  3623. ``` html
  3624. <ul>
  3625. <li>foo</li>
  3626. </ul>
  3627. <p>bar</p>
  3628. <ul>
  3629. <li>baz</li>
  3630. </ul>
  3631. ```
  3632. as the four-space rule demands, rather than a single list,
  3633. ``` html
  3634. <ul>
  3635. <li>
  3636. <p>foo</p>
  3637. <p>bar</p>
  3638. <ul>
  3639. <li>baz</li>
  3640. </ul>
  3641. </li>
  3642. </ul>
  3643. ```
  3644. The choice of four spaces is arbitrary. It can be learned, but it is
  3645. not likely to be guessed, and it trips up beginners regularly.
  3646. Would it help to adopt a two-space rule? The problem is that such
  3647. a rule, together with the rule allowing 1--3 spaces indentation of the
  3648. initial list marker, allows text that is indented *less than* the
  3649. original list marker to be included in the list item. For example,
  3650. `Markdown.pl` parses
  3651. ``` markdown
  3652. - one
  3653. two
  3654. ```
  3655. as a single list item, with `two` a continuation paragraph:
  3656. ``` html
  3657. <ul>
  3658. <li>
  3659. <p>one</p>
  3660. <p>two</p>
  3661. </li>
  3662. </ul>
  3663. ```
  3664. and similarly
  3665. ``` markdown
  3666. > - one
  3667. >
  3668. > two
  3669. ```
  3670. as
  3671. ``` html
  3672. <blockquote>
  3673. <ul>
  3674. <li>
  3675. <p>one</p>
  3676. <p>two</p>
  3677. </li>
  3678. </ul>
  3679. </blockquote>
  3680. ```
  3681. This is extremely unintuitive.
  3682. Rather than requiring a fixed indent from the margin, we could require
  3683. a fixed indent (say, two spaces, or even one space) from the list marker (which
  3684. may itself be indented). This proposal would remove the last anomaly
  3685. discussed. Unlike the spec presented above, it would count the following
  3686. as a list item with a subparagraph, even though the paragraph `bar`
  3687. is not indented as far as the first paragraph `foo`:
  3688. ``` markdown
  3689. 10. foo
  3690. bar
  3691. ```
  3692. Arguably this text does read like a list item with `bar` as a subparagraph,
  3693. which may count in favor of the proposal. However, on this proposal indented
  3694. code would have to be indented six spaces after the list marker. And this
  3695. would break a lot of existing Markdown, which has the pattern:
  3696. ``` markdown
  3697. 1. foo
  3698. indented code
  3699. ```
  3700. where the code is indented eight spaces. The spec above, by contrast, will
  3701. parse this text as expected, since the code block's indentation is measured
  3702. from the beginning of `foo`.
  3703. The one case that needs special treatment is a list item that *starts*
  3704. with indented code. How much indentation is required in that case, since
  3705. we don't have a "first paragraph" to measure from? Rule #2 simply stipulates
  3706. that in such cases, we require one space indentation from the list marker
  3707. (and then the normal four spaces for the indented code). This will match the
  3708. four-space rule in cases where the list marker plus its initial indentation
  3709. takes four spaces (a common case), but diverge in other cases.
  3710. ## Lists
  3711. A [list](@) is a sequence of one or more
  3712. list items [of the same type]. The list items
  3713. may be separated by any number of blank lines.
  3714. Two list items are [of the same type](@)
  3715. if they begin with a [list marker] of the same type.
  3716. Two list markers are of the
  3717. same type if (a) they are bullet list markers using the same character
  3718. (`-`, `+`, or `*`) or (b) they are ordered list numbers with the same
  3719. delimiter (either `.` or `)`).
  3720. A list is an [ordered list](@)
  3721. if its constituent list items begin with
  3722. [ordered list markers], and a
  3723. [bullet list](@) if its constituent list
  3724. items begin with [bullet list markers].
  3725. The [start number](@)
  3726. of an [ordered list] is determined by the list number of
  3727. its initial list item. The numbers of subsequent list items are
  3728. disregarded.
  3729. A list is [loose](@) if any of its constituent
  3730. list items are separated by blank lines, or if any of its constituent
  3731. list items directly contain two block-level elements with a blank line
  3732. between them. Otherwise a list is [tight](@).
  3733. (The difference in HTML output is that paragraphs in a loose list are
  3734. wrapped in `<p>` tags, while paragraphs in a tight list are not.)
  3735. Changing the bullet or ordered list delimiter starts a new list:
  3736. ```````````````````````````````` example
  3737. - foo
  3738. - bar
  3739. + baz
  3740. .
  3741. <ul>
  3742. <li>foo</li>
  3743. <li>bar</li>
  3744. </ul>
  3745. <ul>
  3746. <li>baz</li>
  3747. </ul>
  3748. ````````````````````````````````
  3749. ```````````````````````````````` example
  3750. 1. foo
  3751. 2. bar
  3752. 3) baz
  3753. .
  3754. <ol>
  3755. <li>foo</li>
  3756. <li>bar</li>
  3757. </ol>
  3758. <ol start="3">
  3759. <li>baz</li>
  3760. </ol>
  3761. ````````````````````````````````
  3762. In CommonMark, a list can interrupt a paragraph. That is,
  3763. no blank line is needed to separate a paragraph from a following
  3764. list:
  3765. ```````````````````````````````` example
  3766. Foo
  3767. - bar
  3768. - baz
  3769. .
  3770. <p>Foo</p>
  3771. <ul>
  3772. <li>bar</li>
  3773. <li>baz</li>
  3774. </ul>
  3775. ````````````````````````````````
  3776. `Markdown.pl` does not allow this, through fear of triggering a list
  3777. via a numeral in a hard-wrapped line:
  3778. ``` markdown
  3779. The number of windows in my house is
  3780. 14. The number of doors is 6.
  3781. ```
  3782. Oddly, though, `Markdown.pl` *does* allow a blockquote to
  3783. interrupt a paragraph, even though the same considerations might
  3784. apply.
  3785. In CommonMark, we do allow lists to interrupt paragraphs, for
  3786. two reasons. First, it is natural and not uncommon for people
  3787. to start lists without blank lines:
  3788. ``` markdown
  3789. I need to buy
  3790. - new shoes
  3791. - a coat
  3792. - a plane ticket
  3793. ```
  3794. Second, we are attracted to a
  3795. > [principle of uniformity](@):
  3796. > if a chunk of text has a certain
  3797. > meaning, it will continue to have the same meaning when put into a
  3798. > container block (such as a list item or blockquote).
  3799. (Indeed, the spec for [list items] and [block quotes] presupposes
  3800. this principle.) This principle implies that if
  3801. ``` markdown
  3802. * I need to buy
  3803. - new shoes
  3804. - a coat
  3805. - a plane ticket
  3806. ```
  3807. is a list item containing a paragraph followed by a nested sublist,
  3808. as all Markdown implementations agree it is (though the paragraph
  3809. may be rendered without `<p>` tags, since the list is "tight"),
  3810. then
  3811. ``` markdown
  3812. I need to buy
  3813. - new shoes
  3814. - a coat
  3815. - a plane ticket
  3816. ```
  3817. by itself should be a paragraph followed by a nested sublist.
  3818. Since it is well established Markdown practice to allow lists to
  3819. interrupt paragraphs inside list items, the [principle of
  3820. uniformity] requires us to allow this outside list items as
  3821. well. ([reStructuredText](http://docutils.sourceforge.net/rst.html)
  3822. takes a different approach, requiring blank lines before lists
  3823. even inside other list items.)
  3824. In order to solve of unwanted lists in paragraphs with
  3825. hard-wrapped numerals, we allow only lists starting with `1` to
  3826. interrupt paragraphs. Thus,
  3827. ```````````````````````````````` example
  3828. The number of windows in my house is
  3829. 14. The number of doors is 6.
  3830. .
  3831. <p>The number of windows in my house is
  3832. 14. The number of doors is 6.</p>
  3833. ````````````````````````````````
  3834. We may still get an unintended result in cases like
  3835. ```````````````````````````````` example
  3836. The number of windows in my house is
  3837. 1. The number of doors is 6.
  3838. .
  3839. <p>The number of windows in my house is</p>
  3840. <ol>
  3841. <li>The number of doors is 6.</li>
  3842. </ol>
  3843. ````````````````````````````````
  3844. but this rule should prevent most spurious list captures.
  3845. There can be any number of blank lines between items:
  3846. ```````````````````````````````` example
  3847. - foo
  3848. - bar
  3849. - baz
  3850. .
  3851. <ul>
  3852. <li>
  3853. <p>foo</p>
  3854. </li>
  3855. <li>
  3856. <p>bar</p>
  3857. </li>
  3858. <li>
  3859. <p>baz</p>
  3860. </li>
  3861. </ul>
  3862. ````````````````````````````````
  3863. ```````````````````````````````` example
  3864. - foo
  3865. - bar
  3866. - baz
  3867. bim
  3868. .
  3869. <ul>
  3870. <li>foo
  3871. <ul>
  3872. <li>bar
  3873. <ul>
  3874. <li>
  3875. <p>baz</p>
  3876. <p>bim</p>
  3877. </li>
  3878. </ul>
  3879. </li>
  3880. </ul>
  3881. </li>
  3882. </ul>
  3883. ````````````````````````````````
  3884. To separate consecutive lists of the same type, or to separate a
  3885. list from an indented code block that would otherwise be parsed
  3886. as a subparagraph of the final list item, you can insert a blank HTML
  3887. comment:
  3888. ```````````````````````````````` example
  3889. - foo
  3890. - bar
  3891. <!-- -->
  3892. - baz
  3893. - bim
  3894. .
  3895. <ul>
  3896. <li>foo</li>
  3897. <li>bar</li>
  3898. </ul>
  3899. <!-- -->
  3900. <ul>
  3901. <li>baz</li>
  3902. <li>bim</li>
  3903. </ul>
  3904. ````````````````````````````````
  3905. ```````````````````````````````` example
  3906. - foo
  3907. notcode
  3908. - foo
  3909. <!-- -->
  3910. code
  3911. .
  3912. <ul>
  3913. <li>
  3914. <p>foo</p>
  3915. <p>notcode</p>
  3916. </li>
  3917. <li>
  3918. <p>foo</p>
  3919. </li>
  3920. </ul>
  3921. <!-- -->
  3922. <pre><code>code
  3923. </code></pre>
  3924. ````````````````````````````````
  3925. List items need not be indented to the same level. The following
  3926. list items will be treated as items at the same list level,
  3927. since none is indented enough to belong to the previous list
  3928. item:
  3929. ```````````````````````````````` example
  3930. - a
  3931. - b
  3932. - c
  3933. - d
  3934. - e
  3935. - f
  3936. - g
  3937. .
  3938. <ul>
  3939. <li>a</li>
  3940. <li>b</li>
  3941. <li>c</li>
  3942. <li>d</li>
  3943. <li>e</li>
  3944. <li>f</li>
  3945. <li>g</li>
  3946. </ul>
  3947. ````````````````````````````````
  3948. ```````````````````````````````` example
  3949. 1. a
  3950. 2. b
  3951. 3. c
  3952. .
  3953. <ol>
  3954. <li>
  3955. <p>a</p>
  3956. </li>
  3957. <li>
  3958. <p>b</p>
  3959. </li>
  3960. <li>
  3961. <p>c</p>
  3962. </li>
  3963. </ol>
  3964. ````````````````````````````````
  3965. Note, however, that list items may not be indented more than
  3966. three spaces. Here `- e` is treated as a paragraph continuation
  3967. line, because it is indented more than three spaces:
  3968. ```````````````````````````````` example
  3969. - a
  3970. - b
  3971. - c
  3972. - d
  3973. - e
  3974. .
  3975. <ul>
  3976. <li>a</li>
  3977. <li>b</li>
  3978. <li>c</li>
  3979. <li>d
  3980. - e</li>
  3981. </ul>
  3982. ````````````````````````````````
  3983. And here, `3. c` is treated as in indented code block,
  3984. because it is indented four spaces and preceded by a
  3985. blank line.
  3986. ```````````````````````````````` example
  3987. 1. a
  3988. 2. b
  3989. 3. c
  3990. .
  3991. <ol>
  3992. <li>
  3993. <p>a</p>
  3994. </li>
  3995. <li>
  3996. <p>b</p>
  3997. </li>
  3998. </ol>
  3999. <pre><code>3. c
  4000. </code></pre>
  4001. ````````````````````````````````
  4002. This is a loose list, because there is a blank line between
  4003. two of the list items:
  4004. ```````````````````````````````` example
  4005. - a
  4006. - b
  4007. - c
  4008. .
  4009. <ul>
  4010. <li>
  4011. <p>a</p>
  4012. </li>
  4013. <li>
  4014. <p>b</p>
  4015. </li>
  4016. <li>
  4017. <p>c</p>
  4018. </li>
  4019. </ul>
  4020. ````````````````````````````````
  4021. So is this, with a empty second item:
  4022. ```````````````````````````````` example
  4023. * a
  4024. *
  4025. * c
  4026. .
  4027. <ul>
  4028. <li>
  4029. <p>a</p>
  4030. </li>
  4031. <li></li>
  4032. <li>
  4033. <p>c</p>
  4034. </li>
  4035. </ul>
  4036. ````````````````````````````````
  4037. These are loose lists, even though there is no space between the items,
  4038. because one of the items directly contains two block-level elements
  4039. with a blank line between them:
  4040. ```````````````````````````````` example
  4041. - a
  4042. - b
  4043. c
  4044. - d
  4045. .
  4046. <ul>
  4047. <li>
  4048. <p>a</p>
  4049. </li>
  4050. <li>
  4051. <p>b</p>
  4052. <p>c</p>
  4053. </li>
  4054. <li>
  4055. <p>d</p>
  4056. </li>
  4057. </ul>
  4058. ````````````````````````````````
  4059. ```````````````````````````````` example
  4060. - a
  4061. - b
  4062. [ref]: /url
  4063. - d
  4064. .
  4065. <ul>
  4066. <li>
  4067. <p>a</p>
  4068. </li>
  4069. <li>
  4070. <p>b</p>
  4071. </li>
  4072. <li>
  4073. <p>d</p>
  4074. </li>
  4075. </ul>
  4076. ````````````````````````````````
  4077. This is a tight list, because the blank lines are in a code block:
  4078. ```````````````````````````````` example
  4079. - a
  4080. - ```
  4081. b
  4082. ```
  4083. - c
  4084. .
  4085. <ul>
  4086. <li>a</li>
  4087. <li>
  4088. <pre><code>b
  4089. </code></pre>
  4090. </li>
  4091. <li>c</li>
  4092. </ul>
  4093. ````````````````````````````````
  4094. This is a tight list, because the blank line is between two
  4095. paragraphs of a sublist. So the sublist is loose while
  4096. the outer list is tight:
  4097. ```````````````````````````````` example
  4098. - a
  4099. - b
  4100. c
  4101. - d
  4102. .
  4103. <ul>
  4104. <li>a
  4105. <ul>
  4106. <li>
  4107. <p>b</p>
  4108. <p>c</p>
  4109. </li>
  4110. </ul>
  4111. </li>
  4112. <li>d</li>
  4113. </ul>
  4114. ````````````````````````````````
  4115. This is a tight list, because the blank line is inside the
  4116. block quote:
  4117. ```````````````````````````````` example
  4118. * a
  4119. > b
  4120. >
  4121. * c
  4122. .
  4123. <ul>
  4124. <li>a
  4125. <blockquote>
  4126. <p>b</p>
  4127. </blockquote>
  4128. </li>
  4129. <li>c</li>
  4130. </ul>
  4131. ````````````````````````````````
  4132. This list is tight, because the consecutive block elements
  4133. are not separated by blank lines:
  4134. ```````````````````````````````` example
  4135. - a
  4136. > b
  4137. ```
  4138. c
  4139. ```
  4140. - d
  4141. .
  4142. <ul>
  4143. <li>a
  4144. <blockquote>
  4145. <p>b</p>
  4146. </blockquote>
  4147. <pre><code>c
  4148. </code></pre>
  4149. </li>
  4150. <li>d</li>
  4151. </ul>
  4152. ````````````````````````````````
  4153. A single-paragraph list is tight:
  4154. ```````````````````````````````` example
  4155. - a
  4156. .
  4157. <ul>
  4158. <li>a</li>
  4159. </ul>
  4160. ````````````````````````````````
  4161. ```````````````````````````````` example
  4162. - a
  4163. - b
  4164. .
  4165. <ul>
  4166. <li>a
  4167. <ul>
  4168. <li>b</li>
  4169. </ul>
  4170. </li>
  4171. </ul>
  4172. ````````````````````````````````
  4173. This list is loose, because of the blank line between the
  4174. two block elements in the list item:
  4175. ```````````````````````````````` example
  4176. 1. ```
  4177. foo
  4178. ```
  4179. bar
  4180. .
  4181. <ol>
  4182. <li>
  4183. <pre><code>foo
  4184. </code></pre>
  4185. <p>bar</p>
  4186. </li>
  4187. </ol>
  4188. ````````````````````````````````
  4189. Here the outer list is loose, the inner list tight:
  4190. ```````````````````````````````` example
  4191. * foo
  4192. * bar
  4193. baz
  4194. .
  4195. <ul>
  4196. <li>
  4197. <p>foo</p>
  4198. <ul>
  4199. <li>bar</li>
  4200. </ul>
  4201. <p>baz</p>
  4202. </li>
  4203. </ul>
  4204. ````````````````````````````````
  4205. ```````````````````````````````` example
  4206. - a
  4207. - b
  4208. - c
  4209. - d
  4210. - e
  4211. - f
  4212. .
  4213. <ul>
  4214. <li>
  4215. <p>a</p>
  4216. <ul>
  4217. <li>b</li>
  4218. <li>c</li>
  4219. </ul>
  4220. </li>
  4221. <li>
  4222. <p>d</p>
  4223. <ul>
  4224. <li>e</li>
  4225. <li>f</li>
  4226. </ul>
  4227. </li>
  4228. </ul>
  4229. ````````````````````````````````
  4230. # Inlines
  4231. Inlines are parsed sequentially from the beginning of the character
  4232. stream to the end (left to right, in left-to-right languages).
  4233. Thus, for example, in
  4234. ```````````````````````````````` example
  4235. `hi`lo`
  4236. .
  4237. <p><code>hi</code>lo`</p>
  4238. ````````````````````````````````
  4239. `hi` is parsed as code, leaving the backtick at the end as a literal
  4240. backtick.
  4241. ## Backslash escapes
  4242. Any ASCII punctuation character may be backslash-escaped:
  4243. ```````````````````````````````` example
  4244. \!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~
  4245. .
  4246. <p>!&quot;#$%&amp;'()*+,-./:;&lt;=&gt;?@[\]^_`{|}~</p>
  4247. ````````````````````````````````
  4248. Backslashes before other characters are treated as literal
  4249. backslashes:
  4250. ```````````````````````````````` example
  4251. \→\A\a\ \3\φ\«
  4252. .
  4253. <p>\→\A\a\ \3\φ\«</p>
  4254. ````````````````````````````````
  4255. Escaped characters are treated as regular characters and do
  4256. not have their usual Markdown meanings:
  4257. ```````````````````````````````` example
  4258. \*not emphasized*
  4259. \<br/> not a tag
  4260. \[not a link](/foo)
  4261. \`not code`
  4262. 1\. not a list
  4263. \* not a list
  4264. \# not a heading
  4265. \[foo]: /url "not a reference"
  4266. .
  4267. <p>*not emphasized*
  4268. &lt;br/&gt; not a tag
  4269. [not a link](/foo)
  4270. `not code`
  4271. 1. not a list
  4272. * not a list
  4273. # not a heading
  4274. [foo]: /url &quot;not a reference&quot;</p>
  4275. ````````````````````````````````
  4276. If a backslash is itself escaped, the following character is not:
  4277. ```````````````````````````````` example
  4278. \\*emphasis*
  4279. .
  4280. <p>\<em>emphasis</em></p>
  4281. ````````````````````````````````
  4282. A backslash at the end of the line is a [hard line break]:
  4283. ```````````````````````````````` example
  4284. foo\
  4285. bar
  4286. .
  4287. <p>foo<br />
  4288. bar</p>
  4289. ````````````````````````````````
  4290. Backslash escapes do not work in code blocks, code spans, autolinks, or
  4291. raw HTML:
  4292. ```````````````````````````````` example
  4293. `` \[\` ``
  4294. .
  4295. <p><code>\[\`</code></p>
  4296. ````````````````````````````````
  4297. ```````````````````````````````` example
  4298. \[\]
  4299. .
  4300. <pre><code>\[\]
  4301. </code></pre>
  4302. ````````````````````````````````
  4303. ```````````````````````````````` example
  4304. ~~~
  4305. \[\]
  4306. ~~~
  4307. .
  4308. <pre><code>\[\]
  4309. </code></pre>
  4310. ````````````````````````````````
  4311. ```````````````````````````````` example
  4312. <http://example.com?find=\*>
  4313. .
  4314. <p><a href="http://example.com?find=%5C*">http://example.com?find=\*</a></p>
  4315. ````````````````````````````````
  4316. ```````````````````````````````` example
  4317. <a href="/bar\/)">
  4318. .
  4319. <a href="/bar\/)">
  4320. ````````````````````````````````
  4321. But they work in all other contexts, including URLs and link titles,
  4322. link references, and [info strings] in [fenced code blocks]:
  4323. ```````````````````````````````` example
  4324. [foo](/bar\* "ti\*tle")
  4325. .
  4326. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4327. ````````````````````````````````
  4328. ```````````````````````````````` example
  4329. [foo]
  4330. [foo]: /bar\* "ti\*tle"
  4331. .
  4332. <p><a href="/bar*" title="ti*tle">foo</a></p>
  4333. ````````````````````````````````
  4334. ```````````````````````````````` example
  4335. ``` foo\+bar
  4336. foo
  4337. ```
  4338. .
  4339. <pre><code class="language-foo+bar">foo
  4340. </code></pre>
  4341. ````````````````````````````````
  4342. ## Entity and numeric character references
  4343. All valid HTML entity references and numeric character
  4344. references, except those occurring in code blocks and code spans,
  4345. are recognized as such and treated as equivalent to the
  4346. corresponding Unicode characters. Conforming CommonMark parsers
  4347. need not store information about whether a particular character
  4348. was represented in the source using a Unicode character or
  4349. an entity reference.
  4350. [Entity references](@) consist of `&` + any of the valid
  4351. HTML5 entity names + `;`. The
  4352. document <https://html.spec.whatwg.org/multipage/entities.json>
  4353. is used as an authoritative source for the valid entity
  4354. references and their corresponding code points.
  4355. ```````````````````````````````` example
  4356. &nbsp; &amp; &copy; &AElig; &Dcaron;
  4357. &frac34; &HilbertSpace; &DifferentialD;
  4358. &ClockwiseContourIntegral; &ngE;
  4359. .
  4360. <p>  &amp; © Æ Ď
  4361. ¾ ℋ ⅆ
  4362. ∲ ≧̸</p>
  4363. ````````````````````````````````
  4364. [Decimal numeric character
  4365. references](@)
  4366. consist of `&#` + a string of 1--7 arabic digits + `;`. A
  4367. numeric character reference is parsed as the corresponding
  4368. Unicode character. Invalid Unicode code points will be replaced by
  4369. the REPLACEMENT CHARACTER (`U+FFFD`). For security reasons,
  4370. the code point `U+0000` will also be replaced by `U+FFFD`.
  4371. ```````````````````````````````` example
  4372. &#35; &#1234; &#992; &#0;
  4373. .
  4374. <p># Ӓ Ϡ �</p>
  4375. ````````````````````````````````
  4376. [Hexadecimal numeric character
  4377. references](@) consist of `&#` +
  4378. either `X` or `x` + a string of 1-6 hexadecimal digits + `;`.
  4379. They too are parsed as the corresponding Unicode character (this
  4380. time specified with a hexadecimal numeral instead of decimal).
  4381. ```````````````````````````````` example
  4382. &#X22; &#XD06; &#xcab;
  4383. .
  4384. <p>&quot; ആ ಫ</p>
  4385. ````````````````````````````````
  4386. Here are some nonentities:
  4387. ```````````````````````````````` example
  4388. &nbsp &x; &#; &#x;
  4389. &#987654321;
  4390. &#abcdef0;
  4391. &ThisIsNotDefined; &hi?;
  4392. .
  4393. <p>&amp;nbsp &amp;x; &amp;#; &amp;#x;
  4394. &amp;#987654321;
  4395. &amp;#abcdef0;
  4396. &amp;ThisIsNotDefined; &amp;hi?;</p>
  4397. ````````````````````````````````
  4398. Although HTML5 does accept some entity references
  4399. without a trailing semicolon (such as `&copy`), these are not
  4400. recognized here, because it makes the grammar too ambiguous:
  4401. ```````````````````````````````` example
  4402. &copy
  4403. .
  4404. <p>&amp;copy</p>
  4405. ````````````````````````````````
  4406. Strings that are not on the list of HTML5 named entities are not
  4407. recognized as entity references either:
  4408. ```````````````````````````````` example
  4409. &MadeUpEntity;
  4410. .
  4411. <p>&amp;MadeUpEntity;</p>
  4412. ````````````````````````````````
  4413. Entity and numeric character references are recognized in any
  4414. context besides code spans or code blocks, including
  4415. URLs, [link titles], and [fenced code block][] [info strings]:
  4416. ```````````````````````````````` example
  4417. <a href="&ouml;&ouml;.html">
  4418. .
  4419. <a href="&ouml;&ouml;.html">
  4420. ````````````````````````````````
  4421. ```````````````````````````````` example
  4422. [foo](/f&ouml;&ouml; "f&ouml;&ouml;")
  4423. .
  4424. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4425. ````````````````````````````````
  4426. ```````````````````````````````` example
  4427. [foo]
  4428. [foo]: /f&ouml;&ouml; "f&ouml;&ouml;"
  4429. .
  4430. <p><a href="/f%C3%B6%C3%B6" title="föö">foo</a></p>
  4431. ````````````````````````````````
  4432. ```````````````````````````````` example
  4433. ``` f&ouml;&ouml;
  4434. foo
  4435. ```
  4436. .
  4437. <pre><code class="language-föö">foo
  4438. </code></pre>
  4439. ````````````````````````````````
  4440. Entity and numeric character references are treated as literal
  4441. text in code spans and code blocks:
  4442. ```````````````````````````````` example
  4443. `f&ouml;&ouml;`
  4444. .
  4445. <p><code>f&amp;ouml;&amp;ouml;</code></p>
  4446. ````````````````````````````````
  4447. ```````````````````````````````` example
  4448. f&ouml;f&ouml;
  4449. .
  4450. <pre><code>f&amp;ouml;f&amp;ouml;
  4451. </code></pre>
  4452. ````````````````````````````````
  4453. ## Code spans
  4454. A [backtick string](@)
  4455. is a string of one or more backtick characters (`` ` ``) that is neither
  4456. preceded nor followed by a backtick.
  4457. A [code span](@) begins with a backtick string and ends with
  4458. a backtick string of equal length. The contents of the code span are
  4459. the characters between the two backtick strings, normalized in the
  4460. following ways:
  4461. - First, [line endings] are converted to [spaces].
  4462. - If the resulting string both begins *and* ends with a [space]
  4463. character, but does not consist entirely of [space]
  4464. characters, a single [space] character is removed from the
  4465. front and back. This allows you to include code that begins
  4466. or ends with backtick characters, which must be separated by
  4467. whitespace from the opening or closing backtick strings.
  4468. This is a simple code span:
  4469. ```````````````````````````````` example
  4470. `foo`
  4471. .
  4472. <p><code>foo</code></p>
  4473. ````````````````````````````````
  4474. Here two backticks are used, because the code contains a backtick.
  4475. This example also illustrates stripping of a single leading and
  4476. trailing space:
  4477. ```````````````````````````````` example
  4478. `` foo ` bar ``
  4479. .
  4480. <p><code>foo ` bar</code></p>
  4481. ````````````````````````````````
  4482. This example shows the motivation for stripping leading and trailing
  4483. spaces:
  4484. ```````````````````````````````` example
  4485. ` `` `
  4486. .
  4487. <p><code>``</code></p>
  4488. ````````````````````````````````
  4489. Note that only *one* space is stripped:
  4490. ```````````````````````````````` example
  4491. ` `` `
  4492. .
  4493. <p><code> `` </code></p>
  4494. ````````````````````````````````
  4495. The stripping only happens if the space is on both
  4496. sides of the string:
  4497. ```````````````````````````````` example
  4498. ` a`
  4499. .
  4500. <p><code> a</code></p>
  4501. ````````````````````````````````
  4502. Only [spaces], and not [unicode whitespace] in general, are
  4503. stripped in this way:
  4504. ```````````````````````````````` example
  4505. ` b `
  4506. .
  4507. <p><code> b </code></p>
  4508. ````````````````````````````````
  4509. No stripping occurs if the code span contains only spaces:
  4510. ```````````````````````````````` example
  4511. ` `
  4512. ` `
  4513. .
  4514. <p><code> </code>
  4515. <code> </code></p>
  4516. ````````````````````````````````
  4517. [Line endings] are treated like spaces:
  4518. ```````````````````````````````` example
  4519. ``
  4520. foo
  4521. bar
  4522. baz
  4523. ``
  4524. .
  4525. <p><code>foo bar baz</code></p>
  4526. ````````````````````````````````
  4527. ```````````````````````````````` example
  4528. ``
  4529. foo
  4530. ``
  4531. .
  4532. <p><code>foo </code></p>
  4533. ````````````````````````````````
  4534. Interior spaces are not collapsed:
  4535. ```````````````````````````````` example
  4536. `foo bar
  4537. baz`
  4538. .
  4539. <p><code>foo bar baz</code></p>
  4540. ````````````````````````````````
  4541. Note that browsers will typically collapse consecutive spaces
  4542. when rendering `<code>` elements, so it is recommended that
  4543. the following CSS be used:
  4544. code{white-space: pre-wrap;}
  4545. Note that backslash escapes do not work in code spans. All backslashes
  4546. are treated literally:
  4547. ```````````````````````````````` example
  4548. `foo\`bar`
  4549. .
  4550. <p><code>foo\</code>bar`</p>
  4551. ````````````````````````````````
  4552. Backslash escapes are never needed, because one can always choose a
  4553. string of *n* backtick characters as delimiters, where the code does
  4554. not contain any strings of exactly *n* backtick characters.
  4555. ```````````````````````````````` example
  4556. ``foo`bar``
  4557. .
  4558. <p><code>foo`bar</code></p>
  4559. ````````````````````````````````
  4560. ```````````````````````````````` example
  4561. ` foo `` bar `
  4562. .
  4563. <p><code>foo `` bar</code></p>
  4564. ````````````````````````````````
  4565. Code span backticks have higher precedence than any other inline
  4566. constructs except HTML tags and autolinks. Thus, for example, this is
  4567. not parsed as emphasized text, since the second `*` is part of a code
  4568. span:
  4569. ```````````````````````````````` example
  4570. *foo`*`
  4571. .
  4572. <p>*foo<code>*</code></p>
  4573. ````````````````````````````````
  4574. And this is not parsed as a link:
  4575. ```````````````````````````````` example
  4576. [not a `link](/foo`)
  4577. .
  4578. <p>[not a <code>link](/foo</code>)</p>
  4579. ````````````````````````````````
  4580. Code spans, HTML tags, and autolinks have the same precedence.
  4581. Thus, this is code:
  4582. ```````````````````````````````` example
  4583. `<a href="`">`
  4584. .
  4585. <p><code>&lt;a href=&quot;</code>&quot;&gt;`</p>
  4586. ````````````````````````````````
  4587. But this is an HTML tag:
  4588. ```````````````````````````````` example
  4589. <a href="`">`
  4590. .
  4591. <p><a href="`">`</p>
  4592. ````````````````````````````````
  4593. And this is code:
  4594. ```````````````````````````````` example
  4595. `<http://foo.bar.`baz>`
  4596. .
  4597. <p><code>&lt;http://foo.bar.</code>baz&gt;`</p>
  4598. ````````````````````````````````
  4599. But this is an autolink:
  4600. ```````````````````````````````` example
  4601. <http://foo.bar.`baz>`
  4602. .
  4603. <p><a href="http://foo.bar.%60baz">http://foo.bar.`baz</a>`</p>
  4604. ````````````````````````````````
  4605. When a backtick string is not closed by a matching backtick string,
  4606. we just have literal backticks:
  4607. ```````````````````````````````` example
  4608. ```foo``
  4609. .
  4610. <p>```foo``</p>
  4611. ````````````````````````````````
  4612. ```````````````````````````````` example
  4613. `foo
  4614. .
  4615. <p>`foo</p>
  4616. ````````````````````````````````
  4617. The following case also illustrates the need for opening and
  4618. closing backtick strings to be equal in length:
  4619. ```````````````````````````````` example
  4620. `foo``bar``
  4621. .
  4622. <p>`foo<code>bar</code></p>
  4623. ````````````````````````````````
  4624. ## Emphasis and strong emphasis
  4625. John Gruber's original [Markdown syntax
  4626. description](http://daringfireball.net/projects/markdown/syntax#em) says:
  4627. > Markdown treats asterisks (`*`) and underscores (`_`) as indicators of
  4628. > emphasis. Text wrapped with one `*` or `_` will be wrapped with an HTML
  4629. > `<em>` tag; double `*`'s or `_`'s will be wrapped with an HTML `<strong>`
  4630. > tag.
  4631. This is enough for most users, but these rules leave much undecided,
  4632. especially when it comes to nested emphasis. The original
  4633. `Markdown.pl` test suite makes it clear that triple `***` and
  4634. `___` delimiters can be used for strong emphasis, and most
  4635. implementations have also allowed the following patterns:
  4636. ``` markdown
  4637. ***strong emph***
  4638. ***strong** in emph*
  4639. ***emph* in strong**
  4640. **in strong *emph***
  4641. *in emph **strong***
  4642. ```
  4643. The following patterns are less widely supported, but the intent
  4644. is clear and they are useful (especially in contexts like bibliography
  4645. entries):
  4646. ``` markdown
  4647. *emph *with emph* in it*
  4648. **strong **with strong** in it**
  4649. ```
  4650. Many implementations have also restricted intraword emphasis to
  4651. the `*` forms, to avoid unwanted emphasis in words containing
  4652. internal underscores. (It is best practice to put these in code
  4653. spans, but users often do not.)
  4654. ``` markdown
  4655. internal emphasis: foo*bar*baz
  4656. no emphasis: foo_bar_baz
  4657. ```
  4658. The rules given below capture all of these patterns, while allowing
  4659. for efficient parsing strategies that do not backtrack.
  4660. First, some definitions. A [delimiter run](@) is either
  4661. a sequence of one or more `*` characters that is not preceded or
  4662. followed by a non-backslash-escaped `*` character, or a sequence
  4663. of one or more `_` characters that is not preceded or followed by
  4664. a non-backslash-escaped `_` character.
  4665. A [left-flanking delimiter run](@) is
  4666. a [delimiter run] that is (1) not followed by [Unicode whitespace],
  4667. and either (2a) not followed by a [punctuation character], or
  4668. (2b) followed by a [punctuation character] and
  4669. preceded by [Unicode whitespace] or a [punctuation character].
  4670. For purposes of this definition, the beginning and the end of
  4671. the line count as Unicode whitespace.
  4672. A [right-flanking delimiter run](@) is
  4673. a [delimiter run] that is (1) not preceded by [Unicode whitespace],
  4674. and either (2a) not preceded by a [punctuation character], or
  4675. (2b) preceded by a [punctuation character] and
  4676. followed by [Unicode whitespace] or a [punctuation character].
  4677. For purposes of this definition, the beginning and the end of
  4678. the line count as Unicode whitespace.
  4679. Here are some examples of delimiter runs.
  4680. - left-flanking but not right-flanking:
  4681. ```
  4682. ***abc
  4683. _abc
  4684. **"abc"
  4685. _"abc"
  4686. ```
  4687. - right-flanking but not left-flanking:
  4688. ```
  4689. abc***
  4690. abc_
  4691. "abc"**
  4692. "abc"_
  4693. ```
  4694. - Both left and right-flanking:
  4695. ```
  4696. abc***def
  4697. "abc"_"def"
  4698. ```
  4699. - Neither left nor right-flanking:
  4700. ```
  4701. abc *** def
  4702. a _ b
  4703. ```
  4704. (The idea of distinguishing left-flanking and right-flanking
  4705. delimiter runs based on the character before and the character
  4706. after comes from Roopesh Chander's
  4707. [vfmd](http://www.vfmd.org/vfmd-spec/specification/#procedure-for-identifying-emphasis-tags).
  4708. vfmd uses the terminology "emphasis indicator string" instead of "delimiter
  4709. run," and its rules for distinguishing left- and right-flanking runs
  4710. are a bit more complex than the ones given here.)
  4711. The following rules define emphasis and strong emphasis:
  4712. 1. A single `*` character [can open emphasis](@)
  4713. iff (if and only if) it is part of a [left-flanking delimiter run].
  4714. 2. A single `_` character [can open emphasis] iff
  4715. it is part of a [left-flanking delimiter run]
  4716. and either (a) not part of a [right-flanking delimiter run]
  4717. or (b) part of a [right-flanking delimiter run]
  4718. preceded by punctuation.
  4719. 3. A single `*` character [can close emphasis](@)
  4720. iff it is part of a [right-flanking delimiter run].
  4721. 4. A single `_` character [can close emphasis] iff
  4722. it is part of a [right-flanking delimiter run]
  4723. and either (a) not part of a [left-flanking delimiter run]
  4724. or (b) part of a [left-flanking delimiter run]
  4725. followed by punctuation.
  4726. 5. A double `**` [can open strong emphasis](@)
  4727. iff it is part of a [left-flanking delimiter run].
  4728. 6. A double `__` [can open strong emphasis] iff
  4729. it is part of a [left-flanking delimiter run]
  4730. and either (a) not part of a [right-flanking delimiter run]
  4731. or (b) part of a [right-flanking delimiter run]
  4732. preceded by punctuation.
  4733. 7. A double `**` [can close strong emphasis](@)
  4734. iff it is part of a [right-flanking delimiter run].
  4735. 8. A double `__` [can close strong emphasis] iff
  4736. it is part of a [right-flanking delimiter run]
  4737. and either (a) not part of a [left-flanking delimiter run]
  4738. or (b) part of a [left-flanking delimiter run]
  4739. followed by punctuation.
  4740. 9. Emphasis begins with a delimiter that [can open emphasis] and ends
  4741. with a delimiter that [can close emphasis], and that uses the same
  4742. character (`_` or `*`) as the opening delimiter. The
  4743. opening and closing delimiters must belong to separate
  4744. [delimiter runs]. If one of the delimiters can both
  4745. open and close emphasis, then the sum of the lengths of the
  4746. delimiter runs containing the opening and closing delimiters
  4747. must not be a multiple of 3 unless both lengths are
  4748. multiples of 3.
  4749. 10. Strong emphasis begins with a delimiter that
  4750. [can open strong emphasis] and ends with a delimiter that
  4751. [can close strong emphasis], and that uses the same character
  4752. (`_` or `*`) as the opening delimiter. The
  4753. opening and closing delimiters must belong to separate
  4754. [delimiter runs]. If one of the delimiters can both open
  4755. and close strong emphasis, then the sum of the lengths of
  4756. the delimiter runs containing the opening and closing
  4757. delimiters must not be a multiple of 3 unless both lengths
  4758. are multiples of 3.
  4759. 11. A literal `*` character cannot occur at the beginning or end of
  4760. `*`-delimited emphasis or `**`-delimited strong emphasis, unless it
  4761. is backslash-escaped.
  4762. 12. A literal `_` character cannot occur at the beginning or end of
  4763. `_`-delimited emphasis or `__`-delimited strong emphasis, unless it
  4764. is backslash-escaped.
  4765. Where rules 1--12 above are compatible with multiple parsings,
  4766. the following principles resolve ambiguity:
  4767. 13. The number of nestings should be minimized. Thus, for example,
  4768. an interpretation `<strong>...</strong>` is always preferred to
  4769. `<em><em>...</em></em>`.
  4770. 14. An interpretation `<em><strong>...</strong></em>` is always
  4771. preferred to `<strong><em>...</em></strong>`.
  4772. 15. When two potential emphasis or strong emphasis spans overlap,
  4773. so that the second begins before the first ends and ends after
  4774. the first ends, the first takes precedence. Thus, for example,
  4775. `*foo _bar* baz_` is parsed as `<em>foo _bar</em> baz_` rather
  4776. than `*foo <em>bar* baz</em>`.
  4777. 16. When there are two potential emphasis or strong emphasis spans
  4778. with the same closing delimiter, the shorter one (the one that
  4779. opens later) takes precedence. Thus, for example,
  4780. `**foo **bar baz**` is parsed as `**foo <strong>bar baz</strong>`
  4781. rather than `<strong>foo **bar baz</strong>`.
  4782. 17. Inline code spans, links, images, and HTML tags group more tightly
  4783. than emphasis. So, when there is a choice between an interpretation
  4784. that contains one of these elements and one that does not, the
  4785. former always wins. Thus, for example, `*[foo*](bar)` is
  4786. parsed as `*<a href="bar">foo*</a>` rather than as
  4787. `<em>[foo</em>](bar)`.
  4788. These rules can be illustrated through a series of examples.
  4789. Rule 1:
  4790. ```````````````````````````````` example
  4791. *foo bar*
  4792. .
  4793. <p><em>foo bar</em></p>
  4794. ````````````````````````````````
  4795. This is not emphasis, because the opening `*` is followed by
  4796. whitespace, and hence not part of a [left-flanking delimiter run]:
  4797. ```````````````````````````````` example
  4798. a * foo bar*
  4799. .
  4800. <p>a * foo bar*</p>
  4801. ````````````````````````````````
  4802. This is not emphasis, because the opening `*` is preceded
  4803. by an alphanumeric and followed by punctuation, and hence
  4804. not part of a [left-flanking delimiter run]:
  4805. ```````````````````````````````` example
  4806. a*"foo"*
  4807. .
  4808. <p>a*&quot;foo&quot;*</p>
  4809. ````````````````````````````````
  4810. Unicode nonbreaking spaces count as whitespace, too:
  4811. ```````````````````````````````` example
  4812. * a *
  4813. .
  4814. <p>* a *</p>
  4815. ````````````````````````````````
  4816. Intraword emphasis with `*` is permitted:
  4817. ```````````````````````````````` example
  4818. foo*bar*
  4819. .
  4820. <p>foo<em>bar</em></p>
  4821. ````````````````````````````````
  4822. ```````````````````````````````` example
  4823. 5*6*78
  4824. .
  4825. <p>5<em>6</em>78</p>
  4826. ````````````````````````````````
  4827. Rule 2:
  4828. ```````````````````````````````` example
  4829. _foo bar_
  4830. .
  4831. <p><em>foo bar</em></p>
  4832. ````````````````````````````````
  4833. This is not emphasis, because the opening `_` is followed by
  4834. whitespace:
  4835. ```````````````````````````````` example
  4836. _ foo bar_
  4837. .
  4838. <p>_ foo bar_</p>
  4839. ````````````````````````````````
  4840. This is not emphasis, because the opening `_` is preceded
  4841. by an alphanumeric and followed by punctuation:
  4842. ```````````````````````````````` example
  4843. a_"foo"_
  4844. .
  4845. <p>a_&quot;foo&quot;_</p>
  4846. ````````````````````````````````
  4847. Emphasis with `_` is not allowed inside words:
  4848. ```````````````````````````````` example
  4849. foo_bar_
  4850. .
  4851. <p>foo_bar_</p>
  4852. ````````````````````````````````
  4853. ```````````````````````````````` example
  4854. 5_6_78
  4855. .
  4856. <p>5_6_78</p>
  4857. ````````````````````````````````
  4858. ```````````````````````````````` example
  4859. пристаням_стремятся_
  4860. .
  4861. <p>пристаням_стремятся_</p>
  4862. ````````````````````````````````
  4863. Here `_` does not generate emphasis, because the first delimiter run
  4864. is right-flanking and the second left-flanking:
  4865. ```````````````````````````````` example
  4866. aa_"bb"_cc
  4867. .
  4868. <p>aa_&quot;bb&quot;_cc</p>
  4869. ````````````````````````````````
  4870. This is emphasis, even though the opening delimiter is
  4871. both left- and right-flanking, because it is preceded by
  4872. punctuation:
  4873. ```````````````````````````````` example
  4874. foo-_(bar)_
  4875. .
  4876. <p>foo-<em>(bar)</em></p>
  4877. ````````````````````````````````
  4878. Rule 3:
  4879. This is not emphasis, because the closing delimiter does
  4880. not match the opening delimiter:
  4881. ```````````````````````````````` example
  4882. _foo*
  4883. .
  4884. <p>_foo*</p>
  4885. ````````````````````````````````
  4886. This is not emphasis, because the closing `*` is preceded by
  4887. whitespace:
  4888. ```````````````````````````````` example
  4889. *foo bar *
  4890. .
  4891. <p>*foo bar *</p>
  4892. ````````````````````````````````
  4893. A newline also counts as whitespace:
  4894. ```````````````````````````````` example
  4895. *foo bar
  4896. *
  4897. .
  4898. <p>*foo bar
  4899. *</p>
  4900. ````````````````````````````````
  4901. This is not emphasis, because the second `*` is
  4902. preceded by punctuation and followed by an alphanumeric
  4903. (hence it is not part of a [right-flanking delimiter run]:
  4904. ```````````````````````````````` example
  4905. *(*foo)
  4906. .
  4907. <p>*(*foo)</p>
  4908. ````````````````````````````````
  4909. The point of this restriction is more easily appreciated
  4910. with this example:
  4911. ```````````````````````````````` example
  4912. *(*foo*)*
  4913. .
  4914. <p><em>(<em>foo</em>)</em></p>
  4915. ````````````````````````````````
  4916. Intraword emphasis with `*` is allowed:
  4917. ```````````````````````````````` example
  4918. *foo*bar
  4919. .
  4920. <p><em>foo</em>bar</p>
  4921. ````````````````````````````````
  4922. Rule 4:
  4923. This is not emphasis, because the closing `_` is preceded by
  4924. whitespace:
  4925. ```````````````````````````````` example
  4926. _foo bar _
  4927. .
  4928. <p>_foo bar _</p>
  4929. ````````````````````````````````
  4930. This is not emphasis, because the second `_` is
  4931. preceded by punctuation and followed by an alphanumeric:
  4932. ```````````````````````````````` example
  4933. _(_foo)
  4934. .
  4935. <p>_(_foo)</p>
  4936. ````````````````````````````````
  4937. This is emphasis within emphasis:
  4938. ```````````````````````````````` example
  4939. _(_foo_)_
  4940. .
  4941. <p><em>(<em>foo</em>)</em></p>
  4942. ````````````````````````````````
  4943. Intraword emphasis is disallowed for `_`:
  4944. ```````````````````````````````` example
  4945. _foo_bar
  4946. .
  4947. <p>_foo_bar</p>
  4948. ````````````````````````````````
  4949. ```````````````````````````````` example
  4950. _пристаням_стремятся
  4951. .
  4952. <p>_пристаням_стремятся</p>
  4953. ````````````````````````````````
  4954. ```````````````````````````````` example
  4955. _foo_bar_baz_
  4956. .
  4957. <p><em>foo_bar_baz</em></p>
  4958. ````````````````````````````````
  4959. This is emphasis, even though the closing delimiter is
  4960. both left- and right-flanking, because it is followed by
  4961. punctuation:
  4962. ```````````````````````````````` example
  4963. _(bar)_.
  4964. .
  4965. <p><em>(bar)</em>.</p>
  4966. ````````````````````````````````
  4967. Rule 5:
  4968. ```````````````````````````````` example
  4969. **foo bar**
  4970. .
  4971. <p><strong>foo bar</strong></p>
  4972. ````````````````````````````````
  4973. This is not strong emphasis, because the opening delimiter is
  4974. followed by whitespace:
  4975. ```````````````````````````````` example
  4976. ** foo bar**
  4977. .
  4978. <p>** foo bar**</p>
  4979. ````````````````````````````````
  4980. This is not strong emphasis, because the opening `**` is preceded
  4981. by an alphanumeric and followed by punctuation, and hence
  4982. not part of a [left-flanking delimiter run]:
  4983. ```````````````````````````````` example
  4984. a**"foo"**
  4985. .
  4986. <p>a**&quot;foo&quot;**</p>
  4987. ````````````````````````````````
  4988. Intraword strong emphasis with `**` is permitted:
  4989. ```````````````````````````````` example
  4990. foo**bar**
  4991. .
  4992. <p>foo<strong>bar</strong></p>
  4993. ````````````````````````````````
  4994. Rule 6:
  4995. ```````````````````````````````` example
  4996. __foo bar__
  4997. .
  4998. <p><strong>foo bar</strong></p>
  4999. ````````````````````````````````
  5000. This is not strong emphasis, because the opening delimiter is
  5001. followed by whitespace:
  5002. ```````````````````````````````` example
  5003. __ foo bar__
  5004. .
  5005. <p>__ foo bar__</p>
  5006. ````````````````````````````````
  5007. A newline counts as whitespace:
  5008. ```````````````````````````````` example
  5009. __
  5010. foo bar__
  5011. .
  5012. <p>__
  5013. foo bar__</p>
  5014. ````````````````````````````````
  5015. This is not strong emphasis, because the opening `__` is preceded
  5016. by an alphanumeric and followed by punctuation:
  5017. ```````````````````````````````` example
  5018. a__"foo"__
  5019. .
  5020. <p>a__&quot;foo&quot;__</p>
  5021. ````````````````````````````````
  5022. Intraword strong emphasis is forbidden with `__`:
  5023. ```````````````````````````````` example
  5024. foo__bar__
  5025. .
  5026. <p>foo__bar__</p>
  5027. ````````````````````````````````
  5028. ```````````````````````````````` example
  5029. 5__6__78
  5030. .
  5031. <p>5__6__78</p>
  5032. ````````````````````````````````
  5033. ```````````````````````````````` example
  5034. пристаням__стремятся__
  5035. .
  5036. <p>пристаням__стремятся__</p>
  5037. ````````````````````````````````
  5038. ```````````````````````````````` example
  5039. __foo, __bar__, baz__
  5040. .
  5041. <p><strong>foo, <strong>bar</strong>, baz</strong></p>
  5042. ````````````````````````````````
  5043. This is strong emphasis, even though the opening delimiter is
  5044. both left- and right-flanking, because it is preceded by
  5045. punctuation:
  5046. ```````````````````````````````` example
  5047. foo-__(bar)__
  5048. .
  5049. <p>foo-<strong>(bar)</strong></p>
  5050. ````````````````````````````````
  5051. Rule 7:
  5052. This is not strong emphasis, because the closing delimiter is preceded
  5053. by whitespace:
  5054. ```````````````````````````````` example
  5055. **foo bar **
  5056. .
  5057. <p>**foo bar **</p>
  5058. ````````````````````````````````
  5059. (Nor can it be interpreted as an emphasized `*foo bar *`, because of
  5060. Rule 11.)
  5061. This is not strong emphasis, because the second `**` is
  5062. preceded by punctuation and followed by an alphanumeric:
  5063. ```````````````````````````````` example
  5064. **(**foo)
  5065. .
  5066. <p>**(**foo)</p>
  5067. ````````````````````````````````
  5068. The point of this restriction is more easily appreciated
  5069. with these examples:
  5070. ```````````````````````````````` example
  5071. *(**foo**)*
  5072. .
  5073. <p><em>(<strong>foo</strong>)</em></p>
  5074. ````````````````````````````````
  5075. ```````````````````````````````` example
  5076. **Gomphocarpus (*Gomphocarpus physocarpus*, syn.
  5077. *Asclepias physocarpa*)**
  5078. .
  5079. <p><strong>Gomphocarpus (<em>Gomphocarpus physocarpus</em>, syn.
  5080. <em>Asclepias physocarpa</em>)</strong></p>
  5081. ````````````````````````````````
  5082. ```````````````````````````````` example
  5083. **foo "*bar*" foo**
  5084. .
  5085. <p><strong>foo &quot;<em>bar</em>&quot; foo</strong></p>
  5086. ````````````````````````````````
  5087. Intraword emphasis:
  5088. ```````````````````````````````` example
  5089. **foo**bar
  5090. .
  5091. <p><strong>foo</strong>bar</p>
  5092. ````````````````````````````````
  5093. Rule 8:
  5094. This is not strong emphasis, because the closing delimiter is
  5095. preceded by whitespace:
  5096. ```````````````````````````````` example
  5097. __foo bar __
  5098. .
  5099. <p>__foo bar __</p>
  5100. ````````````````````````````````
  5101. This is not strong emphasis, because the second `__` is
  5102. preceded by punctuation and followed by an alphanumeric:
  5103. ```````````````````````````````` example
  5104. __(__foo)
  5105. .
  5106. <p>__(__foo)</p>
  5107. ````````````````````````````````
  5108. The point of this restriction is more easily appreciated
  5109. with this example:
  5110. ```````````````````````````````` example
  5111. _(__foo__)_
  5112. .
  5113. <p><em>(<strong>foo</strong>)</em></p>
  5114. ````````````````````````````````
  5115. Intraword strong emphasis is forbidden with `__`:
  5116. ```````````````````````````````` example
  5117. __foo__bar
  5118. .
  5119. <p>__foo__bar</p>
  5120. ````````````````````````````````
  5121. ```````````````````````````````` example
  5122. __пристаням__стремятся
  5123. .
  5124. <p>__пристаням__стремятся</p>
  5125. ````````````````````````````````
  5126. ```````````````````````````````` example
  5127. __foo__bar__baz__
  5128. .
  5129. <p><strong>foo__bar__baz</strong></p>
  5130. ````````````````````````````````
  5131. This is strong emphasis, even though the closing delimiter is
  5132. both left- and right-flanking, because it is followed by
  5133. punctuation:
  5134. ```````````````````````````````` example
  5135. __(bar)__.
  5136. .
  5137. <p><strong>(bar)</strong>.</p>
  5138. ````````````````````````````````
  5139. Rule 9:
  5140. Any nonempty sequence of inline elements can be the contents of an
  5141. emphasized span.
  5142. ```````````````````````````````` example
  5143. *foo [bar](/url)*
  5144. .
  5145. <p><em>foo <a href="/url">bar</a></em></p>
  5146. ````````````````````````````````
  5147. ```````````````````````````````` example
  5148. *foo
  5149. bar*
  5150. .
  5151. <p><em>foo
  5152. bar</em></p>
  5153. ````````````````````````````````
  5154. In particular, emphasis and strong emphasis can be nested
  5155. inside emphasis:
  5156. ```````````````````````````````` example
  5157. _foo __bar__ baz_
  5158. .
  5159. <p><em>foo <strong>bar</strong> baz</em></p>
  5160. ````````````````````````````````
  5161. ```````````````````````````````` example
  5162. _foo _bar_ baz_
  5163. .
  5164. <p><em>foo <em>bar</em> baz</em></p>
  5165. ````````````````````````````````
  5166. ```````````````````````````````` example
  5167. __foo_ bar_
  5168. .
  5169. <p><em><em>foo</em> bar</em></p>
  5170. ````````````````````````````````
  5171. ```````````````````````````````` example
  5172. *foo *bar**
  5173. .
  5174. <p><em>foo <em>bar</em></em></p>
  5175. ````````````````````````````````
  5176. ```````````````````````````````` example
  5177. *foo **bar** baz*
  5178. .
  5179. <p><em>foo <strong>bar</strong> baz</em></p>
  5180. ````````````````````````````````
  5181. ```````````````````````````````` example
  5182. *foo**bar**baz*
  5183. .
  5184. <p><em>foo<strong>bar</strong>baz</em></p>
  5185. ````````````````````````````````
  5186. Note that in the preceding case, the interpretation
  5187. ``` markdown
  5188. <p><em>foo</em><em>bar<em></em>baz</em></p>
  5189. ```
  5190. is precluded by the condition that a delimiter that
  5191. can both open and close (like the `*` after `foo`)
  5192. cannot form emphasis if the sum of the lengths of
  5193. the delimiter runs containing the opening and
  5194. closing delimiters is a multiple of 3 unless
  5195. both lengths are multiples of 3.
  5196. For the same reason, we don't get two consecutive
  5197. emphasis sections in this example:
  5198. ```````````````````````````````` example
  5199. *foo**bar*
  5200. .
  5201. <p><em>foo**bar</em></p>
  5202. ````````````````````````````````
  5203. The same condition ensures that the following
  5204. cases are all strong emphasis nested inside
  5205. emphasis, even when the interior spaces are
  5206. omitted:
  5207. ```````````````````````````````` example
  5208. ***foo** bar*
  5209. .
  5210. <p><em><strong>foo</strong> bar</em></p>
  5211. ````````````````````````````````
  5212. ```````````````````````````````` example
  5213. *foo **bar***
  5214. .
  5215. <p><em>foo <strong>bar</strong></em></p>
  5216. ````````````````````````````````
  5217. ```````````````````````````````` example
  5218. *foo**bar***
  5219. .
  5220. <p><em>foo<strong>bar</strong></em></p>
  5221. ````````````````````````````````
  5222. When the lengths of the interior closing and opening
  5223. delimiter runs are *both* multiples of 3, though,
  5224. they can match to create emphasis:
  5225. ```````````````````````````````` example
  5226. foo***bar***baz
  5227. .
  5228. <p>foo<em><strong>bar</strong></em>baz</p>
  5229. ````````````````````````````````
  5230. ```````````````````````````````` example
  5231. foo******bar*********baz
  5232. .
  5233. <p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>
  5234. ````````````````````````````````
  5235. Indefinite levels of nesting are possible:
  5236. ```````````````````````````````` example
  5237. *foo **bar *baz* bim** bop*
  5238. .
  5239. <p><em>foo <strong>bar <em>baz</em> bim</strong> bop</em></p>
  5240. ````````````````````````````````
  5241. ```````````````````````````````` example
  5242. *foo [*bar*](/url)*
  5243. .
  5244. <p><em>foo <a href="/url"><em>bar</em></a></em></p>
  5245. ````````````````````````````````
  5246. There can be no empty emphasis or strong emphasis:
  5247. ```````````````````````````````` example
  5248. ** is not an empty emphasis
  5249. .
  5250. <p>** is not an empty emphasis</p>
  5251. ````````````````````````````````
  5252. ```````````````````````````````` example
  5253. **** is not an empty strong emphasis
  5254. .
  5255. <p>**** is not an empty strong emphasis</p>
  5256. ````````````````````````````````
  5257. Rule 10:
  5258. Any nonempty sequence of inline elements can be the contents of an
  5259. strongly emphasized span.
  5260. ```````````````````````````````` example
  5261. **foo [bar](/url)**
  5262. .
  5263. <p><strong>foo <a href="/url">bar</a></strong></p>
  5264. ````````````````````````````````
  5265. ```````````````````````````````` example
  5266. **foo
  5267. bar**
  5268. .
  5269. <p><strong>foo
  5270. bar</strong></p>
  5271. ````````````````````````````````
  5272. In particular, emphasis and strong emphasis can be nested
  5273. inside strong emphasis:
  5274. ```````````````````````````````` example
  5275. __foo _bar_ baz__
  5276. .
  5277. <p><strong>foo <em>bar</em> baz</strong></p>
  5278. ````````````````````````````````
  5279. ```````````````````````````````` example
  5280. __foo __bar__ baz__
  5281. .
  5282. <p><strong>foo <strong>bar</strong> baz</strong></p>
  5283. ````````````````````````````````
  5284. ```````````````````````````````` example
  5285. ____foo__ bar__
  5286. .
  5287. <p><strong><strong>foo</strong> bar</strong></p>
  5288. ````````````````````````````````
  5289. ```````````````````````````````` example
  5290. **foo **bar****
  5291. .
  5292. <p><strong>foo <strong>bar</strong></strong></p>
  5293. ````````````````````````````````
  5294. ```````````````````````````````` example
  5295. **foo *bar* baz**
  5296. .
  5297. <p><strong>foo <em>bar</em> baz</strong></p>
  5298. ````````````````````````````````
  5299. ```````````````````````````````` example
  5300. **foo*bar*baz**
  5301. .
  5302. <p><strong>foo<em>bar</em>baz</strong></p>
  5303. ````````````````````````````````
  5304. ```````````````````````````````` example
  5305. ***foo* bar**
  5306. .
  5307. <p><strong><em>foo</em> bar</strong></p>
  5308. ````````````````````````````````
  5309. ```````````````````````````````` example
  5310. **foo *bar***
  5311. .
  5312. <p><strong>foo <em>bar</em></strong></p>
  5313. ````````````````````````````````
  5314. Indefinite levels of nesting are possible:
  5315. ```````````````````````````````` example
  5316. **foo *bar **baz**
  5317. bim* bop**
  5318. .
  5319. <p><strong>foo <em>bar <strong>baz</strong>
  5320. bim</em> bop</strong></p>
  5321. ````````````````````````````````
  5322. ```````````````````````````````` example
  5323. **foo [*bar*](/url)**
  5324. .
  5325. <p><strong>foo <a href="/url"><em>bar</em></a></strong></p>
  5326. ````````````````````````````````
  5327. There can be no empty emphasis or strong emphasis:
  5328. ```````````````````````````````` example
  5329. __ is not an empty emphasis
  5330. .
  5331. <p>__ is not an empty emphasis</p>
  5332. ````````````````````````````````
  5333. ```````````````````````````````` example
  5334. ____ is not an empty strong emphasis
  5335. .
  5336. <p>____ is not an empty strong emphasis</p>
  5337. ````````````````````````````````
  5338. Rule 11:
  5339. ```````````````````````````````` example
  5340. foo ***
  5341. .
  5342. <p>foo ***</p>
  5343. ````````````````````````````````
  5344. ```````````````````````````````` example
  5345. foo *\**
  5346. .
  5347. <p>foo <em>*</em></p>
  5348. ````````````````````````````````
  5349. ```````````````````````````````` example
  5350. foo *_*
  5351. .
  5352. <p>foo <em>_</em></p>
  5353. ````````````````````````````````
  5354. ```````````````````````````````` example
  5355. foo *****
  5356. .
  5357. <p>foo *****</p>
  5358. ````````````````````````````````
  5359. ```````````````````````````````` example
  5360. foo **\***
  5361. .
  5362. <p>foo <strong>*</strong></p>
  5363. ````````````````````````````````
  5364. ```````````````````````````````` example
  5365. foo **_**
  5366. .
  5367. <p>foo <strong>_</strong></p>
  5368. ````````````````````````````````
  5369. Note that when delimiters do not match evenly, Rule 11 determines
  5370. that the excess literal `*` characters will appear outside of the
  5371. emphasis, rather than inside it:
  5372. ```````````````````````````````` example
  5373. **foo*
  5374. .
  5375. <p>*<em>foo</em></p>
  5376. ````````````````````````````````
  5377. ```````````````````````````````` example
  5378. *foo**
  5379. .
  5380. <p><em>foo</em>*</p>
  5381. ````````````````````````````````
  5382. ```````````````````````````````` example
  5383. ***foo**
  5384. .
  5385. <p>*<strong>foo</strong></p>
  5386. ````````````````````````````````
  5387. ```````````````````````````````` example
  5388. ****foo*
  5389. .
  5390. <p>***<em>foo</em></p>
  5391. ````````````````````````````````
  5392. ```````````````````````````````` example
  5393. **foo***
  5394. .
  5395. <p><strong>foo</strong>*</p>
  5396. ````````````````````````````````
  5397. ```````````````````````````````` example
  5398. *foo****
  5399. .
  5400. <p><em>foo</em>***</p>
  5401. ````````````````````````````````
  5402. Rule 12:
  5403. ```````````````````````````````` example
  5404. foo ___
  5405. .
  5406. <p>foo ___</p>
  5407. ````````````````````````````````
  5408. ```````````````````````````````` example
  5409. foo _\__
  5410. .
  5411. <p>foo <em>_</em></p>
  5412. ````````````````````````````````
  5413. ```````````````````````````````` example
  5414. foo _*_
  5415. .
  5416. <p>foo <em>*</em></p>
  5417. ````````````````````````````````
  5418. ```````````````````````````````` example
  5419. foo _____
  5420. .
  5421. <p>foo _____</p>
  5422. ````````````````````````````````
  5423. ```````````````````````````````` example
  5424. foo __\___
  5425. .
  5426. <p>foo <strong>_</strong></p>
  5427. ````````````````````````````````
  5428. ```````````````````````````````` example
  5429. foo __*__
  5430. .
  5431. <p>foo <strong>*</strong></p>
  5432. ````````````````````````````````
  5433. ```````````````````````````````` example
  5434. __foo_
  5435. .
  5436. <p>_<em>foo</em></p>
  5437. ````````````````````````````````
  5438. Note that when delimiters do not match evenly, Rule 12 determines
  5439. that the excess literal `_` characters will appear outside of the
  5440. emphasis, rather than inside it:
  5441. ```````````````````````````````` example
  5442. _foo__
  5443. .
  5444. <p><em>foo</em>_</p>
  5445. ````````````````````````````````
  5446. ```````````````````````````````` example
  5447. ___foo__
  5448. .
  5449. <p>_<strong>foo</strong></p>
  5450. ````````````````````````````````
  5451. ```````````````````````````````` example
  5452. ____foo_
  5453. .
  5454. <p>___<em>foo</em></p>
  5455. ````````````````````````````````
  5456. ```````````````````````````````` example
  5457. __foo___
  5458. .
  5459. <p><strong>foo</strong>_</p>
  5460. ````````````````````````````````
  5461. ```````````````````````````````` example
  5462. _foo____
  5463. .
  5464. <p><em>foo</em>___</p>
  5465. ````````````````````````````````
  5466. Rule 13 implies that if you want emphasis nested directly inside
  5467. emphasis, you must use different delimiters:
  5468. ```````````````````````````````` example
  5469. **foo**
  5470. .
  5471. <p><strong>foo</strong></p>
  5472. ````````````````````````````````
  5473. ```````````````````````````````` example
  5474. *_foo_*
  5475. .
  5476. <p><em><em>foo</em></em></p>
  5477. ````````````````````````````````
  5478. ```````````````````````````````` example
  5479. __foo__
  5480. .
  5481. <p><strong>foo</strong></p>
  5482. ````````````````````````````````
  5483. ```````````````````````````````` example
  5484. _*foo*_
  5485. .
  5486. <p><em><em>foo</em></em></p>
  5487. ````````````````````````````````
  5488. However, strong emphasis within strong emphasis is possible without
  5489. switching delimiters:
  5490. ```````````````````````````````` example
  5491. ****foo****
  5492. .
  5493. <p><strong><strong>foo</strong></strong></p>
  5494. ````````````````````````````````
  5495. ```````````````````````````````` example
  5496. ____foo____
  5497. .
  5498. <p><strong><strong>foo</strong></strong></p>
  5499. ````````````````````````````````
  5500. Rule 13 can be applied to arbitrarily long sequences of
  5501. delimiters:
  5502. ```````````````````````````````` example
  5503. ******foo******
  5504. .
  5505. <p><strong><strong><strong>foo</strong></strong></strong></p>
  5506. ````````````````````````````````
  5507. Rule 14:
  5508. ```````````````````````````````` example
  5509. ***foo***
  5510. .
  5511. <p><em><strong>foo</strong></em></p>
  5512. ````````````````````````````````
  5513. ```````````````````````````````` example
  5514. _____foo_____
  5515. .
  5516. <p><em><strong><strong>foo</strong></strong></em></p>
  5517. ````````````````````````````````
  5518. Rule 15:
  5519. ```````````````````````````````` example
  5520. *foo _bar* baz_
  5521. .
  5522. <p><em>foo _bar</em> baz_</p>
  5523. ````````````````````````````````
  5524. ```````````````````````````````` example
  5525. *foo __bar *baz bim__ bam*
  5526. .
  5527. <p><em>foo <strong>bar *baz bim</strong> bam</em></p>
  5528. ````````````````````````````````
  5529. Rule 16:
  5530. ```````````````````````````````` example
  5531. **foo **bar baz**
  5532. .
  5533. <p>**foo <strong>bar baz</strong></p>
  5534. ````````````````````````````````
  5535. ```````````````````````````````` example
  5536. *foo *bar baz*
  5537. .
  5538. <p>*foo <em>bar baz</em></p>
  5539. ````````````````````````````````
  5540. Rule 17:
  5541. ```````````````````````````````` example
  5542. *[bar*](/url)
  5543. .
  5544. <p>*<a href="/url">bar*</a></p>
  5545. ````````````````````````````````
  5546. ```````````````````````````````` example
  5547. _foo [bar_](/url)
  5548. .
  5549. <p>_foo <a href="/url">bar_</a></p>
  5550. ````````````````````````````````
  5551. ```````````````````````````````` example
  5552. *<img src="foo" title="*"/>
  5553. .
  5554. <p>*<img src="foo" title="*"/></p>
  5555. ````````````````````````````````
  5556. ```````````````````````````````` example
  5557. **<a href="**">
  5558. .
  5559. <p>**<a href="**"></p>
  5560. ````````````````````````````````
  5561. ```````````````````````````````` example
  5562. __<a href="__">
  5563. .
  5564. <p>__<a href="__"></p>
  5565. ````````````````````````````````
  5566. ```````````````````````````````` example
  5567. *a `*`*
  5568. .
  5569. <p><em>a <code>*</code></em></p>
  5570. ````````````````````````````````
  5571. ```````````````````````````````` example
  5572. _a `_`_
  5573. .
  5574. <p><em>a <code>_</code></em></p>
  5575. ````````````````````````````````
  5576. ```````````````````````````````` example
  5577. **a<http://foo.bar/?q=**>
  5578. .
  5579. <p>**a<a href="http://foo.bar/?q=**">http://foo.bar/?q=**</a></p>
  5580. ````````````````````````````````
  5581. ```````````````````````````````` example
  5582. __a<http://foo.bar/?q=__>
  5583. .
  5584. <p>__a<a href="http://foo.bar/?q=__">http://foo.bar/?q=__</a></p>
  5585. ````````````````````````````````
  5586. ## Links
  5587. A link contains [link text] (the visible text), a [link destination]
  5588. (the URI that is the link destination), and optionally a [link title].
  5589. There are two basic kinds of links in Markdown. In [inline links] the
  5590. destination and title are given immediately after the link text. In
  5591. [reference links] the destination and title are defined elsewhere in
  5592. the document.
  5593. A [link text](@) consists of a sequence of zero or more
  5594. inline elements enclosed by square brackets (`[` and `]`). The
  5595. following rules apply:
  5596. - Links may not contain other links, at any level of nesting. If
  5597. multiple otherwise valid link definitions appear nested inside each
  5598. other, the inner-most definition is used.
  5599. - Brackets are allowed in the [link text] only if (a) they
  5600. are backslash-escaped or (b) they appear as a matched pair of brackets,
  5601. with an open bracket `[`, a sequence of zero or more inlines, and
  5602. a close bracket `]`.
  5603. - Backtick [code spans], [autolinks], and raw [HTML tags] bind more tightly
  5604. than the brackets in link text. Thus, for example,
  5605. `` [foo`]` `` could not be a link text, since the second `]`
  5606. is part of a code span.
  5607. - The brackets in link text bind more tightly than markers for
  5608. [emphasis and strong emphasis]. Thus, for example, `*[foo*](url)` is a link.
  5609. A [link destination](@) consists of either
  5610. - a sequence of zero or more characters between an opening `<` and a
  5611. closing `>` that contains no line breaks or unescaped
  5612. `<` or `>` characters, or
  5613. - a nonempty sequence of characters that does not start with
  5614. `<`, does not include ASCII space or control characters, and
  5615. includes parentheses only if (a) they are backslash-escaped or
  5616. (b) they are part of a balanced pair of unescaped parentheses.
  5617. (Implementations may impose limits on parentheses nesting to
  5618. avoid performance issues, but at least three levels of nesting
  5619. should be supported.)
  5620. A [link title](@) consists of either
  5621. - a sequence of zero or more characters between straight double-quote
  5622. characters (`"`), including a `"` character only if it is
  5623. backslash-escaped, or
  5624. - a sequence of zero or more characters between straight single-quote
  5625. characters (`'`), including a `'` character only if it is
  5626. backslash-escaped, or
  5627. - a sequence of zero or more characters between matching parentheses
  5628. (`(...)`), including a `(` or `)` character only if it is
  5629. backslash-escaped.
  5630. Although [link titles] may span multiple lines, they may not contain
  5631. a [blank line].
  5632. An [inline link](@) consists of a [link text] followed immediately
  5633. by a left parenthesis `(`, optional [whitespace], an optional
  5634. [link destination], an optional [link title] separated from the link
  5635. destination by [whitespace], optional [whitespace], and a right
  5636. parenthesis `)`. The link's text consists of the inlines contained
  5637. in the [link text] (excluding the enclosing square brackets).
  5638. The link's URI consists of the link destination, excluding enclosing
  5639. `<...>` if present, with backslash-escapes in effect as described
  5640. above. The link's title consists of the link title, excluding its
  5641. enclosing delimiters, with backslash-escapes in effect as described
  5642. above.
  5643. Here is a simple inline link:
  5644. ```````````````````````````````` example
  5645. [link](/uri "title")
  5646. .
  5647. <p><a href="/uri" title="title">link</a></p>
  5648. ````````````````````````````````
  5649. The title may be omitted:
  5650. ```````````````````````````````` example
  5651. [link](/uri)
  5652. .
  5653. <p><a href="/uri">link</a></p>
  5654. ````````````````````````````````
  5655. Both the title and the destination may be omitted:
  5656. ```````````````````````````````` example
  5657. [link]()
  5658. .
  5659. <p><a href="">link</a></p>
  5660. ````````````````````````````````
  5661. ```````````````````````````````` example
  5662. [link](<>)
  5663. .
  5664. <p><a href="">link</a></p>
  5665. ````````````````````````````````
  5666. The destination can only contain spaces if it is
  5667. enclosed in pointy brackets:
  5668. ```````````````````````````````` example
  5669. [link](/my uri)
  5670. .
  5671. <p>[link](/my uri)</p>
  5672. ````````````````````````````````
  5673. ```````````````````````````````` example
  5674. [link](</my uri>)
  5675. .
  5676. <p><a href="/my%20uri">link</a></p>
  5677. ````````````````````````````````
  5678. The destination cannot contain line breaks,
  5679. even if enclosed in pointy brackets:
  5680. ```````````````````````````````` example
  5681. [link](foo
  5682. bar)
  5683. .
  5684. <p>[link](foo
  5685. bar)</p>
  5686. ````````````````````````````````
  5687. ```````````````````````````````` example
  5688. [link](<foo
  5689. bar>)
  5690. .
  5691. <p>[link](<foo
  5692. bar>)</p>
  5693. ````````````````````````````````
  5694. Pointy brackets that enclose links must be unescaped:
  5695. ```````````````````````````````` example
  5696. [link](<foo\>)
  5697. .
  5698. <p>[link](&lt;foo&gt;)</p>
  5699. ````````````````````````````````
  5700. Parentheses inside the link destination may be escaped:
  5701. ```````````````````````````````` example
  5702. [link](\(foo\))
  5703. .
  5704. <p><a href="(foo)">link</a></p>
  5705. ````````````````````````````````
  5706. Any number of parentheses are allowed without escaping, as long as they are
  5707. balanced:
  5708. ```````````````````````````````` example
  5709. [link](foo(and(bar)))
  5710. .
  5711. <p><a href="foo(and(bar))">link</a></p>
  5712. ````````````````````````````````
  5713. However, if you have unbalanced parentheses, you need to escape or use the
  5714. `<...>` form:
  5715. ```````````````````````````````` example
  5716. [link](foo\(and\(bar\))
  5717. .
  5718. <p><a href="foo(and(bar)">link</a></p>
  5719. ````````````````````````````````
  5720. ```````````````````````````````` example
  5721. [link](<foo(and(bar)>)
  5722. .
  5723. <p><a href="foo(and(bar)">link</a></p>
  5724. ````````````````````````````````
  5725. Parentheses and other symbols can also be escaped, as usual
  5726. in Markdown:
  5727. ```````````````````````````````` example
  5728. [link](foo\)\:)
  5729. .
  5730. <p><a href="foo):">link</a></p>
  5731. ````````````````````````````````
  5732. A link can contain fragment identifiers and queries:
  5733. ```````````````````````````````` example
  5734. [link](#fragment)
  5735. [link](http://example.com#fragment)
  5736. [link](http://example.com?foo=3#frag)
  5737. .
  5738. <p><a href="#fragment">link</a></p>
  5739. <p><a href="http://example.com#fragment">link</a></p>
  5740. <p><a href="http://example.com?foo=3#frag">link</a></p>
  5741. ````````````````````````````````
  5742. Note that a backslash before a non-escapable character is
  5743. just a backslash:
  5744. ```````````````````````````````` example
  5745. [link](foo\bar)
  5746. .
  5747. <p><a href="foo%5Cbar">link</a></p>
  5748. ````````````````````````````````
  5749. URL-escaping should be left alone inside the destination, as all
  5750. URL-escaped characters are also valid URL characters. Entity and
  5751. numerical character references in the destination will be parsed
  5752. into the corresponding Unicode code points, as usual. These may
  5753. be optionally URL-escaped when written as HTML, but this spec
  5754. does not enforce any particular policy for rendering URLs in
  5755. HTML or other formats. Renderers may make different decisions
  5756. about how to escape or normalize URLs in the output.
  5757. ```````````````````````````````` example
  5758. [link](foo%20b&auml;)
  5759. .
  5760. <p><a href="foo%20b%C3%A4">link</a></p>
  5761. ````````````````````````````````
  5762. Note that, because titles can often be parsed as destinations,
  5763. if you try to omit the destination and keep the title, you'll
  5764. get unexpected results:
  5765. ```````````````````````````````` example
  5766. [link]("title")
  5767. .
  5768. <p><a href="%22title%22">link</a></p>
  5769. ````````````````````````````````
  5770. Titles may be in single quotes, double quotes, or parentheses:
  5771. ```````````````````````````````` example
  5772. [link](/url "title")
  5773. [link](/url 'title')
  5774. [link](/url (title))
  5775. .
  5776. <p><a href="/url" title="title">link</a>
  5777. <a href="/url" title="title">link</a>
  5778. <a href="/url" title="title">link</a></p>
  5779. ````````````````````````````````
  5780. Backslash escapes and entity and numeric character references
  5781. may be used in titles:
  5782. ```````````````````````````````` example
  5783. [link](/url "title \"&quot;")
  5784. .
  5785. <p><a href="/url" title="title &quot;&quot;">link</a></p>
  5786. ````````````````````````````````
  5787. Titles must be separated from the link using a [whitespace].
  5788. Other [Unicode whitespace] like non-breaking space doesn't work.
  5789. ```````````````````````````````` example
  5790. [link](/url "title")
  5791. .
  5792. <p><a href="/url%C2%A0%22title%22">link</a></p>
  5793. ````````````````````````````````
  5794. Nested balanced quotes are not allowed without escaping:
  5795. ```````````````````````````````` example
  5796. [link](/url "title "and" title")
  5797. .
  5798. <p>[link](/url &quot;title &quot;and&quot; title&quot;)</p>
  5799. ````````````````````````````````
  5800. But it is easy to work around this by using a different quote type:
  5801. ```````````````````````````````` example
  5802. [link](/url 'title "and" title')
  5803. .
  5804. <p><a href="/url" title="title &quot;and&quot; title">link</a></p>
  5805. ````````````````````````````````
  5806. (Note: `Markdown.pl` did allow double quotes inside a double-quoted
  5807. title, and its test suite included a test demonstrating this.
  5808. But it is hard to see a good rationale for the extra complexity this
  5809. brings, since there are already many ways---backslash escaping,
  5810. entity and numeric character references, or using a different
  5811. quote type for the enclosing title---to write titles containing
  5812. double quotes. `Markdown.pl`'s handling of titles has a number
  5813. of other strange features. For example, it allows single-quoted
  5814. titles in inline links, but not reference links. And, in
  5815. reference links but not inline links, it allows a title to begin
  5816. with `"` and end with `)`. `Markdown.pl` 1.0.1 even allows
  5817. titles with no closing quotation mark, though 1.0.2b8 does not.
  5818. It seems preferable to adopt a simple, rational rule that works
  5819. the same way in inline links and link reference definitions.)
  5820. [Whitespace] is allowed around the destination and title:
  5821. ```````````````````````````````` example
  5822. [link]( /uri
  5823. "title" )
  5824. .
  5825. <p><a href="/uri" title="title">link</a></p>
  5826. ````````````````````````````````
  5827. But it is not allowed between the link text and the
  5828. following parenthesis:
  5829. ```````````````````````````````` example
  5830. [link] (/uri)
  5831. .
  5832. <p>[link] (/uri)</p>
  5833. ````````````````````````````````
  5834. The link text may contain balanced brackets, but not unbalanced ones,
  5835. unless they are escaped:
  5836. ```````````````````````````````` example
  5837. [link [foo [bar]]](/uri)
  5838. .
  5839. <p><a href="/uri">link [foo [bar]]</a></p>
  5840. ````````````````````````````````
  5841. ```````````````````````````````` example
  5842. [link] bar](/uri)
  5843. .
  5844. <p>[link] bar](/uri)</p>
  5845. ````````````````````````````````
  5846. ```````````````````````````````` example
  5847. [link [bar](/uri)
  5848. .
  5849. <p>[link <a href="/uri">bar</a></p>
  5850. ````````````````````````````````
  5851. ```````````````````````````````` example
  5852. [link \[bar](/uri)
  5853. .
  5854. <p><a href="/uri">link [bar</a></p>
  5855. ````````````````````````````````
  5856. The link text may contain inline content:
  5857. ```````````````````````````````` example
  5858. [link *foo **bar** `#`*](/uri)
  5859. .
  5860. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5861. ````````````````````````````````
  5862. ```````````````````````````````` example
  5863. [![moon](moon.jpg)](/uri)
  5864. .
  5865. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5866. ````````````````````````````````
  5867. However, links may not contain other links, at any level of nesting.
  5868. ```````````````````````````````` example
  5869. [foo [bar](/uri)](/uri)
  5870. .
  5871. <p>[foo <a href="/uri">bar</a>](/uri)</p>
  5872. ````````````````````````````````
  5873. ```````````````````````````````` example
  5874. [foo *[bar [baz](/uri)](/uri)*](/uri)
  5875. .
  5876. <p>[foo <em>[bar <a href="/uri">baz</a>](/uri)</em>](/uri)</p>
  5877. ````````````````````````````````
  5878. ```````````````````````````````` example
  5879. ![[[foo](uri1)](uri2)](uri3)
  5880. .
  5881. <p><img src="uri3" alt="[foo](uri2)" /></p>
  5882. ````````````````````````````````
  5883. These cases illustrate the precedence of link text grouping over
  5884. emphasis grouping:
  5885. ```````````````````````````````` example
  5886. *[foo*](/uri)
  5887. .
  5888. <p>*<a href="/uri">foo*</a></p>
  5889. ````````````````````````````````
  5890. ```````````````````````````````` example
  5891. [foo *bar](baz*)
  5892. .
  5893. <p><a href="baz*">foo *bar</a></p>
  5894. ````````````````````````````````
  5895. Note that brackets that *aren't* part of links do not take
  5896. precedence:
  5897. ```````````````````````````````` example
  5898. *foo [bar* baz]
  5899. .
  5900. <p><em>foo [bar</em> baz]</p>
  5901. ````````````````````````````````
  5902. These cases illustrate the precedence of HTML tags, code spans,
  5903. and autolinks over link grouping:
  5904. ```````````````````````````````` example
  5905. [foo <bar attr="](baz)">
  5906. .
  5907. <p>[foo <bar attr="](baz)"></p>
  5908. ````````````````````````````````
  5909. ```````````````````````````````` example
  5910. [foo`](/uri)`
  5911. .
  5912. <p>[foo<code>](/uri)</code></p>
  5913. ````````````````````````````````
  5914. ```````````````````````````````` example
  5915. [foo<http://example.com/?search=](uri)>
  5916. .
  5917. <p>[foo<a href="http://example.com/?search=%5D(uri)">http://example.com/?search=](uri)</a></p>
  5918. ````````````````````````````````
  5919. There are three kinds of [reference link](@)s:
  5920. [full](#full-reference-link), [collapsed](#collapsed-reference-link),
  5921. and [shortcut](#shortcut-reference-link).
  5922. A [full reference link](@)
  5923. consists of a [link text] immediately followed by a [link label]
  5924. that [matches] a [link reference definition] elsewhere in the document.
  5925. A [link label](@) begins with a left bracket (`[`) and ends
  5926. with the first right bracket (`]`) that is not backslash-escaped.
  5927. Between these brackets there must be at least one [non-whitespace character].
  5928. Unescaped square bracket characters are not allowed inside the
  5929. opening and closing square brackets of [link labels]. A link
  5930. label can have at most 999 characters inside the square
  5931. brackets.
  5932. One label [matches](@)
  5933. another just in case their normalized forms are equal. To normalize a
  5934. label, strip off the opening and closing brackets,
  5935. perform the *Unicode case fold*, strip leading and trailing
  5936. [whitespace] and collapse consecutive internal
  5937. [whitespace] to a single space. If there are multiple
  5938. matching reference link definitions, the one that comes first in the
  5939. document is used. (It is desirable in such cases to emit a warning.)
  5940. The contents of the first link label are parsed as inlines, which are
  5941. used as the link's text. The link's URI and title are provided by the
  5942. matching [link reference definition].
  5943. Here is a simple example:
  5944. ```````````````````````````````` example
  5945. [foo][bar]
  5946. [bar]: /url "title"
  5947. .
  5948. <p><a href="/url" title="title">foo</a></p>
  5949. ````````````````````````````````
  5950. The rules for the [link text] are the same as with
  5951. [inline links]. Thus:
  5952. The link text may contain balanced brackets, but not unbalanced ones,
  5953. unless they are escaped:
  5954. ```````````````````````````````` example
  5955. [link [foo [bar]]][ref]
  5956. [ref]: /uri
  5957. .
  5958. <p><a href="/uri">link [foo [bar]]</a></p>
  5959. ````````````````````````````````
  5960. ```````````````````````````````` example
  5961. [link \[bar][ref]
  5962. [ref]: /uri
  5963. .
  5964. <p><a href="/uri">link [bar</a></p>
  5965. ````````````````````````````````
  5966. The link text may contain inline content:
  5967. ```````````````````````````````` example
  5968. [link *foo **bar** `#`*][ref]
  5969. [ref]: /uri
  5970. .
  5971. <p><a href="/uri">link <em>foo <strong>bar</strong> <code>#</code></em></a></p>
  5972. ````````````````````````````````
  5973. ```````````````````````````````` example
  5974. [![moon](moon.jpg)][ref]
  5975. [ref]: /uri
  5976. .
  5977. <p><a href="/uri"><img src="moon.jpg" alt="moon" /></a></p>
  5978. ````````````````````````````````
  5979. However, links may not contain other links, at any level of nesting.
  5980. ```````````````````````````````` example
  5981. [foo [bar](/uri)][ref]
  5982. [ref]: /uri
  5983. .
  5984. <p>[foo <a href="/uri">bar</a>]<a href="/uri">ref</a></p>
  5985. ````````````````````````````````
  5986. ```````````````````````````````` example
  5987. [foo *bar [baz][ref]*][ref]
  5988. [ref]: /uri
  5989. .
  5990. <p>[foo <em>bar <a href="/uri">baz</a></em>]<a href="/uri">ref</a></p>
  5991. ````````````````````````````````
  5992. (In the examples above, we have two [shortcut reference links]
  5993. instead of one [full reference link].)
  5994. The following cases illustrate the precedence of link text grouping over
  5995. emphasis grouping:
  5996. ```````````````````````````````` example
  5997. *[foo*][ref]
  5998. [ref]: /uri
  5999. .
  6000. <p>*<a href="/uri">foo*</a></p>
  6001. ````````````````````````````````
  6002. ```````````````````````````````` example
  6003. [foo *bar][ref]
  6004. [ref]: /uri
  6005. .
  6006. <p><a href="/uri">foo *bar</a></p>
  6007. ````````````````````````````````
  6008. These cases illustrate the precedence of HTML tags, code spans,
  6009. and autolinks over link grouping:
  6010. ```````````````````````````````` example
  6011. [foo <bar attr="][ref]">
  6012. [ref]: /uri
  6013. .
  6014. <p>[foo <bar attr="][ref]"></p>
  6015. ````````````````````````````````
  6016. ```````````````````````````````` example
  6017. [foo`][ref]`
  6018. [ref]: /uri
  6019. .
  6020. <p>[foo<code>][ref]</code></p>
  6021. ````````````````````````````````
  6022. ```````````````````````````````` example
  6023. [foo<http://example.com/?search=][ref]>
  6024. [ref]: /uri
  6025. .
  6026. <p>[foo<a href="http://example.com/?search=%5D%5Bref%5D">http://example.com/?search=][ref]</a></p>
  6027. ````````````````````````````````
  6028. Matching is case-insensitive:
  6029. ```````````````````````````````` example
  6030. [foo][BaR]
  6031. [bar]: /url "title"
  6032. .
  6033. <p><a href="/url" title="title">foo</a></p>
  6034. ````````````````````````````````
  6035. Unicode case fold is used:
  6036. ```````````````````````````````` example
  6037. [Толпой][Толпой] is a Russian word.
  6038. [ТОЛПОЙ]: /url
  6039. .
  6040. <p><a href="/url">Толпой</a> is a Russian word.</p>
  6041. ````````````````````````````````
  6042. Consecutive internal [whitespace] is treated as one space for
  6043. purposes of determining matching:
  6044. ```````````````````````````````` example
  6045. [Foo
  6046. bar]: /url
  6047. [Baz][Foo bar]
  6048. .
  6049. <p><a href="/url">Baz</a></p>
  6050. ````````````````````````````````
  6051. No [whitespace] is allowed between the [link text] and the
  6052. [link label]:
  6053. ```````````````````````````````` example
  6054. [foo] [bar]
  6055. [bar]: /url "title"
  6056. .
  6057. <p>[foo] <a href="/url" title="title">bar</a></p>
  6058. ````````````````````````````````
  6059. ```````````````````````````````` example
  6060. [foo]
  6061. [bar]
  6062. [bar]: /url "title"
  6063. .
  6064. <p>[foo]
  6065. <a href="/url" title="title">bar</a></p>
  6066. ````````````````````````````````
  6067. This is a departure from John Gruber's original Markdown syntax
  6068. description, which explicitly allows whitespace between the link
  6069. text and the link label. It brings reference links in line with
  6070. [inline links], which (according to both original Markdown and
  6071. this spec) cannot have whitespace after the link text. More
  6072. importantly, it prevents inadvertent capture of consecutive
  6073. [shortcut reference links]. If whitespace is allowed between the
  6074. link text and the link label, then in the following we will have
  6075. a single reference link, not two shortcut reference links, as
  6076. intended:
  6077. ``` markdown
  6078. [foo]
  6079. [bar]
  6080. [foo]: /url1
  6081. [bar]: /url2
  6082. ```
  6083. (Note that [shortcut reference links] were introduced by Gruber
  6084. himself in a beta version of `Markdown.pl`, but never included
  6085. in the official syntax description. Without shortcut reference
  6086. links, it is harmless to allow space between the link text and
  6087. link label; but once shortcut references are introduced, it is
  6088. too dangerous to allow this, as it frequently leads to
  6089. unintended results.)
  6090. When there are multiple matching [link reference definitions],
  6091. the first is used:
  6092. ```````````````````````````````` example
  6093. [foo]: /url1
  6094. [foo]: /url2
  6095. [bar][foo]
  6096. .
  6097. <p><a href="/url1">bar</a></p>
  6098. ````````````````````````````````
  6099. Note that matching is performed on normalized strings, not parsed
  6100. inline content. So the following does not match, even though the
  6101. labels define equivalent inline content:
  6102. ```````````````````````````````` example
  6103. [bar][foo\!]
  6104. [foo!]: /url
  6105. .
  6106. <p>[bar][foo!]</p>
  6107. ````````````````````````````````
  6108. [Link labels] cannot contain brackets, unless they are
  6109. backslash-escaped:
  6110. ```````````````````````````````` example
  6111. [foo][ref[]
  6112. [ref[]: /uri
  6113. .
  6114. <p>[foo][ref[]</p>
  6115. <p>[ref[]: /uri</p>
  6116. ````````````````````````````````
  6117. ```````````````````````````````` example
  6118. [foo][ref[bar]]
  6119. [ref[bar]]: /uri
  6120. .
  6121. <p>[foo][ref[bar]]</p>
  6122. <p>[ref[bar]]: /uri</p>
  6123. ````````````````````````````````
  6124. ```````````````````````````````` example
  6125. [[[foo]]]
  6126. [[[foo]]]: /url
  6127. .
  6128. <p>[[[foo]]]</p>
  6129. <p>[[[foo]]]: /url</p>
  6130. ````````````````````````````````
  6131. ```````````````````````````````` example
  6132. [foo][ref\[]
  6133. [ref\[]: /uri
  6134. .
  6135. <p><a href="/uri">foo</a></p>
  6136. ````````````````````````````````
  6137. Note that in this example `]` is not backslash-escaped:
  6138. ```````````````````````````````` example
  6139. [bar\\]: /uri
  6140. [bar\\]
  6141. .
  6142. <p><a href="/uri">bar\</a></p>
  6143. ````````````````````````````````
  6144. A [link label] must contain at least one [non-whitespace character]:
  6145. ```````````````````````````````` example
  6146. []
  6147. []: /uri
  6148. .
  6149. <p>[]</p>
  6150. <p>[]: /uri</p>
  6151. ````````````````````````````````
  6152. ```````````````````````````````` example
  6153. [
  6154. ]
  6155. [
  6156. ]: /uri
  6157. .
  6158. <p>[
  6159. ]</p>
  6160. <p>[
  6161. ]: /uri</p>
  6162. ````````````````````````````````
  6163. A [collapsed reference link](@)
  6164. consists of a [link label] that [matches] a
  6165. [link reference definition] elsewhere in the
  6166. document, followed by the string `[]`.
  6167. The contents of the first link label are parsed as inlines,
  6168. which are used as the link's text. The link's URI and title are
  6169. provided by the matching reference link definition. Thus,
  6170. `[foo][]` is equivalent to `[foo][foo]`.
  6171. ```````````````````````````````` example
  6172. [foo][]
  6173. [foo]: /url "title"
  6174. .
  6175. <p><a href="/url" title="title">foo</a></p>
  6176. ````````````````````````````````
  6177. ```````````````````````````````` example
  6178. [*foo* bar][]
  6179. [*foo* bar]: /url "title"
  6180. .
  6181. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6182. ````````````````````````````````
  6183. The link labels are case-insensitive:
  6184. ```````````````````````````````` example
  6185. [Foo][]
  6186. [foo]: /url "title"
  6187. .
  6188. <p><a href="/url" title="title">Foo</a></p>
  6189. ````````````````````````````````
  6190. As with full reference links, [whitespace] is not
  6191. allowed between the two sets of brackets:
  6192. ```````````````````````````````` example
  6193. [foo]
  6194. []
  6195. [foo]: /url "title"
  6196. .
  6197. <p><a href="/url" title="title">foo</a>
  6198. []</p>
  6199. ````````````````````````````````
  6200. A [shortcut reference link](@)
  6201. consists of a [link label] that [matches] a
  6202. [link reference definition] elsewhere in the
  6203. document and is not followed by `[]` or a link label.
  6204. The contents of the first link label are parsed as inlines,
  6205. which are used as the link's text. The link's URI and title
  6206. are provided by the matching link reference definition.
  6207. Thus, `[foo]` is equivalent to `[foo][]`.
  6208. ```````````````````````````````` example
  6209. [foo]
  6210. [foo]: /url "title"
  6211. .
  6212. <p><a href="/url" title="title">foo</a></p>
  6213. ````````````````````````````````
  6214. ```````````````````````````````` example
  6215. [*foo* bar]
  6216. [*foo* bar]: /url "title"
  6217. .
  6218. <p><a href="/url" title="title"><em>foo</em> bar</a></p>
  6219. ````````````````````````````````
  6220. ```````````````````````````````` example
  6221. [[*foo* bar]]
  6222. [*foo* bar]: /url "title"
  6223. .
  6224. <p>[<a href="/url" title="title"><em>foo</em> bar</a>]</p>
  6225. ````````````````````````````````
  6226. ```````````````````````````````` example
  6227. [[bar [foo]
  6228. [foo]: /url
  6229. .
  6230. <p>[[bar <a href="/url">foo</a></p>
  6231. ````````````````````````````````
  6232. The link labels are case-insensitive:
  6233. ```````````````````````````````` example
  6234. [Foo]
  6235. [foo]: /url "title"
  6236. .
  6237. <p><a href="/url" title="title">Foo</a></p>
  6238. ````````````````````````````````
  6239. A space after the link text should be preserved:
  6240. ```````````````````````````````` example
  6241. [foo] bar
  6242. [foo]: /url
  6243. .
  6244. <p><a href="/url">foo</a> bar</p>
  6245. ````````````````````````````````
  6246. If you just want bracketed text, you can backslash-escape the
  6247. opening bracket to avoid links:
  6248. ```````````````````````````````` example
  6249. \[foo]
  6250. [foo]: /url "title"
  6251. .
  6252. <p>[foo]</p>
  6253. ````````````````````````````````
  6254. Note that this is a link, because a link label ends with the first
  6255. following closing bracket:
  6256. ```````````````````````````````` example
  6257. [foo*]: /url
  6258. *[foo*]
  6259. .
  6260. <p>*<a href="/url">foo*</a></p>
  6261. ````````````````````````````````
  6262. Full and compact references take precedence over shortcut
  6263. references:
  6264. ```````````````````````````````` example
  6265. [foo][bar]
  6266. [foo]: /url1
  6267. [bar]: /url2
  6268. .
  6269. <p><a href="/url2">foo</a></p>
  6270. ````````````````````````````````
  6271. ```````````````````````````````` example
  6272. [foo][]
  6273. [foo]: /url1
  6274. .
  6275. <p><a href="/url1">foo</a></p>
  6276. ````````````````````````````````
  6277. Inline links also take precedence:
  6278. ```````````````````````````````` example
  6279. [foo]()
  6280. [foo]: /url1
  6281. .
  6282. <p><a href="">foo</a></p>
  6283. ````````````````````````````````
  6284. ```````````````````````````````` example
  6285. [foo](not a link)
  6286. [foo]: /url1
  6287. .
  6288. <p><a href="/url1">foo</a>(not a link)</p>
  6289. ````````````````````````````````
  6290. In the following case `[bar][baz]` is parsed as a reference,
  6291. `[foo]` as normal text:
  6292. ```````````````````````````````` example
  6293. [foo][bar][baz]
  6294. [baz]: /url
  6295. .
  6296. <p>[foo]<a href="/url">bar</a></p>
  6297. ````````````````````````````````
  6298. Here, though, `[foo][bar]` is parsed as a reference, since
  6299. `[bar]` is defined:
  6300. ```````````````````````````````` example
  6301. [foo][bar][baz]
  6302. [baz]: /url1
  6303. [bar]: /url2
  6304. .
  6305. <p><a href="/url2">foo</a><a href="/url1">baz</a></p>
  6306. ````````````````````````````````
  6307. Here `[foo]` is not parsed as a shortcut reference, because it
  6308. is followed by a link label (even though `[bar]` is not defined):
  6309. ```````````````````````````````` example
  6310. [foo][bar][baz]
  6311. [baz]: /url1
  6312. [foo]: /url2
  6313. .
  6314. <p>[foo]<a href="/url1">bar</a></p>
  6315. ````````````````````````````````
  6316. ## Images
  6317. Syntax for images is like the syntax for links, with one
  6318. difference. Instead of [link text], we have an
  6319. [image description](@). The rules for this are the
  6320. same as for [link text], except that (a) an
  6321. image description starts with `![` rather than `[`, and
  6322. (b) an image description may contain links.
  6323. An image description has inline elements
  6324. as its contents. When an image is rendered to HTML,
  6325. this is standardly used as the image's `alt` attribute.
  6326. ```````````````````````````````` example
  6327. ![foo](/url "title")
  6328. .
  6329. <p><img src="/url" alt="foo" title="title" /></p>
  6330. ````````````````````````````````
  6331. ```````````````````````````````` example
  6332. ![foo *bar*]
  6333. [foo *bar*]: train.jpg "train & tracks"
  6334. .
  6335. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6336. ````````````````````````````````
  6337. ```````````````````````````````` example
  6338. ![foo ![bar](/url)](/url2)
  6339. .
  6340. <p><img src="/url2" alt="foo bar" /></p>
  6341. ````````````````````````````````
  6342. ```````````````````````````````` example
  6343. ![foo [bar](/url)](/url2)
  6344. .
  6345. <p><img src="/url2" alt="foo bar" /></p>
  6346. ````````````````````````````````
  6347. Though this spec is concerned with parsing, not rendering, it is
  6348. recommended that in rendering to HTML, only the plain string content
  6349. of the [image description] be used. Note that in
  6350. the above example, the alt attribute's value is `foo bar`, not `foo
  6351. [bar](/url)` or `foo <a href="/url">bar</a>`. Only the plain string
  6352. content is rendered, without formatting.
  6353. ```````````````````````````````` example
  6354. ![foo *bar*][]
  6355. [foo *bar*]: train.jpg "train & tracks"
  6356. .
  6357. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6358. ````````````````````````````````
  6359. ```````````````````````````````` example
  6360. ![foo *bar*][foobar]
  6361. [FOOBAR]: train.jpg "train & tracks"
  6362. .
  6363. <p><img src="train.jpg" alt="foo bar" title="train &amp; tracks" /></p>
  6364. ````````````````````````````````
  6365. ```````````````````````````````` example
  6366. ![foo](train.jpg)
  6367. .
  6368. <p><img src="train.jpg" alt="foo" /></p>
  6369. ````````````````````````````````
  6370. ```````````````````````````````` example
  6371. My ![foo bar](/path/to/train.jpg "title" )
  6372. .
  6373. <p>My <img src="/path/to/train.jpg" alt="foo bar" title="title" /></p>
  6374. ````````````````````````````````
  6375. ```````````````````````````````` example
  6376. ![foo](<url>)
  6377. .
  6378. <p><img src="url" alt="foo" /></p>
  6379. ````````````````````````````````
  6380. ```````````````````````````````` example
  6381. ![](/url)
  6382. .
  6383. <p><img src="/url" alt="" /></p>
  6384. ````````````````````````````````
  6385. Reference-style:
  6386. ```````````````````````````````` example
  6387. ![foo][bar]
  6388. [bar]: /url
  6389. .
  6390. <p><img src="/url" alt="foo" /></p>
  6391. ````````````````````````````````
  6392. ```````````````````````````````` example
  6393. ![foo][bar]
  6394. [BAR]: /url
  6395. .
  6396. <p><img src="/url" alt="foo" /></p>
  6397. ````````````````````````````````
  6398. Collapsed:
  6399. ```````````````````````````````` example
  6400. ![foo][]
  6401. [foo]: /url "title"
  6402. .
  6403. <p><img src="/url" alt="foo" title="title" /></p>
  6404. ````````````````````````````````
  6405. ```````````````````````````````` example
  6406. ![*foo* bar][]
  6407. [*foo* bar]: /url "title"
  6408. .
  6409. <p><img src="/url" alt="foo bar" title="title" /></p>
  6410. ````````````````````````````````
  6411. The labels are case-insensitive:
  6412. ```````````````````````````````` example
  6413. ![Foo][]
  6414. [foo]: /url "title"
  6415. .
  6416. <p><img src="/url" alt="Foo" title="title" /></p>
  6417. ````````````````````````````````
  6418. As with reference links, [whitespace] is not allowed
  6419. between the two sets of brackets:
  6420. ```````````````````````````````` example
  6421. ![foo]
  6422. []
  6423. [foo]: /url "title"
  6424. .
  6425. <p><img src="/url" alt="foo" title="title" />
  6426. []</p>
  6427. ````````````````````````````````
  6428. Shortcut:
  6429. ```````````````````````````````` example
  6430. ![foo]
  6431. [foo]: /url "title"
  6432. .
  6433. <p><img src="/url" alt="foo" title="title" /></p>
  6434. ````````````````````````````````
  6435. ```````````````````````````````` example
  6436. ![*foo* bar]
  6437. [*foo* bar]: /url "title"
  6438. .
  6439. <p><img src="/url" alt="foo bar" title="title" /></p>
  6440. ````````````````````````````````
  6441. Note that link labels cannot contain unescaped brackets:
  6442. ```````````````````````````````` example
  6443. ![[foo]]
  6444. [[foo]]: /url "title"
  6445. .
  6446. <p>![[foo]]</p>
  6447. <p>[[foo]]: /url &quot;title&quot;</p>
  6448. ````````````````````````````````
  6449. The link labels are case-insensitive:
  6450. ```````````````````````````````` example
  6451. ![Foo]
  6452. [foo]: /url "title"
  6453. .
  6454. <p><img src="/url" alt="Foo" title="title" /></p>
  6455. ````````````````````````````````
  6456. If you just want a literal `!` followed by bracketed text, you can
  6457. backslash-escape the opening `[`:
  6458. ```````````````````````````````` example
  6459. !\[foo]
  6460. [foo]: /url "title"
  6461. .
  6462. <p>![foo]</p>
  6463. ````````````````````````````````
  6464. If you want a link after a literal `!`, backslash-escape the
  6465. `!`:
  6466. ```````````````````````````````` example
  6467. \![foo]
  6468. [foo]: /url "title"
  6469. .
  6470. <p>!<a href="/url" title="title">foo</a></p>
  6471. ````````````````````````````````
  6472. ## Autolinks
  6473. [Autolink](@)s are absolute URIs and email addresses inside
  6474. `<` and `>`. They are parsed as links, with the URL or email address
  6475. as the link label.
  6476. A [URI autolink](@) consists of `<`, followed by an
  6477. [absolute URI] not containing `<`, followed by `>`. It is parsed as
  6478. a link to the URI, with the URI as the link's label.
  6479. An [absolute URI](@),
  6480. for these purposes, consists of a [scheme] followed by a colon (`:`)
  6481. followed by zero or more characters other than ASCII
  6482. [whitespace] and control characters, `<`, and `>`. If
  6483. the URI includes these characters, they must be percent-encoded
  6484. (e.g. `%20` for a space).
  6485. For purposes of this spec, a [scheme](@) is any sequence
  6486. of 2--32 characters beginning with an ASCII letter and followed
  6487. by any combination of ASCII letters, digits, or the symbols plus
  6488. ("+"), period ("."), or hyphen ("-").
  6489. Here are some valid autolinks:
  6490. ```````````````````````````````` example
  6491. <http://foo.bar.baz>
  6492. .
  6493. <p><a href="http://foo.bar.baz">http://foo.bar.baz</a></p>
  6494. ````````````````````````````````
  6495. ```````````````````````````````` example
  6496. <http://foo.bar.baz/test?q=hello&id=22&boolean>
  6497. .
  6498. <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>
  6499. ````````````````````````````````
  6500. ```````````````````````````````` example
  6501. <irc://foo.bar:2233/baz>
  6502. .
  6503. <p><a href="irc://foo.bar:2233/baz">irc://foo.bar:2233/baz</a></p>
  6504. ````````````````````````````````
  6505. Uppercase is also fine:
  6506. ```````````````````````````````` example
  6507. <MAILTO:FOO@BAR.BAZ>
  6508. .
  6509. <p><a href="MAILTO:FOO@BAR.BAZ">MAILTO:FOO@BAR.BAZ</a></p>
  6510. ````````````````````````````````
  6511. Note that many strings that count as [absolute URIs] for
  6512. purposes of this spec are not valid URIs, because their
  6513. schemes are not registered or because of other problems
  6514. with their syntax:
  6515. ```````````````````````````````` example
  6516. <a+b+c:d>
  6517. .
  6518. <p><a href="a+b+c:d">a+b+c:d</a></p>
  6519. ````````````````````````````````
  6520. ```````````````````````````````` example
  6521. <made-up-scheme://foo,bar>
  6522. .
  6523. <p><a href="made-up-scheme://foo,bar">made-up-scheme://foo,bar</a></p>
  6524. ````````````````````````````````
  6525. ```````````````````````````````` example
  6526. <http://../>
  6527. .
  6528. <p><a href="http://../">http://../</a></p>
  6529. ````````````````````````````````
  6530. ```````````````````````````````` example
  6531. <localhost:5001/foo>
  6532. .
  6533. <p><a href="localhost:5001/foo">localhost:5001/foo</a></p>
  6534. ````````````````````````````````
  6535. Spaces are not allowed in autolinks:
  6536. ```````````````````````````````` example
  6537. <http://foo.bar/baz bim>
  6538. .
  6539. <p>&lt;http://foo.bar/baz bim&gt;</p>
  6540. ````````````````````````````````
  6541. Backslash-escapes do not work inside autolinks:
  6542. ```````````````````````````````` example
  6543. <http://example.com/\[\>
  6544. .
  6545. <p><a href="http://example.com/%5C%5B%5C">http://example.com/\[\</a></p>
  6546. ````````````````````````````````
  6547. An [email autolink](@)
  6548. consists of `<`, followed by an [email address],
  6549. followed by `>`. The link's label is the email address,
  6550. and the URL is `mailto:` followed by the email address.
  6551. An [email address](@),
  6552. for these purposes, is anything that matches
  6553. the [non-normative regex from the HTML5
  6554. spec](https://html.spec.whatwg.org/multipage/forms.html#e-mail-state-(type=email)):
  6555. /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  6556. (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
  6557. Examples of email autolinks:
  6558. ```````````````````````````````` example
  6559. <foo@bar.example.com>
  6560. .
  6561. <p><a href="mailto:foo@bar.example.com">foo@bar.example.com</a></p>
  6562. ````````````````````````````````
  6563. ```````````````````````````````` example
  6564. <foo+special@Bar.baz-bar0.com>
  6565. .
  6566. <p><a href="mailto:foo+special@Bar.baz-bar0.com">foo+special@Bar.baz-bar0.com</a></p>
  6567. ````````````````````````````````
  6568. Backslash-escapes do not work inside email autolinks:
  6569. ```````````````````````````````` example
  6570. <foo\+@bar.example.com>
  6571. .
  6572. <p>&lt;foo+@bar.example.com&gt;</p>
  6573. ````````````````````````````````
  6574. These are not autolinks:
  6575. ```````````````````````````````` example
  6576. <>
  6577. .
  6578. <p>&lt;&gt;</p>
  6579. ````````````````````````````````
  6580. ```````````````````````````````` example
  6581. < http://foo.bar >
  6582. .
  6583. <p>&lt; http://foo.bar &gt;</p>
  6584. ````````````````````````````````
  6585. ```````````````````````````````` example
  6586. <m:abc>
  6587. .
  6588. <p>&lt;m:abc&gt;</p>
  6589. ````````````````````````````````
  6590. ```````````````````````````````` example
  6591. <foo.bar.baz>
  6592. .
  6593. <p>&lt;foo.bar.baz&gt;</p>
  6594. ````````````````````````````````
  6595. ```````````````````````````````` example
  6596. http://example.com
  6597. .
  6598. <p>http://example.com</p>
  6599. ````````````````````````````````
  6600. ```````````````````````````````` example
  6601. foo@bar.example.com
  6602. .
  6603. <p>foo@bar.example.com</p>
  6604. ````````````````````````````````
  6605. ## Raw HTML
  6606. Text between `<` and `>` that looks like an HTML tag is parsed as a
  6607. raw HTML tag and will be rendered in HTML without escaping.
  6608. Tag and attribute names are not limited to current HTML tags,
  6609. so custom tags (and even, say, DocBook tags) may be used.
  6610. Here is the grammar for tags:
  6611. A [tag name](@) consists of an ASCII letter
  6612. followed by zero or more ASCII letters, digits, or
  6613. hyphens (`-`).
  6614. An [attribute](@) consists of [whitespace],
  6615. an [attribute name], and an optional
  6616. [attribute value specification].
  6617. An [attribute name](@)
  6618. consists of an ASCII letter, `_`, or `:`, followed by zero or more ASCII
  6619. letters, digits, `_`, `.`, `:`, or `-`. (Note: This is the XML
  6620. specification restricted to ASCII. HTML5 is laxer.)
  6621. An [attribute value specification](@)
  6622. consists of optional [whitespace],
  6623. a `=` character, optional [whitespace], and an [attribute
  6624. value].
  6625. An [attribute value](@)
  6626. consists of an [unquoted attribute value],
  6627. a [single-quoted attribute value], or a [double-quoted attribute value].
  6628. An [unquoted attribute value](@)
  6629. is a nonempty string of characters not
  6630. including [whitespace], `"`, `'`, `=`, `<`, `>`, or `` ` ``.
  6631. A [single-quoted attribute value](@)
  6632. consists of `'`, zero or more
  6633. characters not including `'`, and a final `'`.
  6634. A [double-quoted attribute value](@)
  6635. consists of `"`, zero or more
  6636. characters not including `"`, and a final `"`.
  6637. An [open tag](@) consists of a `<` character, a [tag name],
  6638. zero or more [attributes], optional [whitespace], an optional `/`
  6639. character, and a `>` character.
  6640. A [closing tag](@) consists of the string `</`, a
  6641. [tag name], optional [whitespace], and the character `>`.
  6642. An [HTML comment](@) consists of `<!--` + *text* + `-->`,
  6643. where *text* does not start with `>` or `->`, does not end with `-`,
  6644. and does not contain `--`. (See the
  6645. [HTML5 spec](http://www.w3.org/TR/html5/syntax.html#comments).)
  6646. A [processing instruction](@)
  6647. consists of the string `<?`, a string
  6648. of characters not including the string `?>`, and the string
  6649. `?>`.
  6650. A [declaration](@) consists of the
  6651. string `<!`, a name consisting of one or more uppercase ASCII letters,
  6652. [whitespace], a string of characters not including the
  6653. character `>`, and the character `>`.
  6654. A [CDATA section](@) consists of
  6655. the string `<![CDATA[`, a string of characters not including the string
  6656. `]]>`, and the string `]]>`.
  6657. An [HTML tag](@) consists of an [open tag], a [closing tag],
  6658. an [HTML comment], a [processing instruction], a [declaration],
  6659. or a [CDATA section].
  6660. Here are some simple open tags:
  6661. ```````````````````````````````` example
  6662. <a><bab><c2c>
  6663. .
  6664. <p><a><bab><c2c></p>
  6665. ````````````````````````````````
  6666. Empty elements:
  6667. ```````````````````````````````` example
  6668. <a/><b2/>
  6669. .
  6670. <p><a/><b2/></p>
  6671. ````````````````````````````````
  6672. [Whitespace] is allowed:
  6673. ```````````````````````````````` example
  6674. <a /><b2
  6675. data="foo" >
  6676. .
  6677. <p><a /><b2
  6678. data="foo" ></p>
  6679. ````````````````````````````````
  6680. With attributes:
  6681. ```````````````````````````````` example
  6682. <a foo="bar" bam = 'baz <em>"</em>'
  6683. _boolean zoop:33=zoop:33 />
  6684. .
  6685. <p><a foo="bar" bam = 'baz <em>"</em>'
  6686. _boolean zoop:33=zoop:33 /></p>
  6687. ````````````````````````````````
  6688. Custom tag names can be used:
  6689. ```````````````````````````````` example
  6690. Foo <responsive-image src="foo.jpg" />
  6691. .
  6692. <p>Foo <responsive-image src="foo.jpg" /></p>
  6693. ````````````````````````````````
  6694. Illegal tag names, not parsed as HTML:
  6695. ```````````````````````````````` example
  6696. <33> <__>
  6697. .
  6698. <p>&lt;33&gt; &lt;__&gt;</p>
  6699. ````````````````````````````````
  6700. Illegal attribute names:
  6701. ```````````````````````````````` example
  6702. <a h*#ref="hi">
  6703. .
  6704. <p>&lt;a h*#ref=&quot;hi&quot;&gt;</p>
  6705. ````````````````````````````````
  6706. Illegal attribute values:
  6707. ```````````````````````````````` example
  6708. <a href="hi'> <a href=hi'>
  6709. .
  6710. <p>&lt;a href=&quot;hi'&gt; &lt;a href=hi'&gt;</p>
  6711. ````````````````````````````````
  6712. Illegal [whitespace]:
  6713. ```````````````````````````````` example
  6714. < a><
  6715. foo><bar/ >
  6716. <foo bar=baz
  6717. bim!bop />
  6718. .
  6719. <p>&lt; a&gt;&lt;
  6720. foo&gt;&lt;bar/ &gt;
  6721. &lt;foo bar=baz
  6722. bim!bop /&gt;</p>
  6723. ````````````````````````````````
  6724. Missing [whitespace]:
  6725. ```````````````````````````````` example
  6726. <a href='bar'title=title>
  6727. .
  6728. <p>&lt;a href='bar'title=title&gt;</p>
  6729. ````````````````````````````````
  6730. Closing tags:
  6731. ```````````````````````````````` example
  6732. </a></foo >
  6733. .
  6734. <p></a></foo ></p>
  6735. ````````````````````````````````
  6736. Illegal attributes in closing tag:
  6737. ```````````````````````````````` example
  6738. </a href="foo">
  6739. .
  6740. <p>&lt;/a href=&quot;foo&quot;&gt;</p>
  6741. ````````````````````````````````
  6742. Comments:
  6743. ```````````````````````````````` example
  6744. foo <!-- this is a
  6745. comment - with hyphen -->
  6746. .
  6747. <p>foo <!-- this is a
  6748. comment - with hyphen --></p>
  6749. ````````````````````````````````
  6750. ```````````````````````````````` example
  6751. foo <!-- not a comment -- two hyphens -->
  6752. .
  6753. <p>foo &lt;!-- not a comment -- two hyphens --&gt;</p>
  6754. ````````````````````````````````
  6755. Not comments:
  6756. ```````````````````````````````` example
  6757. foo <!--> foo -->
  6758. foo <!-- foo--->
  6759. .
  6760. <p>foo &lt;!--&gt; foo --&gt;</p>
  6761. <p>foo &lt;!-- foo---&gt;</p>
  6762. ````````````````````````````````
  6763. Processing instructions:
  6764. ```````````````````````````````` example
  6765. foo <?php echo $a; ?>
  6766. .
  6767. <p>foo <?php echo $a; ?></p>
  6768. ````````````````````````````````
  6769. Declarations:
  6770. ```````````````````````````````` example
  6771. foo <!ELEMENT br EMPTY>
  6772. .
  6773. <p>foo <!ELEMENT br EMPTY></p>
  6774. ````````````````````````````````
  6775. CDATA sections:
  6776. ```````````````````````````````` example
  6777. foo <![CDATA[>&<]]>
  6778. .
  6779. <p>foo <![CDATA[>&<]]></p>
  6780. ````````````````````````````````
  6781. Entity and numeric character references are preserved in HTML
  6782. attributes:
  6783. ```````````````````````````````` example
  6784. foo <a href="&ouml;">
  6785. .
  6786. <p>foo <a href="&ouml;"></p>
  6787. ````````````````````````````````
  6788. Backslash escapes do not work in HTML attributes:
  6789. ```````````````````````````````` example
  6790. foo <a href="\*">
  6791. .
  6792. <p>foo <a href="\*"></p>
  6793. ````````````````````````````````
  6794. ```````````````````````````````` example
  6795. <a href="\"">
  6796. .
  6797. <p>&lt;a href=&quot;&quot;&quot;&gt;</p>
  6798. ````````````````````````````````
  6799. ## Hard line breaks
  6800. A line break (not in a code span or HTML tag) that is preceded
  6801. by two or more spaces and does not occur at the end of a block
  6802. is parsed as a [hard line break](@) (rendered
  6803. in HTML as a `<br />` tag):
  6804. ```````````````````````````````` example
  6805. foo
  6806. baz
  6807. .
  6808. <p>foo<br />
  6809. baz</p>
  6810. ````````````````````````````````
  6811. For a more visible alternative, a backslash before the
  6812. [line ending] may be used instead of two spaces:
  6813. ```````````````````````````````` example
  6814. foo\
  6815. baz
  6816. .
  6817. <p>foo<br />
  6818. baz</p>
  6819. ````````````````````````````````
  6820. More than two spaces can be used:
  6821. ```````````````````````````````` example
  6822. foo
  6823. baz
  6824. .
  6825. <p>foo<br />
  6826. baz</p>
  6827. ````````````````````````````````
  6828. Leading spaces at the beginning of the next line are ignored:
  6829. ```````````````````````````````` example
  6830. foo
  6831. bar
  6832. .
  6833. <p>foo<br />
  6834. bar</p>
  6835. ````````````````````````````````
  6836. ```````````````````````````````` example
  6837. foo\
  6838. bar
  6839. .
  6840. <p>foo<br />
  6841. bar</p>
  6842. ````````````````````````````````
  6843. Line breaks can occur inside emphasis, links, and other constructs
  6844. that allow inline content:
  6845. ```````````````````````````````` example
  6846. *foo
  6847. bar*
  6848. .
  6849. <p><em>foo<br />
  6850. bar</em></p>
  6851. ````````````````````````````````
  6852. ```````````````````````````````` example
  6853. *foo\
  6854. bar*
  6855. .
  6856. <p><em>foo<br />
  6857. bar</em></p>
  6858. ````````````````````````````````
  6859. Line breaks do not occur inside code spans
  6860. ```````````````````````````````` example
  6861. `code
  6862. span`
  6863. .
  6864. <p><code>code span</code></p>
  6865. ````````````````````````````````
  6866. ```````````````````````````````` example
  6867. `code\
  6868. span`
  6869. .
  6870. <p><code>code\ span</code></p>
  6871. ````````````````````````````````
  6872. or HTML tags:
  6873. ```````````````````````````````` example
  6874. <a href="foo
  6875. bar">
  6876. .
  6877. <p><a href="foo
  6878. bar"></p>
  6879. ````````````````````````````````
  6880. ```````````````````````````````` example
  6881. <a href="foo\
  6882. bar">
  6883. .
  6884. <p><a href="foo\
  6885. bar"></p>
  6886. ````````````````````````````````
  6887. Hard line breaks are for separating inline content within a block.
  6888. Neither syntax for hard line breaks works at the end of a paragraph or
  6889. other block element:
  6890. ```````````````````````````````` example
  6891. foo\
  6892. .
  6893. <p>foo\</p>
  6894. ````````````````````````````````
  6895. ```````````````````````````````` example
  6896. foo
  6897. .
  6898. <p>foo</p>
  6899. ````````````````````````````````
  6900. ```````````````````````````````` example
  6901. ### foo\
  6902. .
  6903. <h3>foo\</h3>
  6904. ````````````````````````````````
  6905. ```````````````````````````````` example
  6906. ### foo
  6907. .
  6908. <h3>foo</h3>
  6909. ````````````````````````````````
  6910. ## Soft line breaks
  6911. A regular line break (not in a code span or HTML tag) that is not
  6912. preceded by two or more spaces or a backslash is parsed as a
  6913. [softbreak](@). (A softbreak may be rendered in HTML either as a
  6914. [line ending] or as a space. The result will be the same in
  6915. browsers. In the examples here, a [line ending] will be used.)
  6916. ```````````````````````````````` example
  6917. foo
  6918. baz
  6919. .
  6920. <p>foo
  6921. baz</p>
  6922. ````````````````````````````````
  6923. Spaces at the end of the line and beginning of the next line are
  6924. removed:
  6925. ```````````````````````````````` example
  6926. foo
  6927. baz
  6928. .
  6929. <p>foo
  6930. baz</p>
  6931. ````````````````````````````````
  6932. A conforming parser may render a soft line break in HTML either as a
  6933. line break or as a space.
  6934. A renderer may also provide an option to render soft line breaks
  6935. as hard line breaks.
  6936. ## Textual content
  6937. Any characters not given an interpretation by the above rules will
  6938. be parsed as plain textual content.
  6939. ```````````````````````````````` example
  6940. hello $.;'there
  6941. .
  6942. <p>hello $.;'there</p>
  6943. ````````````````````````````````
  6944. ```````````````````````````````` example
  6945. Foo χρῆν
  6946. .
  6947. <p>Foo χρῆν</p>
  6948. ````````````````````````````````
  6949. Internal spaces are preserved verbatim:
  6950. ```````````````````````````````` example
  6951. Multiple spaces
  6952. .
  6953. <p>Multiple spaces</p>
  6954. ````````````````````````````````
  6955. <!-- END TESTS -->
  6956. # Appendix: A parsing strategy
  6957. In this appendix we describe some features of the parsing strategy
  6958. used in the CommonMark reference implementations.
  6959. ## Overview
  6960. Parsing has two phases:
  6961. 1. In the first phase, lines of input are consumed and the block
  6962. structure of the document---its division into paragraphs, block quotes,
  6963. list items, and so on---is constructed. Text is assigned to these
  6964. blocks but not parsed. Link reference definitions are parsed and a
  6965. map of links is constructed.
  6966. 2. In the second phase, the raw text contents of paragraphs and headings
  6967. are parsed into sequences of Markdown inline elements (strings,
  6968. code spans, links, emphasis, and so on), using the map of link
  6969. references constructed in phase 1.
  6970. At each point in processing, the document is represented as a tree of
  6971. **blocks**. The root of the tree is a `document` block. The `document`
  6972. may have any number of other blocks as **children**. These children
  6973. may, in turn, have other blocks as children. The last child of a block
  6974. is normally considered **open**, meaning that subsequent lines of input
  6975. can alter its contents. (Blocks that are not open are **closed**.)
  6976. Here, for example, is a possible document tree, with the open blocks
  6977. marked by arrows:
  6978. ``` tree
  6979. -> document
  6980. -> block_quote
  6981. paragraph
  6982. "Lorem ipsum dolor\nsit amet."
  6983. -> list (type=bullet tight=true bullet_char=-)
  6984. list_item
  6985. paragraph
  6986. "Qui *quodsi iracundia*"
  6987. -> list_item
  6988. -> paragraph
  6989. "aliquando id"
  6990. ```
  6991. ## Phase 1: block structure
  6992. Each line that is processed has an effect on this tree. The line is
  6993. analyzed and, depending on its contents, the document may be altered
  6994. in one or more of the following ways:
  6995. 1. One or more open blocks may be closed.
  6996. 2. One or more new blocks may be created as children of the
  6997. last open block.
  6998. 3. Text may be added to the last (deepest) open block remaining
  6999. on the tree.
  7000. Once a line has been incorporated into the tree in this way,
  7001. it can be discarded, so input can be read in a stream.
  7002. For each line, we follow this procedure:
  7003. 1. First we iterate through the open blocks, starting with the
  7004. root document, and descending through last children down to the last
  7005. open block. Each block imposes a condition that the line must satisfy
  7006. if the block is to remain open. For example, a block quote requires a
  7007. `>` character. A paragraph requires a non-blank line.
  7008. In this phase we may match all or just some of the open
  7009. blocks. But we cannot close unmatched blocks yet, because we may have a
  7010. [lazy continuation line].
  7011. 2. Next, after consuming the continuation markers for existing
  7012. blocks, we look for new block starts (e.g. `>` for a block quote).
  7013. If we encounter a new block start, we close any blocks unmatched
  7014. in step 1 before creating the new block as a child of the last
  7015. matched block.
  7016. 3. Finally, we look at the remainder of the line (after block
  7017. markers like `>`, list markers, and indentation have been consumed).
  7018. This is text that can be incorporated into the last open
  7019. block (a paragraph, code block, heading, or raw HTML).
  7020. Setext headings are formed when we see a line of a paragraph
  7021. that is a [setext heading underline].
  7022. Reference link definitions are detected when a paragraph is closed;
  7023. the accumulated text lines are parsed to see if they begin with
  7024. one or more reference link definitions. Any remainder becomes a
  7025. normal paragraph.
  7026. We can see how this works by considering how the tree above is
  7027. generated by four lines of Markdown:
  7028. ``` markdown
  7029. > Lorem ipsum dolor
  7030. sit amet.
  7031. > - Qui *quodsi iracundia*
  7032. > - aliquando id
  7033. ```
  7034. At the outset, our document model is just
  7035. ``` tree
  7036. -> document
  7037. ```
  7038. The first line of our text,
  7039. ``` markdown
  7040. > Lorem ipsum dolor
  7041. ```
  7042. causes a `block_quote` block to be created as a child of our
  7043. open `document` block, and a `paragraph` block as a child of
  7044. the `block_quote`. Then the text is added to the last open
  7045. block, the `paragraph`:
  7046. ``` tree
  7047. -> document
  7048. -> block_quote
  7049. -> paragraph
  7050. "Lorem ipsum dolor"
  7051. ```
  7052. The next line,
  7053. ``` markdown
  7054. sit amet.
  7055. ```
  7056. is a "lazy continuation" of the open `paragraph`, so it gets added
  7057. to the paragraph's text:
  7058. ``` tree
  7059. -> document
  7060. -> block_quote
  7061. -> paragraph
  7062. "Lorem ipsum dolor\nsit amet."
  7063. ```
  7064. The third line,
  7065. ``` markdown
  7066. > - Qui *quodsi iracundia*
  7067. ```
  7068. causes the `paragraph` block to be closed, and a new `list` block
  7069. opened as a child of the `block_quote`. A `list_item` is also
  7070. added as a child of the `list`, and a `paragraph` as a child of
  7071. the `list_item`. The text is then added to the new `paragraph`:
  7072. ``` tree
  7073. -> document
  7074. -> block_quote
  7075. paragraph
  7076. "Lorem ipsum dolor\nsit amet."
  7077. -> list (type=bullet tight=true bullet_char=-)
  7078. -> list_item
  7079. -> paragraph
  7080. "Qui *quodsi iracundia*"
  7081. ```
  7082. The fourth line,
  7083. ``` markdown
  7084. > - aliquando id
  7085. ```
  7086. causes the `list_item` (and its child the `paragraph`) to be closed,
  7087. and a new `list_item` opened up as child of the `list`. A `paragraph`
  7088. is added as a child of the new `list_item`, to contain the text.
  7089. We thus obtain the final tree:
  7090. ``` tree
  7091. -> document
  7092. -> block_quote
  7093. paragraph
  7094. "Lorem ipsum dolor\nsit amet."
  7095. -> list (type=bullet tight=true bullet_char=-)
  7096. list_item
  7097. paragraph
  7098. "Qui *quodsi iracundia*"
  7099. -> list_item
  7100. -> paragraph
  7101. "aliquando id"
  7102. ```
  7103. ## Phase 2: inline structure
  7104. Once all of the input has been parsed, all open blocks are closed.
  7105. We then "walk the tree," visiting every node, and parse raw
  7106. string contents of paragraphs and headings as inlines. At this
  7107. point we have seen all the link reference definitions, so we can
  7108. resolve reference links as we go.
  7109. ``` tree
  7110. document
  7111. block_quote
  7112. paragraph
  7113. str "Lorem ipsum dolor"
  7114. softbreak
  7115. str "sit amet."
  7116. list (type=bullet tight=true bullet_char=-)
  7117. list_item
  7118. paragraph
  7119. str "Qui "
  7120. emph
  7121. str "quodsi iracundia"
  7122. list_item
  7123. paragraph
  7124. str "aliquando id"
  7125. ```
  7126. Notice how the [line ending] in the first paragraph has
  7127. been parsed as a `softbreak`, and the asterisks in the first list item
  7128. have become an `emph`.
  7129. ### An algorithm for parsing nested emphasis and links
  7130. By far the trickiest part of inline parsing is handling emphasis,
  7131. strong emphasis, links, and images. This is done using the following
  7132. algorithm.
  7133. When we're parsing inlines and we hit either
  7134. - a run of `*` or `_` characters, or
  7135. - a `[` or `![`
  7136. we insert a text node with these symbols as its literal content, and we
  7137. add a pointer to this text node to the [delimiter stack](@).
  7138. The [delimiter stack] is a doubly linked list. Each
  7139. element contains a pointer to a text node, plus information about
  7140. - the type of delimiter (`[`, `![`, `*`, `_`)
  7141. - the number of delimiters,
  7142. - whether the delimiter is "active" (all are active to start), and
  7143. - whether the delimiter is a potential opener, a potential closer,
  7144. or both (which depends on what sort of characters precede
  7145. and follow the delimiters).
  7146. When we hit a `]` character, we call the *look for link or image*
  7147. procedure (see below).
  7148. When we hit the end of the input, we call the *process emphasis*
  7149. procedure (see below), with `stack_bottom` = NULL.
  7150. #### *look for link or image*
  7151. Starting at the top of the delimiter stack, we look backwards
  7152. through the stack for an opening `[` or `![` delimiter.
  7153. - If we don't find one, we return a literal text node `]`.
  7154. - If we do find one, but it's not *active*, we remove the inactive
  7155. delimiter from the stack, and return a literal text node `]`.
  7156. - If we find one and it's active, then we parse ahead to see if
  7157. we have an inline link/image, reference link/image, compact reference
  7158. link/image, or shortcut reference link/image.
  7159. + If we don't, then we remove the opening delimiter from the
  7160. delimiter stack and return a literal text node `]`.
  7161. + If we do, then
  7162. * We return a link or image node whose children are the inlines
  7163. after the text node pointed to by the opening delimiter.
  7164. * We run *process emphasis* on these inlines, with the `[` opener
  7165. as `stack_bottom`.
  7166. * We remove the opening delimiter.
  7167. * If we have a link (and not an image), we also set all
  7168. `[` delimiters before the opening delimiter to *inactive*. (This
  7169. will prevent us from getting links within links.)
  7170. #### *process emphasis*
  7171. Parameter `stack_bottom` sets a lower bound to how far we
  7172. descend in the [delimiter stack]. If it is NULL, we can
  7173. go all the way to the bottom. Otherwise, we stop before
  7174. visiting `stack_bottom`.
  7175. Let `current_position` point to the element on the [delimiter stack]
  7176. just above `stack_bottom` (or the first element if `stack_bottom`
  7177. is NULL).
  7178. We keep track of the `openers_bottom` for each delimiter
  7179. type (`*`, `_`) and each length of the closing delimiter run
  7180. (modulo 3). Initialize this to `stack_bottom`.
  7181. Then we repeat the following until we run out of potential
  7182. closers:
  7183. - Move `current_position` forward in the delimiter stack (if needed)
  7184. until we find the first potential closer with delimiter `*` or `_`.
  7185. (This will be the potential closer closest
  7186. to the beginning of the input -- the first one in parse order.)
  7187. - Now, look back in the stack (staying above `stack_bottom` and
  7188. the `openers_bottom` for this delimiter type) for the
  7189. first matching potential opener ("matching" means same delimiter).
  7190. - If one is found:
  7191. + Figure out whether we have emphasis or strong emphasis:
  7192. if both closer and opener spans have length >= 2, we have
  7193. strong, otherwise regular.
  7194. + Insert an emph or strong emph node accordingly, after
  7195. the text node corresponding to the opener.
  7196. + Remove any delimiters between the opener and closer from
  7197. the delimiter stack.
  7198. + Remove 1 (for regular emph) or 2 (for strong emph) delimiters
  7199. from the opening and closing text nodes. If they become empty
  7200. as a result, remove them and remove the corresponding element
  7201. of the delimiter stack. If the closing node is removed, reset
  7202. `current_position` to the next element in the stack.
  7203. - If none is found:
  7204. + Set `openers_bottom` to the element before `current_position`.
  7205. (We know that there are no openers for this kind of closer up to and
  7206. including this point, so this puts a lower bound on future searches.)
  7207. + If the closer at `current_position` is not a potential opener,
  7208. remove it from the delimiter stack (since we know it can't
  7209. be a closer either).
  7210. + Advance `current_position` to the next element in the stack.
  7211. After we're done, we remove all delimiters above `stack_bottom` from the
  7212. delimiter stack.